Skip to content

Commit

Permalink
Lint and format with ruff 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
matterhorn103 committed Mar 1, 2024
1 parent fc9efc6 commit e66fec4
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 59 deletions.
25 changes: 12 additions & 13 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"energy_units": "kJ/mol",
"method": 2,
"opt_lvl": "normal",
}.items():
}.items():
if option not in config:
config[option] = default

Expand Down Expand Up @@ -177,7 +177,6 @@ def find_obabel():
methods = ["GFN0-xTB", "GFN1-xTB", "GFN2-xTB"]



if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true")
Expand Down Expand Up @@ -207,14 +206,14 @@ def find_obabel():
"type": "string",
"label": "Run calculations (in subfolder) in",
"default": str(calc_dir.parent),
"order": 3.0
"order": 3.0,
},
"energy_units": {
"type": "stringList",
"label": "Preferred energy units",
"values": ["kJ/mol", "kcal/mol"],
"default": 0,
"order": 4.0
"order": 4.0,
},
"solvent": {
"type": "stringList",
Expand Down Expand Up @@ -254,7 +253,7 @@ def find_obabel():
"label": "Method (xtb only)",
"values": methods,
"default": methods[-1],
"order": 6.0
"order": 6.0,
},
"opt_lvl": {
"type": "stringList",
Expand All @@ -267,17 +266,17 @@ def find_obabel():
"normal",
"tight",
"vtight",
"extreme"
],
"extreme",
],
"default": 4,
"order": 7.0
"order": 7.0,
},
"warning": {
"type": "text",
"label": "Note",
"default": "Some changes here will only affect other\nmenus after restarting Avogadro!",
"order": 10.0
}
"order": 10.0,
},
}
}
# Set other options' defaults to match that found in user config
Expand Down Expand Up @@ -316,7 +315,7 @@ def find_obabel():
if avo_input["obabel_bin"] != str(obabel_bin):
obabel_bin = Path(avo_input["obabel_bin"])
config["obabel_bin"] = str(obabel_bin)

# Update energy units
config["energy_units"] = avo_input["energy_units"]

Expand All @@ -327,9 +326,9 @@ def find_obabel():
solvent_selected = avo_input["solvent"]
# Update solvent
config["solvent"] = solvent_selected

# Update method
config["method"] = methods.index(avo_input["method"])

with open(config_file, "w", encoding="utf-8") as config_path:
json.dump(config, config_path, indent=2)
5 changes: 4 additions & 1 deletion conformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ def conformers(
json.dump(result["cjson"], save_file, indent=2)

# If user specified a save location, copy calculation directory to there
if not (avo_input["save_dir"] in ["", None] or Path(avo_input["save_dir"]) == calc_dir):
if not (
avo_input["save_dir"] in ["", None]
or Path(avo_input["save_dir"]) == calc_dir
):
copytree(calc_dir, Path(avo_input["save_dir"]), dirs_exist_ok=True)

# Pass back to Avogadro
Expand Down
24 changes: 17 additions & 7 deletions energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ def energy(
) -> float:
"""Calculate energy in hartree for given geometry."""
unpaired_e = multiplicity - 1
command = ["xtb", geom_file, "--chrg", str(charge), "--uhf", str(unpaired_e), "--gfn", str(method)]
command = [
"xtb",
geom_file,
"--chrg",
str(charge),
"--uhf",
str(unpaired_e),
"--gfn",
str(method),
]
# Add solvation if requested
if solvation is not None:
command.extend(["--alpb", solvation])
Expand Down Expand Up @@ -80,12 +89,13 @@ def energy(
# Start by passing back the original cjson, then add changes
result = {"moleculeFormat": "cjson", "cjson": avo_input["cjson"]}
# Currently Avogadro ignores the energy result
result["message"] = (f"Energy from GFN{config["method"]}-xTB:\n"
+ f"{str(round(energy_hartree, 7))} hartree\n"
+ f"{str(round(energies['eV'], 7))} eV\n"
+ f"{str(round(energies['kJ'], 7))} kJ/mol\n"
+ f"{str(round(energies['kcal'], 7))} kcal/mol\n"
)
result["message"] = (
f"Energy from GFN{config["method"]}-xTB:\n"
+ f"{str(round(energy_hartree, 7))} hartree\n"
+ f"{str(round(energies['eV'], 7))} eV\n"
+ f"{str(round(energies['kJ'], 7))} kJ/mol\n"
+ f"{str(round(energies['kcal'], 7))} kcal/mol\n"
)
result["cjson"]["properties"]["totalEnergy"] = str(round(energies["eV"], 7))
# Pass back to Avogadro
print(json.dumps(result))
27 changes: 18 additions & 9 deletions freq.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@


def frequencies(

geom_file: Path,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
) -> Path:
geom_file: Path,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
) -> Path:
"""Calculate vibrational frequencies and return Gaussian 98 format output file."""
unpaired_e = multiplicity - 1
command = ["xtb", geom_file, "--hess", "--chrg", str(charge), "--uhf", str(unpaired_e), "--gfn", str(method)]
command = [
"xtb",
geom_file,
"--hess",
"--chrg",
str(charge),
"--uhf",
str(unpaired_e),
"--gfn",
str(method),
]

# Add solvation if requested
if solvation is not None:
Expand Down Expand Up @@ -77,7 +86,7 @@ def frequencies(
multiplicity=avo_input["spin"],
solvation=config["solvent"],
method=config["method"],
)
)

