TRNSYS     Type 155: TRNSYS-Matlab link     

Content

- Quick Start instructions

- Type 155 Inputs / Outputs / Parameters description

- How to add the required files to TRNSYS

Quick Start instructions

This section gives step-by-step instructions for reasonably advanced TRNSYS users who are using TRNSYS 15.3.00, Matlab 6.5 (R13) and Compaq Visual Fortran 6.6B.
Please refer to the next sections for detailed instructions and troubleshooting.
  1. Run the setup file (trnsys-matlab.exe)
  2. Select your Trnsys15 installation folder (%TrnsysRoot%) for the destination to unzip files
  3. Open your TRNSYS workspace in the compiler (%TrnsysRoot%\WorkSpace\CVF66B\CVF66B.dsw)
  4. Add %TRNSYSRoot%\TrnLib\SEL\SourceCode\Type155.f90 to the "Types" folder under TRNLIB Files (right click, select add, browse to the file)
  5. Add libeng.lib, libmat.lib and libmx.lib to the "modules" folder under TRNLIB files. Those files are located in %MatlabRoot%\extern\lib\win32\digital\df60\ (%MatlabRoot% is your Matlab installation folder, e.g. C:\Matlab6p5)
  6. Rebuild Trnlib (F7)
  7. Launch IISiBat and open %TRNSYSRoot%\TrnLib\SEL\Examples\trnsys-matlab\trnsys-matlab.tpf
  8. Press F8 to run the simulation. Click on "yes" to exit the online plotter and Matlab will create a new figure

Note: To add Type 155 to a new project, select its proforma in \TrnLib\SEL\Calling Matlab

Type 155 Description

Object TRNSYS - Matlab link

Abstract
TRNSYS - Matlab connection
Note: to use this Type, the following Matlab libraries should be linked with trnlib.dll:
libeng.lib, libmat.lib, libmx.lib
In addition, Matlab's "bin\win32" folder must be on windows search path
 

 

 

Detailed description

This TRNSYS Type implements a link with Matlab

The connection uses the Matlab engine, which is launched as a separate process.
The Fortran routine communicates with the Matlab engine through a Component Object Model (COM) interface.

Type 155 can call different kinds of components which have different behaviors in TRNSYS.
The currently implemented options are:
- Standard Iterative component (called at each call of each time step)
- Real-time controller (called at the first call of each time step with the converged inputs from previous time step)

The current version of Type155 uses fixed names for the m-files that Matlab runs when a given unit is called.
- The m-file run at the first call of the first time step is:    type155initialize_NNN.m
- The m-file run during other time steps is:                     type155simulate_NNN.m
- The m-file run at the very last call (info(8)=-1) is:          type155terminate_NNN.m
NNN is parameter 6 padded with leading zeros if needed.
E.g. if par(6)=2, NNN will be 002 and the file called at the very first call of this unit will be type155initialize_002.m
ALL m-files must be in the same directory as the TRNSYS deck file OR in a directory on Matlab's search path
Note that Type155 will change the current Matlab directory to the TRNSYS deck file directory

When Type 155 calls Matlab, 3 variables are available:
t (1 x 1 array), info (1 x 18 array) and inputs (1 x nInputs array)
- t is TRNSYS simulation time
- inputs is the unit input vector (XIN in TRNSYS types)
- info holds information on the simulation and the current unit
   info (1:15) is the TRNSYS INFO array (see manual for a description)
   info(16) is TIME0 (simulation starting time)
   info(17) is TFINAL (simulation end time)
   info(18) is DELT (TRNSYS time step)

Type155 expects Matlab to create a variable called outputs (1 x nOutputs array).
Note: if necessary, Type 155 will create or pad the 'outputs' variable with -99999 to avoid memory access
violation errors

Warning: if you use ignoreEngineClose to leave Matlab running when TRNSYS exits, you will notice that the
Matlab instance will exit by itself after a certain time. You should save your session and open it in another
Matlab instance (which was not launched by TRNSYS) if you plan to work for a long time with the data

Icon
Author Michaël Kummert
Organization Solar Energy Lab, UW-Madison
Editor MKu
Creation date 2003-05-28
Modification date 2003-06-24
Model type Simplified
Validation Qualitative
Assembly
Type number 155
Allowed instances
Keywords Matlab
external
link

