% A Test file that builds a fairly complicated SSM and writes a DOT file.
% 
% M Hewitson

mc


%% Controller

C = ssm(plist('built-in', 'DFACS_5_Science_Mode_1_All_Optical_Readouts', ...
              'dim', 1, ...
              'withParams', true))
            
%% Actuators

Thrusters = ssm(plist('built-in', 'MPS', ...
              'dim', 1, ...
              'withParams', true))

%% ES actuators

ESS = ssm(plist('built-in', 'TMActuation_xcpl', ...
              'dim', 1, ...
              'withParams', true))

%% Dynamics

D = ssm(plist('built-in', 'LPF_Dynamics', ...
              'dim', 1, ...
              'withParams', true))


%% IFO

% I = ssm(plist('built-in', 'Interferometer_readout_xcpl', ...
%               'dim', 1, ...
%               'withParams', true))

% build a simple IFO with the two cross-couplings we want
I = ssm(plist('name', 'IFO-1D-Xcpl', 'timestep', 0, 'paramnames', {'delta112', 'delta121'}, ...
              'paramvalues', [-1e-4 0], 'inputnames', { 'SC_TM_attitude' 'Dist_Ifo_readout' }, ...
              'inputvarnames', { { 'rx1_H1' 'rx2_H2' } { 'Dist_x1_Ifo_ro' 'Dist_dx_Ifo_ro' } }, ...
              'outputnames', { 'Ifo_readout_delayed' }, ...
              'outputvarnames', { { 'rx1_H1_Ifo' 'dx_Ifo' } }, ...
              'amats', cell(0,0), ...
              'bmats', cell(0,2), ...
              'cmats', cell(1,0), ...
              'dmats', {[1 sym('delta112'); sym('delta121') 1], [1 sym('delta112'); sym('delta121') 1]}, ...
              'ssnames', cell(1,0)));

%% IFO sensing noise

ifo_sense_noise = ssm(plist('built-in', 'NSF_Interferometer_readout', 'dim', 1, ...
  'withParams', true));

%% Thrust noise

pzm = pzmodel(1000e-6, {[1e-4], 1e-4}, {1e-2, 1e-2});
thrust_noise = ssm(pzm);
thrust_noise.modify(plist('commands', {'outputnames = {''Dist_F_solar''}', 'inputvarnames = {{''thrust_noise_in''}}'}));
thrust_noise.setName('thrust noise');

%% TM noise

tm_noise = ssm(plist('built-in', 'NSF_TM', 'dim', 1, ...
  'withParams', true));

%% Build loop

% -- attach continuous parts
% Cparts = assemble(D, thrust_noise);
Cparts = assemble(Thrusters, ESS, D, I, ifo_sense_noise, thrust_noise, tm_noise);

% Set parameter values
pl = plist('KEEP_PARAM_NAMES',  {}, ...
           'KEEP_PARAM_ORDER', 'ALL', ...
           'SET_PARAM_NAMES',  {'delta112'}, ...
           'SET_PARAM_VALUES', 2e-6);

Cparts.modifparams(pl);

% Go numeric
Cparts.modifparams;

% -- discretize
Cparts.modiftimestep(plist('newtimestep', 0.1));


%% -- attach discrete parts

loop = assemble(Cparts, C);

loop.ssm2dot(plist('filename', 'loop.dot'));
