Skip to content

Commit

Permalink
work in progress; more conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon committed Feb 12, 2024
1 parent 208de8f commit 958c90f
Show file tree
Hide file tree
Showing 52 changed files with 228 additions and 212 deletions.
4 changes: 2 additions & 2 deletions examples/cvode_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def f(t,y):
pl.show()

#Basic test
assert y2[-1][0] == pytest.approx(0.00347746, rel = 1e-5)
assert exp_sim.get_last_step() == pytest.approx(0.0222169642893, rel = 1e-3)
assert y2[-1][0] == pytest.approx(0.00347746, abs = 1e-5)
assert exp_sim.get_last_step() == pytest.approx(0.0222169642893, abs = 1e-3)

return exp_mod, exp_sim

Expand Down
2 changes: 1 addition & 1 deletion examples/cvode_basic_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def f(t,y):
pl.show()

#Basic test
assert y[-1][0] == pytest.approx(4.00000000, rel = 1e-3)
assert y[-1][0] == pytest.approx(4.00000000, abs = 1e-3)

return exp_mod, exp_sim

Expand Down
2 changes: 1 addition & 1 deletion examples/cvode_stability.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def f(t,y):

#Basic test
x1 = y[:,0]
assert_less(np.abs(float(x1[-1]) - 1.8601438), 1e-1)
assert np.abs(float(x1[-1]) - 1.8601438) < 1e-1

return exp_mod, exp_sim

Expand Down
9 changes: 3 additions & 6 deletions examples/cvode_with_disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,11 @@ def run_example(with_plots=True):
pl.show()

#Basic test
assert_almost_equal(y[-1][0],8.0)
assert_almost_equal(y[-1][1],3.0)
assert_almost_equal(y[-1][2],2.0)
assert y[-1][0] == pytest.approx(8.0)
assert y[-1][1] == pytest.approx(3.0)
assert y[-1][2] == pytest.approx(2.0)

return exp_mod, exp_sim

if __name__=="__main__":
mod,sim = run_example()



6 changes: 3 additions & 3 deletions examples/cvode_with_initial_sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ def f(t, y, p):
pl.show()

#Basic test
assert y[-1][0] == pytest.approx(1577.6552477, rel = 1e-5)
assert y[-1][1] == pytest.approx(611.9574565, rel = 1e-5)
assert y[-1][2] == pytest.approx(2215.88563217, rel = 1e-5)
assert y[-1][0] == pytest.approx(1577.6552477, abs = 1e-5)
assert y[-1][1] == pytest.approx(611.9574565, abs = 1e-5)
assert y[-1][2] == pytest.approx(2215.88563217, abs = 1e-5)
assert exp_sim.p_sol[0][1][0] == pytest.approx(1.0)

return exp_mod, exp_sim
Expand Down
4 changes: 2 additions & 2 deletions examples/cvode_with_jac.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def jac(t,y):
pl.show()

#Basic tests
assert_almost_equal(y[-1][0],-121.75000000,4)
assert_almost_equal(y[-1][1],-49.100000000)
assert y[-1][0] == pytest.approx(-121.75000000, abs = 1e-4)
assert y[-1][1] == pytest.approx(-49.100000000)

return exp_mod, exp_sim

Expand Down
2 changes: 1 addition & 1 deletion examples/cvode_with_jac_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def jac(t,y):
pl.show()

#Basic tests
assert_almost_equal(y[-1][0],0.9851,3)
assert y[-1][0] == pytest.approx(0.9851, abs = 1e-3)

return exp_mod, exp_sim

Expand Down
4 changes: 2 additions & 2 deletions examples/cvode_with_jac_spgmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def jacv(t,y,fy,v):
pl.show()

#Basic tests
assert_almost_equal(y[-1][0],-121.75000000,4)
assert_almost_equal(y[-1][1],-49.100000000)
assert y[-1][0] == pytest.approx(-121.75000000, abs = 1e-4)
assert y[-1][1] == pytest.approx(-49.100000000)

return exp_mod, exp_sim

Expand Down
12 changes: 6 additions & 6 deletions examples/cvode_with_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ def f(t, y, p):
pl.show()

