Wolfram System Modeler & Mathematica

Quick Start

Wolfram Research
training@wolfram.com

Model Representation

Get a diagram representation of a model:

motor = SystemModel["IntroductoryExamples.MultiDomain.DCMotor"];
motor[{"Diagram",ImageSize->Large,Frame->False}]

Retrieve the model equations of a simple model:

SystemModel["DocumentationExamples.Basic.DiffEq","SystemEquations",t]

motorWithLoad = SystemModel["DocumentationExamples.Simulation.HybridMotor"];
motorWithLoad[{"Diagram",ImageSize->Large,Frame->False}]

Simulate

Analyze a DC motor model:

sim=SystemModelSimulate[motorWithLoad,40]

Plot the angular velocity of the DC motor output inertia:

SystemModelPlot[sim,{"Inertia1.w","Inertia2.w"},PlotRange->All,Filling->Axis]

Retrieve a value for the output variable at time 2 seconds:

sim[{"Inertia2.w"},2]

Parameter Comparison

Use automatic parallel simulation to compare different parameter sets:

sim2=SystemModelSimulate[motorWithLoad,{0,10},
<|"ParameterValues"->{"SpringDamper1.d"->{0.1,1,10}}|>]

Easily compare the different parameter scenarios:

SystemModelPlot[sim2,{"Inertia2.w"},PlotRange->All]

Sensitivity

Find out which parameters the motor speed is sensitive to:

parList={"resistor1.R","inductor1.L","EMF1.k","inertia1.J"};

sim3=SystemModelSimulateSensitivity[motor,parList]

The EMF coefficient has a large influence on the motor speed, whereas the resistance, inductance and moment of inertia do not affect it that much:

Table[SystemModelPlot[sim3,{{"inertia1.w",par,0.2}},
PlotLabel->par,PlotRange->{All,{0,1.4}},PlotLegends->None],{par,parList}]

Calibration: Import Data

Import measurement data from a file:

data=Import["ExampleData/inertiaTestData.dat"];

Show[ListPlot[data],SystemModelPlot[sim,{"Inertia1.w"},PlotStyle->Red],PlotRange->All]

Calibration: Define Objective

Set up a criteria function for model fitting:

criteriaFun[pv:{Verbatim[Rule][_?StringQ,_?NumericQ]..},var_?StringQ]:=
Module[{f,tmax=40},
f=SystemModelSimulate[motorWithLoad,tmax,<|"ParameterValues"->pv|>][{var}][[1]];
((Norm[Table[data[[i,2]]-f[data[[i,1]]],{i,Length[data]}]])^2)
]

Calibration: Fit Parameter to Test Data

Vary resistance and damping to fit the test data:

fittedpars=
FindMinimum[
criteriaFun[{"Resistor1.R"->r,"SpringDamper1.d"->d},"Inertia2.w"],
{{r,0.1},{d,0.25}}]

Simulate with the fitted parameter:

sim4=SystemModelSimulate[motorWithLoad,40,<|"ParameterValues"->({"Resistor1.R"->r,"SpringDamper1.d"->d}/.fittedpars[[2]])|>];

Show[SystemModelPlot[sim4,{"Inertia2.w"},PlotStyle->Red,PlotRange->All],ListPlot[data]]

Create a model of the Lotka–Volterra equations, modeling a predator-prey relationship:

eqs={pred'[t]==pred[t](δ prey[t]-γ),prey'[t]==prey[t](α-β pred[t])};

Define initial values for the two states:

ieqs={prey[0]==10,pred[0]==10};

Define parameter values:

pars={α->0.1,β->0.02,γ->0.4,δ->0.02};

Create the model:

mmodel=CreateSystemModel["LotkaVolterra",Join[eqs,ieqs],t,<|"ParameterValues"->pars|>];

SystemModel[mmodel,"ModelicaDisplay"]

Show the levels of predators and prey over time:

sim6=SystemModelSimulate[mmodel,100];

SystemModelPlot[sim6,{"pred","prey"}]

TransferFunctionModel and StateSpaceModel objects can be translated to corresponding Modelica components:

CreateSystemModel["NewModel1",(StateSpaceModelFull[0, 1, 1, 0, , , ])]

CreateSystemModel["NewModel2",(TransferFunctionModelFull[((1+2 s)/(1+s)), , , ])]

Create a simple electrical circuit:

components={"R"∈"Modelica.Electrical.Analog.Basic.Resistor","L"∈"Modelica.Electrical.Analog.Basic.Inductor","AC"∈"Modelica.Electrical.Analog.Sources.SineVoltage","G"∈"Modelica.Electrical.Analog.Basic.Ground"};

connections={"G.p"->"AC.n","AC.p"->"L.n","L.p"->"R.n","R.p"->"AC.n"};

mmodel=ConnectSystemModelComponents["SimpleCircuit",components,connections]

SystemModelPlot[{mmodel,5},{"L.i","L.v"}]

This function makes it easy to automate model creation. Try several different voltage sources:

voltageSources = SystemModels["*Analog.Sources.*Voltage"]

sources = Delete[voltageSources,{{1},{-1}}]

Start by creating a function that creates the circuit model, given a specific source:

createCircuit[source_,{index_Integer}]:= 
Module[{components,connections,modelName},
components = {
"R"∈"Modelica.Electrical.Analog.Basic.Resistor",
"L"∈"Modelica.Electrical.Analog.Basic.Inductor",
"src"∈source["ModelName"],
"G"∈"Modelica.Electrical.Analog.Basic.Ground"};
connections = {"G.p"->"src.n","src.p"->"L.n","L.p"->"R.n","R.p"->"src.n"};
modelName= "MyTests.SimpleCircuitTest"<>ToString[index];
ConnectSystemModelComponents[modelName,components,connections,GraphLayout->None];
modelName
]

Create models:

testmodels=MapIndexed[createCircuit,sources];

Simulate all test models:

sims=SystemModelSimulate[#,{0,1}]& /@ testmodels;

Plot the currents through the inductor for all tests:

SystemModelPlot[sims,{"L.i"}]

Model Design

Model a Segway built out of LEGO components:

segwayModel="EducationExamples.ElectricalEngineering.LEGOSegway.Components.LEGOSegwayBase";

SystemModeler[segwayModel,"Modeling"]

Control Design: Linearize Model

Get a linear state-space model representation of the Segway. Linearize around an upright position:

ss=SystemModelLinearize [segwayModel,"InitialValues"]

Retrieve a list of all state names:

Part[ss,-1,2,3]

Keep only the state variables you are interested in:

sys=SystemsModelExtract[
ss,
All,
All,
Flatten[
FirstPosition[Part[ss,-1,2,3],#]&/@{
"wheelSet.der_theta1",
"wheelSet.der_theta2",
"revolute1.phi",
"revolute1.w",
"RightMotor.inductor.i",
"LeftMotor.inductor.i"
}]
]

Control Design: Design Controller

Define weight matrices for the control design. Use a large penalty for deviations in the angle of the Segway, "revolute1.phi":

q=(({{.1, 0, 0, 0, 0, 0}, {0, .1, 0, 0, 0, 0}, {0, 0, 50000, 0, 0, 0}, {0, 0, 0, 10, 0, 0}, {0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 1}}));
r=(({{10, 0}, {0, 10}}));

Compute the gains for the LQ controller:

L=LQRegulatorGains[sys,{q,r}]

Control Design: Verify Result

Verify that the eigenvalues of the system are in the left side of the plane:

clsys=SystemsModelStateFeedbackConnect[sys,L];
Eigenvalues[Normal[clsys][[1]]]//ColumnForm

Control Design: Save to Model

Save the computed values of the LQ controller to the model with the LQ controller:

SystemModeler["EducationExamples.ElectricalEngineering.LEGOSegway.LQControl","Modeling"]

segwayWithControl = SystemModel["EducationExamples.ElectricalEngineering.LEGOSegway.LQControl",<|"ParameterValues"-> {"L"->L}|>]

3D Animation

Simulate and show a 3D animation of the LEGO Segway:

SystemModeler[segwayWithControl,"Animation"]

Initialization

Color

Clear[m8red];
m8red[a_]:=RGBColor[0.811765,0.117647,0.145098,a];

Special Cells

Picture cell

PicturePrint[expr_,opts:OptionsPattern[Cell]]:=CellPrint[TextCell[expr,"Picture",opts,ShowStringCharacters->False]]

Overview mouseover

OverviewMouseover[header_,subs_]:=CellPrint@Cell[BoxData[ToBoxes@Mouseover[Framed[Style[header,"OverviewSection"],FrameMargins->{{15,15},{5,5}},BoxFrame->2,FrameStyle->None,RoundingRadius->5],

Evaluate@If[subs=="",Framed[Style[header,"OverviewSection",White],FrameMargins->{{15,15},{5,5}},BoxFrame->2,FrameStyle->None,RoundingRadius->5,Background->m8red[1]],Row[{Framed[Style[header,"OverviewSection",White],FrameMargins->{{15,15},{5,5}},FrameStyle->None,RoundingRadius->5,Background->m8red[1]]," » ",Framed[Style[subs,"OverviewSection"],FrameMargins->{{15,15},{5,5}},FrameStyle->m8red[1],BoxFrame->2,RoundingRadius->5,Background->White]}]]]],"OverviewSection"]

(*MapThread[OverviewMouseover,Transpose[{{"Chapter One","Chapter One topics, …"},{"Chapter Two","Chapter Two topics, …"},{"Chapter Three","Chapter Three topics, …"},{"Chapter Four","Chapter Four topics, …"}}]]*)

Large overview mouseover

LargeOverviewMouseover[header_,subs_]:=CellPrint@Cell[BoxData[ToBoxes@Mouseover[Framed[Style[header,"LargeOverviewSection"],FrameMargins->{{15,15},{5,5}},BoxFrame->2,FrameStyle->None,RoundingRadius->5],

Evaluate@If[subs=="",Framed[Style[header,"LargeOverviewSection",White],FrameMargins->{{15,15},{5,5}},BoxFrame->2,FrameStyle->None,RoundingRadius->5,Background->m8red[1]],Row[{Framed[Style[header,"LargeOverviewSection",White],FrameMargins->{{15,15},{5,5}},FrameStyle->None,RoundingRadius->5,Background->m8red[1]]," » ",Framed[Style[subs,"LargeOverviewSection"],FrameMargins->{{15,15},{5,5}},FrameStyle->m8red[1],BoxFrame->2,RoundingRadius->5,Background->White]}]]]],"LargeOverviewSection"]

MapThread[LargeOverviewMouseover,Transpose[{{"Introduction","Mathematica and System Modeler"},{"Simulation Analysis","Study a DC motor"},{"Model Analysis","LEGO segway control design"},{"All of Mathematica","Powerful computation"}}]];