Skip to content

Commit

Permalink
Unifying numpy imports
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon committed Jan 24, 2024
1 parent 31c5d5e commit 3f8e9ea
Show file tree
Hide file tree
Showing 78 changed files with 960 additions and 975 deletions.
2 changes: 1 addition & 1 deletion doc/sphinx/source/tutorialCVodeDisc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Tutorial example showing how to use the explicit solver CVode for a discontinious problem.
Tutorial example showing how to use the explicit solver CVode for a discontinuous problem.
To run the example simply type,
run tutorialCVodeDisc.py (in IPython)
Expand Down
4 changes: 2 additions & 2 deletions examples/cvode_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
Expand All @@ -37,7 +37,7 @@ def run_example(with_plots=True):
#Define the rhs
def f(t,y):
ydot = -y[0]
return N.array([ydot])
return np.array([ydot])

#Define an Assimulo problem
exp_mod = Explicit_Problem(f, y0=4, name = r'CVode Test Example: $\dot y = - y$')
Expand Down
4 changes: 2 additions & 2 deletions examples/cvode_basic_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
Expand All @@ -34,7 +34,7 @@ def run_example(with_plots=True):
#Define the rhs
def f(t,y):
ydot = -y[0]
return N.array([ydot])
return np.array([ydot])

#Define an Assimulo problem
exp_mod = Explicit_Problem(f,t0=5, y0=0.02695, name = r'CVode Test Example (reverse time): $\dot y = - y$ ')
Expand Down
8 changes: 4 additions & 4 deletions examples/cvode_stability.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
Expand Down Expand Up @@ -54,9 +54,9 @@ def handle_result(self, solver, t, y):
def f(t,y):
yd_0 = y[1]
yd_1 = my*((1.-y[0]**2)*y[1]-y[0])
yd_2 = N.sin(t*y[1])
yd_2 = np.sin(t*y[1])

return N.array([yd_0,yd_1, yd_2])
return np.array([yd_0,yd_1, yd_2])

y0 = [2.0,-0.6, 0.1] #Initial conditions

Expand Down Expand Up @@ -89,7 +89,7 @@ def f(t,y):

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

return exp_mod, exp_sim

Expand Down
6 changes: 3 additions & 3 deletions examples/cvode_with_disc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
Expand Down Expand Up @@ -50,7 +50,7 @@ def rhs(self,t,y,sw):
yd_1 = 0.0
yd_2 = 0.0

return N.array([yd_0,yd_1,yd_2])
return np.array([yd_0,yd_1,yd_2])

#Sets a name to our function
name = 'ODE with discontinuities and a function with consistency problem'
Expand All @@ -65,7 +65,7 @@ def state_events(self,t,y,sw):
event_1 = -y[2] + 1.0
event_2 = -t + 1.0

return N.array([event_0,event_1,event_2])
return np.array([event_0,event_1,event_2])


#Responsible for handling the events.
Expand Down
24 changes: 12 additions & 12 deletions examples/cvode_with_initial_sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
Expand Down Expand Up @@ -57,12 +57,12 @@ def f(t, y, p):
yd_1 = k21*y1-(k02+k12)*y2
yd_2 = k31*y1-k13*y3

return N.array([yd_0,yd_1,yd_2])
return np.array([yd_0,yd_1,yd_2])

#The initial conditions
y0 = [0.0,0.0,0.0] #Initial conditions for y
p0 = [0.0, 0.0, 0.0] #Initial conditions for parameters
yS0 = N.array([[1,0,0],[0,1,0],[0,0,1.]])
yS0 = np.array([[1,0,0],[0,1,0],[0,0,1.]])

