Skip to content

Commit

Permalink
LAMMPS: Move commands to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-janssen committed Nov 28, 2023
1 parent 1e3f16a commit 9ecaf8d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
79 changes: 48 additions & 31 deletions atomistics/calculators/lammps/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
from pylammpsmpi import LammpsASELibrary

from atomistics.calculators.wrapper import as_task_dict_evaluator
from atomistics.calculators.lammps.commands import (
LAMMPS_THERMO_STYLE,
LAMMPS_THERMO,
LAMMPS_MINIMIZE,
LAMMPS_RUN,
LAMMPS_MINIMIZE_VOLUME,
)

if TYPE_CHECKING:
from ase import Atoms
Expand All @@ -15,40 +22,33 @@
from atomistics.calculators.interface import TaskName


LAMMPS_STATIC_RUN_INPUT_TEMPLATE = """\
thermo_style custom step temp pe etotal pxx pxy pxz pyy pyz pzz vol
thermo_modify format float %20.15g
thermo 100
run 0"""


LAMMPS_MINIMIZE_POSITIONS_INPUT_TEMPLATE = """\
thermo_style custom step temp pe etotal pxx pxy pxz pyy pyz pzz vol
thermo_modify format float %20.15g
thermo 10
min_style {{min_style}}
minimize {{etol}} {{ftol}} {{maxiter}} {{maxeval}}"""


LAMMPS_MINIMIZE_POSITIONS_AND_VOLUME_INPUT_TEMPLATE = """\
fix ensemble all box/relax iso 0.0
thermo_style custom step temp pe etotal pxx pxy pxz pyy pyz pzz vol
thermo_modify format float %20.15g
thermo 10
min_style {{min_style}}
minimize {{etol}} {{ftol}} {{maxiter}} {{maxeval}}"""


def template_render(
def template_render_minimize(
template_str,
min_style="cg",
etol=0.0,
ftol=0.0001,
maxiter=100000,
maxeval=10000000,
thermo=10,
):
return Template(template_str).render(
min_style=min_style, etol=etol, ftol=ftol, maxiter=maxiter, maxeval=maxeval
min_style=min_style,
etol=etol,
ftol=ftol,
maxiter=maxiter,
maxeval=maxeval,
thermo=thermo,
)


def template_render_run(
template_str,
run=0,
thermo=100,
):
return Template(template_str).render(
run=run,
thermo=thermo,
)


Expand All @@ -62,11 +62,20 @@ def evaluate_with_lammps_library(
):
results = {}
if "optimize_positions_and_volume" in tasks:
template_str = (
LAMMPS_MINIMIZE_VOLUME
+ "\n"
+ LAMMPS_THERMO_STYLE
+ "\n"
+ LAMMPS_THERMO
+ "\n"
+ LAMMPS_MINIMIZE
)
lmp = _run_simulation(
structure=structure,
potential_dataframe=potential_dataframe,
input_template=template_render(
template_str=LAMMPS_MINIMIZE_POSITIONS_AND_VOLUME_INPUT_TEMPLATE,
input_template=template_render_minimize(
template_str=template_str,
**lmp_optimizer_kwargs,
),
lmp=lmp,
Expand All @@ -76,11 +85,14 @@ def evaluate_with_lammps_library(
structure_copy.positions = lmp.interactive_positions_getter()
results["structure_with_optimized_positions_and_volume"] = structure_copy
elif "optimize_positions" in tasks:
template_str = (
LAMMPS_THERMO_STYLE + "\n" + LAMMPS_THERMO + "\n" + LAMMPS_MINIMIZE
)
lmp = _run_simulation(
structure=structure,
potential_dataframe=potential_dataframe,
input_template=template_render(
template_str=LAMMPS_MINIMIZE_POSITIONS_INPUT_TEMPLATE,
input_template=template_render_minimize(
template_str=template_str,
**lmp_optimizer_kwargs,
),
lmp=lmp,
Expand All @@ -89,10 +101,15 @@ def evaluate_with_lammps_library(
structure_copy.positions = lmp.interactive_positions_getter()
results["structure_with_optimized_positions"] = structure_copy
elif "calc_energy" in tasks or "calc_forces" in tasks:
template_str = LAMMPS_THERMO_STYLE + "\n" + LAMMPS_THERMO + "\n" + LAMMPS_RUN
lmp = _run_simulation(
structure=structure,
potential_dataframe=potential_dataframe,
input_template=LAMMPS_STATIC_RUN_INPUT_TEMPLATE,
input_template=template_render_run(
template_str=template_str,
thermo=100,
run=0,
),
lmp=lmp,
)
if "calc_energy" in tasks:
Expand Down
17 changes: 17 additions & 0 deletions atomistics/calculators/lammps/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
LAMMPS_THERMO_STYLE = """\
thermo_style custom step temp pe etotal pxx pxy pxz pyy pyz pzz vol
thermo_modify format float %20.15g"""


LAMMPS_THERMO = "thermo {{thermo}}"


LAMMPS_RUN = "run {{run}}"


LAMMPS_MINIMIZE = """\
min_style {{min_style}}
minimize {{etol}} {{ftol}} {{maxiter}} {{maxeval}}"""


LAMMPS_MINIMIZE_VOLUME = "fix ensemble all box/relax iso 0.0"

0 comments on commit 9ecaf8d

Please sign in to comment.