Skip to content

Commit

Permalink
start use_masses functionality in to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
loriab committed Jun 23, 2022
1 parent 2956421 commit 37ddc98
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions qcelemental/models/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,11 @@ def to_string( # type: ignore
*,
atom_format: str = None,
ghost_format: str = None,
post_format: str = None,
width: int = 17,
prec: int = 12,
return_data: bool = False,
use_masses: bool = False,
):
r"""Returns a string that can be used by a variety of programs.
Expand All @@ -754,9 +756,11 @@ def to_string( # type: ignore
units=units,
atom_format=atom_format,
ghost_format=ghost_format,
post_format=post_format,
width=width,
prec=prec,
return_data=return_data,
use_masses=use_masses,
)

def get_hash(self):
Expand Down
22 changes: 19 additions & 3 deletions qcelemental/molparse/to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ def to_string(
*,
atom_format: str = None,
ghost_format: str = None,
post_format: str = None,
width: int = 17,
prec: int = 12,
return_data: bool = False,
use_masses: bool = False,
) -> Union[str, Tuple[str, Dict]]:
r"""Format a string representation of QM molecule.
Expand Down Expand Up @@ -45,6 +47,9 @@ def to_string(
ghost_format
General format is ``'@{elem}'``. Like `atom_format`, but this formatter
is used when `real=False`. To suppress ghost atoms, use `ghost_format=''`.
post_format
General format is ``''``. Like `atom_format`, but this formatter can
add text beyond z-coord in element-x-y-z. Uncommon to call directly.
width
Field width for formatting coordinate float.
prec
Expand All @@ -53,6 +58,10 @@ def to_string(
Whether to return dictionary with additional info from the molrec that's
not expressible in the string but may be of interest to the QC program.
Note that field names are in QCSchema, not molrec, language.
use_masses
Write mass data from `molrec` to string. Most commonly, masses are
omitted from format so that CMS program internal mass data can be used.
Available for `dtype`: nwchem
Returns
-------
Expand Down Expand Up @@ -223,10 +232,12 @@ def to_dict(self) -> Dict:

atom_format = "{elem}{elbl}"
ghost_format = "bq{elem}{elbl}"
if use_masses:
post_format = " mass {mass}"
# TODO handle which units valid
umap = {"bohr": "bohr", "angstrom": "angstroms", "nm": "nanometers", "pm": "picometers"}

atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2, post_format=post_format)

first_line = f"""geometry units {umap.get(units.lower())}"""
# noautosym nocenter # no reorienting input geometry
Expand Down Expand Up @@ -471,7 +482,7 @@ def to_dict(self) -> Dict:
return smol_ret


def _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, sp, xyze=False):
def _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, sp, *, xyze=False, post_format=""):
"""Format a list of strings, one per atom from `molrec`."""

nat = geom.shape[0]
Expand Down Expand Up @@ -500,14 +511,19 @@ def _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, sp, x
atom.append(nuc)

atom.extend([fxyz.format(x, width=width, prec=prec) for x in geom[iat]])

if post_format:
post = post_format.format(**atominfo)
atom.append(post)

if xyze:
atom.append(atom.pop(0).rstrip())
atoms.append(sp.join(atom))

return atoms


def formula_generator(elem):
def formula_generator(elem: List[str]) -> str:
"""Return simple chemical formula from element list `elem`.
>>> formula_generator(['C', 'Ca', 'O', 'O', 'Ag']
Expand Down
8 changes: 8 additions & 0 deletions qcelemental/tests/test_molparse_to_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@
bqH {au2:.12f} 0.000000000000 0.000000000000
H_other -{au2:.12f} 0.000000000000 0.000000000000
end
""",
"ans2_nwchem_au_mass": f"""geometry units bohr
Co 0.000000000000 0.000000000000 0.000000000000 mass 58.93319429
bqH 2.000000000000 0.000000000000 0.000000000000 mass 1.00782503223
H_other -2.000000000000 0.000000000000 0.000000000000 mass 1.00782503223
end
""",
"ans2_madness_au": f"""geometry
Expand Down Expand Up @@ -252,6 +259,7 @@
(("subject2", {"dtype": "xyz", "units": "angstrom", "ghost_format": ""}), "ans2c_ang"),
(("subject2", {"dtype": "cfour", "units": "angstrom"}), "ans2_cfour_ang"),
(("subject2", {"dtype": "nwchem", "units": "angstrom"}), "ans2_nwchem_ang"),
(("subject2", {"dtype": "nwchem", "units": "bohr", "use_masses": True}), "ans2_nwchem_au_mass"),
(("subject2", {"dtype": "madness", "units": "bohr"}), "ans2_madness_au"),
(("subject2", {"dtype": "madness", "units": "angstrom"}), "ans2_madness_ang"),
(("subject2", {"dtype": "mrchem", "units": "bohr"}), "ans2_mrchem_au"),
Expand Down

0 comments on commit 37ddc98

Please sign in to comment.