Skip to content

Commit

Permalink
add previously fixed parameters to the user-accessible configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Müller <[email protected]>
  • Loading branch information
marcelmbn committed Aug 16, 2024
1 parent 5a007ea commit 71a06a2
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 28 deletions.
29 changes: 14 additions & 15 deletions mindlessgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,34 @@
# 3. User's home directory (`Path.home()`)

[general]
# Verbosity level defining the printout: Options: 0 = silent, 1 = default, 2 = verbose
# > Verbosity level defining the printout: Options: 0 = silent, 1 = default, 2 = verbose, 3 = debug
verbosity = 1

# Number of parallel processes to use. Options: <int>
# > Number of parallel processes to use. Options: <int>
parallel = 1

# Quantum Mechanics (QM) engine to use. Options: 'xtb', 'orca'
# > Quantum Mechanics (QM) engine to use. Options: 'xtb', 'orca'
engine = "xtb"

# Maximum number of optimization cycles. Options: <int>
# > Maximum number of optimization cycles. Options: <int>
max_cycles = 100

[generate]
# Minimum number of atoms in the generated molecule. Options: <int>
# > Minimum number of atoms in the generated molecule. Options: <int>
min_num_atoms = 2

# Maximum number of atoms in the generated molecule. Options: <int>
# > Maximum number of atoms in the generated molecule. Options: <int>
max_num_atoms = 100
# > Initial coordinate scaling factor. Options: <float>
init_scaling = 3.0
# > Increase in the coordinate scaling factor per trial after dist_threshold was not met. Options: <float>
increase_scaling_factor = 1.3
# > Distance threshold for the inital, randomly generated coordinates. Options: <float>
dist_threshold = 1.2

[refine]
# Maximum number of fragment optimization cycles. Options: <int>
# > Maximum number of fragment optimization cycles. Options: <int>
max_frag_cycles = 100

[xtb]
# > Path to the xtb executable. Options: <str | Path>
xtb_path = "/path/to/xtb"
# TODO
# Specific configurations for the XTB engine (if needed)
# xtb_option_1 = "value1"
# xtb_option_2 = "value2"

[orca]
# TODO
Expand Down
22 changes: 22 additions & 0 deletions src/mindlessgen/cli/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ def cli_parser(argv: Sequence[str] | None = None) -> dict:
required=False,
help="Maximum number of atoms in a molecule.",
)
parser.add_argument(
"--init-coord-scaling",
type=float,
required=False,
help="Initial coordinate scaling factor.",
)
parser.add_argument(
"--increase-scaling-factor",
type=float,
required=False,
help="Factor with which the coordinate scaling factor is increased "
+ "after a failed attempt.",
)
parser.add_argument(
"--dist-threshold",
type=float,
required=False,
help="Distance threshold for generating coordinates.",
)
parser.add_argument(
"--max-frag-cycles",
type=int,
Expand Down Expand Up @@ -104,6 +123,9 @@ def cli_parser(argv: Sequence[str] | None = None) -> dict:
rev_args_dict["generate"] = {
"min_num_atoms": args_dict["min_num_atoms"],
"max_num_atoms": args_dict["max_num_atoms"],
"init_coord_scaling": args_dict["init_coord_scaling"],
"increase_scaling_factor": args_dict["increase_scaling_factor"],
"dist_threshold": args_dict["dist_threshold"],
}
# XTB specific arguments
rev_args_dict["xtb"] = {"xtb_path": args_dict["xtb_path"]}
Expand Down
19 changes: 10 additions & 9 deletions src/mindlessgen/molecules/generate_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
from .molecule import Molecule
from .miscellaneous import set_random_charge

DEFAULT_SCALING = 3.0
DEFAULT_DIST_THRESHOLD = 1.2
EXPANSION_FACTOR = 1.3


def generate_random_molecule(
config_generate: GenerateConfig, verbosity: int
Expand All @@ -29,8 +25,9 @@ def generate_random_molecule(
mol.num_atoms = np.sum(mol.atlist)
mol.xyz, mol.ati = generate_coordinates(
at=mol.atlist,
scaling=DEFAULT_SCALING,
dist_threshold=DEFAULT_DIST_THRESHOLD,
scaling=config_generate.init_coord_scaling,
dist_threshold=config_generate.dist_threshold,
inc_scaling_factor=config_generate.increase_scaling_factor,
verbosity=verbosity,
)
mol.charge = set_random_charge(mol.ati, verbosity)
Expand Down Expand Up @@ -187,7 +184,11 @@ def generate_atom_list(


def generate_coordinates(
at: np.ndarray, scaling: float, dist_threshold: float, verbosity: int = 1
at: np.ndarray,
scaling: float,
dist_threshold: float,
inc_scaling_factor: float = 1.3,
verbosity: int = 1,
) -> tuple[np.ndarray, np.ndarray]:
"""
Generate random coordinates for a molecule.
Expand All @@ -201,10 +202,10 @@ def generate_coordinates(
while not check_distances(xyz, dist_threshold):
if verbosity > 1:
print(
f"Distance check failed. Increasing expansion factor by {EXPANSION_FACTOR}..."
f"Distance check failed. Increasing expansion factor by {inc_scaling_factor}..."
)
xyz, ati = generate_random_coordinates(at)
eff_scaling = eff_scaling * EXPANSION_FACTOR
eff_scaling = eff_scaling * inc_scaling_factor
xyz = xyz * eff_scaling

return xyz, ati
Expand Down
57 changes: 57 additions & 0 deletions src/mindlessgen/prog/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class GenerateConfig(BaseConfig):
def __init__(self):
self._min_num_atoms: int = 2
self._max_num_atoms: int = 100
self._init_coord_scaling: float = 3.0
self._dist_threshold: float = 1.2
self._increase_scaling_factor: float = 1.3

def get_identifier(self) -> str:
return "generate"
Expand Down Expand Up @@ -172,6 +175,60 @@ def max_num_atoms(self, max_num_atoms: int):
raise ValueError("Max num atoms should be greater than 0.")
self._max_num_atoms = max_num_atoms

@property
def init_coord_scaling(self):
"""
Get the initial coordinate scaling.
"""
return self._init_coord_scaling

@init_coord_scaling.setter
def init_coord_scaling(self, init_coord_scaling: float):
"""
Set the initial coordinate scaling.
"""
if not isinstance(init_coord_scaling, float):
raise TypeError("Initial coordinate scaling should be a float.")
if init_coord_scaling <= 0:
raise ValueError("Initial coordinate scaling should be greater than 0.")
self._init_coord_scaling = init_coord_scaling

@property
def dist_threshold(self):
"""
Get the distance threshold.
"""
return self._dist_threshold

@dist_threshold.setter
def dist_threshold(self, dist_threshold: float):
"""
Set the distance threshold.
"""
if not isinstance(dist_threshold, float):
raise TypeError("Distance threshold should be a float.")
if dist_threshold <= 0:
raise ValueError("Distance threshold should be greater than 0.")
self._dist_threshold = dist_threshold

@property
def increase_scaling_factor(self):
"""
Get the increase scaling factor.
"""
return self._increase_scaling_factor

@increase_scaling_factor.setter
def increase_scaling_factor(self, increase_scaling_factor: float):
"""
Set the increase scaling factor.
"""
if not isinstance(increase_scaling_factor, float):
raise TypeError("Increase scaling factor should be a float.")
if increase_scaling_factor <= 1:
raise ValueError("Increase scaling factor should be greater than 1.")
self._increase_scaling_factor = increase_scaling_factor


class RefineConfig(BaseConfig):
"""
Expand Down
38 changes: 37 additions & 1 deletion test/test_config/test_config_set_attributes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import pytest
from mindlessgen.prog import GeneralConfig, GenerateConfig, RefineConfig # type: ignore
from mindlessgen.prog import ( # type: ignore
GeneralConfig,
GenerateConfig,
RefineConfig,
XTBConfig,
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -38,6 +43,12 @@ def test_general_config_property_setters(
("min_num_atoms", 5, "two", TypeError),
("max_num_atoms", 80, -10, ValueError),
("max_num_atoms", 80, None, TypeError),
("init_coord_scaling", 1.0, -0.5, ValueError),
("init_coord_scaling", 1.0, "1.0", TypeError),
("dist_threshold", 1.5, -1.0, ValueError),
("dist_threshold", 1.5, "1.5", TypeError),
("increase_scaling_factor", 1.1, 0.0, ValueError),
("increase_scaling_factor", 1.1, "1.1", TypeError),
],
)
def test_generate_config_property_setters(
Expand Down Expand Up @@ -96,6 +107,9 @@ def test_general_config_default_values(property_name, initial_value):
[
("min_num_atoms", 2),
("max_num_atoms", 100),
("init_coord_scaling", 3.0),
("dist_threshold", 1.2),
("increase_scaling_factor", 1.3),
],
)
def test_generate_config_default_values(property_name, initial_value):
Expand All @@ -112,3 +126,25 @@ def test_generate_config_default_values(property_name, initial_value):
def test_refine_config_default_values(property_name, initial_value):
config = RefineConfig()
assert getattr(config, property_name) == initial_value


# Generate tests for XTBConfig
@pytest.mark.parametrize(
"property_name, valid_value, invalid_value, expected_exception",
[
("xtb_path", "path/to/xtb", 123, TypeError),
("xtb_path", "path/to/xtb", None, TypeError),
],
)
def test_xtb_config_property_setters(
property_name, valid_value, invalid_value, expected_exception
):
config = XTBConfig()

# Test valid value
setattr(config, property_name, valid_value)
assert getattr(config, property_name) == valid_value

# Test invalid value
with pytest.raises(expected_exception):
setattr(config, property_name, invalid_value)
2 changes: 1 addition & 1 deletion test/test_molecules/test_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_iterative_optimization(
config.refine.max_frag_cycles = 1
if config.general.engine == "xtb":
try:
xtb_path = get_xtb_path(["xtb_dev", "xtb"])
xtb_path = get_xtb_path()
if not xtb_path:
raise ImportError("xtb not found.")
except ImportError as e:
Expand Down
4 changes: 2 additions & 2 deletions test/test_qm/test_xtb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_xtb_optimize_xtb(coordinates_ethanol: np.ndarray) -> None:
"""
Test the optimization of ethanol with xtb.
"""
xtb_path = get_xtb_path(["xtb_dev", "xtb"])
xtb_path = get_xtb_path()
if xtb_path:
xtb = XTB(xtb_path)
else:
Expand Down Expand Up @@ -114,7 +114,7 @@ def test_check_gap_low_gap(mol_C2H4N1O1Au1: Molecule, mol_H3B4Pd1Rn1: Molecule):
"""

try:
xtb_path = get_xtb_path(["xtb_dev", "xtb"])
xtb_path = get_xtb_path()
if not xtb_path:
raise ImportError("xtb not found.")
except ImportError as e:
Expand Down

0 comments on commit 71a06a2

Please sign in to comment.