From cc906cbd23b8e4c359d4cf02aaeeec84b492fa57 Mon Sep 17 00:00:00 2001 From: Joppe Blondel Date: Sun, 8 Mar 2026 23:43:57 +0100 Subject: [PATCH] Some fixes --- BondGraphLib.mo | 36 +++++++++++++++++++++++---- BondGraphLib2D.mo | 47 ++++++++++++++++++++++++++++++++--- formatter.py | 2 +- test2d.mo | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 10 deletions(-) mode change 100644 => 100755 formatter.py create mode 100644 test2d.mo diff --git a/BondGraphLib.mo b/BondGraphLib.mo index c5d6ad6..ae34f77 100644 --- a/BondGraphLib.mo +++ b/BondGraphLib.mo @@ -11,7 +11,7 @@ package BondGraphLib 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(origin = {0, 80}, extent = {{-10, -10}, {10, 10}}))); + 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 @@ -31,7 +31,7 @@ package BondGraphLib 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(origin = {0, 80}, extent = {{-10, -10}, {10, 10}}))); + 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 @@ -48,7 +48,7 @@ package BondGraphLib 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(origin = {0, 80}, extent = {{-10, -10}, {10, 10}}))); + 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" @@ -91,7 +91,7 @@ package BondGraphLib 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}}))); - input Real e0 "Imposed effort"; + parameter Real e0 "Imposed effort"; equation p.e = e0; annotation( @@ -101,7 +101,7 @@ package BondGraphLib 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}}))); - input Real f0 "Imposed flow"; + parameter Real f0 "Imposed flow"; equation p.f = f0; annotation( @@ -189,6 +189,32 @@ package BondGraphLib Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "mGY")})); end mGY; + model fsensor + 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.RealOutput f "Flow output" annotation( + Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {82, 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(extent = {{-80, 100}, {80, -100}}, textString = "f", textStyle = {TextStyle.Italic})})); + end fsensor; + + model esensor + 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.RealOutput e "Effort output" annotation( + Placement(transformation(origin = {-8, -64}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {82, 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(extent = {{-80, 100}, {80, -100}}, textString = "e", textStyle = {TextStyle.Italic})})); + 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"))); diff --git a/BondGraphLib2D.mo b/BondGraphLib2D.mo index 2c82223..3f54cc1 100644 --- a/BondGraphLib2D.mo +++ b/BondGraphLib2D.mo @@ -11,7 +11,7 @@ package BondGraphLib2D 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(origin = {0, 80}, extent = {{-10, -10}, {10, 10}}))); + Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}}))); Real f[2]; equation // Efforts sum to zero, with signs from bond directions @@ -33,7 +33,7 @@ package BondGraphLib2D 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(origin = {0, 80}, extent = {{-10, -10}, {10, 10}}))); + Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}}))); Real e[2]; equation // Flows sum to zero, with signs from bond directions @@ -52,7 +52,7 @@ package BondGraphLib2D partial model OnePortPassive "One-port passive 2D multibond element" BondPort p "Generic power port" annotation( - Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {0, 80}, extent = {{-10, -10}, {10, 10}}))); + Placement(transformation(origin = {-44, 18}, extent = {{-10, -10}, {10, 10}}), iconTransformation(extent = {{-10, -10}, {10, 10}}))); end OnePortPassive; partial model OnePortEnergetic "One-port 2D multibond storage element" @@ -193,8 +193,47 @@ package BondGraphLib2D Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "mGY", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) })); end mGY; + package TransRotUtils + + model mTFrot2lin + BondGraphLib.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; + 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")), Diagram(graphics)); -end BondGraphLib2D; \ No newline at end of file +end BondGraphLib2D; diff --git a/formatter.py b/formatter.py old mode 100644 new mode 100755 index 685b8cf..4bcdf23 --- a/formatter.py +++ b/formatter.py @@ -82,4 +82,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/test2d.mo b/test2d.mo new file mode 100644 index 0000000..1a5791c --- /dev/null +++ b/test2d.mo @@ -0,0 +1,63 @@ +model test2d + BondGraphLib.J1 w_com(N = 3, s = {1, -1, -1}) annotation( + Placement(transformation(origin = {0, 120}, extent = {{-10, -10}, {10, 10}}))); + BondGraphLib.Sf sf(f0 = 1) annotation( + Placement(transformation(origin = {50, 120}, extent = {{-10, -10}, {10, 10}}, rotation = 180))); + BondGraphLib2D.TransRotUtils.mTFrot2lin mTFrot2lin(r_body = {0.1, 0}) annotation( + Placement(transformation(origin = {0, 80}, extent = {{-10, -10}, {10, 10}}, rotation = -90))); + BondGraphLib2D.J0 j0(N = 3, s = {1, 1, -1}) annotation( + Placement(transformation(origin = {0, 40}, extent = {{-10, -10}, {10, 10}}))); + BondGraphLib2D.J1 v_p_bff(N = 2, s = {1, -1}) annotation( + Placement(transformation(origin = {-40, 40}, extent = {{-10, -10}, {10, 10}}))); + BondGraphLib2D.J1 v_com_bff(N = 2, s = {-1, -1}) annotation( + Placement(transformation(origin = {40, 40}, extent = {{-10, -10}, {10, 10}}))); + BondGraphLib2D.TransRotUtils.rTF rTF annotation( + Placement(transformation(origin = {40, 0}, extent = {{-10, -10}, {10, 10}}, rotation = -90))); + BondGraphLib2D.TransRotUtils.rTF rTF1 annotation( + Placement(transformation(origin = {-40, 0}, extent = {{10, -10}, {-10, 10}}, rotation = 90))); + BondGraphLib.fsensor wsensor annotation( + Placement(transformation(origin = {50, 90}, extent = {{-10, -10}, {10, 10}}))); + Modelica.Blocks.Continuous.Integrator w2phi annotation( + Placement(transformation(origin = {90, 90}, extent = {{-10, -10}, {10, 10}}))); + BondGraphLib2D.J1 v_y_in(N = 1, s = {1}) annotation( + Placement(transformation(origin = {-40, -40}, extent = {{-10, -10}, {10, 10}}))); + BondGraphLib2D.J1 v_com_in(N = 2, s = {1, -1}) annotation( + Placement(transformation(origin = {40, -40}, extent = {{-10, -10}, {10, 10}}))); + BondGraphLib2D.I mass annotation( + Placement(transformation(origin = {80, -40}, extent = {{-10, -10}, {10, 10}}))); +equation + connect(sf.p, w_com.P[1]) annotation( + Line(points = {{42, 120}, {0, 120}}, color = {0, 0, 127}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6)); + 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( + 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(v_com_bff.P[2], rTF.p1) annotation( + Line(points = {{40, 40}, {40, 8}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6)); + connect(v_p_bff.P[2], rTF1.p1) annotation( + Line(points = {{-40, 40}, {-40, 8}}, 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}, {42, 90}}, color = {0, 0, 127}, 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, rTF1.phi) annotation( + Line(points = {{102, 90}, {110, 90}, {110, 20}, {0, 20}, {0, 0}, {-32, 0}}, color = {0, 0, 127}, arrow = {Arrow.None, Arrow.Filled})); + connect(w2phi.y, rTF.phi) annotation( + Line(points = {{102, 90}, {110, 90}, {110, 20}, {0, 20}, {0, 0}, {32, 0}}, color = {0, 0, 127}, arrow = {Arrow.None, Arrow.Filled})); + connect(rTF1.p2, v_y_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(rTF.p2, v_com_in.P[1]) annotation( + Line(origin = {1, 0},points = {{40, -8}, {40, -40}}, color = {0, 85, 0}, thickness = 0.75, arrow = {Arrow.None, Arrow.Half}, arrowSize = 6)); + 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)); + annotation( + uses(Modelica(version = "4.1.0")), + experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-06, Interval = 0.02), + Diagram(coordinateSystem(extent = {{-60, 140}, {120, -60}}), graphics = {Text(origin = {-40, -52}, extent = {{-12, 4}, {12, -4}}, textString = "v_p"), Text(origin = {40, -52}, extent = {{-12, 4}, {12, -4}}, textString = "v_com"), Text(origin = {0, 134}, extent = {{-12, 4}, {12, -4}}, textString = "w_com")}), + Icon(coordinateSystem(extent = {{-200, -200}, {200, 200}})), + version = ""); +end test2d; \ No newline at end of file