Coverage for ase/io/lammpsdata.py: 94.79%
384 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1"""IO for LAMMPS data files."""
3from __future__ import annotations
5import re
6import warnings
7from dataclasses import dataclass, field
9import numpy as np
11from ase.atoms import Atoms
12from ase.calculators.lammps import Prism, convert
13from ase.data import atomic_masses, atomic_numbers
14from ase.utils import reader, writer
17def _make_cell(box):
18 cell = np.zeros((3, 3))
19 celldisp = np.zeros(3)
20 if 'avec' in box:
21 cell[0] = box['avec']
22 cell[1] = box['bvec']
23 cell[2] = box['cvec']
24 celldisp = box['abc origin']
25 else:
26 cell[0, 0] = box['xhi'] - box['xlo']
27 cell[1, 1] = box['yhi'] - box['ylo']
28 cell[2, 2] = box['zhi'] - box['zlo']
29 cell[1, 0] = box['xy']
30 cell[2, 0] = box['xz']
31 cell[2, 1] = box['yz']
32 return cell, celldisp
35@reader
36def read_lammps_data(
37 fileobj,
38 Z_of_type: dict | None = None,
39 sort_by_id: bool = True,
40 read_image_flags: bool = True,
41 units: str = 'metal',
42 atom_style: str | None = None,
43 style: str | None = None,
44):
45 """Method which reads a LAMMPS data file.
47 Parameters
48 ----------
49 fileobj : file | str
50 File from which data should be read.
51 Z_of_type : dict[int, int], optional
52 Mapping from LAMMPS atom types (typically starting from 1) to atomic
53 numbers. If None, if there is the "Masses" section, atomic numbers are
54 guessed from the atomic masses. Otherwise, atomic numbers of 1 (H), 2
55 (He), etc. are assigned to atom types of 1, 2, etc. Default is None.
56 sort_by_id : bool, optional
57 Order the particles according to their id. Might be faster to set it
58 False. Default is True.
59 read_image_flags: bool, default True
60 If True, the lattice translation vectors derived from image flags are
61 added to atomic positions.
62 units : str, optional
63 `LAMMPS units <https://docs.lammps.org/units.html>`__. Default is
64 'metal'.
65 atom_style : {'atomic', 'charge', 'full'} etc., optional
66 `LAMMPS atom style <https://docs.lammps.org/atom_style.html>`__.
67 If None, `atom_style` is guessed in the following priority (1) comment
68 after `Atoms` (2) length of fields (valid only `atomic` and `full`).
69 Default is None.
70 """
71 if style is not None:
72 warnings.warn(
73 FutureWarning('"style" is deprecated; please use "atom_style".'),
74 )
75 atom_style = style
76 # begin read_lammps_data
77 file_comment = next(fileobj).rstrip()
79 # default values (https://docs.lammps.org/read_data.html)
80 # in most cases these will be updated below
81 natoms = 0
82 # N_types = 0
83 box: dict[str, float | list[float]] = {
84 'xlo': -0.5,
85 'xhi': +0.5,
86 'ylo': -0.5,
87 'yhi': +0.5,
88 'zlo': -0.5,
89 'zhi': +0.5,
90 'xy': 0.0,
91 'xz': 0.0,
92 'yz': 0.0,
93 }
95 mass_in = {}
96 vel_in = {}
97 atom_type_labels = {}
98 bonds_in = []
99 angles_in = []
100 dihedrals_in = []
102 sections = [
103 'Atoms',
104 'Velocities',
105 'Masses',
106 'Charges',
107 'Ellipsoids',
108 'Lines',
109 'Triangles',
110 'Bodies',
111 'Bonds',
112 'Angles',
113 'Dihedrals',
114 'Impropers',
115 'Impropers Pair Coeffs',
116 'PairIJ Coeffs',
117 'Pair Coeffs',
118 'Bond Coeffs',
119 'Angle Coeffs',
120 'Dihedral Coeffs',
121 'Improper Coeffs',
122 'BondBond Coeffs',
123 'BondAngle Coeffs',
124 'MiddleBondTorsion Coeffs',
125 'EndBondTorsion Coeffs',
126 'AngleTorsion Coeffs',
127 'AngleAngleTorsion Coeffs',
128 'BondBond13 Coeffs',
129 'AngleAngle Coeffs',
130 'Atom Type Labels',
131 'Bond Type Labels',
132 'Angle Type Labels',
133 'Dihedral Type Labels',
134 'Improper Type Labels',
135 ]
136 header_fields = [
137 'atoms',
138 'bonds',
139 'angles',
140 'dihedrals',
141 'impropers',
142 'atom types',
143 'bond types',
144 'angle types',
145 'dihedral types',
146 'improper types',
147 'extra bond per atom',
148 'extra angle per atom',
149 'extra dihedral per atom',
150 'extra improper per atom',
151 'extra special per atom',
152 'ellipsoids',
153 'lines',
154 'triangles',
155 'bodies',
156 ]
157 header_fields_restricted_box = [
158 'xlo xhi',
159 'ylo yhi',
160 'zlo zhi',
161 'xy xz yz',
162 ]
163 header_fields_general_box = [
164 'avec',
165 'bvec',
166 'cvec',
167 'abc origin',
168 ]
169 header_fields += header_fields_restricted_box + header_fields_general_box
170 sections_re = '(' + '|'.join(sections).replace(' ', '\\s+') + ')'
171 header_fields_re = '(' + '|'.join(header_fields).replace(' ', '\\s+') + ')'
173 section = None
174 header = True
175 for line in fileobj:
176 # get string after #; if # does not exist, return ''
177 line_comment = re.sub(r'^.*#|^.*$', '', line).strip()
178 line = re.sub('#.*', '', line).rstrip().lstrip()
179 if re.match('^\\s*$', line): # skip blank lines
180 continue
182 # check for known section names
183 match = re.match(sections_re, line)
184 if match is not None:
185 section = match.group(0).rstrip().lstrip()
186 header = False
187 if section == 'Atoms': # id *
188 # guess `atom_style` from the comment after `Atoms` if exists
189 if atom_style is None and line_comment != '':
190 atom_style = line_comment
191 atoms_section = _read_atoms_section(fileobj, natoms, atom_style)
192 continue
194 if header:
195 field = None
196 val = None
197 match = re.match('(.*)\\s+' + header_fields_re, line)
198 if match is not None:
199 field = match.group(2).lstrip().rstrip()
200 val = match.group(1).lstrip().rstrip()
201 if field is not None and val is not None:
202 if field == 'atoms':
203 natoms = int(val)
204 elif field in header_fields_restricted_box:
205 keys = field.split()
206 values = (float(x) for x in val.split())
207 box.update(dict(zip(keys, values)))
208 elif field in header_fields_general_box:
209 box[field] = [float(x) for x in val.split()]
211 if section is not None:
212 fields = line.split()
213 if section == 'Velocities': # id vx vy vz
214 vel_in[int(fields[0])] = [float(fields[_]) for _ in (1, 2, 3)]
215 elif section == 'Masses':
216 mass_in[int(fields[0])] = float(fields[1])
217 elif section == 'Atom Type Labels':
218 atom_type_labels[int(fields[0])] = fields[1]
219 elif section == 'Bonds': # id type atom1 atom2
220 bonds_in.append([int(fields[_]) for _ in (1, 2, 3)])
221 elif section == 'Angles': # id type atom1 atom2 atom3
222 angles_in.append([int(fields[_]) for _ in (1, 2, 3, 4)])
223 elif section == 'Dihedrals': # id type atom1 atom2 atom3 atom4
224 dihedrals_in.append([int(fields[_]) for _ in (1, 2, 3, 4, 5)])
226 # set cell
227 cell, celldisp = _make_cell(box)
229 if sort_by_id:
230 atoms_section.sort()
232 ids = atoms_section.ids
234 if np.all(atoms_section.types != 0): # numeric
235 types = atoms_section.types
236 else: # labels
237 labels2types = {v: k for k, v in atom_type_labels.items()}
238 types = np.array([labels2types[_] for _ in atoms_section.labels])
240 if Z_of_type:
241 # The user-specified `Z_of_type` has the highest priority.
242 numbers = np.array([Z_of_type[_] for _ in types])
243 elif atom_type_labels and all(
244 v in atomic_numbers for v in atom_type_labels.values()
245 ):
246 # if all the labels in the `Atom Type Labels` section are element names
247 numbers = np.array([atomic_numbers[atom_type_labels[_]] for _ in types])
248 else:
249 numbers = types
251 masses = np.array([mass_in[_] for _ in types]) if mass_in else None
252 velocities = np.array([vel_in[_] for _ in ids]) if vel_in else None
254 # convert units
255 positions = convert(atoms_section.positions, 'distance', units, 'ASE')
256 cell = convert(cell, 'distance', units, 'ASE')
257 if masses is not None:
258 masses = convert(masses, 'mass', units, 'ASE')
259 if velocities is not None:
260 velocities = convert(velocities, 'velocity', units, 'ASE')
262 # guess atomic numbers from atomic masses
263 # this must be after the above mass-unit conversion
264 if Z_of_type is None and masses is not None:
265 numbers = _masses2numbers(masses)
267 # create ase.Atoms
268 atoms = Atoms(
269 positions=positions,
270 numbers=numbers,
271 masses=masses,
272 cell=cell,
273 pbc=[True, True, True],
274 celldisp=celldisp,
275 )
277 # add lattice translation vectors
278 if read_image_flags:
279 scaled_positions = atoms.get_scaled_positions(wrap=False)
280 atoms.set_scaled_positions(scaled_positions + atoms_section.cell_ids)
282 # set velocities (can't do it via constructor)
283 if velocities is not None:
284 atoms.set_velocities(velocities)
286 atoms.arrays['id'] = atoms_section.ids
287 atoms.arrays['type'] = atoms_section.types
288 if not np.all(atoms_section.mol_ids == 0):
289 atoms.arrays['mol-id'] = atoms_section.mol_ids
290 if not np.all(np.isnan(atoms_section.charges)):
291 atoms.arrays['initial_charges'] = atoms_section.charges
292 atoms.arrays['mmcharges'] = atoms_section.charges.copy()
294 # mapping from LAMMPS atom-IDs to ASE Atoms IDs
295 mapping = {atom_id: i for i, atom_id in enumerate(atoms_section.ids)}
297 if bonds_in:
298 key = 'bonds'
299 atoms.arrays[key] = _parse_bonds(bonds_in, natoms, mapping)
301 if angles_in:
302 key = 'angles'
303 atoms.arrays[key] = _parse_angles(angles_in, natoms, mapping)
305 if dihedrals_in:
306 key = 'dihedrals'
307 atoms.arrays[key] = _parse_dihedrals(dihedrals_in, natoms, mapping)
309 atoms.info['comment'] = file_comment
311 return atoms
314@dataclass
315class _AtomsSection:
316 natoms: int
317 ids: np.ndarray = field(init=False)
318 types: np.ndarray = field(init=False)
319 labels: np.ndarray = field(init=False)
320 positions: np.ndarray = field(init=False)
321 mol_ids: np.ndarray = field(init=False)
322 charges: np.ndarray = field(init=False)
323 cell_ids: np.ndarray = field(init=False)
325 def __post_init__(self):
326 self.ids = np.zeros(self.natoms, int)
327 self.types = np.zeros(self.natoms, int)
328 self.labels = np.zeros(self.natoms, object)
329 self.positions = np.zeros((self.natoms, 3), float)
330 self.mol_ids = np.zeros(self.natoms, int)
331 self.charges = np.full(self.natoms, np.nan, float)
332 self.cell_ids = np.zeros((self.natoms, 3), int)
334 def sort(self):
335 """Sort IDs."""
336 args = np.argsort(self.ids)
337 self.ids = self.ids[args]
338 self.types = self.types[args]
339 self.positions = self.positions[args]
340 self.mol_ids = self.mol_ids[args]
341 self.charges = self.charges[args]
342 self.cell_ids = self.cell_ids[args]
345def _read_atoms_section(fileobj, natoms: int, style: str | None = None):
346 data = _AtomsSection(natoms)
347 next(fileobj) # skip blank line just after `Atoms`
348 for i in range(natoms):
349 line = next(fileobj)
350 line = re.sub(r'#.*', '', line).rstrip().lstrip()
351 fields = line.split()
352 if style is None:
353 style = _guess_atom_style(fields)
354 style_orig = style
355 if style.endswith('/kk'):
356 style = style[:-3] # remove Kokkos suffix
357 data.ids[i] = int(fields[0])
358 if style == 'full' and len(fields) in (7, 10):
359 # id mol-id type q x y z [tx ty tz]
360 data.labels[i] = fields[2]
361 data.positions[i] = tuple(float(fields[_]) for _ in (4, 5, 6))
362 data.mol_ids[i] = int(fields[1])
363 data.charges[i] = float(fields[3])
364 if len(fields) == 10:
365 data.cell_ids[i] = tuple(int(fields[_]) for _ in (7, 8, 9))
366 elif style == 'atomic' and len(fields) in (5, 8):
367 # id type x y z [tx ty tz]
368 data.labels[i] = fields[1]
369 data.positions[i] = tuple(float(fields[_]) for _ in (2, 3, 4))
370 if len(fields) == 8:
371 data.cell_ids[i] = tuple(int(fields[_]) for _ in (5, 6, 7))
372 elif style in ('angle', 'bond', 'molecular') and len(fields) in (6, 9):
373 # id mol-id type x y z [tx ty tz]
374 data.labels[i] = fields[2]
375 data.positions[i] = tuple(float(fields[_]) for _ in (3, 4, 5))
376 data.mol_ids[i] = int(fields[1])
377 if len(fields) == 9:
378 data.cell_ids[i] = tuple(int(fields[_]) for _ in (6, 7, 8))
379 elif style == 'charge' and len(fields) in (6, 9):
380 # id type q x y z [tx ty tz]
381 data.labels[i] = fields[1]
382 data.positions[i] = tuple(float(fields[_]) for _ in (3, 4, 5))
383 data.charges[i] = float(fields[2])
384 if len(fields) == 9:
385 data.cell_ids[i] = tuple(int(fields[_]) for _ in (6, 7, 8))
386 else:
387 raise RuntimeError(
388 f'Style "{style_orig}" not supported or invalid. '
389 f'Number of fields: {len(fields)}'
390 )
391 if all(_.isdigit() for _ in data.labels):
392 data.types = data.labels.astype(int)
393 return data
396def _guess_atom_style(fields):
397 """Guess `atom_sytle` from the length of fields."""
398 if len(fields) in (5, 8):
399 return 'atomic'
400 if len(fields) in (7, 10):
401 return 'full'
402 raise ValueError('atom_style cannot be guessed from len(fields)')
405def _masses2numbers(masses):
406 """Guess atomic numbers from atomic masses."""
407 return np.argmin(np.abs(atomic_masses - masses[:, None]), axis=1)
410def _parse_bonds(bonds_in, natoms: int, mapping: dict):
411 bonds = [''] * natoms
412 for bond_type, at1, at2 in bonds_in:
413 i_a1 = mapping[at1]
414 i_a2 = mapping[at2]
415 if len(bonds[i_a1]) > 0:
416 bonds[i_a1] += ','
417 bonds[i_a1] += f'{i_a2:d}({bond_type:d})'
418 for i, bond in enumerate(bonds):
419 if len(bond) == 0:
420 bonds[i] = '_'
421 return np.array(bonds)
424def _parse_angles(angles_in, natoms: int, mapping: dict):
425 angles = [''] * natoms
426 for angle_type, at1, at2, at3 in angles_in:
427 i_a1 = mapping[at1]
428 i_a2 = mapping[at2]
429 i_a3 = mapping[at3]
430 if len(angles[i_a2]) > 0:
431 angles[i_a2] += ','
432 angles[i_a2] += f'{i_a1:d}-{i_a3:d}({angle_type:d})'
433 for i, angle in enumerate(angles):
434 if len(angle) == 0:
435 angles[i] = '_'
436 return np.array(angles)
439def _parse_dihedrals(dihedrals_in, natoms: int, mapping: dict):
440 dihedrals = [''] * natoms
441 for dihedral_type, at1, at2, at3, at4 in dihedrals_in:
442 i_a1 = mapping[at1]
443 i_a2 = mapping[at2]
444 i_a3 = mapping[at3]
445 i_a4 = mapping[at4]
446 if len(dihedrals[i_a1]) > 0:
447 dihedrals[i_a1] += ','
448 dihedrals[i_a1] += f'{i_a2:d}-{i_a3:d}-{i_a4:d}({dihedral_type:d})'
449 for i, dihedral in enumerate(dihedrals):
450 if len(dihedral) == 0:
451 dihedrals[i] = '_'
452 return np.array(dihedrals)
455@writer
456def write_lammps_data(
457 fd,
458 atoms: Atoms,
459 *,
460 specorder: list | None = None,
461 reduce_cell: bool = False,
462 force_skew: bool = False,
463 prismobj: Prism = None,
464 write_image_flags: bool = False,
465 masses: bool = False,
466 velocities: bool = False,
467 atom_type_labels: bool = False,
468 units: str = 'metal',
469 bonds: bool = True,
470 atom_style: str = 'atomic',
471):
472 """Write atomic structure data to a LAMMPS data file.
474 Parameters
475 ----------
476 fd : file|str
477 File to which the output will be written.
478 atoms : Atoms
479 Atoms to be written.
480 specorder : list[str], optional
481 Chemical symbols in the order of LAMMPS atom types, by default None
482 force_skew : bool, optional
483 Force to write the cell as a
484 `triclinic <https://docs.lammps.org/Howto_triclinic.html>`__ box,
485 by default False
486 reduce_cell : bool, optional
487 Whether the cell shape is reduced or not, by default False
488 prismobj : Prism|None, optional
489 Prism, by default None
490 write_image_flags : bool, default False
491 If True, the image flags, i.e., in which images of the periodic
492 simulation box the atoms are, are written.
493 masses : bool, optional
494 Whether the atomic masses are written or not, by default False
495 velocities : bool, optional
496 Whether the atomic velocities are written or not, by default False
497 atom_type_labels : bool, optional
498 Whether the atom type labels are written or not, by default False
499 units : str, optional
500 `LAMMPS units <https://docs.lammps.org/units.html>`__,
501 by default 'metal'
502 bonds : bool, optional
503 Whether the bonds are written or not. Bonds can only be written
504 for atom_style='full', by default True
505 atom_style : {'atomic', 'charge', 'full'}, optional
506 `LAMMPS atom style <https://docs.lammps.org/atom_style.html>`__,
507 by default 'atomic'
508 """
509 # FIXME: We should add a check here that the encoding of the file object
510 # is actually ascii once the 'encoding' attribute of IOFormat objects
511 # starts functioning in implementation (currently it doesn't do
512 # anything).
514 if isinstance(atoms, list):
515 if len(atoms) > 1:
516 raise ValueError(
517 'Can only write one configuration to a lammps data file!'
518 )
519 atoms = atoms[0]
521 fd.write('(written by ASE)\n\n')
523 symbols = atoms.get_chemical_symbols()
524 n_atoms = len(symbols)
525 fd.write(f'{n_atoms} atoms\n')
527 if specorder is not None:
528 # To index elements in the LAMMPS data file
529 # (indices must correspond to order in the potential file)
530 species = specorder
531 elif 'type' in atoms.arrays:
532 species = _get_symbols_by_types(atoms)
533 else:
534 # This way it is assured that LAMMPS atom types are always
535 # assigned predictably according to the alphabetic order
536 species = sorted(set(symbols))
538 n_atom_types = len(species)
539 fd.write(f'{n_atom_types} atom types\n\n')
541 bonds_in = []
542 if (
543 bonds
544 and (atom_style == 'full')
545 and (atoms.arrays.get('bonds') is not None)
546 ):
547 n_bonds = 0
548 n_bond_types = 1
549 for i, bondsi in enumerate(atoms.arrays['bonds']):
550 if bondsi != '_':
551 for bond in bondsi.split(','):
552 dummy1, dummy2 = bond.split('(')
553 bond_type = int(dummy2.split(')')[0])
554 at1 = int(i) + 1
555 at2 = int(dummy1) + 1
556 bonds_in.append((bond_type, at1, at2))
557 n_bonds = n_bonds + 1
558 if bond_type > n_bond_types:
559 n_bond_types = bond_type
560 fd.write(f'{n_bonds} bonds\n')
561 fd.write(f'{n_bond_types} bond types\n\n')
563 if prismobj is None:
564 prismobj = Prism(
565 atoms.get_cell(),
566 pbc=atoms.get_pbc(),
567 reduce_cell=reduce_cell,
568 )
570 # Get cell parameters and convert from ASE units to LAMMPS units
571 xhi, yhi, zhi, xy, xz, yz = convert(
572 prismobj.get_lammps_prism(), 'distance', 'ASE', units
573 )
575 fd.write(f'0.0 {xhi:23.17g} xlo xhi\n')
576 fd.write(f'0.0 {yhi:23.17g} ylo yhi\n')
577 fd.write(f'0.0 {zhi:23.17g} zlo zhi\n')
579 if force_skew or prismobj.is_skewed():
580 fd.write(f'{xy:23.17g} {xz:23.17g} {yz:23.17g} xy xz yz\n')
582 if atom_type_labels:
583 _write_atom_type_labels(fd, species)
585 if masses:
586 _write_masses(fd, atoms, species, units)
588 # Write (unwrapped) atomic positions. If wrapping of atoms back into the
589 # cell along periodic directions is desired, this should be done manually
590 # on the Atoms object itself beforehand.
591 fd.write(f'\nAtoms # {atom_style}\n\n')
593 if write_image_flags:
594 scaled_positions = atoms.get_scaled_positions(wrap=False)
595 image_flags = np.floor(scaled_positions).astype(int)
597 # when `write_image_flags` is True, the positions are wrapped while the
598 # unwrapped positions can be recovered from the image flags
599 pos = prismobj.vector_to_lammps(
600 atoms.get_positions(),
601 wrap=write_image_flags,
602 )
604 types = _get_types(atoms, species)
606 if atom_style == 'atomic':
607 # Convert position from ASE units to LAMMPS units
608 pos = convert(pos, 'distance', 'ASE', units)
609 for i, r in enumerate(pos):
610 s = types[i]
611 line = (
612 f'{i + 1:>6} {s:>3} {r[0]:23.17g} {r[1]:23.17g} {r[2]:23.17g}'
613 )
614 if write_image_flags:
615 img = image_flags[i]
616 line += f' {img[0]:6d} {img[1]:6d} {img[2]:6d}'
617 line += '\n'
618 fd.write(line)
619 elif atom_style == 'charge':
620 charges = atoms.get_initial_charges()
621 # Convert position and charge from ASE units to LAMMPS units
622 pos = convert(pos, 'distance', 'ASE', units)
623 charges = convert(charges, 'charge', 'ASE', units)
624 for i, (q, r) in enumerate(zip(charges, pos)):
625 s = types[i]
626 line = (
627 f'{i + 1:>6} {s:>3} {q:>5}'
628 f' {r[0]:23.17g} {r[1]:23.17g} {r[2]:23.17g}'
629 )
630 if write_image_flags:
631 img = image_flags[i]
632 line += f' {img[0]:6d} {img[1]:6d} {img[2]:6d}'
633 line += '\n'
634 fd.write(line)
635 elif atom_style == 'full':
636 charges = atoms.get_initial_charges()
637 # The label 'mol-id' has apparenlty been introduced in read earlier,
638 # but so far not implemented here. Wouldn't a 'underscored' label
639 # be better, i.e. 'mol_id' or 'molecule_id'?
640 if atoms.has('mol-id'):
641 molecules = atoms.get_array('mol-id')
642 if not np.issubdtype(molecules.dtype, np.integer):
643 raise TypeError(
644 f'If "atoms" object has "mol-id" array, then '
645 f'mol-id dtype must be subtype of np.integer, and '
646 f'not {molecules.dtype!s:s}.'
647 )
648 if (len(molecules) != len(atoms)) or (molecules.ndim != 1):
649 raise TypeError(
650 'If "atoms" object has "mol-id" array, then '
651 'each atom must have exactly one mol-id.'
652 )
653 else:
654 # Assigning each atom to a distinct molecule id would seem
655 # preferableabove assigning all atoms to a single molecule
656 # id per default, as done within ase <= v 3.19.1. I.e.,
657 # molecules = np.arange(start=1, stop=len(atoms)+1,
658 # step=1, dtype=int) However, according to LAMMPS default
659 # behavior,
660 molecules = np.zeros(len(atoms), dtype=int)
661 # which is what happens if one creates new atoms within LAMMPS
662 # without explicitly taking care of the molecule id.
663 # Quote from docs at https://lammps.sandia.gov/doc/read_data.html:
664 # The molecule ID is a 2nd identifier attached to an atom.
665 # Normally, it is a number from 1 to N, identifying which
666 # molecule the atom belongs to. It can be 0 if it is a
667 # non-bonded atom or if you don't care to keep track of molecule
668 # assignments.
670 # Convert position and charge from ASE units to LAMMPS units
671 pos = convert(pos, 'distance', 'ASE', units)
672 charges = convert(charges, 'charge', 'ASE', units)
673 for i, (m, q, r) in enumerate(zip(molecules, charges, pos)):
674 s = types[i]
675 line = (
676 f'{i + 1:>6} {m:>3} {s:>3} {q:>5}'
677 f' {r[0]:23.17g} {r[1]:23.17g} {r[2]:23.17g}'
678 )
679 if write_image_flags:
680 img = image_flags[i]
681 line += f' {img[0]:6d} {img[1]:6d} {img[2]:6d}'
682 line += '\n'
683 fd.write(line)
684 if bonds and (atoms.arrays.get('bonds') is not None):
685 fd.write('\nBonds\n\n')
686 for i in range(n_bonds):
687 bond_type = bonds_in[i][0]
688 at1 = bonds_in[i][1]
689 at2 = bonds_in[i][2]
690 fd.write(f'{i + 1:>3} {bond_type:>3} {at1:>3} {at2:>3}\n')
691 else:
692 raise ValueError(atom_style)
694 if velocities and atoms.get_velocities() is not None:
695 fd.write('\nVelocities\n\n')
696 vel = prismobj.vector_to_lammps(atoms.get_velocities())
697 # Convert velocity from ASE units to LAMMPS units
698 vel = convert(vel, 'velocity', 'ASE', units)
699 for i, v in enumerate(vel):
700 fd.write(f'{i + 1:>6} {v[0]:23.17g} {v[1]:23.17g} {v[2]:23.17g}\n')
702 fd.flush()
705def _write_masses(fd, atoms: Atoms, species: list, units: str):
706 symbols_indices = atoms.symbols.indices()
707 fd.write('\nMasses\n\n')
708 for i, s in enumerate(species):
709 if s in symbols_indices:
710 # Find the first atom of the element `s` and extract its mass
711 # Cover by `float` to make a new object for safety
712 mass = float(atoms[symbols_indices[s][0]].mass)
713 else:
714 # Fetch from ASE data if the element `s` is not in the system
715 mass = atomic_masses[atomic_numbers[s]]
716 # Convert mass from ASE units to LAMMPS units
717 mass = convert(mass, 'mass', 'ASE', units)
718 atom_type = i + 1
719 fd.write(f'{atom_type} {mass:23.17g} # {s}\n')
722def _write_atom_type_labels(fd, species: list[str]):
723 fd.write('\nAtom Type Labels\n\n')
724 for i, s in enumerate(species):
725 atom_type = i + 1
726 fd.write(f'{atom_type} {s}\n')
729def _get_types(atoms: Atoms, species: list):
730 if 'type' in atoms.arrays:
731 return atoms.arrays['type']
732 symbols = atoms.get_chemical_symbols()
733 return [species.index(symbols[i]) + 1 for i in range(len(symbols))]
736def _get_symbols_by_types(atoms: Atoms) -> list[str]:
737 _, first_idx = np.unique(atoms.arrays['type'], return_index=True)
738 return [atoms.symbols[i] for i in first_idx]