Skip to content

Conversation

@iProzd
Copy link
Collaborator

@iProzd iProzd commented Mar 5, 2025

Summary by CodeRabbit

  • Refactor
    • Revised the logic behind stress evaluation to ensure results are produced reliably when complete input is provided, and error notifications are triggered appropriately when essential data is missing.

@iProzd iProzd requested review from njzjz and wanghan-iapcm March 5, 2025 06:56
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 5, 2025

📝 Walkthrough

Walkthrough

The PR modifies the calculate method in the DP class within deepmd/calculator.py. The control flow is restructured so that the method first checks if the cell variable is provided. If it is, the stress is computed based on the virial and volume; if not and the "stress" property is requested, a PropertyNotImplementedError is raised. No changes were made to public interfaces.

Changes

File Summary
deepmd/calculator.py Reordered control flow in DP.calculate: now checks for a valid cell to compute stress before checking for "stress" in properties to raise an error.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant DP as DP.calculate
    Client->>DP: Call calculate(properties)
    alt cell is provided
        DP->>DP: Compute stress (using virial & volume)
        DP-->>Client: Return stress result
    else
        alt "stress" in properties
            DP-->>Client: Raise PropertyNotImplementedError
        else
            DP-->>Client: No action
        end
    end
Loading

Possibly related PRs

Suggested labels

Python

Suggested reviewers

  • njzjz
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

Copy link
Contributor

@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: 0

🧹 Nitpick comments (1)
deepmd/calculator.py (1)

141-150: Control flow improvement avoids duplicate stress calculation

The restructured logic now first checks for the presence of the cell variable before considering whether to calculate stress. This is a good optimization as it:

  1. Prevents unnecessary condition checking when cell is available
  2. Properly handles the case where stress calculation is requested but not possible
  3. Maintains the same functionality while making the code more efficient

Minor suggestion: Consider adding a brief comment explaining why stress calculation is only possible with a valid cell (i.e., when periodic boundary conditions are applied). This would make the logic clearer for future developers.

 if cell is not None:
     # the usual convention (tensile stress is positive)
     # stress = -virial / volume
+    # stress calculation requires periodic boundary conditions (valid cell)
     stress = -0.5 * (v[0].copy() + v[0].copy().T) / atoms.get_volume()
     # Voigt notation
     self.results["stress"] = stress.flat[[0, 4, 8, 5, 2, 1]]
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 774a844 and 9ec4560.

📒 Files selected for processing (1)
  • deepmd/calculator.py (1 hunks)

Copy link
Member

@njzjz njzjz left a comment

Choose a reason for hiding this comment

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

I don't get the point of this PR... Why is stress calculated when it is not in properties? And what is the relationship between the title and the change?

@github-actions github-actions bot added the Python label Mar 5, 2025
@codecov
Copy link

codecov bot commented Mar 5, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 84.78%. Comparing base (774a844) to head (9ec4560).
Report is 77 commits behind head on devel.

Files with missing lines Patch % Lines
deepmd/calculator.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            devel    #4633   +/-   ##
=======================================
  Coverage   84.78%   84.78%           
=======================================
  Files         688      688           
  Lines       66089    66088    -1     
  Branches     3540     3540           
=======================================
+ Hits        56032    56033    +1     
+ Misses       8916     8914    -2     
  Partials     1141     1141           

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@iProzd
Copy link
Collaborator Author

iProzd commented Mar 6, 2025

I don't get the point of this PR... Why is stress calculated when it is not in properties? And what is the relationship between the title and the change?

@njzjz It's may be some tricky point in ase calling.
When performing

energy = atoms.get_potential_energy()
forces = atoms.get_forces()
stress = atoms.get_stress()

during one-step calculation, the calculator will magically be called twice, even if you explictly add 'stress' keyword in properties: list[str] = ["energy", "forces", "virial", "stress"].

So I guess the ase will first call calculator with properties=None and store all data in self.results, and when finding some missing data, it will call calculator again with properties being missing keys.

This PR fixes this and meets the efficiency test.

@njzjz
Copy link
Member

njzjz commented Mar 6, 2025

The behavior is related to this piece of code:
https://gitlab.com/ase/ase/-/blob/master/ase/calculators/calculator.py#L533-540

get_potential_energy will set properties to ["energy"].
get_forces will not calculated again as force is already in results.

@iProzd
Copy link
Collaborator Author

iProzd commented Mar 6, 2025

The behavior is related to this piece of code: https://gitlab.com/ase/ase/-/blob/master/ase/calculators/calculator.py#L533-540

get_potential_energy will set properties to ["energy"]. get_forces will not calculated again as force is already in results.

So it's better to store all properties in results, anyway.

@njzjz njzjz added this pull request to the merge queue Mar 6, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 6, 2025
@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Mar 7, 2025
Merged via the queue into deepmodeling:devel with commit 5aa7a8f Mar 7, 2025
60 checks passed
iProzd added a commit to iProzd/deepmd-kit that referenced this pull request May 14, 2025
…modeling#4633)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
- Revised the logic behind stress evaluation to ensure results are
produced reliably when complete input is provided, and error
notifications are triggered appropriately when essential data is
missing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
iProzd added a commit to iProzd/deepmd-kit that referenced this pull request May 14, 2025
…modeling#4633)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
- Revised the logic behind stress evaluation to ensure results are
produced reliably when complete input is provided, and error
notifications are triggered appropriately when essential data is
missing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
iProzd added a commit to iProzd/deepmd-kit that referenced this pull request May 14, 2025
…modeling#4633)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
- Revised the logic behind stress evaluation to ensure results are
produced reliably when complete input is provided, and error
notifications are triggered appropriately when essential data is
missing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@njzjz njzjz added this to the v3.0.3 milestone May 21, 2025
njzjz pushed a commit to njzjz/deepmd-kit that referenced this pull request May 21, 2025
…modeling#4633)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
- Revised the logic behind stress evaluation to ensure results are
produced reliably when complete input is provided, and error
notifications are triggered appropriately when essential data is
missing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

(cherry picked from commit 5aa7a8f)
njzjz pushed a commit to njzjz/deepmd-kit that referenced this pull request May 21, 2025
…modeling#4633)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Refactor**
- Revised the logic behind stress evaluation to ensure results are
produced reliably when complete input is provided, and error
notifications are triggered appropriately when essential data is
missing.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

(cherry picked from commit 5aa7a8f)
@coderabbitai coderabbitai bot mentioned this pull request May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants