Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ff = AllChem.MMFFGetMoleculeForceField(mol, props)
all_params = AllChem.MMFFHasAllMoleculeParams(mol)
else:
raise InputError("RDKit only supports the UFF, MMFF94, and MMFF94s methods currently.")
if all_params is False:
raise InputError("RDKit parameters not found for all atom types in molecule.")
ff.Initialize()
ret_data["properties"] = {"return_energy": ff.CalcEnergy() * ureg.conversion_factor("kJ / mol", "hartree")}
if input_data.driver == "energy":
ret_data["return_result"] = ret_data["properties"]["return_energy"]
elif input_data.driver == "gradient":
coef = ureg.conversion_factor("kJ / mol", "hartree") * ureg.conversion_factor("angstrom", "bohr")
ret_data["return_result"] = [x * coef for x in ff.CalcGrad()]
else:
raise InputError(f"RDKit can only compute energy and gradient driver methods. Found {input_data.driver}.")
ret_data["provenance"] = Provenance(
creator="rdkit", version=rdkit.__version__, routine="rdkit.Chem.AllChem.UFFGetMoleculeForceField"
)
ret_data["schema_name"] = "qcschema_output"
ret_data["success"] = True
# Form up a dict first, then sent to BaseModel to avoid repeat kwargs which don't override each other
return AtomicResult(**{**input_data.dict(), **ret_data})
if input_data.model.method.lower() == "uff":
ff = AllChem.UFFGetMoleculeForceField(mol)
all_params = AllChem.UFFHasAllMoleculeParams(mol)
elif input_data.model.method.lower() in ["mmff94", "mmff94s"]:
props = AllChem.MMFFGetMoleculeProperties(mol, mmffVariant=input_data.model.method)
ff = AllChem.MMFFGetMoleculeForceField(mol, props)
all_params = AllChem.MMFFHasAllMoleculeParams(mol)
else:
raise InputError("RDKit only supports the UFF, MMFF94, and MMFF94s methods currently.")
if all_params is False:
raise InputError("RDKit parameters not found for all atom types in molecule.")
ff.Initialize()
ret_data["properties"] = {"return_energy": ff.CalcEnergy() * ureg.conversion_factor("kJ / mol", "hartree")}
if input_data.driver == "energy":
ret_data["return_result"] = ret_data["properties"]["return_energy"]
elif input_data.driver == "gradient":
coef = ureg.conversion_factor("kJ / mol", "hartree") * ureg.conversion_factor("angstrom", "bohr")
ret_data["return_result"] = [x * coef for x in ff.CalcGrad()]
else:
raise InputError(f"RDKit can only compute energy and gradient driver methods. Found {input_data.driver}.")
ret_data["provenance"] = Provenance(
creator="rdkit", version=rdkit.__version__, routine="rdkit.Chem.AllChem.UFFGetMoleculeForceField"
)
ret_data["schema_name"] = "qcschema_output"
ret_data["success"] = True
# Build model
model = self.get_model(input_data.model.method)
if model is False:
raise InputError("TorchANI only accepts the ANI1x or ANI1ccx method.")
# Build species
species = "".join(input_data.molecule.symbols)
unknown_sym = set(species) - {"H", "C", "N", "O"}
if unknown_sym:
raise InputError(f"TorchANI model '{input_data.model.method}' does not support symbols: {unknown_sym}.")
num_atoms = len(species)
species = model.species_to_tensor(species).to(device).unsqueeze(0)
# Build coord array
geom_array = input_data.molecule.geometry.reshape(1, -1, 3) * ureg.conversion_factor("bohr", "angstrom")
coordinates = torch.tensor(geom_array.tolist(), requires_grad=True, device=device)
_, energy_array = model((species, coordinates))
energy = energy_array.mean()
ensemble_std = energy_array.std()
ensemble_scaled_std = ensemble_std / np.sqrt(num_atoms)
ret_data["properties"] = {"return_energy": energy.item()}
if input_data.driver == "energy":
ret_data["return_result"] = ret_data["properties"]["return_energy"]
elif input_data.driver == "gradient":
derivative = torch.autograd.grad(energy.sum(), coordinates)[0].squeeze()
ret_data["return_result"] = (
np.asarray(derivative * ureg.conversion_factor("angstrom", "bohr")).ravel().tolist()
)
base_mol = Chem.Mol()
rw_mol = Chem.RWMol(base_mol)
for sym in jmol.symbols:
rw_mol.AddAtom(Chem.Atom(sym.title()))
# Add in connectivity
bond_types = {1: Chem.BondType.SINGLE, 2: Chem.BondType.DOUBLE, 3: Chem.BondType.TRIPLE}
for atom1, atom2, bo in jmol.connectivity:
rw_mol.AddBond(atom1, atom2, bond_types[bo])
mol = rw_mol.GetMol()
# Write out the conformer
natom = len(jmol.symbols)
conf = Chem.Conformer(natom)
bohr2ang = ureg.conversion_factor("bohr", "angstrom")
for line in range(natom):
conf.SetAtomPosition(
line,
(
bohr2ang * jmol.geometry[line, 0],
bohr2ang * jmol.geometry[line, 1],
bohr2ang * jmol.geometry[line, 2],
),
)
mol.AddConformer(conf)
Chem.rdmolops.SanitizeMol(mol)
return mol
geom_array = input_data.molecule.geometry.reshape(1, -1, 3) * ureg.conversion_factor("bohr", "angstrom")
coordinates = torch.tensor(geom_array.tolist(), requires_grad=True, device=device)
_, energy_array = model((species, coordinates))
energy = energy_array.mean()
ensemble_std = energy_array.std()
ensemble_scaled_std = ensemble_std / np.sqrt(num_atoms)
ret_data["properties"] = {"return_energy": energy.item()}
if input_data.driver == "energy":
ret_data["return_result"] = ret_data["properties"]["return_energy"]
elif input_data.driver == "gradient":
derivative = torch.autograd.grad(energy.sum(), coordinates)[0].squeeze()
ret_data["return_result"] = (
np.asarray(derivative * ureg.conversion_factor("angstrom", "bohr")).ravel().tolist()
)
elif input_data.driver == "hessian":
hessian = torchani.utils.hessian(coordinates, energies=energy)
ret_data["return_result"] = np.asarray(hessian)
else:
raise InputError(
f"TorchANI can only compute energy, gradient, and hessian driver methods. Found {input_data.driver}."
)
#######################################################################
# Description of the quantities stored in `extras`
#
# ensemble_energies:
# An energy array of all members (models) in an ensemble of models
#
# ensemble_energy_avg: