-
Notifications
You must be signed in to change notification settings - Fork 0
/
Optimization.py
512 lines (441 loc) · 19.3 KB
/
Optimization.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import scipy.optimize as scope
import Logger.Logger as logger
case = 0
'''
Name: optimize
Description: This function finds the optimal values to minimize the given
Principal Error Coefficient and provides the results in
the console.
Parameters:
f : f is the name of the function used as Principal Error Coefficient.
Returns: None
'''
def optimize(f):
if (f == E2):
alpha = [0.1]
elif(f == E3):
alpha = [0.01, 0.01]
elif(f == E4):
if (case == 1):
alpha = [0.1, 0.2]
elif (case == 3):
alpha = [-0.1]
elif ((case == 2) or (case == 4) or (case == 5)):
alpha = [0.1]
log.info("alpha: ", alpha)
res = scope.minimize(f, alpha, tol=1e-8)
print (res)
'''
Name: E2
Description: This function represents as the Principal Error Coefficient
for second order ERK methods. Provided the value of the
free coefficient (alpha), this function provides the
Principal Error Coefficient value.
Parameters:
alpha : alpha is the list of values for the free coefficients.
Returns:
result : result is the Principal Error Coefficient value for the
given free coefficient.
'''
def E2(alpha):
b = [1 - (1 / (2 * alpha[0])), 1 / (2 * alpha[0])]
c = [0, alpha[0]]
A = [[0, 0], [alpha[0], 0]]
csq = [c[0] ** 2, c[1] ** 2]
bcsq = (b[0] * csq[0]) + (b[1] * csq[1])
Ac = [(A[0][0] * c[0]) + (A[0][1] * c[1]),(A[1][0] * c[0]) + (A[1][1] * c[1])]
bAc = (b[0] * Ac[0]) + (b[1] * Ac[1])
result = (((1/2) * (bcsq - (1/3))) ** 2) + ((bAc - (1/6)) ** 2)
return result
'''
Name: setValuesForThirdOrderCase1
Description: This function sets and returns the values for all the
coefficients for Case 1 of the third order ERK methods.
Parameters:
alpha : alpha is the list of values for the free coefficients.
Returns:
c2, c3, b1, b2, b3, a31, a32: coefficients for Case 1 of the
third order ERK methods.
'''
def setValuesForThirdOrderCase1(alpha):
c2 = alpha[0]
c3 = alpha[1]
b1 = (2 - (3 * (c2 + c3)) + (6 * c2 * c3)) / (6 * c2 * c3)
b2 = (c3 - (2/3)) / (2 * c2 * (c3 - c2))
b3 = ((2/3) - c2) / (2 * c3 * (c3 - c2))
a31 = (c3 * (c3 - (3 * c2) + (3 * c2 * c2))) / (c2 * ((3 * c2) - 2))
a32 = (c3 * (c2 - c3)) / (c2 * ((3 * c2) - 2))
log.info("Coefficients selected for E3 are:")
log.info("c2, c3, b1, b2, b3, a31, a32", c2, c3, b1, b2, b3, a31, a32)
return c2, c3, b1, b2, b3, a31, a32
'''
Names: E3Eq1, E3Eq2, E3Eq3, E3Eq4
Description: These functions compute and return the weighted values of
the order conditions of Principal Error Coefficient
of third order ERK methods.
Parameters:
c : c are the nodes.
b : b are the weights.
A : A is the matrix.
Returns:
weighted values of the order conditions of Principal Error
Coefficient of third order ERK methods.
'''
def E3Eq1(c, b, A):
#For Equation 1, find b*c^3
ccube = [c[0] ** 3, c[1] ** 3, c[2] ** 3]
bccube = (b[0] * ccube[0]) + (b[1] * ccube[1]) + (b[2] * ccube[2])
return ((1/6) * (bccube - (1/4)))
def E3Eq2(c, b, A):
#For Equation 2, find b*c*A*c
bc = [(b[0] * c[0]), (b[1] * c[1]), (b[2] * c[2])]
Ac = [((A[0][0] * c[0]) + (A[0][1] * c[1]) + (A[0][2] * c[2])),
((A[1][0] * c[0]) + (A[1][1] * c[1]) + (A[1][2] * c[2])),
((A[2][0] * c[0]) + (A[2][1] * c[1]) + (A[2][2] * c[2]))]
bcAc = (bc[0] * Ac[0]) + (bc[1] * Ac[1]) + (bc[2] * Ac[2])
return (bcAc - (1/8))
def E3Eq3(c, b, A):
#For Equation 3, find b*A*c^2
csq = [c[0] ** 2, c[1] ** 2, c[2] ** 2]
Acsq = [((A[0][0] * csq[0]) + (A[0][1] * csq[1]) + (A[0][2] * csq[2])),
((A[1][0] * csq[0]) + (A[1][1] * csq[1]) + (A[1][2] * csq[2])),
((A[2][0] * csq[0]) + (A[2][1] * csq[1]) + (A[2][2] * csq[2]))]
bAcsq = (b[0] * Acsq[0]) + (b[1] * Acsq[1]) + (b[2] * Acsq[2])
return ((1/2) * (bAcsq - (1/12)))
def E3Eq4(c, b, A):
#For Equation 4, find b*A^2*c
Asq = []
for i in range (0, len(A)):
Asq.append([])
for j in range (0, len(A)):
Asq[i].append((A[i][0] * A[0][j]) + (A[i][1] * A[1][j]) + (A[i][2] * A[2][j]))
Asqc = [((Asq[0][0] * c[0]) + (Asq[0][1] * c[1]) + (Asq[0][2] * c[2])),
((Asq[1][0] * c[0]) + (Asq[1][1] * c[1]) + (Asq[1][2] * c[2])),
((Asq[2][0] * c[0]) + (Asq[2][1] * c[1]) + (Asq[2][2] * c[2]))]
bAsqc = (b[0] * Asqc[0]) + (b[1] * Asqc[1]) + (b[2] * Asqc[2])
return (bAsqc - (1/24))
'''
Name: E3
Description: This function represents as the Principal Error Coefficient
for third order ERK methods. Provided the values of the
free coefficients (alpha), this function provides the
Principal Error Coefficient value.
Parameters:
alpha : alpha is the list of values for the free coefficients.
Returns:
result : result is the Principal Error Coefficient value for the
given free coefficients.
'''
def E3(alpha):
if ((alpha[0] == 0) or (alpha[0] == 2/3) or (alpha[1] == 0) or (alpha[0] == alpha[1])):
return 1
c2, c3, b1, b2, b3, a31, a32 = setValuesForThirdOrderCase1(alpha)
c = [0, c2, c3]
b = [b1, b2, b3]
A = [[0, 0, 0],[c2, 0, 0],[a31, a32, 0]]
#For Equation 1
eq1 = E3Eq1(c, b, A)
#For Equation 2
eq2 = E3Eq2(c, b, A)
#For Equation 3
eq3 = E3Eq3(c, b, A)
#For Equation 4
eq4 = E3Eq4(c, b, A)
#E3^2
result = (eq1 ** 2) + (eq2 ** 2) + (eq3 ** 2) + (eq4 ** 2)
return result
'''
Name: setValuesForFourthOrder
Description: This function sets and returns the values for all the
coefficients for the fourth order ERK methods.
Parameters:
alpha : alpha is the list of values for the free coefficients.
Returns:
c2, c3, c4, b1, b2, b3, b4, a31, a32, a41, a42, a43:
coefficients for the fourth order ERK methods.
'''
def setValuesForFourthOrder(alpha):
if (case == 1):
c2 = alpha[0]
c3 = alpha[1]
c4 = 1
a31 = (c3 * ((3 * c2) - c3 - (4 * c2 * c2))) / (2 * c2 * (1 - (2 * c2)))
a32 = (c3 * (c3 - c2)) / (2 * c2 * (1 - (2 * c2)))
a41 = (((c3 ** 2) * ((12 * c2 * c2) - (12 * c2) + 4)) - (c3 * ((12 * c2 * c2) - (15 * c2) + 5)) + ((4 * c2 * c2) - (6 * c2) + 2)) / ((2 * c2 * c3) * (3 - (4 * (c2 + c3)) + (6 * c2 * c3)))
a42 = (((-4 * c3 * c3) + (5 * c3) + c2 - 2) * (1 - c2)) / ((2 * c2) * (c3 - c2) * (3 - (4 * (c2 + c3)) + (6 * c2 * c3)))
a43 = ((1 - (2 * c2)) * (1 - c3) * (1 - c2)) / (c3 * (c3 - c2) * (3 - (4 * (c2 + c3)) + (6 * c2 * c3)))
b1 = (1 - (2 * (c2 + c3)) + (6 * c2 * c3)) / (12 * c2 * c3)
b2 = ((2 * c3) - 1) / ((12 * c2) * (c3 - c2) * (1 - c2))
b3 = (1 - (2 * c2)) / ((12 * c3) * (c3 - c2) * (1 - c3))
b4 = (3 - (4 * (c2 + c3)) + (6 * c2 * c3)) / (12 * (1 - c2) * (1 - c3))
elif (case == 2):
b3 = alpha[0]
c2 = c3 = 1/2
c4 = 1
a31 = ((3 * b3) - 1) / (6 * b3)
a32 = 1 / (6 * b3)
a41 = 0
a42 = 1 - (3 * b3)
a43 = 3 * b3
b1 = 1/6
b2 = (2 / 3) - b3
b4 = 1/6
elif (case == 3):
b3 = alpha[0]
c2 = 1/2
c3 = 0
c4 = 1
a31 = -1 / (12 * b3)
a32 = 1 / (12 * b3)
a41 = (-1/2) - (6 * b3)
a42 = 3/2
a43 = 6 * b3
b1 = (1/6) - b3
b2 = 2/3
b4 = 1/6
elif (case == 4):
b4 = alpha[0]
c2 = 1
c3 = 1/2
c4 = 1
a31 = 3/8
a32 = 1/8
a41 = 1 - (1 / (4 * b4))
a42 = -1 / (12 * b4)
a43 = 1 / (3 * b4)
b1 = 1/6
b2 = 1/6 - b4
b3 = 2/3
elif (case == 5):
c2 = alpha[0]
c3 = 1/2
c4 = 1
a31 = ((4 * c2) - 1) / (8 * c2)
a32 = 1 / (8 * c2)
a41 = (1 - (2 * c2)) / (2 * c2)
a42 = -1 / (2 * c2)
a43 = 2
b1 = 1/6
b2 = 0
b3 = 2/3
b4 = 1/6
log.info("Coefficients selected for E4 are:")
log.info("c2, c3, c4, b1, b2, b3, b4, a31, a32, a41, a42, a43", c2, c3, c4, b1, b2, b3, b4, a31, a32, a41, a42, a43)
return c2, c3, c4, b1, b2, b3, b4, a31, a32, a41, a42, a43
'''
Names: E4Eq1, E4Eq2, E4Eq3, E4Eq4, E4Eq5, E4Eq6, E4Eq7, E4Eq8, E4Eq9
Description: These functions compute and return the weighted values of
the order conditions of Principal Error Coefficient
of fourth order ERK methods.
Parameters:
c : c are the nodes.
b : b are the weights.
A : A is the matrix.
Returns:
weighted values of the order conditions of Principal Error
Coefficient of fourth order ERK methods.
'''
def E4Eq1(c, b, A):
#For Equation 1, find b*c^4
cquad = [c[0] ** 4, c[1] ** 4, c[2] ** 4, c[3] ** 4]
bcquad = (b[0] * cquad[0]) + (b[1] * cquad[1]) + (b[2] * cquad[2]) + (b[3] * cquad[3])
return ((1/24) * (bcquad - (1/5)))
def E4Eq2(c, b, A):
#For Equation 2, find b*c^2*A*c
csq = [c[0] ** 2, c[1] ** 2, c[2] ** 2, c[3] ** 2]
bcsq = [b[0] * csq[0], b[1] * csq[1], b[2] * csq[2], b[3] * csq[3]]
Ac = [((A[0][0] * c[0]) + (A[0][1] * c[1]) + (A[0][2] * c[2]) + (A[0][3] * c[3])),
((A[1][0] * c[0]) + (A[1][1] * c[1]) + (A[1][2] * c[2]) + (A[1][3] * c[3])),
((A[2][0] * c[0]) + (A[2][1] * c[1]) + (A[2][2] * c[2]) + (A[2][3] * c[3])),
((A[3][0] * c[0]) + (A[3][1] * c[1]) + (A[3][2] * c[2]) + (A[3][3] * c[3]))]
bcsqAc = (bcsq[0] * Ac[0]) + (bcsq[1] * Ac[1]) + (bcsq[2] * Ac[2]) + (bcsq[3] * Ac[3])
return ((1/2) * (bcsqAc - (1/10)))
def E4Eq3(c, b, A):
#For Equation 3, find b*c*A*c^2
csq = [c[0] ** 2, c[1] ** 2, c[2] ** 2, c[3] ** 2]
bc = [b[0] * c[0], b[1] * c[1], b[2] * c[2], b[3] * c[3]]
Acsq = [((A[0][0] * csq[0]) + (A[0][1] * csq[1]) + (A[0][2] * csq[2]) + (A[0][3] * csq[3])),
((A[1][0] * csq[0]) + (A[1][1] * csq[1]) + (A[1][2] * csq[2]) + (A[1][3] * csq[3])),
((A[2][0] * csq[0]) + (A[2][1] * csq[1]) + (A[2][2] * csq[2]) + (A[2][3] * csq[3])),
((A[3][0] * csq[0]) + (A[3][1] * csq[1]) + (A[3][2] * csq[2]) + (A[3][3] * csq[3]))]
bcAcsq = (bc[0] * Acsq[0]) + (bc[1] * Acsq[1]) + (bc[2] * Acsq[2]) + (bc[3] * Acsq[3])
return ((1/2) * (bcAcsq - (1/15)))
def E4Eq4(c, b, A):
#For Equation 4, find b*c*A^2*c
bc = [b[0] * c[0], b[1] * c[1], b[2] * c[2], b[3] * c[3]]
Ac = [((A[0][0] * c[0]) + (A[0][1] * c[1]) + (A[0][2] * c[2]) + (A[0][3] * c[3])),
((A[1][0] * c[0]) + (A[1][1] * c[1]) + (A[1][2] * c[2]) + (A[1][3] * c[3])),
((A[2][0] * c[0]) + (A[2][1] * c[1]) + (A[2][2] * c[2]) + (A[2][3] * c[3])),
((A[3][0] * c[0]) + (A[3][1] * c[1]) + (A[3][2] * c[2]) + (A[3][3] * c[3]))]
AAc = [((A[0][0] * Ac[0]) + (A[0][1] * Ac[1]) + (A[0][2] * Ac[2]) + (A[0][3] * Ac[3])),
((A[1][0] * Ac[0]) + (A[1][1] * Ac[1]) + (A[1][2] * Ac[2]) + (A[1][3] * Ac[3])),
((A[2][0] * Ac[0]) + (A[2][1] * Ac[1]) + (A[2][2] * Ac[2]) + (A[2][3] * Ac[3])),
((A[3][0] * Ac[0]) + (A[3][1] * Ac[1]) + (A[3][2] * Ac[2]) + (A[3][3] * Ac[3]))]
bcAAc = (bc[0] * AAc[0]) + (bc[1] * AAc[1]) + (bc[2] * AAc[2]) + (bc[3] * AAc[3])
return (bcAAc - (1/30))
def E4Eq5(c, b, A):
#For Equation 5, find b*(A*c)^2
Ac = [((A[0][0] * c[0]) + (A[0][1] * c[1]) + (A[0][2] * c[2]) + (A[0][3] * c[3])),
((A[1][0] * c[0]) + (A[1][1] * c[1]) + (A[1][2] * c[2]) + (A[1][3] * c[3])),
((A[2][0] * c[0]) + (A[2][1] * c[1]) + (A[2][2] * c[2]) + (A[2][3] * c[3])),
((A[3][0] * c[0]) + (A[3][1] * c[1]) + (A[3][2] * c[2]) + (A[3][3] * c[3]))]
AcAc = [Ac[0] ** 2, Ac[1] ** 2, Ac[2] ** 2, Ac[3] ** 2]
bAcAc = (b[0] * AcAc[0]) + (b[1] * AcAc[1]) + (b[2] * AcAc[2]) + (b[3] * AcAc[3])
return ((1/2) * (bAcAc - (1/20)))
def E4Eq6(c, b, A):
#For Equation 6, find b*A*c^3
ccube = [c[0] ** 3, c[1] ** 3, c[2] ** 3, c[3] ** 3]
Accube = [((A[0][0] * ccube[0]) + (A[0][1] * ccube[1]) + (A[0][2] * ccube[2]) + (A[0][3] * ccube[3])),
((A[1][0] * ccube[0]) + (A[1][1] * ccube[1]) + (A[1][2] * ccube[2]) + (A[1][3] * ccube[3])),
((A[2][0] * ccube[0]) + (A[2][1] * ccube[1]) + (A[2][2] * ccube[2]) + (A[2][3] * ccube[3])),
((A[3][0] * ccube[0]) + (A[3][1] * ccube[1]) + (A[3][2] * ccube[2]) + (A[3][3] * ccube[3]))]
bAccube = (b[0] * Accube[0]) + (b[1] * Accube[1]) + (b[2] * Accube[2]) + (b[3] * Accube[3])
return ((1/6) * (bAccube - (1/20)))
def E4Eq7(c, b, A):
#For Equation 7, find b*A*c*(A*c)
Ac = [((A[0][0] * c[0]) + (A[0][1] * c[1]) + (A[0][2] * c[2]) + (A[0][3] * c[3])),
((A[1][0] * c[0]) + (A[1][1] * c[1]) + (A[1][2] * c[2]) + (A[1][3] * c[3])),
((A[2][0] * c[0]) + (A[2][1] * c[1]) + (A[2][2] * c[2]) + (A[2][3] * c[3])),
((A[3][0] * c[0]) + (A[3][1] * c[1]) + (A[3][2] * c[2]) + (A[3][3] * c[3]))]
cAc = [c[0] * Ac[0], c[1] * Ac[1], c[2] * Ac[2], c[3] * Ac[3]]
AcAc = [((A[0][0] * cAc[0]) + (A[0][1] * cAc[1]) + (A[0][2] * cAc[2]) + (A[0][3] * cAc[3])),
((A[1][0] * cAc[0]) + (A[1][1] * cAc[1]) + (A[1][2] * cAc[2]) + (A[1][3] * cAc[3])),
((A[2][0] * cAc[0]) + (A[2][1] * cAc[1]) + (A[2][2] * cAc[2]) + (A[2][3] * cAc[3])),
((A[3][0] * cAc[0]) + (A[3][1] * cAc[1]) + (A[3][2] * cAc[2]) + (A[3][3] * cAc[3]))]
bAcAc = (b[0] * AcAc[0]) + (b[1] * AcAc[1]) + (b[2] * AcAc[2]) + (b[3] * AcAc[3])
return (bAcAc - (1/40))
def E4Eq8(c, b, A):
#For Equation 8, find b*A^2*c^2
csq = [c[0] ** 2, c[1] ** 2, c[2] ** 2, c[3] ** 2]
Acsq = [((A[0][0] * csq[0]) + (A[0][1] * csq[1]) + (A[0][2] * csq[2]) + (A[0][3] * csq[3])),
((A[1][0] * csq[0]) + (A[1][1] * csq[1]) + (A[1][2] * csq[2]) + (A[1][3] * csq[3])),
((A[2][0] * csq[0]) + (A[2][1] * csq[1]) + (A[2][2] * csq[2]) + (A[2][3] * csq[3])),
((A[3][0] * csq[0]) + (A[3][1] * csq[1]) + (A[3][2] * csq[2]) + (A[3][3] * csq[3]))]
AAcsq = [((A[0][0] * Acsq[0]) + (A[0][1] * Acsq[1]) + (A[0][2] * Acsq[2]) + (A[0][3] * Acsq[3])),
((A[1][0] * Acsq[0]) + (A[1][1] * Acsq[1]) + (A[1][2] * Acsq[2]) + (A[1][3] * Acsq[3])),
((A[2][0] * Acsq[0]) + (A[2][1] * Acsq[1]) + (A[2][2] * Acsq[2]) + (A[2][3] * Acsq[3])),
((A[3][0] * Acsq[0]) + (A[3][1] * Acsq[1]) + (A[3][2] * Acsq[2]) + (A[3][3] * Acsq[3]))]
bAAcsq = (b[0] * AAcsq[0]) + (b[1] * AAcsq[1]) + (b[2] * AAcsq[2]) + (b[3] * AAcsq[3])
return ((1/2) * (bAAcsq - (1/60)))
def E4Eq9(c, b, A):
#For Equation 9, find b*A^3*c
Ac = [((A[0][0] * c[0]) + (A[0][1] * c[1]) + (A[0][2] * c[2]) + (A[0][3] * c[3])),
((A[1][0] * c[0]) + (A[1][1] * c[1]) + (A[1][2] * c[2]) + (A[1][3] * c[3])),
((A[2][0] * c[0]) + (A[2][1] * c[1]) + (A[2][2] * c[2]) + (A[2][3] * c[3])),
((A[3][0] * c[0]) + (A[3][1] * c[1]) + (A[3][2] * c[2]) + (A[3][3] * c[3]))]
AAc = [((A[0][0] * Ac[0]) + (A[0][1] * Ac[1]) + (A[0][2] * Ac[2]) + (A[0][3] * Ac[3])),
((A[1][0] * Ac[0]) + (A[1][1] * Ac[1]) + (A[1][2] * Ac[2]) + (A[1][3] * Ac[3])),
((A[2][0] * Ac[0]) + (A[2][1] * Ac[1]) + (A[2][2] * Ac[2]) + (A[2][3] * Ac[3])),
((A[3][0] * Ac[0]) + (A[3][1] * Ac[1]) + (A[3][2] * Ac[2]) + (A[3][3] * Ac[3]))]
AAAc = [((A[0][0] * AAc[0]) + (A[0][1] * AAc[1]) + (A[0][2] * AAc[2]) + (A[0][3] * Ac[3])),
((A[1][0] * AAc[0]) + (A[1][1] * AAc[1]) + (A[1][2] * AAc[2]) + (A[1][3] * Ac[3])),
((A[2][0] * AAc[0]) + (A[2][1] * AAc[1]) + (A[2][2] * AAc[2]) + (A[2][3] * Ac[3])),
((A[3][0] * AAc[0]) + (A[3][1] * AAc[1]) + (A[3][2] * AAc[2]) + (A[3][3] * Ac[3]))]
bAAAc = (b[0] * AAAc[0]) + (b[1] * AAAc[1]) + (b[2] * AAAc[2]) + (b[3] * AAAc[3])
return (bAAAc - (1/120))
'''
Name: E4
Description: This function represents as the Principal Error Coefficient
for fourth order ERK methods. Provided the values of the
free coefficients (alpha), this function provides the
Principal Error Coefficient value.
Parameters:
alpha : alpha is the list of values for the free coefficients.
Returns:
result : result is the Principal Error Coefficient value for the
given free coefficients.
'''
def E4(alpha):
if (case == 1):
if ((alpha[0] <= 0) or (alpha[1] <= 0) or (alpha[0] == 1) or (alpha[1] == 1)
or (alpha[0] == alpha[1]) or (alpha[0] == 1/2) or ((3 - (4 * (alpha[0] + alpha[1])) + (6 * alpha[0] * alpha[1])) == 0)):
return 1
elif ((case == 2) or (case == 3) or (case == 4) or (case == 5)):
if (alpha[0] == 0):
return 1
c2, c3, c4, b1, b2, b3, b4, a31, a32, a41, a42, a43 = setValuesForFourthOrder(alpha)
c = [0, c2, c3, c4]
b = [b1, b2, b3, b4]
A = [[0, 0, 0, 0],[c2, 0, 0, 0],[a31, a32, 0, 0], [a41, a42, a43, 0]]
#For Equation 1
eq1 = E4Eq1(c, b, A)
#For Equation 2
eq2 = E4Eq2(c, b, A)
#For Equation 3
eq3 = E4Eq3(c, b, A)
#For Equation 4
eq4 = E4Eq4(c, b, A)
#For Equation 5
eq5 = E4Eq5(c, b, A)
#For Equation 6
eq6 = E4Eq6(c, b, A)
#For Equation 7
eq7 = E4Eq7(c, b, A)
#For Equation 8
eq8 = E4Eq8(c, b, A)
#For Equation 9
eq9 = E4Eq9(c, b, A)
result = (eq1 ** 2) + (eq2 ** 2) + (eq3 ** 2) + (eq4 ** 2) + (eq5 ** 2) + (eq6 ** 2) + (eq7 ** 2) + (eq8 ** 2) + (eq9 ** 2)
return result
'''
Name: displayMenu
Description: This function displays the menu and asks the user to input
a choice.
Parameters: None
Returns:
choice : the interger value given by the user.
'''
def displayMenu():
print ("1. Optimize E2")
print ("2. Optimize E3")
print ("3. Optimize E4")
choice = input("Enter your choice: ")
return int(choice);
'''
Name: chooseE4Case
Description: This function display a menu for the cases of the fourth
order ERK methods and asks the user to input a choice.
Parameters: None
Returns:
choice : the interger value given by the user.
'''
def chooseE4Case():
print ("1. Case 1: 0, c2, c3, 1 all distinct,",
"\nc2≠1/2 and 3 - 4(c2 + c3) + 6*c2*c3 ≠ 0")
print ("2. Case 2: c2 = c3 = 1/2, b3≠0")
print ("3. Case 3: c2 = 1/2, c3 = 0, b3≠0")
print ("4. Case 4: c2 = 1, c3 = 1/2, b4≠0")
print ("5. Case 5: c2≠0, c3 = 1/2, b2 = 0")
choice = input("\nEnter your case choice: ")
return int(choice)
'''
Name: initializeOptimizer
Description: This function initializes the optimization process for the
Principal Error Coefficient of user's choice.
Parameters:
choice : the interger value given by the user.
Returns: None
'''
def initializeOptimizer(choice):
global case
if (choice == 1):
log.info("Optimizing E2")
optimize(E2)
elif (choice == 2):
log.info("Optimizing E3")
optimize(E3)
elif (choice == 3):
case = chooseE4Case()
if ((case < 1) or (case > 5)):
log.info("Invalid case: ", case)
initializeOptimizer(choice)
else:
log.info("Optimizing E4 with case: ", case)
optimize(E4)
else:
print ("Invalid choice. Please try again.")
log.info("Invalid choice: ", choice)
initializeOptimizer(displayMenu())
log = logger.Logger("Optimization Log")
initializeOptimizer(displayMenu())
del log