-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-methods.ts
More file actions
96 lines (71 loc) · 2.51 KB
/
check-methods.ts
File metadata and controls
96 lines (71 loc) · 2.51 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
// A script for checking performance
import {ODEs, corrProbs, CorrProblem, mrt, ros3prw, ros34prw, rk4, ab5, ab4, rkdp, rk3, lsoda, cvode,
perfProbs, refPoints} from '../../index';
/** Return numerical solution error: maximum absolute deviation between approximate & exact solutions */
function getError(method: (odes: ODEs) => Float64Array[], corProb: CorrProblem): number {
let error = 0;
// Get numerical solution
const approxSolution = method(corProb.odes);
const exact = corProb.exact;
const arg = approxSolution[0];
const pointsCount = arg.length;
const funcsCount = approxSolution.length - 1;
// Compute error
for (let i = 0; i < pointsCount; ++i) {
const exactSolution = exact(arg[i]);
for (let j = 0; j < funcsCount; ++j)
error = Math.max(error, Math.abs(exactSolution[j] - approxSolution[j + 1][i]));
}
return error;
} // getError
function getDeviationFromReferencePoint(solution: Float64Array[], refPoint: Float64Array): number {
let absolute = 0;
const lastIdx = solution[0].length - 1;
let cur = 0;
for (let k = 0; k < solution.length; ++k) {
cur = Math.abs(solution[k][lastIdx] - refPoint[k]);
absolute = Math.max(cur, absolute);
}
return absolute;
} // getDeviationFromReferencePoint
const implicitMethods = new Map([
['MRT', mrt],
['ROS3PRw', ros3prw],
['ROS34PRw', ros34prw],
['LSODA', lsoda],
['CVODE', cvode],
]);
const methods = new Map([
...implicitMethods,
['RK4', rk4],
['AB5', ab5],
['AB4', ab4],
['RKDP', rkdp],
['RK3', rk3],
]);
console.log('Performance\n');
implicitMethods.forEach((method, name) => {
console.log(' ', name);
console.log(' PROBLEM TIME,MS ERR*');
perfProbs.forEach((odes, idx) => {
if (name === 'CVODE' && odes.name === 'E5')
return;
const start = Date.now();
const solution = method(odes);
const absErr = getDeviationFromReferencePoint(solution.slice(1), refPoints[idx]).toExponential(2);
const finish = Date.now();
console.log(` ${odes.name} ${finish - start} ${absErr}`.padStart(37));
});
console.log();
});
console.log('* approximate vs. reference (also, numerical)\n\n');
console.log('Correctness (maximum absolute deviation): approximate vs. exact\n');
methods.forEach((method, name) => {
console.log(' ', name);
console.log(' PROBLEM ERR');
corrProbs.forEach((problem) => {
const error = getError(method, problem);
console.log(` ${problem.odes.name} ${error.toExponential(2)}`.padStart(28));
});
console.log();
});