Skip to content

Commit d0657b9

Browse files
Merge pull request #10 from dextorious/master
Performance improvements
2 parents 29268a8 + f57337b commit d0657b9

File tree

4 files changed

+141
-147
lines changed

4 files changed

+141
-147
lines changed

src/basic_potentials.jl

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434

3535
function Base.show(stream::IO, pp::GravitationalParameters)
3636
println(stream, "Gravitational:")
37-
print(stream, "\tG:"); show(stream, pp.G);
37+
print(stream, "\tG:"); show(stream, pp.G);
3838
println(stream)
3939
end
4040

@@ -58,7 +58,7 @@ end
5858

5959
function Base.show(stream::IO, pp::ElectrostaticParameters)
6060
println(stream, "Electrostatic:")
61-
print(stream, "\tk:"); show(stream, pp.k);
61+
print(stream, "\tk:"); show(stream, pp.k);
6262
println(stream)
6363
end
6464

@@ -72,7 +72,7 @@ end
7272

7373
function Base.show(stream::IO, pp::MagnetostaticParameters)
7474
println(stream, "Magnetostatic:")
75-
print(stream, "\tμ/4π:"); show(stream, pp.μ_4π);
75+
print(stream, "\tμ/4π:"); show(stream, pp.μ_4π);
7676
println(stream)
7777
end
7878