#Basic test
assert y[-1][0] == pytest.approx(9.05518032e-01, rel = 1e-4)
assert y[-1][1] == pytest.approx(2.24046805e-05, rel = 1e-4)
assert y[-1][2] == pytest.approx(9.44595637e-02, rel = 1e-4)
assert exp_sim.p_sol[0][-1][0] == pytest.approx(-1.8761, rel = 1e-2) #Values taken from the example in Sundials
assert exp_sim.p_sol[1][-1][0] == pytest.approx(2.9614e-06, rel = 1e-8)
assert exp_sim.p_sol[2][-1][0] == pytest.approx(-4.9334e-10, rel = 1e-12)
assert y[-1][0] == pytest.approx(9.05518032e-01, abs = 1e-4)
assert y[-1][1] == pytest.approx(2.24046805e-05, abs = 1e-4)
assert y[-1][2] == pytest.approx(9.44595637e-02, abs = 1e-4)
assert exp_sim.p_sol[0][-1][0] == pytest.approx(-1.8761, abs = 1e-2) #Values taken from the example in Sundials
assert exp_sim.p_sol[1][-1][0] == pytest.approx(2.9614e-06, abs = 1e-8)
assert exp_sim.p_sol[2][-1][0] == pytest.approx(-4.9334e-10, abs = 1e-12)

return exp_mod, exp_sim

Expand Down
12 changes: 6 additions & 6 deletions examples/cvode_with_parameters_fcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ def fsens(t, y, s, p):
pl.show()

#Basic test
assert y[-1][0] == pytest.approx(9.05518032e-01, rel = 1e-4)
assert y[-1][1] == pytest.approx(2.24046805e-05, rel = 1e-4)
assert y[-1][2] == pytest.approx(9.44595637e-02, rel = 1e-4)
assert exp_sim.p_sol[0][-1][0] == pytest.approx(-1.8761, rel = 1e-2) #Values taken from the example in Sundials
assert exp_sim.p_sol[1][-1][0] == pytest.approx(2.9614e-06, rel = 1e-8)
assert exp_sim.p_sol[2][-1][0] == pytest.approx(-4.9334e-10, rel = 1e-12)
assert y[-1][0] == pytest.approx(9.05518032e-01, abs = 1e-4)
assert y[-1][1] == pytest.approx(2.24046805e-05, abs = 1e-4)
assert y[-1][2] == pytest.approx(9.44595637e-02, abs = 1e-4)
assert exp_sim.p_sol[0][-1][0] == pytest.approx(-1.8761, abs = 1e-2) #Values taken from the example in Sundials
assert exp_sim.p_sol[1][-1][0] == pytest.approx(2.9614e-06, abs = 1e-8)
assert exp_sim.p_sol[2][-1][0] == pytest.approx(-4.9334e-10, abs = 1e-12)

return exp_mod, exp_sim

Expand Down
10 changes: 5 additions & 5 deletions examples/cvode_with_parameters_modified.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ def f(t, y, p):
pl.show()

#Basic test
assert y[-1][0] == pytest.approx(9.05518032e-01, rel = 1e-4)
assert y[-1][1] == pytest.approx(2.24046805e-05, rel = 1e-4)
assert y[-1][2] == pytest.approx(9.44595637e-02, rel = 1e-4)
assert exp_sim.p_sol[0][-1][0] == pytest.approx(-1.8761, rel = 1e-2) #Values taken from the example in Sundials
assert exp_sim.p_sol[1][-1][0] == pytest.approx(2.9614e-06, rel = 1e-8)
assert y[-1][0] == pytest.approx(9.05518032e-01, abs = 1e-4)
assert y[-1][1] == pytest.approx(2.24046805e-05, abs = 1e-4)
assert y[-1][2] == pytest.approx(9.44595637e-02, abs = 1e-4)
assert exp_sim.p_sol[0][-1][0] == pytest.approx(-1.8761, abs = 1e-2) #Values taken from the example in Sundials
assert exp_sim.p_sol[1][-1][0] == pytest.approx(2.9614e-06, abs = 1e-8)

