Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unitful rattle #11

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

using AtomsBase
using AtomsBase: Atom, FlexibleSystem
using Unitful: unit, ustrip, Quantity
using Unitful: unit, uconvert, ustrip, Quantity
using LinearAlgebra: norm

export rattle!,
Expand Down Expand Up @@ -109,12 +109,15 @@ not the same as choosing them uniform in cartesian coordinates!).
If `r` is unitless, then the unit of the system is applied.
"""
function rattle!(at::FlexibleSystem, r::Quantity)
(length(at.particles) > 0) || return at
at_unit = unit(position(at, 1)[1])
r = uconvert(at_unit, r)
for i = 1:length(at.particles)
p = at.particles[i]
𝐫ᵢ = p.position
T = typeof(ustrip(𝐫ᵢ[1]))
ui = randn(Vec3{T})
p_new = _set_position(p, 𝐫ᵢ + r * ui / norm(ui))
p_new = _set_position(p, 𝐫ᵢ + rand() * r * ui / norm(ui))
cortner marked this conversation as resolved.
Show resolved Hide resolved
at.particles[i] = p_new
end
return at
Expand Down
1 change: 1 addition & 0 deletions test/test_bulk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ end
sys0 = rattle!(bulk(:C, cubic=true) * (2,3,4), 0.1u"Å")
sys1 = rattle!(bulk(:C, cubic=true) * (2,3,4), 0.1)
sys2 = rattle!(bulk(:C) * (2,3,4), 0.1)
rattle!(bulk(:C) * (2,3,4), 0.01u"nm")

X = position(sys1, :)
Xnew = [ x .+ 0.01u"Å" for x in X ]
Expand Down