Skip to content

Commit

Permalink
cleanup, union, deleteat!
Browse files Browse the repository at this point in the history
  • Loading branch information
JuliaMolSim committed May 22, 2024
1 parent 288377c commit 4ae1272
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ version = "0.0.2"
AtomsBase = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[compat]
AtomsBase = "0.3.5"
JSON = "0.21.4"
LinearAlgebra = "1.9.0, 1.10.0"
Random = "1.9.0, 1.10.0"
StaticArrays = "1.9"
Unitful = "1.19"
julia = "1.9.0, 1.10.0"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
JuLIP = "945c410c-986d-556a-acb1-167a618e0462"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "JuLIP"]
61 changes: 32 additions & 29 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

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

export rattle!,
Expand Down Expand Up @@ -93,50 +93,53 @@ import Base.*


"""
`rattle!(at, r::Float64) -> at`
```
rattle!(sys, r::Union{AbstractFloat, Quantity}) -> at
```
Randomly perturbs the atom positions within a ball of radius `r`. The perturbation
is uniform in angular component, and uniform in radial component. (Note this is
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::AbstractFloat)
function rattle!(at::FlexibleSystem, r::Quantity)
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) * unit(𝐫ᵢ[1]))
p_new = _set_position(p, 𝐫ᵢ + r * ui / norm(ui))
at.particles[i] = p_new
end
return at
end

rattle!(sys::FlexibleSystem, r::AbstractFloat) =
rattle!(sys, r * unit(position(sys)[1][1]))


# union(at1::Atoms, at2::Atoms) =
# Atoms( X = union(at1.X, at2.X),
# P = union(at1.P, at2.P),
# M = union(at1.M, at2.M),
# Z = union(at1.Z, at2.Z),
# cell = cell(at1),
# pbc = pbc(at1) )



# """
# `deleteat!(at::Atoms, n) -> at`:

# returns the same atoms object `at`, but with the atom(s) specified by `n`
# removed.
# """
# function Base.deleteat!(at::Atoms, n)
# deleteat!(at.X, n)
# deleteat!(at.P, n)
# deleteat!(at.M, n)
# deleteat!(at.Z, n)
# update_data!(at, Inf)
# JuLIP.reset_clamp!(at)
# return at
# end
"""
```
union(sys1::FlexibleSystem, sys2::FlexibleSystem)
```
takes the union of two particle systems provided their cells are identical.
"""
function union(sys1::FlexibleSystem, sys2::FlexibleSystem)
@assert boundary_conditions(sys1) == boundary_conditions(sys2)
@assert bounding_box(sys1) == bounding_box(sys2)
return FlexibleSystem(union(sys1.particles, sys2.particles),
bounding_box(at),
boundary_conditions(at) )
end

"""
`deleteat!(sys::FlexibleSystem, n) -> sys`:
returns the same `FlexibleSystem`` object `sys`, but with the atom(s) specified by `n`
removed.
"""
function Base.deleteat!(sys::FlexibleSystem, n)
deleteat!(sys.particles, n)
return sys
end
13 changes: 12 additions & 1 deletion test/test_bulk.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

using AtomsBuilder, Test, AtomsBase, Unitful
using AtomsBuilder, Test, AtomsBase, Unitful, Random
import JuLIP

##
Expand Down Expand Up @@ -39,6 +39,7 @@ end
##

# not sure how to write a test for this, but at least it should execute
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)

Expand All @@ -54,3 +55,13 @@ Znew = copy(Z); Znew[3:5:end] .= zO
sys4 = set_elements(sys3, Znew)
@test all(atomic_number(sys4) .== Znew)

pold = deepcopy(sys4.particles)
deleteat!(sys4, 1:5)
@test position.(pold[6:end]) == position(sys4)
@test atomic_mass.(pold[6:end]) == atomic_mass(sys4)
@test atomic_number.(pold[6:end]) == atomic_number(sys4)





0 comments on commit 4ae1272

Please sign in to comment.