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

Dev/sym opt #111

Merged
merged 26 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f6a3282
Interface for the qm program turbomole
jonathan-schoeps Dec 18, 2024
d6c60c2
support for turbomole
jonathan-schoeps Jan 8, 2025
eefcb3f
Merge branch 'main' into dev/tm_interface
jonathan-schoeps Jan 8, 2025
0a20340
corrected some typos
jonathan-schoeps Jan 8, 2025
e4f79b7
structure_mod baseline
jonathan-schoeps Jan 10, 2025
44cec38
Merge branch 'main' into dev/symmetrie_operations
jonathan-schoeps Jan 10, 2025
a1f9716
baseline for the symmetrie class
jonathan-schoeps Jan 10, 2025
144e309
Implementation of the structur modification module containing Cn rota…
jonathan-schoeps Jan 17, 2025
6e2c63f
merged recent changes
jonathan-schoeps Jan 21, 2025
e5513ec
update to the structure_modification routine and implemented unit tests.
jonathan-schoeps Jan 22, 2025
7d0e5b2
removal of left over dead code snippets
jonathan-schoeps Jan 22, 2025
6c099b0
changed a typo in the change log
jonathan-schoeps Jan 23, 2025
245c915
Re structerization of Structure_modification
jonathan-schoeps Jan 24, 2025
90fad3e
Merge branch 'main' into dev/sym_opt
jonathan-schoeps Jan 24, 2025
36cb528
bug fixes to get the structure mod classes working
jonathan-schoeps Jan 27, 2025
a7ae337
changed a word in mindlessgen.toml
jonathan-schoeps Jan 27, 2025
c7b1bf3
reorganize translation and initialization
marcelmbn Jan 27, 2025
09c6f73
refactored object-orientation of structure modification classes; fixe…
marcelmbn Jan 27, 2025
a6b61d5
abstract structure merge; minor revisions for config
marcelmbn Jan 28, 2025
d5b5fc8
Fix to print the correct output of tm and remove of a redundant singl…
jonathan-schoeps Jan 28, 2025
f7599c8
distance check for clashes and iterative distance increase if too low
marcelmbn Jan 28, 2025
76b06ff
rename some file and Class names to typical conventions
marcelmbn Jan 28, 2025
2fe6b50
further variable renaming; config clean-up
marcelmbn Jan 28, 2025
4b25ca4
add CLI access for arguments and minor adaptions
marcelmbn Jan 28, 2025
3354eac
update default TOML file
marcelmbn Jan 28, 2025
379b6ab
some renamings, better docstrings, remove useless variable copies
marcelmbn Jan 28, 2025
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- `GXTBConfig` class for the g-xTB method, supporting SCF cycles check
- support for TURBOMOLE as QM engine.
- support for TURBOMOLE as QM engine
- updated the parallelization to work over the number of molecules
- possibility to generate symmetrical molecules (choice from rotation, inversion, mirroring)

### Fixed
- version string is now correctly formatted and printed
Expand Down
10 changes: 10 additions & 0 deletions mindlessgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ num_molecules = 1
postprocess = false
# > Switch molecule structure XYZ writing on and off. Default: true. Options: <bool>
write_xyz = true
# > Switch structure modification on and off. Defaul: false. Options: <bool>
# > postprocess has to be turned on and the postprocess engine has to be turbomole.
structure_mod = false

[generate]
# > Minimum number of atoms in the generated molecule. Options: <int>
Expand Down Expand Up @@ -104,3 +107,10 @@ functional = "PBE"
basis = "def2-SVP"
# > Maximum number of SCF cycles: Options: <int>
scf_cycles = 100

[modification]
# > There is only the posibillity to do one modification at one molecule.
# > define the distance of the opposit NCI fragments. Options: <float>
distance = 3.0
# > defines the symmetrie operation with which the NCI complex should be generated. Options: <mirror>, <inversion> and <c_<n>_rotation>. For n Options: <int>.
operation = "mirror"
54 changes: 54 additions & 0 deletions src/mindlessgen/Structure_modification/StrucMod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
This module defines the abstract base class for all structure modification methods.
"""

from abc import ABC, abstractmethod

from mindlessgen.molecules.molecule import Molecule
from mindlessgen.prog.config import StructureModConfig


class StrucMod(ABC):
"""
This abstract base class defines the interface for all structure modification methods.
"""

def __init__(self, config: StructureModConfig):
"""
Initialize the structure modification class.
"""
self.cfg = config

@abstractmethod
def modify_structure(
self,
mol: Molecule,
) -> Molecule:
"""
Define the structure modification process.

Arguments:
molecule (Molecule): Molecule to modify
config (StructureModConfig): Configuration for the structure modification

Returns:
Molecule: Modified molecule
"""

# All structure modification methods should be able to translate the molecule
def translation(
self,
mol: Molecule,
) -> Molecule:
"""
Translate the molecule and add a little extra distance to the x axis.
"""
xyz = mol.xyz
# Find the translation vector in x direction
translation_vector = xyz.min(axis=0)[0]
# missing distance to reach minimum x value
xshift = self.cfg.distance / 2 - translation_vector
for i in range(mol.num_atoms):
xyz[i, 0] += xshift
mol.xyz = xyz
return mol
16 changes: 16 additions & 0 deletions src/mindlessgen/Structure_modification/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
This module contains all structure modification classes.
"""

from .StrucMod import StrucMod
from .mirror import Mirror
from .inversion import Inversion
from .rotation import CnRotation


__all__ = [
"StrucMod",
"Mirror",
"Inversion",
"CnRotation",
]
38 changes: 38 additions & 0 deletions src/mindlessgen/Structure_modification/inversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
This class handles the inversion structure modification.
"""

import numpy as np

from .StrucMod import StrucMod
from ..molecules.molecule import Molecule


class Inversion(StrucMod):
"""
This class handles the Inversion structure modification.
"""

def modify_structure(
self,
mol: Molecule,
) -> Molecule:
"""
Invert the molecule.
"""
mol = self.translation(mol)
xyz = mol.xyz
inversion_matrix = np.array([[-1, 0, 0], [0, -1, 0], [0, 0, -1]])
xyz_inversion = xyz.copy()
modified_molecule = mol.copy()
for i in range(mol.num_atoms):
xyz_inversion[i] = np.dot(inversion_matrix, xyz[i])
xyz_combined = np.vstack((xyz, xyz_inversion))
modified_molecule.xyz = xyz_combined
modified_molecule.num_atoms += mol.num_atoms
modified_molecule.ati = np.hstack((modified_molecule.ati, mol.ati))
modified_molecule.charge += mol.charge
modified_molecule.uhf += mol.uhf
modified_molecule.atlist += mol.atlist
modified_molecule.set_name_from_formula()
return modified_molecule
39 changes: 39 additions & 0 deletions src/mindlessgen/Structure_modification/mirror.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
This class handles the inversion structure modification.
"""

import numpy as np

from .StrucMod import StrucMod
from ..molecules.molecule import Molecule


class Mirror(StrucMod):
"""
This class handles the translation structure modification.
"""