@@ -93,19 +93,19 @@ function pairwise_lennard_jones_acceleration!(dv,
9393

9494
force = @SVector [0.0, 0.0, 0.0];
9595
ri = @SVector [rs[1, i], rs[2, i], rs[3, i]]
96-
96+
9797
for j indxs
9898
if j != i
9999
rj = @SVector [rs[1, j], rs[2, j], rs[3, j]]
100-
(rij, rij_2, success) = apply_boundary_conditions!(ri, rj, pbc, p.R2)
101-
102-
if success
100+
(rij, r, rij_2) = get_interparticle_distance(ri, rj, pbc)
101+
102+
if rij_2 < p.R2
103103
σ_rij_6 = (p.σ2 / rij_2)^3
104104
σ_rij_12 = σ_rij_6^2
105-
force += (2 * σ_rij_12 - σ_rij_6 ) * rij / rij_2
106-
end
105+
force += (2 * σ_rij_12 - σ_rij_6 ) * rij / rij_2
106+
end
107107
end
108-
end
108+
end
109109
@. dv += 24 * p.ϵ * force / ms[i]
110110
end
111111

@@ -119,29 +119,28 @@ function pairwise_electrostatic_acceleration!(dv,
119119
p::ElectrostaticParameters,
120120
pbc::BoundaryConditions)
121121

122-
force = @SVector [0.0, 0.0, 0.0];
122+
force = @SVector [0.0, 0.0, 0.0]
123123
ri = @SVector [rs[1, i], rs[2, i], rs[3, i]]
124124
@inbounds for j = 1:n
125125
if !in(j, exclude[i])
126126
rj = @SVector [rs[1, j], rs[2, j], rs[3, j]]
127-
128-
(rij, rij_2, success) = apply_boundary_conditions!(ri, rj, pbc, p.R2)
129-
if success
130-
force += qs[j] * rij / norm(rij)^3
127+
rij, r, r2 = get_interparticle_distance(ri, rj, pbc)
128+
if r2 < p.R2
129+
force += qs[j] * rij / (r*r2)
131130
end
132131
end
133-
end
132+
end
134133
@. dv += p.k * qs[i] * force / ms[i]
135134
end
136135

137136

138-
function gravitational_acceleration!(dv,
137+
function gravitational_acceleration!(dv,
139138
rs,
140139
i::Integer,
141140
n::Integer,
142141
bodies::Vector{<:MassBody},
143142
p::GravitationalParameters)
144-
143+
145144
accel = @SVector [0.0, 0.0, 0.0];
146145
ri = @SVector [rs[1, i], rs[2, i], rs[3, i]]
147146
@inbounds for j = 1:n
@@ -151,11 +150,11 @@ function gravitational_acceleration!(dv,
151150
accel -= p.G * bodies[j].m * rij / norm(rij)^3
152151
end
153152
end
154-
153+
155154
@. dv += accel
156155
end
157156

158-
function magnetostatic_dipdip_acceleration!(dv,
157+
function magnetostatic_dipdip_acceleration!(dv,
159158
rs,
160159
i::Integer,
161160
n::Integer,
@@ -177,17 +176,17 @@ function magnetostatic_dipdip_acceleration!(dv,
177176
force += (mi * mij + mj * mir + r * dot(mi, mj) - 5 * r * mir * mij) / rij4
178177
end
179178
end
180-
179+
181180
@. dv += 3 * p.μ_4π * force / bodies[i].m
182181
end
183182

184-
function harmonic_bond_potential_acceleration!(dv,
183+
function harmonic_bond_potential_acceleration!(dv,
185184
rs,
186185
i::Integer,
187186
ms::Vector{<:Real},
188187
neighbouhoods::Dict{Int,Vector{Tuple{Int,Float64}}},
189188
p::SPCFwParameters)
190-
189+
191190
force = @SVector [0.0, 0.0, 0.0];
192191
ri = @SVector [rs[1, i], rs[2, i], rs[3, i]]
193192
@inbounds for (j, k) in neighbouhoods[i]
@@ -197,7 +196,7 @@ function harmonic_bond_potential_acceleration!(dv,
197196
d = r - p.rOH
198197
force -= d * k * rij / r
199198
end
200-
199+
201200
@. dv += force / ms[i]
202201
end
203202

@@ -221,14 +220,14 @@ function valence_angle_potential_acceleration!(dv,
221220
pc = normalize(cross(rcb, rbaXbc))
222221

223222
cosine = dot(rba, rbc) / (norm(rba) * norm(rbc))
224-
223+
225224
if cosine>1
226225
cosine = 1
227226
elseif cosine<-1
228227
cosine =-1
229228
end
230229
aHOH = acos(cosine)
231-
230+
232231
force = - p.ka * (aHOH - p.aHOH)
233232
force_a = pa * force / norm(rba)
234233
force_c = pc * force / norm(rbc)
@@ -237,4 +236,4 @@ function valence_angle_potential_acceleration!(dv,
237236
@. dv[:,a] += force_a / ms[a]
238237
@. dv[:,b] += force_b / ms[b]
239238
@. dv[:,c] += force_c / ms[c]
240-
end
239+
end

src/boundary_conditions.jl

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Base.start(::PeriodicBoundaryConditions) = 1
1111

1212
Base.done(pbc::PeriodicBoundaryConditions, state) = state > length(pbc.boundary)
1313

14-
function Base.next(pbc::PeriodicBoundaryConditions, state)
14+
function Base.next(pbc::PeriodicBoundaryConditions, state)
1515
pbc.boundary[state], state + 1
1616
end
1717

@@ -31,46 +31,41 @@ struct CubicPeriodicBoundaryConditions{cType <: Real} <: BoundaryConditions
3131
L::cType
3232
end
3333

34-
function apply_boundary_conditions!(ri, rj, pbc::PeriodicBoundaryConditions, R2)
35-
rij = @MVector [Inf, Inf, Inf]
36-
success = false
37-
rij2 = 0
38-
39-
for dx in [0,-pbc[2],pbc[2]], dy in [0,-pbc[4],pbc[4]], dz in [0,-pbc[6],pbc[6]]
40-
rij = @MVector [ri[1] - rj[1] + dx, ri[2] - rj[2] + dy, ri[3] - rj[3] + dz]
41-
for x in (1, 2, 3)
42-
rij[x] -= (pbc[2x] - pbc[2x - 1]) * div(rij[x], (pbc[2x] - pbc[2x - 1]))
43-
end
44-
rij2 = dot(rij, rij)
45-
if rij2 < R2
46-
success = true
47-
break
48-
end
49-
end
50-
return (rij, rij2, success)
34+
function get_interparticle_distance(ri, rj, pbc::PeriodicBoundaryConditions)
35+
rij = ri - rj
36+
x, y, z = rij
37+
while x < pbc[1] x += pbc[2]-pbc[1] end
38+
while x >= pbc[2] x -= pbc[2]-pbc[1] end
39+
while y < pbc[3] y += pbc[4]-pbc[3] end
40+
while y >= pbc[4] y -= pbc[4]-pbc[3] end
41+
while z < pbc[5] z += pbc[6]-pbc[5] end
42+
while z >= pbc[6] z -= pbc[6]-pbc[5] end
43+
rij = @SVector [x, y, z]
44+
r2 = rij[1]^2 + rij[2]^2 + rij[3]^2
45+
r = sqrt(r2)
46+
return (rij, r, r2)
5147
end
5248

53-
function apply_boundary_conditions!(ri, rj, pbc::CubicPeriodicBoundaryConditions, R2)
54-
rij = @MVector [Inf, Inf, Inf]
55-
success = false
56-
rij2 = 0
57-
58-
shifts = @SVector [0,-pbc.L,pbc.L]
59-
for dx in shifts, dy in shifts, dz in shifts
60-
rij = @MVector [ri[1] - rj[1] + dx, ri[2] - rj[2] + dy, ri[3] - rj[3] + dz]
61-
for x in (1, 2, 3)
62-
rij[x] -= pbc.L * div(rij[x], pbc.L)
63-
end
64-
rij2 = dot(rij, rij)
65-
if rij2 < R2
66-
success = true
67-
break
68-
end
69-
end
70-
return (rij, rij2, success)
49+
function get_interparticle_distance(ri, rj, bc::CubicPeriodicBoundaryConditions)
50+
rij = ri - rj
51+
x, y, z = rij
52+
size = bc.L
53+
radius = 0.5 * size
54+
while x >= radius x -= size end
55+
while x < -radius x += size end
56+
while y >= radius y -= size end
57+
while y < -radius y += size end
58+
while z >= radius z -= size end
59+
while z < -radius z += size end
60+
rij = @SVector [x, y, z]
61+
r2 = rij[1]^2 + rij[2]^2 + rij[3]^2
62+
r = sqrt(r2)
63+
return (rij, r, r2)
7164
end
7265

73-
function apply_boundary_conditions!(ri, rj, pbc::BoundaryConditions, R2)
66+
function get_interparticle_distance(ri, rj, ::BoundaryConditions)
7467
rij = ri - rj
75-
(rij, dot(rij,rij),true)
76-
end
68+
r2 = rij[1]^2 + rij[2]^2 + rij[3]^2
69+
r = sqrt(r2)
70+
(rij, r, r2)
71+
end

src/nbody_simulation_result.jl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ end
3232

3333
function get_velocity(sr::SimulationResult, time::Real, i::Integer=0)
3434
n = get_coordinate_vector_count(sr.simulation.system)
35-
35+
3636
if typeof(sr.solution[1]) <: RecursiveArrayTools.ArrayPartition
3737
velocities = sr(time).x[1]
3838
if i <= 0
@@ -183,13 +183,13 @@ function lennard_jones_potential(p::LennardJonesParameters, indxs::Vector{<:Inte
183183
j = indxs[ind_j]
184184
rj = @SVector [coordinates[1, j], coordinates[2, j], coordinates[3, j]]
185185

186-
(rij, rij_2, success) = apply_boundary_conditions!(ri, rj, pbc, p.R2)
186+
(rij, r, r2) = get_interparticle_distance(ri, rj, pbc)
187187

188-
if !success
189-
rij_2 = p.R2
188+
if !(r2 < p.R2)
189+
r2 = p.R2
190190
end
191191

192-
σ_rij_6 = (p.σ2 / rij_2)^3
192+
σ_rij_6 = (p.σ2 / r2)^3
193193
σ_rij_12 = σ_rij_6^2
194194
e_lj += (σ_rij_12 - σ_rij_6 )
195195
end
@@ -199,24 +199,25 @@ function lennard_jones_potential(p::LennardJonesParameters, indxs::Vector{<:Inte
199199
end
200200

201201
function electrostatic_potential(p::ElectrostaticParameters, indxs::Vector{<:Integer}, exclude::Dict{Int,Vector{Int}}, qs, rs, pbc::BoundaryConditions)
202-
e_el = 0
202+
e_el = 0.0
203203

204204
n = length(indxs)
205205
for ind_i = 1:n
206206
i = indxs[ind_i]
207207
ri = @SVector [rs[1, i], rs[2, i], rs[3, i]]
208-
e_el_i = 0
208+
e_el_i = 0.0
209209
for ind_j = ind_i + 1:n
210210
j = indxs[ind_j]
211211
if !in(j, exclude[i])
212212
rj = @SVector [rs[1, j], rs[2, j], rs[3, j]]
213213

214-
(rij, rij_2, success) = apply_boundary_conditions!(ri, rj, pbc, p.R2)
215-
if !success
216-
rij = p.R
217-
end
214+
(rij, r, r2) = get_interparticle_distance(ri, rj, pbc)
218215

219-
e_el_i += qs[j] / norm(rij)
216+
if r2 < p.R2
217+
e_el_i += qs[j] / r
218+
else
219+
e_el_i += qs[j] / p.R
220+
end
220221
end
221222
end
222223
e_el += e_el_i * qs[i]
@@ -461,11 +462,10 @@ function rdf(sr::SimulationResult)
461462
j = indxs[ind_j]
462463
rj = @SVector [cc[1, j], cc[2, j], cc[3, j]]
463464

464-
(rij, rij_2, success) = apply_boundary_conditions!(ri, rj, pbc, (0.5 * pbc.L)^2)
465+
(rij, r, r2) = get_interparticle_distance(ri, rj, pbc)
465466

466-
if success
467-
rij_1 = sqrt(rij_2)
468-
bin = ceil(Int, rij_1 / dr)
467+
if r2 < (0.5 * pbc.L)^2
468+
bin = ceil(Int, r / dr)
469469
if bin > 1 && bin <= maxbin
470470
hist[bin] += 2
471471
end
@@ -567,12 +567,12 @@ function write_pdb_data(f::IO, sr::SimulationResult{<:WaterSPCFw})
567567
@. cc[:, indH2] = cc[:,indO] + cc0[:, indH2] - cc0[:, indO]
568568
end
569569

570-
count+=1
570+
count+=1
571571
println(f,rpad("MODEL",10), count)
572572
println(f,"REMARK 250 time=$t picoseconds")
573573
for i 1:n
574574
indO, indH1, indH2 = 3 * (i - 1) + 1, 3 * (i - 1) + 2, 3 * (i - 1) + 3
575-
575+
576576
println(f,"HETATM",lpad(indO,5)," ",rpad("O",4),"HOH",lpad(i,6)," ", lpad(@sprintf("%8.3f",cc[1,indO]),8), lpad(@sprintf("%8.3f",cc[2,indO]),8), lpad(@sprintf("%8.3f",cc[3,indO]),8),lpad("1.00",6),lpad("0.00",6), lpad("",10), lpad("O",2))
577577
println(f,"HETATM",lpad(indH1,5)," ",rpad("H1",4),"HOH",lpad(i,6)," ", lpad(@sprintf("%8.3f",cc[1,indH1]),8), lpad(@sprintf("%8.3f",cc[2,indH1]),8), lpad(@sprintf("%8.3f",cc[3,indH1]),8),lpad("1.00",6),lpad("0.00",6), lpad("",10), lpad("H",2))
578578
println(f,"HETATM",lpad(indH2,5)," ",rpad("H2",4),"HOH",lpad(i,6)," ", lpad(@sprintf("%8.3f",cc[1,indH2]),8), lpad(@sprintf("%8.3f",cc[2,indH2]),8), lpad(@sprintf("%8.3f",cc[3,indH2]),8),lpad("1.00",6),lpad("0.00",6), lpad("",10), lpad("H",2))
@@ -588,11 +588,11 @@ function write_pdb_data(f::IO, sr::SimulationResult)
588588
for t in sr.solution.t
589589
cc = 10*get_position(sr, t)
590590
map!(x -> x -= L * floor(x / L), cc, cc)
591-
count+=1
591+
count+=1
592592
println(f,rpad("MODEL",10), count)
593593
println(f,"REMARK 250 time=$t steps")
594594
for i 1:n
595-
595+
596596
println(f,"HETATM",lpad(i,5)," ",rpad("Ar",4),"Ar",lpad(i,6)," ", lpad(@sprintf("%8.3f",cc[1,i]),8), lpad(@sprintf("%8.3f",cc[2,i]),8), lpad(@sprintf("%8.3f",cc[3,i]),8),lpad("1.00",6),lpad("0.00",6), lpad("",10), lpad("Ar",2))
597597
end
598598
println(f,"ENDMDL")
@@ -656,7 +656,7 @@ function extract_from_pdb(file)
656656
break
657657
end
658658
end
659-
659+
660660

661661
ln_time = readline(file)
662662
parts = split(ln_time)
@@ -687,7 +687,7 @@ function extract_from_pdb(file)
687687

688688
push!(wms, WaterMolecule(o,h1,h2))
689689
end
690-
end
690+
end
691691

692692
wms
693-
end
693+
end

0 commit comments

Comments
 (0)