return exp_mod, exp_sim

Expand Down
4 changes: 2 additions & 2 deletions examples/cvode_with_preconditioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def prec_solve(t, y, fy, r, gamma, delta, data):
exp_sim.plot()

#Basic verification
assert_almost_equal(y[-1,0],3.11178295,4)
assert_almost_equal(y[-1,1],3.19318992,4)
assert y[-1,0] == pytest.approx(3.11178295, abs = 1e-4)
assert y[-1,1] == pytest.approx(3.19318992, abs = 1e-4)

return exp_mod, exp_sim

Expand Down
2 changes: 1 addition & 1 deletion examples/dasp3_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def dzdt(t,y,z):
pl.show()

#Basic test
assert_almost_equal(y[-1,0], 10.860063849896818, 3)
assert y[-1,0] == pytest.approx( 10.860063849896818, abs = 1e-3)

return exp_mod, exp_sim

Expand Down
2 changes: 1 addition & 1 deletion examples/dopri5_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def f(t,y):
pl.show()

#Basic test
assert_almost_equal(y[-1][0],0.02695199,5)
assert y[-1][0] == pytest.approx(0.02695199, abs = 1e-5)

return exp_mod, exp_sim

Expand Down
6 changes: 3 additions & 3 deletions examples/dopri5_with_disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def run_example(with_plots=True):
pl.show()

#Basic test
assert_almost_equal(y[-1][0],8.0)
assert_almost_equal(y[-1][1],3.0)
assert_almost_equal(y[-1][2],2.0)
assert y[-1][0] == pytest.approx(8.0)
assert y[-1][1] == pytest.approx(3.0)
assert y[-1][2] == pytest.approx(2.0)

return exp_mod, exp_sim

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_vanderpol.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def jac(t,y):

#Basic test
x1 = y[:,0]
assert_less(np.abs(float(x1[-1]) - 1.8601438), 1e-1)
assert np.abs(float(x1[-1]) - 1.8601438) < 1e-1

return exp_mod, exp_sim

Expand Down
6 changes: 3 additions & 3 deletions examples/euler_with_disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ def run_example(with_plots=True):
pl.show()

#Basic test
assert_almost_equal(y[-1][0],8.0)
assert_almost_equal(y[-1][1],3.0)
assert_almost_equal(y[-1][2],2.0)
assert y[-1][0] == pytest.approx(8.0)
assert y[-1][1] == pytest.approx(3.0)
assert y[-1][2] == pytest.approx(2.0)

return exp_mod, exp_sim

Expand Down
2 changes: 1 addition & 1 deletion examples/glimda_vanderpol.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def f(t,y,yd):

#Basic test
x1 = y[:,0]
assert float(x1[-1]) == pytest.approx(1.706168035, rel = 1e-3)
assert float(x1[-1]) == pytest.approx(1.706168035, abs = 1e-3)

return imp_mod, imp_sim

Expand Down
2 changes: 1 addition & 1 deletion examples/ida_basic_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def f(t,y,yd):
pl.show()

#Basic test
assert y[-1][0] == pytest.approx(4.00000000, rel = 1e-3)
assert y[-1][0] == pytest.approx(4.00000000, abs = 1e-3)

return imp_mod, imp_sim

Expand Down
6 changes: 3 additions & 3 deletions examples/ida_with_disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ def run_example(with_plots=True):
pl.show()

#Basic test
assert_almost_equal(y[-1][0],8.0)
assert_almost_equal(y[-1][1],3.0)
assert_almost_equal(y[-1][2],2.0)
assert y[-1][0] == pytest.approx(8.0)
assert y[-1][1] == pytest.approx(3.0)
assert y[-1][2] == pytest.approx(2.0)

return imp_mod, imp_sim

Expand Down
6 changes: 3 additions & 3 deletions examples/ida_with_initial_sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def f(t, y, yd,p):
pl.show()

