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

Add callback function for LAMMPS #458

Merged
merged 17 commits into from
Dec 7, 2021
Merged

Add callback function for LAMMPS #458

merged 17 commits into from
Dec 7, 2021

Conversation

samwaseda
Copy link
Member

@samwaseda samwaseda commented Dec 3, 2021

callback is one of the features of LAMMPS which makes it extremely efficient while being still very interactive. In short, you can append a function that returns forces which are then added to the total forces calculated in LAMMPS. You can find more info on this page.

Example I: Add random forces

def random_forces(x, *args):
    return 0.1 * np.random.randn(*x.shape)

lmp = pr.create_job('Lammps', 'random_forces')
lmp.structure = pr.create.structure.bulk('Ni', cubic=True).repeat(3)
lmp.server.run_mode.interactive = True
lmp.set_callback(random_forces)
lmp.calc_md()
lmp.run()
lmp.interactive_close()

Example II: Push H in (1, 1, 1)-direction

class CallBack:
    def __init__(self, job):
        self.job = job

    def push_hydrogen(self, x, *args):
        f = np.zeros_like(self.job.structure.positions)
        f[self.job.structure.select_index('H')[0]] = np.array(3*[0.5])
        return f

lmp = pr.create_job('Lammps', 'push_hydrogen')
lmp.structure = pr.create.structure.bulk('Ni', cubic=True).repeat(3)
lmp.structure += pr.create.structure.atoms(
    elements=['H'], positions=[lmp.structure.analyse.get_voronoi_vertices()[0]]
)
callback = CallBack(lmp)
lmp.server.run_mode.interactive = True
lmp.set_callback(callback.push_hydrogen)
lmp.calc_md()
lmp.run()
lmp.interactive_close()

@coveralls
Copy link

coveralls commented Dec 3, 2021

Pull Request Test Coverage Report for Build 1546466930

  • 15 of 18 (83.33%) changed or added relevant lines in 1 file are covered.
  • 24 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.08%) to 69.923%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pyiron_atomistics/lammps/interactive.py 15 18 83.33%
Files with Coverage Reduction New Missed Lines %
pyiron_atomistics/atomistics/structure/factory.py 24 76.74%
Totals Coverage Status
Change from base Build 1531836487: 0.08%
Covered Lines: 11503
Relevant Lines: 16451

💛 - Coveralls

Copy link
Member

@liamhuber liamhuber left a comment

Choose a reason for hiding this comment

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

Super cool, I think this is absolutely very useful. One small syntax tweak requested in the code itself, and a whole bunch of (sometimes optional) documentation request.

Looking at it from 30k ft, the big issue I see is how to connect this to our serialization and reproducibility. As far as I can see, right now if you activate this functionality the final serialized job will give no indication of what was done, and so you'll just strangely get a trajectory whose forces and evolution don't align with your potential+thermo/barostatting. I guess this is already the case for jobs we mess with interactively. Since you explicitly list this as an "expert feature", I don't think this concern should stop this PR from merging, and I don't have a good solution myself right now anyhow -- but we should think about how to handle this...maybe some sort of my_callback = pr.atomistics.lammps.Callback(fnc=my_function_defined_elsewhere); lmp.set_callback(my_callback.fnc) (or lmp.set_callback(my_callback), where we check if the argument is an instance of our fancy new class and parse accordingly), and then handle serialization of the raw function code under the hood?

*** Expert feature ***
**********************

Set callback function that modifies forces.
Copy link
Member

Choose a reason for hiding this comment

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

add example 1 to docstring

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean example 2? (1 is already there)

pyiron_atomistics/lammps/interactive.py Show resolved Hide resolved
pyiron_atomistics/lammps/interactive.py Outdated Show resolved Hide resolved
pyiron_atomistics/lammps/interactive.py Outdated Show resolved Hide resolved
pyiron_atomistics/lammps/interactive.py Outdated Show resolved Hide resolved
tests/lammps/test_interactive.py Show resolved Hide resolved
pyiron_atomistics/lammps/interactive.py Outdated Show resolved Hide resolved
pyiron_atomistics/lammps/interactive.py Show resolved Hide resolved
@samwaseda samwaseda merged commit 73a235d into master Dec 7, 2021
@delete-merged-branch delete-merged-branch bot deleted the callback branch December 7, 2021 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants