Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math asserts (#4344) #4470

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Modelica/Math/Nonlinear.mo
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ package Nonlinear "Library of functions operating on nonlinear equations"
print("Numerical integral value = " + String(I_numerical[1], format=
"2.16f"));
print("Absolute difference = " + String(I_err[1], format="2.0e"));
assert(abs(I_err[1])<1e-4, "Integral should be close to desired");

print("");
print("Function 2 (integral(sin(5*x)*dx) from x=0 to x=13): ");
Expand All @@ -56,6 +57,7 @@ package Nonlinear "Library of functions operating on nonlinear equations"
print("Numerical integral value = " + String(I_numerical[2], format=
"2.16f"));
print("Absolute difference = " + String(I_err[2], format="2.0e"));
assert(abs(I_err[2])<1e-4, "Integral should be close to desired");

print("");
print("Function 3 (Elliptic integral from x=0 to pi/2): ");
Expand All @@ -64,6 +66,7 @@ package Nonlinear "Library of functions operating on nonlinear equations"
print("Numerical integral value = " + String(I_numerical[3], format=
"2.16f"));
print("Absolute difference = " + String(I_err[3], format="2.0e"));
assert(abs(I_err[3])<1e-4, "Integral should be close to desired");

annotation (Documentation(info="<html>
<p>
Expand Down Expand Up @@ -200,18 +203,21 @@ The following integrals are computed:
print("Analytical zero = " + String(u_analytical[1], format="2.16f"));
print("Numerical zero = " + String(u_numerical[1], format="2.16f"));
print("Absolute difference = " + String(u_err[1], format="2.0e"));
assert(abs(u_err[1])<1e-10, "Solution should be close to desired");

print("");
print("Function 2 (3*u - sin(3*u) - 1 = 0): ");
print("Analytical zero = " + String(u_analytical[2], format="2.16f"));
print("Numerical zero = " + String(u_numerical[2], format="2.16f"));
print("Absolute difference = " + String(u_err[2], format="2.0e"));
assert(abs(u_err[2])<1e-10, "Solution should be close to desired");

print("");
print("Function 3 (5 + log(u) - u = 0): ");
print("Analytical zero = " + String(u_analytical[3], format="2.16f"));
print("Numerical zero = " + String(u_numerical[3], format="2.16f"));
print("Absolute difference = " + String(u_err[3], format="2.0e"));
assert(abs(u_err[3])<1e-10, "Solution should be close to desired");

annotation (Documentation(info="<html>
<p>
Expand Down
18 changes: 14 additions & 4 deletions Modelica/Math/package.mo
Original file line number Diff line number Diff line change
Expand Up @@ -718,26 +718,36 @@ package Matrices "Library of functions operating on matrices"
Real B3[3, 2]=[b3, -3*b3];
Real X3[3, 2];

Real diff;
algorithm
print("\nDemonstrate how to solve linear equation systems:\n");

// Solve regular linear equation with a right hand side vector
x1 := Math.Matrices.solve(A1, b1);
print("diff1 = " + String(Vectors.norm(x1 - x1_ref)));
diff := Vectors.norm(x1 - x1_ref);
print("diff1 = " + String(diff));
assert(abs(diff)<1e-10, "Solution should be close to desired");

// Solve regular linear equation with a right hand side matrix
X2 := Math.Matrices.solve2(A1, B2);
print("diff2 = " + String(Matrices.norm(X2 - [x1_ref, 2*x1_ref, -3*x1_ref])));
diff := Matrices.norm(X2 - [x1_ref, 2*x1_ref, -3*x1_ref]);
print("diff2 = " + String(diff));
assert(abs(diff)<1e-10, "Solution should be close to desired");

// Solve singular linear equation with a right hand side vector
(x3,rank) := Math.Matrices.leastSquares(A3, b3);
print("diff3 = " + String(Vectors.norm(A3*x3 - b3)) + ", n = " + String(
diff := Vectors.norm(A3*x3 - b3);
print("diff3 = " + String(diff) + ", n = " + String(
size(A3, 1)) + ", rank = " + String(rank));
assert(abs(diff)<1e-10, "Solution should be close to desired");


// Solve singular linear equation with a right hand side matrix
(X3,rank) := Math.Matrices.leastSquares2(A3, B3);
print("diff4 = " + String(Matrices.norm(A3*X3 - B3)) + ", n = " + String(
diff := Matrices.norm(A3*X3 - B3);
print("diff4 = " + String(diff) + ", n = " + String(
size(A3, 1)) + ", rank = " + String(rank));
assert(abs(diff)<1e-10, "Solution should be close to desired");

annotation (Documentation(info="<html>
<p>
Expand Down