Skip to content

Feat/energy minimizer - #64

Merged
sayeg84 merged 4 commits into
mainfrom
feat/energy_minimizer
Aug 18, 2026
Merged

Feat/energy minimizer#64
sayeg84 merged 4 commits into
mainfrom
feat/energy_minimizer

Conversation

@EdoardoRolando

Copy link
Copy Markdown
Contributor

PR Checklist

  • [] Bug fix
  • Feature addition/change
  • [] Documentation addition/change
  • [] Test addition/change
  • [] Black formatting

Adds utilities to relax coarse-grained configurations to a local energy minimum of a trained mlcg model.

What's added (src/mlcg/simulation/minimizer.py):

minimize_energy: batched minimization. All configurations are collated into a single batch and relaxed together with one model forward call per step, using a torch.optim optimizer (LBFGS by default). Model forces are used directly as the negative energy gradient, so no second backward pass is needed.

minimize_energy_ase: relaxes one structure at a time through an ASE optimizer (FIRE by default)

MLCGCalculator: an ASE Calculator wrapper that evaluates energies/forces from an mlcg model, needed for minimize_energy_ase.

Both support per-configuration fixed atoms (frozen during relaxation) and share the same input validation and convergence criterion (max per-atom force magnitude vs fmax).

Tests (tests/unit/simulation/test_minimizer.py): each minimizer is checked for energy decrease, convergence toward equilibrium, and fixed-atom immobility, plus a test that the two methods converge to the same minimum.

Notes: On a real system the batched path was ~15–18× faster than the sequential ASE path while producing matching geometries.

Add minimize_energy (batched torch minimization) and minimize_energy_ase
(ASE-driven), an MLCGCalculator ASE wrapper, and unit tests covering both
minimizers plus their agreement.
@sayeg84

sayeg84 commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

This looks great, thanks, although I have a couple of questions:

  1. Is this MLCG calculator the same one that @zakmachachi wrote?
  2. Why not adding also a script with a yaml-like interface so that we can run a minimization from command line like simulation?

@zakmachachi

Copy link
Copy Markdown
Contributor

https://github.com/ClementiGroup/mlcg/blob/feat/ase_calc/src/mlcg/nn/ase_calculator.py

Here is a batched calculator FYI. this branch is sort of semi finished. if it is useful, we can talk @EdoardoRolando and combine the two branches?

Remove the ASE-based MLCGCalculator and minimize_energy_ase from
minimizer.py, keeping only the batched, no-ASE minimize_energy. The
module no longer imports ASE at all.

While auditing minimize_energy itself:
- fix a GPU->CPU sync on every structure on every step, from using a
  CUDA tensor (batch.ptr) directly as a Python slice bound
- fix dtype= being silently ignored, so float64 configurations were
  fed into a float32 model
- warn instead of silently returning unconverged geometry when
  runs out
- guard the LBFGS-subclass check against non-class optimizer_cls
  values (e.g. functools.partial), which issubclass() would reject

Expand tests/unit/simulation/test_minimizer.py from 4 to 14 cases:
drop the two ASE-only tests and the cross-implementation comparison
(nothing left to compare against), add fmax-reached, fixed-atoms-
excluded-from-convergence, no-mutation, LBFGS max_iter guard,
SGD/Adam support, input validation, converged-structures-stop-being-
stepped, mixed-difficulty batches, and the new convergence warning
(both that it fires and that it doesn't fire spuriously).
Add mlcg_minimize_energy.py plus parse_minimizer_config() in
simulation/cli.py, following the same jsonargparse/--config convention
as mlcg_nvt_langevin.py: minimize_energy's own keyword arguments are
exposed under a `minimizer:` YAML block via add_function_arguments,
alongside model_file/structure_file/output_file. Registers the
mlcg-minimize_energy console script and adds an example YAML.

Also rename minimize_energy's fixed_atoms parameter to free_atoms,
inverting its semantics (list the atoms allowed to move rather than
the ones held in place) and keeping None as the "everything moves"
default -- shorter and more natural for the common case of relaxing
only a handful of atoms. Update the constrained-optimization tests
accordingly.
@EdoardoRolando

Copy link
Copy Markdown
Contributor Author

Thanks Aldo and Zak! I removed the not-bached ase energy minimizer and corresponding mlcg calculator to avoid duplicates, i needed them just as a double check for the pytorch based batched energy minimizer.

Thanks @sayeg84 for the suggestion, I have also added a script to interface the energy minimizer from CLI, aside from being usable as a function.

@sayeg84 sayeg84 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, it seems to be good and working.

@sayeg84
sayeg84 merged commit 72dfcae into main Aug 18, 2026
4 checks passed
@sayeg84
sayeg84 deleted the feat/energy_minimizer branch August 18, 2026 08: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