|
 |
|
 |
|
Speeding up parted simulation
|
|
On Wed, 27 May 2009 08:19:02 +0000 (UTC), "pierre " <...@yopmail.com
Hello,
Thanks to 'sim' function, I run a simulation on a total timespan of 1s, from Matlab.
Today, this simulation is actually parted in 10 simulations of 0.1s. At the end of each 0.1s interval, I :
- get in Matlab the simulation FinalState, xf_sml ;
- get the variables sent to Matlab Workspace by 'To Workspace' Simulink blocks ;
- process these data in my main Matlab .m file
. polynomial evaluation,
. matrix computing,
. data reshaping {concatenation, extraction, etc.},
. state-space matrix updating ;
- runs a new 0.1s simulation, using previous FinalState as new IntialState x0_sml, in order to keep simulation "continuity".
Here's a shortened version of my script :
timespan = 10; % total timespan
T = 0.1; % elementary timepsan
Te = 1e-4; % sample time
for i=0:(round(timespan/T)-1)
inter_sml = [i, i+1]* T;
options_sml = simset('Solver', 'FixedStepDiscrete', 'FixedStep', ...
Te, 'OutputVariables', 'x', 'SaveFormat', 'StructureWithTime', ...
'InitialState', x0_sml, 'FinalStateName', 'xf_sml');
[t, x, y] = sim(mymodel, inter_sml, options_sml);
x0_sml = xf_sml; % x0_sml(i+1)=xf_sml(i)
end;
Problem : this code is too slow, especially each call to 'sim' function, whose self time is about 10 times the simulated timespan (according to profiler).
Launched alone (outside Matlab) thanks to 'Start simulation' button [or with a unique 'sim' call on 10s without any return to Matlab every 0.1s], this model runs twice faster than real-time. What lets me think the reason for this slowness is elsewhere (maybe could the exchange of variables between Simulink to Matlab be guilty?).
Any ideas for speeding up this simulation?
Either re-thinking the whole solution or thinking about a way to speed up lazy 'sim'...
Thanks for your help,
pierre
|
| |
 |
|
 |
|
 |
|
 |
|
On Wed, 27 May 2009 11:59:12 +0100, "Arnaud Miege" <...@nospam.mathworks.co.uk
"Sprinceana " <...@fred.mathworks.com...
This is for HDL co-simulation only and is not relevant to the OP's problem.
Arnaud
|
|
 |
|
 |
 |
|
 |
|
On Wed, 27 May 2009 11:41:01 +0000 (UTC), "Sprinceana " <...@yahoo.com
"Arnaud Miege" <...@nospam.mathworks.co.uk
Ok. Thanks Arnaud!
Sry for that! Just wanted to help a little!
|
|
 |
|
 |
 |
|
 |
|
On Wed, 27 May 2009 10:47:28 +0100, "Arnaud Miege" <...@nospam.mathworks.co.uk
"pierre " <...@fred.mathworks.com...
Do you use the same options/solver settings when you run the model
interactively from Simulink using the start simulation button? I'm thinking
that maybe you are using a variable-step solver when running interactively
and a fixed-step solver when using the sim command, as shown in your code
above. The variable-step solver is likely to be faster. Make sure you are
using the same settings when comparing simulation times.
]
You can also try the accelerator or rapid accelerator mode:
http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/ug/f0-22210.html
HTH,
Arnaud
|
|
 |
|
 |
 |
|
 |
|
On Thu, 28 May 2009 09:17:02 +0000 (UTC), "pierre " <...@yopmail.com
"Arnaud Miege" <...@nospam.mathworks.co.uk
Arnaud,
I checked my Simulink 'Configuration parameters' : these are the same as the ones I give to 'simset' when I run from Matlab, in particular the 'FixedStepDiscrete' solver.
I then had a look at 'Accelerator' and 'Rapid Accelerator' simulation modes.
Here's the code I tested [*] :
timespan = 10; % total timespan
T = 0.1; % elementary timepsan
Te = 1e-4; % sample time
set_param(modele, 'SaveFinalState', 'on'); % required by accelerator
set_param(modele, 'SimulationMode', 'accelerator');
for i=0:(round(timespan/T)-1)
inter_sml = [i, i+1]* T;
set_param(modele, 'InitialState', 'x0_sml');
set_param(modele, 'FinalStateName', 'xf_sml');
options_sml = simset('Solver', 'FixedStepDiscrete', 'FixedStep', Te, ...
'OutputVariables', 'x', 'SaveFormat', 'StructureWithTime');
[t, x, y] = sim(mymodel, inter_sml, options_sml);
x0_sml = xf_sml; % x0_sml(i+1)=xf_sml(i)
end;
Problem : from now on, each 0.1s simulation seems to restart from scratch, as if the 'x0_sml = xf_sml' instruction (supposed to assure simulation continuity) was ignored.
Could the compiled code used by Acceleration modes be responsible for this problem (for example freezing simulation state variables)?
Thanks,
pierre
[*] changes since previous version :
- 4 new 'set_param' lines for the accelerator mode to be happy
. 2 for initialisation
. 2 in order to replace simset InitialState and FinalStateName inputs
- 'simset' call shortened (goodbye InitialState and FinalStateName)
|
|
 |
