Skip to content

Commit

Permalink
Added check_simulation_cell in utils
Browse files Browse the repository at this point in the history
Added testing
  • Loading branch information
JosePizarro3 committed Sep 13, 2024
1 parent 30192c0 commit ca090f8
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/nomad_simulations/schema_packages/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from .utils import (
RussellSaundersState,
check_simulation_cell,
get_composition,
get_sibling_section,
get_variables,
Expand Down
53 changes: 53 additions & 0 deletions src/nomad_simulations/schema_packages/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from nomad.datamodel.data import ArchiveSection
from structlog.stdlib import BoundLogger

from nomad_simulations.schema_packages.model_system import Cell


def get_sibling_section(
section: 'ArchiveSection',
Expand Down Expand Up @@ -168,3 +170,54 @@ def get_composition(children_names: 'list[str]') -> str:
children_count_tup = np.unique(children_names, return_counts=True)
formula = ''.join([f'{name}({count})' for name, count in zip(*children_count_tup)])
return formula if formula else None


def check_simulation_cell(cell_1: 'Cell', cell_2: 'Cell') -> bool:
"""
Check if the two `Cell` objects are the same by checking if the defined `positions` are all matching. If
the objects are `AtomicCell`, it checks if the `AtomsState[*].chemical_symbol` are the same.
Args:
cell_1 (Cell): The first `Cell` to compare.
cell_2 (Cell): The second `Cell` to compare.
Returns:
bool: True if the cells are the same, False otherwise.
"""
# If any of the cells is None or the positions are empty, return False
if cell_1 is None or cell_2 is None:
return False
if cell_1.positions is None or cell_2.positions is None:
return False

# The `positions` should have the same length (same number of positions)
if len(cell_1.positions) != len(cell_2.positions):
return False
n_positions = len(cell_1.positions)

# Check that all the `positions`` of `cell_1` match with the ones in `cell_2`
check_positions = []
for i1, pos1 in enumerate(cell_1.positions):
for i2, pos2 in enumerate(cell_2.positions):
if np.allclose(pos1, pos2):
check_positions.append([i1, i2])
break
if len(check_positions) != n_positions:
return False

# If the cells are not `AtomicCell`, finish function (using m_def.name to avoid circular import problems)
if cell_1.m_def.name == 'Cell' and cell_2.m_def.name == 'Cell':
return True
if cell_1.m_def.name != 'AtomicCell' or cell_2.m_def.name != 'AtomicCell':
return False

# Check that the `chemical_symbol` of the atoms in `cell_1` match with the ones in `cell_2`
try:
for atom in check_positions:
element_1 = cell_1.atoms_state[atom[0]].chemical_symbol
element_2 = cell_2.atoms_state[atom[1]].chemical_symbol
if element_1 != element_2:
return False
except Exception:
return False
return True
110 changes: 110 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

import pytest

from nomad_simulations.schema_packages.atoms_state import AtomsState
from nomad_simulations.schema_packages.model_system import (
AtomicCell,
Cell,
ModelSystem,
Symmetry,
)
from nomad_simulations.schema_packages.utils import (
check_simulation_cell,
get_sibling_section,
get_variables,
is_not_representative,
Expand Down Expand Up @@ -93,3 +96,110 @@ def test_get_variables(variables: list, result: list, result_length: int):
assert len(energies) == result_length
for i, energy in enumerate(energies): # asserting energies == result does not work
assert energy.n_points == result[i].n_points


@pytest.mark.parametrize(
'cell_1, cell_2, result',
[
(None, None, False), # both are None
(Cell(), None, False), # one cell is None
(Cell(), Cell(), False), # both cells are empty
(
Cell(positions=[[1, 0, 0]]),
Cell(),
False,
), # one cell has positions, the other is empty
(
Cell(positions=[[1, 0, 0], [0, 1, 0]]),
Cell(positions=[[1, 0, 0]]),
False,
), # length mismatch
(
Cell(positions=[[1, 0, 0], [0, 1, 0]]),
Cell(positions=[[1, 0, 0], [0, -1, 0]]),
False,
), # different positions
(
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
True,
), # same ordered positions
(
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 0, 1], [0, 1, 0]]),
True,
), # different ordered positions but same cell
(
AtomicCell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
Cell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
False,
), # one atomic cell and another cell (missing chemical symbols)
(
AtomicCell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
AtomicCell(positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
False,
), # missing chemical symbols
(
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
True,
), # same ordered positions and chemical symbols
(
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='Cu'),
AtomsState(chemical_symbol='O'),
],
),
False,
), # same ordered positions but different chemical symbols
(
AtomicCell(
positions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
],
),
AtomicCell(
positions=[[1, 0, 0], [0, 0, 1], [0, 1, 0]],
atoms_state=[
AtomsState(chemical_symbol='H'),
AtomsState(chemical_symbol='O'),
AtomsState(chemical_symbol='H'),
],
),
True,
), # different ordered positions but same chemical symbols
],
)
def test_check_simulation_cell(cell_1: Cell, cell_2: Cell, result: bool):
"""
Test the `check_simulation_cell` utility function.
"""
assert check_simulation_cell(cell_1=cell_1, cell_2=cell_2) == result

