Skip to content

Commit

Permalink
feat: custom step (#346)
Browse files Browse the repository at this point in the history
* feat: custom topology mod as a recipe step

* chore: doc wording

* format
  • Loading branch information
jmbuhr committed Dec 12, 2023
1 parent e308279 commit 5b28f04
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/kimmdy/recipe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Contains the Reaction Recipe, RecipeStep and RecipeCollection.
"""
from __future__ import annotations
from typing import Optional
from typing import TYPE_CHECKING, Callable, Optional

from abc import ABC
from dataclasses import dataclass, field
Expand All @@ -14,6 +14,9 @@

logger = logging.getLogger(__name__)

if TYPE_CHECKING:
from kimmdy.topology.topology import Topology


class RecipeStep(ABC):
"""Base class for all RecipeSteps.
Expand Down Expand Up @@ -210,6 +213,19 @@ class Bind(BondOperation):
"""


@dataclass
class CustomTopMod(RecipeStep):
"""A custom recipe step that can be used to define a custom topology modification.
Parameters
----------
f : Callable[[Topology], Topology]
A function that takes a Topology object and modifies it in place.
"""

f: Callable[[Topology], None]


@dataclass
class Recipe:
"""A reaction path defined by one series of RecipeSteps.
Expand Down Expand Up @@ -328,6 +344,9 @@ def get_recipe_name(self):
elif isinstance(rs, Relax):
pass

elif isinstance(rs, CustomTopMod):
name += "🪛"

else:
logger.warning(f"get_recipe_name got unknown step type: {type(rs)}")
name += "?".join(list(map(str, rs.__dict__.values())))
Expand Down
5 changes: 4 additions & 1 deletion src/kimmdy/runmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
reaction_plugins,
ReactionPlugin,
)
from kimmdy.recipe import RecipeCollection, Break, Bind, Place, Relax
from kimmdy.recipe import CustomTopMod, RecipeCollection, Break, Bind, Place, Relax
from kimmdy.utils import run_gmx, truncate_sim_files
from kimmdy.coordinates import place_atom, break_bond_plumed, merge_top_slow_growth
from kimmdy.tasks import Task, TaskFiles, get_plumed_out
Expand Down Expand Up @@ -624,6 +624,9 @@ def _apply_recipe(self, files: TaskFiles) -> TaskFiles:
logger.info(f"Finished task: {task.name}")
self._discover_output_files(task.name, md_files)

elif isinstance(step, CustomTopMod):
step.f(self.top)

write_top(self.top.to_dict(), files.outputdir / self.config.top.name)
files.output["top"] = files.outputdir / self.config.top.name

Expand Down

0 comments on commit 5b28f04

Please sign in to comment.