Skip to content

Commit fb10bbc

Browse files
Jonas BreulingCopilot
andcommitted
Test for Jacobian shape.
Co-authored-by: Copilot <copilot@github.com>
1 parent df43e64 commit fb10bbc

3 files changed

Lines changed: 88 additions & 22 deletions

File tree

solve_dae/integrate/_dae/base.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,6 @@ def jac_wrapped(t, y, yp, _=None):
348348

349349
return jac_wrapped, Jy, Jyp
350350

351-
@property
352-
def step_size(self):
353-
if self.t_old is None:
354-
return None
355-
else:
356-
return np.abs(self.t - self.t_old)
357-
358351
def step(self):
359352
"""Perform one integration step.
360353
@@ -369,22 +362,22 @@ def step(self):
369362
raise RuntimeError("Attempt to step on a failed or finished "
370363
"solver.")
371364

372-
if self.n == 0 or self.t == self.t_bound:
373-
# Handle corner cases of empty solver or no integration.
374-
self.t_old = self.t
375-
self.t = self.t_bound
376-
message = None
377-
self.status = 'finished'
365+
# if self.n == 0 or self.t == self.t_bound:
366+
# # Handle corner cases of empty solver or no integration.
367+
# self.t_old = self.t
368+
# self.t = self.t_bound
369+
# message = None
370+
# self.status = 'finished'
371+
# else:
372+
t = self.t
373+
success, message = self._step_impl()
374+
375+
if not success:
376+
self.status = 'failed'
378377
else:
379-
t = self.t
380-
success, message = self._step_impl()
381-
382-
if not success:
383-
self.status = 'failed'
384-
else:
385-
self.t_old = t
386-
if self.direction * (self.t - self.t_bound) >= 0:
387-
self.status = 'finished'
378+
self.t_old = t
379+
if self.direction * (self.t - self.t_bound) >= 0:
380+
self.status = 'finished'
388381

389382
return message
390383

solve_dae/integrate/_dae/tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
py3.install_sources([
22
'__init__.py',
33
'test_check_arguments.py',
4+
'test_base.py',
45
'test_consistent_initial_conditions.py',
56
'test_integration_complex.py',
67
'test_integration_linear.py',
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import numpy as np
2+
import pytest
3+
from itertools import product
4+
from scipy.sparse import csc_matrix
5+
from solve_dae.integrate import solve_dae
6+
7+
8+
# initial conditions
9+
y0 = [1]
10+
yp0 = [0.5 * y0[0]]
11+
t_span = (0, 1)
12+
13+
14+
def F(t, y, yp):
15+
return yp - 0.5 * y
16+
17+
def jac_dense(t, y, yp):
18+
return np.eye(len(y)), -0.5 * np.eye(len(yp))
19+
20+
def jac_sparse(t, y, yp):
21+
return csc_matrix(np.eye(len(y))), csc_matrix(-0.5 * np.eye(len(yp)))
22+
23+
def jac_wrong_shape_dense(t, y, yp):
24+
return np.eye(len(y) + 1), -0.5 * np.eye(len(yp) + 1)
25+
26+
def jac_wrong_shape_sparse(t, y, yp):
27+
return csc_matrix(np.eye(len(y) + 1)), csc_matrix(-0.5 * np.eye(len(yp) + 1))
28+
29+
30+
parameters_method = ["BDF", "Radau"]
31+
parameters_jac = [jac_dense, jac_sparse, jac_wrong_shape_dense, jac_wrong_shape_sparse]
32+
33+
34+
parameters = product(
35+
parameters_method,
36+
parameters_jac,
37+
)
38+
39+
40+
@pytest.mark.parametrize("method, jac", parameters)
41+
def test_jacobian_shape(method, jac):
42+
if jac in [jac_wrong_shape_dense, jac_wrong_shape_sparse]:
43+
with pytest.raises(ValueError) as excinfo:
44+
solve_dae(F, t_span, y0, yp0, method=method, jac=jac)
45+
46+
Jyp, Jy = jac(t_span[0], y0, yp0)
47+
message = ("`Jy` is expected to have shape {}, but " +
48+
"actually has {}.").format((len(y0), len(y0)), Jy.shape)
49+
assert (
50+
message
51+
in str(excinfo.value)
52+
)
53+
54+
message = ("`Jpy` is expected to have shape {}, but " +
55+
"actually has {}.").format((len(y0), len(y0)), Jyp.shape)
56+
assert (
57+
message
58+
in str(excinfo.value)
59+
)
60+
else:
61+
solve_dae(F, t_span, y0, yp0, method=method, jac=jac)
62+
63+
64+
# @pytest.mark.parametrize("method,", parameters_method)
65+
# def test_step(method):
66+
# sol = solve_dae(F, t_span, y0, yp0, method=method)
67+
# pass
68+
69+
70+
# if __name__ == "__main__":
71+
# for params in parameters:
72+
# test_jacobian_shape(*params)

0 commit comments

Comments
 (0)