# Currently Avogadro fails to convert the g98 file to cjson itself
# So we have to convert output in g98 format to cjson ourselves
Expand Down
31 changes: 21 additions & 10 deletions ohess.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@


def opt_freq(
geom_file: Path,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
level: str = "normal",
) -> tuple[Path, Path, float]:
geom_file: Path,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
level: str = "normal",
) -> tuple[Path, Path, float]:
"""Optimize geometry then calculate vibrational frequencies. Distort and reoptimize if negative frequency detected."""
spin = multiplicity - 1
command = ["xtb", geom_file, "--ohess", level, "--chrg", str(charge), "--uhf", str(spin), "--gfn", str(method)]
command = [
"xtb",
geom_file,
"--ohess",
level,
"--chrg",
str(charge),
"--uhf",
str(spin),
"--gfn",
str(method),
]
# Add solvation if requested
if solvation is not None:
command.extend(["--alpb", solvation])
Expand Down Expand Up @@ -88,8 +99,8 @@ def opt_freq(
solvation=config["solvent"],
method=config["method"],
level=config["level"],
)
)

# Convert frequencies
# Currently Avogadro fails to convert the g98 file to cjson itself
# So we have to convert output in g98 format to cjson ourselves
Expand Down
29 changes: 20 additions & 9 deletions opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,27 @@


def optimize(
geom_file: Path,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
level: str = "normal",
) -> tuple[Path, float]:
geom_file: Path,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
level: str = "normal",
) -> tuple[Path, float]:
"""Return optimized geometry as file in same format as the input, along with the energy."""
unpaired_e = multiplicity - 1
command = ["xtb", geom_file, "--opt", level, "--chrg", str(charge), "--uhf", str(unpaired_e), "--gfn", str(method)]
command = [
"xtb",
geom_file,
"--opt",
level,
"--chrg",
str(charge),
"--uhf",
str(unpaired_e),
"--gfn",
str(method),
]

# Add solvation if requested
if solvation is not None:
Expand Down Expand Up @@ -77,7 +88,7 @@ def optimize(
solvation=config["solvent"],
method=config["method"],
level=config["level"],
)
)

# Read the xyz file
with open(result_path.with_name("xtbopt.xyz"), encoding="utf-8") as result_xyz:
Expand Down
27 changes: 18 additions & 9 deletions orbitals.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@


def orbitals(
geom_file: Path,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
) -> Path:

geom_file: Path,
charge: int = 0,
multiplicity: int = 1,
solvation: str | None = None,
method: int = 2,
) -> Path:
"""Calculate molecular orbitals for given geometry, return file in Molden format."""
spin = multiplicity - 1
# Just do a single point calculation but pass molden option to get orbital printout
command = ["xtb", geom_file, "--molden", "--chrg", str(charge), "--uhf", str(spin), "--gfn", str(method)]
command = [
"xtb",
geom_file,
"--molden",
"--chrg",
str(charge),
"--uhf",
str(spin),
"--gfn",
str(method),
]
# Add solvation if requested
if solvation is not None:
command.extend(["--alpb", solvation])
Expand Down Expand Up @@ -76,7 +85,7 @@ def orbitals(
multiplicity=avo_input["spin"],
solvation=config["solvent"],
method=config["method"],
)
)

# Get molden file as string
with open(result_path, encoding="utf-8") as molden_file:
Expand Down
4 changes: 3 additions & 1 deletion run_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@
}
# Add solvation to default command if found in user config
if config["solvent"] is not None:
options["userOptions"]["command"]["default"] += f" --alpb {config['solvent']}"
options["userOptions"]["command"]["default"] += (
f" --alpb {config['solvent']}"
)
# Add method to default command but only if not the default (currently GFN2-xTB)
if config["method"] != 2:
options["userOptions"]["command"]["default"] += f" --gfn {config['method']}"
Expand Down

0 comments on commit e66fec4

Please sign in to comment.