Skip to content

Commit

Permalink
Added testing for GeometricSpace
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed Mar 5, 2024
1 parent 510118b commit d9990dc
Showing 1 changed file with 144 additions and 19 deletions.
163 changes: 144 additions & 19 deletions tests/test_model_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
from . import logger

from nomad_simulations.model_system import (
GeometricSpace,
Cell,
AtomicCell,
Symmetry,
ChemicalFormula,
Expand All @@ -35,6 +33,33 @@


class TestAtomicCell:
"""
Test the `AtomicCell`, `Cell` and `GeometricSpace` classes defined in model_system.py
"""

@staticmethod
def atomic_cell(
lattice_vectors, positions, periodic_boundary_conditions, atoms, atomic_numbers
) -> AtomicCell:
# Define the atomic cell
atomic_cell = AtomicCell()
if lattice_vectors:
atomic_cell.lattice_vectors = lattice_vectors * ureg('angstrom')
if positions:
atomic_cell.positions = positions * ureg('angstrom')
if periodic_boundary_conditions:
atomic_cell.periodic_boundary_conditions = periodic_boundary_conditions

# Add the elements information
for index, atom in enumerate(atoms):
atom_state = AtomsState()
setattr(atom_state, 'chemical_symbol', atom)
atomic_number = atom_state.resolve_atomic_number(logger)
assert atomic_number == atomic_numbers[index]
atom_state.atomic_number = atomic_number
atomic_cell.atoms_state.append(atom_state)
return atomic_cell

@pytest.mark.parametrize(
'atoms, atomic_numbers, formula, lattice_vectors, positions, periodic_boundary_conditions',
[
Expand Down Expand Up @@ -89,23 +114,13 @@ def test_generate_ase_atoms(
positions,
periodic_boundary_conditions,
):
# Define the atomic cell
atomic_cell = AtomicCell()
if lattice_vectors:
atomic_cell.lattice_vectors = lattice_vectors * ureg('angstrom')
if positions:
atomic_cell.positions = positions * ureg('angstrom')
if periodic_boundary_conditions:
atomic_cell.periodic_boundary_conditions = periodic_boundary_conditions

# Add the elements information
for index, atom in enumerate(atoms):
atom_state = AtomsState()
setattr(atom_state, 'chemical_symbol', atom)
atomic_number = atom_state.resolve_atomic_number(logger)
assert atomic_number == atomic_numbers[index]
atom_state.atomic_number = atomic_number
atomic_cell.atoms_state.append(atom_state)
atomic_cell = self.atomic_cell(
lattice_vectors,
positions,
periodic_boundary_conditions,
atoms,
atomic_numbers,
)

# Test `to_ase_atoms` function
ase_atoms = atomic_cell.to_ase_atoms(logger)
Expand All @@ -120,3 +135,113 @@ def test_generate_ase_atoms(
assert (ase_atoms.pbc == periodic_boundary_conditions).all()
assert (ase_atoms.symbols.numbers == atomic_numbers).all()
assert ase_atoms.symbols.get_chemical_formula() == formula

@pytest.mark.parametrize(
'atoms, atomic_numbers, lattice_vectors, positions, vectors_results, angles_results, volume',
[
(
['H', 'H', 'O'],
[1, 1, 8],
[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
[[0, 0, 0], [0.5, 0.5, 0.5], [1, 1, 1]],
[1.0, 1.0, 1.0],
[90.0, 90.0, 90.0],
1.0,
), # full atomic cell
(
['H', 'H', 'O'],
[1, 1, 8],
[[1.2, 2.3, 0], [1.2, -2.3, 0], [0, 0, 1]],
[[0, 0, 0], [0.5, 0.5, 0.5], [1, 1, 1]],
[2.59422435, 2.59422435, 1.0],
[90.0, 90.0, 124.8943768],
5.52,
), # full atomic cell with different lattice_vectors
(
[],
[1, 1, 8],
[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
[[0, 0, 0], [0.5, 0.5, 0.5], [1, 1, 1]],
[None, None, None],
[None, None, None],
None,
), # missing chemical_symbols
(
['H', 'H', 'O'],
[1, 1, 8],
[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
[],
[None, None, None],
[None, None, None],
None,
), # missing positions
(
['H', 'H', 'O'],
[1, 1, 8],
[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
[[0, 0, 0], [0.5, 0.5, 0.5], [1, 1, 1], [2, 2, 2]],
[None, None, None],
[None, None, None],
None,
), # chemical_symbols and positions with different lengths
(
['H', 'H', 'O'],
[1, 1, 8],
[],
[[0, 0, 0], [0.5, 0.5, 0.5], [1, 1, 1]],
[0.0, 0.0, 0.0],
[90.0, 90.0, 90.0],
0.0,
), # missing lattice_vectors
],
)
def test_geometric_space(
self,
atoms,
atomic_numbers,
lattice_vectors,
positions,
vectors_results,
angles_results,
volume,
):
pbc = [False, False, False]
atomic_cell = self.atomic_cell(
lattice_vectors,
positions,
pbc,
atoms,
atomic_numbers,
)

# Get `GeometricSpace` quantities via normalization of `AtomicCell`
atomic_cell.normalize(None, logger)
# Testing lengths of cell vectors
for index, name in enumerate(
['length_vector_a', 'length_vector_b', 'length_vector_c']
):
quantity = getattr(atomic_cell, name)
if quantity is not None:
assert np.isclose(
quantity.to('angstrom').magnitude,
vectors_results[index],
)
else:
assert quantity == vectors_results[index]
# Testing angles between cell vectors
for index, name in enumerate(
['angle_vectors_b_c', 'angle_vectors_a_c', 'angle_vectors_a_b']
):
quantity = getattr(atomic_cell, name)
if quantity is not None:
assert np.isclose(
quantity.to('degree').magnitude,
angles_results[index],
)
else:
assert quantity == angles_results[index]
# Testing volume
if atomic_cell.volume is not None:
assert np.isclose(atomic_cell.volume.to('angstrom^3').magnitude, volume)
else:
assert atomic_cell.volume == volume

0 comments on commit d9990dc

Please sign in to comment.