-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrator.py
More file actions
146 lines (115 loc) · 5.28 KB
/
integrator.py
File metadata and controls
146 lines (115 loc) · 5.28 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import numpy as np
from scipy.integrate import solve_ivp
####### Functions to integrate various ODE Types:
# ODE_function: General purpose integrator of dn/dt = k
# ODE_function_onefeat: General purpose integrator of dn/dt = k
# but with different regression function
# ODE_function_test: Integrates a test 4-species ODE
# ODE_function_test_simple: Integrates a test 2-species ODE
# ODE_function_test_simple_2: implements a test 3-species ODE
def ODE_function(t, state, regressor_class):
# the function dy/dt = f(t,y) = S_n
# first find source terms via regressor
source_terms = regressor_class.run_regression(state.reshape(1,-1))
# then return next state
dndt = source_terms
return dndt
def ODE_function_onefeat(t, state, regressor_class):
# the function dy/dt = f(t,y) = S_n
# first find source terms via regressor
source_terms = regressor_class.run_regression_onefeat(state.reshape(1,-1))
# then return next state
dndt = source_terms
return dndt
def ODE_function_test(t,state, k12, k13, k23, k24, k34 ):
# test ode fucntion, system of 4 species
dn1dt = -k12*state[0] + k13*state[2]
dn2dt = k12*state[0] - k23*state[1] - k24*state[1]
dn3dt = k23*state[1] - k13*state[2] - k34*state[2]
dn4dt = k24*state[1] + k34*state[2]
return np.array([dn1dt, dn2dt, dn3dt, dn4dt])
def ODE_function_test_simple(t,state,):
# test ode fucntion, system of 2 species
dn1dt = -0.5*state[0]
dn2dt = 0.5*state[0]
return np.array([dn1dt, dn2dt])
def ODE_function_test_simple_2(t,state,):
# test ode fucntion, system of 3 species
dn1dt = -0.5*state[0]
dn2dt = 0.5*state[0] - 0.5*state[1]
dn3dt = 0.5*state[1]
return np.array([dn1dt, dn2dt, dn3dt])
##########################
##########################
##########################
####### Class holding integration functions as well as relevant information
# Each integration routine calls the function which matches its name, as
# listed above
class integrator:
def __init__(self,final_time, principal_axes, principal_components, \
density, regressor_class):
# variable initialization
self.t_span = (0.0, final_time)
self.t = np.arange(0.0,final_time, final_time/100)
self.regressor_class = regressor_class
self.A = principal_axes
self.Z = principal_components
self.density = density
def integrate_ODEs(self,y0):
# this integrates the ODEs described in ODE_function
# use initial values from PC matrix, or from real densities
# real densities requires multiplying the row vector by the
# principal axes matrix, like so
# y0_components = y0_real.dot(self.A)
p = (self.regressor_class,)
result = solve_ivp(ODE_function, self.t_span, y0, args=p, \
method='LSODA',t_eval=self.t)
return result
def integrate_ODEs_onefeat(self,y0):
# this integrates the ODEs described in ODE_function
# use initial values from PC matrix, or from real densities
# real densities requires multiplying the row vector by the
# principal axes matrix, like so
# y0_components = y0_real.dot(self.A)
#y0 = self.Z[0,:]
p = (self.regressor_class,)
result = solve_ivp(ODE_function_onefeat, self.t_span, y0, args=p, \
method='LSODA',t_eval=self.t)
return result
def integrate_ODEs_test(self, initial_state):
# TEST ODE INTEGRATION FUNCTION
# use initial values from PC matrix, or from real densities
# real densities requires multiplying the row vector by the
# principal axes matrix, like so
# y0_components = y0_real.dot(self.A)
self.t_span = (0.0, 10.0)
self.t = np.arange(0.0,10.0,10.0/50)
y0 = initial_state
p = (0.5,1.5,1,0.2,0.2)
result = solve_ivp(ODE_function_test, self.t_span, y0, args=p, \
method='LSODA',t_eval=self.t)
return result
def integrate_ODEs_test_simple(self, initial_state):
# TEST ODE INTEGRATION FUNCTION
# use initial values from PC matrix, or from real densities
# real densities requires multiplying the row vector by the
# principal axes matrix, like so
# y0_components = y0_real.dot(self.A)
self.t_span = (0.0, 10.0)
self.t = np.arange(0.0,10.0,10.0/50)
y0 = initial_state
result = solve_ivp(ODE_function_test_simple, self.t_span, y0, \
method='LSODA',t_eval=self.t)
return result
def integrate_ODEs_test_simple_2(self, initial_state):
# TEST ODE INTEGRATION FUNCTION
# use initial values from PC matrix, or from real densities
# real densities requires multiplying the row vector by the
# principal axes matrix, like so
# y0_components = y0_real.dot(self.A)
self.t_span = (0.0, 10.0)
self.t = np.arange(0.0,10.0,10.0/50)
y0 = initial_state
result = solve_ivp(ODE_function_test_simple_2, self.t_span, y0, \
method='LSODA',t_eval=self.t)
return result