Skip to content

Commit 31b04be

Browse files
Improve bond potentials + adapt (#49)
1 parent 1ed3f05 commit 31b04be

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

src/IO/IO.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,29 @@ function construct_bonds_array(io, N_bonds, N)
124124
return bond
125125
end
126126

127+
filter_kwargs(pairs...) = (; (k => v for (k, v) in pairs if v !== nothing)...)
128+
127129
function get_model(data, i::Int, j::Int)
128130
key = i <= j ? "$i-$j" : "$j-$i"
129131
m = data[key]
130132
if m["name"] == "GeneralKG"
131-
return GeneralKG(m["epsilon"], m["sigma"], m["k"], m["r0"]; rcut = m["rcut"])
133+
rcut = get(m, "rcut", nothing)
134+
135+
return GeneralKG(m["epsilon"], m["sigma"], m["k"], m["r0"];
136+
filter_kwargs(
137+
:rcut => get(m, "rcut", nothing),
138+
:ϵbond => get(m, "epsilonbond", nothing),
139+
:σbond => get(m, "sigmabond", nothing),
140+
:rcutbond => get(m, "rcutbond", nothing),
141+
)...)
132142
elseif m["name"] == "SmoothLennardJones"
133-
return SmoothLennardJones(m["epsilon"], m["sigma"]; rcut = m["rcut"])
143+
return SmoothLennardJones(m["epsilon"], m["sigma"];
144+
filter_kwargs(
145+
:rcut => get(m, "rcut", nothing))...)
134146
elseif m["name"] == "LennardJones"
135-
return LennardJones(m["epsilon"], m["sigma"]; rcut = m["rcut"])
147+
return LennardJones(m["epsilon"], m["sigma"];
148+
filter_kwargs(
149+
:rcut => get(m, "rcut", nothing))...)
136150
else
137151
error("Model $(m["name"]) is not implemented")
138152
return nothing

src/models.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,32 +180,45 @@ struct GeneralKG{T<:AbstractFloat} <: DiscreteModel
180180
name::String
181181
ϵ::T
182182
ϵ4::T
183+
ϵbond::T
184+
ϵ4bond::T
183185
σ2::T
186+
σ2bond::T
184187
shift::T
188+
shiftbond::T
185189
k::T
186190
kr02::T
187191
r02::T
188192
rcut::T
189193
rcut2::T
190-
194+
rcutbond::T
195+
rcut2bond::T
191196
end
192197

193-
function GeneralKG(ϵ, σ, k, r0; rcut=2^(1 / 6) * σ)
198+
function GeneralKG(ϵ, σ, k, r0; rcut=2^(1 / 6) * σ, ϵbond=ϵ, σbond=σ, rcutbond=rcut)
194199
name = "GeneralKG"
195200
r02 = r0 ^ 2
196201
rcut2 = rcut ^ 2
202+
rcut2bond = rcutbond ^2
197203
kr02 = - k * r02 / 2
198204
σ2 = σ ^ 2
205+
σ2bond = σbond ^ 2
199206
shift = lennard_jones(rcut2, 4ϵ, σ2)
200-
return GeneralKG(name, ϵ, 4ϵ, σ2, shift, k, kr02, r02, rcut, rcut2)
207+
shiftbond = lennard_jones(rcut2bond, 4ϵbond, σ2bond)
208+
return GeneralKG(name, ϵ, 4ϵ, ϵbond, 4ϵbond, σ2, σ2bond, shift, shiftbond, k, kr02, r02, rcut, rcut2, rcutbond, rcut2bond)
201209
end
202210

203211
@inline function potential(r2::T, model::GeneralKG) where T<:AbstractFloat
204212
return lennard_jones(r2, model.ϵ4, model.σ2) - model.shift
205213
end
206214

207215
@inline function bond_potential(r2::T, model::GeneralKG) where T<:AbstractFloat
208-
return r2 model.r02 ? fene(r2, model.kr02, model.r02) : Inf
216+
u_fene = r2 model.r02 ? fene(r2, model.kr02, model.r02) : Inf
217+
u_lj = 0.0
218+
if r2 cutoff2(model)
219+
u_lj += lennard_jones(r2, model.ϵ4bond, model.σ2bond) - model.shiftbond
220+
end
221+
return u_fene + u_lj
209222
end
210223

211224
cutoff(model::DiscreteModel) = model.rcut

src/molecules.jl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,9 @@ end
105105
function compute_energy_ij(system::Molecules, position_i, position_j, model_ij::Model, ::Bonded)
106106
box = get_box(system)
107107
r2 = nearest_image_distance_squared(position_i, position_j, box)
108-
energy_ij = bond_potential(r2, model_ij)
109-
cutoff2_val = cutoff2(model_ij)
110-
if r2 cutoff2_val
111-
energy_ij += potential(r2, model_ij)
112-
end
113-
114-
return energy_ij
108+
return bond_potential(r2, model_ij)
115109
end
110+
116111
function compute_energy_ij(system::Molecules, position_i, position_j, model_ij::Model, ::NonBonded)
117112
r2 = nearest_image_distance_squared(position_i, position_j, get_box(system))
118113
cutoff2_val = cutoff2(model_ij)

0 commit comments

Comments
 (0)