[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[TRNSYS-users] TRNSYS + MATLAB ErrorCode



Dear All,

I am very new in TRNSYS.  What I am planning to do is I want to couple
between Simulink and TRNSYS . As I know there is the way to couple
mfile and TRNSYS. but i didnt found yet how to couple  .mdl file and
trnsys.

Initially,  I try to create on_off controller by modifying example
mfile and type155 and I wanted to change original on_off in
trnsys SDHW however have error ..

" the m file has flaged an error . Return value for mfileErrorcode :101
the error occurde at info(7)=0,info(13)=0."

..and I dont know how to solve it...

 I am glad if somebody have idea to help me to solve the problem..

 I am sending the m-file and project-file in attachment.

Thanks a lot in advance,

Best regards,

Hanani
 % On_Off Controller
% ----------------------------------------------------------------------------------------------------------------------
%
% Example M-file called by TRNSYS Type 155
%
% Data passed from / to TRNSYS 
% ---------------------------- 
%
% trnTime (1x1)        : simulation time 
% trnInfo (15x1)       : TRNSYS info array
% trnInputs (nIx1)     : TRNSYS inputs 
% trnStartTime (1x1)   : TRNSYS Simulation Start time
% trnStopTime (1x1)    : TRNSYS Simulation Stop time
% trnTimeStep (1x1)    : TRNSYS Simulation time step
% mFileErrorCode (1x1) : Error code for this m-file. It is set to 1 by TRNSYS and the m-file should set it to 0 at the
%                        end to indicate that the call was successful. Any non-zero value will stop the simulation
% trnOutputs (nOx1)    : TRNSYS outputs  
%
% 
% Notes: 
% ------
% 
% You can use the values of trnInfo(7), trnInfo(8) and trnInfo(13) to identify the call (e.g. first iteration, etc.)
% Real-time controllers (callingMode = 10) will only be called once per time step with trnInfo(13) = 1 (after convergence)
% 
% The number of inputs is given by trnInfo(3)
% The number of expected outputs is given by trnInfo(6)
% WARNING: if multiple units of Type 155 are used, the variables passed from/to TRNSYS will be sized according to  
%          the maximum required by all units. You should cope with that by only using the part of the arrays that is 
%          really used by the current m-File. Example: use "nI = trnInfo(3); myInputs = trnInputs(1:nI);" 
%                                                      rather than "MyInputs = trnInputs;" 
%          Please also note that all m-files share the same workspace in Matlab (they are "scripts", not "functions") so
%          variables like trnInfo, trnTime, etc. will be overwritten at each call. 
%
% ----------------------------------------------------------------------------------------------------------------------
% This example implements a very simple component that returns the double of each input as the corresponding output.
% The component is iterative (should be called at each TRNSYS call).

%combine with 
% This example implements a very simple solar collector model. The component is iterative (should be called at each
% TRNSYS call)
%
% MKu, March 2006 % Editted by Hanani 2009
% -------------------------------------------------------------------------
% ---------------------------------------------
%
% trnInputs
% ---------
%
% trnInputs(1) : Th,Upper Input Temperature
% trnInputs(2) : Tl,Lower Input Temperature
% trnInputs(3) : Tin,Monitoring Temperature
% trnInputs(4) : I,Input Control function
% trnInputs(5) : dTU,Upper Dead Band
% trnInputs(6) : dTL,Lower Dead Band
%
% trnOutputs
% ----------
%
% trnOutputs(1) : O,Output Control function

%
%Hanani 2009

% TRNSYS sets mFileErrorCode = 1 at the beginning of the M-File for error detection
% This file increments mFileErrorCode at different places. If an error occurs in the m-file the last succesful step will
% be indicated by mFileErrorCode, which is displayed in the TRNSYS error message
% At the very end, the m-file sets mFileErrorCode to 0 to indicate that everything was OK

mFileErrorCode = 100;    % Beginning of the m-file 

% --- Process Inputs and global parameters -----------------------------------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------

nI = trnInfo(3);
nO = trnInfo(6);

if (nI ~= nO)
    mFileErrorCode = 101; % mFileErrorCode = 101 in the list file will indicate that this component requires nI = nO
    return
end

MyInputs = trnInputs(1:nI); % In case of multiple units the size of trnInputs will be the largest required size for all units

mFileErrorCode = 110;    % After processing inputs


% --- Process Inputs ---------------------------------------------------------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------

Th   = trnInputs(1);
Tl   = trnInputs(2);
Tin  = trnInputs(3);
I    = trnInputs(4);
dTU  = trnInputs(5);
dTL  = trnInputs(6);

mFileErrorCode = 120;    % After processing inputs



% --- First call of the simulation: initial time step (no iterations) --------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------
% (note that Matlab is initialized before this at the info(7) = -1 call, but the m-file is not called)

if ( (trnInfo(7) == 0) & (trnTime-trnStartTime < 1e-6) )  
    
    % Do some initialization stuff, e.g. initialize history of the variables for plotting at the end of the simulation
    % (uncomment lines if you wish to store variables) 
    % nTimeSteps = (trnStopTime-trnStartTime)/trnTimeStep + 1;
    % history.inputs = zeros(nTimeSteps,nI);



    % No return, normal calculations are also performed during this call
    mFileErrorCode = 120    % After initialization call
    
end

% --- Very last call of the simulation (after the user clicks "OK") ----------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------

if ( trnInfo(8) == -1 )

    mFileErrorCode = 1000;
    
    % Do stuff at the end of the simulation, e.g. calculate stats, draw plots, etc... 
        
    mFileErrorCode = 0; % Tell TRNSYS that we reached the end of the m-file without errors
    return

end

% --- Post convergence calls: store values -----------------------------------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------

if (trnInfo(13) == 1)
    
    mFileErrorCode = 200;   % Beginning of a post-convergence call 

    % This is the extra call that indicates that all Units have converged. You should do things like: 
    % - calculate control signal that should be applied at next time step
    % - Store history of variables

    % history.inputs(iStep) = MyInput; 

    % Note: If Calling Mode is set to 10, Matlab will not be called during iterative calls.
    % In that case only this loop will be executed and things like incrementing the "iStep" counter should be done here
    
    mFileErrorCode = 0; % Tell TRNSYS that we reached the end of the m-file without errors
    return  % Do not update outputs at this call

end


% --- All iterative calls ----------------------------------------------------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------

% --- If this is a first call in the time step, do things like incrementing step counters ---

if ( trnInfo(7) == 0 )

    mFileErrorCode = 130;   % Beginning of iterative call
    % Nothing to do here
    
end

% --- Process Inputs ---

mFileErrorCode = 140;   % Beginning of iterative call



% --- Set outputs ---

mFileErrorCode = 150;   % Beginning of setting outputs
trnOutputs(1:nO) = MyResults;

mFileErrorCode = 0; % Tell TRNSYS that we reached the end of the m-file without errors
return



% --- All iterative calls ----------------------------------------------------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------

% --- If this is a first call in the time step, increment counter ---

if ( trnInfo(7) == 0 )
    nStep = nStep+1;
end

% --- Get TRNSYS Inputs ---

nI = trnInfo(3);     % For bookkeeping
nO = trnInfo(6);   % For bookkeeping

Th   = trnInputs(1);
Tl   = trnInputs(2);
Tin  = trnInputs(3);
I    = trnInputs(4);
dTU  = trnInputs(5);
dTL  = trnInputs(6);

mFileErrorCode = 150;   % After reading inputs 

% --- Calculate on Off controller performance ---
error = (trnInputs(1)-trnInputs(2));
On= 1;
Off=0;

    if (On)
        
        (error >= trnInputs(6))
        
            trnOutputs(1)=1;
    else

            trnOutputs(1) =0;

    end

    if (Off)
        (error >= trnInputs(5))

            trnOutputs(1)=1;
    else

            trnOutputs(1) =0;

    end
    
 OC= trnOutputs(1) ;


% --- Set outputs ---

trnOutputs(1) = OC;


mFileErrorCode = 0; % Tell TRNSYS that we reached the end of the m-file without errors
return



Attachment: Project6.tpf
Description: Binary data