Parameters

Indice Name Dimension Unit Type Min Max Default Comment
1 typeVersion Dimensionless - real 0 +Inf 0 This parameter gives the version number of the Type. It will be used in future versions to ensure backwards compatibility
2 nInputs Dimensionless - integer 1 200 0 Number of inputs sent to Matlab
3 nOutputs Dimensionless - integer 1 200 0 Number of outputs from Matlab
4 componentKind Dimensionless - integer 0 10 0 This parameter describes the behavior of this component. Currently, the values 0 and 10 are defined.
0: Standard Iterative component (called at each call of each time step)
10: Real-time controller (called at the first call of each time step with the converged inputs from previous time step)
5 ignoreEngClose Dimensionless - boolean 0 1 0 If this parameter is not 0 for at least one unit of Type 155 in the simulation, the Matlab engine will not be closed when TRNSYS exits. This can be useful for further data analysis or debugging purposes. Please read the warning in "Description"
Please note that the Matlab instance launched by TRNSYS will exit by itself after a certain time, so you should save your session and re-open it in another Matlab instance if you want to keep working on the data
6 mFileId Dimensionless - integer 0 999 0 The current version of Type155 uses fixed names for the m-files that Matlab runs when a given unit is called. The m-file names are:
- type155initialize_NNN at very first call
- type155terminate_NNN at very last call
- type155simulate_NNN at other calls
NNN is parameter 6, padded with leading zeroes if needed (e.g. if par 6=2) the initialisation m-file will be type155initialize_002.m

Inputs

Indice Name Dimension Unit Type Min Max Default Comment
1..n input any - real -Inf +Inf 0 Input sent to Matlab. The total number of inputs is set by parameter 2 (nInputs)

Outputs

Indice Name Dimension Unit Type Min Max Default Comment
1..n output any - real -Inf +Inf 0 Output from Matlab. The total number of outputs is set by parameter 3 (nOutputs)

 

How to add the required files to TRNSYS

1. General information

The following information applies to TRNSYS 15.3.00 and above and Matlab 6.5 (R13) and assumes you are using the Compaq Visual Fortran 6.6B compiler. However, you should be able to apply the method to other Matlab versions and different compilers.

In the following, the strings hereunder will refer to the folders in which these 3 programs are installed:

Program Install Folder Example (default install folder)
Matlab R13 %MatlabRoot% C:\Maltab6p5
TRNSYS 15.3.00 %TRNSYSRoot% C:\Trnsys15
Compaq Visual Fortran 6.6B %CVFRoot% C:\Program Files\Microsoft Visual Studio\DF98

1.1. Included files and their location

Note: if you unzip the files in the package and select your TRNSYS root folder for the destination directory, all files should be saved at the correct location. This will replace your current param.inc but the new one is fully compatible with TRNSYS 15.3.00 (see 3.1)

Type155.f90:
Type 155 source code
%TRNSYSRoot%\TrnLib\SEL\SourceCode\

param.inc
:
updated include file for TRNSYS
%TRNSYSRoot%\Include\

Type155.tmf and type155.bmp
:
IISiBat proforma and icon for Type155
%TRNSYSRoot%\IIsiBat3\Data\TrnLib\SEL\Calling Matlab\

trnsys-matlab.tpf, type155*.m
:
Example files
%TRNSYSRoot%\TrnLib\SEL\Examples\trnsys-matlab\

type155-manual.html, trnsys-matlab-example.html:
Documentation files
%TRNSYSRoot%\TrnLib\SEL\Documentation\trnsys-matlab\

2. Computer Setup

NOTE: If the programs have been correctly installed, you should not need to do anything.  This section is mainly there for reference and troubleshooting purposes.

The following folders should be added to Windows search path: 
%MatlabRoot%\Bin\Win32 ; %TRNSYSRoot%; %CVFRoot%\Bin

In Windows NT/2000/XP, right click on My Computer, choose "Properties",  "Advanced" and then "Environment variables". Look for Path in you user variables or in the System variables (the latter is recommended). Add all the above strings separated by semicolons(;) if they are not already present.