#Basic test
assert_almost_equal(y[-1][0], 1577.6552477,3)
assert y[-1][1] == pytest.approx(611.9574565, rel = 1e-3)
assert y[-1][2] == pytest.approx(2215.88563217, rel = 1e-3)
assert y[-1][0] == pytest.approx(1577.6552477, abs = 1e-3)
assert y[-1][1] == pytest.approx(611.9574565, abs = 1e-3)
assert y[-1][2] == pytest.approx(2215.88563217, abs = 1e-3)
assert imp_sim.p_sol[0][1][0] == pytest.approx(1.0)


Expand Down
10 changes: 5 additions & 5 deletions examples/ida_with_jac.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def jac(c,t,y,yd):
#Sets the parameters
imp_sim.atol = 1e-6 #Default 1e-6
imp_sim.rtol = 1e-6 #Default 1e-6
imp_sim.suppress_alg = True #Suppres the algebraic variables on the error test
imp_sim.suppress_alg = True #Suppress the algebraic variables on the error test

#Let Sundials find consistent initial conditions by use of 'IDA_YA_YDP_INIT'
imp_sim.make_consistent('IDA_YA_YDP_INIT')
Expand All @@ -107,10 +107,10 @@ def jac(c,t,y,yd):
t, y, yd = imp_sim.simulate(5,1000) #Simulate 5 seconds with 1000 communication points

#Basic tests
assert_almost_equal(y[-1][0],0.9401995, places=4)
assert_almost_equal(y[-1][1],-0.34095124, places=4)
assert yd[-1][0] == pytest.approx(-0.88198927, rel = 1e-4)
assert yd[-1][1] == pytest.approx(-2.43227069, rel = 1e-4)
assert y[-1][0] == pytest.approx(0.9401995, abs = 1e-4)
assert y[-1][1] == pytest.approx(-0.34095124, abs = 1e-4)
assert yd[-1][0] == pytest.approx(-0.88198927, abs = 1e-4)
assert yd[-1][1] == pytest.approx(-2.43227069, abs = 1e-4)

#Plot
if with_plots:
Expand Down
4 changes: 2 additions & 2 deletions examples/ida_with_jac_spgmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def jacv(t,y,yd,res,v,c):
t, y, yd = imp_sim.simulate(5, 1000) #Simulate 5 seconds with 1000 communication points

#Basic tests
assert_almost_equal(y[-1][0],-121.75000000,4)
assert_almost_equal(y[-1][1],-49.100000000)
assert y[-1][0] == pytest.approx(-121.75000000, abs = 1e-4)
assert y[-1][1] == pytest.approx(-49.100000000)

#Plot
if with_plots:
Expand Down
14 changes: 7 additions & 7 deletions examples/ida_with_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def f(t, y, yd, p):
#Sets the parameters
imp_sim.atol = np.array([1.0e-8, 1.0e-14, 1.0e-6])
imp_sim.algvar = [1.0,1.0,0.0]
imp_sim.suppress_alg = False #Suppres the algebraic variables on the error test
imp_sim.suppress_alg = False #Suppress the algebraic variables on the error test
imp_sim.report_continuously = True #Store data continuous during the simulation
imp_sim.pbar = p0
imp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test.
Expand All @@ -78,12 +78,12 @@ def f(t, y, yd, p):
t, y, yd = imp_sim.simulate(4,400) #Simulate 4 seconds with 400 communication points

#Basic test
assert y[-1][0] == pytest.approx(9.05518032e-01, rel = 1e-4)
assert y[-1][1] == pytest.approx(2.24046805e-05, rel = 1e-4)
assert y[-1][2] == pytest.approx(9.44595637e-02, rel = 1e-4)
assert imp_sim.p_sol[0][-1][0] == pytest.approx(-1.8761, rel = 1e-2) #Values taken from the example in Sundials
assert imp_sim.p_sol[1][-1][0] == pytest.approx(2.9614e-06, rel = 1e-8)
assert imp_sim.p_sol[2][-1][0] == pytest.approx(-4.9334e-10, rel = 1e-12)
assert y[-1][0] == pytest.approx(9.05518032e-01, abs = 1e-4)
assert y[-1][1] == pytest.approx(2.24046805e-05, abs = 1e-4)
assert y[-1][2] == pytest.approx(9.44595637e-02, abs = 1e-4)
assert imp_sim.p_sol[0][-1][0] == pytest.approx(-1.8761, abs = 1e-2) #Values taken from the example in Sundials
assert imp_sim.p_sol[1][-1][0] == pytest.approx(2.9614e-06, abs = 1e-8)
assert imp_sim.p_sol[2][-1][0] == pytest.approx(-4.9334e-10, abs = 1e-12)

