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

GXTBConfig #104

Merged
merged 4 commits into from
Jan 14, 2025
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- to set the elemental composition it is now possible to use dicts with not only int but also the element symbols (str)
- dict keys for elemental compositions will now always be checked for validity
- Renamed GP3-xTB to g-xTB

### Added
- `GXTBConfig` class for the g-xTB method, supporting SCF cycles check

### Fixed
- version string is now correctly formatted and printed

## [0.5.0] - 2024-12-16
### Changed
Expand Down
18 changes: 18 additions & 0 deletions src/mindlessgen/cli/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ def cli_parser(argv: Sequence[str] | None = None) -> dict:
required=False,
help="Maximum number of SCF cycles in ORCA.",
)
### g-xTB specific arguments ###
parser.add_argument(
"--gxtb-path",
type=str,
required=False,
help="Path to the g-xTB binary.",
)
parser.add_argument(
"--gxtb-scf-cycles",
type=int,
required=False,
help="Maximum number of SCF cycles in g-xTB.",
)
args = parser.parse_args(argv)
args_dict = vars(args)

Expand Down Expand Up @@ -306,6 +319,11 @@ def cli_parser(argv: Sequence[str] | None = None) -> dict:
"gridsize": args_dict["orca_gridsize"],
"scf_cycles": args_dict["orca_scf_cycles"],
}
# g-xTB specific arguments
rev_args_dict["gxtb"] = {
"gxtb_path": args_dict["gxtb_path"],
"scf_cycles": args_dict["gxtb_scf_cycles"],
}
# Postprocessing arguments
rev_args_dict["postprocess"] = {
"engine": args_dict["postprocess_engine"],
Expand Down
4 changes: 2 additions & 2 deletions src/mindlessgen/generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ def setup_engines(
elif engine_type == "gxtb":
if gxtb_path_func is None:
raise ImportError("No callable function for determining the g-xTB path.")
path = gxtb_path_func()
path = gxtb_path_func(cfg.gxtb.gxtb_path)
if not path:
raise ImportError("'gxtb' binary could not be found.")
return GXTB(path)
return GXTB(path, cfg.gxtb)
else:
raise NotImplementedError("Engine not implemented.")
2 changes: 1 addition & 1 deletion src/mindlessgen/molecules/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ def postprocess_mol(
postprocmol = mol
except RuntimeError as e:
raise RuntimeError(
"Single point calculation in postprocessing failed."
f"Single point calculation in postprocessing failed with error: {e}"
) from e
return postprocmol
2 changes: 2 additions & 0 deletions src/mindlessgen/prog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
GeneralConfig,
XTBConfig,
ORCAConfig,
GXTBConfig,
GenerateConfig,
RefineConfig,
PostProcessConfig,
Expand All @@ -17,6 +18,7 @@
"GeneralConfig",
"XTBConfig",
"ORCAConfig",
"GXTBConfig",
"GenerateConfig",
"RefineConfig",
"PostProcessConfig",
Expand Down
48 changes: 48 additions & 0 deletions src/mindlessgen/prog/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,53 @@ def scf_cycles(self, max_scf_cycles: int):
self._scf_cycles = max_scf_cycles


class GXTBConfig(BaseConfig):
"""
Configuration class for g-xTB.
"""

def __init__(self: GXTBConfig) -> None:
self._gxtb_path: str | Path = "gxtb"
self._scf_cycles: int = 100

def get_identifier(self) -> str:
return "gxtb"

@property
def gxtb_path(self):
"""
Get the g-xTB path.
"""
return self._gxtb_path

@gxtb_path.setter
def gxtb_path(self, gxtb_path: str | Path):
"""
Set the g-xTB path.
"""
if not isinstance(gxtb_path, str | Path):
raise TypeError("gxtb_path should be a string or Path.")
self._gxtb_path = gxtb_path

@property
def scf_cycles(self):
"""
Get the maximum number of SCF cycles.
"""
return self._scf_cycles

@scf_cycles.setter
def scf_cycles(self, max_scf_cycles: int):
"""
Set the maximum number of SCF cycles.
"""
if not isinstance(max_scf_cycles, int):
raise TypeError("Max SCF cycles should be an integer.")
if max_scf_cycles < 1:
raise ValueError("Max SCF cycles should be greater than 0.")
self._scf_cycles = max_scf_cycles


class ConfigManager:
"""
Overall configuration manager for the program.
Expand All @@ -940,6 +987,7 @@ def __init__(self, config_file: str | Path | None = None):
self.refine = RefineConfig()
self.postprocess = PostProcessConfig()
self.generate = GenerateConfig()
self.gxtb = GXTBConfig()

if config_file:
self.load_from_toml(config_file)
Expand Down
22 changes: 21 additions & 1 deletion src/mindlessgen/qm/gxtb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tempfile import TemporaryDirectory

from ..molecules import Molecule
from ..prog import GXTBConfig
from .base import QMMethod


Expand All @@ -17,7 +18,7 @@ class GXTB(QMMethod):
This class handles all interaction with the g-xTB external dependency.
"""

def __init__(self, path: str | Path) -> None:
def __init__(self, path: str | Path, gxtbcfg: GXTBConfig) -> None:
"""
Initialize the GXTB class.
"""
Expand All @@ -27,6 +28,7 @@ def __init__(self, path: str | Path) -> None:
self.path = path
else:
raise TypeError("gxtb_path should be a string or a Path object.")
self.cfg = gxtbcfg

def singlepoint(self, molecule: Molecule, verbosity: int = 1) -> str:
"""
Expand Down Expand Up @@ -64,6 +66,24 @@ def singlepoint(self, molecule: Molecule, verbosity: int = 1) -> str:
raise RuntimeError(
f"g-xTB failed with return code {return_code}:\n{gxtb_log_err}"
)
# gp3_output looks like this:
# [...]
# 13 -155.03101038 0.00000000 0.00000001 16.45392733 8 F
# 13 scf iterations
# eigenvalues
# [...]
# Check for the number of scf iterations
scf_iterations = 0
for line in gxtb_log_out.split("\n"):
if "scf iterations" in line:
scf_iterations = int(line.strip().split()[0])
break
if scf_iterations == 0:
raise RuntimeError("SCF iterations not found in GP3 output.")
if scf_iterations > self.cfg.scf_cycles:
raise RuntimeError(
f"SCF iterations exceeded limit of {self.cfg.scf_cycles}."
)

return gxtb_log_out

Expand Down
Loading