Return function doesn't work after a while

Good afternoon everybody,
I have an issue with one of my Matlab script. I will try to explain clearly my problem. I've linked the software TRNSYS with Matlab. Basically, TRNSYS calls a Matlab script at every timestep. When I wrote the Matlab code, I've used a small simulation time (only 48 hours) in order to check that everything was working. And it was working without any problem.
However, when I increases the simulation time, the simulation blocked after many hours (the number of hours depends on the starting time of my simulation, but I don't find any logic. Sometimes it stops working after 50 hours, sometimes after 600...). I've used the dbstop function to understand the problem, and I think that the problem comes from the "return function" at the end. Indeed, during the first hours, the return function works without any problem: at each time that the program reaches the return, the timestep of my simulation increases. However, after a while, when the program reaches the return, the program come back to the beginning of the matlab program without increasing the TRNSYS simulation timestep.
Since the return is not dependent of any other value (from my knowledge), I really don't understand how it may work perfectly at the beginning and stop working after. Do you have any idea for me of what it may come from ?
Thanks a lot for your time and your help ! Hélène

3 Commenti

Hélène - can you tell us more about what your function does? Do you increment a counter indicating which tilmestep you are at? How often do you call your function? Are you keeping track of an array of data which gets updated at each call to your function?
You may want to provide a small sample of your code so that we can get a better idea of what is happening.
Thanks a lot for your response. You can find on this message all my code. Since I am not sure of what is relevant, I prefer give you everything.
To summarize what my code is doing: I receive some information from my TRNSYS program at every timestep (my Inputs). You can find the timestep iStep in the next that is the hour of my simulation in TRNSYS. As an example, if I start my simulation at 744 hours in TRNSYS to not consider the first month, iStep will be 744 (even if I am not sure why...). On the contrary, TimeStep will be the number of the timestep (always 1 at the beginning). I do some calculations from these values. Some matrix are updates at every time step (as E1_mod and E2_mod). Finally, my code sent to TRNSYS some information. The "return" that I mentioned in my previous message is the last word of the code.
I am sorry, I am aware that my code is not easy to read for other people than me... I hope it will be ok.
Again, thanks a lot for your time and help !
% Type155_CallingMatlab.m
% ----------------------------------------------------------------------------------------------------------------------
%
% 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.
% "Local" variables like iCall, iStep in this example will also be shared by all units
% (i.e. they should be given a different name in each m-File if required)
%
% ----------------------------------------------------------------------------------------------------------------------
% This example implements a very simple component. The component is iterative (should be called at each TRNSYS call)
%
%
% MKu, October 2004
% ----------------------------------------------------------------------------------------------------------------------
% 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 = 1000; % Beginning of the m-file
% --- Process Inputs and global parameters -----------------------------------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------
nI = trnInfo(3); % number of inputs: time step - hour - half day solar radiation amount - half day min T
nO = trnInfo(6); %number of outputs
% MyInput = trnInputs(1);
Hour=trnInputs(1);
% Feedback
Tin(1)=trnInputs(2);
Tin(2)=trnInputs(3);
Tin(3)=trnInputs(4);
Tin(4)=trnInputs(5);
Tin(5)=trnInputs(6);
Tin(6)=trnInputs(7);
Tsurf(1)=trnInputs(8);
Tsurf(2)=trnInputs(9);
Tsurf(3)=trnInputs(10);
Tsurf(4)=trnInputs(11);
Tsurf(5)=trnInputs(12);
Tsurf(6)=trnInputs(13);
Dayoftheyear=trnInputs(14);
SimulTimeStep=trnInputs(15);
mFileErrorCode = 900; % 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) )
% This is the first call (Counter will be incremented later for this very first call)
iCall = 0;
% This is the first time step
iStep = 1;
% 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);
Day=0;
mFileErrorCode = 910;
%Predictions
Data1=dlmread('C:\Users\Helene\Dropbox\Master\Model\Models finaux\6-Learning - No basement\SolarPred_NovJan_amount.txt','\t',0,0);
Data2=dlmread('C:\Users\Helene\Dropbox\Master\Model\Models finaux\6-Learning - No basement\TemperaturePred_NovJan.txt','\t',0,0);
NbreData=62;
TimeStep=1;
T_category=14; %Number of temperature categories
S_category=3; %Number of solar categories
mFileErrorCode = 920;
min_T_1=zeros(NbreData,1);
min_T_2=zeros(NbreData,1);
for n=1:NbreData
Morning_solar_amount(n,1)=sum(Data1(n,1:12));
Afternoon_solar_amount(n,1)=sum(Data1(n,13:24));
MinT1(n,1)=min(Data2(n,1:12));
MinT2(n,1)=min(Data2(n,13:24));
i=1;
while min_T_1(n,1)==0
if i<T_category
if MinT1(n,1)<(-40+i*5)
min_T_1(n,1)=i;
end
i=i+1;
else min_T_1(n,1)=T_category;
end
end
i2=1;
while min_T_2(n,1)==0
if i2<T_category
if MinT2(n,1)<(-40+i2*5)
min_T_2(n,1)=i2;
end
i2=i2+1;
else min_T_2(n,1)=T_category;
end
end
end
mFileErrorCode = 930;
Room=6;
E1_mod=1000*ones(T_category, S_category, Room, nTimeSteps);
E2_mod=1000*ones(T_category, S_category, Room, nTimeSteps);
SimTimeStep=zeros(nTimeSteps,1);
mFileErrorCode = 931;
Ini_con_morning=40*8; %Initial consumption for the morning in Wh/m2
Ini_con_afternoon=0; %Initial consumption for the afternoon
mFileErrorCode = 932;
E1_ini=Ini_con_morning*ones(T_category, S_category,Room);
E2_ini=Ini_con_afternoon*ones(T_category, S_category,Room);
mFileErrorCode = 933;
Power=zeros(6,1); %Power that will be sent at every time step
mFileErrorCode = 940;
Start_time_1=zeros(Room,1);
Start_time_2=zeros(Room,1);
Stop_time_1=zeros(Room,1);
Stop_time_2=zeros(Room,1);
%Building analysis
Tin_lastday=ones(6,288)*21;
Power_applied=zeros(6,288);
un_coef=0.8; %Coefficient i.e. un_coef % of the values in E_mod should be lower of the chosen value
% No return, normal calculations are also performed during this call
mFileErrorCode = 800; % After initialization call
end
% --- Very last call of the simulation (after the user clicks "OK") ----------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------
if ( trnInfo(8) == -1 )
mFileErrorCode = 700;
% 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 = 600; % 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
for r=1:6
Power_applied(r,mod(iStep,288)+1)=Power(r,1);
end
% 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 ----------------------------------------------------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------
% --- Process Inputs ---
mFileErrorCode = 190; % Beginning of iterative call
% Do calculations here
%Store indoor temperature values
for r=1:6
Tin_lastday(r,mod(iStep,288)+1)=Tin(r);
end
mFileErrorCode = 180;
iStep = iStep+1;
TimeStep=TimeStep+1;
if mod(iStep,288)==0 %high-level controller %If simulation starts at 22h
Day=Day+1;
S1=Solar_rad_learning(Morning_solar_amount(Day,1),S_category);
S2=Solar_rad_learning(Afternoon_solar_amount(Day,1),S_category);
T1=min_T_1(Day,1);
T2=min_T_2(Day,1);
mFileErrorCode = 170;
%Building Analysis
for r=1:6
Tmax_1(r,1)=max(Tin_lastday(r,1:144));
Tmax_2(r,1)=max(Tin_lastday(r,145:288));
Tmin_1(r,1)=min(Tin_lastday(r,1:144));
Tmin_2(r,1)=min(Tin_lastday(r,145:288));
Consumption_applied_1(r,1)=sum(Power_applied(r,1:144)*0.08333);
Consumption_applied_2(r,1)=sum(Power_applied(r,145:288)*0.08333);
if Tmax_1(r,1)>23
Emod_1(T1,S1,r,TimeStep)=Consumption_applied_1(r,1)*(1-un_coef);
if Tmin_2(r,1)<20
Emod_2(T2,S2,r,TimeStep)=Consumption_applied_2(r,1)*(1+un_coef);
else
if Tmax_2(r,1)>23
Emod_2(T2,S2,r,TimeStep)=Consumption_applied_2(r,1)*(1-un_coef);
else
Emod_2(T2,S2,r,TimeStep)=Consumption_applied_2(r,1);
end
end
else
if Tmin_1(r,1)<20
Emod_1(T1,S1,r,TimeStep)=Consumption_applied_1(r,1)*(1+un_coef);
Emod_2(T2,S2,r,TimeStep)=Consumption_applied_2(r,1);
else
Emod_1(T1,S1,r,TimeStep)=Consumption_applied_1(r,1);
if Tmin_2(r,1)<20
if Tmax_1(r,1)>22
Emod_1(T1,S1,r,TimeStep)=Consumption_applied_1(r,1)*(1+un_coef); %check pour 2
else Emod_2(T2,S2,r,TimeStep)=Consumption_applied_2(r,1)*(1+un_coef);
end
else
if Tmax_2(r,1)>23
Emod_2(T2,S2,r,TimeStep)=Consumption_applied_2(r,1)*(1-un_coef);
else Emod_2(T2,S2,r,TimeStep)=Consumption_applied_2(r,1);
end
end
end
end
end
mFileErrorCode = 160;
%Update of the initialization matrix
for r=1:6
for t=1:T_category %For the morning update
for s=1:S_category
if T1>t && s==S1
if E1_mod(T1,S1,r,TimeStep)>E1_ini(t,S1,r)
E1_ini(t,S1,r)=E1_mod(T1,S1,r, iStep);
end
end
if T1<t && s==S1
if E1_mod(T1,S1,r,TimeStep)<E1_ini(t,S1,r)
E1_ini(t,S1,r)=E1_mod(T1,S1,r,iStep);
end
end
if S1>s && t==T1
if E1_mod(T1,S1,r,TimeStep)>E1_ini(T1,s,r)
E1_ini(T1,s,r)=E1_mod(T1,S1,r,iStep);
end
end
if S1<s && t==T1
if E1_mod(T1,S1,r,TimeStep)<E1_ini(T1,s,r)
E1_ini(T1,s,r)=E1_mod(T1,S1,r,iStep);
end
end
end
end
end
mFileErrorCode = 150;
for r=1:6
for t=1:T_category %For the afternoon update
for s=1:S_category
if T2>t && s==S2
if E2_mod(T2,S2,r,TimeStep)>E2_ini(t,S2,r)
E2_ini(t,S2,r)=E2_mod(T2,S2,r,iStep);
end
end
if T2<t && s==S2
if E2_mod(T2,S2,r,TimeStep)<E2_ini(t,S2,r)
E2_ini(t,S2,r)=E2_mod(T2,S2,r,iStep);
end
end
if S2>s && t==T2
if E2_mod(T2,S2,r,TimeStep)>E2_ini(T2,s,r)
E2_ini(T2,s,r)=E2_mod(T2,S2,r,iStep);
end
end
if S2<s && t==T2
if E2_mod(T2,S2,r,TimeStep)<E2_ini(T2,s,r)
E2_ini(T2,s,r)=E2_mod(T2,S2,r,iStep);
end
end
end
end
end
mFileErrorCode = 140;
%Choice of the output value
for r=1:6
if min(E1_mod(T1,S1,r,:))==1000
E_1(r,1)=E1_ini(T1,S1,r);
else
Array_E1_mod=sort(E1_mod(T1,S1,r,:));
Index_1=find(Array_E1_mod==1000, 1);
Reduced_Array_E1_mod=Array_E1_mod(1,1:Index_1);
E_1(r,1)=Reduced_Array_E1_mod(1,floor(size(Reduced_Array_E1_mod,2)*un_coef));
end
if min(E2_mod(T2,S2,r,:))==1000
E_2(r,1)=E2_ini(T2,S2,r);
else
Array_E2_mod=sort(E2_mod(T2,S2,r,:));
Index_2=find(Array_E2_mod==1000, 1);
Reduced_Array_E2_mod=Array_E2_mod(1,1:Index_2);
E_2(r,1)=Reduced_Array_E2_mod(1,floor(size(Reduced_Array_E2_mod,2)*un_coef));
end
end
mFileErrorCode = 130;
%Load Management
for r=1:6
Power_1(r,1)=80; %W/m2
D_1(r,1)=E_1(r,1)/Power_1(r,1);
Power_2(r,1)=40; %W/m2
D_2(r,1)=E_2(r,1)/Power_2(r,1);
if D_1(r,1)>8
D_1(r,1)=8;
Power_1(r,1)=E_1(r,1)/D_1(r,1);
if Power_1(r,1)>120
Power_1(r,1)=120;
D_2(r,1)=E_2(r,1)+E_1(r,1)-(Power_1(r,1)*D_1(r,1));
end
end
if D_2(r,1)>6
D_2(r,1)=6;
if Power_1(r,1)<120
D_1(r,1)=E_2(r,1)+E_1(r,1)-(Power_2(r,1)*D_2(r,1));
else
Power_2(r,1)=E_2(r,1)/D_2(r,1);
if Power_2(r,1)>120
Power_2(r,1)=120;
end
end
end
if D_1(r,1)>8
D_1(r,1)=8;
Power_1(r,1)=E_1(r,1)/D_1(r,1);
if Power_1(r,1)>120
Power_1(r,1)=120;
D_2(r,1)=E_2(r,1)+E_1(r,1)-(Power_1(r,1)*D_1(r,1));
end
end
if D_2(r,1)>6
D_2(r,1)=6;
end
if D_1(r,1)<=4
Start_time_1(r,1)=2;
Stop_time_1(r,1)=2+D_1(r,1);
else
if D_1(r,1)>6
Start_time_1(r,1)=24-(D_1(r,1)-6);
else
Start_time_1(r,1)=6-D_1(r,1);
Stop_time_1(r,1)=6;
end
end
Start_time_2(r,1)=16-D_2(r,1);
Stop_time_2(r,1)=16;
end
end
mFileErrorCode = 120;
%low-level controller
for r=1:6
Start_time_1(r,1)
Stop_time_1(r,1)
if and((Start_time_1(r,1)<Hour),(Hour<Stop_time_1(r,1)))
if Tin(r)<20
Power(r,1)=max(Power_1(r,1),60);
else Power(r,1)=Power_1(r,1);
end
else Power(r,1)=0;
end
if (Start_time_2(r,1)<Hour) && (Hour<Stop_time_2(r,1))
if Tin(r)<20
Power(r,1)=max(Power_2(r,1),60);
else Power(r,1)=Power_2(r,1);
end
else Power(r,1)=0;
end
if Tin(r)<20
if Power(r,1)<60
Power(r,1)=60;
end
end
if Tin(r)>23
Power(r,1)=0;
elseif Tsurf(r)>28
Power(r,1)=0;
end
end
mFileErrorCode = 110;
% --- Set outputs ---
trnOutputs(1) = Power(1,1);
trnOutputs(2) = Power(2,1);
trnOutputs(3) = Power(3,1);
trnOutputs(4) = Power(4,1);
trnOutputs(5) = Power(5,1);
trnOutputs(6) = Power(6,1);
mFileErrorCode = 0; % Tell TRNSYS that we reached the end of the m-file without errors
return
Hello, I am going to perform whole building optimization using heuristic algorithm in matlab and Transys. do you know how to connect matlab and transys? Thank you so much

