From c9f37f9ad9538972110d3407c9dca325cc049725 Mon Sep 17 00:00:00 2001 From: Niklas Schmitz Date: Wed, 23 Oct 2024 17:27:06 +0200 Subject: [PATCH 1/3] Fix rattle!(...) unit conversion for in-place update --- src/utils.jl | 5 ++++- test/test_bulk.jl | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 2df2797..59cd781 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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!, @@ -109,6 +109,9 @@ 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 diff --git a/test/test_bulk.jl b/test/test_bulk.jl index 374ff11..0f8e661 100644 --- a/test/test_bulk.jl +++ b/test/test_bulk.jl @@ -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 ] From 76995b0b8a7f725cdc1e5308ef51f5f9e134abec Mon Sep 17 00:00:00 2001 From: Niklas Schmitz Date: Wed, 23 Oct 2024 17:32:06 +0200 Subject: [PATCH 2/3] Fix rattle!(...) uniform random in radial component --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 59cd781..5552453 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -117,7 +117,7 @@ function rattle!(at::FlexibleSystem, r::Quantity) 𝐫ᵢ = 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)) at.particles[i] = p_new end return at From 9d6c1a49791840a7287972827a11206d3b9fbc97 Mon Sep 17 00:00:00 2001 From: Niklas Schmitz Date: Wed, 23 Oct 2024 17:41:45 +0200 Subject: [PATCH 3/3] Fix float type of radial rand() --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 5552453..53ca5e7 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -117,7 +117,7 @@ function rattle!(at::FlexibleSystem, r::Quantity) 𝐫ᵢ = p.position T = typeof(ustrip(𝐫ᵢ[1])) ui = randn(Vec3{T}) - p_new = _set_position(p, 𝐫ᵢ + rand() * r * ui / norm(ui)) + p_new = _set_position(p, 𝐫ᵢ + rand(T) * r * ui / norm(ui)) at.particles[i] = p_new end return at