Skip to content

Conversation

@jan-janssen
Copy link
Member

@jan-janssen jan-janssen commented Aug 6, 2025

Summary by CodeRabbit

  • New Features
    • Added support for setting atomic velocities when using the interactive structure setter with LAMMPS, enabling more accurate initialization of simulations when velocity data is available.
  • Tests
    • Introduced a new test to verify correct handling and retrieval of atomic velocities alongside positions and energies in the LAMMPS-ASE interface.
@coderabbitai
Copy link

coderabbitai bot commented Aug 6, 2025

Walkthrough

The change updates the interactive_structure_setter method in pylammpsmpi/wrapper/ase.py to support setting atomic velocities when creating atoms in the LAMMPS interactive library. A helper function _vector_to_lammps was introduced to convert position and velocity vectors properly considering the simulation box geometry. Additionally, a new test test_velocities was added in tests/test_ase_interface.py to verify correct velocity handling in the LAMMPS-ASE interface.

Changes

Cohort / File(s) Change Summary
ASE Wrapper Velocity Handling
pylammpsmpi/wrapper/ase.py
Added helper _vector_to_lammps for vector conversion; refactored interactive_positions_setter and interactive_structure_setter to use it for positions and velocities. Velocities are now properly transformed and passed to atom creation.
Velocity Handling Test
tests/test_ase_interface.py
Added test_velocities method to verify setting and retrieving atomic velocities alongside positions and energies using the LAMMPS-ASE interface.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Poem

In the world of atoms, swift and spry,
Velocities now join as they zip by!
ASE and LAMMPS, in harmony they meet,
With every structure, now more complete.
A bunny hops, with joy anew—
"Atoms in motion? Yes, thank you!" 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 302ccd1 and 15dcfc1.

📒 Files selected for processing (1)
  • pylammpsmpi/wrapper/ase.py (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pylammpsmpi/wrapper/ase.py
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ase_velocity

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
  • 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.

Support

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

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 generate unit tests to generate unit tests for 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.
@jan-janssen jan-janssen requested a review from samwaseda August 6, 2025 11:45
@codecov
Copy link

codecov bot commented Aug 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.22%. Comparing base (15ec161) to head (15dcfc1).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@ ## main #356 +/- ## ========================================== + Coverage 81.12% 81.22% +0.10%  ========================================== Files 5 5 Lines 551 554 +3 ========================================== + Hits 447 450 +3  Misses 104 104 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3a96fa0 and 950d956.

📒 Files selected for processing (1)
  • pylammpsmpi/wrapper/ase.py (3 hunks)
🔇 Additional comments (2)
pylammpsmpi/wrapper/ase.py (2)

312-312: LGTM!

Correctly passes the computed velocities variable instead of hardcoded None.


322-322: LGTM!

Correctly passes the computed velocities variable for the multi-core case, maintaining consistency with the single-core implementation.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/test_ase_interface.py (1)

161-163: Consider using np.allclose for velocity comparison.

The test uses np.equal for velocity comparison, which requires exact equality. Given that velocities might undergo coordinate transformations in non-orthogonal cells, np.allclose would be more robust against floating-point precision issues.

- self.assertTrue( - np.all(np.equal(lmp.interactive_velocities_getter(), velocities_initial)) - ) + self.assertTrue( + np.all(np.allclose(lmp.interactive_velocities_getter(), velocities_initial)) + )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 950d956 and 302ccd1.

📒 Files selected for processing (1)
  • tests/test_ase_interface.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/test_ase_interface.py (1)
pylammpsmpi/wrapper/ase.py (7)
  • LammpsASELibrary (16-469)
  • interactive_structure_setter (192-329)
  • interactive_lib_command (66-75)
  • interactive_positions_getter (77-90)
  • interactive_energy_pot_getter (340-347)
  • interactive_velocities_getter (441-448)
  • interactive_positions_setter (92-108)
🔇 Additional comments (3)
tests/test_ase_interface.py (3)

124-138: LGTM! Test setup follows established patterns.

The test initialization correctly follows the same pattern as other tests in the class, using appropriate logger configuration and LammpsLibrary backend.


164-174: LGTM! Position modification test is well-structured.

The test correctly modifies positions, updates LAMMPS, and validates both energy and position changes. This ensures the velocity functionality doesn't interfere with position handling.


135-138: Verify velocity array length matches bulk Al structure atom count

We’re hardcoding four 3-component velocity vectors but couldn’t import ASE here to confirm that bulk('Al', cubic=True) indeed yields exactly 4 atoms. Please manually verify that the structure has 4 atoms (i.e. positions shape (4, 3)), and if it differs, update velocities_initial accordingly.

• File: tests/test_ase_interface.py (lines 135–138)
• Snippet:

 velocities_initial = np.array( [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]] ) structure.set_velocities(velocities_initial)
