From: Paul BIGNIER Date: Tue, 26 Mar 2013 12:50:48 +0000 (+0100) Subject: Indenting scicos_simulate and xcos_simulate X-Git-Tag: 5.5.0-beta-1~850 X-Git-Url: http://gitweb.scilab.org/?p=scilab.git;a=commitdiff_plain;h=80d8f67a755d751e4ed6c322c6a9a0324dcde493 Indenting scicos_simulate and xcos_simulate Formatting those files in order for the future changes to be visible Change-Id: I3e0685cd939695668d091288357caddf114b184d --- diff --git a/scilab/modules/scicos/macros/scicos_auto/scicos_simulate.sci b/scilab/modules/scicos/macros/scicos_auto/scicos_simulate.sci index e1b868b..8c93680 100644 --- a/scilab/modules/scicos/macros/scicos_auto/scicos_simulate.sci +++ b/scilab/modules/scicos/macros/scicos_auto/scicos_simulate.sci @@ -22,334 +22,334 @@ // function Info = scicos_simulate(scs_m, Info, updated_vars, flag, Ignb) -// Function for running scicos simulation in batch mode -// Info = scicos_simulate(scs_m[,Info][,updated_vars][,flag][,Ignb]) -// -// scs_m: scicos diagram (obtained by "load file.cos"). Note that -// the version of file.cos must be the current version. If not, load -// into scicos and save. -// -// updated_vars: a scilab struct containing values of -// symbolic variables used in the context and Scicos blocks. This -// is often used to change a parameter in the diagram context. In that -// case, make sure that in the diagram context the variable is defined such -// that it can be modified. Say a variable "a" is to be defined in the -// context having value 1, and later in batch mode, we want to change -// the value of "a". In that case, in the context of the diagram place: -// if ~exists('a') then a=1,end -// If you want then to run the simulation in batch mode using the value -// a=2, set: -// updated_vars.a=2 -// -// Info: a list. It must be list() at the first call, then use output -// Info as input Info for the next calls. Info contains compilation and -// simulation information and is used to avoid recompilation when not -// needed. -// -// flag: string. If it equals 'nw' (no window), then blocks using -// graphical windows are not executed. Note that the list of such -// blocks must be updated as new blocks are added. -// -// Ignb : matrix of string : The name of blocks to ignore. -// If flag is set and equal to 'nw' then Ignb contains -// name of blocks that are added to the list -// of blocks to ignore. -// + // Function for running scicos simulation in batch mode + // Info = scicos_simulate(scs_m[,Info][,updated_vars][,flag][,Ignb]) + // + // scs_m: scicos diagram (obtained by "load file.cos"). Note that + // the version of file.cos must be the current version. If not, load + // into scicos and save. + // + // updated_vars: a scilab struct containing values of + // symbolic variables used in the context and Scicos blocks. This + // is often used to change a parameter in the diagram context. In that + // case, make sure that in the diagram context the variable is defined such + // that it can be modified. Say a variable "a" is to be defined in the + // context having value 1, and later in batch mode, we want to change + // the value of "a". In that case, in the context of the diagram place: + // if ~exists('a') then a=1,end + // If you want then to run the simulation in batch mode using the value + // a=2, set: + // updated_vars.a=2 + // + // Info: a list. It must be list() at the first call, then use output + // Info as input Info for the next calls. Info contains compilation and + // simulation information and is used to avoid recompilation when not + // needed. + // + // flag: string. If it equals 'nw' (no window), then blocks using + // graphical windows are not executed. Note that the list of such + // blocks must be updated as new blocks are added. + // + // Ignb : matrix of string : The name of blocks to ignore. + // If flag is set and equal to 'nw' then Ignb contains + // name of blocks that are added to the list + // of blocks to ignore. + // + + + //** check/set rhs parameters + //--------------------------- + if argn(2) == 1 then + //scicos_simulate(scs_m) + Info = list() + updated_vars = struct() + flag = "" + Ignb = "" + elseif argn(2) == 2 then + //scicos_simulate(scs_m, Info) + //or scicos_simulate(scs_m,updated_vars) + //or scicos_simulate(scs_m,flag) + select typeof(Info) + case "st" then //scicos_simulate(scs_m,updated_vars) + updated_vars = Info + Info=list() + ku=2 + flag = "" + case "string" then //scicos_simulate(scs_m,flag) + flag=Info + Info=list() + kf=2 + updated_vars = struct() + else //scicos_simulate(scs_m,Info) + flag = "" + updated_vars = struct() + ki=2 + end + Ignb = "" + elseif argn(2) == 3 then + //scicos_simulate(scs_m, Info,updated_vars) or scicos_simulate(scs_m, Info,"nw") + if type(updated_vars) == 10 then + flag = updated_vars + updated_vars = struct() + else + ku=3 + flag = "" + end + Ignb = "" + elseif argn(2) >= 4 then + //scicos_simulate(scs_m, Info,updated_vars,"nw") or + //scicos_simulate(scs_m, Info,"nw",updated_vars) + ku=3;kf=4 + if type(updated_vars) == 10 then + [updated_vars,flag]=(flag,updated_vars) + kf=3;ku=4 + end + if argn(2) == 4 then Ignb = "",end + else + error(msprintf(_("%s: Wrong number of input arguments. Must be between %d and %d.\n"),... + "scicos_simulate", 1, 5)) + end + + //Check variable types + if typeof(scs_m)<>"diagram" then + error(msprintf(_("%s: Wrong type for input argument #%d: %s data structure expected.\n"),... + "scicos_simulate",1,"scs_m")) + end + if type(Info)<>15 then + error(msprintf(_("%s: Wrong type for input argument #%d: A list expected.\n"),.. + "scicos_simulate",2)) + end + if typeof(updated_vars)<>"st" then + error(msprintf(_("%s: Wrong type for input argument #%d: A list expected.\n"),... + "scicos_simulate",ku)) + end + if and(stripblanks(flag)<>["nw" ""]) then + error(msprintf(_("%s: Wrong value for input argument #%d: ''%s'' expected.\n"),... + "scicos_simulate",kf,"nw")) + end + if type(Ignb) <> 10 then + error(msprintf(_("%s: Wrong type for input argument #%d: String array expected.\n"),... + "scicos_simulate",5)) + end + + if or(sciargs() == "-nogui")|or(sciargs() == "-nwni") then + flag = "nw" + end + //**blocks to ignore requested by user + Ignb = matrix(Ignb,1,-1)//make it row vector + + //**blocks to ignore in any cases + Ignore=["affich",... + "affich2"] + //**blocks to ignore with flag=="nw" + Ignoreb = ["bouncexy", ... + "cscope", ... + "cmscope", ... + "canimxy", ... + "canimxy3d", ... + "cevscpe", ... + "cfscope", ... + "cscopxy", ... + "cscopxy3d", ... + "cmatview", ... + "cmat3d"] + + //** load the scicos function libraries + //------------------------------------ + + if exists("scicos_scicoslib")==0 then + load("SCI/modules/scicos/macros/scicos_scicos/lib") ; + end + + if exists("scicos_autolib")==0 then + load("SCI/modules/scicos/macros/scicos_auto/lib") ; + end + if exists("scicos_utilslib")==0 then + load("SCI/modules/scicos/macros/scicos_utils/lib") ; + end -//** check/set rhs parameters -//--------------------------- - if argn(2) == 1 then - //scicos_simulate(scs_m) - Info = list() - updated_vars = struct() - flag = "" - Ignb = "" - elseif argn(2) == 2 then - //scicos_simulate(scs_m, Info) - //or scicos_simulate(scs_m,updated_vars) - //or scicos_simulate(scs_m,flag) - select typeof(Info) - case "st" then //scicos_simulate(scs_m,updated_vars) - updated_vars = Info - Info=list() - ku=2 - flag = "" - case "string" then //scicos_simulate(scs_m,flag) - flag=Info - Info=list() - kf=2 - updated_vars = struct() - else //scicos_simulate(scs_m,Info) - flag = "" - updated_vars = struct() - ki=2 + // Define Scicos data tables =========================================== + [modelica_libs, scicos_pal_libs, %scicos_with_grid, %scs_wgrid] = initial_scicos_tables(); + // ===================================================================== + + //** initialize a "scicos_debug_gr" variable + %scicos_debug_gr = %f; + + + //** redefine some functions + prot = funcprot();funcprot(0); + do_terminate = do_terminate1 + funcprot(prot) + + + //check version + current_version = get_scicos_version() + scicos_ver = find_scicos_version(scs_m) + + //do version + if scicos_ver <> current_version then + ierr = execstr("scs_m = do_version(scs_m, scicos_ver)","errcatch") + if ierr <> 0 then + messagebox(_("Can''t convert old diagram (problem in version)"),"modal") + return + end end - Ignb = "" - elseif argn(2) == 3 then - //scicos_simulate(scs_m, Info,updated_vars) or scicos_simulate(scs_m, Info,"nw") - if type(updated_vars) == 10 then - flag = updated_vars - updated_vars = struct() + + //** prepare from and to workspace stuff + //------------------------------------- + scicos_workspace_init() + + if flag == "nw" then + Ignore = [Ignore,Ignoreb] + end + //add user ignored blocks if any + Ignore = [Ignore, Ignb] + + //** retrieve Info list + if Info <> list() then + [%tcur, %cpr, alreadyran, needstart, needcompile, %state0] = Info(:) else - ku=3 - flag = "" + %tcur = 0; + %cpr = list(); + alreadyran = %f; + needstart = %t; + needcompile = 4; + %state0 = list(); end - Ignb = "" - elseif argn(2) >= 4 then - //scicos_simulate(scs_m, Info,updated_vars,"nw") or - //scicos_simulate(scs_m, Info,"nw",updated_vars) - ku=3;kf=4 - if type(updated_vars) == 10 then - [updated_vars,flag]=(flag,updated_vars) - kf=3;ku=4 + + //** set solver + tolerances = scs_m.props.tol + solver = tolerances(6) + %scicos_solver = solver + //** set variables of context + [%scicos_context, ierr] = script2var(scs_m.props.context,struct()) + //overload %scicos_context variables with those defined by updated_vars + contextvars=fieldnames(%scicos_context) + updatedvars=fieldnames(updated_vars) + for k=1:size(updatedvars,"*") + u=updatedvars(k) + if or(u==contextvars) then + %scicos_context(u)=updated_vars(u) + else + mprintf(_("Warning the variable %s does not match any context variable name\nignored"),u) + end end - if argn(2) == 4 then Ignb = "",end - else - error(msprintf(_("%s: Wrong number of input arguments. Must be between %d and %d.\n"),... - "scicos_simulate", 1, 5)) - end - - //Check variable types - if typeof(scs_m)<>"diagram" then - error(msprintf(_("%s: Wrong type for input argument #%d: %s data structure expected.\n"),... - "scicos_simulate",1,"scs_m")) - end - if type(Info)<>15 then - error(msprintf(_("%s: Wrong type for input argument #%d: A list expected.\n"),.. - "scicos_simulate",2)) - end - if typeof(updated_vars)<>"st" then - error(msprintf(_("%s: Wrong type for input argument #%d: A list expected.\n"),... - "scicos_simulate",ku)) - end - if and(stripblanks(flag)<>["nw" ""]) then - error(msprintf(_("%s: Wrong value for input argument #%d: ''%s'' expected.\n"),... - "scicos_simulate",kf,"nw")) - end - if type(Ignb) <> 10 then - error(msprintf(_("%s: Wrong type for input argument #%d: String array expected.\n"),... - "scicos_simulate",5)) - end - - if or(sciargs() == "-nogui")|or(sciargs() == "-nwni") then - flag = 'nw' - end - //**blocks to ignore requested by user - Ignb = matrix(Ignb,1,-1)//make it row vector - - //**blocks to ignore in any cases - Ignore=['affich',... - 'affich2'] - //**blocks to ignore with flag=="nw" - Ignoreb = ['bouncexy', ... - 'cscope', ... - 'cmscope', ... - 'canimxy', ... - 'canimxy3d', ... - 'cevscpe', ... - 'cfscope', ... - 'cscopxy', ... - 'cscopxy3d', ... - 'cmatview', ... - 'cmat3d'] - - //** load the scicos function libraries - //------------------------------------ - - if exists('scicos_scicoslib')==0 then - load("SCI/modules/scicos/macros/scicos_scicos/lib") ; - end - - if exists('scicos_autolib')==0 then - load("SCI/modules/scicos/macros/scicos_auto/lib") ; - end - - if exists('scicos_utilslib')==0 then - load("SCI/modules/scicos/macros/scicos_utils/lib") ; - end - -// Define Scicos data tables =========================================== - [modelica_libs, scicos_pal_libs, %scicos_with_grid, %scs_wgrid] = initial_scicos_tables(); - // ===================================================================== - - //** initialize a "scicos_debug_gr" variable - %scicos_debug_gr = %f; - - - //** redefine some functions - prot = funcprot();funcprot(0); - do_terminate = do_terminate1 - funcprot(prot) - - - //check version - current_version = get_scicos_version() - scicos_ver = find_scicos_version(scs_m) - - //do version - if scicos_ver <> current_version then - ierr = execstr('scs_m = do_version(scs_m, scicos_ver)','errcatch') - if ierr <> 0 then - messagebox(_("Can''t convert old diagram (problem in version)"),"modal") - return + if ierr == 0 then //++ no error + [scs_m, %cpr, needcompile, ok] = do_eval(scs_m, %cpr,%scicos_context) + if ~ok then + error(msprintf(gettext("%s: Error during block parameters evaluation.\n"), "scicos_simulate")); + end + if needcompile <> 4 & size(%cpr) > 0 then + %state0 = %cpr.state + end + alreadyran = %f + else + error(["Incorrect context definition, " + lasterror()]) end - end - - //** prepare from and to workspace stuff - //------------------------------------- - scicos_workspace_init() - - if flag == 'nw' then - Ignore = [Ignore,Ignoreb] - end - //add user ignored blocks if any - Ignore = [Ignore, Ignb] - - //** retrieve Info list - if Info <> list() then - [%tcur, %cpr, alreadyran, needstart, needcompile, %state0] = Info(:) - else - %tcur = 0; - %cpr = list(); - alreadyran = %f; - needstart = %t; - needcompile = 4; - %state0 = list(); - end - - //** set solver - tolerances = scs_m.props.tol - solver = tolerances(6) - %scicos_solver = solver - //** set variables of context - [%scicos_context, ierr] = script2var(scs_m.props.context,struct()) - //overload %scicos_context variables with those defined by updated_vars - contextvars=fieldnames(%scicos_context) - updatedvars=fieldnames(updated_vars) - for k=1:size(updatedvars,'*') - u=updatedvars(k) - if or(u==contextvars) then - %scicos_context(u)=updated_vars(u) + + if %cpr == list() then + need_suppress =%t else - mprintf(_("Warning the variable %s does not match any context variable name\nignored"),u) + need_suppress = %f end - end - if ierr == 0 then //++ no error - [scs_m, %cpr, needcompile, ok] = do_eval(scs_m, %cpr,%scicos_context) + + [%cpr, %state0_n, needcompile, alreadyran, ok] = do_update(%cpr, ... + %state0, needcompile) + if ~ok then - error(msprintf(gettext("%s: Error during block parameters evaluation.\n"), "scicos_simulate")); + error(msprintf(gettext("%s: Error during block parameters update.\n"), "scicos_simulate")); end - if needcompile <> 4 & size(%cpr) > 0 then - %state0 = %cpr.state + + if or(%state0_n <> %state0) then //initial state has been changed + %state0 = %state0_n + [alreadyran, %cpr] = do_terminate1(scs_m, %cpr) + choix = [] end - alreadyran = %f - else - error(['Incorrect context definition, ' + lasterror()]) - end - - if %cpr == list() then - need_suppress =%t - else - need_suppress = %f - end - - [%cpr, %state0_n, needcompile, alreadyran, ok] = do_update(%cpr, ... - %state0, needcompile) - - if ~ok then - error(msprintf(gettext("%s: Error during block parameters update.\n"), "scicos_simulate")); - end - - if or(%state0_n <> %state0) then //initial state has been changed - %state0 = %state0_n - [alreadyran, %cpr] = do_terminate1(scs_m, %cpr) - choix = [] - end - if (%cpr.sim.xptr($) - 1) < size(%cpr.state.x,'*') & solver < 100 then - warning(msprintf(_("Diagram has been compiled for implicit solver\nswitching to implicit solver.\n"))) - solver = 100 - tolerances(6) = solver - elseif (%cpr.sim.xptr($) - 1) == size(%cpr.state.x,'*') & ... - solver == 100 & size(%cpr.state.x,'*') <> 0 then - warning(msprintf(_("Diagram has been compiled for explicit solver\nswitching to explicit solver.\n"))) - solver = 0 - tolerances(6) = solver - end - - if need_suppress then //this is done only once - for i = 1:length(%cpr.sim.funs) - if type(%cpr.sim.funs(i)) <> 13 then - if find(%cpr.sim.funs(i)(1) == Ignore) <> [] then - %cpr.sim.funs(i)(1) = 'trash'; - end - end + if (%cpr.sim.xptr($) - 1) < size(%cpr.state.x,"*") & solver < 100 then + warning(msprintf(_("Diagram has been compiled for implicit solver\nswitching to implicit solver.\n"))) + solver = 100 + tolerances(6) = solver + elseif (%cpr.sim.xptr($) - 1) == size(%cpr.state.x,"*") & ... + solver == 100 & size(%cpr.state.x,"*") <> 0 then + warning(msprintf(_("Diagram has been compiled for explicit solver\nswitching to explicit solver.\n"))) + solver = 0 + tolerances(6) = solver end - end - if needstart then //scicos initialization - if alreadyran then - [alreadyran, %cpr] = do_terminate1(scs_m, %cpr) - alreadyran = %f - end - %tcur = 0 - %cpr.state = %state0 - tf = scs_m.props.tf; - if tf*tolerances == [] then - error(_("Simulation parameters not set:")); + if need_suppress then //this is done only once + for i = 1:length(%cpr.sim.funs) + if type(%cpr.sim.funs(i)) <> 13 then + if find(%cpr.sim.funs(i)(1) == Ignore) <> [] then + %cpr.sim.funs(i)(1) = "trash"; + end + end + end end - ierr = execstr('[state, t] = scicosim(%cpr.state, %tcur, tf, %cpr.sim,' + .. - '''start'', tolerances)','errcatch') - if ierr <> 0 then - error(_("Initialisation problem:")) + + if needstart then //scicos initialization + if alreadyran then + [alreadyran, %cpr] = do_terminate1(scs_m, %cpr) + alreadyran = %f + end + %tcur = 0 + %cpr.state = %state0 + tf = scs_m.props.tf; + if tf*tolerances == [] then + error(_("Simulation parameters not set:")); + end + ierr = execstr("[state, t] = scicosim(%cpr.state, %tcur, tf, %cpr.sim," + .. + "''start'', tolerances)","errcatch") + if ierr <> 0 then + error(_("Initialisation problem:")) + end + %cpr.state = state end - %cpr.state = state - end - - ierr = execstr('[state, t] = scicosim(%cpr.state, %tcur, tf, %cpr.sim,' + .. - '''run'', tolerances)','errcatch') - if ierr == 0 then - %cpr.state = state - alreadyran = %t - if (tf - t) < tolerances(3) then - needstart = %t - [alreadyran, %cpr] = do_terminate1(scs_m, %cpr) + + ierr = execstr("[state, t] = scicosim(%cpr.state, %tcur, tf, %cpr.sim," + .. + "''run'', tolerances)","errcatch") + if ierr == 0 then + %cpr.state = state + alreadyran = %t + if (tf - t) < tolerances(3) then + needstart = %t + [alreadyran, %cpr] = do_terminate1(scs_m, %cpr) + else + %tcur = t + end else - %tcur = t + error([_("Simulation problem: ");lasterror()]) + end + + Info = list(%tcur, %cpr, alreadyran, needstart, needcompile, %state0) + + [txt, files] = returntoscilab() + n = size(files,1) + for i = 1:n + load(TMPDIR + "/Workspace/" + files(i)) + execstr(files(i) + " = struct('"values'", x, '"time'", t)") end - else - error([_("Simulation problem: ");lasterror()]) - end - - Info = list(%tcur, %cpr, alreadyran, needstart, needcompile, %state0) - - [txt, files] = returntoscilab() - n = size(files,1) - for i = 1:n - load(TMPDIR + '/Workspace/' + files(i)) - execstr(files(i) + ' = struct('"values'", x, '"time'", t)') - end - execstr(txt) + execstr(txt) endfunction function [alreadyran, %cpr] = do_terminate1(scs_m, %cpr) -// Copyright INRIA + // Copyright INRIA - if prod(size(%cpr)) < 2 then - alreadyran = %f - return - end + if prod(size(%cpr)) < 2 then + alreadyran = %f + return + end - par = scs_m.props; + par = scs_m.props; - if alreadyran then - alreadyran = %f - // terminate current simulation - ierr = execstr('[state, t] = scicosim(%cpr.state, par.tf, par.tf, %cpr.sim, ''finish'', par.tol)', 'errcatch') + if alreadyran then + alreadyran = %f + // terminate current simulation + ierr = execstr("[state, t] = scicosim(%cpr.state, par.tf, par.tf, %cpr.sim, ''finish'', par.tol)", "errcatch") - %cpr.state = state - if ierr <> 0 then - error([_("End problem: ");lasterror()]) + %cpr.state = state + if ierr <> 0 then + error([_("End problem: ");lasterror()]) + end end - end endfunction diff --git a/scilab/modules/xcos/macros/xcos_simulate.sci b/scilab/modules/xcos/macros/xcos_simulate.sci index 7aea80c..0638176 100644 --- a/scilab/modules/xcos/macros/xcos_simulate.sci +++ b/scilab/modules/xcos/macros/xcos_simulate.sci @@ -12,372 +12,372 @@ function %cpr = xcos_simulate(scs_m, needcompile) -// Load the block libs if not defined - prot = funcprot(); - funcprot(0); + // Load the block libs if not defined + prot = funcprot(); + funcprot(0); if ~exists("scicos_diagram") then loadXcosLibs(); end - funcprot(prot); - [modelica_libs, scicos_pal_libs, %scicos_with_grid, %scs_wgrid] = initial_scicos_tables(); + funcprot(prot); + [modelica_libs, scicos_pal_libs, %scicos_with_grid, %scs_wgrid] = initial_scicos_tables(); // Hook according to SEP066 function [ok]=invoke_pre_simulate(fun, scs_m, needcompile) ok=%f; - ierr=execstr('[continueSimulation]='+fun+'(scs_m, needcompile);', 'errcatch'); + ierr=execstr("[continueSimulation]="+fun+"(scs_m, needcompile);", "errcatch"); if ierr<>0 then disp(_("Error occurred in pre_xcos_simulate: Cancelling simulation.")); [str,n,line,func]=lasterror(); - mprintf(' at line %d of function %s\n', line, func); + mprintf(" at line %d of function %s\n", line, func); return end if ~continueSimulation then return end ok=%t; - + // force update on the parent in case of scoped modification scs_m=resume(scs_m); endfunction - - if isdef("pre_xcos_simulate") then - if type(pre_xcos_simulate) == 15 then - // if has a multiple implementation (on a list) - for f=pre_xcos_simulate; - ok=invoke_pre_simulate(f, scs_m, needcompile); - if ~ok then - %cpr=[]; - return; - end - end - else - // if has a unique implementation - ok=invoke_pre_simulate("pre_xcos_simulate", scs_m, needcompile); - if ~ok then - %cpr=[]; - return; - end - end - end - - //**---- prepare from and to workspace stuff ( "From workspace" block ) - scicos_workspace_init() - - -//** extract tolerances from scs_m.props.tol - tolerances = scs_m.props.tol ; - //** extract solver type from tolerances - solver = tolerances(6) ; - //** initialize a "scicos_debug_gr" variable - %scicos_debug_gr = %f; - - //////////////////////////////////////////////////////////////// - // Add global environment variable so that scicos is not lost // - //////////////////////////////////////////////////////////////// - if needcompile == 4 then - %state0 = list(); - needcompile = 4; - curwin = 1000; - %cpr = struct(); - %tcur = 0; - %cpr.state = %state0; - alreadyran = %f; - else - %state0 = %cpr.state; - alreadyran = %f; - end - - tf = scs_m.props.tf; - %zoom = 1.4; - Select = []; - - //** extract tolerances from scs_m.props.tol - tolerances = scs_m.props.tol ; - //** extract solver type from tolerances - solver = tolerances(6) ; - - // Propagate context through all blocks - %scicos_context = struct(); - context = scs_m.props.context; - //** context eval here - [%scicos_context, ierr] = script2var(context, %scicos_context); - - //for backward compatibility for scifunc - if ierr==0 then - %mm = getfield(1,%scicos_context) - for %mi=%mm(3:$) - ierr = execstr(%mi+'=%scicos_context(%mi)','errcatch') - if ierr<>0 then - break; //** in case of error exit - end + + if isdef("pre_xcos_simulate") then + if type(pre_xcos_simulate) == 15 then + // if has a multiple implementation (on a list) + for f=pre_xcos_simulate; + ok=invoke_pre_simulate(f, scs_m, needcompile); + if ~ok then + %cpr=[]; + return; + end + end + else + // if has a unique implementation + ok=invoke_pre_simulate("pre_xcos_simulate", scs_m, needcompile); + if ~ok then + %cpr=[]; + return; + end + end end - end - //end of for backward compatibility for scifuncpagate context values - - [scs_m,%cpr,needcompile,ok] = do_eval(scs_m, %cpr, %scicos_context); - if ~ok then - msg = msprintf(gettext("%s: Error during block parameters evaluation.\n"), "Xcos"); - messagebox(msg, "Xcos", "error"); - error(msprintf(gettext("%s: Error during block parameters evaluation.\n"), "xcos_simulate")); - end - - //** update parameters or compilation results - [%cpr,%state0_n,needcompile,alreadyran,ok] = do_update(%cpr,%state0,needcompile) - if ~ok then - error(msprintf(gettext("%s: Error during block parameters update.\n"), "xcos_simulate")); - end - - //** if alreadyran then set the var choice - if alreadyran then - choix = ['Continue';'Restart';'End'] - else - choix = [] ; - end - - issequal = %t; - //** initial state has been changed - if ~isequal(%state0_n,%state0) then - issequal = %f - else - //** test typeof outtb element - for i=1:lstsize(%state0_n.outtb) - if typeof(%state0_n.outtb(i))<>typeof(%state0.outtb(i)) - issequal = %f - break - end + + //**---- prepare from and to workspace stuff ( "From workspace" block ) + scicos_workspace_init() + + + //** extract tolerances from scs_m.props.tol + tolerances = scs_m.props.tol ; + //** extract solver type from tolerances + solver = tolerances(6) ; + //** initialize a "scicos_debug_gr" variable + %scicos_debug_gr = %f; + + //////////////////////////////////////////////////////////////// + // Add global environment variable so that scicos is not lost // + //////////////////////////////////////////////////////////////// + if needcompile == 4 then + %state0 = list(); + needcompile = 4; + curwin = 1000; + %cpr = struct(); + %tcur = 0; + %cpr.state = %state0; + alreadyran = %f; + else + %state0 = %cpr.state; + alreadyran = %f; + end + + tf = scs_m.props.tf; + %zoom = 1.4; + Select = []; + + //** extract tolerances from scs_m.props.tol + tolerances = scs_m.props.tol ; + //** extract solver type from tolerances + solver = tolerances(6) ; + + // Propagate context through all blocks + %scicos_context = struct(); + context = scs_m.props.context; + //** context eval here + [%scicos_context, ierr] = script2var(context, %scicos_context); + + //for backward compatibility for scifunc + if ierr==0 then + %mm = getfield(1,%scicos_context) + for %mi=%mm(3:$) + ierr = execstr(%mi+"=%scicos_context(%mi)","errcatch") + if ierr<>0 then + break; //** in case of error exit + end + end + end + //end of for backward compatibility for scifuncpagate context values + + [scs_m,%cpr,needcompile,ok] = do_eval(scs_m, %cpr, %scicos_context); + if ~ok then + msg = msprintf(gettext("%s: Error during block parameters evaluation.\n"), "Xcos"); + messagebox(msg, "Xcos", "error"); + error(msprintf(gettext("%s: Error during block parameters evaluation.\n"), "xcos_simulate")); + end + + //** update parameters or compilation results + [%cpr,%state0_n,needcompile,alreadyran,ok] = do_update(%cpr,%state0,needcompile) + if ~ok then + error(msprintf(gettext("%s: Error during block parameters update.\n"), "xcos_simulate")); + end + + //** if alreadyran then set the var choice + if alreadyran then + choix = ["Continue";"Restart";"End"] + else + choix = [] ; end - //** test typeof oz element - for i=1:lstsize(%state0_n.oz) - if typeof(%state0_n.oz(i))<>typeof(%state0.oz(i)) + + issequal = %t; + //** initial state has been changed + if ~isequal(%state0_n,%state0) then issequal = %f - break - end + else + //** test typeof outtb element + for i=1:lstsize(%state0_n.outtb) + if typeof(%state0_n.outtb(i))<>typeof(%state0.outtb(i)) + issequal = %f + break + end + end + //** test typeof oz element + for i=1:lstsize(%state0_n.oz) + if typeof(%state0_n.oz(i))<>typeof(%state0.oz(i)) + issequal = %f + break + end + end end - end - - //** if state have changed - //** finish the simulation via do_terminate() - if ~issequal then - %state0 = %state0_n - [alreadyran,%cpr] = do_terminate() - choix = [] - end - - //** switch appropriate solver - if %cpr.sim.xptr($)-10) then - message(["Diagram has been compiled for explicit solver" - "switching to explicit Solver"]) - solver = 0 ; //** Magic number - tolerances(6) = solver ; //** save Magic number solver type - end - - //** ask user what to do - if choix<>[] then - //** open dialog box - to_do = choose(choix,"What do you want to do") - - //** if cancel then exit - if to_do==0 then - ok = %f - return + + //** if state have changed + //** finish the simulation via do_terminate() + if ~issequal then + %state0 = %state0_n + [alreadyran,%cpr] = do_terminate() + choix = [] end - select choix(to_do) + //** switch appropriate solver + if %cpr.sim.xptr($)-10) then + message(["Diagram has been compiled for explicit solver" + "switching to explicit Solver"]) + solver = 0 ; //** Magic number + tolerances(6) = solver ; //** save Magic number solver type + end - case "Continue" then - needstart = %f ; - state = %cpr.state ; + //** ask user what to do + if choix<>[] then + //** open dialog box + to_do = choose(choix,"What do you want to do") - case "Restart" then - needstart = %t ; - state = %state0 ; + //** if cancel then exit + if to_do==0 then + ok = %f + return + end - case "End" then - state = %cpr.state ; - needstart = %t ; - tf = scs_m.props.tf; + select choix(to_do) - //Alan: Cannot call do_terminate() here ? - //Answer: No, because do_terminate() doesn't - // return control to the user + case "Continue" then + needstart = %f ; + state = %cpr.state ; - //** run scicosim via 'finish' flag - ierr = execstr('[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,'+.. - '''finish'',tolerances)','errcatch') + case "Restart" then + needstart = %t ; + state = %state0 ; - %cpr.state = state - alreadyran = %f + case "End" then + state = %cpr.state ; + needstart = %t ; + tf = scs_m.props.tf; - //** error case - if ierr<>0 then - str_err = split_lasterror(lasterror()); - kfun = curblock() - corinv = %cpr.corinv + //Alan: Cannot call do_terminate() here ? + //Answer: No, because do_terminate() doesn't + // return control to the user - if kfun<>0 then //** block error - path = corinv(kfun) - //** get error cmd for the block - get_errorcmd(path,'End problem.',str_err); + //** run scicosim via 'finish' flag + ierr = execstr("[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,"+.. + "''finish'',tolerances)","errcatch") - else //** simulator error - message(["End problem:";str_err]) - //scf(curwin); - end - ok = %f + %cpr.state = state + alreadyran = %f + + //** error case + if ierr<>0 then + str_err = split_lasterror(lasterror()); + kfun = curblock() + corinv = %cpr.corinv + + if kfun<>0 then //** block error + path = corinv(kfun) + //** get error cmd for the block + get_errorcmd(path,"End problem.",str_err); + + else //** simulator error + message(["End problem:";str_err]) + //scf(curwin); + end + ok = %f + end + + return end - return + else //** Normal first start simulation + + needstart = %t + state = %state0 + end - else //** Normal first start simulation + //gh_win = gcf(); - needstart = %t - state = %state0 + //** scicos initialization + if needstart then + //** if the simulation has already run + //** and is not finished then call do_terminate + if alreadyran then + [alreadyran,%cpr] = do_terminate() + alreadyran = %f ; + end + //** set initial values for a new simulation + %tcur = 0 + %cpr.state = %state0 + tf = scs_m.props.tf; + if tf*tolerances==[] then + message(["Simulation parameters not set";"use setup button"]); + return; + end - end + //** Run the normal first start simulation here - //gh_win = gcf(); + //** run scicosim via 'start' flag + ierr = execstr("[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,"+.. + "''start'',tolerances)","errcatch") - //** scicos initialization - if needstart then - //** if the simulation has already run - //** and is not finished then call do_terminate - if alreadyran then - [alreadyran,%cpr] = do_terminate() - alreadyran = %f ; - end - //** set initial values for a new simulation - %tcur = 0 - %cpr.state = %state0 - tf = scs_m.props.tf; - if tf*tolerances==[] then - message(["Simulation parameters not set";"use setup button"]); - return; - end + %cpr.state = state ; //** save the state + //** error case + if ierr<>0 then + str_err=split_lasterror(lasterror()); - //** Run the normal first start simulation here + kfun=curblock() + corinv=%cpr.corinv + if kfun<>0 then //** block error + path=corinv(kfun) + //** get error cmd for the block + disp(str_err); + get_errorcmd(path,gettext("Initialisation problem"),str_err); - //** run scicosim via 'start' flag - ierr = execstr('[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,'+.. - '''start'',tolerances)','errcatch') - - %cpr.state = state ; //** save the state - //** error case - if ierr<>0 then - str_err=split_lasterror(lasterror()); - - kfun=curblock() - corinv=%cpr.corinv - if kfun<>0 then //** block error - path=corinv(kfun) - //** get error cmd for the block - disp(str_err); - get_errorcmd(path,gettext('Initialisation problem'),str_err); - - - else //** simulator error - message(['Initialisation problem:';str_err]) - //scf(curwin); - end - - ok = %f; - //xset('window',curwin) - return - end - //scf(gh_win); - //xset('window',win); - end - //** scicos simulation - tf = scs_m.props.tf + else //** simulator error + message(["Initialisation problem:";str_err]) + //scf(curwin); + end - // inform Xcos the simulator is going to run - xcosSimulationStarted(); + ok = %f; + //xset('window',curwin) + return + end + //scf(gh_win); + //xset('window',win); + end - //** run scicosim via 'start' flag - ierr = execstr('[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,'+.. - '''run'',tolerances)','errcatch') + //** scicos simulation + tf = scs_m.props.tf - %cpr.state = state + // inform Xcos the simulator is going to run + xcosSimulationStarted(); - //** no error - if ierr==0 then - alreadyran = %t; - //** we are at the end of the simulation - //** finish the simulation via do_terminate() - if tf-t0 then + //** run scicosim via 'start' flag + ierr = execstr("[state,t]=scicosim(%cpr.state,%tcur,tf,%cpr.sim,"+.. + "''run'',tolerances)","errcatch") + + %cpr.state = state + + //** no error + if ierr==0 then + alreadyran = %t; + //** we are at the end of the simulation + //** finish the simulation via do_terminate() + if tf-t0 then + str_err = split_lasterror(lasterror()); + + kfun = curblock() + corinv = %cpr.corinv + + if kfun<>0 then //** block error + path = corinv(kfun) + //** get error cmd for the block + get_errorcmd(path,gettext("End problem"),str_err); + else //** simulator error + message(["End problem:";str_err]) + //scf(curwin); + end + end + else + %tcur = t; + end + //** error case + else str_err = split_lasterror(lasterror()); - kfun = curblock() - corinv = %cpr.corinv + alreadyran = %f; + kfun = curblock(); + corinv = %cpr.corinv; if kfun<>0 then //** block error - path = corinv(kfun) - //** get error cmd for the block - get_errorcmd(path,gettext('End problem'),str_err); + path = corinv(kfun); + //** get error cmd for the block + get_errorcmd(path,gettext("Simulation problem"),str_err); else //** simulator error - message(['End problem:';str_err]) - //scf(curwin); + message(["Simulation problem:";str_err]) + //scf(curwin); end - end - else - %tcur = t; - end - //** error case - else - str_err = split_lasterror(lasterror()); - - alreadyran = %f; - kfun = curblock(); - corinv = %cpr.corinv; - - if kfun<>0 then //** block error - path = corinv(kfun); - //** get error cmd for the block - get_errorcmd(path,gettext("Simulation problem"),str_err); - else //** simulator error - message(['Simulation problem:';str_err]) - //scf(curwin); - end - ok = %f; - end - - //restore saved variables in Scilab environment ( "To workspace" block ) - [txt,files]=returntoscilab() - n=size(files,1) - for i=1:n - load(TMPDIR+'/Workspace/'+files(i)) - ierr = execstr(files(i)+'=struct('"values'",x,'"time'",t)', "errcatch") - if ierr <> 0 - str_err = split_lasterror(lasterror()); - message(['Simulation problem:';str_err]); + ok = %f; end - end + //restore saved variables in Scilab environment ( "To workspace" block ) + [txt,files]=returntoscilab() + n=size(files,1) + for i=1:n + load(TMPDIR+"/Workspace/"+files(i)) + ierr = execstr(files(i)+"=struct('"values'",x,'"time'",t)", "errcatch") + if ierr <> 0 + str_err = split_lasterror(lasterror()); + message(["Simulation problem:";str_err]); + end + + end // Hook according to SEP066 function ok=invoke_post_simulate(fun, %cpr, scs_m, needcompile) ok=%f; - ierr=execstr(fun+'(%cpr, scs_m, needcompile);', 'errcatch'); + ierr=execstr(fun+"(%cpr, scs_m, needcompile);", "errcatch"); if ierr<>0 then disp(_("Error in post_xcos_simulate: ending simulation.")) return @@ -386,34 +386,34 @@ function %cpr = xcos_simulate(scs_m, needcompile) // force update on the parent in case of scoped modification scs_m=resume(scs_m); endfunction - + if isdef("post_xcos_simulate") then - if type(post_xcos_simulate) == 15 then - // if has a multiple implementation (on a list) - for f=post_xcos_simulate; - ok=invoke_post_simulate(f, scs_m, needcompile); - if ~ok then - %cpr=[]; - return; - end - end - else - // if has a unique implementation - ok=invoke_post_simulate("post_xcos_simulate", %cpr, scs_m, needcompile); - if ~ok then - %cpr=[]; - return; - end - end + if type(post_xcos_simulate) == 15 then + // if has a multiple implementation (on a list) + for f=post_xcos_simulate; + ok=invoke_post_simulate(f, scs_m, needcompile); + if ~ok then + %cpr=[]; + return; + end + end + else + // if has a unique implementation + ok=invoke_post_simulate("post_xcos_simulate", %cpr, scs_m, needcompile); + if ~ok then + %cpr=[]; + return; + end + end end - // finally restore the exported variables on the parent context - if ~isempty(txt) then - ierr = execstr(txt, "errcatch") - if ierr <> 0 then - str_err = split_lasterror(lasterror()); - message(['Simulation problem while executing <'+txt+'>:';str_err]); + // finally restore the exported variables on the parent context + if ~isempty(txt) then + ierr = execstr(txt, "errcatch") + if ierr <> 0 then + str_err = split_lasterror(lasterror()); + message(["Simulation problem while executing <"+txt+">:";str_err]); + end end - end endfunction