Interacting with gProms simulations in MATLAB

The gProms simulation suite is a compelling set of tools for practical engineering problems in chemical engineering. Unfortunately, interfacing with the solvers is not very well documented. It has been my experience that the only proper way to learn how to interact with the gProms simulations from a programming environment (e.g., Matlab) is from someone else who knows how to do so. A fellow Ph.D. student taught me the way, and I hope to teach you the way as well. I am going to make a simple gProms project and the interfacing MATLAB code via gProms.

To start with, I will make a simple process defined as follows. This solves integrates the initial value problem $y’=f(t), y(0)=0$, where $f(t)$ is the input to the program.

Variable
    F AS Positive
    process_time AS Positive
    integrating_rate AS Positive


# Model equations

EQUATION
$F = integrating_rate;
$process_time = 1;


ASSIGN
    integrating_rate := 0;

INITIAL
F = 0;
process_time = 0;

With a process file defined as follows.


UNIT
MyUnit AS my_unit

SOLUTIONPARAMETERS
    FPI := "eventFPI";
    reportingInterval := 10;

SCHEDULE
# OperationSchedule
   SEQUENCE
        While TRUE DO
            SEQUENCE
                GET
                    MyUnit.integrating_rate;
                END
                Continue for 1
                SEND
                    MyUnit.F;
                END
            END
        END
    END

Let’s go over the process file to understand what is happening. In the SolutionParameters section, we see the following line tells the foreign process interface that we want to interact with an external source.

FPI := "eventFPI";

And the Get and Send blocks declare that we are getting values and sending values to this interface. Here gProms is ‘getting’ a value from the foreign process, and then gProms is ‘sending’ a value from the foreign process.

We are going to show how to interface this with MATLAB(trademark(TM(™))). The first hurdle to get over is exporting our simulation via go:Run. Yes, go:Run, and not go:Matlab.

image image

And now for the MatLab side of the simulation. First, we need to add the gProms components to the path. You will need to change the version number after core to match the version that you have installed on your machine.

addpath('C:\Program Files\PSE\gPROMS-core_6.0.4.55142\bin');
addpath('C:\Program Files\PSE\gPROMS-core_6.0.4.55142\gOMATLAB');

The following MatLab code is a generic MATLAB script to interact with gProms. Change the values as needed for your needs.


gOMATLAB('startONLY');
gOMATLAB('select','Simulate_plant','secret_password');  %('select','file_name','encryption_password')
gOMATLAB('simulate','Simulate_plant');                  %('simulate','process_name')

n_sim = 10;                         % Number of time steps
n_output = 1;                       % Number of outputs in gPROMS send task
n_input = length(set_conditions);   % number of inputs in gProms send task

input = zeros(n_input);   % We are going to send in an input of only zeros

result_matrix  = zeros(n_sim, n_output+2);  % (Nsim, number of outputs+2) [outputs are the entities in gOMATLAB_send]
                                            % row format [step number, output values, gProms return code]
%[j, gOMATLAB('evaluate',in',number of outputs)];
% error message from gOMATLAB seem to appear in the last column of the
% output, for instance 19 means not enough inputs while 18 seems to mean
% correct inputs just crashed instead (unexpected ACTIVITY FINISH
% event.)

for j=1:n_sim
    k = gOMATLAB('evaluate',input,n_output); % ('evaluate', variables to gProms, number of variables to get from gProms )
    result_matrix(j, :) = [j, k]; 
    %check for a gproms crash/reset
    if k(end) >= 17
        err = 1;
        time_steps = time;
        results = result_matrix(1:j,:);
        return;
    end
    fprintf('Step Time: %s\n', datestr(now,'HH:MM:SS'));
end

gOMATLAB('stop');