Examples.SignConvention
Examples for the used sign conventions.
Demonstrates force sign conventions using the two-flange Force component. A positive input force of 1 N is applied to a 1 kg mass in three different wiring configurations:
force.flange_bconnected tomass.flange_a: force pushes mass in positive direction. Result: a = 1 m/s², v = 1 m/s after 1 s, s = 0.5 m after 1 s.force.flange_bconnected tomass.flange_b: equivalent to system 1. It doesn't matter which flange of the mass is used, as long asforce.flange_bis the side connected to the mass.force.flange_aconnected tomass.flange_b,force.flange_bconnected to fixed: arrows are opposed. The force now acts in the negative direction. Result: a = -1 m/s².
Usage
TranslationalComponents.Examples.SignConvention()
Behavior
using TranslationalComponents #hide
using ModelingToolkit #hide
@named sys = TranslationalComponents.Examples.SignConvention() #hide
full_equations(sys) #hide<< @example-block not executed in draft mode >>Source
"""
Examples for the used sign conventions.
Demonstrates force sign conventions using the two-flange `Force` component.
A positive input force of 1 N is applied to a 1 kg mass in three different
wiring configurations:
1) `force.flange_b` connected to `mass.flange_a`: force pushes mass in positive
direction. Result: a = 1 m/s², v = 1 m/s after 1 s, s = 0.5 m after 1 s.
2) `force.flange_b` connected to `mass.flange_b`: equivalent to system 1.
It doesn't matter which flange of the mass is used, as long as `force.flange_b`
is the side connected to the mass.
3) `force.flange_a` connected to `mass.flange_b`, `force.flange_b` connected to
fixed: arrows are opposed. The force now acts in the negative direction.
Result: a = -1 m/s².
"""
example component SignConvention
"System 1: force.flange_b pushes at mass.flange_a"
constant1 = BlockComponents.Sources.Constant(k = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": -100, "x2": 300, "y2": 0, "rot": 0}
},
"tags": []
}
}
force1 = TranslationalComponents.Sources.Force() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 450, "y1": 20, "x2": 550, "y2": 120, "rot": 0}
},
"tags": []
}
}
mass1 = TranslationalComponents.Components.Mass(L = 1.0, m = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 630, "y1": 20, "x2": 730, "y2": 120, "rot": 0}
},
"tags": []
}
}
fixed1 = TranslationalComponents.Components.Fixed() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": 150, "x2": 300, "y2": 250, "rot": 0}
},
"tags": []
}
}
"System 2: force.flange_b pushes at mass.flange_b (equivalent to system 1)"
constant2 = BlockComponents.Sources.Constant(k = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 250, "y1": 350, "x2": 350, "y2": 450, "rot": 0}
},
"tags": []
}
}
force2 = TranslationalComponents.Sources.Force() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 400, "y1": 450, "x2": 500, "y2": 550, "rot": 0}
},
"tags": []
}
}
mass2 = TranslationalComponents.Components.Mass(L = 1.0, m = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 640, "y1": 340, "x2": 740, "y2": 440, "rot": 0}
},
"tags": []
}
}
fixed2 = TranslationalComponents.Components.Fixed() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 190, "y1": 570, "x2": 290, "y2": 670, "rot": 0}
},
"tags": []
}
}
"System 3: force.flange_a at mass (arrows opposed, reversed acceleration)"
constant3 = BlockComponents.Sources.Constant(k = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 400, "y1": 710, "x2": 500, "y2": 810, "rot": 0}
},
"tags": []
}
}
force3 = TranslationalComponents.Sources.Force() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 570, "y1": 800, "x2": 670, "y2": 900, "rot": 0}
},
"tags": []
}
}
mass3 = TranslationalComponents.Components.Mass(L = 1.0, m = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 190, "y1": 800, "x2": 290, "y2": 900, "rot": 0}
},
"tags": []
}
}
fixed3 = TranslationalComponents.Components.Fixed() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 810, "y1": 900, "x2": 910, "y2": 1000, "rot": 0}
},
"tags": []
}
}
relations
# System 1
connect(constant1.y, force1.f) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 500, "y": -50}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force1.flange_a, fixed1.flange) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 250, "y": 70}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force1.flange_b, mass1.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
initial mass1.s = 0.0
initial mass1.v = 0.0
# System 2
connect(constant2.y, force2.f) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 450, "y": 400}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force2.flange_a, fixed2.flange) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 240, "y": 500}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force2.flange_b, mass2.flange_b) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 840, "y": 500}, {"x": 840, "y": 390}], "E": 2}],
"renderStyle": "standard"
}
}
initial mass2.s = 0.0
initial mass2.v = 0.0
# System 3
connect(constant3.y, force3.f) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 620, "y": 760}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force3.flange_a, mass3.flange_b) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(force3.flange_b, fixed3.flange) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 860, "y": 850}], "E": 2}],
"renderStyle": "standard"
}
}
initial mass3.s = 0.0
initial mass3.v = 0.0
metadata {
"Dyad": {
"icons": {"default": "dyad://TranslationalComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1,
"atol": {
"mass1.s": 0.001,
"mass1.v": 0.001,
"mass2.s": 0.001,
"mass2.v": 0.001,
"mass3.s": 0.001,
"mass3.v": 0.001
},
"expect": {
"final": {
"mass1.s": 0.5,
"mass1.v": 1,
"mass2.s": 0.5,
"mass2.v": 1,
"mass3.s": -0.5,
"mass3.v": -1
},
"signals": ["mass1.s", "mass1.v", "mass2.s", "mass2.v", "mass3.s", "mass3.v"]
}
}
}
}
}
endFlattened Source
"""
Examples for the used sign conventions.
Demonstrates force sign conventions using the two-flange `Force` component.
A positive input force of 1 N is applied to a 1 kg mass in three different
wiring configurations:
1) `force.flange_b` connected to `mass.flange_a`: force pushes mass in positive
direction. Result: a = 1 m/s², v = 1 m/s after 1 s, s = 0.5 m after 1 s.
2) `force.flange_b` connected to `mass.flange_b`: equivalent to system 1.
It doesn't matter which flange of the mass is used, as long as `force.flange_b`
is the side connected to the mass.
3) `force.flange_a` connected to `mass.flange_b`, `force.flange_b` connected to
fixed: arrows are opposed. The force now acts in the negative direction.
Result: a = -1 m/s².
"""
example component SignConvention
"System 1: force.flange_b pushes at mass.flange_a"
constant1 = BlockComponents.Sources.Constant(k = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": -100, "x2": 300, "y2": 0, "rot": 0}
},
"tags": []
}
}
force1 = TranslationalComponents.Sources.Force() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 450, "y1": 20, "x2": 550, "y2": 120, "rot": 0}
},
"tags": []
}
}
mass1 = TranslationalComponents.Components.Mass(L = 1.0, m = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 630, "y1": 20, "x2": 730, "y2": 120, "rot": 0}
},
"tags": []
}
}
fixed1 = TranslationalComponents.Components.Fixed() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 200, "y1": 150, "x2": 300, "y2": 250, "rot": 0}
},
"tags": []
}
}
"System 2: force.flange_b pushes at mass.flange_b (equivalent to system 1)"
constant2 = BlockComponents.Sources.Constant(k = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 250, "y1": 350, "x2": 350, "y2": 450, "rot": 0}
},
"tags": []
}
}
force2 = TranslationalComponents.Sources.Force() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 400, "y1": 450, "x2": 500, "y2": 550, "rot": 0}
},
"tags": []
}
}
mass2 = TranslationalComponents.Components.Mass(L = 1.0, m = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 640, "y1": 340, "x2": 740, "y2": 440, "rot": 0}
},
"tags": []
}
}
fixed2 = TranslationalComponents.Components.Fixed() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 190, "y1": 570, "x2": 290, "y2": 670, "rot": 0}
},
"tags": []
}
}
"System 3: force.flange_a at mass (arrows opposed, reversed acceleration)"
constant3 = BlockComponents.Sources.Constant(k = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 400, "y1": 710, "x2": 500, "y2": 810, "rot": 0}
},
"tags": []
}
}
force3 = TranslationalComponents.Sources.Force() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 570, "y1": 800, "x2": 670, "y2": 900, "rot": 0}
},
"tags": []
}
}
mass3 = TranslationalComponents.Components.Mass(L = 1.0, m = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 190, "y1": 800, "x2": 290, "y2": 900, "rot": 0}
},
"tags": []
}
}
fixed3 = TranslationalComponents.Components.Fixed() {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 810, "y1": 900, "x2": 910, "y2": 1000, "rot": 0}
},
"tags": []
}
}
relations
# System 1
connect(constant1.y, force1.f) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 500, "y": -50}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force1.flange_a, fixed1.flange) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 250, "y": 70}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force1.flange_b, mass1.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
initial mass1.s = 0.0
initial mass1.v = 0.0
# System 2
connect(constant2.y, force2.f) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 450, "y": 400}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force2.flange_a, fixed2.flange) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 240, "y": 500}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force2.flange_b, mass2.flange_b) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 840, "y": 500}, {"x": 840, "y": 390}], "E": 2}],
"renderStyle": "standard"
}
}
initial mass2.s = 0.0
initial mass2.v = 0.0
# System 3
connect(constant3.y, force3.f) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 620, "y": 760}], "E": 2}],
"renderStyle": "standard"
}
}
connect(force3.flange_a, mass3.flange_b) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(force3.flange_b, fixed3.flange) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 860, "y": 850}], "E": 2}],
"renderStyle": "standard"
}
}
initial mass3.s = 0.0
initial mass3.v = 0.0
metadata {
"Dyad": {
"icons": {"default": "dyad://TranslationalComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1,
"atol": {
"mass1.s": 0.001,
"mass1.v": 0.001,
"mass2.s": 0.001,
"mass2.v": 0.001,
"mass3.s": 0.001,
"mass3.v": 0.001
},
"expect": {
"final": {
"mass1.s": 0.5,
"mass1.v": 1,
"mass2.s": 0.5,
"mass2.v": 1,
"mass3.s": -0.5,
"mass3.v": -1
},
"signals": ["mass1.s", "mass1.v", "mass2.s", "mass2.v", "mass3.s", "mass3.v"]
}
}
}
}
}
endTest Cases
using TranslationalComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @named
using CSV, DataFrames, Plots
snapshotsdir = joinpath(dirname(dirname(pathof(TranslationalComponents))), "test", "snapshots")<< @setup-block not executed in draft mode >>Test Case case1
@named model_case1 = TranslationalComponents.Examples.SignConvention()
model_case1 = toggle_namespacing(model_case1, false)
model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = ODEAlg.Auto(), start = 0e+0, stop = 1e+0, abstol=1e-6, reltol=1e-6)
sol_case1 = rebuild_sol(result_case1)<< @setup-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.mass1.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.SignConvention_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass1.s], width=2, label="Actual value of mass1.s")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass1.s")
end
scatter!(plt, [df_case1.t[end]], [0.5], label="Final Condition for `mass1.s`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.mass1.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.SignConvention_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass1.v], width=2, label="Actual value of mass1.v")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass1.v")
end
scatter!(plt, [df_case1.t[end]], [1], label="Final Condition for `mass1.v`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.mass2.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.SignConvention_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass2.s], width=2, label="Actual value of mass2.s")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass2.s")
end
scatter!(plt, [df_case1.t[end]], [0.5], label="Final Condition for `mass2.s`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.mass2.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.SignConvention_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass2.v], width=2, label="Actual value of mass2.v")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass2.v")
end
scatter!(plt, [df_case1.t[end]], [1], label="Final Condition for `mass2.v`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.mass3.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.SignConvention_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass3.s], width=2, label="Actual value of mass3.s")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass3.s")
end
scatter!(plt, [df_case1.t[end]], [-0.5], label="Final Condition for `mass3.s`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.mass3.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.SignConvention_case1_sig5.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.mass3.v], width=2, label="Actual value of mass3.v")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of mass3.v")
end
scatter!(plt, [df_case1.t[end]], [-1], label="Final Condition for `mass3.v`")<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>Related
Examples
Experiments
Analyses