Calling EES from MATLAB
The Professional license provides macro commands that simplify calling MATLAB from EES to execute MATLAB commands or pass data back and forth.
Starting with version 12.131, it is possible to call EES from MATLAB. The interaction is made using the SendMessage command. It is possible to transfer data to EES using one or more text files that are used to solve a problem and/or run a series of macro commands. EES then writes output files that are read by MATLAB. Both numerical values and plots can be transferred from EES to MATLAB.
An example illustrating the interaction is provided here. An EES program is shown that will read a data file ('C:\MATLABExample\State.dat') that contains a fluid name (R$), temperature (T) and pressure (P). The EES program calculates the specific volume (v) and writes it to a second data file ('C:\MATLABExample\v.dat').
$Import 'C:\MATLABExample\State.dat' R$, T, P
$UnitSystem SI Mass kPa C kJ
v = Volume(R$, T=T, P=P)
$RunMacroAfter
Export 'C:\MATLABExample\v.dat' v
$EndMacro
A function Volume is created within MATLAB. The function takes in the fluid name, temperature and pressure and returns the volume.
function[v]=Volume(R,T,P)
Folder='C:\MATLABExample\';
fid=fopen([Folder,'State.dat'],'w');
if(fid==-1)
error('Could not open State.dat');
end
fprintf(fid, '''%s'',%.8f,%.8f\n',R,T,P);
fclose(fid);
if ~libisloaded('user32');
loadlibrary('user32.dll', 'C:\MATLABExample\user32_proto.h','alias','user32');
end
%Find EES window
p=System.Diagnostics.Process.GetProcessesByName('EES');
if (p.Length==0)
error('EES is not running - start EES before calling this function');
end
%Use first instance
hwnd = int64(p(1).MainWindowHandle.ToInt64);
%Send message to EES to Solve
Result=calllib('user32','SendMessageA', hwnd, EESConstants.wm_RunEES, EESConstants.em_SOLVE, EESConstants.notUsed);
if(~Result==0)
error(['EES returned error #',num2str(Result)]);
end
%Read the result
fid=fopen([Folder,'v.dat']);
v=fscanf(fid, '%f');
fclose(fid);
end
The function Volume first writes the values R, T, and P to the file State.dat (note that single quotes are attached to the fluid name to ensure that EES recognizes it as a string). Then the library user32.dll is loaded using the user32_proto.h file which must be placed in the same folder (or at least in the path that MATLAB will search). This file is shown below.
#include <windows.h>
/* SendMessageA using 64-bit LONG_PTR for hwnd, wParam, lParam */
long long SendMessageA(long long hwnd, unsigned int Msg, long long wParam, long long lParam);
Next the System.Diagnostics.Process.GetProcessesByName method is used to obtain a handle to any open EES windows. The SendMessage command is used to send the EES program the Solve command, causing it to read the file State.dat, calculate the specific volume and write thefile v.dat. Finally, the value of v is read from this file and returned. Note that the values of the constants used in this process (e.g., wm_RunEES) are defined in the class definition EESConstants.m which is also contained in the folder and shown below.
classdef EESConstants
% EESConstants holds the messages that EES recognizes
properties (Constant)
wm_RunEES = int64(32777);
em_LogOff = int64(1);
em_LogOn = int64(2);
em_SOLVE = int64(10);
em_RunMacroTab = int64(20);
notUsed = int64(0);
end
end
The MATLAB function Volume can be called as any other MATLAB function. For example, the MATLAB code below calculates the specific volume for R134a at 20 C over a range of pressures and creates a plot.
P = linspace(50,300,10);
for i=1:10
v(i) = Volume('R134a',20,P(i));
end
plot(P,v);
xlabel('Pressure (kPa)');
ylabel('Specific volume (m^3/kg)');
Any error messages that arise will be written to file EESCallFromApp.log in the EES directory.