From e8f96249eb3319f7372146297c8410b9a1d4a8a8 Mon Sep 17 00:00:00 2001
From: PeterMeisrimelModelon
<92585725+PeterMeisrimelModelon@users.noreply.github.com>
Date: Wed, 24 Jan 2024 11:19:43 +0100
Subject: [PATCH] Unifying scipy imports
---
examples/cvode_with_jac_sparse.py | 4 +--
examples/kinsol_ors.py | 33 +++++++++++------------
examples/mech_system_pendulum.py | 7 +++--
examples/radau5ode_with_disc_sparse.py | 4 +--
examples/radau5ode_with_jac_sparse.py | 6 ++---
src/lib/sundials_callbacks_ida_cvode.pxi | 13 ++++-----
src/solvers/euler.pyx | 4 +--
src/solvers/odepack.py | 27 +++++++++----------
src/solvers/radau5.py | 26 +++++++++---------
src/solvers/sundials.pyx | 2 --
tests/solvers/test_euler.py | 4 +--
tests/solvers/test_odepack.py | 4 +--
tests/solvers/test_radau5.py | 34 ++++++++++++------------
tests/solvers/test_rosenbrock.py | 4 +--
tests/solvers/test_sundials.py | 4 +--
15 files changed, 85 insertions(+), 91 deletions(-)
diff --git a/examples/cvode_with_jac_sparse.py b/examples/cvode_with_jac_sparse.py
index d7da5cc3..bdc745c9 100644
--- a/examples/cvode_with_jac_sparse.py
+++ b/examples/cvode_with_jac_sparse.py
@@ -16,7 +16,7 @@
# along with this program. If not, see .
import numpy as np
-import scipy.sparse as SP
+import scipy.sparse as sps
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
@@ -59,7 +59,7 @@ def jac(t,y):
rowvals = [0, 1, 0, 1, 2, 0, 1]
data = [-0.04, 0.04, 1e4*y[2], -1e4*y[2]-6e7*y[1], 6e7*y[1], 1e4*y[1], -1e4*y[1]]
- J = SP.csc_matrix((data, rowvals, colptrs))
+ J = sps.csc_matrix((data, rowvals, colptrs))
return J
#Defines an Assimulo explicit problem
diff --git a/examples/kinsol_ors.py b/examples/kinsol_ors.py
index 046cbb5c..f904ad42 100644
--- a/examples/kinsol_ors.py
+++ b/examples/kinsol_ors.py
@@ -15,19 +15,16 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see .
-import numpy as np
-import scipy.linalg as LIN
-import scipy.io as IO
-import scipy.sparse as SPARSE
-import scipy.sparse.linalg as LINSP
-import nose
import os
+import nose
+import numpy as np
+import scipy as sp
+import scipy.sparse as sps
from assimulo.solvers import KINSOL
from assimulo.problem import Algebraic_Problem
import warnings
-import scipy.sparse
-warnings.simplefilter("ignore", scipy.sparse.SparseEfficiencyWarning)
+warnings.simplefilter("ignore", sps.SparseEfficiencyWarning)
file_path = os.path.dirname(os.path.realpath(__file__))
@@ -39,22 +36,22 @@ def run_example(with_plots=True):
Iterative Methods for Sparse Linear Systems.
"""
#Read the original matrix
- A_original = IO.mmread(os.path.join(file_path,"kinsol_ors_matrix.mtx"))
+ A_original = sp.io.mmread(os.path.join(file_path,"kinsol_ors_matrix.mtx"))
#Scale the original matrix
- A = SPARSE.spdiags(1.0/A_original.diagonal(), 0, len(A_original.diagonal()), len(A_original.diagonal())) * A_original
+ A = sps.spdiags(1.0/A_original.diagonal(), 0, len(A_original.diagonal()), len(A_original.diagonal())) * A_original
#Preconditioning by Symmetric Gauss Seidel
if True:
- D = SPARSE.spdiags(A.diagonal(), 0, len(A_original.diagonal()), len(A_original.diagonal()))
- Dinv = SPARSE.spdiags(1.0/A.diagonal(), 0, len(A_original.diagonal()), len(A_original.diagonal()))
- E = -SPARSE.tril(A,k=-1)
- F = -SPARSE.triu(A,k=1)
+ D = sps.spdiags(A.diagonal(), 0, len(A_original.diagonal()), len(A_original.diagonal()))
+ Dinv = sps.spdiags(1.0/A.diagonal(), 0, len(A_original.diagonal()), len(A_original.diagonal()))
+ E = -sps.tril(A,k=-1)
+ F = -sps.triu(A,k=1)
L = (D-E).dot(Dinv)
U = D-F
Prec = L.dot(U)
- solvePrec = LINSP.factorized(Prec)
+ solvePrec = sps.linalg.factorized(Prec)
#Create the RHS
b = A.dot(np.ones(A.shape[0]))
@@ -91,7 +88,7 @@ def prec_solve(r):
def setup_param(solver):
solver.linear_solver = "spgmr"
solver.max_dim_krylov_subspace = 10
- solver.ftol = LIN.norm(res(solver.y0))*1e-9
+ solver.ftol = np.linalg.norm(res(solver.y0))*1e-9
solver.max_iter = 300
solver.verbosity = 10
solver.globalization_strategy = "none"
@@ -105,8 +102,8 @@ def setup_param(solver):
#Solve Preconditioned system
y_prec = alg_solver_prec.solve()
- print("Error , in y: ", LIN.norm(y-np.ones(len(y))))
- print("Error (preconditioned), in y: ", LIN.norm(y_prec-np.ones(len(y_prec))))
+ print("Error , in y: ", np.linalg.norm(y-np.ones(len(y))))
+ print("Error (preconditioned), in y: ", np.linalg.norm(y_prec-np.ones(len(y_prec))))
if with_plots:
import pylab as P
diff --git a/examples/mech_system_pendulum.py b/examples/mech_system_pendulum.py
index 6f13bdcc..852b2e11 100644
--- a/examples/mech_system_pendulum.py
+++ b/examples/mech_system_pendulum.py
@@ -15,9 +15,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see .
-import numpy as np
import nose
-import scipy.linalg as sl
+import numpy as np
from assimulo.special_systems import Mechanical_System
from assimulo.solvers import IDA, ODASSL
@@ -58,10 +57,10 @@ def run_example(index="ind1", with_plots=True, with_test=False):
final_residual=my_pend.res(0.,dae_pend.y,dae_pend.yd)
print(my_pend.name+" Residuals after the integration run\n")
- print(final_residual, 'Norm: ', sl.norm(final_residual))
+ print(final_residual, 'Norm: ', np.linalg.norm(final_residual))
if with_test:
- nose.tools.assert_less(sl.norm(final_residual), 1.5e-1)
+ nose.tools.assert_less(np.linalg.norm(final_residual), 1.5e-1)
if with_plots:
dae_pend.plot(mask=[1,1]+(len(my_pend.y0)-2)*[0])
return my_pend, dae_pend
diff --git a/examples/radau5ode_with_disc_sparse.py b/examples/radau5ode_with_disc_sparse.py
index bed2de1c..2906f318 100644
--- a/examples/radau5ode_with_disc_sparse.py
+++ b/examples/radau5ode_with_disc_sparse.py
@@ -16,7 +16,7 @@
# along with this program. If not, see .
import numpy as np
-import scipy.sparse as sp
+import scipy.sparse as sps
import nose
from assimulo.solvers import Radau5ODE
from assimulo.problem import Explicit_Problem
@@ -54,7 +54,7 @@ def rhs(self,t,y,sw):
return np.array([yd_0,yd_1,yd_2])
def jac(self, t, y):
- return sp.csc_matrix((len(y), len(y)), dtype = 'float')
+ return sps.csc_matrix((len(y), len(y)), dtype = 'float')
jac_nnz = 0
diff --git a/examples/radau5ode_with_jac_sparse.py b/examples/radau5ode_with_jac_sparse.py
index 4264300d..54abe28b 100644
--- a/examples/radau5ode_with_jac_sparse.py
+++ b/examples/radau5ode_with_jac_sparse.py
@@ -15,9 +15,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see .
-import numpy as np
-import scipy.sparse as SP
import nose
+import numpy as np
+import scipy.sparse as sps
from assimulo.solvers import Radau5ODE
from assimulo.problem import Explicit_Problem
@@ -57,7 +57,7 @@ def jac(t,y):
rowvals = [0, 1, 0, 1, 2, 0, 1]
data = [-0.04, 0.04, 1e4*y[2], -1e4*y[2]-6e7*y[1], 6e7*y[1], 1e4*y[1], -1e4*y[1]]
- J = SP.csc_matrix((data, rowvals, colptrs))
+ J = sps.csc_matrix((data, rowvals, colptrs))
return J
#Defines an Assimulo explicit problem
diff --git a/src/lib/sundials_callbacks_ida_cvode.pxi b/src/lib/sundials_callbacks_ida_cvode.pxi
index d200c589..5f84ea4e 100644
--- a/src/lib/sundials_callbacks_ida_cvode.pxi
+++ b/src/lib/sundials_callbacks_ida_cvode.pxi
@@ -17,6 +17,7 @@
import cython
import traceback
+import scipy.sparse as sps
from assimulo.exception import AssimuloRecoverableError
cdef int cv_rhs(realtype t, N_Vector yv, N_Vector yvdot, void* problem_data) noexcept:
@@ -123,8 +124,8 @@ IF SUNDIALS_VERSION >= (3,0,0):
else:
jac=(