-
Notifications
You must be signed in to change notification settings - Fork 70
/
to_string.py
511 lines (417 loc) · 18.6 KB
/
to_string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import collections
from typing import Any, Dict, List, Tuple, Union
import numpy as np
from ..molutil import guess_connectivity
from ..physical_constants import constants
def to_string(
molrec: Dict,
dtype: str,
units: str = None,
*,
atom_format: str = None,
ghost_format: str = None,
width: int = 17,
prec: int = 12,
return_data: bool = False,
) -> Union[str, Tuple[str, Dict]]:
r"""Format a string representation of QM molecule.
Parameters
----------
molrec
Psi4 json Molecule spec.
dtype
{"xyz", "cfour", "nwchem", "molpro", "orca", "turbomole", "qchem"}
Overall string format. Note that it's possible to request variations
that don't fit the dtype spec so may not be re-readable (e.g., ghost
and mass in nucleus label with ``'xyz'``).
'cfour' forces nucleus label, ignoring atom_format, ghost_format
units
Units in which to write string. Usually ``Angstrom`` or ``Bohr``
but may be any length unit. There is not an option to write in
intrinsic/input units. For ``dtype='xyz', units='Bohr'`` where the
format doesn't have a slot to specify units, "au" is added so that
readable as ``dtype='xyz+'``.
atom_format
General format is ``'{elem}'``. A format string that may contain fields
'elea' (-1 will be ''), 'elez', 'elem', 'mass', 'elbl' in any
arrangement. For example, if a format naturally uses element symbol
and you want atomic number instead with mass info, too, pass
``'{elez}@{mass}'``. See `ghost_format` for handling field 'real'.
ghost_format
General format is ``'@{elem}'``. Like `atom_format`, but this formatter
is used when `real=False`. To suppress ghost atoms, use `ghost_format=''`.
width
Field width for formatting coordinate float.
prec
Number of decimal places for formatting coordinate float.
return_data
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.
Returns
-------
str
String representation of the molecule.
str, dict
When ``return_data=True``, return additionally a dictionary
* keywords: key, value pairs for processing molecule info into options
* fields: aspects of ``qcelemental.models.Molecule`` expressed into string *or* keywords.
Model fields *not* listed are lost in QCSchema -> QC DSL translation.
"""
# funits, fiutau = process_units(molrec)
# molrec = self.to_dict(force_units=units, np_out=True)
dtype = dtype.lower()
default_units = {
"xyz": "Angstrom",
"xyz+": "Angstrom",
"nglview-sdf": "Angstrom",
"cfour": "Bohr",
"gamess": "Bohr",
"molpro": "Bohr",
"nwchem": "Bohr",
"orca": "Bohr",
"psi4": "Bohr",
"qchem": "Bohr",
"terachem": "Bohr",
"turbomole": "Bohr",
"madness": "Bohr", # madness by default reads au and optionally can read angs/angstrom
"mrchem": "Bohr", # MRChem by default reads au and optionally can read angs/angstrom
}
if dtype not in default_units:
raise KeyError(f"dtype '{dtype}' not understood.")
# Handle units
if units is None:
units = default_units[dtype]
if molrec["units"] == "Angstrom" and units.capitalize() == "Angstrom":
factor = 1.0
elif molrec["units"] == "Angstrom" and units.capitalize() == "Bohr":
if "input_units_to_au" in molrec:
factor = molrec["input_units_to_au"]
else:
factor = 1.0 / constants.bohr2angstroms
elif molrec["units"] == "Bohr" and units.capitalize() == "Angstrom":
factor = constants.bohr2angstroms
elif molrec["units"] == "Bohr" and units.capitalize() == "Bohr":
factor = 1.0
else:
factor = constants.conversion_factor(molrec["units"], units)
geom = np.asarray(molrec["geom"]).reshape((-1, 3)) * factor
name = molrec.get("name", formula_generator(molrec["elem"]))
tagline = """auto-generated by QCElemental from molecule {}""".format(name)
class Data:
fields: List[str] = ["atomic_numbers", "geometry", "symbols"]
keywords: Dict[str, Any] = {}
_dict_attrs: List[str] = ["fields", "keywords"]
def to_dict(self) -> Dict:
return {attr: getattr(self, attr) for attr in self._dict_attrs}
data = Data()
if dtype in ["xyz", "xyz+"]:
# Notes
# * if units not in umap (e.g., nm), can't be read back in by from_string()
atom_format = "{elem}" if atom_format is None else atom_format
ghost_format = "@{elem}" if ghost_format is None else ghost_format
umap = {"bohr": "au", "angstrom": ""}
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
nat = len(atoms)
first_line = """{} {}""".format(str(nat), umap.get(units.lower(), units.lower()))
smol = [first_line.rstrip()]
smol.append(f"{int(molrec['molecular_charge'])} {molrec['molecular_multiplicity']} {name}")
smol.extend(atoms)
elif dtype == "orca":
atom_format = "{elem}"
ghost_format = "{elem}:"
umap = {"bohr": "! Bohrs", "angstrom": "!"}
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
smol = []
smol.append(umap[units.lower()])
smol.append("")
smol.append(f"*xyz {int(molrec['molecular_charge'])} {molrec['molecular_multiplicity']}")
smol.extend(atoms)
smol.append("*")
elif dtype == "cfour":
# Notes
# * losing identity of ghost atoms. picked up again in basis formatting
# * casting 'molecular_charge' to int
# * no spaces at the beginning of 1st/comment line is important
atom_format = "{elem}"
ghost_format = "GH"
# TODO handle which units valid
umap = {"bohr": "bohr", "angstrom": "angstrom"}
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
smol = [tagline]
smol.extend(atoms)
data.fields.extend(["molecular_charge", "molecular_multiplicity", "real"])
data.keywords = {
"charge": int(molrec["molecular_charge"]),
"multiplicity": molrec["molecular_multiplicity"],
"units": umap.get(units.lower()),
"coordinates": "cartesian",
}
elif dtype == "molpro":
atom_format = "{elem}"
ghost_format = "{elem}"
umap = {"bohr": "bohr", "angstrom": "angstrom"}
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
smol = []
# Don't orient the molecule if asked to fix_com or fix_orientation
if molrec["fix_orientation"] or molrec["fix_com"]:
smol.append("{orient,noorient}")
# Have no symmetry if asked to fix_symmetry
if "fix_symmetry" in molrec.keys() and molrec["fix_symmetry"] == "c1":
smol.append("{symmetry,nosym}")
elif "fix_symmetry" not in molrec.keys():
smol.append("{symmetry,auto}")
smol.append("")
units_line = f"""{{{umap.get(units.lower())}}}"""
geom_line = """geometry={"""
end_bracket = """}"""
smol.append(units_line)
smol.append(geom_line)
smol.extend(atoms)
smol.append(end_bracket)
# Write ghost atom declarations in Molpro (using dummy card)
if False in molrec["real"]:
ghost_line = "dummy," + ",".join([str(idx + 1) for idx, real in enumerate(molrec["real"]) if not real])
smol.append(ghost_line)
smol.append(f"set,charge={molrec['molecular_charge']}")
# The Molpro "spin" is the multiplicity minus one
smol.append(f"set,spin={molrec['molecular_multiplicity']-1}")
elif dtype == "nwchem":
atom_format = "{elem}{elbl}"
ghost_format = "bq{elem}{elbl}"
# 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)
first_line = f"""geometry units {umap.get(units.lower())}"""
# noautosym nocenter # no reorienting input geometry
fix_symm = molrec.get("fix_symmetry", None)
symm_line = ""
if fix_symm:
symm_line = "symmetry {}".format(fix_symm) # not quite what Jiyoung had
last_line = """end"""
smol = [first_line]
smol.extend(atoms)
smol.append(symm_line)
smol.append(last_line)
data.fields.extend(["molecular_charge", "molecular_multiplicity", "real"])
data.keywords = {"charge": int(molrec["molecular_charge"])}
if molrec["molecular_multiplicity"] != 1:
data.keywords["scf__nopen"] = molrec["molecular_multiplicity"] - 1
data.keywords["dft__mult"] = molrec["molecular_multiplicity"]
data.keywords["mcscf__multiplicity"] = molrec["molecular_multiplicity"]
elif dtype == "madness":
atom_format = "{elem}"
ghost_format = "GH"
# TODO handle which units valid
umap = {"bohr": "au", "angstrom": "angstrom"}
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
first_line = f"""geometry"""
second_line = f"""units {umap.get(units.lower())}"""
last_line = """end"""
# noautosym nocenter # no reorienting input geometry
smol = [first_line]
smol.append(second_line)
smol.extend(atoms)
smol.append(last_line)
data.fields.extend(["molecular_charge", "molecular_multiplicity"])
data.keywords = {"charge": int(molrec["molecular_charge"])}
if molrec["molecular_multiplicity"] != 1:
data.keywords["spin_restricted"] = "false"
elif dtype == "gamess":
# * GAMESS can't detect or run in symmetry w/o explicit notation
# * symm detection is out-of-scope for qcel -- hence the explicit C1 default
# * symm provisionally hackable by passing both point group and naxis (if needed)
# through ``fix_symmetry``. newline encoded here if needed.
# * coord=prinaxis, as set up here, can't handle ghost atoms
atom_format = " {elem}{elbl} {elez}"
ghost_format = " {elem} -{elez}"
umap = {"bohr": "bohr", "angstrom": "angs"}
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
first_line = """ $data"""
second_line = f""" {tagline}""" # card -1-
fix_symm = molrec.get("fix_symmetry", "C1").strip()
symm_line = f" {fix_symm}" # card -2-
if fix_symm.upper() != "C1":
symm_line += "\n" # blank card replaces -3- & -4-
last_line = """ $end"""
smol = [first_line, second_line, symm_line]
smol.extend(atoms) # card -5C-
smol.append(last_line)
data.fields.extend(["molecular_charge", "molecular_multiplicity", "real"])
data.keywords = {
"contrl__icharg": int(molrec["molecular_charge"]),
"contrl__mult": molrec["molecular_multiplicity"],
"contrl__units": umap.get(units.lower()),
"contrl__coord": "prinaxis",
}
elif dtype == "terachem":
atom_format = "{elem}"
ghost_format = "X{elem}"
umap = {"bohr": "au", "angstrom": ""}
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
first_line = f"""{len(atoms)} {umap[units.lower()]}"""
smol = [first_line.rstrip(), name]
smol.extend(atoms)
elif dtype == "psi4":
atom_format = "{elem}{elbl}"
ghost_format = "Gh({elem}{elbl})"
umap = {"bohr": "bohr", "angstrom": "angstrom"}
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
smol = [f"""{int(molrec['molecular_charge'])} {molrec['molecular_multiplicity']}"""]
split_atoms = np.split(atoms, molrec["fragment_separators"])
for ifr, fr in enumerate(split_atoms):
if len(split_atoms) > 1: # harmless to include but tidier to exclude
smol.extend(["--", f"{int(molrec['fragment_charges'][ifr])} {molrec['fragment_multiplicities'][ifr]}"])
smol.extend(fr.tolist())
# append units and any other non-default molecule keywords
smol.append(f"units {umap[units.lower()]}")
if molrec["fix_com"]:
smol.append("no_com")
if molrec["fix_orientation"]:
smol.append("no_reorient")
data.fields.extend(
[
"molecular_charge",
"molecular_multiplicity",
"fragments",
"fragment_charges",
"fragment_multiplicities",
"fix_com",
"fix_orientation",
"real",
]
)
data.keywords = {}
elif dtype == "turbomole":
# In Turbomole coord files the coordinates come first, and the atomic
# symbol comes afterwards.
# Handling of ghost atoms is done in the basis section of the control
# file by setting the nuclear charge of certain atoms to zero.
atom_format = "{elem}"
ghost_format = "{elem}"
umap = {"bohr": "bohr"}
umap[units.lower()] # trigger error if downstream can't handle
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2, xyze=True)
atoms = [at.lower() for at in atoms]
smol = ["$coord"] + atoms + ["$end"]
elif dtype == "nglview-sdf":
# SDF is pretty special, handle it manually
if units.capitalize() != "Angstrom":
raise ValueError("SDF Format must be in Angstroms")
ghost_format = ghost_format or "Gh"
connectivity = molrec.get("connectivity", None)
if connectivity is None:
bohr_geom = geom * constants.conversion_factor("Angstrom", "Bohr")
connectivity = guess_connectivity(molrec["elem"], bohr_geom, default_connectivity=1)
smol = []
smol.append("")
smol.append("QCElemental\n")
smol.append(f"{len(molrec['real']):3d} {len(connectivity):2d} 0 0 0 0 0 0 0 0 0")
for real, sym, xyz in zip(molrec["real"], molrec["elem"], geom):
if bool(real) is False:
sym = ghost_format
smol.append(f"{xyz[0]:10.4f}{xyz[1]:10.4f}{xyz[2]:10.4f}{sym:>3s} 0 0 0 0 0 0 0 0")
for a1, a2, b in connectivity:
smol.append(f" {(a1 + 1):2d} {(a2 + 1):2d} {int(b):1d} 0 0 0 0")
elif dtype == "qchem":
atom_format = "{elem}"
ghost_format = "@{elem}"
umap = {"bohr": "True", "angstrom": "False"}
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
first_line = "$molecule"
chgmult_line = f"""{int(molrec['molecular_charge'])} {molrec['molecular_multiplicity']}"""
last_line = "$end"
smol = [first_line, chgmult_line]
split_atoms = np.split(atoms, molrec["fragment_separators"])
for ifr, fr in enumerate(split_atoms):
if len(split_atoms) > 1:
smol.extend(
["--", f"""{int(molrec['fragment_charges'][ifr])} {molrec['fragment_multiplicities'][ifr]}"""]
)
smol.extend(fr.tolist())
smol.append(last_line)
data.fields.extend(
[
"fix_com",
"fix_orientation",
"fragment_charges",
"fragment_multiplicities",
"molecular_charge",
"molecular_multiplicity",
"real",
"units",
]
)
data.keywords = {
"no_reorient": molrec["fix_orientation"] or molrec["fix_com"],
"input_bohr": umap[units.lower()],
}
if "fix_symmetry" in molrec.keys() and molrec["fix_symmetry"] == "c1":
data.keywords["sym_ignore"] = True
data.keywords["symmetry"] = False
elif dtype == "mrchem":
atom_format = "{elem}"
ghost_format = "{elem}"
atoms = _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, 2)
smol = (
[
"Molecule {",
f"charge = {int(molrec['molecular_charge'])}",
f"multiplicity = {molrec['molecular_multiplicity']}",
f"translate = {molrec['fix_com']}",
"$coords",
]
+ atoms
+ ["$end\n}"]
)
# this is what we are actually interested in:
# when dtype="mrchem", we always want to set return_data=True
data.keywords = {
"charge": int(molrec["molecular_charge"]),
"multiplicity": molrec["molecular_multiplicity"],
"translate": molrec["fix_com"],
"coords": "\n".join(atoms),
}
else:
raise KeyError(f"dtype '{dtype}' not understood.")
smol_ret = "\n".join(smol) + "\n"
if return_data:
return smol_ret, data.to_dict()
else:
return smol_ret
def _atoms_formatter(molrec, geom, atom_format, ghost_format, width, prec, sp, xyze=False):
"""Format a list of strings, one per atom from `molrec`."""
nat = geom.shape[0]
fxyz = """{:>{width}.{prec}f}"""
sp = """{:{sp}}""".format("", sp=sp)
atoms = []
for iat in range(nat):
atom = []
atominfo = {
"elea": "" if molrec["elea"][iat] == -1 else molrec["elea"][iat],
"elez": molrec["elez"][iat],
"elem": molrec["elem"][iat],
"mass": molrec["mass"][iat],
"elbl": molrec["elbl"][iat],
}
if molrec["real"][iat]:
nuc = """{:{width}}""".format(atom_format.format(**atominfo), width=width)
atom.append(nuc)
else:
if ghost_format in ["", None]:
continue
else:
nuc = """{:{width}}""".format(ghost_format.format(**atominfo), width=width)
atom.append(nuc)
atom.extend([fxyz.format(x, width=width, prec=prec) for x in geom[iat]])
if xyze:
atom.append(atom.pop(0).rstrip())
atoms.append(sp.join(atom))
return atoms
def formula_generator(elem):
"""Return simple chemical formula from element list `elem`.
>>> formula_generator(['C', 'Ca', 'O', 'O', 'Ag']
AgCCaO2
"""
counted = collections.Counter(elem)
return "".join((el if cnt == 1 else (el + str(cnt))) for el, cnt in sorted(counted.items()))