Accedi per commentare.

Risposte (2)

I can guarantee, that return is not the problem. I promise this.
You wrote:
However, after a while, when the program reaches the return, the program come back
to the beginning of the matlab program without increasing the TRNSYS simulation timestep.
Then this is a problem in TRNSYS? The shown script reaches the final return, but the calling function does not proceed as expected?

1 Commento

Thanks for your help ! Yes exactly. As an example, during the 50 first timestep, at each time step, I will see that TRNSYS calls Matlab correctly, Matlab read the script, and when it reaches the return, I can see that TRNSYS goes to the next time step, and the Matlab script is read again from the beginning. But the 51st time, when the program reaches the return, Matlab goes back to the beginning of the program, but apparently TRNSYS doesn't have the information that it has to go to the next time step. Therefore, matlab continue to calculate, but TRNSYS is blocked. That's why I was thinking that it was the return function.
Do you have any idea of what may be the problem ?
If it may help, as function of the start time that I chose (trnStartTime in my code), the problem doesn't appear at the same time step.

Accedi per commentare.

Josef
Josef il 9 Feb 2018
Modificato: Josef il 9 Feb 2018
We also have this problem an we changed "Calling Mode" in Tab "Parameter" of the TRNSYS Type 155 from "0" (Standard iterative component) to "10" (Non-iterative component). According to the TRNSYS Programmers Guide this is suitable for controllers. And for us it worked so far we can see until now. We also implement some control algorithms to control energy systems for longterm simulation of at least one year. As I've seen from your code this is nearly the same. So have you already tried this?

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Prodotti

Richiesto:

il 30 Gen 2017

Commentato:

il 3 Giu 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by