-
Notifications
You must be signed in to change notification settings - Fork 9
/
dasslc_example.py
312 lines (246 loc) · 8.46 KB
/
dasslc_example.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#########################################################################
# #
# This is a template for the dasslc module in Python. #
# Use it and modify as you wish. #
# #
# Author: Ataide Neto #
# email:ataide@peq.coppe.ufrj.br #
# Universidade Federal do Rio de Janeiro #
# Version: 0.1-6 #
# #
#########################################################################
#t,y,yp = dasslc.solve(resfun,tspan,y0,yp0,rpar,rtol,atol,idx,ipfile,jac,display)
## Import the modules
import dasslc, time
import numpy as np
import matplotlib.pyplot as plt
SPARSE = False # Change it to True if sparse was compiled
### Defining the residual functions
def model0(t,y,yp): #--------- Minimum of 3 input arguments
res = np.empty(1) #------- Always allocate res as a numpy array,
# even if it has len = 1.
res[0] = yp[0] + 2*y[0] #- Declare the residual
ires = 0 #---------------- Set ires = 0 if everything is ok
return res, ires #-------- Beware: ires must always be returned
# as second output.
def model1(t,y,yp): #--------------- Just another example
res = np.empty(2)
res[0] = yp[0]-20*np.cos(20*t)
res[1] = yp[1]+20*np.sin(20*t)
return res, 0 #----------------- ires can be a literal
def model2(t,y,yp,par): #------------- Maximum of 4 input arguments
res = np.empty(3)
k1 = par[0] #|||||||||||||||||||||||||||||
k2 = par[1] #| |
CT0 = par[2] #| aliasing is optional, |
Ca = y[0]; dCa = yp[0] #--------#| but always encoraged |
Cb = y[1]; dCb = yp[1] #| |
Cc = y[2]; dCc = yp[2] #|||||||||||||||||||||||||||||
res[0] = -k1*Ca - dCa
res[1] = k1*Ca - k2*Cb - dCb
res[2] = Ca + Cb + Cc - CT0
ires = 0
return res, ires
def model3(t,y,yp,par): #------- The parameter may be a whole class
res = np.empty(5)
res[0] = yp[0] - y[2]
res[1] = yp[1] - y[3]
res[2] = yp[2] + y[4]*y[0]
res[3] = yp[3] + y[4]*y[1] + par.g
if (par.dae == 3):
res[4] = y[0]*y[0] + y[1]*y[1] - par.L*par.L
ires = 0
elif (par.dae == 2):
res[4] = y[0]*y[2] + y[1]*y[3]
ires = 0
elif (par.dae == 1):
res[4] = y[2]**2 + y[3]**2 - par.g*y[1] - par.L**2*y[4]
ires = 0
elif (par.dae == 0):
res[4] = yp[4] + 3*y[3]*par.g/par.L**2
ires = 0
else:
print("Invalid index.")
ires = -1
return res, ires
### Solve model0
t0 = np.array([1, 2]) #-- Integration interval with
# initial and final time
y0 = np.array([1]) #----- Initial condition
# The simplest call to dasslc, with all the mandatory inputs
# and outputs. y and yp are equally spaced in all time span
t, y, yp = dasslc.solve(model0,t0,y0)
# Plot results
plt.figure(1)
plt.subplot(211)
plt.plot(t,y)
plt.ylabel('y')
plt.title('Model0 Solution')
plt.subplot(212)
plt.plot(t,yp)
plt.xlabel('time')
plt.ylabel('yp')
### Solve model1
# The time span can also be a vector.
# In this case y and yp are returned at all values of t
t0 = np.linspace(0,1,100)
y0 = np.array([0,1]) #--- Initial condition
yp0 = np.array([1,0]) #-- Derivatives at initial condition (optional)
#-- Call with the optional yp0
t, y, yp = dasslc.solve(model1,t0,y0,yp0)
# Plot results
plt.figure(2)
plt.plot(t,y)
plt.ylabel('y')
plt.xlabel('time')
plt.title('Model1 Solution')
plt.legend(["y1","y2"])
### Solve model2
# You can also specify only the final time.
# In this case y and yp are equally spaced in [0 t0]
t0 = np.array([500])
y0 = np.array([1,0,0])
# If you are not passing an optional input,
# but is passing the next one, define it as None.
# Only positional arguments are supported.
yp0 = None
par = np.array([0.01,0.02,1]) #-- The optional parameter vector
atol = 1e-8 #-------------------- The absolute tolerance
rtol = 1e-6 #-------------------- The relative tolerance
# Call with optional arguments (yp0 = None)
t, y, yp = dasslc.solve(model2,t0,y0,yp0,par,rtol,atol)
# Plot results
plt.figure(3)
plt.plot(t,y)
plt.ylabel('y')
plt.xlabel('time')
plt.title('Model2 Solution')
plt.legend(["Ca","Cb","Cc"])
### Solve model2 (steady state)
# If you specify the tspan as None,
t0 = None
# Here, y0 is the initial guess for the steady state
y0 = np.array([0,0,0])
yp0 = np.zeros(3) #--- At steady state, yp = 0
# Call with t0 = None for the steady state
t, y, yp = dasslc.solve(model2,t0,y0,yp0,par,rtol,atol)
# Plot results
plt.figure(4)
plt.plot(0,y[0],'o')
plt.plot(1,y[1],'o')
plt.plot(2,y[2],'o')
plt.ylabel('Concentration')
plt.xlabel('Component')
plt.title('Model2 Solution (steady state)')
plt.legend(["Ca","Cb","Cc"])
#### Solve model3
#Defining the parameter class for
class pend_par:
g = 9.81
L = 1.0
dae = 3
t0 = np.linspace(0,50,10000)
y0 = np.array([1,0,0,0,0])
yp0 = None
# The optional parameter class initialization
par = pend_par()
atol = 1e-10
rtol = 1e-8
# The dependent variable index vector (needed for high index DAE)
index = np.array([1,1,2,2,3])
t, y, yp = dasslc.solve(model3,t0,y0,yp0,par,rtol,atol,index)
# Plot results
plt.figure(5)
plt.plot(t,y)
plt.ylabel('y')
plt.xlabel('time')
plt.title('Model3 Solution')
plt.legend(["x","y","vx","vy","mu"])
### Solve model3 (with jacobian)
# The jacobian definition. See dasslc manual for more details
def jac_pend(t,y,yp,cj,par):
PD = np.zeros((5,5))
PD[0][0] = cj
PD[0][2] = -1
PD[1][1] = cj
PD[1][3] = -1
PD[2][0] = y[4]
PD[2][2] = cj
PD[2][4] = y[0]
PD[3][1] = y[4]
PD[3][3] = cj
PD[3][4] = y[1]
if (par.dae == 3):
PD[4][0] = 2*y[0]
PD[4][1] = 2*y[1]
ires = 0
elif (par.dae == 2):
PD[4][0] = y[2]
PD[4][1] = y[3]
PD[4][2] = y[0]
PD[4][3] = y[1]
ires = 0
elif (par.dae == 1):
PD[4][1] = -par.g
PD[4][2] = 2*y[2]
PD[4][3] = 2*y[3]
PD[4][4] = -par.L**2
ires = 0
elif (par.dae == 0):
PD[4][3] = 3*par.g/par.L**2
PD[4][4] = cj
ires = 0
else:
print("Invalid index.")
ires = -1
return PD, ires
# if passing the jacobian, the input file
# must be properly configured. See model3.dat file
display = False
t,y,yp = dasslc.solve(model3,t0,y0,yp0,par,rtol,atol,index,"model3.dat",jac_pend,display)
# Plot results
plt.figure(6)
plt.plot(t,y)
plt.ylabel('y')
plt.xlabel('time')
plt.title('Model3 Solution (with jacobian)')
plt.legend(["x","y","vx","vy","mu"])
if SPARSE:
def model4(t,y,yp): #------------- A huge sparse system
res = np.empty(Ns)
#for i in range(0,Ns): #------ Avoid using for-loops
# res[i] = yp[i] + y[i] in python at all cost
res = yp + y
return res, 0
Ns = 5000
t0 = np.linspace(0,1,15)
y0 = np.ones(Ns)
yp0 = -y0
#with dense algebra
tic = time.time()
t, y, _ = dasslc.solve(model4,t0,y0,None,None,1e-6,1e-8)
toc = time.time() - tic
#with sparse algebra
tic = time.time()
t1, y1, _ = dasslc.solve(model4,t0,y0,None,None,1e-6,1e-8,None,"model4.dat")
toc1 = time.time() - tic
#with sparse algebra and jacobian supplied
def jacSparse(t,y,yp,cj): #---------------- The jacobian function
J = np.eye(Ns,Ns) #-------------------- Just an eye matrix
J = J + cj*J #------------------------- The true jacobian (iteration matrix)
i = range(0,Ns) #---------------------- The rows list
j = range(0,Ns) #---------------------- The columns list
ires = 0
return J, ires, i, j
tic = time.time()
t2, y2, _ = dasslc.solve(model4,t0,y0,None,None,1e-6,1e-8,None,"model4b.dat",jacSparse)
toc2 = time.time() - tic
plt.figure(7)
plt.plot(0,toc,'o')
plt.plot(1,toc1,'o')
plt.plot(2,toc2,'o')
plt.ylabel('Time (s)')
plt.title('Model4 Dense vs. Sparse performance comparison')
plt.legend(["Dense = %0.2f s" % toc,"Sparse = %0.2f s" % toc1,"Jac Sparse = %0.2f s" % toc2])
## Show all figures
plt.show()