.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/basic/06_geometry_optimization_lbfgs.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_basic_06_geometry_optimization_lbfgs.py: L-BFGS Geometry Optimization with Lennard-Jones Argon ====================================================== :class:`~nvalchemi.dynamics.optimizers.LBFGS` is a drop-in alternative to :class:`~nvalchemi.dynamics.optimizers.FIRE2`: same constructor shape, same hooks, same convergence criterion, one force evaluation per step. It builds a quasi-Newton direction from the last ``history_size`` position/force differences, so it usually needs far fewer steps. L-BFGS also launches more Warp kernels per step than FIRE2, so it has higher per-step overhead. That's invisible behind a real MLIP forward pass, but with a model this cheap (Lennard-Jones), the per-step overhead can outweigh the step-count win, so L-BFGS can be slower in wall-clock time here even though it converges in far fewer steps. This example relaxes a batch of argon clusters with L-BFGS. .. GENERATED FROM PYTHON SOURCE LINES 33-44 .. code-block:: Python from __future__ import annotations import torch from nvalchemi.data import AtomicData, Batch from nvalchemi.dynamics import LBFGS, ConvergenceHook, DynamicsStage from nvalchemi.models.lj import LennardJonesModelWrapper device = "cuda" if torch.cuda.is_available() else "cpu" .. GENERATED FROM PYTHON SOURCE LINES 45-49 Model and systems ----------------- Lennard-Jones argon, as in the FIRE2 example. Three clusters of different sizes are batched together; ragged batches need no special handling. .. GENERATED FROM PYTHON SOURCE LINES 49-78 .. code-block:: Python model = LennardJonesModelWrapper(epsilon=0.0104, sigma=3.40, cutoff=8.5).to(device) R_MIN = 2 ** (1 / 6) * 3.40 # LJ equilibrium pair distance, ≈ 3.82 Å def make_cluster(n_per_side: int, seed: int) -> AtomicData: """Perturbed simple-cubic argon cluster with ``n_per_side**3`` atoms.""" coords = torch.arange(n_per_side, dtype=torch.float32) * R_MIN * 1.1 grid = torch.stack(torch.meshgrid(coords, coords, coords, indexing="ij"), -1) positions = grid.reshape(-1, 3) positions += 0.1 * torch.randn( positions.shape, generator=torch.Generator().manual_seed(seed) ) n = len(positions) return AtomicData( positions=positions, atomic_numbers=torch.full((n,), 18, dtype=torch.long), forces=torch.zeros(n, 3), energy=torch.zeros(1, 1), ) def make_batch() -> Batch: """Batch of 8-, 27- and 64-atom clusters.""" return Batch.from_data_list( [make_cluster(2, 0), make_cluster(3, 1), make_cluster(4, 2)] ).to(device) .. GENERATED FROM PYTHON SOURCE LINES 79-83 Relax with L-BFGS ----------------- ``convergence_hook`` stops the run once every system's fmax is below the threshold. .. GENERATED FROM PYTHON SOURCE LINES 83-111 .. code-block:: Python def relax(optimizer) -> Batch: """Attach neighbor-list hooks and run *optimizer* on a fresh batch.""" for hook in model.make_neighbor_hooks(): optimizer.register_hook(hook, stage=DynamicsStage.BEFORE_COMPUTE) with optimizer: return optimizer.run(make_batch()) def fmax(batch: Batch) -> float: """Largest per-atom force norm in *batch*.""" return batch.forces.norm(dim=-1).max().item() converged = ConvergenceHook.from_fmax(1e-3) lbfgs = LBFGS( model=model, history_size=6, # stored curvature pairs maxstep=0.2, # largest displacement per step (Å) n_steps=2000, convergence_hook=converged, ) lbfgs_batch = relax(lbfgs) print(f"L-BFGS: {lbfgs.step_count:4d} steps, fmax {fmax(lbfgs_batch):.1e} eV/Å") .. rst-class:: sphx-glr-script-out .. code-block:: none L-BFGS: 92 steps, fmax 6.6e-04 eV/Å .. GENERATED FROM PYTHON SOURCE LINES 112-115 Final energies -------------- Each system is relaxed to a local minimum. .. GENERATED FROM PYTHON SOURCE LINES 115-118 .. code-block:: Python for i, e_lbfgs in enumerate(lbfgs_batch.energy.squeeze(-1).tolist()): print(f"sys{i}: E(L-BFGS) = {e_lbfgs:+.5f} eV") .. rst-class:: sphx-glr-script-out .. code-block:: none sys0: E(L-BFGS) = -0.19735 eV sys1: E(L-BFGS) = -1.09276 eV sys2: E(L-BFGS) = -3.19346 eV .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.390 seconds) .. _sphx_glr_download_examples_basic_06_geometry_optimization_lbfgs.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 06_geometry_optimization_lbfgs.ipynb <06_geometry_optimization_lbfgs.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 06_geometry_optimization_lbfgs.py <06_geometry_optimization_lbfgs.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 06_geometry_optimization_lbfgs.zip <06_geometry_optimization_lbfgs.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_