Skip to content

Commit 72f68ea

Browse files
committed
Adapt examples to new ModiaLang/Modia3D versions
1 parent 5444724 commit 72f68ea

15 files changed

+89
-80
lines changed

examples/BouncingSphere3D_1.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module BouncingSphere3D_1
22

33
using Modia
4+
using ModiaLang.StaticArrays
45

5-
BouncingSphere = Model(
6+
BouncingSphere = Model3D(
67
boxHeigth = 0.1,
78
groundMaterial = VisualMaterial(color="DarkGreen", transparency=0.5),
89
gravField = UniformGravityField(g=9.81, n=[0, -1, 0]),
@@ -24,10 +25,10 @@ BouncingSphere = Model(
2425
solidMaterial="Steel",
2526
massProperties=MassPropertiesFromShapeAndMass(mass=0.001),
2627
collision=true)),
27-
free = FreeMotion(obj1=:world, obj2=:sphere, r=Var(init=[0.0, 1.0, 0.0]))
28+
free = FreeMotion(obj1=:world, obj2=:sphere, r=Var(init=SVector{3,Float64}(0.0, 1.0, 0.0)))
2829
)
2930

30-
bouncingSphere = @instantiateModel(buildModia3D(BouncingSphere), unitless=true, log=false, logStateSelection=false, logCode=false)
31+
bouncingSphere = @instantiateModel(BouncingSphere, unitless=true, log=false, logStateSelection=false, logCode=false)
3132

