Skip to content

Commit 17a15ed

Browse files
committed
Fix assert checks in online examples
1 parent e276ef1 commit 17a15ed

2 files changed

Lines changed: 34 additions & 30 deletions

File tree

docs/source/examples/robertson.ipynb

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"outputs": [],
3939
"source": [
4040
"import numpy as np\n",
41+
"import numpy.testing as npt\n",
42+
"\n",
4143
"import sksundae as sun\n",
4244
"import matplotlib.pyplot as plt\n",
4345
"\n",
@@ -76,9 +78,9 @@
7678
"soln.y[:,1] *= 1e4 # scale the y1 values for plotting\n",
7779
"\n",
7880
"plt.semilogx(soln.t, soln.y)\n",
79-
"plt.legend(['y0', 'y1', 'y2'])\n",
80-
"plt.xlabel(r\"$t$\");\n",
81-
"plt.ylabel(\"concentration\");"
81+
"plt.legend([\"y0\", \"y1\", \"y2\"])\n",
82+
"plt.xlabel(\"time, $t$\");\n",
83+
"plt.ylabel(\"concentration, $c$\");"
8284
]
8385
},
8486
{
@@ -114,9 +116,9 @@
114116
"soln.y[:,1] *= 1e4 # scale the y1 values for plotting\n",
115117
"\n",
116118
"plt.semilogx(soln.t, soln.y)\n",
117-
"plt.legend(['y0', 'y1', 'y2'])\n",
118-
"plt.xlabel(r\"$t$\");\n",
119-
"plt.ylabel(\"concentration\");"
119+
"plt.legend([\"y0\", \"y1\", \"y2\"])\n",
120+
"plt.xlabel(\"time, $t$\");\n",
121+
"plt.ylabel(\"concentration, $c$\");"
120122
]
121123
},
122124
{
@@ -126,7 +128,7 @@
126128
"## Step-wise solutions\n",
127129
"Solving step-by-step instead of across a full time span can be beneficial in some cases, especially for debugging. Therefore, a `step` method is also available in `IDA`. Before taking a step, the solver needs to know the initial conditions and time to determine the direction of integration for the following steps. Thus, before calling `step`, you should call `init_step`, as shown below. The initialization is handled automatically when using the `solve` method but must be done manually in a step-by-step approach. Since we are using the same solver instance that we initialized above, the `init_step` call will also provide a correction to `yp0` as specified by the `calc_initcond` option.\n",
128130
"\n",
129-
"Below, we run `init_step` and compare the solution `soln_0` to the initial values from the full solve, from above. Afterward, we take a step evaluated at `soln.t[10]` using the solution object from the full solve, allowing us to compare the step-by-step solution to a portion of the full solution. We only check that the solutions are within some tolerance (`1e-6`) because the solver's internal steps may differ from those in the full solution, meaning the values will be close but may not be exactly the same."
131+
"Below, we run `init_step` and compare the solution `soln_0` to the initial values from the full solve, from above. Afterward, we take a step evaluated at `soln.t[10]` using the solution object from the full solve, allowing us to compare the step-by-step solution to a portion of the full solution. We only check that the solutions are within some tolerance (`1e-7`) because the solver's internal steps may differ from those in the full solution, meaning the values will be close but may not be exactly the same."
130132
]
131133
},
132134
{
@@ -140,14 +142,14 @@
140142
"soln_0 = solver.init_step(tspan[0], y0, yp0)\n",
141143
"print(soln_0)\n",
142144
"\n",
143-
"assert soln_0.t == soln.t[0]\n",
144-
"assert np.all(soln_0.y - soln.y[0] < 1e-6)\n",
145+
"npt.assert_allclose(soln.t[0], soln_0.t)\n",
146+
"npt.assert_allclose(soln.y[0], soln_0.y, atol=1e-7)\n",
145147
"\n",
146148
"soln_1 = solver.step(soln.t[10])\n",
147149
"print(soln_1)\n",
148150
"\n",
149-
"assert soln_1.t == soln.t[10]\n",
150-
"assert np.all(soln_1.y - soln.y[10] < 1e-6)"
151+
"npt.assert_allclose(soln.t[10], soln_1.t)\n",
152+
"npt.assert_allclose(soln.y[10], soln_1.y, atol=1e-7)"
151153
]
152154
},
153155
{
@@ -190,9 +192,9 @@
190192
"soln.y_events[:,1] *= 1e4 # scale the y1 values for plotting\n",
191193
"\n",
192194
"plt.semilogx(soln.t, soln.y, '-', soln.t_events, soln.y_events, 'or')\n",
193-
"plt.legend(['y0', 'y1', 'y2'])\n",
194-
"plt.xlabel(r\"$t$\");\n",
195-
"plt.ylabel(\"concentration\");\n",
195+
"plt.legend([\"y0\", \"y1\", \"y2\"])\n",
196+
"plt.xlabel(\"time, $t$\")\n",
197+
"plt.ylabel(\"concentration, $c$\")\n",
196198
"\n",
197199
"for t in soln.t_events:\n",
198200
" plt.axvline(t, linestyle='--', color='k')"
@@ -242,9 +244,9 @@
242244
"soln.y[:,1] *= 1e4 # scale the y1 values for plotting\n",
243245
"\n",
244246
"plt.semilogx(soln.t, soln.y)\n",
245-
"plt.legend(['y0', 'y1', 'y2'])\n",
246-
"plt.xlabel(r\"$t$\");\n",
247-
"plt.ylabel(\"concentration\");"
247+
"plt.legend([\"y0\", \"y1\", \"y2\"])\n",
248+
"plt.xlabel(\"time, $t$\");\n",
249+
"plt.ylabel(\"concentration, $c$\");"
248250
]
249251
},
250252
{
@@ -273,7 +275,7 @@
273275
"name": "python",
274276
"nbconvert_exporter": "python",
275277
"pygments_lexer": "ipython3",
276-
"version": "3.12.5"
278+
"version": "3.14.0"
277279
}
278280
},
279281
"nbformat": 4,

