@@ -6,7 +6,7 @@ using LinearAlgebra
6
6
using Unitful
7
7
using UnitfulAtomic
8
8
9
- using AtomsBase: AbstractSystem
9
+ using AtomsBase: AbstractSystem
10
10
11
11
export test_approx_eq
12
12
export make_test_system
@@ -17,12 +17,10 @@ properties can be ignored during the comparison using the respective kwargs.
17
17
"""
18
18
function test_approx_eq (s:: AbstractSystem , t:: AbstractSystem ;
19
19
rtol= 1e-14 , ignore_atprop= Symbol[], ignore_sysprop= Symbol[],
20
- common_only= false )
20
+ common_only= false , quiet = false )
21
21
rnorm (a, b) = (ustrip (norm (a)) < rtol ? norm (a - b) / 1 unit (norm (a))
22
22
: norm (a - b) / norm (a))
23
23
24
- _isinfinite (s) = any (isinf, reduce (vcat, bounding_box (s)))
25
-
26
24
for method in (length, size, periodicity, )
27
25
@test method (s) == method (t)
28
26
end
@@ -32,7 +30,7 @@ function test_approx_eq(s::AbstractSystem, t::AbstractSystem;
32
30
@test rnorm (method (s, 1 ), method (t, 1 )) < rtol
33
31
end
34
32
35
- # TODO : add element_symbol back in
33
+ # TODO : add element_symbol back in
36
34
for method in (species, atomic_symbol, atomic_number, )
37
35
@test method (s, :) == method (t, :)
38
36
@test method (s, 1 ) == method (t, 1 )
@@ -46,6 +44,7 @@ function test_approx_eq(s::AbstractSystem, t::AbstractSystem;
46
44
end
47
45
end
48
46
47
+ # test properties of atoms
49
48
if common_only
50
49
test_atprop = [k for k in atomkeys (s) if hasatomkey (t, k)]
51
50
else
@@ -56,7 +55,7 @@ function test_approx_eq(s::AbstractSystem, t::AbstractSystem;
56
55
prop in ignore_atprop && continue
57
56
prop in (:velocity , :position ) && continue
58
57
if hasatomkey (s, prop) != hasatomkey (t, prop)
59
- println (" hashatomkey mismatch for $prop " )
58
+ quiet || println (" hashatomkey mismatch for $prop " )
60
59
@test hasatomkey (s, prop) == hasatomkey (t, prop)
61
60
continue
62
61
end
@@ -72,6 +71,12 @@ function test_approx_eq(s::AbstractSystem, t::AbstractSystem;
72
71
end
73
72
end
74
73
74
+ # Test some things on cell objects
75
+ @test typeof (cell (s)) == typeof (cell (t))
76
+ @test periodicity (cell (s)) == periodicity (cell (t))
77
+ @test n_dimensions (cell (s)) == n_dimensions (cell (t))
78
+
79
+ # test properties of systems
75
80
if common_only
76
81
test_sysprop = [k for k in keys (s) if haskey (t, k)]
77
82
else
@@ -81,15 +86,15 @@ function test_approx_eq(s::AbstractSystem, t::AbstractSystem;
81
86
for prop in test_sysprop
82
87
prop in ignore_sysprop && continue
83
88
if haskey (s, prop) != haskey (t, prop)
84
- println (" haskey mismatch for $prop " )
89
+ quiet || println (" haskey mismatch for $prop " )
85
90
@test haskey (s, prop) == haskey (t, prop)
86
91
continue
87
92
end
88
93
(haskey (s, prop) && haskey (t, prop)) || continue
89
94
90
95
if s[prop] isa Quantity
91
96
@test rnorm (s[prop], t[prop]) < rtol
92
- elseif prop in (:bounding_box , ) && ! ( _isinfinite (s))
97
+ elseif prop in (:bounding_box , ) && ( cell (s) isa PeriodicCell )
93
98
@test maximum (map (rnorm, s[prop], t[prop])) < rtol
94
99
else
95
100
@test s[prop] == t[prop]
@@ -104,23 +109,44 @@ Extra atomic or system properties can be specified using `extra_atprop` and `ext
104
109
and specific standard keys can be ignored using `drop_atprop` and `drop_sysprop`.
105
110
"""
106
111
function make_test_system (D= 3 ; drop_atprop= Symbol[], drop_sysprop= Symbol[],
107
- extra_atprop= (; ), extra_sysprop= (; ), cellmatrix= :full ,
112
+ extra_atprop= (; ), extra_sysprop= (; ), cellmatrix= :full ,
108
113
n_atoms = 5 , )
109
114
@assert D == 3
110
115
111
- # Generate some random data to store in Atoms
116
+ if cellmatrix == :lower_triangular
117
+ box = ([1.54732 , - 0.807289 , - 0.500870 ]u " Å" ,
118
+ [ 0.0 , 0.4654985 , 0.5615117 ]u " Å" ,
119
+ [ 0.0 , 0.0 , 0.7928950 ]u " Å" )
120
+ elseif cellmatrix == :upper_triangular
121
+ box = ([1.54732 , 0.0 , 0.0 ]u " Å" ,
122
+ [- 0.807289 , 0.4654985 , 0.0 ]u " Å" ,
123
+ [- 0.500870 , 0.5615117 , 0.7928950 ]u " Å" )
124
+ elseif cellmatrix == :diagonal
125
+ box = ([1.54732 , 0.0 , 0.0 ]u " Å" ,
126
+ [0.0 , 0.4654985 , 0.0 ]u " Å" ,
127
+ [0.0 , 0.0 , 0.7928950 ]u " Å" )
128
+ else
129
+ box = ([1.50304 , 0.850344 , 0.717239 ]u " Å" ,
130
+ [ 0.36113 , 1.008144 , 0.814712 ]u " Å" ,
131
+ [ 0.06828 , 0.381122 , 2.129081 ]u " Å" )
132
+ end
133
+
134
+ # Generate some random data to store in atoms and system
112
135
atprop = Dict {Symbol,Any} (
113
136
:position => [randn (3 ) for _ = 1 : n_atoms]u " Å" ,
114
137
:velocity => [randn (3 ) for _ = 1 : n_atoms] * 10 ^ 6 * u " m/s" ,
115
- # Note: reasonable velocity range in au
116
- :atomic_symbol => [:H , :H , :C , :N , :He ],
138
+ # Note to above: Reasonable velocity range in au
139
+ :species => ChemicalSpecies .( [:H , :H , :C , :N , :He ]) ,
117
140
:charge => [2 , 1 , 3.0 , - 1.0 , 0.0 ]u " e_au" ,
118
- :atomic_mass => 10 rand (n_atoms)u " u" ,
141
+ :mass => 10 rand (n_atoms)u " u" ,
119
142
:vdw_radius => randn (n_atoms)u " Å" ,
120
143
:covalent_radius => randn (n_atoms)u " Å" ,
121
144
:magnetic_moment => [0.0 , 0.0 , 1.0 , - 1.0 , 0.0 ],
122
145
)
123
146
sysprop = Dict {Symbol,Any} (
147
+ :bounding_box => box,
148
+ :periodicity => (true , true , false ),
149
+ #
124
150
:extra_data => 42 ,
125
151
:charge => - 1 u " e_au" ,
126
152
:multiplicity => 2 ,
@@ -136,36 +162,23 @@ function make_test_system(D=3; drop_atprop=Symbol[], drop_sysprop=Symbol[],
136
162
atprop = merge (atprop, pairs (extra_atprop))
137
163
138
164
atoms = map (1 : n_atoms) do i
139
- atargs = Dict (k => v[i] for (k, v) in pairs (atprop)
140
- if ! (k in (:position , :velocity )))
165
+ atargs = Dict (k => v[i] for (k, v) in pairs (atprop) if ! (k in (:position , :velocity )))
141
166
if haskey (atprop, :velocity )
142
- Atom (atprop[:atomic_symbol ][i], atprop[:position ][i], atprop[:velocity ][i];
143
- atargs... )
167
+ Atom (atprop[:species ][i], atprop[:position ][i], atprop[:velocity ][i]; atargs... )
144
168
else
145
- Atom (atprop[:atomic_symbol ][i], atprop[:position ][i]; atargs... )
169
+ Atom (atprop[:species ][i], atprop[:position ][i]; atargs... )
146
170
end
147
171
end
148
- if cellmatrix == :lower_triangular
149
- box = ([1.54732 , - 0.807289 , - 0.500870 ]u " Å" ,
150
- [ 0.0 , 0.4654985 , 0.5615117 ]u " Å" ,
151
- [ 0.0 , 0.0 , 0.7928950 ]u " Å" )
152
- elseif cellmatrix == :upper_triangular
153
- box = ([1.54732 , 0.0 , 0.0 ]u " Å" ,
154
- [- 0.807289 , 0.4654985 , 0.0 ]u " Å" ,
155
- [- 0.500870 , 0.5615117 , 0.7928950 ]u " Å" )
156
- elseif cellmatrix == :diagonal
157
- box = ([1.54732 , 0.0 , 0.0 ]u " Å" ,
158
- [0.0 , 0.4654985 , 0.0 ]u " Å" ,
159
- [0.0 , 0.0 , 0.7928950 ]u " Å" )
160
- else
161
- box = ([1.50304 , 0.850344 , 0.717239 ]u " Å" ,
162
- [ 0.36113 , 1.008144 , 0.814712 ]u " Å" ,
163
- [ 0.06828 , 0.381122 , 2.129081 ]u " Å" )
164
- end
165
- pbcs = (true , true , false )
166
- system = atomic_system (atoms, box, pbcs; sysprop... )
172
+ cell = PeriodicCell (; cell_vectors= sysprop[:bounding_box ],
173
+ periodicity= sysprop[:periodicity ])
174
+
175
+ sysargs = Dict (k => v for (k, v) in pairs (sysprop)
176
+ if ! (k in (:bounding_box , :periodicity )))
177
+ system = FlexibleSystem (atoms, cell; sysargs... )
167
178
168
- (; system, atoms, atprop= NamedTuple (atprop), sysprop= NamedTuple (sysprop), box, pbcs)
179
+ (; system, atoms, cell,
180
+ bounding_box= sysprop[:bounding_box ], periodicity= sysprop[:periodicity ],
181
+ atprop= NamedTuple (atprop), sysprop= NamedTuple (sysprop))
169
182
end
170
183
171
184
end
0 commit comments