Skip to content

Commit

Permalink
Registered "ensemble" and "ensemble_name" class attrs with decorator,…
Browse files Browse the repository at this point in the history
… fixed NVE VerletIntegrator initialization bug
  • Loading branch information
timbernat committed Dec 4, 2024
1 parent 6ecbf9a commit b17f0d3
Showing 1 changed file with 7 additions and 33 deletions.
40 changes: 7 additions & 33 deletions polymerist/mdtools/openmmtools/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from openmm.openmm import Force, MonteCarloBarostat
from openmm.unit import Quantity, kelvin, atmosphere, picosecond

from ...genutils.decorators.classmod import register_subclasses, register_abstract_class_attrs
from ...genutils.fileutils.jsonio.jsonify import make_jsonifiable
from ...genutils.fileutils.jsonio.serialize import QuantitySerializer
from ...genutils.decorators.classmod import register_subclasses


# PARAMETER CLASSES
Expand All @@ -38,6 +38,7 @@ def __post_init__(self) -> None:
# ABSTRACT BASE FOR CREATING ENSEMBLE-SPECIFIC SIMULATION
@dataclass
@register_subclasses(key_attr='ensemble')
@register_abstract_class_attrs('ensemble', 'ensemble_name')
class EnsembleFactory(ABC):
'''Base class for implementing interface for generating ensemble-specific simulations'''
thermo_params : ThermoParameters
Expand All @@ -48,18 +49,6 @@ def from_thermo_params(cls, thermo_params : ThermoParameters) -> 'EnsembleFactor
return EnsembleFactory.subclass_registry[thermo_params.ensemble](thermo_params)

# ENSEMBLE NAMING ATTRIBUTES
@abstractproperty
@classmethod
def ensemble(cls) -> str: # to be implemented for each particular ensemble
'''Specify state variables of ensemble'''
pass

@abstractproperty
@classmethod
def ensemble_name(cls) -> str: # to be implemented for each particular ensemble
'''Specify name of ensemble'''
pass

@property
def desc(self) -> str:
'''Verbal description of ensemble'''
Expand Down Expand Up @@ -99,38 +88,23 @@ def forces(self) -> Optional[Iterable[Force]]:

# CONCRETE IMPLEMENTATIONS OF ENSEMBLES
@dataclass
class NVESimulationFactory(EnsembleFactory):
thermo_params : ThermoParameters

ensemble : ClassVar[str] = 'NVE'
ensemble_name : ClassVar[str] = 'microcanonical'

class NVESimulationFactory(EnsembleFactory, ensemble='NVE', ensemble_name='microcanonical'):
def _integrator(self, time_step : Quantity) -> Integrator:
return VerletIntegrator(stepSize=time_step)
return VerletIntegrator(time_step)

def _forces(self) -> Optional[Iterable[Force]]:
return None

@dataclass
class NVTSimulationFactory(EnsembleFactory): # TODO : add implementation support for Andersen and Nose-Hoover thermostats (added to forces instead)
thermo_params : ThermoParameters

ensemble : ClassVar[str] = 'NVT'
ensemble_name : ClassVar[str] = 'canonical'

class NVTSimulationFactory(EnsembleFactory, ensemble='NVT', ensemble_name='canonical'):
def _integrator(self, time_step : Quantity) -> Integrator:
return LangevinMiddleIntegrator(self.thermo_params.temperature, self.thermo_params.friction_coeff, time_step)

def _forces(self) -> Optional[Iterable[Force]]:
return None
return None # TODO : add implementation support for Andersen and Nose-Hoover thermostats (added to forces instead)

@dataclass
class NPTSimulationFactory(EnsembleFactory):
thermo_params : ThermoParameters

ensemble : ClassVar[str] = 'NPT'
ensemble_name : ClassVar[str] = 'isothermal-isobaric'

class NPTSimulationFactory(EnsembleFactory, ensemble='NPT', ensemble_name='isothermal-isobaric'):
def _integrator(self, time_step : Quantity) -> Integrator:
return LangevinMiddleIntegrator(self.thermo_params.temperature, self.thermo_params.friction_coeff, time_step)

Expand Down

0 comments on commit b17f0d3

Please sign in to comment.