-
Notifications
You must be signed in to change notification settings - Fork 894
/
Copy pathtest_read_write.py
241 lines (198 loc) · 9.64 KB
/
test_read_write.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from __future__ import annotations
import json
from numpy.testing import assert_allclose
from pytest import approx
from pymatgen.analysis.chemenv.coordination_environments.chemenv_strategies import (
AngleNbSetWeight,
CNBiasNbSetWeight,
DeltaCSMNbSetWeight,
DistanceAngleAreaNbSetWeight,
MultiWeightsChemenvStrategy,
NormalizedAngleDistanceNbSetWeight,
SelfCSMNbSetWeight,
SimplestChemenvStrategy,
)
from pymatgen.analysis.chemenv.coordination_environments.coordination_geometry_finder import LocalGeometryFinder
from pymatgen.analysis.chemenv.coordination_environments.structure_environments import (
LightStructureEnvironments,
StructureEnvironments,
)
from pymatgen.analysis.chemenv.coordination_environments.voronoi import DetailedVoronoiContainer
from pymatgen.core.structure import Structure
from pymatgen.util.testing import TEST_FILES_DIR, MatSciTest
__author__ = "waroquiers"
json_dir = f"{TEST_FILES_DIR}/analysis/chemenv/json"
struct_env_dir = f"{TEST_FILES_DIR}/analysis/chemenv/structure_environments"
class TestReadWriteChemenv(MatSciTest):
@classmethod
def setup_class(cls):
cls.lgf = LocalGeometryFinder()
cls.lgf.setup_parameters(centering_type="standard")
def test_read_write_structure_environments(self):
with open(f"{json_dir}/test_T--4_FePO4_icsd_4266.json", encoding="utf-8") as file:
dd = json.load(file)
atom_indices = dd["atom_indices"]
struct = Structure.from_dict(dd["structure"])
self.lgf.setup_structure(struct)
se = self.lgf.compute_structure_environments(
only_indices=atom_indices, maximum_distance_factor=2.25, get_from_hints=True
)
with open(f"{self.tmp_path}/se.json", mode="w", encoding="utf-8") as file:
json.dump(se.as_dict(), file)
with open(f"{self.tmp_path}/se.json", encoding="utf-8") as file:
dd = json.load(file)
se2 = StructureEnvironments.from_dict(dd)
assert se == se2
strategy = SimplestChemenvStrategy()
lse = LightStructureEnvironments.from_structure_environments(
structure_environments=se, strategy=strategy, valences="undefined"
)
with open(f"{self.tmp_path}/lse.json", mode="w", encoding="utf-8") as file:
json.dump(
lse.as_dict(),
file,
default=lambda obj: getattr(obj, "tolist", lambda: obj)(),
)
with open(f"{self.tmp_path}/lse.json", encoding="utf-8") as file:
LightStructureEnvironments.from_dict(json.load(file))
# assert lse == lse2
def test_structure_environments_neighbors_sets(self):
with open(f"{struct_env_dir}/se_mp-7000.json", encoding="utf-8") as file:
dct = json.load(file)
struct_envs = StructureEnvironments.from_dict(dct)
isite = 6
nb_set = struct_envs.neighbors_sets[isite][4][0]
nb_set_surface_points = [
[1.0017922780870239, 0.99301365328679292],
[1.0017922780870239, 0],
[2.2237615554448569, 0],
[2.2237615554448569, 0.0060837],
[2.25, 0.0060837],
[2.25, 0.99301365328679292],
]
assert_allclose(nb_set.voronoi_grid_surface_points(), nb_set_surface_points, atol=1e-8)
neighb_sites = nb_set.neighb_sites
coords = [
[0.244379, 1.804096, -1.132183],
[1.440203, 1.113687, 1.132183],
[2.755130, 2.544652, -0.704672],
[0.826167, 3.658339, 0.704672],
]
for idx, coord in enumerate(coords):
assert coord == approx(neighb_sites[idx].coords, abs=6)
neighb_coords = nb_set.coords
assert_allclose(coords, neighb_coords[1:], atol=1e-6)
assert_allclose(nb_set.structure[nb_set.isite].coords, neighb_coords[0])
norm_dist = nb_set.normalized_distances
assert sorted(norm_dist) == approx(sorted([1.001792, 1.001792, 1, 1]))
norm_ang = nb_set.normalized_angles
assert sorted(norm_ang) == approx(sorted([0.999999, 1, 0.993013, 0.993013]))
dist = nb_set.distances
assert sorted(dist) == approx(sorted([1.628439, 1.628439, 1.625526, 1.625526]))
ang = nb_set.angles
assert sorted(ang) == approx(sorted([3.117389, 3.117389, 3.095610, 3.095610]))
nb_set_info = nb_set.info
assert nb_set_info["normalized_angles_mean"] == approx(0.996506826547)
assert nb_set_info["normalized_distances_std"] == approx(0.000896138995037)
assert nb_set_info["angles_std"] == approx(0.0108895833142)
assert nb_set_info["distances_std"] == approx(0.00145669776056)
assert nb_set_info["distances_mean"] == approx(1.62698328347)
assert (
str(nb_set) == "Neighbors Set for site #6 :\n - Coordination number : 4\n - Voronoi indices : 1, 4, 5, 6\n"
)
assert nb_set == nb_set
assert hash(nb_set) == 4
def test_strategies(self):
simplest_strategy_1 = SimplestChemenvStrategy()
simplest_strategy_2 = SimplestChemenvStrategy(distance_cutoff=1.5, angle_cutoff=0.5)
assert simplest_strategy_1 != simplest_strategy_2
simplest_strategy_1_from_dict = SimplestChemenvStrategy.from_dict(simplest_strategy_1.as_dict())
assert simplest_strategy_1, simplest_strategy_1_from_dict
effective_csm_estimator = {
"function": "power2_inverse_decreasing",
"options": {"max_csm": 8},
}
self_csm_weight = SelfCSMNbSetWeight()
surface_definition = {
"type": "standard_elliptic",
"distance_bounds": {"lower": 1.1, "upper": 1.9},
"angle_bounds": {"lower": 0.1, "upper": 0.9},
}
surface_definition_2 = {
"type": "standard_elliptic",
"distance_bounds": {"lower": 1.1, "upper": 1.9},
"angle_bounds": {"lower": 0.1, "upper": 0.95},
}
da_area_weight = DistanceAngleAreaNbSetWeight(
weight_type="has_intersection",
surface_definition=surface_definition,
nb_sets_from_hints="fallback_to_source",
other_nb_sets="0_weight",
additional_condition=DistanceAngleAreaNbSetWeight.AC.ONLY_ACB,
)
da_area_weight_2 = DistanceAngleAreaNbSetWeight(
weight_type="has_intersection",
surface_definition=surface_definition_2,
nb_sets_from_hints="fallback_to_source",
other_nb_sets="0_weight",
additional_condition=DistanceAngleAreaNbSetWeight.AC.ONLY_ACB,
)
weight_estimator = {
"function": "smootherstep",
"options": {"delta_csm_min": 0.5, "delta_csm_max": 3},
}
symmetry_measure_type = "csm_wcs_ctwcc"
delta_weight = DeltaCSMNbSetWeight(
effective_csm_estimator=effective_csm_estimator,
weight_estimator=weight_estimator,
symmetry_measure_type=symmetry_measure_type,
)
bias_weight = CNBiasNbSetWeight.linearly_equidistant(weight_cn1=1, weight_cn13=4)
bias_weight_2 = CNBiasNbSetWeight.linearly_equidistant(weight_cn1=1, weight_cn13=5)
angle_weight = AngleNbSetWeight()
nad_weight = NormalizedAngleDistanceNbSetWeight(average_type="geometric", aa=1, bb=1)
multi_weights_strategy_1 = MultiWeightsChemenvStrategy(
dist_ang_area_weight=da_area_weight,
self_csm_weight=self_csm_weight,
delta_csm_weight=delta_weight,
cn_bias_weight=bias_weight,
angle_weight=angle_weight,
normalized_angle_distance_weight=nad_weight,
symmetry_measure_type=symmetry_measure_type,
)
multi_weights_strategy_2 = MultiWeightsChemenvStrategy(
dist_ang_area_weight=da_area_weight,
self_csm_weight=self_csm_weight,
delta_csm_weight=delta_weight,
cn_bias_weight=bias_weight_2,
angle_weight=angle_weight,
normalized_angle_distance_weight=nad_weight,
symmetry_measure_type=symmetry_measure_type,
)
multi_weights_strategy_3 = MultiWeightsChemenvStrategy(
dist_ang_area_weight=da_area_weight_2,
self_csm_weight=self_csm_weight,
delta_csm_weight=delta_weight,
cn_bias_weight=bias_weight,
angle_weight=angle_weight,
normalized_angle_distance_weight=nad_weight,
symmetry_measure_type=symmetry_measure_type,
)
multi_weights_strategy_1_from_dict = MultiWeightsChemenvStrategy.from_dict(multi_weights_strategy_1.as_dict())
assert multi_weights_strategy_1 == multi_weights_strategy_1_from_dict
assert simplest_strategy_1 != multi_weights_strategy_1
assert multi_weights_strategy_1 != multi_weights_strategy_2
assert multi_weights_strategy_1 != multi_weights_strategy_3
assert multi_weights_strategy_2 != multi_weights_strategy_3
def test_read_write_voronoi(self):
with open(f"{json_dir}/test_T--4_FePO4_icsd_4266.json", encoding="utf-8") as file:
dd = json.load(file)
struct = Structure.from_dict(dd["structure"])
valences = [site.specie.oxi_state for site in struct]
detailed_voronoi_container = DetailedVoronoiContainer(structure=struct, valences=valences)
with open(f"{self.tmp_path}/se.json", mode="w", encoding="utf-8") as file:
json.dump(detailed_voronoi_container.as_dict(), file)
with open(f"{self.tmp_path}/se.json", encoding="utf-8") as file:
dd = json.load(file)
detailed_voronoi_container2 = DetailedVoronoiContainer.from_dict(dd)
assert detailed_voronoi_container == detailed_voronoi_container2