|
9 | 9 | "outputs": [], |
10 | 10 | "source": [ |
11 | 11 | "import sympy as sp\n", |
| 12 | + "from packaging.version import parse\n", |
12 | 13 | "from sympy import * # noqa\n", |
13 | 14 | "from sympy.solvers.solveset import linsolve\n", |
14 | 15 | "\n", |
15 | 16 | "sp.init_session()\n", |
16 | | - "sp.init_printing()" |
| 17 | + "sp.init_printing()\n", |
| 18 | + "\n", |
| 19 | + "sp_version_compatible = \"1.12.1\"\n", |
| 20 | + "if parse(sp.__version__) > parse(sp_version_compatible):\n", |
| 21 | + " raise ValueError(\n", |
| 22 | + " f\"Versions of sympy>{sp_version_compatible} do not yield correct results, please use sympy<={sp_version_compatible}\"\n", |
| 23 | + " )" |
17 | 24 | ] |
18 | 25 | }, |
19 | 26 | { |
|
151 | 158 | " MX_charpoly = MX_charmat.det()\n", |
152 | 159 | " MX_charpoly = factor(MX_charpoly.as_expr())\n", |
153 | 160 | "\n", |
154 | | - " print(r\"Characteristic Polynomial:\")\n", |
| 161 | + " print(r\"Characteristic polynomial:\")\n", |
155 | 162 | " display(MX_charpoly)\n", |
156 | 163 | "\n", |
157 | 164 | " MX_eigenvals = sp.solve(MX_charpoly, lamda)\n", |
|
171 | 178 | " print(r\"Eigenvalue:\")\n", |
172 | 179 | " display(ev)\n", |
173 | 180 | "\n", |
174 | | - " print(r\"Characteristic Matrix:\")\n", |
| 181 | + " print(r\"Characteristic matrix:\")\n", |
175 | 182 | " display(A)\n", |
176 | 183 | "\n", |
177 | 184 | " # Perform Gaussian elimination (necessary for lamda != 0)\n", |
178 | 185 | " if ev != 0.0:\n", |
179 | | - " print(r\"Gaussian Elimination:\")\n", |
| 186 | + " print(r\"Gaussian elimination:\")\n", |
180 | 187 | " print(r\"A[0,:] += A[1,:]\")\n", |
181 | 188 | " A[0, :] += A[1, :]\n", |
182 | 189 | " print(r\"A[0,:] += A[2,:]\")\n", |
|
235 | 242 | " diff = MX * ep[2][j] - ep[0] * ep[2][j]\n", |
236 | 243 | " diff.simplify()\n", |
237 | 244 | " if diff != zeros(DD, 1):\n", |
238 | | - " print(\"Could Not Verify Characteristic Equation for Some Eigenpairs\")\n", |
239 | | - " display(diff)\n", |
| 245 | + " raise ValueError(\n", |
| 246 | + " \"Could not verify characteristic equation for some eigenpairs\"\n", |
| 247 | + " )\n", |
240 | 248 | "\n", |
241 | 249 | " # Define integration constants\n", |
242 | 250 | " a = []\n", |
|
276 | 284 | " diff = X_t.diff(t).diff(t).subs(t, tn).subs(om, c * knorm).expand() - d2X_dt2\n", |
277 | 285 | " diff.simplify()\n", |
278 | 286 | " if diff != zeros(DD, 1):\n", |
279 | | - " print(rf\"Could Not Verify Time Integration for {str(X)}\")\n", |
280 | | - " display(diff)\n", |
| 287 | + " raise ValueError(rf\"Could not verify time integration for {str(X)}\")\n", |
281 | 288 | "\n", |
282 | 289 | " return X_t, X_new" |
283 | 290 | ] |
|
565 | 572 | "metadata": {}, |
566 | 573 | "outputs": [], |
567 | 574 | "source": [ |
568 | | - "print(r\"Solve Equations for E and G:\")\n", |
| 575 | + "print(r\"Solving equations for E and G...\")\n", |
569 | 576 | "EG_t, EG_new = evolve(EG, dEG_dt, d2EG_dt2)\n", |
570 | 577 | "\n", |
571 | | - "print(r\"Solve Equations for B and F:\")\n", |
| 578 | + "print(r\"Solving equations for B and F...\")\n", |
572 | 579 | "BF_t, BF_new = evolve(BF, dBF_dt, d2BF_dt2)\n", |
573 | 580 | "\n", |
574 | 581 | "# Check correctness by taking *first* derivative\n", |
|
577 | 584 | "diff = EG_t.diff(t).subs(t, tn).subs(om, c * knorm).expand() - dEG_dt\n", |
578 | 585 | "diff.simplify()\n", |
579 | 586 | "if diff != zeros(DD, 1):\n", |
580 | | - " print(rf\"Could Not Verify Time Integration for {str(EG)}\")\n", |
581 | | - " display(diff)\n", |
| 587 | + " raise ValueError(rf\"Could not verify time integration for {str(EG)}\")\n", |
582 | 588 | "# B,F\n", |
583 | 589 | "diff = BF_t.diff(t).subs(t, tn).subs(om, c * knorm).expand() - dBF_dt\n", |
584 | 590 | "diff.simplify()\n", |
585 | 591 | "if diff != zeros(DD, 1):\n", |
586 | | - " print(rf\"Could Not Verify Time Integration for {str(BF)}\")\n", |
587 | | - " display(diff)" |
| 592 | + " raise ValueError(rf\"Could not verify time integration for {str(BF)}\")" |
588 | 593 | ] |
589 | 594 | }, |
590 | 595 | { |
|
600 | 605 | "metadata": {}, |
601 | 606 | "outputs": [], |
602 | 607 | "source": [ |
603 | | - "# Code generation\n", |
604 | | - "\n", |
605 | 608 | "# 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\n", |
606 | 609 | "# EG: Exx, Exy, Exz, Eyx, Eyy, Eyz, Ezx, Ezy, Ezz, Gx, Gy, Gz\n", |
607 | 610 | "# BF: Bxx, Bxy, Bxz, Byx, Byy, Byz, Bzx, Bzy, Bzz, Fx, Fy, Fz\n", |
608 | 611 | "\n", |
609 | 612 | "# Select update equation (left hand side)\n", |
610 | 613 | "X_new = sp.Matrix.vstack(EG_new, BF_new)\n", |
| 614 | + "X_old = sp.Matrix.vstack(EG, BF)\n", |
611 | 615 | "for i in range(X_new.shape[0]):\n", |
612 | 616 | " field_lhs = X_new[i, 0]\n", |
| 617 | + " field_lhs_label = X_old[i, 0]\n", |
613 | 618 | " # Extract individual terms (right hand side)\n", |
614 | | - " X = sp.Matrix.vstack(EG, BF)\n", |
615 | | - " for j in range(X.shape[0]):\n", |
616 | | - " field_rhs = X[j, 0]\n", |
| 619 | + " for j in range(X_old.shape[0]):\n", |
| 620 | + " field_rhs = X_old[j, 0]\n", |
617 | 621 | " coeff = field_lhs.coeff(field_rhs, 1).simplify()\n", |
618 | | - " print(rf\"Coefficient of {str(field_rhs)} Multiplying {str(field_rhs)}\")\n", |
619 | | - " display(coeff)\n", |
620 | | - " # print(ccode(Assignment(sp.symbols(r'LHS'), C1)))" |
| 622 | + " print(rf\"Coefficient of {str(field_lhs_label)} multiplying {str(field_rhs)}\")\n", |
| 623 | + " display(coeff)" |
621 | 624 | ] |
| 625 | + }, |
| 626 | + { |
| 627 | + "cell_type": "code", |
| 628 | + "execution_count": null, |
| 629 | + "metadata": {}, |
| 630 | + "outputs": [], |
| 631 | + "source": [] |
622 | 632 | } |
623 | 633 | ], |
624 | 634 | "metadata": { |
|
637 | 647 | "name": "python", |
638 | 648 | "nbconvert_exporter": "python", |
639 | 649 | "pygments_lexer": "ipython3", |
640 | | - "version": "3.13.1" |
| 650 | + "version": "3.13.3" |
641 | 651 | } |
642 | 652 | }, |
643 | 653 | "nbformat": 4, |
|
0 commit comments