def modify_structure(
self,
mol: Molecule,
) -> Molecule:
"""
Mirror the molecule.
"""
mol = self.translation(mol)
xyz = mol.xyz
mirror_matrix = np.array([[-1, 0, 0], [0, 1, 0], [0, 0, 1]])
xyz_mirror = xyz.copy()
modified_molecule = mol.copy()
for i in range(mol.num_atoms):
xyz_mirror[i] = np.dot(mirror_matrix, xyz[i])
# Combine the original and the mirror image
xyz_combined = np.vstack((xyz, xyz_mirror))
modified_molecule.xyz = xyz_combined
modified_molecule.num_atoms += mol.num_atoms
modified_molecule.ati = np.hstack((modified_molecule.ati, mol.ati))
modified_molecule.charge += mol.charge
modified_molecule.uhf += mol.uhf
modified_molecule.atlist += mol.atlist
modified_molecule.set_name_from_formula()
return modified_molecule
47 changes: 47 additions & 0 deletions src/mindlessgen/Structure_modification/rotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
This class handles the rotation structure modification.
"""

import numpy as np

from .StrucMod import StrucMod
from ..molecules.molecule import Molecule


class CnRotation(StrucMod):
"""
This class handles the rotation structure modification.
"""

def modify_structure(
self,
mol: Molecule,
) -> Molecule:
"""
Rotate the molecule around the z-axis.
"""
mol = self.translation(mol)
xyz = mol.xyz
n = self.cfg.rotation
rotation_matrix = np.array(
[
[np.cos((2 * np.pi) / n), -np.sin((2 * np.pi) / n), 0],
[np.sin((2 * np.pi) / n), np.cos((2 * np.pi) / n), 0],
[0, 0, 1],
]
)
xyz_rotation = xyz.copy()
modified_molecule = mol.copy()
# For loop to get n times the molecule
for _ in range(1, n):
for k in range(mol.num_atoms):
xyz_rotation[k] = np.dot(rotation_matrix, xyz_rotation[k])
xyz = np.vstack((xyz, xyz_rotation))
modified_molecule.num_atoms += mol.num_atoms
modified_molecule.ati = np.hstack((modified_molecule.ati, mol.ati))
modified_molecule.charge += mol.charge
modified_molecule.uhf += mol.uhf
modified_molecule.atlist += mol.atlist
modified_molecule.xyz = xyz
modified_molecule.set_name_from_formula()
return modified_molecule
53 changes: 52 additions & 1 deletion src/mindlessgen/generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,19 @@
get_gxtb_path,
)
from ..molecules import iterative_optimization, postprocess_mol
from ..prog import ConfigManager, setup_managers, ResourceMonitor, setup_blocks
from ..prog import (
ConfigManager,
StructureModConfig,
setup_managers,
ResourceMonitor,
setup_blocks,
)
from ..Structure_modification import (
StrucMod,
CnRotation,
Mirror,
Inversion,
)
from ..__version__ import __version__

MINDLESS_MOLECULES_FILE = "mindless.molecules"
Expand Down Expand Up @@ -71,6 +83,13 @@ def generator(config: ConfigManager) -> tuple[list[Molecule], int]:
get_jobex_path,
)

if config.general.structure_mod:
structure_mod_model: StrucMod | None = setup_structure_modification_model(
config.modification.operation, config.modification
)
else:
structure_mod_model = None

if config.general.postprocess:
postprocess_engine: QMMethod | None = setup_engines(
config.postprocess.engine,
Expand Down Expand Up @@ -137,6 +156,7 @@ def generator(config: ConfigManager) -> tuple[list[Molecule], int]:
resources,
refine_engine,
postprocess_engine,
structure_mod_model,
block.ncores,
)
)
Expand Down Expand Up @@ -181,6 +201,7 @@ def single_molecule_generator(
resources: ResourceMonitor,
refine_engine: QMMethod,
postprocess_engine: QMMethod | None,
structure_mod_model: StrucMod,
ncores: int,
) -> Molecule | None:
"""
Expand Down Expand Up @@ -211,6 +232,7 @@ def single_molecule_generator(
resources_local,
refine_engine,
postprocess_engine,
structure_mod_model,
cycle,
stop_event,
)
Expand Down Expand Up @@ -250,6 +272,7 @@ def single_molecule_step(
resources_local: ResourceMonitor,
refine_engine: QMMethod,
postprocess_engine: QMMethod | None,
structure_mod_model: StrucMod,
cycle: int,
stop_event: Event,
) -> Molecule | None:
Expand Down Expand Up @@ -315,6 +338,18 @@ def single_molecule_step(
if config.refine.debug:
stop_event.set()

if config.general.structure_mod:
try:
optimized_molecule = structure_mod_model.modify_structure(
optimized_molecule,
)
except RuntimeError as e:
if config.general.verbosity > 0:
print(f"Structure modification failed for cycle {cycle + 1}.")
if config.general.verbosity > 1:
print(e)
return None

if config.general.postprocess:
try:
optimized_molecule = postprocess_mol(
Expand Down Expand Up @@ -422,3 +457,19 @@ def setup_engines(
return GXTB(path, cfg.gxtb)
else:
raise NotImplementedError("Engine not implemented.")


def setup_structure_modification_model(
structure_mod_type: str, config: StructureModConfig
) -> StrucMod:
"""
Set up the structure modification model.
"""
# TODO: Enable the use of more than one structure modification model at a time
if structure_mod_type.endswith("rotation"):
return CnRotation(config)
if structure_mod_type == "mirror":
return Mirror(config)
if structure_mod_type == "inversion":
return Inversion(config)
raise NotImplementedError("Structure modification not implemented.")
2 changes: 2 additions & 0 deletions src/mindlessgen/prog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
GenerateConfig,
RefineConfig,
PostProcessConfig,
StructureModConfig,
)
from .parallel import setup_managers, ResourceMonitor, setup_blocks

Expand All @@ -29,4 +30,5 @@
"setup_managers",
"ResourceMonitor",
"setup_blocks",
"StructureModConfig",
]
Loading
Loading