-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDyMMMODESolver.py
58 lines (44 loc) · 1.7 KB
/
DyMMMODESolver.py
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
from __future__ import print_function
import math
import numpy as np
from numpy import linalg as LA
from scipy.integrate import solve_ivp
from importlib import import_module
class DyMMMODESolver(object):
"""
ODE Solver.
Attributes
----------
Methods
-------
"""
community=None
def __init__(self, community):
#self.DyMMMCommunity = import_module('{}.DyMMMCommunity'.format(communityDir))
self.community=community
def run(self, tspan, solver, y_init=None):
t=None
y=None
if y_init is None:
y_init=self.community.initialConditions
if solver == 'BDF' or solver == 'Radau' or solver =='LSODA':
sol = solve_ivp(lambda t, y: self.community.solve(t,y)
,tspan, y_init, solver
#,events=[self.infeasible_event, self.steadystate_event]
,events=[self.infeasible_event]
,dense_output=True)
# if sol.status == 1:
# print('Success, termination event occured.')
# if sol.status == 0:
# print('Success, t end reached.')
t = sol.t
y = sol.y.transpose()
return t,y,sol.status
def infeasible_event(self, t, y):
#print("Event evaulated---------------------------------------------------------{}".format(str(y)))
return y[self.community.glucoseStateIndex] - self.infeasible_event.epsilon #position of Glucose
infeasible_event.epsilon = 1e-3
#infeasible_event.direction = 0
infeasible_event.terminal = True
if __name__ == '__main__':
solver = DyMMMODESolver('DyMMMCommunity')