clc; clear;
% -------------------------------------------
% Trevor Slade
% Brigham Young University
% Prism Group
% Dr. Hedengren
% --------------------------------------------
% -------------------------------------------
% Setting up the NLC in APMonitor
% For any questions see http://apmonitor.com/wiki/index.php/Main/MATLAB
% -------------------------------------------
% Add path to APM libraries
addpath('apm');
% Clear MATLAB
clear all
close all
% Select server
server = 'http://xps.apmonitor.com';
%server = 'http://apm.byu.edu';
% Application
app = '4tank';
% Clear previous application
apm(server,app,'clear all');
% load model variables and equations
apm_load(server,app,'4tank.apm');
% load data
csv_load(server,app,'control.csv');
% Set up variable classifications for data flow
% Feedforwards - measured process disturbances
apm_info(server,app,'FV','gamma[1]');
apm_info(server,app,'FV','gamma[2]');
% Manipulated variables (for controller design)
apm_info(server,app,'MV','v1');
apm_info(server,app,'MV','v2');
% State variables (for display only)
apm_meas(server,app,'h[1]',0);
apm_meas(server,app,'h[2]',0);
apm_info(server,app,'SV','h[1]');
apm_info(server,app,'SV','h[2]');
% Controlled variables (for controller design)
apm_info(server,app,'CV','h[3]');
apm_info(server,app,'CV','h[4]');
% solve here for steady state initialization
% imode = 3, steady state mode
apm_option(server,app,'nlc.imode',3);
apm(server,app,'solve')
% imode = 6, switch to dynamic control
apm_option(server,app,'nlc.imode',6);
% nodes = 3, internal nodes in the collocation structure (2-6)
apm_option(server,app,'nlc.nodes',3);
% time units
apm_option(server,app,'nlc.ctrl_units',1);
apm_option(server,app,'nlc.hist_units',2);
% read csv file
apm_option(server,app,'nlc.csv_read',1);
% Manipulated variables
apm_option(server,app,'v1.status',1);
apm_option(server,app,'v1.upper',1);
apm_option(server,app,'v1.lower',0);
apm_option(server,app,'v1.dmax',1);
apm_option(server,app,'v1.dcost',1);
apm_option(server,app,'v2.status',1);
apm_option(server,app,'v2.upper',1);
apm_option(server,app,'v2.lower',0);
apm_option(server,app,'v2.dmax',1);
apm_option(server,app,'v2.dcost',1);
% Controlled variables
apm_option(server,app,'h[3].status',1);
apm_option(server,app,'h[3].fstatus',1);
apm_option(server,app,'h[3].sphi',10.1);
apm_option(server,app,'h[3].splo',9.9);
apm_option(server,app,'h[3].tau',.01);
apm_option(server,app,'h[4].status',1);
apm_option(server,app,'h[4].fstatus',1);
apm_option(server,app,'h[4].sphi',15.1);
apm_option(server,app,'h[4].splo',14.9);
apm_option(server,app,'h[4].tau',.01);
apm_meas(server,app,'gamma[1]',.5);
apm_meas(server,app,'gamma[2]',.5);
% Set controller mode
apm_option(server,app,'nlc.reqctrlmode',3);
% ------------------------------------------------
% PARAMETERS
% ------------------------------------------------
% Set it up so calcs are made in SI units
% the variable h refers to height
% Diameters
P.dP = .25; % inner pipe diameter (inches)
P.dT = 10; % inner tank diameter (inches)
% height
P.hT = 18; % inner tank height (inches)
% density
P.rho = 1; % g/cm^3
P.g = 9.8; % acceleration of gravity (m/s^2)
% Convert to SI (specifically cm and g)
P.dP = 6*P.dP; % inner pipe diameter (centimeters)
P.dT = 2.54*P.dT; % inner tank diameter (centimeters)
P.hT = 2.54*P.hT; % inner tank height (centimeters)
P.g = P.g*100; % acceleration of gravity (cm/s^2)
% coefficient for flow out of the bottom of the tanks
P.Cf_tanks = (pi*(P.dP/2)^2)*(P.rho^1.5)*sqrt(2*P.g);
% Constants (to make calculations easier
P.C_HM = P.rho*(pi*(P.dT/2)^2); % Convert from dhdt to dmdt
P.C_CM = (pi*(P.dP/2)^2)*P.rho; % Convert from velocity to mass flow
% Make it even more convenient...
P.C1 = P.C_CM/P.C_HM;
P.C2 = P.Cf_tanks/P.C_HM;
% Velocities (they are static as of now)
P.v1_max = 500; % cm/s
P.v2_max = P.v1_max; % cm/s