|
 |
 |
|
 |
|
On Thu, 28 May 2009 17:52:02 +0100, "Arnaud Miege" <...@nospam.mathworks.co.uk
Hi Pierre,
I think you are probably also need to save the complete SimState in the
final state (BTW, you also need this in normal mode):
set_param(modele, 'SaveCompleteFinalSimState', 'on')
Have a look at the following page in the documentation for more details:
http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/ug/bry62t3.html
However, looking at the limitations at the bottom of the page, it seems that
it only works in normal mode, not in accelerated mode. So it looks like
we're back to your starting point. If the settings are the same, I would
expect that running a model programmatically or intercatively should take
approximately the same amount of time. I would suggest contacting technical
support:
http://www.mathworks.com/support/contact_us/index.html
Arnaud
|
|
 |
|
 |
 |
|
 |
|
On Sun, 31 May 2009 10:33:03 +0000 (UTC), "pierre " <...@yopmail.com
"Arnaud Miege" <...@nospam.mathworks.co.uk
- I read the "Saving and Restoring the Simulation State as the SimState" page and tried to use its few set_param lines.
I muss confess I'm not sure how to use them programmatically. Simulink documentation writes :
set_param(mdl, 'SaveFinalState', 'on', 'FinalStateName',...
[mdl] 'SimState','SaveCompleteFinalSimState', 'on')
What is the "[mdl]" between 'FinalStateName' and 'SimState' for?
- Assuming it's useless, I took it off and naively tried :
'SaveCompleteFinalSimState', 'on')
But I can't get rid of the following error :
??? Error using ==block_diagram does not have a parameter named 'SaveCompleteFinalSimState'.
- So, I tested the interactive way [*] and figured out (surprise) I have no "Save complete SimState in final state" check box. Which explains the "programmatical" error. But why wouldn't I have this feature?
Well, except an offset, it nearly does, for example when I run a 10s simulation with a unique call to 'sim' on this timespan.
The problem really comes from repeated calls to 'sim', from 0 to 10s with 0.1s step, which causes as many offsets (here, 10/0.1 = 100).
Thx,
pierre
[*] Simulation \ Configuration Parameters \ Data Import/Export \ Save to workspace
|
|
 |
|
 |
 |
|
 |
|
On Sun, 31 May 2009 22:33:58 +0100, "Arnaud Miege" <...@nospam.mathworks.co.uk
"pierre " <...@fred.mathworks.com...
Which version are you using? This is only available in the latest version
(R2009a).
Arnaud
|
|
 |
|
 |
 |
|
 |
|
On Mon, 1 Jun 2009 15:08:02 +0000 (UTC), "pierre " <...@yopmail.com
"Arnaud Miege" <...@nospam.mathworks.co.uk
I'm unfortunately under R2007b...
++
pierre
|
|
 |
|
 |
 |
|
 |
|
On Wed, 3 Jun 2009 07:15:19 +0000 (UTC), "pierre " <...@yopmail.com
"pierre " <...@yopmail.com
So it looks like I'm trapped, right?
And muss choose between a very slow simulation with repeated calls to 'sim' or a faster discontinuous one based on accelerator modes...
Anyway, thanks very much, your advice really helped. I'm gonna see how to handle this.
However, any ideas? Do not hesitate to post, I'm reading you!
pierre
|
|
 |
|
 |
 |
|
 |
|
On Wed, 3 Jun 2009 09:49:27 +0100, "Arnaud Miege" <...@nospam.mathworks.co.uk
"pierre " <...@fred.mathworks.com...
I think you have 2 options:
* upgrade to R2009a if you can and use the new SimState functionality
* contact technical support to see if something can be done in 7b to speed
up. I still don't understand why it takes longer when run progammatically,
all others things being equal.
Arnaud
|
|
 |
|
 |
|
|