%
% OsiExample.m		File containing an introductonary example 
%                       on the usage of the OSI toolbox.    
% 
% Copyright (c) 1998 by ABB Corporate Research.
% All Rights Reserved.
%
% $Revision: 1.0, 12/10-1998 by Kjetil Havre, ABB-NOCRC$ 


% Connect to OLGA running on the computer NOCRC-WKJHA142 with service olga_1
[err, errmsg] = OsiConnect('NOCRC-WKJHA142 olga_1');

% To test response, PING
[err,errmsg] = OsiPing;

% To load input file 'hvm.inp' and initialize OLGA in one step.
[err, errmsg] = OsiStart('hvm.inp');

% To subscribe for pipeline profile results.  
[err, errmsg] = OsiSetProfileVar('$HOD-VALH PT TM');

% To subscribe for pipeline trend results.  
[err, errmsg] = OsiSetTrendVar('$HOD-OUTL PT TM');

% To advertise input variables (prepare OLGA server to receive input).
% Note that the valve opening can not directly be set in the OLGA server,
% one set the valve opening indirectly using a manual controller 
% (with gain equal to one) and then adjust the set point to thtis controller.
[err, errmsg] = OsiSetInputVar('CONTROLLER C-PV', [1]);

% To set integration stop time to time 24.0 seconds. 
OsiSetTend(24.0);

% To get information about the planned integration time step from OLGA.
dt = OsiGetDT;

% Set input data 
[err, errmsg] = OsiSetInputData(0.6);

% To simulate from current time to simulation end time (24.0) with the input.
[t, y, d, p, err, errmsg] = OsiSim;

% To plot profile result using MATLAB plot command.
plot(d, p(:,1));				% Pressure in first column. 
title('Hod outlet');		
xlabel('Section number'); ylabel('Pressure [Pa]');

plot(d, p(:,2));				% Temperature in second column. 
title('Hod outlet');		
xlabel('Section number'); ylabel('Temperature [C]');

% To get current time.
t0 = OsiGetTime;			% The time should then be 24.0. 

% To simulate using time varying input. 
tv = [t0+1.0; t0+2.0; t0+3.0; t0+4.0; t0+5.0; t0+6.0; t0+7.0; t0+8.0; t0+9.0; t0+10.0];
pV = [0.5; 0.55: 0.60; 0.65; 0.70; 0.75; 0.80; 0.85; 0.90; 0.90];
[t, y, d, p, err, errmsg] = OsiSim(tv, pV);

% Plot results using MATLAB's plot command.
plot(t, y(:,1));				% Pressure in first column. 
title('Hod outlet');		
xlabel('Time [seconds]'); ylabel('Pressure [Pa]');

plot(t, y(:,2));				% Temperature in second column. 
title('Hod outlet');		
xlabel('Time [seconds]'); ylabel('Temperature [C]');

% To stop the OLGA server. 
[err, errmsg] = OsiStopServer;
% To disconnect from OLGA (must be done after OsiStopServer 
% otherwise is the connection to the server lost, including the socket id etc.). 
[err, errmsg] = OsiDisconnect;


