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

Docs: Add example of LammpsCalculation with script input #74

Merged
merged 1 commit into from
May 14, 2023
Merged
Changes from all commits
Commits
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
106 changes: 106 additions & 0 deletions examples/launch_lammps_base_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""Run a LAMMPS calculation from a pre-defined complete input script.
The example input script is taken from https://www.lammps.org/inputs/in.lj.txt and is the first example script for the
official benchmarks of LAMMPS. It is a simple NVE simulation of a Lennard-Jones liquid. When passing a complete input
script for the ``script`` input of the ``LammpsBaseCalculation``, other inputs, such as ``structure`` and ``potential``
no longer have to be specified.
"""
import io
import textwrap

from aiida import orm
from aiida.engine import run_get_node
from aiida.plugins import CalculationFactory
import numpy as np

from aiida_lammps.data.potential import LammpsPotentialData


def main(
script: orm.SinglefileData,
options: AttributeDict,
code: orm.Code,
) -> orm.Node:
"""Submission of the calculation.
:param script: complete input script to be used in the calculation
:type script: orm.SinglefileData
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need types in docstrings, now there is annotations

:param options: options to control the submission parameters
:type options: AttributeDict
:param code: code describing the ``LAMMPS`` calculation
:type code: orm.Code
:return: node containing the ``LAMMPS`` calculation
:rtype: orm.Node
"""
calculation = CalculationFactory("lammps.base")

builder = calculation.get_builder()
builder.code = code
builder.script = script
builder.metadata.options = options

_, node = run_get_node(calculation, **builder)

return node


if __name__ == "__main__":

# Get the lammps code defined in AiiDA database
CODE = orm.load_code("lammps-23.06.2022@localhost")
# Define the parameters for the resources requested for the calculation
OPTIONS = AttributeDict()
OPTIONS.resources = AttributeDict()
# Total number of machines used
OPTIONS.resources.num_machines = 1
# Total number of mpi processes
OPTIONS.resources.tot_num_mpiprocs = 2

# Define the complete input script that should be run
SCRIPT = orm.SinglefileData(
io.StringIO(
textwrap.dedent(
"""
# 3d Lennard-Jones melt
variable x index 1
variable y index 1
variable z index 1
variable xx equal 20*$x
variable yy equal 20*$y
variable zz equal 20*$z
units lj
atom_style atomic
lattice fcc 0.8442
region box block 0 ${xx} 0 ${yy} 0 ${zz}
create_box 1 box
create_atoms 1 box
mass 1 1.0
velocity all create 1.44 87287 loop geom
pair_style lj/cut 2.5
pair_coeff 1 1 1.0 1.0 2.5
neighbor 0.3 bin
neigh_modify delay 0 every 20 check no
fix 1 all nve
run 100
"""
)
)
)

# Run the aiida-lammps calculation
submission_node = main(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit arbitrary to have part of the script as a function, but not the end of the world

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I agree but I was just keeping with the style of the other examples. I would personally also simplify and improve them, but didn't want to do this in this PR and create an example that is different from all the others in style.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep no worries 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think that the examples and documentation need to be improved quite a bit before release. I just thought it would be good to have a baseline where to start. I'll make an issue about that so that we can keep track of it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So let's merge this one and then fix all of the examples in one go to make sure they are consistent

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it is okay to merge, unless @chrisjsewell has strong feeling about it I think it can be done, and then we can focus on the documentation/fixing the examples to be more consistent.

script=SCRIPT,
options=OPTIONS,
code=CODE,
)

print(f"Calculation node: {submission_node}")