Comment on lines +124 to +174
def test_velocities(self):
lmp = LammpsASELibrary(
working_directory=None,
cores=1,
comm=None,
logger=logging.getLogger("TestStaticLogger"),
log_file=None,
library=LammpsLibrary(cores=2),
disable_log_file=True,
)
structure = bulk("Al", cubic=True)
velocities_initial = np.array(
[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]
)
structure.set_velocities(velocities_initial)
lmp.interactive_structure_setter(
structure=structure,
units="lj",
dimension=3,
boundary=" ".join(["p" if coord else "f" for coord in structure.pbc]),
atom_style="atomic",
el_eam_lst=["Al"],
calc_md=False,
)
lmp.interactive_lib_command("pair_style lj/cut 6.0")
lmp.interactive_lib_command("pair_coeff 1 1 1.0 1.0 4.04")
lmp.interactive_lib_command(
command="thermo_style custom step temp pe etotal pxx pxy pxz pyy pyz pzz vol"
)
lmp.interactive_lib_command(command="thermo_modify format float %20.15g")
lmp.interactive_lib_command("run 0")
self.assertTrue(
np.all(np.isclose(lmp.interactive_positions_getter(), structure.positions))
)
self.assertTrue(
np.isclose(lmp.interactive_energy_pot_getter(), -0.04342932384411341)
)
self.assertTrue(
np.all(np.equal(lmp.interactive_velocities_getter(), velocities_initial))
)
positions = structure.positions.copy()
positions[0] += np.array([0.1, 0.1, 0.1])
lmp.interactive_positions_setter(positions=positions)
lmp.interactive_lib_command("run 0")
self.assertTrue(
np.isclose(lmp.interactive_energy_pot_getter(), -0.043829529274767506)
)
self.assertTrue(
np.all(np.isclose(lmp.interactive_positions_getter(), positions))
)

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Test coverage gaps: Missing velocity persistence and edge cases.

The test validates initial velocity setting but doesn't verify velocity persistence after position updates or test edge cases like zero velocities or missing velocity data.

Consider adding these test scenarios:

# After line 173, add velocity persistence check: self.assertTrue( np.all(np.allclose(lmp.interactive_velocities_getter(), velocities_initial)), "Velocities should persist after position updates" ) # Also consider testing with zero velocities or no initial velocities
🤖 Prompt for AI Agents
In tests/test_ase_interface.py around lines 124 to 174, the test_velocities method sets initial velocities but does not verify if velocities persist after updating positions or handle edge cases like zero or missing velocities. To fix this, add an assertion after updating positions to check that velocities remain unchanged using np.allclose on lmp.interactive_velocities_getter() and velocities_initial. Additionally, create new test cases to set zero velocities and test behavior when velocities are not set initially, ensuring these edge cases are covered. 
Copy link
Member

@samwaseda samwaseda left a comment

Choose a reason for hiding this comment

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

Except for the one comment that I made I'm fine with this one 👍

* Implement _vector_to_lammps * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * run black * Remove flatten because it's always flattened --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@jan-janssen jan-janssen merged commit 22587f1 into main Aug 6, 2025
19 checks passed
@jan-janssen jan-janssen deleted the ase_velocity branch August 6, 2025 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants