Skip to content

Commit

Permalink
Import cleanups (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon authored Jan 25, 2024
1 parent c527587 commit 834517a
Show file tree
Hide file tree
Showing 80 changed files with 1,369 additions and 1,390 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
18 changes: 9 additions & 9 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 All @@ -57,13 +57,13 @@ def f(t,y):

#Plot
if with_plots:
import pylab as P
P.plot(t1, y1, color="b")
P.plot(t2, y2, color="r")
P.title(exp_mod.name)
P.ylabel('y')
P.xlabel('Time')
P.show()
import pylab as pl
pl.plot(t1, y1, color="b")
pl.plot(t2, y2, color="r")
pl.title(exp_mod.name)
pl.ylabel('y')
pl.xlabel('Time')
pl.show()

#Basic test
nose.tools.assert_almost_equal(y2[-1][0], 0.00347746, 5)
Expand Down
16 changes: 8 additions & 8 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 All @@ -54,12 +54,12 @@ def f(t,y):

#Plot
if with_plots:
import pylab as P
P.plot(t, y, color="b")
P.title(exp_mod.name)
P.ylabel('y')
P.xlabel('Time')
P.show()
import pylab as pl
pl.plot(t, y, color="b")
pl.title(exp_mod.name)
pl.ylabel('y')
pl.xlabel('Time')
pl.show()

#Basic test
nose.tools.assert_almost_equal(y[-1][0], 4.00000000, 3)
Expand Down
12 changes: 6 additions & 6 deletions examples/cvode_gyro.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def f(t, u):

# Plot
if with_plots:
import pylab as P
P.plot(t, y/10000.)
P.xlabel('Time')
P.ylabel('States, scaled by $10^4$')
P.title(exp_mod.name)
P.show()
import pylab as pl
pl.plot(t, y/10000.)
pl.xlabel('Time')
pl.ylabel('States, scaled by $10^4$')
pl.title(exp_mod.name)
pl.show()

#Basic tests
nose.tools.assert_almost_equal(y[-1][0], 692.800241862)
Expand Down
28 changes: 14 additions & 14 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 All @@ -76,20 +76,20 @@ def f(t,y):

#Plot
if with_plots:
import pylab as P
P.subplot(211)
P.plot(t,y[:,2])
P.ylabel("State: $y_1$")
P.subplot(212)
P.plot(t,exp_mod.order)
P.ylabel("Order")
P.suptitle(exp_mod.name)
P.xlabel("Time")
P.show()
import pylab as pl
pl.subplot(211)
pl.plot(t,y[:,2])
pl.ylabel("State: $y_1$")
pl.subplot(212)
pl.plot(t,exp_mod.order)
pl.ylabel("Order")
pl.suptitle(exp_mod.name)
pl.xlabel("Time")
pl.show()

#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
18 changes: 9 additions & 9 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 Expand Up @@ -148,12 +148,12 @@ def run_example(with_plots=True):

#Plot
if with_plots:
import pylab as P
P.plot(t,y)
P.title(exp_mod.name)
P.ylabel('States')
P.xlabel('Time')
P.show()
import pylab as pl
pl.plot(t,y)
pl.title(exp_mod.name)
pl.ylabel('States')
pl.xlabel('Time')
pl.show()

#Basic test
nose.tools.assert_almost_equal(y[-1][0],8.0)
Expand Down
56 changes: 28 additions & 28 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 All @@ -88,39 +88,39 @@ def f(t, y, p):

#Plot
if with_plots:
import pylab as P
import pylab as pl
title_text=r"Sensitivity w.r.t. ${}$"
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.title(title_text.format('p_1'))
P.legend((legend_text.format('y_1','p_1'),
pl.figure(1)
pl.subplot(221)
pl.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])
pl.title(title_text.format('p_1'))
pl.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.title(title_text.format('p_2'))
P.legend((legend_text.format('y_2','p_1'),
pl.subplot(222)
pl.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])
pl.title(title_text.format('p_2'))
pl.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.title(title_text.format('p_3'))
P.legend((legend_text.format('y_3','p_1'),
pl.subplot(223)
pl.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])
pl.title(title_text.format('p_3'))
pl.legend((legend_text.format('y_3','p_1'),
legend_text.format('y_3','p_2'),
legend_text.format('y_3','p_3')))
P.subplot(224)
P.title('ODE Solution')
P.plot(t, y)
P.suptitle(exp_mod.name)
P.show()
pl.subplot(224)
pl.title('ODE Solution')
pl.plot(t, y)
pl.suptitle(exp_mod.name)
pl.show()

#Basic test
nose.tools.assert_almost_equal(y[-1][0], 1577.6552477, 5)
Expand Down
19 changes: 9 additions & 10 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 All @@ -71,12 +70,12 @@ def jac(t,y):

#Plot
if with_plots:
import pylab as P
P.plot(t,y,linestyle="dashed",marker="o") #Plot the solution
P.xlabel('Time')
P.ylabel('State')
P.title(exp_mod.name)
P.show()
import pylab as pl
pl.plot(t,y,linestyle="dashed",marker="o") #Plot the solution
pl.xlabel('Time')
pl.ylabel('State')
pl.title(exp_mod.name)
pl.show()

#Basic tests
nose.tools.assert_almost_equal(y[-1][0],-121.75000000,4)
Expand Down
20 changes: 10 additions & 10 deletions examples/cvode_with_jac_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
# 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 scipy.sparse as SP
import numpy as np
import scipy.sparse as sps
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):
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 All @@ -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
Expand All @@ -85,12 +85,12 @@ def jac(t,y):

#Plot
if with_plots:
import pylab as P
P.plot(t,y[:,1],linestyle="dashed",marker="o") #Plot the solution
P.xlabel('Time')
P.ylabel('State')
P.title(exp_mod.name)
P.show()
import pylab as pl
pl.plot(t,y[:,1],linestyle="dashed",marker="o") #Plot the solution
pl.xlabel('Time')
pl.ylabel('State')
pl.title(exp_mod.name)
pl.show()

#Basic tests
nose.tools.assert_almost_equal(y[-1][0],0.9851,3)
Expand Down
Loading

0 comments on commit 834517a

Please sign in to comment.