Compare commits

..

2 Commits

Author SHA1 Message Date
aad519dcb2 new 2d lib and test 2026-03-09 20:46:06 +01:00
1312baa41d pre-big change 2026-03-09 18:27:26 +01:00
12 changed files with 683 additions and 447 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
build/
__pycache__*

View File

@@ -1,234 +0,0 @@
within BondGraph;
package _1D
connector BondPort "Bond graph power port"
Real e "Effort variable";
flow Real f "Flow variable";annotation(
Icon(graphics = {Rectangle(lineColor = {0, 0, 127}, fillColor = {0, 0, 127},fillPattern = FillPattern.Solid, extent = {{-60, 60}, {60, -60}})}));
end BondPort;
model J1 "Bond graph 1-junction (common flow, efforts sum to zero)"
parameter Integer N(min=1) = 2 "# of power ports";
parameter Real s[N] = fill(1.0, N)
"Bond orientation signs used in the effort balance";
BondPort P[N] "Power ports" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}})));
Real f;
equation
// Efforts sum to zero, with signs from bond directions
sum(s[i] * P[i].e for i in 1:N) = 0;
// Flows are all equal
for i in 2:N loop
P[i].f = P[i-1].f;
end for;
f = P[1].f;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "1"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end J1;
model J0 "Bond graph 0-junction (common effort, flows sum to zero)"
parameter Integer N(min=1) = 2 "# of power ports";
parameter Real s[N] = fill(1.0, N)
"Bond orientation signs used in the effort balance";
BondPort P[N] "Power ports" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}})));
Real e;
equation
// Flows sum to zero, with signs from bond directions
sum(s[i] * P[i].f for i in 1:N) = 0;
// Efforts are all equal
for i in 2:N loop
P[i].e = P[1].e;
end for;
e = P[1].e;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "0"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end J0;
partial model OnePortPassive "One-port passive bond graph element"
BondPort p "Generic power port" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}})));
end OnePortPassive;
partial model OnePortEnergetic "One-port storage element"
extends OnePortPassive annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {0, 80}, extent = {{-10, -10}, {10, 10}})));
Real state "Conserved quantity";
end OnePortEnergetic;
model C "Bond graph C element"
extends OnePortEnergetic(state(start=q0, fixed=true));
parameter Real c(min=0) = 1 "Capacitance";
parameter Real q0 = 0 "Initial stored quantity (charge)";
equation
der(state) = p.f;
p.e = state / c;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "C"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end C;
model I "Bond graph I element"
extends OnePortEnergetic(state(start=p0, fixed=true));
parameter Real I(min=0) = 1 "Inertance / inductance / mass";
parameter Real p0 = 0 "Initial stored quantity (momentum / flux)";
equation
der(state) = p.e;
p.f = state / I;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "I"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end I;
model R "Bond graph resistor"
extends OnePortPassive;
parameter Real R(min=0) = 1 "Resistance";
equation
p.e = R * p.f;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "R"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end R;
model Se "Effort source"
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real e0 "Imposed effort";
equation
p.e = e0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "Se"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end Se;
model Sf "Flow source"
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real f0 "Imposed flow";
equation
p.f = f0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "Sf"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end Sf;
model TF "Bond graph transformer"
BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real n = 1 "Transformer ratio";
equation
p1.e = n * p2.e;
p2.f = n * p1.f;
annotation(
Icon(graphics = {Text(extent = {{-80, 100}, {80, -100}}, textString = "TF"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end TF;
model GY "Bond graph gyrator"
BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real r = 1 "Gyrator modulus";
equation
p1.e = r * p2.f;
p2.e = r * p1.f;
annotation(
Icon(graphics = {Text(extent = {{-80, 100}, {80, -100}}, textString = "GY"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end GY;
model mSe "Bond graph modulated effort source"
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput e0 "Imposed effort" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
equation
p.e = e0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "mSe"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end mSe;
model mSf "Bond graph modulated flow source"
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput f0 "Imposed flow" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
equation
p.f = f0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "mSf"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end mSf;
model mTF "Bond graph modulated transformer"
BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput m "Modulation" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
equation
p1.e = m * p2.e;
p2.f = m * p1.f;
annotation(
Diagram(graphics),
Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "mTF"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}));
end mTF;
model mGY "Bond graph modulated gyrator"
BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput m "Modulation" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
equation
p1.e = m * p2.f;
p2.e = m * p1.f;
annotation(
Diagram(graphics),
Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "mGY"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}));
end mGY;
model fsensor
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-52, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealOutput f "Flow output" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {58, 0}, extent = {{-8, -8}, {8, 8}})));
equation
// Ideal flow sensor in bond-graph form: zero effort loading.
p.e = 0;
f = p.f;
annotation(
Icon(graphics = {Text(origin = {-10, 0}, extent = {{-50, 60}, {50, -60}}, textString = "f", textStyle = {TextStyle.Italic}), Ellipse(origin = {-2, 0}, lineThickness = 5, extent = {{-50, 50}, {50, -50}})}));
end fsensor;
model esensor
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-52, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealOutput e "Effort output" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {58, 0}, extent = {{-8, -8}, {8, 8}})));
equation
// Ideal effort sensor in bond-graph form: zero flow loading.
p.f = 0;
e = p.e;
annotation(
Icon(graphics = {Text(origin = {-10, 0}, extent = {{-50, 60}, {50, -60}}, textString = "e", textStyle = {TextStyle.Italic}), Ellipse(origin = {-2, 0}, lineThickness = 5, extent = {{-50, 50}, {50, -50}})}),
Diagram(graphics));
end esensor;
annotation(
Icon(graphics = {Text(origin = {50, 0}, extent = {{-50, 100}, {50, -100}}, textString = "R"), Line(origin = {-45.22, 20.19}, points = {{-58.7774, -20.1934}, {21.2226, -20.1934}, {-38.7774, 19.8066}}, thickness = 5), Line(origin = {-9.81, -8.19}, points = {{-10.1934, 48.1934}, {-10.1934, -31.8066}}, thickness = 5)}),
uses(Modelica(version = "4.1.0")));
end _1D;

View File

@@ -1,18 +0,0 @@
BondPort
J1
J0
OnePortPassive
OnePortEnergetic
C
I
R
Se
Sf
TF
GY
mSe
mSf
mTF
mGY
fsensor
esensor

View File

@@ -7,13 +7,16 @@ package _2D
Icon(graphics = {Rectangle(lineColor = {0, 85, 0}, fillColor = {0, 85, 0},fillPattern = FillPattern.Solid, extent = {{-60, 60}, {60, -60}})}));
end BondPort;
model J1 "Bond graph 2D 1-junction (common flow, efforts sum to zero)"
model J1 "Bond graph 1-junction (common flow, efforts sum to zero)"
parameter Integer N(min=1) = 2 "# of power ports";
parameter Real s[N] = fill(1.0, N)
"Bond orientation signs used in the effort balance";
BondPort P[N] "Power ports" annotation(
parameter Real s[N] = fill(1.0, N) "Bond orientation signs used in the effort balance";
BondPort P[N] "Power ports"
annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}})));
Real f[2];
Modelica.Blocks.Interfaces.RealVectorOutput f[2] "Flow in junction"
annotation(
Placement(transformation(origin = {-100, -6}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {60, -60}, extent = {{-10, -10}, {10, 10}})));
equation
// Efforts sum to zero, with signs from bond directions
for j in 1:2 loop
@@ -24,18 +27,24 @@ package _2D
for i in 2:N loop
P[i].f = P[i-1].f;
end for;
f = P[1].f;
f = -1 * P[1].f * s[1];
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "1", textStyle = {TextStyle.Bold, TextStyle.UnderLine}), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}));
end J1;
model J0 "Bond graph 2D 0-junction (common effort, flows sum to zero)"
model J0 "Bond graph 1-junction (common effort, flows sum to zero)"
parameter Integer N(min=1) = 2 "# of power ports";
parameter Real s[N] = fill(1.0, N)
"Bond orientation signs used in the effort balance";
BondPort P[N] "Power ports" annotation(
parameter Real s[N] = fill(1.0, N) "Bond orientation signs used in the effort balance";
BondPort P[N] "Power ports"
annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}})));
Real e[2];
Modelica.Blocks.Interfaces.RealVectorOutput e[2] "Flow in junction"
annotation(
Placement(transformation(origin = {-100, -6}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {60, -60}, extent = {{-10, -10}, {10, 10}})));
equation
// Flows sum to zero, with signs from bond directions
for j in 1:2 loop
@@ -44,9 +53,11 @@ package _2D
// Efforts are all equal
for i in 2:N loop
P[i].e = P[1].e;
P[i].e = P[i-1].e;
end for;
e = P[1].e;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "0", textStyle = {TextStyle.Bold, TextStyle.UnderLine}), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
@@ -206,62 +217,6 @@ package _2D
Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "mGY", textStyle = {TextStyle.Bold, TextStyle.UnderLine}), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}));
end mGY;
package TransRotUtils
model mTFrot2lin
_1D.BondPort pR annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort pT annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real r_body[2] = {1,0};
protected
Real B[2];
equation
B = {-r_body[2], r_body[1]};
pT.f = B * pR.f;
pR.e = B[1]*pT.e[1] + B[2]*pT.e[2];
annotation(
Icon(graphics = {Text(extent = {{-70, 100}, {70, -100}}, textString = "rlTF", textStyle = {TextStyle.Bold, TextStyle.UnderLine})}));
end mTFrot2lin;
model rTF
BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput phi "angle" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
protected
Real R[2,2];
equation
R = [cos(phi), -sin(phi);
sin(phi), cos(phi)];
p1.e = R * p2.e;
transpose(R) * p1.f = p2.f;
annotation(
Diagram(graphics),
Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "rTF", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
end rTF;
end TransRotUtils;
model fsensor2d
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-52, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealOutput f0 "Flow output" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {54, 20}, extent = {{-8, -8}, {8, 8}})));
Modelica.Blocks.Interfaces.RealOutput f1 "Flow output" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {54, -20}, extent = {{-8, -8}, {8, 8}})));
equation
// Ideal flow sensor in bond-graph form: zero effort loading.
p.e = {0,0};
f0 = p.f[1];
f1 = p.f[2];
annotation(
Icon(graphics = {Text(origin = {-10, 0}, extent = {{-50, 60}, {50, -60}}, textString = "f", textStyle = {TextStyle.Italic}), Ellipse(origin = {-2, 0}, lineThickness = 5, extent = {{-50, 50}, {50, -50}})}));
end fsensor2d;
annotation(
Icon(graphics = {Text(origin = {50, 0}, extent = {{-50, 100}, {50, -100}}, textString = "R", textStyle = {TextStyle.Bold}), Line(origin = {-45.22, 20.19}, points = {{-58.7774, -20.1934}, {21.2226, -20.1934}, {-38.7774, 19.8066}}, thickness = 5), Line(origin = {-9.81, -8.19}, points = {{-10.1934, 48.1934}, {-10.1934, -31.8066}}, thickness = 5), Line(origin = {-78, 16}, points = {{-26, 0}, {30, 0}}, thickness = 5)}),
uses(Modelica(version = "4.1.0")),

View File

@@ -14,5 +14,3 @@ mSe
mSf
mTF
mGY
TransRotUtils
fsensor2d

View File

@@ -2,9 +2,222 @@ within ;
package BondGraph
extends Modelica.Icons.Package;
connector BondPort "Bond graph power port"
Real e "Effort variable";
flow Real f "Flow variable";
annotation(
Icon(graphics = {Rectangle(lineColor = {0, 0, 127}, fillColor = {0, 0, 127},fillPattern = FillPattern.Solid, extent = {{-60, 60}, {60, -60}})}));
end BondPort;
model J1 "Bond graph 1-junction (common flow, efforts sum to zero)"
parameter Integer N(min=1) = 2 "# of power ports";
parameter Real s[N] = fill(1.0, N) "Bond orientation signs used in the effort balance";
BondPort P[N] "Power ports"
annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealOutput f "Flow in junction"
annotation(
Placement(transformation(origin = {-100, -6}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {60, -60}, extent = {{-10, -10}, {10, 10}})));
equation
// Efforts sum to zero, with signs from bond directions
sum(s[i] * P[i].e for i in 1:N) = 0;
// Flows are all equal
for i in 2:N loop
P[i].f = P[i-1].f;
end for;
f = -1 * P[1].f * s[1];
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "1"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end J1;
model J0 "Bond graph 1-junction (common effort, flows sum to zero)"
parameter Integer N(min=1) = 2 "# of power ports";
parameter Real s[N] = fill(1.0, N) "Bond orientation signs used in the effort balance";
BondPort P[N] "Power ports"
annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealOutput e "Flow in junction"
annotation(
Placement(transformation(origin = {-100, -6}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {60, -60}, extent = {{-10, -10}, {10, 10}})));
equation
// Flows sum to zero, with signs from bond directions
sum(s[i] * P[i].f for i in 1:N) = 0;
// Efforts are all equal
for i in 2:N loop
P[i].e = P[i-1].e;
end for;
e = P[1].e * s[1];
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "0"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end J0;
partial model OnePortPassive "One-port passive bond graph element"
BondPort p "Generic power port" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}})));
end OnePortPassive;
partial model OnePortEnergetic "One-port storage element"
extends OnePortPassive annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {0, 80}, extent = {{-10, -10}, {10, 10}})));
Real state "Conserved quantity";
end OnePortEnergetic;
model C "Bond graph C element"
extends OnePortEnergetic(state(start=q0, fixed=true));
parameter Real c(min=0) = 1 "Capacitance";
parameter Real q0 = 0 "Initial stored quantity (charge)";
equation
der(state) = p.f;
p.e = state / c;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "C"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end C;
model I "Bond graph I element"
extends OnePortEnergetic(state(start=p0, fixed=true));
parameter Real I(min=0) = 1 "Inertance / inductance / mass";
parameter Real p0 = 0 "Initial stored quantity (momentum / flux)";
equation
der(state) = p.e;
p.f = state / I;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "I"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end I;
model R "Bond graph resistor"
extends OnePortPassive;
parameter Real R(min=0) = 1 "Resistance";
equation
p.e = R * p.f;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "R"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end R;
model Se "Effort source"
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real e0 "Imposed effort";
equation
p.e = e0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "Se"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end Se;
model Sf "Flow source"
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real f0 "Imposed flow";
equation
p.f = f0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "Sf"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end Sf;
model TF "Bond graph transformer"
BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real n = 1 "Transformer ratio";
equation
p1.e = n * p2.e;
p2.f = n * p1.f;
annotation(
Icon(graphics = {Text(extent = {{-80, 100}, {80, -100}}, textString = "TF"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end TF;
model GY "Bond graph gyrator"
BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real r = 1 "Gyrator modulus";
equation
p1.e = r * p2.f;
p2.e = r * p1.f;
annotation(
Icon(graphics = {Text(extent = {{-80, 100}, {80, -100}}, textString = "GY"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end GY;
model mSe "Bond graph modulated effort source"
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput e0 "Imposed effort" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
equation
p.e = e0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "mSe"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end mSe;
model mSf "Bond graph modulated flow source"
BondPort p annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput f0 "Imposed flow" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
equation
p.f = f0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "mSf"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}),
Diagram(graphics));
end mSf;
model mTF "Bond graph modulated transformer"
BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput m "Modulation" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
equation
p1.e = m * p2.e;
p2.f = m * p1.f;
annotation(
Diagram(graphics),
Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "mTF"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}));
end mTF;
model mGY "Bond graph modulated gyrator"
BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput m "Modulation" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
equation
p1.e = m * p2.f;
p2.e = m * p1.f;
annotation(
Diagram(graphics),
Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "mGY"), Text(origin = {50, 80}, textColor = {0, 0, 255}, extent = {{-50, 20}, {50, -20}}, textString = "%name")}));
end mGY;
annotation(
Diagram(graphics),
Icon(graphics = {Line(origin = {-45.22, 20.19}, points = {{-50.7774, -20.1934}, {21.2226, -20.1934}, {-38.7774, 19.8066}}, thickness = 5), Line(origin = {-9.81, -8.19}, points = {{-10.1934, 48.1934}, {-10.1934, -31.8066}}, thickness = 5), Line(origin = {156.78, 20.19}, points = {{-60.7774, -20.1934}, {-134.777, -20.1934}, {-76.7774, 19.8066}}, thickness = 5), Line(origin = {28.19, -8.19}, points = {{-10.1934, 48.1934}, {-10.1934, -31.8066}}, thickness = 5), Line(origin = {66, 12}, points = {{-26, 0}, {30, 0}}, thickness = 5)}));
Icon(graphics = {Line(origin = {-45.22, 20.19}, points = {{-50.7774, -20.1934}, {21.2226, -20.1934}, {-38.7774, 19.8066}}, thickness = 5), Line(origin = {-9.81, -8.19}, points = {{-10.1934, 48.1934}, {-10.1934, -31.8066}}, thickness = 5), Line(origin = {156.78, 20.19}, points = {{-60.7774, -20.1934}, {-134.777, -20.1934}, {-76.7774, 19.8066}}, thickness = 5), Line(origin = {28.19, -8.19}, points = {{-10.1934, 48.1934}, {-10.1934, -31.8066}}, thickness = 5), Line(origin = {66, 12}, points = {{-26, 0}, {30, 0}}, thickness = 5)}),
uses(Modelica(version = "4.1.0")));
end BondGraph;

View File

@@ -1,2 +1,17 @@
_1D
BondPort
J1
J0
OnePortPassive
OnePortEnergetic
C
I
R
Se
Sf
TF
GY
mSe
mSf
mTF
mGY
_2D

View File

@@ -24,7 +24,9 @@ END_RE = re.compile(r"^\s*end\b")
def format_modelica(text: str) -> str:
lines = text.replace("\t", " ").splitlines()
normalized = text.replace("\r\n", "\n").replace("\r", "\n").replace("\t", " ")
had_trailing_newline = normalized.endswith("\n")
lines = normalized.split("\n")
out = []
indent = 0
@@ -63,7 +65,10 @@ def format_modelica(text: str) -> str:
elif SECTION_RE.match(stripped):
indent += 1
return "\n".join(out) + "\n"
formatted = "\n".join(out)
if had_trailing_newline:
return formatted
return formatted + "\n"
def main():

185
suggest_junction_signs.py Normal file
View File

@@ -0,0 +1,185 @@
#!/usr/bin/env python3
"""
Suggest BondGraph junction s-arrays from connect argument order.
Rule used:
- connect(a, b) means a is source and b is sink.
- Junction port in first argument => +1 vote
- Junction port in second argument => -1 vote
This is a heuristic helper. It does not modify files.
"""
from __future__ import annotations
import argparse
import re
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, List, Optional, Tuple
CONNECT_RE = re.compile(r"connect\s*\(\s*([^,]+?)\s*,\s*([^)]+?)\s*\)\s*(?:annotation\s*\(|;)", re.S)
JUNCTION_DECL_RE = re.compile(
r"\b(BondGraph(?:\._2D)?\.J[01])\s+([A-Za-z_]\w*)\s*(\((.*?)\))?\s*annotation\s*\(",
re.S,
)
PORT_RE = re.compile(r"^([A-Za-z_]\w*)\.P\[(\d+)\]$")
N_RE = re.compile(r"\bN\s*=\s*(\d+)")
S_RE = re.compile(r"\bs\s*=\s*\{([^}]*)\}")
@dataclass
class Junction:
cls: str
name: str
n: Optional[int]
current_s: Optional[List[str]]
votes: Dict[int, List[int]] = field(default_factory=dict)
def add_vote(self, port_idx: int, vote: int) -> None:
self.votes.setdefault(port_idx, []).append(vote)
def parse_current_s(params: str) -> Optional[List[str]]:
m = S_RE.search(params)
if not m:
return None
return [x.strip() for x in m.group(1).split(",") if x.strip()]
def parse_junctions(text: str) -> Dict[str, Junction]:
result: Dict[str, Junction] = {}
for m in JUNCTION_DECL_RE.finditer(text):
cls, name, _, params = m.groups()
params = params or ""
n_m = N_RE.search(params)
n = int(n_m.group(1)) if n_m else None
result[name] = Junction(cls=cls, name=name, n=n, current_s=parse_current_s(params))
return result
def normalize_endpoint(ep: str) -> str:
return re.sub(r"\s+", "", ep)
def extract_junction_port(endpoint: str) -> Optional[Tuple[str, int]]:
m = PORT_RE.match(endpoint)
if not m:
return None
return m.group(1), int(m.group(2))
def suggest_sign(votes: List[int]) -> str:
pos = sum(1 for v in votes if v < 0)
neg = sum(1 for v in votes if v > 0)
if pos and neg:
return "?"
if pos:
return "-1"
if neg:
return "+1"
return "0"
def fmt_current(j: Junction, idx: int) -> str:
if not j.current_s:
return "n/a"
if idx - 1 < len(j.current_s):
return j.current_s[idx - 1]
return "n/a"
def as_int_sign(token: str) -> Optional[int]:
t = token.strip()
if t in {"+1", "1", "1.0"}:
return 1
if t in {"-1", "-1.0"}:
return -1
return None
def main() -> int:
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("modelica_file", type=Path)
ap.add_argument(
"--emit-patch",
action="store_true",
help="Emit patch-style s-array replacements per junction.",
)
args = ap.parse_args()
text = args.modelica_file.read_text(encoding="utf-8")
junctions = parse_junctions(text)
for m in CONNECT_RE.finditer(text):
left, right = normalize_endpoint(m.group(1)), normalize_endpoint(m.group(2))
l = extract_junction_port(left)
if l and l[0] in junctions:
junctions[l[0]].add_vote(l[1], +1)
r = extract_junction_port(right)
if r and r[0] in junctions:
junctions[r[0]].add_vote(r[1], -1)
print(f"File: {args.modelica_file}")
print("Rule: connect(a,b) => a:-1, b:+1")
print("")
patch_lines: List[str] = []
for name in sorted(junctions):
j = junctions[name]
max_idx = j.n or (max(j.votes) if j.votes else 0)
suggested = []
conflicts = []
for i in range(1, max_idx + 1):
votes = j.votes.get(i, [])
s = suggest_sign(votes)
suggested.append(s)
if s == "?":
conflicts.append(i)
current_s = "{" + ", ".join(j.current_s) + "}" if j.current_s else "n/a"
sug_s = "{" + ", ".join(suggested) + "}" if suggested else "{}"
print(f"{j.name} ({j.cls}, N={j.n if j.n is not None else 'n/a'})")
print(f" current s: {current_s}")
print(f" suggested s: {sug_s}")
for i in range(1, max_idx + 1):
votes = j.votes.get(i, [])
vote_str = ",".join("+1" if v > 0 else "-1" for v in votes) if votes else "-"
print(f" P[{i}]: current={fmt_current(j, i)} votes={vote_str} -> {suggested[i-1]}")
if conflicts:
conflict_ports = ", ".join(f"P[{i}]" for i in conflicts)
print(f" conflicts: {conflict_ports} (used as both source and sink)")
print("")
if args.emit_patch and max_idx > 0:
suggested_int = []
current_int = []
ok = True
for i in range(1, max_idx + 1):
s = suggested[i - 1]
if s == "?":
ok = False
break
suggested_int.append("1" if s == "+1" else "-1")
current_int.append(fmt_current(j, i))
if ok:
old_txt = "{" + ", ".join(current_int) + "}"
new_txt = "{" + ", ".join(suggested_int) + "}"
patch_lines.append(f"- {j.name}.s = {old_txt}")
patch_lines.append(f"+ {j.name}.s = {new_txt}")
if args.emit_patch:
print("Suggested Patch")
for line in patch_lines:
print(line)
return 0
if __name__ == "__main__":
raise SystemExit(main())

24
test1d.mo Normal file
View File

@@ -0,0 +1,24 @@
model test1d
BondGraph.J1 j1(N = 3, s = {-1, -1, 1}) annotation(
Placement(transformation(extent = {{-10, -10}, {10, 10}})));
BondGraph.R r1(R = 1) annotation(
Placement(transformation(origin = {0, 42}, extent = {{-10, -10}, {10, 10}})));
BondGraph.mSe mSe annotation(
Placement(transformation(origin = {-40, 0}, extent = {{-10, -10}, {10, 10}})));
BondGraph.C c(c = 0.1) annotation(
Placement(transformation(origin = {0, -40}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Sources.Step step(startTime = 0.5) annotation(
Placement(transformation(origin = {-70, 0}, extent = {{-10, -10}, {10, 10}})));
equation
connect(j1.P[1], r1.p) annotation(
Line(points = {{0, 0}, {0, 42}}, color = {0, 0, 127}));
connect(mSe.p, j1.P[3]) annotation(
Line(points = {{-32, 0}, {0, 0}}, color = {0, 0, 127}));
connect(j1.P[2], c.p) annotation(
Line(points = {{0, 0}, {0, -40}}, color = {0, 0, 127}));
connect(step.y, mSe.e0) annotation(
Line(points = {{-58, 0}, {-40, 0}, {-40, -8}}, color = {0, 0, 127}));
annotation(
uses(Modelica(version = "4.1.0")));
end test1d;

197
test2d.mo
View File

@@ -1,94 +1,127 @@
model test2d
import _1D = BondGraph._1D;
import _2D = BondGraph._2D;
_1D.J1 w_com(N = 4, s = {1, -1, -1, -1}) annotation(
Placement(transformation(origin = {0, 120}, extent = {{-10, -10}, {10, 10}})));
_2D.TransRotUtils.mTFrot2lin mTFrot2lin(r_body = {0.5, 0}) annotation(
Placement(transformation(origin = {0, 80}, extent = {{-10, -10}, {10, 10}}, rotation = -90)));
_2D.J0 j0(N = 3, s = {1, 1, -1}) annotation(
package test2d
model test2d
BondGraph.J1 w_com(N = 3, s = {1, 1, 1}) annotation(
Placement(transformation(origin = {0, 100}, extent = {{-10, -10}, {10, 10}})));
BondGraph.I moi(I = 0.01) annotation(
Placement(transformation(origin = {40, 100}, extent = {{-10, -10}, {10, 10}})));
mTFrot2lin mTFrot2lin1(r_body = {0.5, 0}) annotation(
Placement(transformation(origin = {0, 70}, extent = {{-10, -10}, {10, 10}}, rotation = -90)));
BondGraph._2D.J0 j0(N = 3, s = {-1, -1, 1}) annotation(
Placement(transformation(origin = {0, 40}, extent = {{-10, -10}, {10, 10}})));
_2D.J1 v_p_bff(N = 2, s = {1, 1}) annotation(
BondGraph._2D.J1 v_com_i(N = 3, s = {-1, 1, -1}) annotation(
Placement(transformation(origin = {40, -20}, extent = {{-10, -10}, {10, 10}})));
BondGraph._2D.J1 v_P_bff(N = 2, s = {-1, -1}) annotation(
Placement(transformation(origin = {-40, 40}, extent = {{-10, -10}, {10, 10}})));
_2D.J1 v_com_bff(N = 2, s = {-1, -1}) annotation(
BondGraph._2D.J1 v_com_bff(N = 2, s = {1, 1}) annotation(
Placement(transformation(origin = {40, 40}, extent = {{-10, -10}, {10, 10}})));
_2D.TransRotUtils.rTF rTF annotation(
Placement(transformation(origin = {40, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 90)));
_1D.fsensor wsensor annotation(
Placement(transformation(origin = {50, 90}, extent = {{-10, -10}, {10, 10}})));
BondGraph._2D.Sf hinge(f0 = {0, 0}) annotation(
Placement(transformation(origin = {-78, 40}, extent = {{-10, -10}, {10, 10}})));
rTF rTF1 annotation(
Placement(transformation(origin = {40, 10}, extent = {{-10, -10}, {10, 10}}, rotation = 90)));
Modelica.Blocks.Continuous.Integrator w2phi(k = 1) annotation(
Placement(transformation(origin = {90, 90}, extent = {{-10, -10}, {10, 10}})));
_2D.J1 v_com_in(N = 4, s = {1, -1, -1, 1}) annotation(
Placement(transformation(origin = {40, -40}, extent = {{-10, -10}, {10, 10}})));
_2D.I mass(I = [1, 0; 0, 1]) annotation(
Placement(transformation(origin = {80, -40}, extent = {{-10, -10}, {10, 10}})));
BondGraph._2D.Sf ground(f0 = {0, 0}) annotation(
Placement(transformation(origin = {-80, 40}, extent = {{-10, -10}, {10, 10}})));
BondGraph._1D.I m_inertial(I = 0.001) annotation(
Placement(transformation(origin = {40, 120}, extent = {{-10, -10}, {10, 10}})));
BondGraph._1D.Sf ground2(f0 = 0) annotation(
Placement(transformation(origin = {-80, 120}, extent = {{-10, -10}, {10, 10}})));
BondGraph._1D.J0 j1(N = 3, s = {1, -1, -1}) annotation(
Placement(transformation(origin = {-40, 120}, extent = {{-10, -10}, {10, 10}})));
BondGraph._1D.J1 w_hinge(N = 2, s = {1, -11}) annotation(
Placement(transformation(origin = {-40, 90}, extent = {{-10, -10}, {10, 10}})));
BondGraph._1D.R hinge_r(R = 0.01) annotation(
Placement(transformation(origin = {-80, 90}, extent = {{-10, -10}, {10, 10}})));
BondGraph._2D.Se g(e0 = {0, 9.81}) annotation(
Placement(transformation(origin = {2, -40}, extent = {{-10, -10}, {10, 10}})));
BondGraph._2D.fsensor2d v_inertial annotation(
Placement(transformation(origin = {40, -60}, extent = {{-10, -10}, {10, 10}}, rotation = -90)));
Modelica.Blocks.Continuous.Integrator vx_to_x(k = -1, y_start = -0.5) annotation(
Placement(transformation(origin = {70, -70}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Continuous.Integrator vy_to_y annotation(
Placement(transformation(origin = {70, -100}, extent = {{-10, -10}, {10, 10}})));
vis2d link;
equation
connect(w_com.P[2], mTFrot2lin.pR) annotation(
Line(points = {{0, 120}, {0, 88}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(mTFrot2lin.pT, j0.P[1]) annotation(
Line(points = {{0, 72}, {0, 40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(v_com_bff.P[1], j0.P[2]) annotation(
Placement(transformation(origin = {30, 70}, extent = {{-10, -10}, {10, 10}})));
BondGraph._2D.I m(I = [1, 0; 0, 1]) annotation(
Placement(transformation(origin = {80, -20}, extent = {{-10, -10}, {10, 10}})));
BondGraph._2D.Se g(e0 = {0, -9.81}) annotation(
Placement(transformation(origin = {0, -20}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Continuous.Integrator vx2x(y_start = -0.5) annotation(
Placement(transformation(origin = {70, -50}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Continuous.Integrator vy2y(y_start = 0) annotation(
Placement(transformation(origin = {70, -80}, extent = {{-10, -10}, {10, 10}})));
BondGraph.J0 j1(N = 3, s = {-1, -1, 1}) annotation(
Placement(transformation(origin = {-40, 100}, extent = {{-10, -10}, {10, 10}})));
BondGraph.Sf hinge2(f0 = 0) annotation(
Placement(transformation(origin = {-78, 100}, extent = {{-10, -10}, {10, 10}})));
BondGraph.J1 w_hinge(N = 2, s = {-1, 1}) annotation(
Placement(transformation(origin = {-40, 70}, extent = {{-10, -10}, {10, 10}})));
BondGraph.R r(R = 0.2) annotation(
Placement(transformation(origin = {-70, 70}, extent = {{-10, -10}, {10, 10}})));
vis2d vis2d1 annotation(
Placement(transformation(origin = {118, -64}, extent = {{-10, -10}, {10, 10}})));
equation
connect(w_com.f, w2phi.u) annotation(
Line(points = {{6, 94}, {18, 70}}, color = {0, 0, 127}));
connect(w2phi.y, rTF1.phi) annotation(
Line(points = {{41, 70}, {100, 70}, {100, 10}, {48, 10}}, color = {0, 0, 127}));
connect(w_com.P[1], moi.p) annotation(
Line(points = {{0, 100}, {40, 100}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(w_com.P[2], mTFrot2lin1.pR) annotation(
Line(points = {{0, 100}, {0, 78}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(v_com_bff.P[1], j0.P[1]) annotation(
Line(points = {{40, 40}, {0, 40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(j0.P[3], v_p_bff.P[1]) annotation(
Line(origin = {-1, 0}, points = {{0, 40}, {-40, 40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(wsensor.f, w2phi.u) annotation(
Line(points = {{58, 90}, {78, 90}}, color = {0, 0, 127}));
connect(w2phi.y, rTF.phi) annotation(
Line(points = {{102, 90}, {110, 90}, {110, 0}, {48, 0}}, color = {0, 0, 127}, arrow = {Arrow.None, Arrow.Filled}));
connect(v_com_in.P[2], mass.p) annotation(
Line(points = {{40, -40}, {80, -40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(w_com.P[3], wsensor.p) annotation(
Line(points = {{0, 120}, {44, 90}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(w_com.P[1], m_inertial.p) annotation(
Line(points = {{0, 120}, {40, 120}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(ground2.p, j1.P[1]) annotation(
Line(points = {{-72, 120}, {-40, 120}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(j1.P[2], w_com.P[4]) annotation(
Line(points = {{-40, 120}, {0, 120}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.Half, Arrow.None}, arrowSize = 6));
connect(ground.p, v_p_bff.P[2]) annotation(
Line(points = {{-72, 40}, {-40, 40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(mTFrot2lin1.pT, j0.P[2]) annotation(
Line(points = {{0, 62}, {0, 40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(j0.P[3], v_P_bff.P[1]) annotation(
Line(points = {{0, 40}, {-40, 40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(hinge.p, v_P_bff.P[2]) annotation(
Line(points = {{-70, 40}, {-40, 40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(v_com_bff.P[2], rTF1.p2) annotation(
Line(points = {{40, 40}, {40, 18}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(rTF1.p1, v_com_i.P[1]) annotation(
Line(points = {{40, 2}, {40, -20}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(g.p, v_com_i.P[3]) annotation(
Line(points = {{8, -20}, {40, -20}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(v_com_i.P[2], m.p) annotation(
Line(points = {{40, -20}, {80, -20}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(v_com_i.f[1], vx2x.u) annotation(
Line(points = {{46, -26}, {50, -50}, {58, -50}}, color = {0, 0, 127}));
connect(v_com_i.f[2], vy2y.u) annotation(
Line(points = {{46, -26}, {50, -80}, {58, -80}}, color = {0, 0, 127}));
connect(hinge2.p, j1.P[1]) annotation(
Line(points = {{-70, 100}, {-40, 100}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(j1.P[3], w_hinge.P[1]) annotation(
Line(points = {{-40, 120}, {-40, 90}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(w_hinge.P[2], hinge_r.p) annotation(
Line(points = {{-40, 90}, {-80, 90}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(g.p, v_com_in.P[3]) annotation(
Line(points = {{10, -40}, {40, -40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(v_com_bff.P[2], rTF.p2) annotation(
Line(points = {{40, 40}, {40, 8}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(rTF.p1, v_com_in.P[1]) annotation(
Line(points = {{40, -8}, {40, -40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(v_com_in.P[4], v_inertial.p) annotation(
Line(points = {{40, -40}, {40, -54}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(v_inertial.f0, vx_to_x.u) annotation(
Line(points = {{42, -66}, {42, -70}, {58, -70}}, color = {0, 0, 127}));
connect(v_inertial.f1, vy_to_y.u) annotation(
Line(points = {{38, -66}, {38, -100}, {58, -100}}, color = {0, 0, 127}));
link.r = {vx_to_x.y, vy_to_y.y, 0};
link.phi = w2phi.y;
Line(points = {{-40, 100}, {-40, 70}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(w_hinge.P[2], r.p) annotation(
Line(points = {{-40, 70}, {-70, 70}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
connect(vy2y.y, vis2d1.y) annotation(
Line(points = {{82, -80}, {94, -80}, {94, -70}, {108, -70}}, color = {0, 0, 127}));
connect(vx2x.y, vis2d1.x) annotation(
Line(points = {{82, -50}, {94, -50}, {94, -64}, {108, -64}}, color = {0, 0, 127}));
connect(w2phi.y, vis2d1.phi) annotation(
Line(points = {{42, 70}, {100, 70}, {100, -58}, {108, -58}}, color = {0, 0, 127}));
connect(w_com.P[3], j1.P[2]) annotation(
Line(points = {{0, 100}, {-40, 100}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6));
annotation(
uses(Modelica(version = "4.1.0")),
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-06, Interval = 0.02),
Diagram(coordinateSystem(extent = {{-100, 140}, {120, -120}})),
Diagram(coordinateSystem(extent = {{-100, 120}, {140, -100}})),
Icon(coordinateSystem(extent = {{-200, -200}, {200, 200}})),
version = "");
end test2d;
model mTFrot2lin
BondGraph.BondPort pR annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondGraph._2D.BondPort pT annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
parameter Real r_body[2] = {1, 0};
protected
Real B[2];
equation
B = {-r_body[2], r_body[1]};
pT.f = B*pR.f;
pR.e = B[1]*pT.e[1] + B[2]*pT.e[2];
annotation(
Icon(graphics = {Text(extent = {{-70, 100}, {70, -100}}, textString = "rlTF", textStyle = {TextStyle.Bold, TextStyle.UnderLine})}));
end mTFrot2lin;
model rTF
BondGraph._2D.BondPort p1 "Port 1" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {-80, 0}, extent = {{-10, -10}, {10, 10}})));
BondGraph._2D.BondPort p2 "Port 2" annotation(
Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {80, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput phi "angle" annotation(
Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {0, -78}, extent = {{-8, -8}, {8, 8}}, rotation = 90)));
protected
Real R[2, 2];
equation
R = [cos(phi), -sin(phi); sin(phi), cos(phi)];
p1.e = R*p2.e;
transpose(R)*p1.f = p2.f;
annotation(
Diagram(graphics),
Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "rTF", textStyle = {TextStyle.Bold, TextStyle.UnderLine})}));
end rTF;
annotation(
uses(Modelica(version = "4.1.0")));
end test2d;

View File

@@ -5,9 +5,21 @@ model vis2d
parameter Real L = 1;
parameter Real w = 0.02;
parameter Real orbRadius = 0.04;
parameter Real axisLength = 0.25;
parameter Real axisWidth = 0.01;
parameter Real planeSize = 2.0;
parameter Real planeThickness = 0.002;
parameter Real planeOffset = -0.001;
input Real r[3];
input Real phi;
Modelica.Blocks.Interfaces.RealInput phi annotation(
Placement(transformation(origin = {-98, -42}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {-94, 60}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput y annotation(
Placement(transformation(origin = {-98, 0}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {-94, -60}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.RealInput x annotation(
Placement(transformation(origin = {-98, 40}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {-94, 0}, extent = {{-10, -10}, {10, 10}})));
protected
Real Rz[3,3];
Real r[3];
Visualizers.Advanced.Shape link(
shapeType = "cylinder",
@@ -18,11 +30,7 @@ model vis2d
lengthDirection = {1, 0, 0},
widthDirection = {0, 1, 0},
r = r,
R = Frames.from_T(
[cos(phi), -sin(phi), 0;
sin(phi), cos(phi), 0;
0, 0, 1],
zeros(3)));
R = Frames.from_T(Rz, zeros(3)));
Visualizers.Advanced.Shape orb(
shapeType = "sphere",
@@ -33,9 +41,60 @@ model vis2d
lengthDirection = {1, 0, 0},
widthDirection = {0, 1, 0},
r = r,
R = Frames.from_T(
[cos(phi), -sin(phi), 0;
sin(phi), cos(phi), 0;
0, 0, 1],
zeros(3)));
R = Frames.from_T(Rz, zeros(3)));
Visualizers.Advanced.Shape xAxis(
shapeType = "cylinder",
length = axisLength,
width = axisWidth,
height = axisWidth,
color = {255, 0, 0},
specularCoefficient = 0.1,
r_shape = {axisLength / 2, 0, 0},
lengthDirection = {1, 0, 0},
widthDirection = {0, 1, 0},
r = zeros(3),
R = Frames.from_T([1, 0, 0;
0, 1, 0;
0, 0, 1], zeros(3)));
Visualizers.Advanced.Shape yAxis(
shapeType = "cylinder",
length = axisLength,
width = axisWidth,
height = axisWidth,
color = {0, 200, 0},
specularCoefficient = 0.1,
r_shape = {0, axisLength / 2, 0},
lengthDirection = {0, 1, 0},
widthDirection = {0, 0, 1},
r = zeros(3),
R = Frames.from_T([1, 0, 0;
0, 1, 0;
0, 0, 1], zeros(3)));
Visualizers.Advanced.Shape zAxis(
shapeType = "cylinder",
length = axisLength,
width = axisWidth,
height = axisWidth,
color = {0, 90, 255},
specularCoefficient = 0.1,
r_shape = {0, 0, axisLength / 2},
lengthDirection = {0, 0, 1},
widthDirection = {1, 0, 0},
r = zeros(3),
R = Frames.from_T([1, 0, 0;
0, 1, 0;
0, 0, 1], zeros(3)));
equation
r = {x, y, 0};
// Negate phi here since shapes use inverse frame-mapping
Rz = [cos(-phi), -sin(-phi), 0;
sin(-phi), cos(-phi), 0;
0, 0, 1];
annotation(
uses(Modelica(version = "4.1.0")),
Icon(graphics = {Rectangle(lineColor = {204, 204, 204}, fillColor = {255, 255, 255}, fillPattern = FillPattern.Sphere, extent = {{-100, 100}, {100, -100}}, radius = 10), Text(extent = {{-100, 100}, {100, -100}}, textString = "VIS")}));
end vis2d;