#Plot
if with_plots:
Expand Down
12 changes: 6 additions & 6 deletions examples/ida_with_user_defined_handle_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def handle_result(solver, t ,y, yd):
#Sets the parameters
imp_sim.atol = 1e-6 #Default 1e-6
imp_sim.rtol = 1e-6 #Default 1e-6
imp_sim.suppress_alg = True #Suppres the algebraic variables on the error test
imp_sim.suppress_alg = True #Suppress the algebraic variables on the error test
imp_sim.report_continuously = True

#Let Sundials find consistent initial conditions by use of 'IDA_YA_YDP_INIT'
Expand All @@ -107,11 +107,11 @@ def handle_result(solver, t ,y, yd):
pl.show()

#Basic tests
assert_almost_equal(y[-1][0],0.9401995, places=4)
assert_almost_equal(y[-1][1],-0.34095124, places=4)
assert yd[-1][0] == pytest.approx(-0.88198927, rel = 1e-4)
assert yd[-1][1] == pytest.approx(-2.43227069, rel = 1e-4)
assert order[-1] == pytest.approx(5, rel = 1e-4)
assert y[-1][0] == pytest.approx(0.9401995, abs = 1e-4)
assert y[-1][1] == pytest.approx(-0.34095124, abs = 1e-4)
assert yd[-1][0] == pytest.approx(-0.88198927, abs = 1e-4)
assert yd[-1][1] == pytest.approx(-2.43227069, abs = 1e-4)
assert order[-1] == pytest.approx(5, abs = 1e-4)

return imp_mod, imp_sim

Expand Down
2 changes: 1 addition & 1 deletion examples/kinsol_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def res(y):
y = alg_solver.solve()

#Basic test
assert y == pytest.approx(1.0, rel = 1e-5)
assert y == pytest.approx(1.0, abs = 1e-5)

return alg_mod, alg_solver

Expand Down
2 changes: 1 addition & 1 deletion examples/kinsol_ors.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def setup_param(solver):

#Basic test
for j in range(len(y)):
assert y[j] == pytest.approx(1.0, rel = 1e-4)
assert y[j] == pytest.approx(1.0, abs = 1e-4)

return [alg_mod, alg_mod_prec], [alg_solver, alg_solver_prec]

Expand Down
4 changes: 2 additions & 2 deletions examples/kinsol_with_jac.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def jac(y):
y = alg_solver.solve()

#Basic test
assert y[0] == pytest.approx(1.5, rel = 1e-5)
assert y[1] == pytest.approx(1.0, rel = 1e-5)
assert y[0] == pytest.approx(1.5, abs = 1e-5)
assert y[1] == pytest.approx(1.0, abs = 1e-5)

return alg_mod, alg_solver

Expand Down
2 changes: 1 addition & 1 deletion examples/lsodar_vanderpol.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def f(t,y):

#Basic test
x1 = y[:,0]
assert_less(np.abs(x1[-1] - 1.706168035), 1e-3)
assert np.abs(x1[-1] - 1.706168035) < 1e-3

return exp_mod, exp_sim

Expand Down
6 changes: 3 additions & 3 deletions examples/lsodar_with_disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ def run_example(with_plots=True):
pl.show()

#Basic test
assert_almost_equal(y[-1][0],8.0)
assert_almost_equal(y[-1][1],3.0)
assert_almost_equal(y[-1][2],2.0)
assert y[-1][0] == pytest.approx(8.0)
assert y[-1][1] == pytest.approx(3.0)
assert y[-1][2] == pytest.approx(2.0)

return exp_mod, exp_sim

Expand Down
Loading

0 comments on commit 958c90f

Please sign in to comment.