KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ExperimentRunner.m
Go to the documentation of this file.
1 namespace models{
2 namespace muscle{
3 
4 
5 /* (Autoinserted by mtoc++)
6  * This source code has been filtered by the mtoc++ executable,
7  * which generates code that can be processed by the doxygen documentation tool.
8  *
9  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
10  * Except for the comments, the function bodies of your M-file functions are untouched.
11  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
12  * attached source files that are highly readable by humans.
13  *
14  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
15  * the correct locations in the source code browser.
16  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
17  */
18 
20  :public handle {
27  public:
28 
30 
31 
32  public: /* ( Dependent ) */
33 
35 
36 
37  public:
38 
40 
42 
43 
44  /* (Access=privat*/
45 public:
46 
48 
50 
52 
53 
54  public:
55 
57  this.Model= model;
58  config = model.Config;
59  if ~isa(config, " models.muscle.AExperimentModelConfig ")
60  error(" 'config' parameter must be a AExperimentModelConfig ");
61  end
62  this.Config= config;
63 
64  /* Do parallel if parallel toolbox is around! */
65  this.RunParallel= exist(" matlabpool "," file ") == 2;
66  this.StoreExperimentData= true;
67  }
68 
69 
70  function [out , matrix<double>y , ct ] = getCachedExperimentConfig(nr,colvec<double> mu) {
71  m = this.Model;
72  if nargin < 3
73  mu = m.DefaultMu;
74  end
75  /* The cache key is param + config nr */
76  key = [mu; nr; m.Geo.NumElements; m.Config.FEM.GaussPointsPerElem];
77  out = [];
78  y = [];
79  ct = [];
80  if this.cachedata && this.cache.hasData(key);
81  data = this.cache.getData(key);
82  out = data.out;
83  y = data.y;
84  ct = data.ct;
85  this.Config.CurrentConfigNr= nr;
86  this.Model.System.setConfig(mu,this.Model.DefaultInput);
87  end
88  }
89 
90 
91  function [out , matrix<double>y , ct ] = runExperimentConfig(nr,colvec<double> mu) {
92 
93  m = this.Model;
94  if nargin < 3
95  mu = m.DefaultMu;
96  end
97  [out, y, ct] = this.getCachedExperimentConfig(nr, mu);
98  /* Uncomment this to re-generate the output
99  * out = this.Config.getOutputOfInterest(m.Times,y); */
100  if isempty(out)
101  c = this.Config;
102  out = NaN(1,c.NumOutputs);
103  try
104  /* Apply configuration set within ModelConfig
105  * Updates the associated model automatically (precomputations) */
106  c.CurrentConfigNr= nr;
107 
108  /* m.plotGeometrySetup; */
109 
110  /* Run simulation */
111  [t,y,ct] = m.simulate(mu);
112 
113  /* Get output of interest */
114  tic;
115  out = c.getOutputOfInterest(t,y);
116  ct = ct + toc;
117 
118  if this.cachedata
119  /* The cache key is param + config nr */
120  key = [mu; nr; m.Geo.NumElements; c.FEM.GaussPointsPerElem];
121  data = struct(" y ",y," t ",t," mu ",mu," nr ",nr," ct ",ct," out ",out);
122  this.cache.addData(key,data);
123  end
124  catch ME
125  /* Set to NaN to notify there's something wrong */
126  out(:) = NaN;
127  ME.getReport
128  end
129  end
130  }
141  function [out , ct ] = runExperiment(colvec<double> mu) {
142  if nargin < 2
143  mu = this.Model.DefaultMu;
144  end
145  c = this.Config;
146  out = zeros(c.NumConfigurations,c.NumOutputs);
147  ct = zeros(c.NumConfigurations,1);
148  /* Run all the settings! */
149  if this.RunParallel && ~this.multipleexperiments
150  fprintf(" Running %d experiment configurations in parallel...\n ",c.NumConfigurations);
151  closeafterrun = false;
152  if matlabpool(" size ") == 0
153  matlabpool open;
154  closeafterrun = true;
155  end
156  parfor nr = 1:c.NumConfigurations
157  t = getCurrentTask();
158  fprintf(" Worker %d: Running configuration %d\n ",t.ID,nr);
159  [out(nr,:), ~, ct(nr)] = this.runExperimentConfig(nr, mu);/* #ok */
160 
161  end
162  if closeafterrun
163  matlabpool close;
164  end
165  fprintf(" done ");
166  else
167  if ~this.multipleexperiments
168  pi = ProcessIndicator(" Running experiment configurations ",c.NumConfigurations);
169  end
170  for nr = 1:c.NumConfigurations
171  [out(nr,:), ~, ct(nr)] = this.runExperimentConfig(nr, mu);
172  if ~this.multipleexperiments
173  pi.step;
174  end
175  end
176  if ~this.multipleexperiments
177  pi.stop;
178  end
179  end
180  }
190  function [allout , allct ] = runExperiments(mus) {
191  c = this.Config;
192  nex = size(mus,2);
193  allout = zeros(c.NumConfigurations,c.NumOutputs,nex);
194  allct = zeros(c.NumConfigurations,nex);
195  this.multipleexperiments= nex > 1;
196 
197  if this.RunParallel && nex > 1
198  closeafterrun = false;
199  if matlabpool(" size ") == 0
200  matlabpool open;
201  closeafterrun = true;
202  end
203  fprintf(" Running %d experiments (%d configs each) in parallel...\n ",nex,c.NumConfigurations);
204  parfor k=1:nex
205  t = getCurrentTask();
206  fprintf(" Worker %d: Running experiment %d\n ",t.ID,k);
207  [allout(:,:,k), allct(:,k)] = this.runExperiment(mus(:,k));/* #ok */
208 
209  end
210  fprintf(" done ");
211  if closeafterrun
212  matlabpool close;
213  end
214  else
215  pi = ProcessIndicator(" Running experiment configurations (%d each) for %d parameters ",...
216  nex,false,c.NumConfigurations,nex);
217  for k=1:nex
218  [allout(:,:,k), allct(:,k)] = this.runExperiment(mus(:,k));
219  pi.step;
220  end
221  pi.stop;
222  end
223  /* Put number of experiment to first row */
224  allout = permute(allout,[3 1 2]);
225 
226  this.multipleexperiments= false;
227  }
236  function res = runExperimentsCached(mus) {
237  c = this.Config;
238  fi = fullfile(c.OutputDir,[c.getOptionStr " .mat "]);
239  if ~(exist(fi," file ") == 2)
240  m = this.Model;
241  e = this;
242  if nargin < 2
243  [o, ct] = this.runExperiment;/* #ok */
244 
245  save(fi," m "," o "," c "," ct "," e ");
246  else
247  [o, ct] = this.runExperiments(mus);/* #ok */
248 
249  save(fi," m "," o "," c "," mus "," ct "," e ");
250  end
251  end
252  res = load(fi);
253  }
254 
255 
256 
257 #if 0 //mtoc++: 'set.StoreExperimentData'
258 function StoreExperimentData(value) {
259  if value && isempty(this.cache)
260  dir = fullfile(this.Config.OutputDir," trajectories ",this.Config.getOptionStr(false));
261  this.cache= data.FileDataCollection(dir);
262  this.cache.KeepFiles= true;
263  elseif ~isempty(value) && ~value
264  this.cache= [];
265  end
266  this.cachedata= value;
267  }
268 
269 #endif
270 
271 
272 
273 #if 0 //mtoc++: 'get.StoreExperimentData'
274 function value = StoreExperimentData() {
275  value = this.cachedata;
276  }
277 
278 #endif
279 
280 
286 };
287 }
288 }
289 
290 
291 
function [ out , matrix< double > y , ct ] = runExperimentConfig(nr,colvec< double > mu)
Runs a single experiment configuration. Coded here for convenience only.
The base class for any KerMor detailed model.
Definition: BaseFullModel.m:18
models.BaseFirstOrderSystem System
The actual dynamical system used in the model.
Definition: BaseModel.m:102
function setConfig(mu, inputidx)
Sets the dynamical system's configuration.
ExperimentRunner(models.BaseFullModel model)
Matlab's base handle class (documentation generation substitute)
integer DefaultInput
The default input to use if none is given.
Definition: BaseModel.m:189
permute
Rearranges the dimensions of the handle object array. See the MATLAB permute function.
colvec< double > DefaultMu
The default parameter value if none is given.
Definition: BaseModel.m:355
function [ out , matrix< double > y , ct ] = getCachedExperimentConfig(nr,colvec< double > mu)
function [ allout , allct ] = runExperiments(mus)
Runs the experiments in parallel for all given mu values.
function [ out , ct ] = runExperiment(colvec< double > mu)
Runs the complete set of experiments for a given parameter mu.
ProcessIndicator: A simple class that indicates process either via waitbar or text output...
Model: Model for a FEM-discretized muscle model.
Definition: Model.m:19
function res = runExperimentsCached(mus)