Skip to content

Commit 172cf24

Browse files
committed
Move testing logic to test file
1 parent 306800a commit 172cf24

2 files changed

Lines changed: 57 additions & 11 deletions

File tree

examples/advanced/ex09_align_ldos.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
left_truncate=True, right_truncate_value=11, number_of_electrons=4
3434
)
3535

36+
# The same thing works also with openPMD-based data
37+
3638
try:
3739
import openpmd_api
3840
use_openpmd = True
@@ -55,14 +57,5 @@
5557
left_truncate=True, right_truncate_value=11, number_of_electrons=4
5658
)
5759

58-
parameters = mala.Parameters()
59-
data_handler = mala.DataHandler(parameters)
60-
for i in range(1, 4):
61-
data_openpmd = data_handler.target_calculator.read_from_openpmd_file(
62-
f"{data_path}/aligned/Be_snapshot0.out.h5")
63-
data_numpy = data_handler.target_calculator.read_from_numpy_file(
64-
f"{data_path}/aligned/Be_snapshot0.out.npy")
65-
if not numpy.allclose(data_numpy, data_openpmd):
66-
raise Exception("Inconsistency in snapshot", i)
67-
else:
68-
print("All close.")
60+
# A test that checks for data equivalence between the Numpy-based
61+
# and openPMD-based implementations can be found under test/align_ldos.py.

test/splitting_test.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,56 @@ def test_dos_splitting(self):
8383
)
8484
)
8585
)
86+
87+
# This test is based on examples/advanced/ex09_align_ldos.py, but adds a
88+
# test to check that the aligned data is equivalent between the numpy-based
89+
# and the openPMD-based implementations.
90+
def test_ldos_alignment(self):
91+
parameters = mala.Parameters()
92+
parameters.targets.ldos_gridoffset_ev = -5
93+
parameters.targets.ldos_gridsize = 11
94+
parameters.targets.ldos_gridspacing_ev = 2.5
95+
96+
# initialize and add snapshots to workflow
97+
ldos_aligner = mala.LDOSAligner(parameters)
98+
ldos_aligner.clear_data()
99+
ldos_aligner.add_snapshot("Be_snapshot0.out.npy", data_path)
100+
ldos_aligner.add_snapshot("Be_snapshot1.out.npy", data_path)
101+
ldos_aligner.add_snapshot("Be_snapshot2.out.npy", data_path)
102+
103+
# align and cut the snapshots from the left and right-hand sides
104+
ldos_aligner.align_ldos_to_ref(
105+
left_truncate=True, right_truncate_value=11, number_of_electrons=4
106+
)
107+
108+
try:
109+
import openpmd_api
110+
use_openpmd = True
111+
except ImportError:
112+
use_openpmd = False
113+
114+
if use_openpmd:
115+
# initialize and add snapshots to workflow
116+
ldos_aligner = mala.LDOSAligner(parameters)
117+
ldos_aligner.clear_data()
118+
ldos_aligner.add_snapshot("Be_snapshot0.out.h5",
119+
data_path, snapshot_type='openpmd')
120+
ldos_aligner.add_snapshot("Be_snapshot1.out.h5",
121+
data_path, snapshot_type='openpmd')
122+
ldos_aligner.add_snapshot("Be_snapshot2.out.h5",
123+
data_path, snapshot_type='openpmd')
124+
125+
# align and cut the snapshots from the left and right-hand sides
126+
ldos_aligner.align_ldos_to_ref(
127+
left_truncate=True, right_truncate_value=11, number_of_electrons=4
128+
)
129+
130+
parameters = mala.Parameters()
131+
data_handler = mala.DataHandler(parameters)
132+
for i in range(1, 4):
133+
data_openpmd = data_handler.target_calculator.read_from_openpmd_file(
134+
f"{data_path}/aligned/Be_snapshot0.out.h5")
135+
data_numpy = data_handler.target_calculator.read_from_numpy_file(
136+
f"{data_path}/aligned/Be_snapshot0.out.npy")
137+
if not np.allclose(data_numpy, data_openpmd):
138+
raise Exception("Inconsistency in snapshot", i)

0 commit comments

Comments
 (0)