Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct units of Element.atomic_orbitals #3714

Merged
merged 5 commits into from
Mar 26, 2024

Conversation

esoteric-ephemera
Copy link
Contributor

@esoteric-ephemera esoteric-ephemera commented Mar 26, 2024

The docstr for pymatgen.core.Element.atomic_orbitals indicates that the units of the LDA energies is eV. However the data in periodic_table.json was never converted to eV from Hartree, so element.atomic_orbitals prints energies in Hartree. (This is also true for pymatgen versions before the periodic table data was updated in #3542.)

This was brought up in a matsci post.

This PR just adds an Element.atomic_orbitals_eV property which converts the atomic orbitals energies to eV, and ensures no breaking changes.

Summary by CodeRabbit

  • New Features
    • Added a new property to display atomic orbital energies in electronvolts (eV) for clarity and consistency.
    • Enhanced atomic_orbitals_eV property to include a calculation based on Ha_to_eV conversion.
    • Added new unit conversion constants (Ha_to_eV, Ry_to_eV, bohr_to_angstrom, amu_to_kg) and tests for unit conversions.

Copy link

coderabbitai bot commented Mar 26, 2024

Walkthrough

The recent updates to the pymatgen library introduce a significant enhancement in representing atomic orbital energies. A new atomic_orbitals_eV property now provides these energies in electronvolts, complementing the existing atomic_orbitals in Hartree units. This addition ensures users have clear and consistent options for energy units across the library.

Changes

File Path Change Summary
pymatgen/core/periodic_table.py Added atomic_orbitals_eV property for atomic orbital energies in eV; enhances energy unit options.
tests/core/test_periodic_table.py, tests/core/test_units.py Updated tests to include Ha_to_eV conversion, ensure consistency in element attribute testing, and added new unit conversion constants and tests.

🐇✨
In pymatgen's land, amidst the code and bytes,
A new change gleams, in the computational nights.
Atomic orbitals in eV, alongside Hartree's grace,
For clarity and choice, in the elemental space.
So hop along, dear scientists, with glee,
For in pymatgen's realm, more discoveries to be!
🌌🔬🐰

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • Generate unit-tests 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 tests 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 51b87f8 and 0d36dc6.
Files selected for processing (1)
  • pymatgen/core/periodic_table.py (3 hunks)
Additional comments: 4
pymatgen/core/periodic_table.py (4)
  • 258-268: The addition of the atomic_orbitals_eV property correctly addresses the issue of unit discrepancy in atomic orbital energies by providing these energies in electronvolts (eV). This change is well-documented and directly converts the energies from Hartree to eV using the Ha_to_eV constant, ensuring accuracy and consistency with the expectations set by the documentation.
  • 19-19: It's good practice to ensure that all imported modules and functions are used within the file. The imports are well-organized, and the use of TYPE_CHECKING to conditionally import for type hints is a good practice to avoid unnecessary runtime dependencies.
  • 77-77: The documentation for atomic_orbitals property clearly states that the energies are in Hartree units, which is crucial for avoiding confusion given the addition of atomic_orbitals_eV. Maintaining clear and accurate documentation for each property ensures that users of the library can correctly interpret and use the provided data.
  • 258-268: The implementation of the atomic_orbitals_eV property uses a dictionary comprehension to convert all orbital energies from Hartree to eV. This approach is both efficient and readable. However, it's important to ensure that the Ha_to_eV constant is accurately defined elsewhere in the codebase to guarantee correct conversion values.
Verification successful

The Ha_to_eV constant is defined in pymatgen/core/units.py as 1 / const.physical_constants["electron volt-hartree relationship"][0], indicating that it relies on a physical constants dictionary for its value. This approach suggests a reliance on scientifically reputable sources for conversion factors, which is a good practice for ensuring accuracy. However, without direct access to the const.physical_constants dictionary, the exact numerical accuracy of the conversion cannot be independently verified here. Given the context, it's reasonable to trust the accuracy of this definition based on its source.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the accuracy of the Ha_to_eV constant definition.
rg "Ha_to_eV = " --type py

Length of output: 132

Copy link
Member

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

oh dear 😵‍💫 thanks for the fix @esoteric-ephemera 👍

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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0d36dc6 and aaff776.
Files selected for processing (3)
  • pymatgen/core/periodic_table.py (3 hunks)
  • tests/core/test_periodic_table.py (2 hunks)
  • tests/core/test_units.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pymatgen/core/periodic_table.py
Additional comments: 6
tests/core/test_units.py (4)
  • 12-12: The constant Ha_to_eV is correctly defined and follows the naming convention of other constants in the module. However, ensure that the value assigned to this constant is accurate and consistent with authoritative sources.
  • 17-17: The constant Ry_to_eV is added, which is essential for conversions between Rydberg and electron volts. Verify the accuracy of the conversion factor used here.
  • 22-23: The constants amu_to_kg and bohr_to_angstrom are added. It's crucial to verify these conversion factors for accuracy. Additionally, consider adding a brief comment above each constant to describe its purpose or source for future reference.
  • 30-35: The test test_unit_conversions correctly asserts the values of the newly added constants. It's good practice to ensure these tests cover all edge cases and potential numerical precision issues. Consider adding a brief comment explaining the significance of each assertion for clarity.
tests/core/test_periodic_table.py (2)
  • 14-14: The import of Ha_to_eV is correctly added to support the conversion of atomic orbital energies from Hartree to electron volts. This is a necessary step for the implementation of atomic_orbitals_eV.
  • 246-286: The addition of "atomic_orbitals_eV" to the keys tuple in the test_attributes method is appropriate for testing the new property. This ensures that the property is correctly integrated and functional for all elements where applicable.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0d36dc6 and 92d1a80.
Files selected for processing (3)
  • pymatgen/core/periodic_table.py (3 hunks)
  • tests/core/test_periodic_table.py (2 hunks)
  • tests/core/test_units.py (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • pymatgen/core/periodic_table.py
  • tests/core/test_periodic_table.py
  • tests/core/test_units.py

@janosh janosh merged commit 75c771c into materialsproject:master Mar 26, 2024
22 checks passed
@esoteric-ephemera esoteric-ephemera deleted the atom_orb_en branch March 26, 2024 18:48
@janosh janosh added fix Bug fix PRs core Pymatgen core labels Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Pymatgen core fix Bug fix PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants