Initial commit

This commit is contained in:
2026-03-08 19:34:12 +01:00
commit 3bb0a04d4c
3 changed files with 480 additions and 0 deletions

195
BondGraphLib.mo Normal file
View File

@@ -0,0 +1,195 @@
package BondGraphLib
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(origin = {0, 80}, 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")}));
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(origin = {0, 80}, 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")}));
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(origin = {0, 80}, 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")}));
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")}));
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")}));
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}})));
input Real e0 "Imposed effort";
equation
p.e = e0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "Se")}));
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}})));
input Real f0 "Imposed flow";
equation
p.f = f0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "Sf")}));
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")}));
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")}));
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")}));
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")}));
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")}));
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")}));
end mGY;
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 BondGraphLib;

200
BondGraphLib2D.mo Normal file
View File

@@ -0,0 +1,200 @@
package BondGraphLib2D
connector BondPort "Bond graph 2D multibond power port"
Real e[2] "Effort vector";
flow Real f[2] "Flow vector";annotation(
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)"
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(origin = {0, 80}, extent = {{-10, -10}, {10, 10}})));
Real f[2];
equation
// Efforts sum to zero, with signs from bond directions
for j in 1:2 loop
sum(s[i] * P[i].e[j] for i in 1:N) = 0;
end for;
// 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", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
end J1;
model J0 "Bond graph 2D 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(origin = {0, 80}, extent = {{-10, -10}, {10, 10}})));
Real e[2];
equation
// Flows sum to zero, with signs from bond directions
for j in 1:2 loop
sum(s[i] * P[i].f[j] for i in 1:N) = 0;
end for;
// 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", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
end J0;
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}})));
end OnePortPassive;
partial model OnePortEnergetic "One-port 2D multibond 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[2] "Conserved quantity";
end OnePortEnergetic;
model C "Bond graph 2D C element"
extends OnePortEnergetic(state(start=q0, each fixed=true));
parameter Real c[2,2] = [1, 0; 0, 1] "Capacitance matrix inverse denominator form";
parameter Real q0[2] = {0, 0} "Initial stored quantity (charge)";
equation
der(state) = p.f;
c * p.e = state;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "C", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
end C;
model I "Bond graph 2D I element"
extends OnePortEnergetic(state(start=p0, each fixed=true));
parameter Real I[2,2] = [1, 0; 0, 1] "Inertance / inductance / mass matrix";
parameter Real p0[2] = {0, 0} "Initial stored quantity (momentum / flux)";
equation
der(state) = p.e;
I * p.f = state;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "I", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
end I;
model R "Bond graph 2D resistor"
extends OnePortPassive;
parameter Real R[2,2] = [1, 0; 0, 1] "Resistance matrix";
equation
p.e = R * p.f;
annotation(
Icon(graphics = {Text(extent = {{-100, 100}, {100, -100}}, textString = "R", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
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}})));
input Real e0[2] "Imposed effort";
equation
p.e = e0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "Se", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
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}})));
input Real f0[2] "Imposed flow";
equation
p.f = f0;
annotation(
Icon(graphics = {Text(origin = {-20, 0}, extent = {{-80, 100}, {80, -100}}, textString = "Sf", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
end Sf;
model TF "Bond graph 2D 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[2,2] = [1, 0; 0, 1] "Transformer ratio matrix";
equation
p1.e = n * p2.e;
transpose(n) * p1.f = p2.f;
annotation(
Icon(graphics = {Text(extent = {{-80, 100}, {80, -100}}, textString = "TF", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
end TF;
model GY "Bond graph 2D 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[2,2] = [1, 0; 0, 1] "Gyrator modulus matrix";
equation
p1.e = r * p2.f;
p2.e = transpose(r) * p1.f;
annotation(
Icon(graphics = {Text(extent = {{-80, 100}, {80, -100}}, textString = "GY", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
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[2] "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", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
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[2] "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", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
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[2,2] "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;
transpose(m) * p1.f = p2.f;
annotation(
Diagram(graphics),
Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "mTF", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
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[2,2] "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 = transpose(m) * p1.f;
annotation(
Diagram(graphics),
Icon(graphics = {Text(extent = {{-60, 100}, {60, -100}}, textString = "mGY", textStyle = {TextStyle.Bold, TextStyle.UnderLine}) }));
end mGY;
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;

85
formatter.py Normal file
View File

@@ -0,0 +1,85 @@
#!/usr/bin/env python3
import sys
import re
from pathlib import Path
INDENT = " " * 4
CLASS_START_RE = re.compile(
r"^\s*(?:partial\s+)?(?:package|model|connector|record|function|block|type|operator)\b"
)
SECTION_RE = re.compile(
r"^\s*(?:equation|algorithm|initial\s+equation|initial\s+algorithm|protected|public)\b"
)
IF_RE = re.compile(r"^\s*if\b.*\bthen\b")
ELSEIF_RE = re.compile(r"^\s*elseif\b.*\bthen\b")
ELSE_RE = re.compile(r"^\s*else\b")
FOR_RE = re.compile(r"^\s*for\b.*\bloop\b")
WHEN_RE = re.compile(r"^\s*when\b.*\bthen\b")
ELSEWHEN_RE = re.compile(r"^\s*elsewhen\b.*\bthen\b")
END_RE = re.compile(r"^\s*end\b")
def format_modelica(text: str) -> str:
lines = text.replace("\t", " ").splitlines()
out = []
indent = 0
for raw in lines:
stripped = raw.strip()
if not stripped:
out.append("")
continue
# Closing constructs outdent before printing
if END_RE.match(stripped):
indent = max(indent - 1, 0)
# else / elseif / elsewhen align with matching if/when
elif ELSE_RE.match(stripped) or ELSEIF_RE.match(stripped) or ELSEWHEN_RE.match(stripped):
indent = max(indent - 1, 0)
# Sections stay at model level, not one level deeper
elif SECTION_RE.match(stripped):
indent = max(indent - 1, 0)
out.append(f"{INDENT * indent}{stripped}")
# Increase indent only for real blocks
if CLASS_START_RE.match(stripped):
indent += 1
elif IF_RE.match(stripped):
indent += 1
elif FOR_RE.match(stripped):
indent += 1
elif WHEN_RE.match(stripped):
indent += 1
elif ELSEIF_RE.match(stripped) or ELSE_RE.match(stripped) or ELSEWHEN_RE.match(stripped):
indent += 1
elif SECTION_RE.match(stripped):
indent += 1
return "\n".join(out) + "\n"
def main():
if len(sys.argv) < 2:
print("Usage: python format_mo.py input.mo [output.mo]")
sys.exit(1)
input_file = Path(sys.argv[1])
output_file = Path(sys.argv[2]) if len(sys.argv) > 2 else input_file
text = input_file.read_text(encoding="utf-8")
formatted = format_modelica(text)
output_file.write_text(formatted, encoding="utf-8")
print(f"Formatted {output_file}")
if __name__ == "__main__":
main()