Repository navigation
Expand file tree
/
Copy pathRungeKutta.ts
More file actions
145 lines (131 loc) · 4.03 KB
/
Copy pathRungeKutta.ts
File metadata and controls
145 lines (131 loc) · 4.03 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
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
// Copyright 2016 Erik Neumann. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { ODESim } from './ODESim.js'
import { DiffEqSolver } from './DiffEqSolver.js'
import { Util } from '../util/Util.js'
/** Runge-Kutta method for solving ordinary differential equations
expressed as a {@link ODESim}; operates by using the
differential equations to advance the variables by a small time step.
This is the most numerically accurate DiffEqSolver provided in myPhysicsLab. See
[Runge-Kutta Algorithm](https://www.myphysicslab.com/runge_kutta.html) for an
explanation of the algorithm.
*/
export class RungeKutta implements DiffEqSolver {
/** the set of differential equations to solve. */
private ode_: ODESim;
/** array used within algorithm, retained to avoid reallocation of array. */
private inp_: number[] = [];
/** array used within algorithm, retained to avoid reallocation of array. */
private k1_: number[] = [];
/** array used within algorithm, retained to avoid reallocation of array. */
private k2_: number[] = [];
/** array used within algorithm, retained to avoid reallocation of array. */
private k3_: number[] = [];
/** array used within algorithm, retained to avoid reallocation of array. */
private k4_: number[] = [];
/**
* @param ode the set of differential equations to solve
*/
constructor(ode: ODESim) {
this.ode_ = ode;
};
/** @inheritDoc */
toString() {
return this.toStringShort();
};
/** @inheritDoc */
toStringShort() {
return 'RungeKutta{ode_: '+this.ode_.toStringShort()+'}';
};
/** @inheritDoc */
getName(opt_localized?: boolean): string {
return opt_localized ? RungeKutta.i18n.NAME :
Util.toName(RungeKutta.en.NAME);
};
/** @inheritDoc */
nameEquals(name: string): boolean {
return this.getName() == Util.toName(name);
};
/** @inheritDoc */
step(stepSize: number): null|object {
const va = this.ode_.getVarsList();
const vars = va.getValues();
const N = vars.length;
if (this.inp_.length < N) {
this.inp_ = new Array(N);
this.k1_ = new Array(N);
this.k2_ = new Array(N);
this.k3_ = new Array(N);
this.k4_ = new Array(N);
}
const inp = this.inp_;
const k1 = this.k1_;
const k2 = this.k2_;
const k3 = this.k3_;
const k4 = this.k4_;
// evaluate at time t
for (let i=0; i<N; i++) {
inp[i] = vars[i];
}
Util.zeroArray(k1);
let error = this.ode_.evaluate(inp, k1, 0);
if (error !== null) {
return error;
}
// evaluate at time t+stepSize/2
for (let i=0; i<N; i++) {
inp[i] = vars[i]+k1[i]*stepSize/2;
}
Util.zeroArray(k2);
error = this.ode_.evaluate(inp, k2, stepSize/2);
if (error !== null) {
return error;
}
// evaluate at time t+stepSize/2
for (let i=0; i<N; i++) {
inp[i] = vars[i]+k2[i]*stepSize/2;
}
Util.zeroArray(k3);
error = this.ode_.evaluate(inp, k3, stepSize/2);
if (error !== null) {
return error;
}
// evaluate at time t+stepSize
for (let i=0; i<N; i++) {
inp[i] = vars[i]+k3[i]*stepSize;
}
Util.zeroArray(k4);
error = this.ode_.evaluate(inp, k4, stepSize);
if (error !== null) {
return error;
}
for (let i=0; i<N; i++) {
vars[i] += (k1[i] + 2*k2[i] + 2*k3[i] + k4[i])*stepSize/6;
}
va.setValues(vars, /*continuous=*/true);
return null;
};
static en: i18n_strings = {
NAME: 'Runge-Kutta'
};
static de_strings: i18n_strings = {
NAME: 'Runge-Kutta'
};
static readonly i18n = Util.LOCALE === 'de' ? RungeKutta.de_strings :
RungeKutta.en;
} // end class
type i18n_strings = {
NAME: string
};
Util.defineGlobal('lab$model$RungeKutta', RungeKutta);