2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution. The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 function %cpr = xcos_simulate(scs_m, needcompile)
15 // Load the block libs if not defined
18 if ~exists("scicos_diagram") then
22 [modelica_libs, scicos_pal_libs, %scicos_with_grid, %scs_wgrid] = initial_scicos_tables();
24 // Hook according to SEP066
25 function [ok]=invoke_pre_simulate(fun, scs_m, needcompile)
27 ierr=execstr('[continueSimulation]='+fun+'(scs_m, needcompile);', 'errcatch');
29 disp(_("Error occurred in pre_xcos_simulate: Cancelling simulation."));
30 [str,n,line,func]=lasterror();
31 mprintf(' at line %d of function %s\n', line, func);
34 if ~continueSimulation then
39 // force update on the parent in case of scoped modification
43 if isdef("pre_xcos_simulate") then
44 if type(pre_xcos_simulate) == 15 then
45 // if has a multiple implementation (on a list)
46 for f=pre_xcos_simulate;
47 ok=invoke_pre_simulate(f, scs_m, needcompile);
54 // if has a unique implementation
55 ok=invoke_pre_simulate("pre_xcos_simulate", scs_m, needcompile);
63 //**---- prepare from and to workspace stuff ( "From workspace" block )
64 scicos_workspace_init()
67 //** extract tolerances from scs_m.props.tol
68 tolerances = scs_m.props.tol ;
69 //** extract solver type from tolerances
70 solver = tolerances(6) ;
71 //** initialize a "scicos_debug_gr" variable
72 %scicos_debug_gr = %f;
74 ////////////////////////////////////////////////////////////////
75 // Add global environment variable so that scicos is not lost //
76 ////////////////////////////////////////////////////////////////
77 if needcompile == 4 then
94 //** extract tolerances from scs_m.props.tol
95 tolerances = scs_m.props.tol ;
96 //** extract solver type from tolerances
97 solver = tolerances(6) ;
99 // Propagate context through all blocks
100 %scicos_context = struct();
101 context = scs_m.props.context;
102 //** context eval here
103 [%scicos_context, ierr] = script2var(context, %scicos_context);
105 //for backward compatibility for scifunc
107 %mm = getfield(1,%scicos_context)
109 ierr = execstr(%mi+'=%scicos_context(%mi)','errcatch')
111 break; //** in case of error exit
115 //end of for backward compatibility for scifuncpagate context values
117 [scs_m,%cpr,needcompile,ok] = do_eval(scs_m, %cpr, %scicos_context);
119 msg = msprintf(gettext("%s: Error during block parameters evaluation.\n"), "Xcos");
120 messagebox(msg, "Xcos", "error");
121 error(msprintf(gettext("%s: Error during block parameters evaluation.\n"), "xcos_simulate"));
124 //** update parameters or compilation results
125 [%cpr,%state0_n,needcompile,alreadyran,ok] = do_update(%cpr,%state0,needcompile)
127 error(msprintf(gettext("%s: Error during block parameters update.\n"), "xcos_simulate"));
130 //** if alreadyran then set the var choice
132 choix = ['Continue';'Restart';'End']
138 //** initial state has been changed
139 if ~isequal(%state0_n,%state0) then
142 //** test typeof outtb element
143 for i=1:lstsize(%state0_n.outtb)
144 if typeof(%state0_n.outtb(i))<>typeof(%state0.outtb(i))
149 //** test typeof oz element
150 for i=1:lstsize(%state0_n.oz)
151 if typeof(%state0_n.oz(i))<>typeof(%state0.oz(i))
158 //** if state have changed
159 //** finish the simulation via do_terminate()
162 [alreadyran,%cpr] = do_terminate()
166 //** switch appropriate solver
167 if %cpr.sim.xptr($)-1<size(%cpr.state.x,'*') & solver<100 then
168 warning(["Diagram has been compiled for implicit solver"
169 "switching to implicit Solver"])
170 solver = 100 ; //** Magic number
171 tolerances(6) = solver ; //** save Magic number solver type
172 elseif (%cpr.sim.xptr($)-1==size(%cpr.state.x,'*')) & (solver==100 & size(%cpr.state.x,'*')<>0) then
173 message(["Diagram has been compiled for explicit solver"
174 "switching to explicit Solver"])
175 solver = 0 ; //** Magic number
176 tolerances(6) = solver ; //** save Magic number solver type
179 //** ask user what to do
182 to_do = choose(choix,"What do you want to do")
184 //** if cancel then exit
205 //Alan: Cannot call do_terminate() here ?
206 //Answer: No, because do_terminate() doesn't
207 // return control to the user
209 //** run scicosim via 'finish' flag
210 ierr = execstr('[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,'+..
211 '''finish'',tolerances)','errcatch')
218 str_err = split_lasterror(lasterror());
222 if kfun<>0 then //** block error
224 //** get error cmd for the block
225 get_errorcmd(path,'End problem.',str_err);
227 else //** simulator error
228 message(["End problem:";str_err])
237 else //** Normal first start simulation
246 //** scicos initialization
248 //** if the simulation has already run
249 //** and is not finished then call do_terminate
251 [alreadyran,%cpr] = do_terminate()
254 //** set initial values for a new simulation
258 if tf*tolerances==[] then
259 message(["Simulation parameters not set";"use setup button"]);
263 //** Run the normal first start simulation here
265 //** run scicosim via 'start' flag
266 ierr = execstr('[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,'+..
267 '''start'',tolerances)','errcatch')
269 %cpr.state = state ; //** save the state
272 str_err=split_lasterror(lasterror());
276 if kfun<>0 then //** block error
278 //** get error cmd for the block
280 get_errorcmd(path,gettext('Initialisation problem'),str_err);
283 else //** simulator error
284 message(['Initialisation problem:';str_err])
289 //xset('window',curwin)
293 //xset('window',win);
296 //** scicos simulation
299 // inform Xcos the simulator is going to run
300 xcosSimulationStarted();
302 //** run scicosim via 'start' flag
303 ierr = execstr('[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,'+..
304 '''run'',tolerances)','errcatch')
311 //** we are at the end of the simulation
312 //** finish the simulation via do_terminate()
313 if tf-t<tolerances(3) then
315 //Alan : removing do_terminate() here because it
316 // doesn't return control
317 //[alreadyran,%cpr]=do_terminate()
320 //** run scicosim via 'finish' flag
321 ierr = execstr('[state,t]=scicosim(%cpr.state,tf,tf,%cpr.sim,'+..
322 '''finish'',tolerances)','errcatch')
328 str_err = split_lasterror(lasterror());
333 if kfun<>0 then //** block error
335 //** get error cmd for the block
336 get_errorcmd(path,gettext('End problem'),str_err);
337 else //** simulator error
338 message(['End problem:';str_err])
347 str_err = split_lasterror(lasterror());
351 corinv = %cpr.corinv;
353 if kfun<>0 then //** block error
355 //** get error cmd for the block
356 get_errorcmd(path,gettext("Simulation problem"),str_err);
357 else //** simulator error
358 message(['Simulation problem:';str_err])
364 //restore saved variables in Scilab environment ( "To workspace" block )
365 [txt,files]=returntoscilab()
368 load(TMPDIR+'/Workspace/'+files(i))
369 ierr = execstr(files(i)+'=struct('"values'",x,'"time'",t)', "errcatch")
371 str_err = split_lasterror(lasterror());
372 message(['Simulation problem:';str_err]);
377 // Hook according to SEP066
378 function ok=invoke_post_simulate(fun, %cpr, scs_m, needcompile)
380 ierr=execstr(fun+'(%cpr, scs_m, needcompile);', 'errcatch');
382 disp(_("Error in post_xcos_simulate: ending simulation."))
386 // force update on the parent in case of scoped modification
390 if isdef("post_xcos_simulate") then
391 if type(post_xcos_simulate) == 15 then
392 // if has a multiple implementation (on a list)
393 for f=post_xcos_simulate;
394 ok=invoke_post_simulate(f, scs_m, needcompile);
401 // if has a unique implementation
402 ok=invoke_post_simulate("post_xcos_simulate", %cpr, scs_m, needcompile);
410 // finally restore the exported variables on the parent context
411 if ~isempty(txt) then
412 ierr = execstr(txt, "errcatch")
414 str_err = split_lasterror(lasterror());
415 message(['Simulation problem while executing <'+txt+'>:';str_err]);