Skip to content

Conversation

t-reents
Copy link
Contributor

It seems that the remaining deviations could be fixed. This branch is already based on the changes in #201, I'll do the rebase once it is merged.

fig1

fig2

fig3

CompRhys and others added 9 commits May 22, 2025 16:44
---------

Co-authored-by: Janosh Riebesell <[email protected]>
* Initialize velocities to None in the pos-only case, see previous
  changes to the optimizers. (ensures that the correct `dt` is used)

* Change the order of the increase of `n_pos`. Again, this ensures the
  usage of the correct `dt` compared to ASE
* Remove rescaling of positions when updating cell, it's not relevant
* Correctly rescale the positions with respect to the deformation
  gradient
* Consider the `cell_forces` in the convergence when doing cell
  optimizations
Copy link

cla-bot bot commented May 28, 2025

We require contributors to sign our Contributor License Agreement (CLA), and we don't have record of your signature. In order for us to review and merge your code, please sign the CLA.

dr_atom = torch.where(
dr_scaling_atom > max_step, max_step * dr_atom / (dr_scaling_atom + eps), dr_atom
)
state.positions = state.positions + dr_atom

if is_cell_optimization:
Copy link
Collaborator

Choose a reason for hiding this comment

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

this seems strange to me? does this logic not add the cartesian dr to the fractional space positions?

Copy link
Collaborator

Choose a reason for hiding this comment

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

        positions = (
            torch.linalg.solve(
                cur_deform_grad[state.batch], (state.positions+dr_atom).unsqueeze(-1)
            ).squeeze(-1)
        )
        state.positions = torch.bmm(
            positions.unsqueeze(1), F_new[state.batch].transpose(-2, -1)
        ).squeeze(1)

why would this not be the right thing to do in terms of trying to match your operations here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this seems strange to me? does this logic not add the cartesian dr to the fractional space positions?

No, it doesn't scale between fractional/cartesian coordinates. Based on the comments in ASE, this step is done to consider the positions in the original cell. The dr_atom is applied to those coordinates and the new cell deformation is applied afterwards. This is also briefly mentioned in the documentation of the UnitCellFilter (https://wiki.fysik.dtu.dk/ase/ase/filters.html#ase.filters.UnitCellFilter):

Degrees of freedom are the positions in the original undeformed cell, plus the 
deformation tensor (extra 3 “atoms”). This gives forces consistent with numerical 
derivatives of the potential energy with respect to the cell degreees of freedom.

why would this not be the right thing to do in terms of trying to match your operations here?

Again, to my understanding, you want to consider the positions in the undeformed cell and apply the dr_atom to those. That's why I apply the linalg.solve first and then add the dr_atom, and not the other way around.

@cla-bot cla-bot bot added the cla-signed Contributor license agreement signed label May 28, 2025
@t-reents
Copy link
Contributor Author

t-reents commented May 31, 2025

I would have to look into the failing test in more detail. As a first step, I took the rattled_sio2 and manually inspected the whole relaxation trajectory (I did the same for the other debugging as well, i.e., a bunch of print statements comparing all the tensors etc. during relaxation). The final optimized structure as well as the intermediate states seem to agree very well.

Still, the test is failing, so I'd have to understand what exactly is happening there. Nonetheless, based on my previous comment, those deviations might be related to the logic of the test and not necessarily to actual differences in the optimizations. I'll look into it and give an update here.

Update: I have to stop for now, but there is indeed a problem with the logic of the tests/the loop logic in torch_sim.runners.optimize. I'll push a fix tomorrow or Monday at the lastest. The failing of the tests for some of the examples is related to a different number of iterations that are performed, while for others it's the reinitialization (the init function is always called again, e.g. resetting the velocities to 0, while ASE keeps the previous state). When adjusting those aspects, I the test does pass locally.

t-reents added 2 commits June 2, 2025 12:24
* Include the `cell_forces` in the convergence check
* Fix the number of iterations that are performed. `steps_between_swaps` is set to 1, so the number of iterations is equal to the number of swaps. In the previous version, less iterations would have been performed when reaching the maximum number of swaps. For example, when trying to run 32 steps with `steps_between_swaps=5`, the optimization would have stopped after 30 iterations, i.e., 6 swaps.
* Fix `autobatching.py`. The if statement would have been triggered for `max_attempts=0`, which was the case when running one iteration and `steps_between_swaps=5`
* Fix the `None` initialization
* Fix the cell update when using `UnitCellFilter`
@t-reents
Copy link
Contributor Author

