TRNSYS与MATLAB联合仿真

1、对于不知道该怎样来进行TRNSYS与MATLAB仿真和matlab程序编程的,可以参考TRNSYS安装文件夹里的例子。

这个例子说明了TRNSYS与MATLAB的连接,基于SDHW示例。使用Matlab的一个组件,实现太阳能集热器模型,并在仿真最后绘制效率曲线。

2、MATLAB代码块

写matlab代码时,只需要把输入和输出,还有要写的代码的核心或计算部分改成自己要写的就行。

其他的不需要改动。

% SolarCollector.m
% ----------------------------------------------------------------------------------------------------------------------
%
% Simple first-order solar collector model (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 solar collector model. The component is iterative (should be called at each
% TRNSYS call)
%
% trnInputs
% ---------
%
% trnInputs(1) : Ti, collector inlet temperature
% trnInputs(2) : mdot, collector flowrate
% trnInputs(3) : Tamb , ambient temperature
% trnInputs(4) : Gt, solar radiation in the collector plane
%
% trnOutputs
% ----------
%
% trnOutputs(1) : To, collector outlet temperature
% trnOutputs(2) : mdot, collector flowrate
% trnOutputs(3) : Quseful, useful energy gain
%
% 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 = 100    % Beginning of the m-file 

% --- Solar collector parameters----------------------------------------------------------------------------------------
% ----------------------------------------------------------------------------------------------------------------------

% Efficiency: 
% eta = qUseful / (Gt A) = mdot Cp (To - Ti) / (Gt A) = eta0 - eta1 * Gt / (To-Tamb)
% can be rewritten as:
% To = (eta0*Gt+mdot*Cp/A*Ti+eta1*Tamb) / (mdot*Cp/Aa+eta1)

% 改成自己的参数
% Collector area [m^2]
A = 5;
% Intercept efficiency [-]
eta0 = 0.8;
% Negative First order loss coefficient [kJ/h-m^2-K]
eta1 = 15;
% Specific heat [kJ/kg-K]
Cp = 4.19;

mFileErrorCode = 110    % After setting parameters


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

% 输入改成自己的输入
Ti   = trnInputs(1);
mdot = trnInputs(2);
Tamb = trnInputs(3);
Gt   = trnInputs(4);

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) )  
    
    % This is the first call (Counter will be incremented later for this very first call)
    nCall = 0;

    % This is the first time step
    nStep = 1;
    
    % Initialize history of the variables for plotting at the end of the simulation
    nTimeSteps = (trnStopTime-trnStartTime)/trnTimeStep + 1;
    
history.Tamb    = zeros(nTimeSteps,1);
    history.Gt      = zeros(nTimeSteps,1);
    history.To      = zeros(nTimeSteps,1);
    history.Quseful = zeros(nTimeSteps,1);

    % No return, we will calculate the solar collector performance during this call
    mFileErrorCode = 130    % After initialization
    
end


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

if ( trnInfo(8) == -1 )

    mFileErrorCode = 1000;
    
    % Draw a plot of efficiency versus (To-Ta)/Gt
    isOk = find(history.Gt > 10);
    plot( (history.To(isOk)-history.Tamb(isOk))./history.Gt(isOk) , history.Quseful(isOk)/A./history.Gt(isOk) , 'r.' );
    title('Collector Efficiency');
    ylabel('Efficiency [-]');
    xlabel('(To-Tamb)/Gt  [癈.m�h/kJ]');
        
    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 = 140;   % Beginning of a post-convergence call 
    
    % history后的改成自己的
    history.Tamb(nStep)    = Tamb;
    history.Gt(nStep)      = Gt;
    history.To(nStep)      = To;
    history.Quseful(nStep) = Quseful;
    
    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, increment counter ---

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

% --- Get TRNSYS Inputs ---

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

% 输入改成自己的输入
Ti   = trnInputs(1);
mdot = trnInputs(2);
Tamb = trnInputs(3);
Gt   = trnInputs(4);

mFileErrorCode = 150;   % After reading inputs 

% --- Calculate solar collector performance ---

% 计算程序改成自己的
To = (eta0*Gt+mdot*Cp/A*Ti+eta1*Tamb) / (mdot*Cp/A+eta1);
Quseful = mdot*Cp*(To-Ti);

% --- Set outputs ---

% 输出改成自己的
trnOutputs(1) = To;
trnOutputs(2) = mdot;
trnOutputs(3) = Quseful;

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

3、设置TRNSYS type155

如果做自己的仿真,这里type155的m文件名一定要改成自己的m文件名。m文件名最好与TRNSYS仿真模型在一个文件夹中,否则就要写出m文件的具体路径。在calling_matlab的另一个示例中m-file name是".\Examples\Data Files\Type155_CallingMatlab.m"

  • 4
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
MATLAB中导入自制软件的方法取决于你的自制软件的类型和文件格式。如果你的自制软件是一个.m文件,你可以直接将该文件放在MATLAB的当前工作目录中,然后使用命令行或脚本中的函数来调用它。例如,你可以使用"run"函数来运行该文件。如果你的自制软件是一个函数文件,你可以使用"addpath"函数将包含该函数的文件夹添加到MATLAB的搜索路径中,然后使用函数名来调用它。 如果你的自制软件是一个Simulink模型,你可以使用"load_system"函数加载该模型。你可以指定模型的文件路径或使用相对路径。一旦加载了模型,你可以使用其他Simulink函数来操作和仿真该模型。 总之,根据你的自制软件的类型和文件格式,你可以使用适当的MATLAB函数来导入和调用它。请确保你的自制软件文件位于正确的位置,并按照MATLAB的语法和规范进行调用。 #### 引用[.reference_title] - *1* [TRNSYSMATLAB联合仿真](https://blog.csdn.net/luckyme_/article/details/119008312)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [基于MATLABSimulink库存系统建模与仿真.doc](https://blog.csdn.net/weixin_33490603/article/details/115815180)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Matlab实现深度学习(附上多个完整仿真源码)](https://blog.csdn.net/m0_62143653/article/details/129914044)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值