MapleSim CodeGeneration - Compiled Procedure
details of the compiled module of the MapleSim model
Calling Sequence
Parameters
Description
Options
Exports
Examples
A := MapleSim:-LinkModel(...)
cProc := A:-GetCompiledProc(...)
cProc(opts)
cProc:-f(...)
cProc
-
arbitrary name assigned the module returned by GetCompiledProc
f
some export of module cProc (see Exports, below)
opts
(optional) equation(s) of the form option = value; specify options for cProc
When GetCompiledProc is called with option ccodeonly = false (the default) it returns a module that simulates the linked model using a compiled procedure. Reusing this procedure avoids the overhead of recompiling the model. The module allows changing some simulation settings and some model parameters, however, settings that select a different solver, or parameters that modify the structure of the model, cannot be changed without recompilation.
The module, cProc, has exported procedures that return the state of the module and modify its operation. See the Exports section for descriptions of these procedures.
The module, cProc, acts like a normal Maple procedure: it accepts arguments and returns a result. The following bullets describes the operation.
All arguments to cProc are keyword options; see the Options section for descriptions of the available options.
The call cProc(...) returns a Matrix of the simulation data:
Each row of the matrix corresponds to one time sample.
The first column of the matrix is time. The remaining columns correspond to the signals returned by GetOutputs.
If GetCompiledProc was called with the witherror option, cProc(...) returns two elements:
The matrix previously described.
Any error message generated during the integration. An empty string means no error was generated.
The SetObjective export assigns a procedure that modifies what is returned. See the Exports section.
cProc takes the keyword options listed below. Unless otherwise stated, the default for each option comes from same option used by GetCompiledProc and can be inspected with cProc:-GetSettings() (see below). The availability of some options depends on whether the solver specified for GetCompiledProc was a fixed-step or variable-step solver.
fixed-step solvers: Euler, RK2, RK3, RK4, ImplicitEuler.
variable-step solvers: RKF45, CK45, Rosenbrock.
abserr = nonnegative
Specifies the absolute tolerance for the solver. See relerr. This is only available with a variable-step solver.
ds = numeric
Specifies the sample time, in seconds. This is the duration between consecutive data steps in the generated matrix. For a variable-step solver the default is (tf-t0)/minpoints; for a fixed-step solver the default is dt.
dt = numeric
Specifies the time step. This is only available with a fixed-step solver. The default comes from the stepsize option to GetCompiledProc.
ics = list of numbers or list/set of equations
Specifies the initial conditions.
A list of numbers must match the order of the system's state variables; see the GetICs export, in the Exports section.
A list or set of equations of the form x⁡t0=numeric, can specify initial conditions for just the state variables that differ from the default values.
params = list of numbers or list/set of equations
Specifies the values of parameters.
A list of numbers must match the order of the system's parameters; see the GetParameters export, in the Exports section.
A list or set of equations of the form k=numeric, can specify just the parameters that differ from the default values.
relerr = nonnegative
Specifies the relative tolerance for the solver. See abserr. This is only available with a variable-step solver.
returntype = datapoint, simrec, or list of those
Specifies what is returned; it can be one of the symbols datapoint or simrec, or a list of those symbols. If a list, then a list of the corresponding values is returned. The default is datapoint.
datapoint : return a Matrix of data
simrec : return a simulation record. This can only be used if GetCompiledProc was called with the addoutputs option set to "all" or "continuous".
settings = list or set of equations
Specify additional solver settings. The available settings depend on the calling arguments to GetCompiledProc. The following is supported:
baumgarte = [alpha,beta]
The alpha and beta values are numeric values. This setting is available if the active model's Baumgarte option was selected under the Settings tab in the MapleSim GUI, or GetCompiledProc was called with baumgarte option set to a non-empty list.
t0 = numeric
Specifies the simulation start time, in seconds.
tf = numeric
Specifies the simulation final time, in seconds.
The following procedures are exports of the module returned by GetCompiledProc. Most take no arguments. They are accessed in the usual way, for example, cProc:-GetICs() or cProc[GetICS](), where cProc is the name to which the module was assigned.
GetICs()
Return a list of equations of the initial conditions of the system. Each equation has the form x⁡t0=value, where t0 is the simulation start time.
GetObjective()
Return the objective procedure (see SetObjective, below). If no objective procedure is assigned, the string "NONE" is returned.
GetOutputs()
Return a list of the outputs of the system. The list is in the same order as columns two through M of the returned M-column matrix. Each term has the form x⁡t.
GetParameters()
Return a list of equations of the available parameters of the model. Each equation has the form name=value.
GetSettings()
Return a list of equations of the simulation settings of the system. Each equation has the form name=value.
SetObjective(obj,threadsafe)
Assign a procedure that modifies the return value.
The argument obj must be a Maple procedure, or other appliable expression.
When cProc is called, obj is passed the matrix of data that would otherwise be returned. The expression returned by obj is returned by the call to cProc.
If the module was generated with the witherror option to GetCompiledProc, an error string is also returned.
The keyword parameter threadsafe is optional. Passing the name threadsafe, or the equation threadsafe = true indicates that the objective procedure is threadsafe and can be safely run in parallel using the Threads package. This is used by the ParameterSweep and MonteCarlo procedures. The default is false.
If obj is the string "NONE", the assigned procedure is cleared.
Threadsafe()
Return true if Cproc is assumed threadsafe, false otherwise. Normally this is true, but when the SetObjective procedure has been called to create an objective procedure, the Threadsafe procedure then depends on the value of the threadsafe option passed to SetObjective.
Linking to a MapleSim model located in the following .msim file:
file≔cat⁡kernelopts⁡toolboxdir=MapleSim,/data/examples/RLCcircuit.msim:
A≔MapleSim:-LinkModel⁡filename=file:
Creating a module with the compiled version of the model.
cProc≔A:-GetCompiledProc⁡:
Displaying the module exports:
exports⁡cProc
GetICs,GetObjective,GetOutputs,GetParameters,GetSettings,GetDLLName,ModuleApply,SetObjective,Threadsafe
Displaying the output signals stored in the returned matrix.
cProc:-GetOutputs⁡
Vout⁡t
Executing the compiled procedure:
simData≔cProc⁡
Plotting the data. The indices to simData select the rows and columns to plot. The first index, 1..-1, is a range that selects the rows of interest (all of them). The second index, [1, 2], selects the first column, t, and the second column, Vout⁡t, so that the plot is Vout⁡t versus t. The labels option passed to plot specifies the axes labels.
plot⁡simData1..−1,1,2,labels=t,Vout
Selected options
Display the initial conditions of the module:
cProc:-GetICs⁡
I1_p_i⁡0=0.,SC1_v⁡0=0.
Select a simulation time of five seconds (tf = 5.0) and set the initial conditions of the state variables to 1.0 and -1.0 respectively.
simData≔cProc⁡tf=5.0,ics=1.0,−1.0
Because, in this simple example, simData has just two columns, the output of cProc can be passed directly to plot without specifying the rows or columns as was previously done.
plot⁡simData
Set the initial condition of I1_p_i to 2 and leave the initial condition of SC1_v at its default (0). Use a sample step of 0.1 seconds (ds = 0.1); this returns fewer rows of data than the default, so the plot is somewhat coarser.
plot⁡cProc⁡tf=5.0,ds=0.1,ics=I1_p_i⁡0=2
Now, set the initial condition of SC1_v to -1 and leave the initial condition of I1_p_i at its default (0).
plot⁡cProc⁡tf=5.0,ds=0.1,ics=SC1_v⁡0=−1
The cProc module has no available parameters, as can be seen by callings its GetParameters export:
cProc:-GetParameters⁡
To determine what model parameters are available, call the GetParameters export of A, the linked model, with the allparams option.
A:-GetParameters⁡allparams
C=1,L=1,T=600320,R1_alpha=0,R=1,S1_amplitude=1,S1_f=1,S1_offset=0,S1_phase=0
To generate a new compiled module with accessible parameters, call A's GetCompiledProc export with the params option. Here the parameters C, L, and R are selected and given new values. The new compiled module is assigned to cProc.
cProc≔A:-GetCompiledProc⁡params=C=5,L=2,R=5:
To verify that the new module has the selected parameters, call its GetParameters export. Note that the order of the parameters matches that of the call to GetCompiledProc.
C=5,L=2,R=5
Executing and plotting the new procedure:
plot⁡cProc⁡
The params option of cProc can be used to modify parameters without recompiling. Here the R parameter is set to 0.1, while the C and L parameters keep their values, 5 and 2, respectively.
plot⁡cProc⁡params=R=0.1
Now, the the C and L parameters are set to 2.5 and 1, respectively while the R parameter keeps its original value, 5.
plot⁡cProc⁡params=C=2.5,L=1
The params option of cProc can also be called without specifying the names of the parameters. With this call, the values are in a list and the order matches that of the order as returned by GetParameters.
plot⁡cProc⁡params=1,2,3
Settings
Displaying the current simulation settings:
cProc:-GetSettings⁡
reduceevents=false,optimize=fast,diagnosticinfo=false,compile=false,plotevents=true,ix1relfac=1,numericjacobian=false,maxstep=0,minpoints=200,evtiter=100,ix1errctl=false,tf=10,abserr=110000000,ds=10199,relerr=110000000,method=RKF45,ratehyst=110000000000,minstep=0,scalemethod=none,coniter=50,t0=0
Objective Procedure
The SetObjective export of cProc assigns a procedure that modifies the data cProc returns. Here it assigns a procedure that returns the minimum value of Vout(t) (the first output, which is in column 2 of the matrix). The threadsafe option indicates that this procedure can be safely executed in parallel.
cProc:-SetObjective(proc(M) min(M[.., 2]) end proc, threadsafe):
Compute the minimum value.
cProc⁡
−0.0580100521407440
Pass cProc to ParameterSweep, varying C from 5 to 50, and return a record of the results.
rec≔MapleSim:-Analysis:-ParameterSweep⁡cProc,C=5..50,num_points=20,ret_record:
Plot the results.
plot⁡rec:-values1,rec:-data,style=pointline,labels=C,min(Vout)
Pass the string "NONE" to SetObjective to clear the objective procedure so subsequent calls return the full data set.
cProc:-SetObjective⁡NONE:
See Also
MapleSim
MapleSim[Analysis][MonteCarlo]
MapleSim[Analysis][ParameterSweep]
MapleSim[LinkModel]
MapleSim[LinkModel][GetCompiledProc]
Download Help Document