Skip to content

Commit d46863c

Browse files
cleaning up langevin
1 parent 9a57ddf commit d46863c

File tree

1 file changed

+29
-46
lines changed

1 file changed

+29
-46
lines changed

trajectories/langevin.js

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ export class SecondOrderLangevin extends Trajectory {
1616
// Time step
1717
dt: { range: [0.001, 0.1], default: 0.01 },
1818
// EMA alpha
19-
alpha: { range: [0, 1], default: 0.01 }
19+
alpha: { range: [0, 1], default: 0.01 },
20+
// seed
21+
seed: { range: [0, 10000], default: 1337 }
2022
})
21-
this.seed = "1337"
2223
this.precompute()
2324
}
2425

@@ -37,47 +38,28 @@ export class SecondOrderLangevin extends Trajectory {
3738

3839
this.N = Math.floor(T / dt) + 1
3940

40-
this.t_array = new Array(this.N)
41-
this.X_raw = new Array(this.N)
42-
this.Y_raw = new Array(this.N)
43-
this.Z_raw = new Array(this.N)
44-
this.Vx_raw = new Array(this.N)
45-
this.Vy_raw = new Array(this.N)
46-
this.Vz_raw = new Array(this.N)
47-
this.X = new Array(this.N)
48-
this.Y = new Array(this.N)
49-
this.Z = new Array(this.N)
50-
this.Vx = new Array(this.N)
51-
this.Vy = new Array(this.N)
52-
this.Vz = new Array(this.N)
41+
this.P_raw = Array(this.N).fill().map(() => new Array(3))
42+
this.V_raw = Array(this.N).fill().map(() => new Array(3))
43+
this.P = Array(this.N).fill().map(() => new Array(3))
44+
this.V = Array(this.N).fill().map(() => new Array(3))
5345

54-
this.X_raw[0] = 0
55-
this.Y_raw[0] = 0
56-
this.Z_raw[0] = 0
57-
this.Vx_raw[0] = 0
58-
this.Vy_raw[0] = 0
59-
this.Vz_raw[0] = 0
60-
this.X[0] = 0
61-
this.Y[0] = 0
62-
this.Z[0] = 0
63-
this.Vx[0] = 0
64-
this.Vy[0] = 0
65-
this.Vz[0] = 0
66-
const rng = new Rand(this.seed)
46+
for(let dim_i=0; dim_i < 3; dim_i++){
47+
this.P_raw[0][dim_i] = 0
48+
this.V_raw[0][dim_i] = 0
49+
this.P[0][dim_i] = 0
50+
this.V[0][dim_i] = 0
51+
}
52+
const rng = new Rand(this.parameter_values.seed)
6753

68-
for (let i = 1; i < this.N; ++i) {
54+
for (let step_i = 1; step_i < this.N; ++step_i) {
6955
for(let dim_i=0; dim_i<3; dim_i++) {
70-
const P_raw = dim_i === 0 ? this.X_raw : dim_i === 1 ? this.Y_raw : this.Z_raw;
71-
const V_raw = dim_i === 0 ? this.Vx_raw : dim_i === 1 ? this.Vy_raw : this.Vz_raw;
72-
const P = dim_i === 0 ? this.X : dim_i === 1 ? this.Y : this.Z;
73-
const V = dim_i === 0 ? this.Vx : dim_i === 1 ? this.Vy : this.Vz;
7456
const noise = randomGaussian(rng) * Math.sqrt(dt);
75-
const v_raw = V_raw[i-1] + (-gamma*V_raw[i-1] - omega*omega*P_raw[i-1]) * dt + sigma * noise;
76-
V_raw[i] = v_raw;
77-
P_raw[i] = P_raw[i-1] + v_raw * dt;
78-
const v_smooth = alpha * v_raw + (1-alpha) * V[i-1];
79-
V[i] = v_smooth;
80-
P[i] = P[i-1] + v_smooth * dt;
57+
const v_raw = this.V_raw[step_i-1][dim_i] + (-gamma*this.V_raw[step_i-1][dim_i] - omega*omega*this.P_raw[step_i-1][dim_i]) * dt + sigma * noise;
58+
this.V_raw[step_i][dim_i] = v_raw;
59+
this.P_raw[step_i][dim_i] = this.P_raw[step_i-1][dim_i] + v_raw * dt;
60+
const v_smooth = alpha * v_raw + (1-alpha) * this.V[step_i-1][dim_i];
61+
this.V[step_i][dim_i] = v_smooth;
62+
this.P[step_i][dim_i] = this.P[step_i-1][dim_i] + v_smooth * dt;
8163
}
8264
}
8365
}
@@ -89,13 +71,14 @@ export class SecondOrderLangevin extends Trajectory {
8971
let i = Math.floor(tr / dt)
9072
if (i < 0) i = 0
9173
if (i >= this.N) i = this.N - 1
92-
const x = this.X[i]
93-
const y = this.Y[i]
94-
const z = this.Z[i]
95-
const vx = this.Vx[i]
96-
const vy = this.Vy[i]
97-
const vz = this.Vz[i]
98-
return [x, y, z, vx, vy, vz]
74+
return [
75+
this.P[i][0],
76+
this.P[i][1],
77+
this.P[i][2],
78+
this.V[i][0],
79+
this.V[i][1],
80+
this.V[i][2],
81+
]
9982
}
10083
}
10184

0 commit comments

Comments
 (0)