docs/source/examples/van_der_pol.ipynb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"outputs": [],
3636
"source": [
3737
"import numpy as np\n",
38+
"import numpy.testing as npt\n",
39+
"\n",
3840
"import sksundae as sun\n",
3941
"import matplotlib.pyplot as plt\n",
4042
"\n",
@@ -69,8 +71,8 @@
6971
"print(soln)\n",
7072
"\n",
7173
"plt.plot(soln.t, soln.y[:,0])\n",
72-
"plt.xlabel(r\"$t$\");\n",
73-
"plt.ylabel(r\"$x$\");"
74+
"plt.xlabel(\"time, $t$\");\n",
75+
"plt.ylabel(\"position, $x$\");"
7476
]
7577
},
7678
{
@@ -80,7 +82,7 @@
8082
"## Step-wise solutions\n",
8183
"Solving step-by-step instead of across a full time span can be beneficial in some cases, especially for debugging. Therefore, a `step` method is also available in `CVODE`. Before taking a step, the solver needs to know the initial conditions and time to determine the direction of integration for the following steps. Thus, before calling `step`, you should call `init_step`, as shown below. The initialization is handled automatically when using the `solve` method but must be done manually in a step-by-step approach.\n",
8284
"\n",
83-
"Below, we run `init_step` and compare the solution `soln_0` to the initial values from the full solve, from above. Afterward, we take a step evaluated at `soln.t[10]` using the solution object from the full solve, allowing us to compare the step-by-step solution to a portion of the full solution. We only check that the solutions are within some tolerance (`1e-6`) because the solver's internal steps may differ from those in the full solution, meaning the values will be close but may not be exactly the same."
85+
"Below, we run `init_step` and compare the solution `soln_0` to the initial values from the full solve, from above. Afterward, we take a step evaluated at `soln.t[10]` using the solution object from the full solve, allowing us to compare the step-by-step solution to a portion of the full solution. We only check that the solutions are within some tolerance (`1e-7`) because the solver's internal steps may differ from those in the full solution, meaning the values will be close but may not be exactly the same."
8486
]
8587
},
8688
{
@@ -92,14 +94,14 @@
9294
"soln_0 = solver.init_step(0, y0)\n",
9395
"print(soln_0)\n",
9496
"\n",
95-
"assert soln_0.t == soln.t[0]\n",
96-
"assert np.all(soln_0.y - soln.y[0] < 1e-6)\n",
97+
"npt.assert_allclose(soln_0.t, soln.t[0])\n",
98+
"npt.assert_allclose(soln_0.y, soln.y[0], atol=1e-7)\n",
9799
"\n",
98100
"soln_1 = solver.step(soln.t[10])\n",
99101
"print(soln_1)\n",
100102
"\n",
101-
"assert soln_1.t == soln.t[10]\n",
102-
"assert np.all(soln_1.y - soln.y[10] < 1e-6)"
103+
"npt.assert_allclose(soln_1.t, soln.t[10])\n",
104+
"npt.assert_allclose(soln_1.y, soln.y[10], atol=1e-7)"
103105
]
104106
},
105107
{
@@ -142,8 +144,8 @@
142144
"print(soln)\n",
143145
"\n",
144146
"plt.plot(soln.t, soln.y[:,0], '-', soln.t_events, soln.y_events[:,0], 'or')\n",
145-
"plt.xlabel(r\"$t$\");\n",
146-
"plt.ylabel(r\"$x$\");"
147+
"plt.xlabel(\"time, $t$\");\n",
148+
"plt.ylabel(\"position, $x$\");"
147149
]
148150
},
149151
{
@@ -181,8 +183,8 @@
181183
"print(soln)\n",
182184
"\n",
183185
"plt.plot(soln.t, soln.y[:,0])\n",
184-
"plt.xlabel(r\"$t$\");\n",
185-
"plt.ylabel(r\"$x$\");"
186+
"plt.xlabel(\"time, $t$\");\n",
187+
"plt.ylabel(\"position, $x$\");"
186188
]
187189
},
188190
{
@@ -211,7 +213,7 @@
211213
"name": "python",
212214
"nbconvert_exporter": "python",
213215
"pygments_lexer": "ipython3",
214-
"version": "3.12.4"
216+
"version": "3.14.0"
215217
}
216218
},
217219
"nbformat": 4,

0 commit comments

Comments
 (0)