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

Re-introduce HOMO-LUMO gap check #25

Merged
merged 5 commits into from
Sep 4, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Return type for `single_molecule_generator`
- Check for consistency of the `min_num_atoms` and `max_num_atoms` constraint
- Similar to the `<basename>.CHRG` file, also a `<basename>.UHF` is printed
- HOMO-LUMO gap check within the refinement step and corresponding Config option called "refine_hlgap"

## [0.3.0] - 2024-08-20
### Breaking Changes
Expand Down
2 changes: 2 additions & 0 deletions mindlessgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ forbidden_elements = "57-71"
max_frag_cycles = 100
# > Quantum Mechanics (QM) engine to use. Options: 'xtb', 'orca'
engine = "xtb"
# > HOMO-LUMO gap threshold applied at the end of the refinement step
hlgap = 0.5
# > Debug this step. Leads to more verbose output as soon as the refinement part is reached. Options: <bool>
# > If `debug` is true, the process is terminated after the first (successful or not) refinement step.
debug = false
Expand Down
7 changes: 7 additions & 0 deletions src/mindlessgen/cli/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def cli_parser(argv: Sequence[str] | None = None) -> dict:
required=False,
help="Maximum number of fragment optimization cycles.",
)
parser.add_argument(
"--refine-hlgap",
type=float,
required=False,
help="Minimum HOMO-LUMO gap required in the end of the refinement process",
)
parser.add_argument(
"--refine-debug",
action="store_true",
Expand Down Expand Up @@ -225,6 +231,7 @@ def cli_parser(argv: Sequence[str] | None = None) -> dict:
rev_args_dict["refine"] = {
"engine": args_dict["refine_engine"],
"max_frag_cycles": args_dict["max_frag_cycles"],
"hlgap": args_dict["refine_hlgap"],
"debug": args_dict["refine_debug"],
}
# Molecule generation arguments
Expand Down
2 changes: 1 addition & 1 deletion src/mindlessgen/molecules/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def write_xyz_to_file(self, filename: str | Path | None = None):
with open(filename.with_suffix(".UHF"), "w", encoding="utf8") as f:
f.write(f"{self.uhf}\n")

def read_xyz_from_file(self, filename: str | Path):
def read_xyz_from_file(self, filename: str | Path) -> None:
"""
Read the XYZ coordinates of the molecule from a file.

Expand Down
19 changes: 8 additions & 11 deletions src/mindlessgen/molecules/refinement.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,14 @@ def iterative_optimization(
"Final fragment has an invalid number of atoms (less than min or more than max)."
)

# IMPORTANT:
# TODO: Reintroduce `check_gap` either at this point or in `postprocess.py`
# Sample implementation:
# try:
# gap_sufficient = engine.check_gap(
# molecule=rev_mol, threshold=0.5, verbosity=verbosity
# )
# except RuntimeError as e:
# raise RuntimeError("HOMO-LUMO gap could not be checked.") from e
# if not gap_sufficient:
# raise RuntimeError("HOMO-LUMO gap does not meet the lower threshold.")
try:
gap_sufficient = engine.check_gap(
molecule=rev_mol, threshold=config_refine.hlgap, verbosity=verbosity
)
except RuntimeError as e:
raise RuntimeError("HOMO-LUMO gap could not be checked.") from e
if not gap_sufficient:
raise RuntimeError("HOMO-LUMO gap does not meet the lower threshold.")

return rev_mol

Expand Down
19 changes: 19 additions & 0 deletions src/mindlessgen/prog/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ class RefineConfig(BaseConfig):
def __init__(self: RefineConfig) -> None:
self._max_frag_cycles: int = 100
self._engine: str = "xtb"
self._hlgap: float = 0.5
self._debug: bool = False

def get_identifier(self) -> str:
Expand Down Expand Up @@ -391,6 +392,24 @@ def engine(self, engine: str):
raise ValueError("Refinement engine can only be xtb or orca.")
self._engine = engine

@property
def hlgap(self):
"""
Get the minimum HOM
"""
return self._hlgap

@hlgap.setter
def hlgap(self, hlgap: float):
"""
Set the minimum HOM
"""
if not isinstance(hlgap, float):
raise TypeError("Minimum HL gap should be a float.")
if hlgap < 0:
raise ValueError("Minimum HL gap should be greater than 0.")
self._hlgap = hlgap

@property
def debug(self):
"""
Expand Down