MapleSim CodeGeneration - Compiled Procedure
details of the compiled module of the MapleSim model
A := MapleSim:-LinkModel(...)
cProc := A:-GetCompiledProc(...)
arbitrary name assigned the module returned by GetCompiledProc
some export of module cProc (see Exports, below)
(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.
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.
Return the objective procedure (see SetObjective, below). If no objective procedure is assigned, the string "NONE" is returned.
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.
Return a list of equations of the available parameters of the model. Each equation has the form name=value.
Return a list of equations of the simulation settings of the system. Each equation has the form name=value.
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.
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:
Creating a module with the compiled version of the model.
Displaying the module exports:
Displaying the output signals stored in the returned matrix.
Executing the compiled procedure:
simData≔0.−0.0.05025125628140700.007735249521200850.1005025125628140.02965038813511750.1507537688442210.06279050336450660.2010050251256280.1030947046225570.2512562814070350.1457993504140160.3015075376884420.1859106539024980.3517587939698490.2186999259983500.4020100502512560.2401725027976940.4522613065326630.247464113277155⋮⋮200 × 2 Matrix
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.
Display the initial conditions of the module:
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≔0.1.0.05025125628140700.9085160084619880.1005025125628140.8338565169670160.1507537688442210.7731749315912970.2010050251256280.7225064792621560.2512562814070350.6771718527824750.3015075376884420.6322502385664930.3517587939698490.5830749699967800.4020100502512560.5257028718175240.4522613065326630.457310914231265⋮⋮100 × 2 Matrix
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.
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.
Now, set the initial condition of SC1_v to -1 and leave the initial condition of I1_p_i at its default (0).
The cProc module has no available parameters, as can be seen by callings its GetParameters export:
To determine what model parameters are available, call the GetParameters export of A, the linked model, with the allparams option.
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.
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.
Executing and plotting the new procedure:
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.
Now, the the C and L parameters are set to 2.5 and 1, respectively while the R parameter keeps its original value, 5.
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.
Displaying the current simulation settings:
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.
Pass cProc to ParameterSweep, varying C from 5 to 50, and return a record of the results.
Plot the results.
Pass the string "NONE" to SetObjective to clear the objective procedure so subsequent calls return the full data set.
Download Help Document
What kind of issue would you like to report? (Optional)