3233
#@show bouncingSphere.parameterExpressions
3334
#@show bouncingSphere.parameters
@@ -36,7 +37,8 @@ stopTime = 2.2
3637
dtmax = 0.1
3738
tolerance = 1e-8
3839
requiredFinalStates = [0.0, 0.10092547226369847, 0.0, 0.0, 0.01950941258387679, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
39-
simulate!(bouncingSphere, stopTime=stopTime, tolerance=tolerance, dtmax=dtmax, log=true, logStates=true, logEvents=true, requiredFinalStates=requiredFinalStates)
40+
simulate!(bouncingSphere, stopTime=stopTime, tolerance=tolerance, dtmax=dtmax, log=true, logStates=true, logEvents=true,
41+
requiredFinalStates_atol=0.01, requiredFinalStates=requiredFinalStates)
4042

4143
@usingModiaPlot
4244
plot(bouncingSphere, "free.r", figure=1)

examples/BouncingSphere3D_2.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module BouncingSphere3D_2
22

33
using Modia
4+
using ModiaLang.StaticArrays
45
@usingModiaPlot
56

6-
BouncingSphere = Model(
7+
BouncingSphere = Model3D(
78
boxHeigth = 0.05,
89
groundMaterial = VisualMaterial(color="DarkGreen", transparency=0.5),
910
sphereMaterial = VisualMaterial(color="Red"),
@@ -27,16 +28,17 @@ BouncingSphere = Model(
2728
sphere = Object3D(feature=Solid(shape=Sphere(diameter=0.2),
2829
visualMaterial=:sphereMaterial, solidMaterial="Steel",
2930
collision=true)),
30-
free = FreeMotion(obj1=:world, obj2=:sphere, r=Var(init=[0.0, 1.0, 0.0]), w=Var(init=[10.0, 0.0, -5.0]))
31+
free = FreeMotion(obj1=:world, obj2=:sphere, r=Var(init=SVector{3,Float64}(0.0, 1.0, 0.0)), w=Var(init=SVector{3,Float64}(10.0, 0.0, -5.0)))
3132
)
3233

33-
bouncingSphere = @instantiateModel(buildModia3D(BouncingSphere), unitless=true, log=false, logStateSelection=false, logCode=false)
34+
bouncingSphere = @instantiateModel(BouncingSphere, unitless=true, log=false, logStateSelection=false, logCode=false)
3435

3536

3637
stopTime = 2.7
3738
tolerance = 1e-8
38-
requiredFinalStates = [0.28711931505126853, -0.9780916472966511, -0.20833195055744314, 0.10925258092251605, -3.8123627743313273, -0.9364166852211897, 1.228619049097046, -0.5724547180688829, 0.22566245156307535, -7.694985753129207, 0.3826986158402523, 5.519005772512668]
39-
simulate!(bouncingSphere, stopTime=stopTime, tolerance=tolerance, log=true, logStates=true, logEvents=true, requiredFinalStates=requiredFinalStates)
39+
requiredFinalStates = [0.29302652789657424, -1.2392524758992525, -0.13171402091799628, 0.11357140027502943, -4.417276610857313, -0.8729857092038694, 2.3810648360145152, -0.426312211044484 , 0.13656197405807574, -7.335181005993924, 0.812877179123953, 4.870682494334355]
40+
simulate!(bouncingSphere, stopTime=stopTime, tolerance=tolerance, log=true, logStates=true, logEvents=true,
41+
requiredFinalStates_rtol = 0.2, requiredFinalStates_atol = 0.2, requiredFinalStates=requiredFinalStates)
4042

4143
plot(bouncingSphere, ["free.r" "free.rot"; "free.v" "free.w"], figure=1)
4244

examples/Mobile3D.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ function createMobile(depth)
8686
)
8787
end
8888
end
89-
Mobile = Model(
89+
Mobile = Model3D(
9090
world = Object3D(feature=Scene(gravityField=UniformGravityField(g=9.81, n=[0, -1, 0]), enableContactDetection=false, nominalLength=0.2*barLength(depthMax), enableVisualization=enableVisualization, animationFile=animationFile)),
9191
worldFrame = Object3D(parent = :world, feature = Visual(shape = CoordinateSystem(length=rodLength))),
9292
top = createMobile(depthMax),
9393
rev0 = RevoluteWithDamping(obj1 = :world, obj2 = :(top.rod.frame1), phi_start=0.2)
9494
)
9595

96-
mobile = @instantiateModel(buildModia3D(Mobile), unitless=true, log=false, logDetails=false, logModel=false, logStateSelection=false, logCode=false, logExecution=false, logTiming=false)
96+
mobile = @instantiateModel(Mobile, unitless=true, log=false, logDetails=false, logModel=false, logStateSelection=false, logCode=false, logExecution=false, logTiming=false)
9797

9898
stopTime = 5.0
9999
tolerance = 1e-5

examples/MotorControl.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ println("\nMotorControl: Demonstrating the ability to simulate hierarchical mixe
44

55
using Modia
66
@usingModiaPlot
7-
using Measurements
7+
using ModiaLang.Measurements
88
export MotorControl2
99

1010
include("$(Modia.modelsPath)/Blocks.jl")

examples/Pendulum.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module PendulumSimulation
22

33
using Modia
4+
using ModiaLang.Measurements
45
@usingModiaPlot
5-
using Test
6+
67

78
Pendulum = Model(
89
L = 0.8u"m",
@@ -23,7 +24,6 @@ pendulum = @instantiateModel(Pendulum)
2324
simulate!(pendulum, Tsit5(), stopTime = 10.0u"s", log=true)
2425
plot(pendulum, [("phi", "w"); "r"])
2526

26-
using Measurements
2727
PendulumWithUncertainties = Pendulum | Map(L = (0.8 ± 0.2)u"m",
2828
m = (1.0 ± 0.2)u"kg",
2929
d = (0.5 ± 0.2)u"N*m*s/rad")
@@ -36,17 +36,18 @@ plot(pendulum2, [("phi", "w"); "r"], figure = 2)
3636

3737

3838
# Linearize
39-
using DoubleFloats
40-
using Measurements
4139
println("\n... Numerically linearize at stopTime = 10 with Float64 and Double64:")
4240
(A_10, x_10) = linearize!(pendulum2, stopTime=10)
4341

42+
#= DoubleFloats is not necessarily defined in the environment
43+
using ModiaLang.DoubleFloats
4444
pendulum3 = SimulationModel{Measurement{Double64}}(pendulum2)
4545
(A_10_Double64, x_10_Double64) = linearize!(pendulum3, stopTime=10)
46+
=#
4647

4748
xNames = get_xNames(pendulum2)
4849
@show xNames
4950
println(IOContext(stdout, :error_digits=>15), "A_10 = ", A_10, ", x_10 = ", x_10)
50-
println(IOContext(stdout, :error_digits=>15), "A_10_Double64 = ", A_10_Double64, ", x_10_Double64 = ", x_10_Double64)
51+
#println(IOContext(stdout, :error_digits=>15), "A_10_Double64 = ", A_10_Double64, ", x_10_Double64 = ", x_10_Double64)
5152

5253
end

examples/Pendulum3D_1.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ println("... Pendulum3D_1.jl: Simple pendulum modeled with multibody components"
55
using Modia
66
@usingModiaPlot
77

8-
Pendulum = Model(
8+
Pendulum = Model3D(
99
world = Object3D(feature=Scene()),
1010
body = Object3D(feature=Solid(massProperties=MassProperties(mass=1.0))),
1111
bodyFrame = Object3D(parent=:body, translation=[-0.5, 0.0, 0.0]),
1212
rev = Revolute(obj1=:world, obj2=:bodyFrame)
1313
)
1414

15-
pendulum = @instantiateModel(buildModia3D(Pendulum))
15+
pendulum = @instantiateModel(Pendulum, unitless=true)
1616
simulate!(pendulum, stopTime=3.0)
1717
plot(pendulum, "rev.phi")
1818
end

examples/Pendulum3D_2.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ println("... Pendulum3D_2 - simple pendulum modeled with multibody components wi
55
using Modia
66
@usingModiaPlot
77

8-
Pendulum = Model(
8+
Pendulum = Model3D(
99
world = Object3D(feature=Scene(animationFile="Pendulum2.json")),
1010
obj1 = Object3D(feature=Solid(shape=Box(lengthX=1.0, lengthY=0.2, lengthZ=0.2),
1111
solidMaterial="Steel", visualMaterial=VisualMaterial(color="Blue"))),
@@ -14,7 +14,7 @@ Pendulum = Model(
1414
rev = Revolute(obj1=:world, obj2=:obj2)
1515
)
1616

17-
pendulum = @instantiateModel(buildModia3D(Pendulum), unitless=true)
17+
pendulum = @instantiateModel(Pendulum, unitless=true)
1818
simulate!(pendulum, stopTime=3.0)
1919
plot(pendulum, "rev.phi")
2020
end

examples/Pendulum3D_3.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using Modia
77
# Modia equation-based models
88
include("$(Modia.modelsPath)/AllModels.jl")
99

10-
Pendulum = Model(
10+
Pendulum = Model3D(
1111
world = Object3D(feature=Scene(animationFile="Pendulum3.json")),
1212
obj1 = Object3D(feature=Solid(shape=Box(lengthX=1.0, lengthY=0.2, lengthZ=0.2),
1313
solidMaterial="Steel", visualMaterial=VisualMaterial(color="Blue"))),
@@ -21,7 +21,7 @@ Pendulum = Model(
2121
(damper.flange_a, fixed.flange)]
2222
)
2323

24-
pendulum = @instantiateModel(buildModia3D(Pendulum), unitless=true)
24+
pendulum = @instantiateModel(Pendulum, unitless=true)
2525
simulate!(pendulum, stopTime=3.0)
2626

2727
@usingModiaPlot

examples/Rectifier.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ IdealDiode = OnePort | Model(
2626

2727
Rectifier1 = Model(
2828
R1 = Resistor | Map(R=1.0u"Ω"),
29-
R2 = Resistor | Map(R=100.0u"Ω"),
29+
R2 = Resistor | Map(R=10.0u"Ω"),
3030
C = Capacitor | Map(C=0.1u"F", v=Var(init=0.0u"V")),
3131
D = IdealDiode,
3232
V = SineVoltage | Map(V=5.0u"V", f=1.5u"Hz"),
@@ -40,7 +40,7 @@ Rectifier1 = Model(
4040
)
4141

4242
rectifier1 = @instantiateModel(Rectifier1)
43-
@time simulate!(rectifier1, Tsit5(), stopTime = 3, requiredFinalStates=[4.5665505086780565])
43+
simulate!(rectifier1, Tsit5(), stopTime = 3, requiredFinalStates=[3.509663836166844])
4444
plot(rectifier1, [("V.v", "D.v", "C.v"), "D.i"], figure=1)
4545

4646

@@ -64,10 +64,10 @@ Rectifier2 = Model(
6464
)
6565

6666
rectifier2 = @instantiateModel(Rectifier2, unitless=true)
67-
@time simulate!(rectifier2, Tsit5(), stopTime = 0.1, log=true, requiredFinalStates=[183.9899542497182])
67+
simulate!(rectifier2, Tsit5(), stopTime = 0.1, tolerance=1e-8, log=true, requiredFinalStates=[183.9899542497182]) # Wrong result if tolerance = 1e-6
6868
plot(rectifier2, [("V.v", "C.v"), "V.i"], figure=2)
6969

70-
@time simulate!(rectifier2, stopTime = 0.1, log=true) #, requiredFinalStates=[183.9899542497182])
70+
simulate!(rectifier2, stopTime = 0.1, log=true, requiredFinalStates=[183.9899542497182])
7171
plot(rectifier2, [("V.v", "C.v"), "V.i"], figure=3)
7272

7373
end

examples/Rectifier.mo

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package Rectifier
33
model Rectifier1
44
extends Modelica.Icons.Example;
55

6-
Modelica.Electrical.Analog.Sources.SineVoltage sineVoltage(V=5, freqHz=1.5)
7-
annotation (Placement(transformation(
6+
Modelica.Electrical.Analog.Sources.SineVoltage V(V=5, f=1.5) annotation (
7+
Placement(transformation(
88
extent={{-10,10},{10,-10}},
99
rotation=270,
1010
origin={-50,30})));
@@ -15,104 +15,109 @@ package Rectifier
1515
extent={{-10,-10},{10,10}},
1616
rotation=270,
1717
origin={30,30})));
18-
Modelica.Electrical.Analog.Basic.Capacitor capacitor(C=0.1) annotation (
19-
Placement(transformation(
18+
Modelica.Electrical.Analog.Basic.Capacitor C(C=0.1) annotation (Placement(
19+
transformation(
2020
extent={{-10,-10},{10,10}},
2121
rotation=-90,
2222
origin={52,30})));
2323
Modelica.Electrical.Analog.Basic.Ground ground
2424
annotation (Placement(transformation(extent={{-60,-20},{-40,0}})));
25-
Modelica.Electrical.Analog.Ideal.IdealDiode diode
25+
Modelica.Electrical.Analog.Ideal.IdealDiode D
2626
annotation (Placement(transformation(extent={{0,40},{20,60}})));
2727
equation
28-
connect(sineVoltage.p, R1.p)
28+
connect(V.p, R1.p)
2929
annotation (Line(points={{-50,40},{-50,50},{-32,50}}, color={0,0,255}));
30-
connect(R2.n, sineVoltage.n) annotation (Line(points={{30,20},{30,6},{-50,6},{
31-
-50,20}}, color={0,0,255}));
32-
connect(capacitor.n, sineVoltage.n) annotation (Line(points={{52,20},{52,6},{-50,
33-
6},{-50,20}}, color={0,0,255}));
34-
connect(sineVoltage.n, ground.p)
30+
connect(R2.n, V.n) annotation (Line(points={{30,20},{30,6},{-50,6},{-50,20}},
31+
color={0,0,255}));
32+
connect(C.n, V.n) annotation (Line(points={{52,20},{52,6},{-50,6},{-50,20}},
33+
color={0,0,255}));
34+
connect(V.n, ground.p)
3535
annotation (Line(points={{-50,20},{-50,0}}, color={0,0,255}));
36-
connect(R1.n, diode.p)
36+
connect(R1.n, D.p)
3737
annotation (Line(points={{-12,50},{0,50}}, color={0,0,255}));
38-
connect(diode.n, R2.p)
38+
connect(D.n, R2.p)
3939
annotation (Line(points={{20,50},{30,50},{30,40}}, color={0,0,255}));
40-
connect(diode.n, capacitor.p)
40+
connect(D.n, C.p)
4141
annotation (Line(points={{20,50},{52,50},{52,40}}, color={0,0,255}));
4242
annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
43-
coordinateSystem(preserveAspectRatio=false)));
43+
coordinateSystem(preserveAspectRatio=false)),
44+
experiment(StopTime=3, __Dymola_Algorithm="Dassl"));
4445
end Rectifier1;
4546

4647
model Rectifier2
4748
extends Modelica.Icons.Example;
4849

49-
Modelica.Electrical.Analog.Sources.SineVoltage Vsource(V=220, freqHz=50)
50-
annotation (Placement(transformation(
50+
Modelica.Electrical.Analog.Sources.SineVoltage V(V=220, f=50) annotation (
51+
Placement(transformation(
5152
extent={{-10,10},{10,-10}},
5253
rotation=270,
53-
origin={-80,30})));
54+
origin={-60,10})));
5455
Modelica.Electrical.Analog.Basic.Resistor R1(R=20)
55-
annotation (Placement(transformation(extent={{-62,30},{-42,50}})));
56+
annotation (Placement(transformation(extent={{-42,10},{-22,30}})));
5657
Modelica.Electrical.Analog.Basic.Resistor R2(R=500)
5758
annotation (Placement(
5859
transformation(
5960
extent={{-10,-10},{10,10}},
6061
rotation=270,
61-
origin={30,42})));
62+
origin={30,18})));
6263
Modelica.Electrical.Analog.Basic.Capacitor C(C=1e-4) annotation (Placement(
6364
transformation(
6465
extent={{-10,-10},{10,10}},
6566
rotation=-90,
66-
origin={52,40})));
67+
origin={52,18})));
6768
Modelica.Electrical.Analog.Basic.Ground ground
68-
annotation (Placement(transformation(extent={{-90,-8},{-70,12}})));
69+
annotation (Placement(transformation(extent={{-70,-28},{-50,-8}})));
6970
Modelica.Electrical.Analog.Ideal.IdealDiode D1 annotation (Placement(
7071
transformation(
7172
extent={{10,10},{-10,-10}},
7273
rotation=-90,
73-
origin={-10,58})));
74+
origin={-10,38})));
7475
Modelica.Electrical.Analog.Ideal.IdealDiode D3 annotation (Placement(
7576
transformation(
7677
extent={{10,10},{-10,-10}},
7778
rotation=-90,
78-
origin={-10,0})));
79+
origin={-10,-20})));
7980
Modelica.Electrical.Analog.Ideal.IdealDiode D2 annotation (Placement(
8081
transformation(
8182
extent={{10,10},{-10,-10}},
8283
rotation=-90,
83-
origin={10,58})));
84+
origin={10,38})));
8485
Modelica.Electrical.Analog.Ideal.IdealDiode D4 annotation (Placement(
8586
transformation(
8687
extent={{10,10},{-10,-10}},
8788
rotation=-90,
88-
origin={10,0})));
89+
origin={10,-20})));
8990
equation
90-
connect(Vsource.p, R1.p)
91-
annotation (Line(points={{-80,40},{-62,40}}, color={0,0,255}));
92-
connect(Vsource.n, ground.p)
93-
annotation (Line(points={{-80,20},{-80,12}}, color={0,0,255}));
91+
connect(V.p, R1.p)
92+
annotation (Line(points={{-60,20},{-42,20}}, color={0,0,255}));
93+
connect(V.n, ground.p)
94+
annotation (Line(points={{-60,0},{-60,-8}}, color={0,0,255}));
9495
connect(R1.n, D1.p)
95-
annotation (Line(points={{-42,40},{-10,40},{-10,48}}, color={0,0,255}));
96+
annotation (Line(points={{-22,20},{-10,20},{-10,28}}, color={0,0,255}));
9697
connect(D3.n, D1.p)
97-
annotation (Line(points={{-10,10},{-10,48}}, color={0,0,255}));
98+
annotation (Line(points={{-10,-10},{-10,28}},color={0,0,255}));
9899
connect(D1.n, D2.n)
99-
annotation (Line(points={{-10,68},{10,68}}, color={0,0,255}));
100+
annotation (Line(points={{-10,48},{10,48}}, color={0,0,255}));
100101
connect(D4.n, D2.p)
101-
annotation (Line(points={{10,10},{10,48}}, color={0,0,255}));
102-
connect(Vsource.n, D2.p)
103-
annotation (Line(points={{-80,20},{10,20},{10,48}}, color={0,0,255}));
102+
annotation (Line(points={{10,-10},{10,28}},color={0,0,255}));
103+
connect(V.n, D2.p)
104+
annotation (Line(points={{-60,0},{10,0},{10,28}}, color={0,0,255}));
104105
connect(D3.p, D4.p)
105-
annotation (Line(points={{-10,-10},{10,-10}}, color={0,0,255}));
106-
connect(D2.n, R2.p) annotation (Line(points={{10,68},{40,68},{40,56},{30,56},
107-
{30,52}}, color={0,0,255}));
108-
connect(R2.p, C.p) annotation (Line(points={{30,52},{30,56},{52,56},{52,50}},
106+
annotation (Line(points={{-10,-30},{10,-30}}, color={0,0,255}));
107+
connect(D2.n, R2.p) annotation (Line(points={{10,48},{40,48},{40,36},{30,36},
108+
{30,28}}, color={0,0,255}));
109+
connect(R2.p, C.p) annotation (Line(points={{30,28},{30,36},{52,36},{52,28}},
109110
color={0,0,255}));
110-
connect(D4.p, C.n) annotation (Line(points={{10,-10},{40,-10},{40,20},{52,
111-
20},{52,30}}, color={0,0,255}));
112-
connect(R2.n, C.n) annotation (Line(points={{30,32},{30,20},{52,20},{52,30}},
111+
connect(D4.p, C.n) annotation (Line(points={{10,-30},{40,-30},{40,0},{52,0},
112+
{52,8}}, color={0,0,255}));
113+
connect(R2.n, C.n) annotation (Line(points={{30,8},{30,0},{52,0},{52,8}},
113114
color={0,0,255}));
114115
annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
115-
coordinateSystem(preserveAspectRatio=false)));
116+
coordinateSystem(preserveAspectRatio=false)),
117+
experiment(StopTime=0.1, __Dymola_Algorithm="Dassl"));
116118
end Rectifier2;
117-
annotation (uses(Modelica(version="3.2.3")));
119+
annotation (uses(Modelica(version="4.0.0")),
120+
version="1",
121+
conversion(from(version="", script=
122+
"modelica://Rectifier/ConvertFromRectifier_.mos")));
118123
end Rectifier;

0 commit comments

Comments
 (0)