aboutsummaryrefslogtreecommitdiff

cl-lammps

Common Lisp library to generate LAMMPS input files.

My goal right now is to make this work for my own usecases, which probably include less than 10% of what LAMMPS can actually do, if this project survives for a while I may consider supporting more functionality.

Motivation

LAMMPS is a great piece of software if you consider all of the efforts put into optimizing its algorithms and parallelizing computations, but when it comes to its language and its library interfaces, it really lacks some taste.

One cannot simply pass strings using Python interpolation and call it a 'library interface'. This is not a library interface:

def potential(v0):
    lammps.command(f"variable potential equal {v0}")

This is more like it:

def potential(v0):
    lammps.variable(id="potential", equal=v0)

This does not really make any difference until one has to work with if statements or LAMMPS loops: it really sucks not having some sensible language to wrap those constructs.

Not that I'm actually coding up anything crazy here, but at least I bother creating functions that abstract most of the LAMMPS language, explicitly require needed arguments and maybe add some syntax checks here and there.

Goals

  • Abstract LAMMPS' idiosyncratic language away
  • Try to implement things in a lisp-y way, e.g. implement loops with a for macro that abstracts all of LAMMPS gotos, express calculations in common lisp and translate them to LAMMPS code automatically
  • Provide some rudimentary syntax checking, e.g. avoid repeated IDs and unknown arguments to computes
  • Automatically insert c_, v_, ${} in calculations

Dependencies

All of the Common Lisp libraries needed will be installed by Quicklisp when first loading the system.

Installation

Clone this repository where quicklisp can locate it e.g. in $HOME/quicklisp/local-projects:

git clone https://git.marioforzanini.com/cl-lammps

Then fire up a Common Lisp image and load the lammps system:

(ql:quickload :lammps)

To run the unit tests:

(ql:quickload :fiveam)
(asdf:test-system :lammps)

Usage

See the examples in examples/examples.lisp, you can play around with them loading the `lammps/examples system:

> (asdf:load-system :lammps/examples)
> (lammps.examples:test-simulation 1000 1e-7)

This function returns the LAMMPS code needed to run the simulation, including translating math expressions written in Common Lisp to LAMMPS code, with the proper "${}" and "c_". This functionality is still limited and cannot evaluate the variables passed to the function in the lammps:lammps-math calls, so those variables need to be set as LAMMPS :equal variables, before they can be referenced to in math expressions, e.g.:

> (lammps:defsimulation simulation (some-var)
      "Simulation to show CL-LAMMPS' problems."
    (lammps:variable-equal :var (lammps:math (* 5 some-var))))

> (simulation 5)
; Evaluation aborted on #<UNBOUND-VARIABLE SOME-VAR {1001922473}>

The right way to do this is to first wrap the value in a variable-equal definition:

> (lammps:defsimulation simulation (some-var)
      "Simulation to show CL-LAMMPS' problems."
    (lammps:variable-equal :some-var some-var)
    (lammps:variable-equal :var (lammps:math (* 5 :some-var))))

> (simulation 5)
variable SOME_VAR equal 5
variable VAR equal (5*${SOME_VAR})

License

Copyright © 2023 Mario Forzanini

GPL3.0-or-later