-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathode2ivp.py
More file actions
104 lines (90 loc) · 3.73 KB
/
ode2ivp.py
File metadata and controls
104 lines (90 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
ODE2IVP - Base class for 2nd-order ordinary differential equation initial
value problems
This module provides the base functionality for all 2nd-order ordinary
differential equation initial value problem objects used in the nnode
software.
Example:
Create an empty ODE2IVP object.
ode2ivp = ODE2IVP()
Create an ODE2IVP object from a Python module.
ode2ivp = ODE2IVP(modname)
Attributes:
name - String containing name of equation definition module
Gf - Function for equation
ic - Scalar for initial condition at x=0 (y(x) = y(0))
ic1 - Scalar for initial derivative condition at x=0 (dy/dx(x) = dy/dx(0))
dG_dyf - Function for derivative of Gf wrt y
dg_dydxf - Function for derivative of Gf wrt dy/dx
dg_d2ydx2f - Function for derivative of Gf wrt d2y/dx2
yaf - (Optional) function for analytical solution y(x)
dya_dxf - (Optional) function for analytical derivative dy/dx
d2ya_dx2f - (Optional) function for analytical derivative d2y/dx2
Methods:
__init__
__str__
Todo:
* Expand base functionality.
"""
from importlib import import_module
from inspect import getsource
from ode2 import ODE2
class ODE2IVP(ODE2):
"""Base class for all 2nd-order ordinary differential equation initial-
value problem objects"""
def __init__(self, diffeqmod=None):
self.name = None
self.Gf = None
self.ic = None
self.ic1 = None
self.dG_dyf = None
self.dG_dydxf = None
self.dG_d2ydx2f = None
self.yaf = None
self.dya_dxf = None
self.d2ya_dx2f = None
if diffeqmod:
self.name = diffeqmod
odemod = import_module(diffeqmod)
assert odemod.Gf # Function for the ODE as a whole
assert odemod.ic is not None # y(0)
assert odemod.ic1 is not None # dy/dx (0)
assert odemod.dG_dyf # Function for deriv of G wrt y
assert odemod.dG_dydxf # Function for deriv of G wrt dy/dx
assert odemod.dG_d2ydx2f # Function for deriv of G wrt d2y/dx2
# yaf() is the optional function for analytical solution ya
# dya_dxf is the optional function for analytical derivative dya/dx
# d2ya_dx2f is the optional function for analytical derivative
# d2ya/dx2
self.Gf = odemod.Gf
self.ic = odemod.ic
self.ic1 = odemod.ic1
self.dG_dyf = odemod.dG_dyf
self.dG_dydxf = odemod.dG_dydxf
self.dG_d2ydx2f = odemod.dG_d2ydx2f
if odemod.yaf:
self.yaf = odemod.yaf
if odemod.dya_dxf:
self.dya_dxf = odemod.dya_dxf
if odemod.d2ya_dx2f:
self.d2ya_dx2f = odemod.d2ya_dx2f
def __str__(self):
s = ''
s += 'ODE2BVP:\n'
s += "name = %s\n" % self.name
s += "Gf = %s\n" % (getsource(self.Gf).rstrip() if self.Gf else None)
s += "dG_dyf = %s\n" % (getsource(self.dG_dyf).rstrip()
if self.dG_dyf else None)
s += "dG_dydxf = %s\n" % (getsource(self.dG_dydxf).rstrip()
if self.dG_dydxf else None)
s += "ic = %s\n" % (self.ic if self.ic is not None else None)
s += "ic1 = %s\n" % (getsource(self.yaf).rstrip()
if self.yaf else None)
s += "dya_dxf = %s\n" % (getsource(self.dya_dxf).rstrip()
if self.dya_dxf else None)
s += "d2ya_dx2f = %s\n" % (getsource(self.d2ya_dx2f).rstrip()
if self.d2ya_dx2f else None)
return s.rstrip() # Strip trailing newline if any.
if __name__ == '__main__':
ode2ivp = ODE2IVP()
print(ode2ivp)