In Windows 95/98, you should edit the Autoexec.bat file and add the following line:
PATH=%PATH%;%MatlabRoot%\Bin\Win32;%TrnsysRoot%;%CVFRoot%\Bin
(attention, %PATH% should appear literally, while other strings should be replaced with the real installation directories, e.g. C:\Matlab instead of %MatlabRoot%)

NOTE: The presence of different TEMP variables in User and System variables sometimes causes problems when using the Compaq Visual Fortran compiler. It is recommended to remove one of the TEMP definitions (e.g. the one in System Variables).

3. Add Type155 to your Trnlib DLL

3.1. If you are using CVF 6.6B

Open the Trnsys Workspace (%TRNSYSRoot%\WorkSpace\CVF66B\CVF66B.dsw). In the left pane, select "Trnlib files", then the "Types" folder. Right-click and select "add files to folder". Browse to the location where you have saved Type155.f90 (e.g. %TRNSYSRoot%\TrnLib\SEL\SourceCode) and select it.

NOTE: Type155 requires a new version of param.inc, which is included in the package. You should replace the existing param.inc file in %TRNSYSRoot%\Include with the new version prior to compiling Type155.

3.2. If you are not using CVF 6.6B

You should modify your project to include Type155, as you would do for any non-standard Type. Instructions are available on the TRNSYS website (http://sel.me.wisc.edu/trnsys, see "Fortran Info").

NOTE: Type155 requires a new version of param.inc, which is included in the package. You should replace the existing param.inc file in %TRNSYSRoot%\Include with the new version prior to compiling Type155.

4. Add the required libraries to the CVF project

4.1. If you are using CVF 6.6B

In order to use the Matlab engine library, you have to add 3 libraries: libmat.lib, libeng.lib and libmx.lib. A version of those libraries which is compatible with CVF 6.6B is included in the Matlab R13 distribution. To add them to the Trnlib Project:

Open the Trnsys Workspace (%TRNSYSRoot%\WorkSpace\CVF66B.CVF66B.dsw). In the left pane, select "Trnlib files", then the "modules" folder. Right-click and select "add files to folder".

Select %MatlabRoot%\extern\lib\win32\digital\df60\libeng.lib, %MatlabRoot%\extern\lib\win32\digital\df60\libmat.lib and %MatlabRoot%\extern\lib\win32\digital\df60\libmx.lib

4.2. If you are not using CVF 6.6B

Please check the Matlab website to see if your compiler is supported. At the time of writing, only the "Digital Fortran 5.0" and "Digital Fortran 6.0" families were supported (CVF6.6B is the latest version in the "Digital 6.0 family"). Use the appropriate .lib files for your compiler (all of them are included in %MatlabRoot%\extern\lib\win32\YOUR_COMPILER

If your compiler is not supported, you can still use the lib command to make the .lib files using the corresponding .def files.
Those .def files are in %MatlabRoot%\extern\include. You need to use libeng.def, libmat.def and libmx.def

5. Rebuild Trnsys

The easiest way to rebuild the entire workspace is to use the "Build/Batch Build" command. You can then select the appropriate configuration for each one of the three projects. For example, if you want to be able to debug your application, select:

DummyLib - Win32 Debug
ExtDLL - Win32 Debug
TrnLib - Win32 Debug

Then click on "Rebuild all"

The following files should be created:

\Trnsys15\Dummy\CVF66BDummy.lib
\Trnsys15\ExtDLL.dll
\Trnsys15\ExtDLL.lib
\Trnsys15\ExtDLL.exp
\Trnsys15\Trnlib.dll
\Trnsys15\Trnlib.lib
\Trnsys15\Trnlib.exp

6. Launch IISiBat and run the example

Open the example (trnsys-matlab.tpf, which should be in %TrnsysRoot%\TrnLib\SEL\Examples\trnsys-matlab) and press F8 to run it.
Please read trnsys-matlab-example.html for more details on that example.

To use Type155 in other projects, you should drag it from the direct access tool (the proforma will be in %TRNSYSRoot%\IIsiBat3\Data\TrnLib\SEL\Calling Matlab\)

Comments, Questions? Please contact trnsys@engr.wisc.edu
 

Michaël Kummert
Solar Energy Lab, UW-Madison
2003-06-25