Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

170 broken parsing of 0d systems in crystal #177

Merged
merged 9 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 23 additions & 42 deletions electronicparsers/crystal/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ def parse_output(self, filepath):
dtype=np.float64,
repeats=False,
),
Quantity(
"lattice_positions_raw",
fr'AT\.IRR\.\s+AT\s+AT\.N\.\s+X\s+Y\s+Z\s*{br}' +\
fr'((?:\s+{integer}\s+{integer}\s+{integer}\s+{flt}\s+{flt}\s+{flt}{br})+)',
shape=(-1, 6),
Copy link
Collaborator

Choose a reason for hiding this comment

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

first time I have seen shape -1, good to know.

Copy link
Contributor Author

@ndaelman-hu ndaelman-hu Nov 3, 2023

Choose a reason for hiding this comment

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

I forgot that I left this capturing group in... It didn't work, but I fixed it now. Still, it isn't used anywhere atm.

dtype=str,
),
Quantity(
"labels_positions",
fr'\s+ATOM\s+X(?:/A|\(ANGSTROM\))\s+Y(?:/B|\(ANGSTROM\))\s+Z(?:/C|\(ANGSTROM\))\s*{br}' +\
Expand Down Expand Up @@ -608,20 +615,18 @@ def parse(self, filepath, archive, logger):
# System. There are several alternative sources for this information
# depending on the run type.
system = run.m_create(System)
material_type = out["material_type"]
system_edited = out["system_edited"]
labels_positions = out["labels_positions"]
lattice_vectors_restart = out["lattice_vectors_restart"]
pbc = None if material_type == "MOLECULAR CALCULATION" else np.array([True, True, True])
dimensionality = out["dimensionality"]
if dimensionality == 0:
pbc = np.array([False] * 3)
ndaelman-hu marked this conversation as resolved.
Show resolved Hide resolved
else:
pbc = np.array([True] * 3)

# By default the system is read from the configuration at the beginning
# of the file: it may come from restart or clean start
atomic_numbers = None
pos_type = {
"MOLECULAR CALCULATION": "cartesian",
"SLAB CALCULATION": "slab",
None: "scaled",
}.get(material_type)
if labels_positions is not None:
atomic_numbers = labels_positions[:, 2] # pylint: disable=E1136
atom_labels = labels_positions[:, 3] # pylint: disable=E1136
Expand All @@ -633,14 +638,12 @@ def parse(self, filepath, archive, logger):
atom_labels = labels_positions[:, 2] # pylint: disable=E1136
atom_pos = labels_positions[:, 4:7] # pylint: disable=E1136
lattice = lattice_vectors_restart
pos_type = "cartesian"

# If any geometry edits (supercells, substitutions, dispplacements,
# deformations, nanotube construction, etc.) are done on top of the
# original system, they override the original system.
if system_edited is not None:
if system_edited["labels_positions_nanotube"] is not None: # pylint: disable=E1136
pos_type = "nanotube"
labels_positions = system_edited["labels_positions_nanotube"] # pylint: disable=E1136
else:
labels_positions = system_edited["labels_positions"] # pylint: disable=E1136
Expand All @@ -662,13 +665,12 @@ def parse(self, filepath, archive, logger):
atom_labels,
atom_pos,
lattice,
pos_type=pos_type,
dimensionality,
)

system.atoms = Atoms(
lattice_vectors=lattice_vectors, periodic=pbc, positions=cart_pos,
species=atomic_numbers, labels=atom_labels)
dimensionality = out["dimensionality"]
system.x_crystal_dimensionality = dimensionality
crystal_family = out["crystal_family"]
system.x_crystal_family = crystal_family
Expand Down Expand Up @@ -948,7 +950,7 @@ def add_functionals(functionals):
i_atom_labels,
i_atom_pos,
i_lattice_parameters,
pos_type,
dimensionality,
)
i_system.atoms = Atoms(
species=i_atomic_numbers, labels=i_atom_labels, positions=i_cart_pos,
Expand Down Expand Up @@ -1006,7 +1008,7 @@ def to_k_points(segments):
return all_k_points


def to_system(atomic_numbers, labels, positions, lattice, pos_type="scaled", wrap=False):
def to_system(atomic_numbers, labels, positions, lattice, dimensionality):
"""Converts a Crystal-specific structure format into cartesian positions
and lattice vectors (if present). The conversion depends on the material
type.
Expand All @@ -1016,43 +1018,22 @@ def to_system(atomic_numbers, labels, positions, lattice, pos_type="scaled", wra
positions = positions.astype(np.float64)

# Get the lattice vectors
lattice_vectors = None
if lattice is not None:
if lattice.shape == (6,):
lattice_vectors = atomutils.cellpar_to_cell(lattice, degrees=True)
elif lattice.shape == (3, 3):
lattice_vectors = lattice
else:
lattice_vectors = None

# Convert positions based on the given type
if pos_type == "cartesian":
if lattice_vectors is not None and wrap:
cart_pos = atomutils.wrap_positions(positions, lattice_vectors)
else:
cart_pos = positions
elif pos_type == "slab":
n_atoms = atomic_numbers.shape[0]
scaled_pos = np.zeros((n_atoms, 3), dtype=np.float64)
scaled_pos[:, 0:2] = positions[:, 0:2]
if wrap:
wrapped_pos = atomutils.wrap_positions(scaled_pos)
else:
wrapped_pos = scaled_pos
cart_pos = atomutils.to_cartesian(wrapped_pos, lattice_vectors)
cart_pos[:, 2:3] = positions[:, 2:3]
elif pos_type == "nanotube":
n_atoms = atomic_numbers.shape[0]
scaled_pos = np.zeros((n_atoms, 3), dtype=np.float64)
scaled_pos[:, 0:1] = positions[:, 0:1]
if wrap:
wrapped_pos = atomutils.wrap_positions(scaled_pos)
else:
wrapped_pos = scaled_pos
cart_pos = atomutils.to_cartesian(wrapped_pos, lattice_vectors)
cart_pos[:, 1:3] = positions[:, 1:3]
elif pos_type == "scaled":
scaled_pos = atomutils.wrap_positions(positions) if wrap else positions
n_atoms = atomic_numbers.shape[0]
scaled_pos = np.zeros((n_atoms, 3), dtype=np.float64)
scaled_pos[:, :dimensionality] = positions[:, :dimensionality]
if lattice_vectors is not None:
cart_pos = atomutils.to_cartesian(scaled_pos, lattice_vectors)
cart_pos[:, dimensionality:] = positions[:, dimensionality:]
else:
cart_pos = scaled_pos

if lattice_vectors is not None:
lattice_vectors *= ureg.angstrom
Expand Down
5 changes: 4 additions & 1 deletion tests/test_crystalparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,10 @@ def asserts_basic(archive, method_type="DFT", system_type="3D", vdw=None, forces
if system_type != "0D":
assert system.atoms.lattice_vectors is not None
assert system.atoms.lattice_vectors.shape == (3, 3)
assert system.atoms.periodic == [True, True, True]
assert system.atoms.periodic == [True] * 3
else:
assert system.atoms.lattice_vectors is None
assert system.atoms.periodic == [False] * 3
assert system.atoms.positions.shape[0] == n_atoms
assert system.atoms.species.shape[0] == n_atoms
assert len(system.atoms.labels) == n_atoms
Expand Down
Loading