1 comment on commit ca090f8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/nomad_simulations
   __init__.py4250%3–4
   _version.py11282%5–6
src/nomad_simulations/schema_packages
   __init__.py14286%53–55
   atoms_state.py1902189%31–33, 219–222, 246, 301–302, 370–371, 373, 555, 567–568, 629–633, 648–652, 659
   basis_set.py2402888%8–9, 122–133, 172–185, 208, 391–395, 417–418, 462–465, 584, 615, 617
   general.py89891%22–25, 139, 203, 313–314, 324
   model_method.py2657771%28–30, 189–192, 195–202, 294–295, 315, 336–355, 371–397, 400–417, 771, 782, 824–831, 869, 888, 968, 1025, 1100, 1214
   model_system.py2612292%43–45, 520–523, 570–577, 751–752, 973–977, 983–984, 992–993, 998, 1021
   numerical_settings.py2596176%30–32, 235, 237–238, 241–244, 248–249, 256–259, 268–271, 275–278, 280–283, 288–291, 297–300, 487–514, 589, 624–627, 651, 654, 699, 701–704, 708, 712, 759, 763–784, 839–840, 907
   outputs.py1201092%27–28, 270–273, 313–316, 341, 343, 380, 399
   physical_property.py102793%38–40, 220, 349–351
   variables.py861286%26–28, 116, 139, 163, 185, 207, 229, 251, 274, 294
src/nomad_simulations/schema_packages/properties
   band_gap.py51590%26–28, 153–154
   band_structure.py1232580%27–29, 250–283, 296, 303, 339–340, 343, 390–391, 396
   energies.py42979%25–27, 54, 75, 100, 121, 137, 152
   fermi_surface.py17476%25–27, 58
   forces.py22673%26–28, 55, 75, 98
   greens_function.py991387%25–27, 228–229, 232, 253–254, 257, 278–279, 282, 418
   hopping_matrix.py29583%25–27, 76, 112
   permittivity.py48883%25–27, 115–123
   spectral_profile.py26012851%27–29, 75–78, 113–116, 217–318, 374–386, 411–414, 434, 439–442, 484–520, 544, 591–594, 610–611, 616–622
   thermodynamics.py752764%25–27, 53, 74, 90, 99, 108, 119, 128, 155, 165, 175, 190–192, 195, 211, 231–233, 236, 252, 272–274, 277
src/nomad_simulations/schema_packages/utils
   utils.py981585%26–31, 81–90, 99–100, 105, 108
TOTAL251649780% 

Tests Skipped Failures Errors Time
395 0 💤 0 ❌ 0 🔥 3.510s ⏱️

Please sign in to comment.