-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathflow_functions.py
149 lines (119 loc) · 3.92 KB
/
flow_functions.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
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
147
148
149
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
class FlowProblem:
def __init__(
self,
T=1.0,
td=0.2,
amp=900.0,
dt=0.001,
ncycles=10,
ncomp = 10,
C = 38.,
R = 0.06,
L = 0.0017,
R_o = 0.025,
p_o = 10.,
) -> None :
'''
Inputs
-----
T (float): cycle length (default 1.0)
td (float): pulse duration, make sure to make this less than T (default 0.2)
amp (float): inflow amplitude (default 1.0)
dt (float): temporal discretisation resolution (default 0.001)
C (float): tube average compliance (default 38.)
R (float): tube average impedance (default 0.06)
L (float): hydraulic impedance, inertia (default 0.0017)
R_o (float) : outflow resistance
p_o (float) : outflow pressure
'''
assert td < T, f'td should be smaller than T but {td} >= {T}.'
self._td = td
self._T = T
self._dt = dt
self._amp = amp
self._ncomp = ncomp
self._ncycles= ncycles
self._C = C
self._R = R
self._L = L
self._R_o = R_o
self._p_o = p_o
self.res = None
@property
def td(self):
return self._td
@property
def T(self):
return self._T
@property
def dt(self):
return self._dt
@property
def amp(self):
return self._amp
@property
def ncomp(self):
return self._ncomp
@property
def ncycles(self):
return self._ncycles
@property
def C(self):
return self._C
@property
def L(self):
return self._L
@property
def R(self):
return self._R
@property
def R_o(self):
return self._R_o
@property
def p_o(self):
return self._p_o
def generate_pulse_function(self):
self.Q_mi_lambda = lambda t : np.sin(np.pi /self.td * t) ** 2.0 * np.heaviside(self.td - t, 0.0) * self.amp
def dfdt_fd(self, t:float, y:np.ndarray, Q_in):
Cn = self.C / self.ncomp
Rn = self.R / self.ncomp
Ln = self.L / self.ncomp
out = np.zeros((self.ncomp, 2))
y_temp = y.reshape((-1, 2))
for i in range(self.ncomp):
if i > 0:
out[i,0] = ( y_temp[i-1,1] - y_temp[i,1]) / Cn
else:
out[i,0] = (Q_in(t%self.T) - y_temp[i,1]) / Cn
if i < self.ncomp-1:
out[i,1] = (-y_temp[i+1, 0] + y_temp[i, 0] - Rn * y_temp[i, 1]) / Ln
pass
else:
out[i,1] = (-self.p_o + y_temp[i, 0] - (Rn + self.R_o) * y_temp[i, 1]) / Ln
return out.reshape((-1,))
def solve(self):
dfdt_fd_spec = lambda t, y : self.dfdt_fd(t=t, y=y, Q_in=self.Q_mi_lambda)
self.res = sp.integrate.solve_ivp(
dfdt_fd_spec,
[0.0, self.T * self.ncycles],
y0 = np.zeros(self.ncomp*2),
method='BDF',
max_step=self.dt
)
self.res.y = self.res.y[:,self.res.t >= self.T*(self.ncycles-1)]
self.res.t = self.res.t[self.res.t >= self.T*(self.ncycles-1)]
def plot_res(self):
fig, ax = plt.subplots(ncols=2, figsize = (10, 5))
for i in range(self.ncomp):
ax[0].plot(self.res.t, self.res.y[2*i,:] , 'r', alpha=0.1 + (1.-i/self.ncomp) *0.9)
ax[1].plot(self.res.t, self.res.y[2*i+1,:] , 'r', alpha=0.1 + (1.-i/self.ncomp) *0.9)
ax[0].set_title('Pressure')
ax[1].set_title('Flow rate')
ax[0].set_xlabel('Time (s)')
ax[1].set_xlabel('Time (s)')
ax[0].set_ylabel('mmHg')
ax[1].set_ylabel('$ml\cdot s^{-1}$')
return (fig, ax)