t-reents commented Jun 2, 2025

The test is now passing, at least locally. As mentioned earlier, one of the issues was the reinitializations between the different steps that are tested. To fix it, I added an if statement for now that checks if the state is already of the correct type.

Moreover, as described in the commit message, the current logic of the autobatching caused a different number of iterations, since the structures are set as converged when the maximum number of swap attempts is reached and not necessarily the maximum number of iterations (which led to different number of steps compared to ASE).

Copy link
Contributor

@janosh janosh left a comment

Choose a reason for hiding this comment

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

thanks a lot for your continued help in fixing the torch-sim FIRE relaxation! 👍

@janosh janosh merged commit 2e4a408 into TorchSim:main Jun 3, 2025
1 check passed
janosh added a commit that referenced this pull request Jun 3, 2025
…m` (#203)

* fea: use batched vdot

---------

Co-authored-by: Janosh Riebesell <[email protected]>

* clean: remove ai slop

* clean: further attempts to clean but still not matching PR

* fix: dr is vdt rather than fdt

* typing: fix typing issue

* wip: still not sure where the difference is now

* update forces per comment

* Fix ASE pos only implementation

* Initialize velocities to None in the pos-only case, see previous
  changes to the optimizers. (ensures that the correct `dt` is used)

* Change the order of the increase of `n_pos`. Again, this ensures the
  usage of the correct `dt` compared to ASE

* Fix torch-sim ASE-FIRE (Frechet Cell)

* Remove rescaling of positions when updating cell, it's not relevant
* Correctly rescale the positions with respect to the deformation
  gradient
* Consider the `cell_forces` in the convergence when doing cell
  optimizations

* linting

* test: still differ significantly after step 1 for distorted structures

* Fix test comparing ASE and torch-sim optimization

* Include the `cell_forces` in the convergence check
* Fix the number of iterations that are performed. `steps_between_swaps` is set to 1, so the number of iterations is equal to the number of swaps. In the previous version, less iterations would have been performed when reaching the maximum number of swaps. For example, when trying to run 32 steps with `steps_between_swaps=5`, the optimization would have stopped after 30 iterations, i.e., 6 swaps.
* Fix `autobatching.py`. The if statement would have been triggered for `max_attempts=0`, which was the case when running one iteration and `steps_between_swaps=5`

* Fix `optimizers` when using `UnitCellFilter`

* Fix the `None` initialization
* Fix the cell update when using `UnitCellFilter`

* fix test_optimize_fire

* allow FireState.velocities = None since it's being set to None in multiple places

* safer `batched_vdot`: check dimensionality of input tensors `y` and `batch_indices`

- fix stale docstring mentioning is_sum_sq kwarg

* generate_force_convergence_fn raise informative error on needed but missing cell_forces

* pascal case VALID_FIRE_CELL_STATES->AnyFireCellState and fix non-f-string error messages

* fix FireState TypeError: non-default argument 'dt' follows default argument

* allow None but don't set default for state.velocities

* fix bad merge conflict resolution

* tweaks

---------

Co-authored-by: Rhys Goodall <[email protected]>
Co-authored-by: Janosh Riebesell <[email protected]>
@CompRhys
Copy link
Collaborator

CompRhys commented Jun 3, 2025

@t-reents thank you for your careful work here!

janosh added a commit that referenced this pull request Jun 9, 2025
* clean: move pair fn logic into batched model code

* clean: begin to remove unbatched scripts

* clean: update the dynamics scripts, remove old code, refactor the integrators into folder

* fix: all unit tests run

* fix: remove circular import

* wip: address more issues with removing unbatched code

* wip: another attempt to fix examples

* fix: mace needs the shifts? we should add a unit test that needs them as the MaceModel unit tests didn't fail removing them.

* metatensor models have been renamed to metatomic models (#205)

* Fix discrepancies between `FIRE` optimisations in `ASE` and `torch-sim`  (#203)

* fea: use batched vdot

---------

Co-authored-by: Janosh Riebesell <[email protected]>

* clean: remove ai slop

* clean: further attempts to clean but still not matching PR

* fix: dr is vdt rather than fdt

* typing: fix typing issue

* wip: still not sure where the difference is now

* update forces per comment

* Fix ASE pos only implementation

* Initialize velocities to None in the pos-only case, see previous
  changes to the optimizers. (ensures that the correct `dt` is used)

* Change the order of the increase of `n_pos`. Again, this ensures the
  usage of the correct `dt` compared to ASE

* Fix torch-sim ASE-FIRE (Frechet Cell)

* Remove rescaling of positions when updating cell, it's not relevant
* Correctly rescale the positions with respect to the deformation
  gradient
* Consider the `cell_forces` in the convergence when doing cell
  optimizations

* linting

* test: still differ significantly after step 1 for distorted structures

* Fix test comparing ASE and torch-sim optimization

* Include the `cell_forces` in the convergence check
* Fix the number of iterations that are performed. `steps_between_swaps` is set to 1, so the number of iterations is equal to the number of swaps. In the previous version, less iterations would have been performed when reaching the maximum number of swaps. For example, when trying to run 32 steps with `steps_between_swaps=5`, the optimization would have stopped after 30 iterations, i.e., 6 swaps.
* Fix `autobatching.py`. The if statement would have been triggered for `max_attempts=0`, which was the case when running one iteration and `steps_between_swaps=5`

* Fix `optimizers` when using `UnitCellFilter`

* Fix the `None` initialization
* Fix the cell update when using `UnitCellFilter`

* fix test_optimize_fire

* allow FireState.velocities = None since it's being set to None in multiple places

* safer `batched_vdot`: check dimensionality of input tensors `y` and `batch_indices`

- fix stale docstring mentioning is_sum_sq kwarg

* generate_force_convergence_fn raise informative error on needed but missing cell_forces

* pascal case VALID_FIRE_CELL_STATES->AnyFireCellState and fix non-f-string error messages

* fix FireState TypeError: non-default argument 'dt' follows default argument

* allow None but don't set default for state.velocities

* fix bad merge conflict resolution

* tweaks

---------

Co-authored-by: Rhys Goodall <[email protected]>
Co-authored-by: Janosh Riebesell <[email protected]>

* bump pre-commit hooks and fix new errors

* fix MACE examples not using ts.io state conversion utilities

* fix unused shifts_list not torch.cat()-ed in mace.py

* fix examples/scripts/3_Dynamics/3.9_MACE_NVT_staggered_stress.py using nvt_langevin in place of nvt_nose_hoover_invariant

remove debug print in torch_sim/runners.py

* just calc total energy manually in nvt_langevin_invariant in torch_sim/integrators/nvt.py

also fix broadcasting bug in Ornstein-Uhlenbeck (ou_step)

* fix calculate_momenta() in npt_nose_hoover_init

also fix 3.8_MACE_NPT_Nose_Hoover.py broken imports from torch_sim.integrators.npt

* fix unpack of n_particles, dim = state.positions.shape

* fix missing external_pressure in npt_nose_hoover call in 3.8_MACE_NPT_Nose_Hoover.py

* update cell_position in npt_nose_hoover before downstream usage

* refactor (no bug fixes)

* fix npt_nose_hoover transposing cell that's already in column-vector convention

* feat: Add batching support to NPT Nose-Hoover integrator

- Update NPTNoseHooverState cell properties to support batch dimensions

  - reference_cell: [n_batches, 3, 3] instead of [3, 3]

  - cell_position, cell_momentum, cell_mass: [n_batches] instead of scalar

- Fix tensor broadcasting in exp_iL1, exp_iL2 with proper atom-to-batch mapping

- Update compute_cell_force for per-batch kinetic energy and stress calculations

- Fix npt_nose_hoover_init to properly initialize batched cell variables

- Update npt_nose_hoover_invariant for per-batch energy conservation

- Replace pbc_wrap_general with pbc_wrap_batched for proper PBC handling

- Fix example script 3.8_MACE_NPT_Nose_Hoover.py output formatting

Enables multiple independent NPT systems in a single simulation while maintaining backward compatibility for single-batch systems.

* fix: Remove cell dimension squeezing in NVT Nose-Hoover integrator

- Remove problematic cell.squeeze(0) that breaks batching support

- Fix calculate_momenta function call to use correct signature with batch parameter

- Resolves RuntimeError when using MACE with NVT Nose-Hoover thermostat

Fixes example script 3.5_MACE_NVT_Nose_Hoover.py which was failing due to

neighbor list function receiving wrong tensor shapes when cell batch

dimension was incorrectly removed.

* fix: Handle scalar kT and batched stress tensors in NPT Nose-Hoover

- Convert scalar kT to tensor before accessing .ndim attribute in npt_nose_hoover_init and update_cell_mass

- Fix stress tensor trace computation in compute_cell_force to handle 3D batched tensors

- Use torch.diagonal().sum() for batched stress tensors instead of torch.trace()

Fixes Lennard-Jones NPT Nose-Hoover script that was failing with:

- AttributeError: 'float' object has no attribute 'ndim'

- RuntimeError: trace: expected a matrix, but got tensor with dim 3

* fix: Handle batched cell tensors in get_fractional_coordinates

- Replace deprecated .T with .mT for matrix transpose on 3D tensors
- Add support for batched cell tensors with shape [n_batches, 3, 3]
- Extract first batch cell matrix when cell.ndim == 3
- Maintains backward compatibility with 2D cell matrices

Fixes batched silicon workflow script that was failing with:
- UserWarning about deprecated .T usage on >2D tensors
- RuntimeError: linalg.solve: A must be batches of square matrices

The get_fractional_coordinates function now properly handles both
single [3,3] and batched [n_batches,3,3] cell tensors, enabling a2c_silicon_batched.py workflow to run

* replace .transpose(-2, -1) with .mT everywhere

* fix nvt + npt integrators: ensure consistent batch dimensions in kinetic energy calculations

- Add missing `batch=state.batch` parameter to `calc_kinetic_energy` calls in NPT integrator
- Enhance NVT/NPT invariant functions with explicit broadcasting for chain variables
- Replace inefficient manual loops with batch-aware kinetic energy calculation in NPT invariant
- Fix undefined variable reference in NPT invariant function (n_batches → state.n_batches)

Resolves broadcasting issues where scalar kinetic energy was incorrectly combined with
batched energy and temperature tensors, ensuring all energy terms have consistent
batch dimensions before addition.

* fix transforms.py: prevent silent data corruption in get_fractional_coordinates

Replace problematic batched cell handling that only processed the first batch
(cell[0]) with explicit NotImplementedError. This prevents silent data
corruption where multi-batch systems would incorrectly use only the first
batch's cell parameters for all coordinate transformations.

- Raise NotImplementedError for 3D batched cell tensors instead of silently
  ignoring batches 1, 2, ... N
- Preserve existing functionality for 2D cell matrices
- Add clear error message indicating limitation and suggesting workarounds

Breaking change: Code that previously silently failed will now explicitly
error, but this prevents incorrect results in multi-batch scenarios.

* fix nvt_langevin: handle None gamma parameter correctly

* fix npt: missing PBC check in Nose-Hoover position update

- Check state.pbc before applying ts.transforms.pbc_wrap_batched
- Return unwrapped positions when state.pbc is False
- Ensure consistent behavior with NPT Langevin implementation

* Initialize cur_deform_grad to prevent UnboundLocalError

* fix nvt: compute degrees of freedom per batch in Nose-Hoover init

Replace count_dof() with proper batch-aware DOF calculation using
torch.bincount(state.batch) to ensure consistency with invariant function.
Previously ignored batch structure, causing incorrect DOF for batched systems.

* fix /5.1_a2c_silicon_batched.py: restore single-batch cell tensors support in transforms.py's get_fractional_coordinates

- Handle batched cell tensors with shape [1, 3, 3] by auto-squeezing to [3, 3]
- Improve error messages for multi-batch cases to be more informative
- Add comprehensive tests for batched cell tensor scenarios
- Fixes NotImplementedError in examples/scripts/5_Workflow/5.1_a2c_silicon_batched.py:159
- Maintains full backward compatibility with existing 2D cell matrix usage

---------

Signed-off-by: Janosh Riebesell <[email protected]>
Co-authored-by: Janosh Riebesell <[email protected]>
@janosh janosh added tests Test all the things fix Bug fix labels Jun 10, 2025
Copy link

coderabbitai bot commented Jun 10, 2025

Warning

Rate limit exceeded

@janosh has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 9 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 2c3a862 and 8deb1c8.

📒 Files selected for processing (8)
  • .github/workflows/link-check.yml (0 hunks)
  • examples/scripts/7_Others/7.6_Compare_ASE_to_VV_FIRE.py (2 hunks)
  • tests/test_optimizers_vs_ase.py (5 hunks)
  • tests/test_runners.py (2 hunks)
  • torch_sim/autobatching.py (1 hunks)
  • torch_sim/math.py (1 hunks)
  • torch_sim/optimizers.py (23 hunks)
  • torch_sim/runners.py (5 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed Contributor license agreement signed fix Bug fix tests Test all the things
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants