Skip to content

Closes: #16 Add the ability to control the simulation from JS#24

Merged
andeplane merged 11 commits intomasterfrom
feature/js-syncing
Feb 19, 2026
Merged

Closes: #16 Add the ability to control the simulation from JS#24
andeplane merged 11 commits intomasterfrom
feature/js-syncing

Conversation

@alexjilkin
Copy link
Collaborator

@alexjilkin alexjilkin commented Dec 27, 2025

Related Issue

  • Similarly to Atomify, I added fix js/async to hook LAMMPS end-of-step and minimization (MIN_POST_FORCE) and call back into JS via Asyncify, blocking the simulation until the Promise resolves. Still no LAMMPS
    core patching, but now we also expose selected compute scalar values in callbacks (in addition to particles/bonds/box snapshots).

  • runScriptWithAsyncCallback now supports both run and minimize flows, injects fix js/async automatically when needed, and keeps managed fix state so frequency updates can be reused safely across runs. It also handles sync throw cleanup correctly (callback is always cleared).

  • Callback payload still returns simulation snapshots by default (particles/bonds/box), and now optionally includes computeScalars for requested compute IDs.

ThreeJS example

  • Three.js demo now renders live compute metrics (temp + pe) from async callback data.
  • Input script defines computes used by the UI.

Tests / stability

  • Added coverage for minimize injection path, managed fix reuse, sync throw cleanup, and async callback compute scalar payloads.
  • Added regression coverage for unfix before minimize/run handling.
  • Full test suite passes with these changes.

Dist packaging

  • Build emits dist/cpp/lammps.js, dist/client.js, and .d.ts into dist/ directly (no extra copy step).

@alexjilkin alexjilkin marked this pull request as ready for review February 13, 2026 16:26
@alexjilkin alexjilkin requested a review from andeplane February 15, 2026 09:34
@alexjilkin alexjilkin changed the title Add the ability to control the simulation from JS Closes: #16 Add the ability to control the simulation from JS Feb 16, 2026
@alexjilkin alexjilkin linked an issue Feb 16, 2026 that may be closed by this pull request
@alexjilkin alexjilkin self-assigned this Feb 16, 2026
}

const bool ok = LAMMPSWebAsync::invokeStepCallbackAndWait(
static_cast<std::int32_t>(update->ntimestep)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should cast this as 64 bit int?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or maybe we don't have 64 bit ints in the compiled version, hmm...unlikely to be a problem regardless.

}

void FixJsAsync::min_post_force(int) {
++step_count;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be reset after a successful minimization?

namespace LAMMPSWebAsync {
namespace {
emscripten::val g_step_callback = emscripten::val::undefined();
emscripten::val g_promise_waiter = emscripten::val::undefined();
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are global so it means we can only have one LAMMPS instance at the same time (afaik)? Anything we could do here instead?

(err) => {
module.HEAP32[errPtr >> 2] = 1;
module.HEAP32[donePtr >> 2] = 1;
module.__lammpsAsyncError = err;
Copy link
Collaborator

Choose a reason for hiding this comment

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

How is this used?

}
nevery = utils::inumeric(FLERR, arg[3], false, lmp);
if (nevery <= 0) {
error->all(FLERR, 3, "Illegal fix js/async nevery value {}; must be > 0", nevery);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the 3?

@andeplane andeplane merged commit fdaad9b into master Feb 19, 2026
1 check passed
@andeplane andeplane deleted the feature/js-syncing branch February 19, 2026 12:59
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.

Enable callback to JS during a run and minimize command

2 participants