#Create an Assimulo explicit problem
exp_mod = Explicit_Problem(f, y0, p0=p0,name='Example: Computing Sensitivities')
Expand Down Expand Up @@ -93,25 +93,25 @@ def f(t, y, p):
legend_text=r"$\mathrm{{d}}{}/\mathrm{{d}}{}$"
P.figure(1)
P.subplot(221)
P.plot(t, N.array(exp_sim.p_sol[0])[:,0],
t, N.array(exp_sim.p_sol[0])[:,1],
t, N.array(exp_sim.p_sol[0])[:,2])
P.plot(t, np.array(exp_sim.p_sol[0])[:,0],
t, np.array(exp_sim.p_sol[0])[:,1],
t, np.array(exp_sim.p_sol[0])[:,2])
P.title(title_text.format('p_1'))
P.legend((legend_text.format('y_1','p_1'),
legend_text.format('y_1','p_2'),
legend_text.format('y_1','p_3')))
P.subplot(222)
P.plot(t, N.array(exp_sim.p_sol[1])[:,0],
t, N.array(exp_sim.p_sol[1])[:,1],
t, N.array(exp_sim.p_sol[1])[:,2])
P.plot(t, np.array(exp_sim.p_sol[1])[:,0],
t, np.array(exp_sim.p_sol[1])[:,1],
t, np.array(exp_sim.p_sol[1])[:,2])
P.title(title_text.format('p_2'))
P.legend((legend_text.format('y_2','p_1'),
legend_text.format('y_2','p_2'),
legend_text.format('y_2','p_3')))
P.subplot(223)
P.plot(t, N.array(exp_sim.p_sol[2])[:,0],
t, N.array(exp_sim.p_sol[2])[:,1],
t, N.array(exp_sim.p_sol[2])[:,2])
P.plot(t, np.array(exp_sim.p_sol[2])[:,0],
t, np.array(exp_sim.p_sol[2])[:,1],
t, np.array(exp_sim.p_sol[2])[:,2])
P.title(title_text.format('p_3'))
P.legend((legend_text.format('y_3','p_1'),
legend_text.format('y_3','p_2'),
Expand Down
7 changes: 3 additions & 4 deletions examples/cvode_with_jac.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem


def run_example(with_plots=True):
r"""
Example for demonstrating the use of a user supplied Jacobian
Expand All @@ -45,11 +44,11 @@ def run_example(with_plots=True):
def f(t,y):
yd_0 = y[1]
yd_1 = -9.82
return N.array([yd_0,yd_1])
return np.array([yd_0,yd_1])

#Defines the Jacobian
def jac(t,y):
j = N.array([[0,1.],[0,0]])
j = np.array([[0,1.],[0,0]])
return j

#Defines an Assimulo explicit problem
Expand Down
4 changes: 2 additions & 2 deletions examples/cvode_with_jac_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import scipy.sparse as SP
import nose
from assimulo.solvers import CVode
Expand Down Expand Up @@ -50,7 +50,7 @@ def f(t,y):
yd_0 = -0.04*y[0] + 1e4*y[1]*y[2]
yd_2 = 3e7*y[1]*y[1]
yd_1 = -yd_0 - yd_2
return N.array([yd_0,yd_1,yd_2])
return np.array([yd_0,yd_1,yd_2])

#Defines the Jacobian
def jac(t,y):
Expand Down
8 changes: 4 additions & 4 deletions examples/cvode_with_jac_spgmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
Expand Down Expand Up @@ -48,12 +48,12 @@ def f(t,y):
yd_0 = y[1]
yd_1 = -9.82

return N.array([yd_0,yd_1])
return np.array([yd_0,yd_1])

#Defines the Jacobian*vector product
def jacv(t,y,fy,v):
j = N.array([[0,1.],[0,0]])
return N.dot(j,v)
j = np.array([[0,1.],[0,0]])
return np.dot(j,v)

y0 = [1.0,0.0] #Initial conditions

Expand Down
8 changes: 4 additions & 4 deletions examples/cvode_with_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
Expand Down Expand Up @@ -50,7 +50,7 @@ def f(t, y, p):
yd_1 = p[0]*y[0]-p[1]*y[1]*y[2]-p[2]*y[1]**2
yd_2 = p[2]*y[1]**2

return N.array([yd_0,yd_1,yd_2])
return np.array([yd_0,yd_1,yd_2])

#The initial conditions
y0 = [1.0,0.0,0.0] #Initial conditions for y
Expand All @@ -65,11 +65,11 @@ def f(t, y, p):
#Create an Assimulo explicit solver (CVode)
exp_sim = CVode(exp_mod)

#Sets the solver paramters
#Sets the solver parameters
exp_sim.iter = 'Newton'
exp_sim.discr = 'BDF'
exp_sim.rtol = 1.e-4
exp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6])
exp_sim.atol = np.array([1.0e-8, 1.0e-14, 1.0e-6])
exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitivity method used
exp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test.
exp_sim.report_continuously = True
Expand Down
14 changes: 7 additions & 7 deletions examples/cvode_with_parameters_fcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
Expand Down Expand Up @@ -50,19 +50,19 @@ def f(t, y, p):
yd_1 = p[0]*y[0]-p[1]*y[1]*y[2]-p[2]*y[1]**2
yd_2 = p[2]*y[1]**2

return N.array([yd_0,yd_1,yd_2])
return np.array([yd_0,yd_1,yd_2])

def jac(t,y, p):
J = N.array([[-p[0], p[1]*y[2], p[1]*y[1]],
J = np.array([[-p[0], p[1]*y[2], p[1]*y[1]],
[p[0], -p[1]*y[2]-2*p[2]*y[1], -p[1]*y[1]],
[0.0, 2*p[2]*y[1],0.0]])
return J

def fsens(t, y, s, p):
J = N.array([[-p[0], p[1]*y[2], p[1]*y[1]],
J = np.array([[-p[0], p[1]*y[2], p[1]*y[1]],
[p[0], -p[1]*y[2]-2*p[2]*y[1], -p[1]*y[1]],
[0.0, 2*p[2]*y[1],0.0]])
P = N.array([[-y[0],y[1]*y[2],0],
P = np.array([[-y[0],y[1]*y[2],0],
[y[0], -y[1]*y[2], -y[1]**2],
[0,0,y[1]**2]])
return J.dot(s)+P
Expand All @@ -82,11 +82,11 @@ def fsens(t, y, s, p):
#Create an Assimulo explicit solver (CVode)
exp_sim = CVode(exp_mod)

#Sets the solver paramters
#Sets the solver parameters
exp_sim.iter = 'Newton'
exp_sim.discr = 'BDF'
exp_sim.rtol = 1.e-4
exp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6])
exp_sim.atol = np.array([1.0e-8, 1.0e-14, 1.0e-6])
exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitivity method used
exp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test.
exp_sim.report_continuously = True
Expand Down
8 changes: 4 additions & 4 deletions examples/cvode_with_parameters_modified.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import CVode
from assimulo.problem import Explicit_Problem
Expand Down Expand Up @@ -48,7 +48,7 @@ def f(t, y, p):
yd_1 = p[0]*y[0]-p[1]*y[1]*y[2]-p3*y[1]**2
yd_2 = p3*y[1]**2

return N.array([yd_0,yd_1,yd_2])
return np.array([yd_0,yd_1,yd_2])

#The initial conditions
y0 = [1.0,0.0,0.0] #Initial conditions for y
Expand All @@ -63,11 +63,11 @@ def f(t, y, p):
#Create an Assimulo explicit solver (CVode)
exp_sim = CVode(exp_mod)

#Sets the paramters
#Sets the parameters
exp_sim.iter = 'Newton'
exp_sim.discr = 'BDF'
exp_sim.rtol = 1.e-4
exp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6])
exp_sim.atol = np.array([1.0e-8, 1.0e-14, 1.0e-6])
exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitivity method used
exp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test.
exp_sim.report_continuously = True
Expand Down
8 changes: 4 additions & 4 deletions examples/dasp3_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose

try:
Expand Down Expand Up @@ -56,22 +56,22 @@ def run_example(with_plots=True):
#Define the slow rhs
def dydt(t,y,z):
eps=(1./3)*1.e-3
yp = N.array([-(.6*z[0]+.8*y[2])*y[0]+10.*y[1],
yp = np.array([-(.6*z[0]+.8*y[2])*y[0]+10.*y[1],
-10.*y[1]+ 1.6*z[0] *y[2],
-1.33*eps**2*y[2]*(y[0]+2.*z[0])])
return yp

#Define the fast rhs
def dzdt(t,y,z):
eps=(1./3)*1.e-3
zp = N.array([1.6*z[0]*y[2]-.6*z[0]*y[0]
zp = np.array([1.6*z[0]*y[2]-.6*z[0]*y[0]
-45.*(eps*z[0])**2+.8*y[2]*y[0]])
return zp

#The initial values
y0 = [ 3.0, 0.216, 1.0]
z0 = [1.35]
eps = N.array([.33333333e-3])
eps = np.array([.33333333e-3])

#Define an Assimulo problem
exp_mod = SingPerturbed_Problem(dydt, dzdt,
Expand Down
4 changes: 2 additions & 2 deletions examples/dopri5_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import numpy as N
import numpy as np
import nose
from assimulo.solvers import Dopri5
from assimulo.problem import Explicit_Problem
Expand All @@ -35,7 +35,7 @@ def run_example(with_plots=True):
#Defines the rhs
def f(t,y):
ydot = -y[0]
return N.array([ydot])
return np.array([ydot])

#Define an Assimulo problem
exp_mod = Explicit_Problem(f, 4.0,
Expand Down
Loading

0 comments on commit 3f8e9ea

Please sign in to comment.