Skip to content

Commit 3746541

Browse files
changed the Langevin thermostating for water
1 parent 388df58 commit 3746541

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

src/nbody_to_ode.jl

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,10 @@ function DiffEqBase.SDEProblem(simulation::NBodySimulation{<:WaterSPCFw})
449449
simultaneous_acceleration = gather_simultaneous_acceleration(simulation)
450450

451451
therm = simulation.thermostat
452+
mH = simulation.system.mH
453+
mO = simulation.system.mO
452454

453-
function deterministic_acceleration!(du2, u2, p, t)
454-
du = reshape(du2,3,2*3*n)
455-
u = reshape(u2,3,2*3*n)
456-
455+
function deterministic_acceleration!(du, u, p, t)
457456
du[:, 1:3*n] = @view u[:, 3*n+1 : 2*3*n];
458457

459458
@inbounds for i = 1:n
@@ -462,6 +461,10 @@ function DiffEqBase.SDEProblem(simulation::NBodySimulation{<:WaterSPCFw})
462461
acceleration!(a, (@view u[:, 1:3*n]), (@view u[:, 3*n+1 : 2*3*n]), t, 3 * (i - 1) + 1);
463462
end
464463
du[:, 3*n+3 * (i - 1) + 1] .= a
464+
465+
@. du[:, 3*n+3 * (i - 1) + 1] -= therm.γ*u[:, 3*n+3 * (i - 1) + 1]/mO
466+
@. du[:, 3*n+3 * (i - 1) + 2] -= therm.γ*u[:, 3*n+3 * (i - 1) + 2]/mH
467+
@. du[:, 3*n+3 * (i - 1) + 3] -= therm.γ*u[:, 3*n+3 * (i - 1) + 3]/mH
465468
end
466469
@inbounds for i in 1:n, j in (2, 3)
467470
a = MVector(0.0, 0.0, 0.0)
@@ -472,42 +475,27 @@ function DiffEqBase.SDEProblem(simulation::NBodySimulation{<:WaterSPCFw})
472475
end
473476
@inbounds for i = 1:n
474477
for acceleration! in group_accelerations
475-
acceleration!((@view du[:, 3*n+1 : 2*3*n]), (@view u[:, 1:3*n]), (@view u[:, 3*n+1 : 2*3*n]), t, i);
478+
acceleration!((@view du[ :, 3*n+1 : 2*3*n]), (@view u[:, 1:3*n]), (@view u[:, 3*n+1 : 2*3*n]), t, i);
476479
end
477480
end
478481
for acceleration! in simultaneous_acceleration
479482
acceleration!((@view du[:, 3*n+1 : 2*3*n]), (@view u[:, 1:3*n]), (@view u[:, 3*n+1 : 2*3*n]), t);
480483
end
481484
@. du[:, 3*n+1 : 2*3*n] -= therm.γ*u[:, 3*n+1 : 2*3*n]
482-
483-
du2 = reshape(du, 3*3*2*n)
484-
u2 = reshape(u, 3*3*2*n)
485485
end
486486

487-
σO = sqrt(2*therm.γ*simulation.kb*therm.T/simulation.system.mO)
488-
σH = sqrt(2*therm.γ*simulation.kb*therm.T/simulation.system.mH)
487+
#σO = sqrt(2*therm.γ*simulation.kb*therm.T/simulation.system.mO)
488+
#σH = sqrt(2*therm.γ*simulation.kb*therm.T/simulation.system.mH)
489+
σO = sqrt(2*therm.γ*simulation.kb*therm.T)/mO
490+
σH = sqrt(2*therm.γ*simulation.kb*therm.T)/mH
491+
489492
function noise!(du, u, p, t)
490493
@inbounds for i = 1:n
491-
n_ind = 3 * (i - 1)
492-
p_ind = 9 * (i - 1)
493-
for j = 1:3
494-
du[p_ind+j, n_ind+j] = σO
495-
du[p_ind+j+3, n_ind+j] = σH
496-
du[p_ind+j+6, n_ind+j] = σH
497-
end
498-
end
499-
end
500-
501-
A = zeros(3*3*2*n,3*n)
502-
for i = 1:n
503-
n_ind = 3 * (i - 1)
504-
p_ind = 9 * (i - 1)
505-
for j = 1:3
506-
A[3*3*n+p_ind+j, n_ind+j] = 1
507-
A[3*3*n+p_ind+j+3, n_ind+j] = 1
508-
A[3*3*n+p_ind+j+6, n_ind+j] = 1
494+
@. du[:, 3*n+3 * (i - 1) + 1] += σO
495+
@. du[:, 3*n+3 * (i - 1) + 2] += σH
496+
@. du[:, 3*n+3 * (i - 1) + 3] += σH
509497
end
510498
end
511499

512-
return SDEProblem(deterministic_acceleration!, noise!, reshape(hcat(u0, v0), 3*3*2*n), simulation.tspan, noise_rate_prototype=A)
513-
end
500+
return SDEProblem(deterministic_acceleration!, noise!, hcat(u0, v0), simulation.tspan)
501+
end

src/thermostats.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ end
2222

2323
function berendsen_acceleration!(dv, v, ms, kb, N, Nc, p::BerendsenThermostat)
2424
T = md_temperature(v, ms, kb, N, Nc)
25-
@. dv += p.γ * (p.T / T - 1) * v
25+
if inv(T) == Inf
26+
@. dv += p.γ * v
27+
else
28+
@. dv += p.γ * (p.T / T - 1) * v
29+
end
2630
end
2731

2832
# N - number of particles

0 commit comments

Comments
 (0)