Skip to content

Commit 37f6426

Browse files
committed
get_cartesian_coordinates function and test
1 parent f339f4e commit 37f6426

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

src/diffpy/structure/structure.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def getLastAtom(self):
174174
last_atom = self[-1]
175175
return last_atom
176176

177-
def get_chemical_symbols(self, include_charge_state=False):
177+
def get_chemical_symbols(self):
178178
"""Return list of chemical symbols for all `Atoms` in this
179179
structure.
180180
@@ -194,7 +194,8 @@ def get_fractional_coordinates(self):
194194
Returns
195195
-------
196196
numpy.ndarray
197-
The array of fractional coordinates of all `Atoms` in this structure.
197+
The array of fractional coordinates of all `Atoms` in this structure
198+
in the same order as `Structure.get_chemical_symbols()`.
198199
"""
199200
coords = numpy.array([a.xyz for a in self])
200201
return coords
@@ -206,9 +207,12 @@ def get_cartesian_coordinates(self):
206207
Returns
207208
-------
208209
numpy.ndarray
209-
The array of Cartesian coordinates of all `Atoms` in this structure.
210+
The array of Cartesian coordinates of all `Atoms` in this structure
211+
in the same order as `Structure.get_chemical_symbols()`.
210212
"""
211213
cartn_coords = numpy.array([a.xyz_cartn for a in self])
214+
# round coordinates to avoid negative numbers close to zero
215+
cartn_coords = numpy.round(cartn_coords, 5)
212216
return cartn_coords
213217

214218
def get_anisotropic_displacement_parameters(self):

tests/test_structure.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,12 +634,22 @@ def test_get_fractional_coordinates(datafile):
634634
[0.5, 0.5, 0.0],
635635
]
636636
)
637-
assert actual_fractional_coords.tolist() == expected_fractional_coords.tolist()
637+
assert numpy.allclose(actual_fractional_coords, expected_fractional_coords)
638638

639639

640-
# def test_get_cartesian_coordinates(datafile):
641-
# """Check Structure.get_cartesian_coordinates()"""
642-
# assert False
640+
def test_get_cartesian_coordinates(datafile):
641+
"""Check Structure.get_cartesian_coordinates()"""
642+
cdse_stru = Structure(filename=datafile("CdSe_bulk.stru"))
643+
actual_cartesian_coords = cdse_stru.get_cartesian_coordinates()
644+
expected_cartesian_coords = numpy.array(
645+
[
646+
[1.22284, 2.1176, 0.0],
647+
[2.44495, 0.0, 3.45301],
648+
[1.22284, 2.1176, 2.60129],
649+
[2.44495, 0.0, 6.05431],
650+
]
651+
)
652+
assert numpy.allclose(actual_cartesian_coords, expected_cartesian_coords)
643653

644654

645655
# def test_get_anisotropic_displacement_parameters(datafile):

0 commit comments

Comments
 (0)