Newer
Older
classdef SCDclass_algo
%SCD algorithm handling object
% The class holds all information and
% methods for handling a Simulink
% algorithm
properties (Access = private)
modelname % Name of the model
mdscontainer % Container class for MDS+ interface objects
taskcontainer % Container class for init and term tasks
exportedtps % List of tunable parameters variable to be exported
datadictionary % Name of the used data dictionary
stdinits % Standard inits scripts for fixed parameters
timing % Timing info structure
end
methods
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
% Empty algorithm constructor
obj.modelname=name;
obj.mdscontainer = SCDclass_mdsobjcontainer;
obj.taskcontainer = SCDclass_taskcontainer;
obj.exportedtps = {};
obj.stdinits = {};
obj.datadictionary = '';
obj.timing.t_start=0;
obj.timing.t_stop=1;
obj.timing.dt=1e-3;
% Standard data dictionary equal to algorithm name,
% overriddable by the setter method
obj=obj.setdatadictionary([name '.sldd']);
end
%% Print infos
function printinfo(obj)
fprintf('*****************************************************\n');
fprintf('* SCD algorithm: ''%s''\n',obj.modelname);
fprintf('*****************************************************\n');
if(~isempty(obj.datadictionary))
fprintf('* Data dictionary:\n %s\n',obj.datadictionary);
else
fprintf('* Data dictionary:\n base workspace\n');
end
if numel(obj.exportedtps)>0
fprintf('* Tunable params structs:\n');
for ii=1:numel(obj.exportedtps)
fprintf(' %s\n',obj.exportedtps{ii});
end
end
fprintf('* Tunable params objects:\n')
obj.printparameters;
fprintf('* Wavegen objects:\n');
obj.printwavegens;
fprintf('* Tasks objects:\n');
obj.printtasks;
fprintf('* Fixed parameters inits:\n');
obj.printinits;
fprintf('*****************************************************\n');
end
%% Setup
function setup(obj)
obj.buildworkspacetpstruct;
end
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
%% General purpose getters
function out = getname(obj)
out=obj.modelname;
end
function out = getmdscontainer(obj)
out=obj.mdscontainer;
end
function out = gettaskcontainer(obj)
out=obj.taskcontainer;
end
function out = gettiming(obj)
out=obj.timing;
end
function out = getinits(obj)
out=obj.stdinits;
end
%% Data dictionary setter and getter
function obj = setdatadictionary(obj, datadict)
obj.datadictionary=datadict;
end
function out = getdatadictionary(obj)
out = obj.datadictionary;
end
%% Tunable parameters structures handling functions
function out = getexportedtps(obj)
out = obj.exportedtps;
end
function obj = addtunparamstruct(obj, structname)
if(~ismember(structname, obj.exportedtps))
obj.exportedtps{end+1}=structname;
else
error('SCDclass_algo:addtunparamsstruct','Tunable parameter struct already present, cannot add!');
end
end
function obj = buildworkspacetpstruct(obj)
% this funtion builds a workspace structures containing
% replicas of all tunable parameters structurea in the data
% dictionaries, this structure is the one actually used
% for loading simulation wavegen data
% It is better not to use directly data dictionaries structures
% to avoid flooding dds with big sim data sets (and
% conseguently the SCD SVN itself
if(isempty(obj.datadictionary))
warning('SCDclass_algo:buildworkspacetpstruct','Methods ignored for this object, data dictionary isn''t configured');
return;
end
dd=SCDconf_getdatadict(obj.datadictionary);
for ii=1:numel(obj.exportedtps)
simstructnamedd=sprintf('%s_tmpl',char(obj.exportedtps(ii)));
simstructnamews=char(obj.exportedtps(ii));
simstruct=dd.getEntry(simstructnamedd).getValue;
assignstr=sprintf('%s=temp;',simstructnamews);
assignin('base','temp',simstruct);
evalin('base',assignstr);
end
evalin('base','clear temp;');
end
%% MDS container methods forwarders
function obj = addparameter(obj, param)
obj.mdscontainer=obj.mdscontainer.addparameter(param);
obj.mdscontainer=obj.mdscontainer.bindlastparameter(obj.modelname, obj.datadictionary, obj.exportedtps);
end
function obj = printparameters(obj)
obj.mdscontainer=obj.mdscontainer.printparameters;
end
function obj = actualizeparameters(obj, shot)
obj.mdscontainer=obj.mdscontainer.actualizeparameters(shot);
end
function obj = addwavegen(obj, wavegen)
obj.mdscontainer=obj.mdscontainer.addwavegen(wavegen);
obj.mdscontainer=obj.mdscontainer.bindlastwavegen(obj.modelname, obj.datadictionary, obj.timing);
end
function obj = printwavegens(obj)
obj.mdscontainer=obj.mdscontainer.printwavegens;
end
function obj = actualizewavegens(obj, shot)
obj.mdscontainer=obj.mdscontainer.actualizewavegens(shot);
end
function obj = cleanwavegens(obj)
obj.mdscontainer=obj.mdscontainer.cleanwavegens;
end
%function obj = bindparameters(obj)
% obj.mdscontainer=obj.mdscontainer.bindparameters(obj.modelname, obj.datadictionary, obj.exportedtps);
%end
% This method has only sense in the expcode context
%function obj = buildworkspacesimstruct(obj)
% obj.mdscontainer=obj.mdscontainer.buildworkspacesimstruct;
%end
%% Task container methods forwarders
function obj = addtask(obj, task)
obj.taskcontainer=obj.taskcontainer.addtask(task);
obj.taskcontainer=obj.taskcontainer.bindlasttask(obj.modelname, obj.datadictionary);
end
function obj = printtasks(obj)
obj.taskcontainer=obj.taskcontainer.printtasks;
end
%% Wavegen timing structure setters
% function obj = setwavegentimingsources(obj, tstart, dt, tstop)
% assert(ischar(tstart), 'SCDclass_algo.setwavegentimingsources first parameter must be a char array');
% assert(ischar(dt), 'SCDclass_algo.setwavegentimingsources first parameter must be a char array');
% assert(ischar(tstop), 'SCDclass_algo.setwavegentimingsources first parameter must be a char array');
%
% obj.wavegentiming.t_start=tstart;
% obj.wavegentiming.dt=dt;
% obj.wavegentiming.t_stop=tstop;
% end
%% Timing information
function obj = settiming(obj, t_start, dt, t_stop)
obj.timing.t_start=t_start;
obj.timing.dt=dt;
obj.timing.t_stop=t_stop;
end
%% Fixed inits
function obj = addfpinitfcn(obj, fcnname, targetstruct,targetworkspace)
if nargin<3
targetstruct = '';
end
if nargin<4
targetworkspace = 'global'; % default: global workspace (assigninGlobal)
end
if ~iscell(targetstruct)
targetstruct = {targetstruct};
end
if(~isempty(obj.stdinits))
for ii=1:numel(obj.stdinits)
if contains(obj.stdinits{ii}{2},targetstruct)
warning('SCDclass_algo:addfpinitfcn','A function defining the structure %s has already been added, ignoring.\d',targetstruct)
return
% FF question to CG: why is this a cell and not a struct?
temp=cell(10,1);
temp{1}=fcnname;
temp{2}=targetstruct;
temp{3}=targetworkspace;
obj.stdinits{end+1}=temp;
end
function printinits(obj)
if(~isempty(obj.stdinits))
for ii=1:numel(obj.stdinits)
fprintf('%s -> %s in workspace %s \n',char(obj.stdinits{ii}{1}),char(obj.stdinits{ii}{2},obj.stdinits{ii}{3}));
% call initialization functions that set fixed parameters
if ~isempty(obj.stdinits)
for ii=1:numel(obj.stdinits)
initfunction = obj.stdinits{ii}{1};
targetnames = obj.stdinits{ii}{2};
workspace = obj.stdinits{ii}{3};
nout = numel(targetnames);
fprintf('Calling init function ''%s'', assigning its output to ''%s'' ...\n',...
char(initfunction),cell2mat(targetnames));
if ischar(initfunction)
initcmd=sprintf('%s(obj);', initfunction);
[value{1:nout}] = eval(initcmd);
elseif isa(initfunction,'function_handle')
if nargin(initfunction)==2
argins = {obj,shot};
elseif nargin(initfunction)==1
argins={obj};
elseif nargin(initfunction)==0
else
error('unexpected number of input arguments for function %s',func2str(initfunction));
end
[value{1:nout}] = initfunction(argins{:}); % function has an input argument
else
error('initfunction must be a string or a function handle')
end
% assigns in base workspace or datadictionary depending
% on target workspace
for iout = 1:nout
% cycle over number of output arguments of the function
val = value{iout};
target = targetnames{iout};
if strcmp(workspace,'global')
Simulink.data.assigninGlobal(obj.modelname, target, val);
elseif strcmp(workspace,'base')
assignin('base',target,val)
end
end
function compile(obj)
try
eval(sprintf('%s([],[],[],''compile'')',obj.modelname));
catch
eval(sprintf('%s([],[],[],''term'')',obj.modelname));
end
eval(sprintf('%s([],[],[],''term'')',obj.modelname));
end
function sim(obj)
sim(obj.modelname);
end