|
| 1 | +# Copyright 2025 InstaDeep Ltd |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Custom ASE simulation engine. |
| 15 | +
|
| 16 | +This module is kept separate from :mod:`mlipaudit.utils.simulation` because |
| 17 | +importing it pulls in the heavy ``mlip.simulation.ase`` (and therefore JAX-MD) |
| 18 | +stack. It is only imported lazily, when a simulation is actually run, so that |
| 19 | +merely importing the benchmark modules (e.g. for the CLI) stays fast. |
| 20 | +""" |
| 21 | + |
| 22 | +import logging |
| 23 | +from typing import Callable |
| 24 | + |
| 25 | +import ase |
| 26 | +from ase.calculators.calculator import Calculator as ASECalculator |
| 27 | +from mlip.simulation import SimulationState |
| 28 | +from mlip.simulation.ase import ASESimulationEngine |
| 29 | +from mlip.simulation.configs import ASESimulationConfig |
| 30 | +from mlip.simulation.enums import SimulationType |
| 31 | +from mlip.simulation.temperature_scheduling import get_temperature_schedule |
| 32 | + |
| 33 | +logger = logging.getLogger("mlipaudit") |
| 34 | + |
| 35 | + |
| 36 | +class ASESimulationEngineWithCalculator(ASESimulationEngine): |
| 37 | + """Class derived from mlip's ASE simulation engine but allowing for a passed |
| 38 | + ASE calculator object. |
| 39 | + """ |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + atoms: ase.Atoms, |
| 44 | + ase_calculator: ASECalculator, |
| 45 | + config: ASESimulationConfig, |
| 46 | + ) -> None: |
| 47 | + """Overridden constructor that takes in an ASE calculator instead of an |
| 48 | + mlip force field class. |
| 49 | +
|
| 50 | + Args: |
| 51 | + atoms: The ASE atoms. |
| 52 | + ase_calculator: The ASE calculator to use in the simulation. |
| 53 | + config: The simulation config. |
| 54 | + """ |
| 55 | + self.state = SimulationState() |
| 56 | + self.loggers: list[Callable[[SimulationState], None]] = [] |
| 57 | + |
| 58 | + logger.debug("Initialization of simulation begins...") |
| 59 | + self._config = config |
| 60 | + self.atoms = atoms |
| 61 | + self.atoms.center() |
| 62 | + positions = atoms.get_positions() |
| 63 | + self._num_atoms = positions.shape[0] |
| 64 | + self.state.atomic_numbers = atoms.numbers |
| 65 | + |
| 66 | + self._init_box() |
| 67 | + |
| 68 | + self.is_md_simulation = self._config.simulation_type == SimulationType.MD |
| 69 | + self.is_npt_simulation = ( |
| 70 | + self.is_md_simulation and self._config.md_integrator.ensemble == "npt" |
| 71 | + ) |
| 72 | + |
| 73 | + self.model_calculator = ase_calculator |
| 74 | + |
| 75 | + self._temperature_schedule = get_temperature_schedule( |
| 76 | + self._config.temperature_schedule_config, self._config.num_steps |
| 77 | + ) |
| 78 | + |
| 79 | + logger.debug("Initialization of simulation completed.") |
0 commit comments