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

Make charge and spin properties of a Geometry #43

Merged
merged 5 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions conformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
# Read input from Avogadro
avo_input = json.loads(sys.stdin.read())
# Extract the coords
geom = easyxtb.Geometry.from_xyz(avo_input["xyz"].split("\n"))
geom = easyxtb.Geometry.from_cjson(avo_input["cjson"])

# If provided crest path different to that stored, use it and save it
if Path(avo_input["crest_bin"]) != easyxtb.CREST_BIN:
Expand All @@ -157,8 +157,6 @@
# Run calculation; returns set of conformers as well as Calculation object
conformers, calc = easyxtb.calc.conformers(
geom,
charge=avo_input["charge"],
multiplicity=avo_input["spin"],
solvation=solvation,
method=2,
ewin=ewin_kcal,
Expand Down
4 changes: 1 addition & 3 deletions deprotonate.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@
# Read input from Avogadro
avo_input = json.loads(sys.stdin.read())
# Extract the coords
geom = easyxtb.Geometry.from_xyz(avo_input["xyz"].split("\n"))
geom = easyxtb.Geometry.from_cjson(avo_input["cjson"])

# Run calculation; returns set of tautomers as well as Calculation object
tautomers, calc = easyxtb.calc.deprotonate(
geom,
charge=avo_input["charge"],
multiplicity=avo_input["spin"],
solvation=easyxtb.config["solvent"],
method=2,
return_calc=True,
Expand Down
62 changes: 21 additions & 41 deletions easyxtb/src/easyxtb/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ def run(self):
*self.runtype_args,
"--",
]
if "chrg" not in self.options and self.input_geometry.charge != 0:
command.extend(["--chrg", str(self.input_geometry.charge)])
if "uhf" not in self.options and self.input_geometry.multiplicity != 1:
command.extend(["--uhf", str(self.input_geometry.multiplicity - 1)])
for flag, value in self.options.items():
# Add appropriate number of minuses to flags
if len(flag) == 1:
Expand Down Expand Up @@ -216,7 +220,11 @@ def process_xtb(self):
# If there's an output geometry, read it
if self.output_file.with_name("xtbopt.xyz").exists():
logger.debug(f"Output geometry found at {self.output_file.with_name('xtbopt.xyz')}")
self.output_geometry = Geometry.from_file(self.output_file.with_name("xtbopt.xyz"))
self.output_geometry = Geometry.from_file(
self.output_file.with_name("xtbopt.xyz"),
charge=self.input_geometry.charge,
multiplicity=self.input_geometry.multiplicity,
)
logger.debug("Read output geometry")
else:
# Assume geom was the same at end of calc as at beginning
Expand Down Expand Up @@ -248,7 +256,11 @@ def process_crest(self):
# Conformer search
logger.debug("A conformer search was requested, so checking for files")
# Get energy and geom of lowest conformer
best = Geometry.from_file(self.output_file.with_name("crest_best.xyz"))
best = Geometry.from_file(
self.output_file.with_name("crest_best.xyz"),
charge=self.input_geometry.charge,
multiplicity=self.input_geometry.multiplicity,
)
logger.debug(f"Geometry of lowest energy conformer read from {self.output_file.with_name('crest_best.xyz')}")
self.output_geometry = best
self.energy = float(best._comment)
Expand All @@ -257,6 +269,8 @@ def process_crest(self):
conformer_geoms = Geometry.from_file(
self.output_file.with_name("crest_conformers.xyz"),
multi=True,
charge=self.input_geometry.charge,
multiplicity=self.input_geometry.multiplicity,
)
logger.debug(f"Geometries of conformers read from {self.output_file.with_name('crest_conformers.xyz')}")
self.conformers = [
Expand All @@ -273,6 +287,8 @@ def process_crest(self):
tautomer_geoms = Geometry.from_file(
self.output_file.with_name("protonated.xyz"),
multi=True,
charge=self.input_geometry.charge + 1,
multiplicity=self.input_geometry.multiplicity,
)
logger.debug(f"Geometries of tautomers read from {self.output_file.with_name('protonated.xyz')}")
self.tautomers = [
Expand All @@ -294,6 +310,8 @@ def process_crest(self):
tautomer_geoms = Geometry.from_file(
self.output_file.with_name("deprotonated.xyz"),
multi=True,
charge=self.input_geometry.charge - 1,
multiplicity=self.input_geometry.multiplicity,
)
logger.debug(f"Geometries of tautomers read from {self.output_file.with_name('deprotonated.xyz')}")
self.tautomers = [
Expand All @@ -314,8 +332,6 @@ def process_crest(self):

def energy(
input_geometry: Geometry,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
n_proc: int | None = None,
Expand All @@ -326,8 +342,6 @@ def energy(
calc = Calculation(
input_geometry=input_geometry,
options={
"chrg": charge,
"uhf": multiplicity - 1,
"gfn": method,
"alpb": solvation,
"p": n_proc if n_proc else config["n_proc"],
Expand All @@ -342,8 +356,6 @@ def energy(

def optimize(
input_geometry: Geometry,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
level: str = "normal",
Expand All @@ -358,8 +370,6 @@ def optimize(
runtype="opt",
runtype_args=[level],
options={
"chrg": charge,
"uhf": multiplicity - 1,
"gfn": method,
"alpb": solvation,
"p": n_proc if n_proc else config["n_proc"],
Expand All @@ -377,8 +387,6 @@ def optimize(

def frequencies(
input_geometry: Geometry,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
n_proc: int | None = None,
Expand All @@ -390,8 +398,6 @@ def frequencies(
input_geometry=input_geometry,
runtype="hess",
options={
"chrg": charge,
"uhf": multiplicity - 1,
"gfn": method,
"alpb": solvation,
"p": n_proc if n_proc else config["n_proc"],
Expand All @@ -406,8 +412,6 @@ def frequencies(

def opt_freq(
input_geometry: Geometry,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
level: str = "normal",
Expand All @@ -426,8 +430,6 @@ def opt_freq(
runtype="ohess",
runtype_args=[level],
options={
"chrg": charge,
"uhf": multiplicity - 1,
"gfn": method,
"alpb": solvation,
"p": n_proc if n_proc else config["n_proc"],
Expand All @@ -441,12 +443,10 @@ def opt_freq(
distorted_geom_file = calc.output_file.with_name("xtbhess.xyz")
while distorted_geom_file.exists():
calc = Calculation(
input_geometry=Geometry.from_file(distorted_geom_file),
input_geometry=Geometry.from_file(distorted_geom_file, charge=input_geometry.charge, multiplicity=input_geometry.multiplicity),
runtype="ohess",
runtype_args=[level],
options={
"chrg": charge,
"uhf": multiplicity - 1,
"gfn": method,
"alpb": solvation,
"p": n_proc if n_proc else config["n_proc"],
Expand All @@ -462,8 +462,6 @@ def opt_freq(

def orbitals(
input_geometry: Geometry,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
n_proc: int | None = None,
Expand All @@ -479,8 +477,6 @@ def orbitals(
input_geometry=input_geometry,
options={
"molden": True,
"chrg": charge,
"uhf": multiplicity - 1,
"gfn": method,
"alpb": solvation,
"p": n_proc if n_proc else config["n_proc"],
Expand All @@ -495,8 +491,6 @@ def orbitals(

def conformers(
input_geometry: Geometry,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
ewin: int | float = 6,
Expand All @@ -519,8 +513,6 @@ def conformers(
options={
"xnam": XTB_BIN,
method_flag: True,
"chrg": charge,
"uhf": multiplicity - 1,
"alpb": solvation,
"ewin": ewin,
"T": n_proc if n_proc else config["n_proc"],
Expand All @@ -537,8 +529,6 @@ def conformers(

def protonate(
input_geometry: Geometry,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
n_proc: int | None = None,
Expand All @@ -547,8 +537,6 @@ def protonate(
"""Screen possible protonation sites and return set of tautomer Geometries and energies.

The returned tautomers are ordered from lowest to highest energy.

Note that `charge` should be the charge on the molecule before protonation.
"""
method_flag = f"gfn{method}"
calc = Calculation(
Expand All @@ -558,8 +546,6 @@ def protonate(
options={
"xnam": XTB_BIN,
method_flag: True,
"chrg": charge,
"uhf": multiplicity - 1,
"alpb": solvation,
"T": n_proc if n_proc else config["n_proc"],
},
Expand All @@ -573,8 +559,6 @@ def protonate(

def deprotonate(
input_geometry: Geometry,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
n_proc: int | None = None,
Expand All @@ -583,8 +567,6 @@ def deprotonate(
"""Screen possible deprotonation sites and return set of tautomer Geometries and energies.

The returned tautomers are ordered from lowest to highest energy.

Note that `charge` should be the charge on the molecule before deprotonation.
"""
method_flag = f"gfn{method}"
calc = Calculation(
Expand All @@ -594,8 +576,6 @@ def deprotonate(
options={
"xnam": XTB_BIN,
method_flag: True,
"chrg": charge,
"uhf": multiplicity - 1,
"alpb": solvation,
"T": n_proc if n_proc else config["n_proc"],
},
Expand Down
Loading