Skip to content

Commit 6bf9fa4

Browse files
committed
Merge branch 'gpu-constraint-docs-tests' of https://github.com/ejmeitz/Molly.jl into gpu-constraint-docs-tests
2 parents 3babb0d + 36a7d39 commit 6bf9fa4

File tree

7 files changed

+66
-65
lines changed

7 files changed

+66
-65
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ MollyKernelDensityExt = "KernelDensity"
5050
MollyPythonCallExt = "PythonCall"
5151

5252
[compat]
53-
AcceleratedKernels = "0.4.2"
53+
AcceleratedKernels = "0.4.3"
5454
Atomix = "0.1, 1.0"
5555
AtomsBase = "0.5"
5656
AtomsCalculators = "0.2"

docs/src/documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,6 @@ The available loggers are:
11151115
- [`GeneralObservableLogger`](@ref)
11161116
- [`TemperatureLogger`](@ref)
11171117
- [`CoordinatesLogger`](@ref)
1118-
- [`DisplacementsLogger`](@ref)
11191118
- [`VelocitiesLogger`](@ref)
11201119
- [`TotalEnergyLogger`](@ref)
11211120
- [`KineticEnergyLogger`](@ref)
@@ -1125,6 +1124,7 @@ The available loggers are:
11251124
- [`DensityLogger`](@ref)
11261125
- [`VirialLogger`](@ref)
11271126
- [`PressureLogger`](@ref)
1127+
- [`DisplacementsLogger`](@ref)
11281128
- [`TrajectoryWriter`](@ref)
11291129
- [`StructureWriter`](@ref)
11301130
- [`TimeCorrelationLogger`](@ref)

src/Molly.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ using FFTW
2020
using GPUArrays
2121
using Graphs
2222
using KernelAbstractions
23-
import KernelAbstractions as KA
2423
using NearestNeighbors
2524
using PeriodicTable
2625
using SimpleCrystals

src/constraints/constraints_kernel_helper.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33

44
# No-op when backends are same
5-
to_backend(arr, old::T, new::T) where {T <: KA.Backend} = arr
5+
to_backend(arr, old::T, new::T) where {T <: Backend} = arr
66

77
# Allocates and copies when backends are different
8-
function to_backend(arr, old::A, new::B) where {A <: KA.Backend, B <: KA.Backend}
8+
function to_backend(arr, old::A, new::B) where {A <: Backend, B <: Backend}
99
out = allocate(new, eltype(arr), size(arr))
1010
copy!(out, arr)
1111
return out

src/constraints/shake_kernels.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ function shake_gpu!(
369369
active_idxs .= 1:N_active_clusters
370370
# Doesnt need to be initialized, kernel will do that
371371
still_active = allocate(backend, Bool, N_active_clusters)
372-
KA.pagelock!(backend, still_active)
372+
KernelAbstractions.pagelock!(backend, still_active)
373373

374374
iter = 1
375375
while iter <= max_iters

src/loggers.jl

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ export
1616
DensityLogger,
1717
VirialLogger,
1818
PressureLogger,
19+
DisplacementsLogger,
1920
write_structure,
2021
TrajectoryWriter,
2122
StructureWriter,
2223
TimeCorrelationLogger,
2324
AutoCorrelationLogger,
2425
AverageObservableLogger,
2526
ReplicaExchangeLogger,
26-
MonteCarloLogger,
27-
DisplacementsLogger
27+
MonteCarloLogger
2828

2929
"""
3030
apply_loggers!(system, neighbors=nothing, step_n=0, run_loggers=true;
@@ -301,7 +301,6 @@ function virial_wrapper(sys, neighbors, step_n; n_threads, kwargs...)
301301
return virial(sys, neighbors, step_n; n_threads=n_threads)
302302
end
303303

304-
305304
"""
306305
VirialLogger(n_steps)
307306
VirialLogger(T, n_steps)
@@ -342,6 +341,58 @@ function Base.show(io::IO, pl::GeneralObservableLogger{T, typeof(pressure_wrappe
342341
pl.n_steps, ", ", length(values(pl)), " pressures recorded")
343342
end
344343

344+
"""
345+
DisplacementsLogger(n_steps, coords_start; n_steps_update::Integer=10)
346+
347+
Log the displacements of atoms in a system throughout a simulation, useful for calculating
348+
properties like mean square displacement in periodic systems.
349+
350+
Displacements are updated every `n_steps_update` steps and a copy is saved every
351+
`n_steps` steps.
352+
`coords_start` are the initial reference positions and should match the coordinate type
353+
in the system.
354+
355+
It is assumed that a particle does not cross half the box size in `n_steps_update` steps.
356+
By default `n_steps_update` is set to 10 to mitigate this assumption, but it can be
357+
set to a higher value to reduce cost.
358+
`n_steps` must be a multiple of `n_steps_update`.
359+
"""
360+
mutable struct DisplacementsLogger{D, C}
361+
displacements::D
362+
coords_ref::C
363+
last_displacements::C
364+
n_steps::Int
365+
n_steps_update::Int
366+
end
367+
368+
function DisplacementsLogger(n_steps::Integer, coords_start; n_steps_update::Integer=10)
369+
if n_steps % n_steps_update != 0
370+
throw(ArgumentError("DisplacementsLogger n_steps ($n_steps) must be a multiple of " *
371+
"n_steps_update ($n_steps_update)"))
372+
end
373+
displacements = typeof(from_device(coords_start))[]
374+
return DisplacementsLogger(displacements, copy(coords_start), zero(coords_start),
375+
n_steps, n_steps_update)
376+
end
377+
378+
Base.values(dl::DisplacementsLogger) = dl.displacements
379+
380+
function log_property!(dl::DisplacementsLogger, sys::System, neighbors=nothing,
381+
step_n::Integer=0; kwargs...)
382+
if (step_n % dl.n_steps_update) == 0
383+
dl.last_displacements .+= vector.(dl.coords_ref, sys.coords, (sys.boundary,))
384+
dl.coords_ref .= sys.coords
385+
if (step_n % dl.n_steps) == 0
386+
push!(dl.displacements, Array(dl.last_displacements))
387+
end
388+
end
389+
end
390+
391+
function Base.show(io::IO, dl::DisplacementsLogger)
392+
print(io, "DisplacementsLogger with update n_steps ", dl.n_steps_update, ", saving n_steps ",
393+
dl.n_steps, ", ", length(dl.displacements), " displacements recorded")
394+
end
395+
345396
pdb_cryst1_length(l_Å) = lpad(round(l_Å; digits=3), 9)
346397
pdb_cryst1_angle(θ_rad) = lpad(round(rad2deg(θ_rad); digits=2), 7)
347398

@@ -958,54 +1009,3 @@ function log_property!(mcl::MonteCarloLogger{T},
9581009
push!(mcl.state_changed, success)
9591010
push!(mcl.energy_rates, energy_rate)
9601011
end
961-
962-
963-
"""
964-
DisplacementsLogger(n_steps, r0; n_update::Integer=1, dims::Integer=3)
965-
966-
Log the displacements of atoms in a system throughout a simulation. Displacements are
967-
updated every `n_update` steps and saved every `n_steps` steps. `r0` are the
968-
intitial refernce positions and should match the coords type in your `System` object.
969-
970-
The logger assumes a particle does not cross 2 periodic boxes in `n_update` steps.
971-
By default `n_update` is set to one to mitigate this assumption, but it can be
972-
set to a higher value to reduce cost. `n_steps` must be a multiple of `n_update`.
973-
"""
974-
mutable struct DisplacementsLogger{A, B}
975-
displacements::Vector{A}
976-
reference::Vector{B}
977-
last_displacements::Vector{B}
978-
n_steps::Int
979-
n_update::Int
980-
end
981-
982-
983-
function DisplacementsLogger(n_steps::Integer, r0; n_update::Integer = 1, dims::Integer = 3)
984-
T = eltype(first(r0))
985-
B = SArray{Tuple{dims}, T, 1, dims}
986-
A = Array{B, 1}
987-
if n_steps % n_update != 0
988-
throw(ArgumentError("DisplacementsLogger: n_steps ($n_steps) must be a multiple n_update ($(n_update))"))
989-
end
990-
return DisplacementsLogger{A, B}(A[], copy(r0), zero(r0), n_steps, n_update)
991-
end
992-
993-
Base.values(dl::DisplacementsLogger) = dl.displacements
994-
995-
function log_property!(dl::DisplacementsLogger, s::System, neighbors=nothing,
996-
step_n::Integer=0; kwargs...)
997-
998-
if (step_n % dl.n_update) == 0
999-
dl.last_displacements .+= vector.(dl.reference, s.coords, Ref(s.boundary))
1000-
dl.reference .= s.coords
1001-
if (step_n % dl.n_steps) == 0
1002-
push!(dl.displacements, copy(dl.last_displacements))
1003-
end
1004-
end
1005-
end
1006-
1007-
function Base.show(io::IO, dl::DisplacementsLogger)
1008-
print(io, "DisplacementsLogger with updating every ", dl.n_update, " steps, saving every ",
1009-
dl.n_steps, " steps with ", length(dl.displacements), " displacements in storage.")
1010-
end
1011-

test/simulation.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,12 @@ end
370370
),
371371
loggers=(
372372
coords=CoordinatesLogger(100),
373-
disp=DisplacementsLogger(100, coords)
373+
disp=DisplacementsLogger(100, coords),
374374
),
375375

376376
)
377377

378-
@test_throws ArgumentError DisplacementsLogger(100, coords; n_update = 17)
378+
@test_throws ArgumentError DisplacementsLogger(100, coords; n_steps_update=17)
379379

380380
if run_cuda_tests
381381
sys_gpu = System(
@@ -390,17 +390,19 @@ end
390390
),
391391
loggers=(
392392
coords=CoordinatesLogger(100),
393-
disp=DisplacementsLogger(100, CuArray(coords))
393+
disp=DisplacementsLogger(100, CuArray(coords)),
394394
),
395395
)
396396
end
397397

398398
for simulator in simulators
399399
@time simulate!(sys, simulator, n_steps; n_threads=1)
400-
@test sum(sum(first(values(sys.loggers.disp)))) == 0.0u"nm"
400+
@test all(isequal(0.0u"nm"), norm.(first(values(sys.loggers.disp))))
401+
@test mean(norm.(sys.loggers.disp.displacements[end])) > 0.005u"nm"
401402
if run_cuda_tests
402403
@time simulate!(sys_gpu, simulator, n_steps; n_threads=1)
403-
@test sum(first(values(sys_gpu.loggers.disp))) == 0.0u"nm"
404+
@test all(isequal(0.0u"nm"), norm.(first(values(sys_gpu.loggers.disp))))
405+
@test mean(norm.(sys.loggers.disp.displacements[end])) > 0.005u"nm"
404406
coord_diff = sys.coords .- from_device(sys_gpu.coords)
405407
coord_diff_size = sum(sum(map(x -> abs.(x), coord_diff))) / (3 * n_atoms)
406408
E_diff = abs(potential_energy(sys) - potential_energy(sys_gpu))

0 commit comments

Comments
 (0)