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

feat: support spin for ABACUS #718

Merged
merged 13 commits into from
Sep 20, 2024
Merged

Conversation

pxlxingliang
Copy link
Contributor

@pxlxingliang pxlxingliang commented Sep 4, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced data retrieval capabilities with the inclusion of magnetic moment and force data in the output.
    • Streamlined method for handling parameters in the ABACUS plugin, improving usability.
    • New function added for registering magnetic data types, enhancing functionality.
    • Comprehensive test suite introduced for validating ABACUS spin simulation functionalities.
  • Bug Fixes

    • Improved control flow to ensure accurate integration of new magnetic data.
  • Tests

    • Introduced new test cases to validate the accuracy of ABACUS spin simulation outputs, enhancing overall test coverage.

@pxlxingliang pxlxingliang changed the base branch from master to devel September 4, 2024 05:36
Copy link

coderabbitai bot commented Sep 4, 2024

Walkthrough

Walkthrough

The changes focus on enhancing the dpdata/plugins/abacus.py file by modifying the to_system method to simplify its parameter handling and introducing a new function, register_mag_data, for managing magnetic data types. Additionally, a new test suite in tests/test_abacus_spin.py has been created to validate the functionality of the ABACUS spin simulation module across various scenarios, ensuring the accuracy and integrity of the processed data.

Changes

Files Change Summary
dpdata/plugins/abacus.py Simplified to_system method by removing several parameters and utilizing **kwargs; added register_mag_data for magnetic data registration.
tests/test_abacus_spin.py Introduced a suite of unit tests for the ABACUS spin simulation module, validating SCF, relaxation, and MD scenarios with accuracy checks.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between be0f414 and c3c6196.

Files selected for processing (2)
  • dpdata/plugins/abacus.py (4 hunks)
  • tests/test_abacus_spin.py (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • dpdata/plugins/abacus.py
  • tests/test_abacus_spin.py

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

codspeed-hq bot commented Sep 4, 2024

CodSpeed Performance Report

Merging #718 will not alter performance

Comparing pxlxingliang:spin (c3c6196) with devel (482775f)

Summary

✅ 2 untouched benchmarks

Copy link

codecov bot commented Sep 4, 2024

Codecov Report

Attention: Patch coverage is 91.48148% with 23 lines in your changes missing coverage. Please review.

Project coverage is 84.73%. Comparing base (2648d50) to head (c3c6196).
Report is 26 commits behind head on devel.

Files with missing lines Patch % Lines
dpdata/abacus/scf.py 90.45% 23 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            devel     #718      +/-   ##
==========================================
+ Coverage   84.42%   84.73%   +0.31%     
==========================================
  Files          81       81              
  Lines        7095     7345     +250     
==========================================
+ Hits         5990     6224     +234     
- Misses       1105     1121      +16     

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

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

Outside diff range, codebase verification and nitpick comments (1)
dpdata/abacus/scf.py (1)

Line range hint 530-751: The enhancements to make_unlabeled_stru are impressive!

The modifications significantly enhance the functionality of the function by allowing the inclusion of additional atomic properties in the generated STRU file. The new parameters are correctly handled, and the code is well-structured with appropriate error handling and validation checks.

However, there are a couple of areas that could be improved:

  1. Test Coverage:
    The static analysis hints from codecov indicate that some of the added lines are not covered by tests. To ensure the correctness and robustness of the new functionality, it would be beneficial to add tests for the new code paths.

If you need any help with writing tests for the new functionality, please let me know. I'd be happy to assist you in improving the test coverage.

  1. Code Simplification:
    The hint from Ruff suggests using a single if statement instead of nested if statements to improve code readability and simplicity.

Consider simplifying the nested if statements at lines 604-605 to enhance code clarity. Here's a suggestion:

if key is not None and not isinstance(ndarray2list(key), (list, tuple)) and len(key) != atom_numbs:
    key_name = [name for name, value in locals().items() if value is key][0]
    print(f"ERROR: make_unlabeled_stru: the length of '{key_name}' ({len(key)}) should be equal to the number of atom number ({atom_numbs}).")
    return ""

This change combines the two conditions into a single if statement, making the code more concise and easier to understand.

Tools
Ruff

604-605: Use a single if statement instead of nested if statements

(SIM102)

GitHub Check: codecov/patch

[warning] 583-591: dpdata/abacus/scf.py#L583-L591
Added lines #L583 - L591 were not covered by tests


[warning] 595-595: dpdata/abacus/scf.py#L595
Added line #L595 was not covered by tests


[warning] 600-600: dpdata/abacus/scf.py#L600
Added line #L600 was not covered by tests


[warning] 608-610: dpdata/abacus/scf.py#L608-L610
Added lines #L608 - L610 were not covered by tests


[warning] 623-623: dpdata/abacus/scf.py#L623
Added line #L623 was not covered by tests


[warning] 629-630: dpdata/abacus/scf.py#L629-L630
Added lines #L629 - L630 were not covered by tests


[warning] 634-634: dpdata/abacus/scf.py#L634
Added line #L634 was not covered by tests


[warning] 639-641: dpdata/abacus/scf.py#L639-L641
Added lines #L639 - L641 were not covered by tests


[warning] 653-653: dpdata/abacus/scf.py#L653
Added line #L653 was not covered by tests


[warning] 658-659: dpdata/abacus/scf.py#L658-L659
Added lines #L658 - L659 were not covered by tests


[warning] 663-663: dpdata/abacus/scf.py#L663
Added line #L663 was not covered by tests


[warning] 668-669: dpdata/abacus/scf.py#L668-L669
Added lines #L668 - L669 were not covered by tests


[warning] 680-680: dpdata/abacus/scf.py#L680
Added line #L680 was not covered by tests

dpdata/abacus/md.py Outdated Show resolved Hide resolved
Copy link
Contributor

@wanghan-iapcm wanghan-iapcm left a comment

Choose a reason for hiding this comment

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

spins and mag_forces are not registered, which means that when you dump LabeledSystem to e.g. deepmd/npy, you won't have spins and mag_forces data output.

UTs detecting the issue are necessary.

dpdata/abacus/md.py Outdated Show resolved Hide resolved
dpdata/abacus/scf.py Show resolved Hide resolved
dpdata/abacus/scf.py Outdated Show resolved Hide resolved
dpdata/abacus/scf.py Outdated Show resolved Hide resolved
dpdata/abacus/scf.py Outdated Show resolved Hide resolved
dpdata/abacus/scf.py Outdated Show resolved Hide resolved
@wanghan-iapcm wanghan-iapcm requested a review from njzjz September 5, 2024 01:10
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: 13

Outside diff range, codebase verification and nitpick comments (5)
tests/abacus.spin/INPUT (1)

1-27: Configuration file appears complete and correct.

The configuration parameters are well-defined and appropriate for supporting spin calculations in ABACUS. Consider adding a header comment at the top of the file to briefly describe its purpose and usage.

tests/abacus.spin/STRU (1)

1-21: Structure file appears complete and correct.

The atomic structure definitions are well-defined and appropriate for supporting spin calculations in ABACUS. Consider adding a header comment at the top of the file to briefly describe its purpose and usage.

dpdata/abacus/scf.py (3)

Line range hint 201-250: Approve modifications to get_coords and suggest comprehensive testing.

The changes to get_coords effectively integrate the parsing of new atomic properties using parse_stru_pos. It is crucial to ensure that these modifications are covered by comprehensive tests, especially since they involve multiple new data attributes that are critical to the system's modeling capabilities.

Would you like assistance in setting up tests to cover these new functionalities?


Line range hint 392-454: Approve updates to get_frame and recommend verification of magnetic data integration.

The updates to get_frame to include magnetic moment and force data are well-implemented. It is recommended to verify the integration of this new data through functional testing to ensure that the magnetic properties are correctly retrieved and represented in the output.

Would you like assistance in setting up functional tests to verify the magnetic data integration?


Line range hint 527-754: Approve enhancements to make_unlabeled_stru and suggest enhancing documentation.

The enhancements to make_unlabeled_stru to include new parameters for movement, velocity, magnetic moments, angles, spin constraints, and lambda values are correctly implemented. It is advisable to enhance the documentation to clearly describe these new parameters and their impact on the output format, ensuring that users can effectively utilize the updated function.

Would you like help in updating the documentation to reflect these changes?

Tools
Ruff

610-611: Use a single if statement instead of nested if statements

(SIM102)

GitHub Check: codecov/patch

[warning] 586-594: dpdata/abacus/scf.py#L586-L594
Added lines #L586 - L594 were not covered by tests


[warning] 598-598: dpdata/abacus/scf.py#L598
Added line #L598 was not covered by tests


[warning] 603-603: dpdata/abacus/scf.py#L603
Added line #L603 was not covered by tests


[warning] 614-616: dpdata/abacus/scf.py#L614-L616
Added lines #L614 - L616 were not covered by tests


[warning] 629-629: dpdata/abacus/scf.py#L629
Added line #L629 was not covered by tests


[warning] 635-636: dpdata/abacus/scf.py#L635-L636
Added lines #L635 - L636 were not covered by tests


[warning] 640-640: dpdata/abacus/scf.py#L640
Added line #L640 was not covered by tests


[warning] 645-647: dpdata/abacus/scf.py#L645-L647
Added lines #L645 - L647 were not covered by tests


[warning] 659-659: dpdata/abacus/scf.py#L659
Added line #L659 was not covered by tests


[warning] 664-665: dpdata/abacus/scf.py#L664-L665
Added lines #L664 - L665 were not covered by tests


[warning] 669-669: dpdata/abacus/scf.py#L669
Added line #L669 was not covered by tests


[warning] 674-675: dpdata/abacus/scf.py#L674-L675
Added lines #L674 - L675 were not covered by tests


[warning] 686-686: dpdata/abacus/scf.py#L686
Added line #L686 was not covered by tests

tests/abacus.relax/STRU.spin Show resolved Hide resolved
tests/abacus.spin/INPUT.relax Show resolved Hide resolved
tests/abacus.spin/INPUT.md Show resolved Hide resolved
tests/abacus.spin/OUT.ABACUS/MD_dump Show resolved Hide resolved
tests/test_abacus_spin.py Outdated Show resolved Hide resolved
dpdata/deepmd/raw.py Outdated Show resolved Hide resolved
dpdata/deepmd/hdf5.py Outdated Show resolved Hide resolved
dpdata/deepmd/hdf5.py Outdated Show resolved Hide resolved
dpdata/abacus/scf.py Outdated Show resolved Hide resolved
dpdata/abacus/scf.py Outdated Show resolved Hide resolved
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: 3

Outside diff range, codebase verification and nitpick comments (2)
dpdata/abacus/scf.py (2)

Line range hint 206-255: Review of get_coords function:

This function is responsible for retrieving and storing atomic coordinates and related attributes from structured blocks of data. It utilizes the parse_stru_pos function to parse individual lines of atomic data.

Observations and Recommendations:

  1. Integration with parse_stru_pos: The function correctly integrates the parse_stru_pos function to parse each line of atomic data. This modular approach is good for maintainability.
  2. Error Handling: Similar to parse_stru_pos, consider adding error handling for cases where the expected data blocks are not found or are malformed.
  3. Performance Considerations: The function processes potentially large data blocks. Ensure that the performance is acceptable for large inputs. Consider optimizations if necessary.

Code Quality:

  • The function is structured logically, and the use of helper functions like get_stru_block aids in readability.
  • Consider adding more inline comments explaining the logic, especially in the more complex parsing sections.

Security:

  • As with parse_stru_pos, ensure that input data is validated and sanitized if it comes from untrusted sources.

Documentation:

  • The function's purpose and behavior are clear from the context, but adding a docstring describing the parameters, expected input format, and output would enhance understandability.

Tags:

  • : The integration with parse_stru_pos is handled well.
  • : For missing error handling and potential performance issues with large inputs.
  • : To help in optimizing performance and adding necessary error handling.

Would you like suggestions on optimizing performance or implementing error handling?


Line range hint 394-462: Review of get_frame function:

This function is a central part of the module, responsible for retrieving a complete frame of atomic data, including coordinates, energies, forces, and now magnetic moments and forces.

Observations and Recommendations:

  1. Integration of New Features: The function has been updated to include magnetic data, which is a significant enhancement. The integration seems to be handled correctly, using the get_mag_force function.
  2. Error Handling: The function handles file existence checks but could be improved to handle other potential errors, such as parsing errors or data inconsistencies.
  3. Performance: The function deals with multiple file reads and data parsing operations. Ensure that its performance is acceptable, especially for large input files or numerous frames.

Code Quality:

  • The function is well-organized, with clear separation of concerns. Each block of functionality (e.g., reading files, parsing data) is distinct, which aids in readability.
  • Consider adding more detailed comments to explain the logic, especially for the new features related to magnetic data.

Security:

  • Ensure that all file operations and data parsing are secure, especially if data can come from untrusted sources. Consider adding validation and sanitization steps.

Documentation:

  • Update the documentation to reflect the new capabilities of the function, including the handling of magnetic data.

Tags:

  • : The updates to include magnetic data are well-integrated.
  • : For potential missing error handling and performance concerns.
  • : To help in optimizing performance and enhancing error handling.

Would you like suggestions on optimizing performance or enhancing error handling?

dpdata/abacus/scf.py Outdated Show resolved Hide resolved
dpdata/system.py Outdated Show resolved Hide resolved
dpdata/abacus/scf.py Show resolved Hide resolved
dpdata/abacus/scf.py Outdated Show resolved Hide resolved
dpdata/abacus/scf.py Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

you still have may lines not covered by the UT. please check carefully and improve the UTs.

Copy link
Contributor

@wanghan-iapcm wanghan-iapcm left a comment

Choose a reason for hiding this comment

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

Please use register_data_type instead of hacking all over the code for spins and mag_forces.

def register_data_type(cls, *data_type: DataType):

@pxlxingliang
Copy link
Contributor Author

Please use register_data_type instead of hacking all over the code for spins and mag_forces.

def register_data_type(cls, *data_type: DataType):

Is there some examples or doc to use this function?

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

Outside diff range, codebase verification and nitpick comments (2)
dpdata/abacus/scf.py (2)

Line range hint 236-285: Review of get_coords function:

This function retrieves atomic coordinates and other related properties from a structured block of lines. It utilizes the parse_stru_pos function to parse individual lines and extract detailed atomic properties.

Observations and Recommendations:

  1. Integration with parse_stru_pos: The function effectively integrates the output from parse_stru_pos, demonstrating good modularity and reuse of code.
  2. Error Handling: The function assumes that the input lines are well-formed and that all necessary data is present. Consider adding checks to handle cases where the expected data is missing or malformed.
  3. Performance: The function iterates over the lines multiple times, which could be optimized. Consider processing the lines in a single pass if possible.

Code Quality:

  • The use of list comprehensions and numpy for handling numerical data is efficient and concise.
  • The function's logic is straightforward, but adding more detailed comments explaining each section would be helpful.

Security:

  • If the input data can come from untrusted sources, consider adding mechanisms to validate and sanitize the input data.

Documentation:

  • The function is documented with comments explaining the process, which is helpful. Ensure that the documentation is comprehensive and covers all aspects of the function's behavior.

Tags:

  • : The basic functionality of retrieving atomic coordinates is implemented correctly.
  • : For potential performance issues.
  • : To help in optimizing the parsing logic and adding robust error handling.

Line range hint 565-822: Review of make_unlabeled_stru function:

This function generates an unlabeled STRU file from a dictionary of system data. It handles various parameters such as pseudo potential files, orbital files, and atomic properties like magnetic moments and angles.

Observations and Recommendations:

  1. Complexity and Maintainability: The function is complex due to its handling of multiple parameters and conditions. Consider refactoring to simplify the logic and improve readability.
  2. Error Handling: The function prints errors but does not raise exceptions or handle errors robustly. Consider raising exceptions for critical errors to ensure that they are handled appropriately in the calling code.
  3. Performance: The function iterates over multiple lists and performs several checks, which could be optimized. Consider simplifying the logic and reducing the number of iterations.

Code Quality:

  • The function uses descriptive variable names and comments, which help in understanding its logic. However, the complexity could be reduced.
  • The function modifies the output string directly, which could lead to issues if the function is interrupted. Consider using a more robust method of constructing the output.

Security:

  • If the input data can come from untrusted sources, consider adding sanitization and validation to prevent injection attacks or data corruption.

Documentation:

  • The function is well-documented with a detailed docstring describing the input and output. Ensure that the documentation is updated if any changes are made to the function's logic or parameters.

Tags:

  • : To improve readability and maintainability.
  • : For missing error handling.
  • : To help in optimizing the function and improving error handling.
Tools
Ruff

646-650: Use a single if statement instead of nested if statements

(SIM102)

dpdata/abacus/scf.py Outdated Show resolved Hide resolved
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: 0

Outside diff range, codebase verification and nitpick comments (1)
dpdata/abacus/scf.py (1)

Line range hint 572-832: Approve changes to make_unlabeled_stru with a refactoring suggestion.

The modifications to make_unlabeled_stru correctly integrate the new attributes into the STRU file generation. The function handles various scenarios for the new attributes and implements the linking of files and handling of spins data correctly.

However, consider the following refactoring suggestion:

  • Refactor the nested if statements at lines 653-657 to use a single if statement with logical operators. This will improve code readability and maintainability.

Here's an example of how you can refactor the nested if statements:

if key is not None and not isinstance(ndarray2list(key), (list, tuple)) and len(key) != atom_numbs:
    key_name = [name for name, value in locals().items() if value is key][0]
    print(f"ERROR: make_unlabeled_stru: the length of '{key_name}' ({len(key)}) should be equal to the number of atom number ({atom_numbs}).")
    return ""
Tools
Ruff

653-657: Use a single if statement instead of nested if statements

(SIM102)

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

Outside diff range, codebase verification and nitpick comments (1)
dpdata/abacus/scf.py (1)

Line range hint 583-832: Approve the changes to make_unlabeled_stru and suggest minor refactoring and improving test coverage.

The changes made to the make_unlabeled_stru function to include the new parameters and generate the corresponding output in the STRU file are implemented correctly. However, consider the following suggestions:

  1. Use a single if statement instead of nested if statements at line range 653-657 to improve code readability.

  2. The static analysis hints indicate missing test coverage for several lines added to this function. Consider adding more comprehensive unit tests, especially for the lines flagged by the tool, to ensure the correctness of the function across various scenarios.

Would you like assistance in refactoring the code and adding unit tests?

Tools
Ruff

653-657: Use a single if statement instead of nested if statements

(SIM102)

GitHub Check: codecov/patch

[warning] 625-633: dpdata/abacus/scf.py#L625-L633
Added lines #L625 - L633 were not covered by tests


[warning] 637-637: dpdata/abacus/scf.py#L637
Added line #L637 was not covered by tests


[warning] 642-642: dpdata/abacus/scf.py#L642
Added line #L642 was not covered by tests


[warning] 658-659: dpdata/abacus/scf.py#L658-L659
Added lines #L658 - L659 were not covered by tests


[warning] 662-662: dpdata/abacus/scf.py#L662
Added line #L662 was not covered by tests


[warning] 670-670: dpdata/abacus/scf.py#L670
Added line #L670 was not covered by tests


[warning] 681-681: dpdata/abacus/scf.py#L681
Added line #L681 was not covered by tests


[warning] 684-684: dpdata/abacus/scf.py#L684
Added line #L684 was not covered by tests


[warning] 688-688: dpdata/abacus/scf.py#L688
Added line #L688 was not covered by tests


[warning] 693-695: dpdata/abacus/scf.py#L693-L695
Added lines #L693 - L695 were not covered by tests


[warning] 707-707: dpdata/abacus/scf.py#L707
Added line #L707 was not covered by tests


[warning] 712-712: dpdata/abacus/scf.py#L712
Added line #L712 was not covered by tests


[warning] 715-715: dpdata/abacus/scf.py#L715
Added line #L715 was not covered by tests


[warning] 719-719: dpdata/abacus/scf.py#L719
Added line #L719 was not covered by tests


[warning] 724-726: dpdata/abacus/scf.py#L724-L726
Added lines #L724 - L726 were not covered by tests


[warning] 736-738: dpdata/abacus/scf.py#L736-L738
Added lines #L736 - L738 were not covered by tests

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

Outside diff range, codebase verification and nitpick comments (1)
dpdata/abacus/scf.py (1)

Line range hint 572-835: Approve the implementation of make_unlabeled_stru but recommend adding more unit tests.

The changes to the make_unlabeled_stru function significantly enhance its functionality by supporting the generation of atomic positions with additional properties. The function correctly handles the new parameters and generates the appropriate output format.

However, static analysis hints suggest that some lines are not covered by tests. To ensure the correctness of the function across various input scenarios, it is recommended to add more comprehensive unit tests, particularly for the lines that are currently uncovered.

Would you like assistance in writing unit tests for the uncovered lines?

Tools
Ruff

658-662: Use a single if statement instead of nested if statements

(SIM102)

GitHub Check: codecov/patch

[warning] 626-627: dpdata/abacus/scf.py#L626-L627
Added lines #L626 - L627 were not covered by tests


[warning] 630-630: dpdata/abacus/scf.py#L630
Added line #L630 was not covered by tests


[warning] 633-634: dpdata/abacus/scf.py#L633-L634
Added lines #L633 - L634 were not covered by tests


[warning] 636-636: dpdata/abacus/scf.py#L636
Added line #L636 was not covered by tests


[warning] 642-642: dpdata/abacus/scf.py#L642
Added line #L642 was not covered by tests


[warning] 647-647: dpdata/abacus/scf.py#L647
Added line #L647 was not covered by tests


[warning] 663-664: dpdata/abacus/scf.py#L663-L664
Added lines #L663 - L664 were not covered by tests


[warning] 667-667: dpdata/abacus/scf.py#L667
Added line #L667 was not covered by tests


[warning] 675-675: dpdata/abacus/scf.py#L675
Added line #L675 was not covered by tests


[warning] 686-686: dpdata/abacus/scf.py#L686
Added line #L686 was not covered by tests


[warning] 689-689: dpdata/abacus/scf.py#L689
Added line #L689 was not covered by tests


[warning] 693-693: dpdata/abacus/scf.py#L693
Added line #L693 was not covered by tests


[warning] 712-712: dpdata/abacus/scf.py#L712
Added line #L712 was not covered by tests


[warning] 717-717: dpdata/abacus/scf.py#L717
Added line #L717 was not covered by tests


[warning] 720-720: dpdata/abacus/scf.py#L720
Added line #L720 was not covered by tests


[warning] 724-724: dpdata/abacus/scf.py#L724
Added line #L724 was not covered by tests

dpdata/abacus/scf.py Outdated Show resolved Hide resolved
dpdata/abacus/scf.py Outdated Show resolved Hide resolved
@njzjz
Copy link
Member

njzjz commented Sep 10, 2024

Please fix errors reported by pre-commit.ci - pr.

@wanghan-iapcm
Copy link
Contributor

wanghan-iapcm commented Sep 11, 2024

Please use register_data_type instead of hacking all over the code for spins and mag_forces.

def register_data_type(cls, *data_type: DataType):

Is there some examples or doc to use this function?

The doc string should be clear enough.
example:

self.cls.register_data_type(dt)

@pxlxingliang
Copy link
Contributor Author

pxlxingliang commented Sep 11, 2024

Please use register_data_type instead of hacking all over the code for spins and mag_forces.

def register_data_type(cls, *data_type: DataType):

Is there some examples or doc to use this function?

The doc string should be clear enough. example:

self.cls.register_data_type(dt)

I have added the register codes in dpdata/plugins/abacus.py, but when I dump a dpdata.System to deepmd npy type by mysys.to(file_name="dump", fmt="deepmd/npy"), there still have no spins.npy and mag_forces.npy.

def register_data(data):
    if "spins" in data:
        dt = DataType("spins", np.ndarray, (Axis.NFRAMES,Axis.NATOMS,3 ), required=True)
        print("registering spins")
        dpdata.System.register_data_type(dt)
    if "mag_forces" in data:
        dt = DataType("mag_forces", np.ndarray, (Axis.NFRAMES,Axis.NATOMS,3 ), required=True)
        print("registering mag_forces")
        dpdata.System.register_data_type(dt)

@Format.register("abacus/scf")
@Format.register("abacus/pw/scf")
@Format.register("abacus/lcao/scf")
class AbacusSCFFormat(Format):
    # @Format.post("rot_lower_triangular")
    def from_labeled_system(self, file_name, **kwargs):
        data = dpdata.abacus.scf.get_frame(file_name)
        register_data(data)
        return data

@njzjz
Copy link
Member

njzjz commented Sep 11, 2024

You register for System in LabeledSystem

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

Outside diff range and nitpick comments (1)
tests/abacus.scf/stru.ref (1)

1-29: Changes look good, but consider adding comments to clarify the additional attributes.

The file structure and format appear to be correct for an ABACUS input file used for testing purposes. The additional attributes in the atomic positions section, like magnetic moments, angles, and lambda values, seem to be intentional based on the PR objectives related to handling spins and magnetic forces.

Assuming these changes align with the specific testing requirements, they are approved. However, consider adding comments to clarify the meaning and purpose of the additional attributes, if appropriate, to improve the maintainability and readability of the test file.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2195ebe and ba7bcad.

Files ignored due to path filters (3)
  • tests/abacus.spin/OUT.ABACUS/running_md.log is excluded by !**/*.log
  • tests/abacus.spin/OUT.ABACUS/running_relax.log is excluded by !**/*.log
  • tests/abacus.spin/OUT.ABACUS/running_scf.log is excluded by !**/*.log
Files selected for processing (15)
  • dpdata/abacus/md.py (3 hunks)
  • dpdata/abacus/relax.py (3 hunks)
  • dpdata/abacus/scf.py (10 hunks)
  • dpdata/plugins/abacus.py (4 hunks)
  • tests/abacus.relax/INPUT.spin (1 hunks)
  • tests/abacus.relax/STRU.spin (1 hunks)
  • tests/abacus.scf/stru.ref (1 hunks)
  • tests/abacus.spin/INPUT (1 hunks)
  • tests/abacus.spin/INPUT.md (1 hunks)
  • tests/abacus.spin/INPUT.relax (1 hunks)
  • tests/abacus.spin/INPUT.scf (1 hunks)
  • tests/abacus.spin/OUT.ABACUS/MD_dump (1 hunks)
  • tests/abacus.spin/STRU (1 hunks)
  • tests/test_abacus_spin.py (1 hunks)
  • tests/test_abacus_stru_dump.py (2 hunks)
Files skipped from review due to trivial changes (1)
  • tests/abacus.relax/INPUT.spin
Files skipped from review as they are similar to previous changes (8)
  • dpdata/abacus/md.py
  • dpdata/abacus/relax.py
  • tests/abacus.relax/STRU.spin
  • tests/abacus.spin/INPUT
  • tests/abacus.spin/INPUT.relax
  • tests/abacus.spin/INPUT.scf
  • tests/abacus.spin/OUT.ABACUS/MD_dump
  • tests/test_abacus_spin.py
Additional context used
LanguageTool
tests/abacus.spin/INPUT.md

[misspelling] ~9-~9: ‘pw’ zou fout kunnen zijn. Misschien bedoelt u: “p.w.”
Context: ...genelpa basis_type pw symmetry 0 nonco...

(NL_SIMPLE_REPLACE_PW)


[misspelling] ~29-~29: In deze afkorting horen punten.
Context: ... 3 md_type nvt md_nstep 3 md_dt ...

(NL_SIMPLE_REPLACE_NVT)


[misspelling] ~31-~31: ‘dt’ zou fout kunnen zijn. Misschien bedoelt u: “dat”, “dit”
Context: ... nvt md_nstep 3 md_dt 1 md_tfirst ...

(NL_SIMPLE_REPLACE_DT)

Markdownlint
tests/abacus.spin/INPUT.md

11-11: Column: 9
Hard tabs

(MD010, no-hard-tabs)


8-8: null
No space after hash on atx style heading

(MD018, no-missing-space-atx)

Ruff
dpdata/abacus/scf.py

670-674: Use a single if statement instead of nested if statements

(SIM102)

GitHub Check: codecov/patch
dpdata/abacus/scf.py

[warning] 638-639: dpdata/abacus/scf.py#L638-L639
Added lines #L638 - L639 were not covered by tests


[warning] 642-642: dpdata/abacus/scf.py#L642
Added line #L642 was not covered by tests


[warning] 645-646: dpdata/abacus/scf.py#L645-L646
Added lines #L645 - L646 were not covered by tests


[warning] 648-648: dpdata/abacus/scf.py#L648
Added line #L648 was not covered by tests


[warning] 654-654: dpdata/abacus/scf.py#L654
Added line #L654 was not covered by tests


[warning] 659-659: dpdata/abacus/scf.py#L659
Added line #L659 was not covered by tests


[warning] 675-676: dpdata/abacus/scf.py#L675-L676
Added lines #L675 - L676 were not covered by tests


[warning] 679-679: dpdata/abacus/scf.py#L679
Added line #L679 was not covered by tests


[warning] 687-687: dpdata/abacus/scf.py#L687
Added line #L687 was not covered by tests


[warning] 698-698: dpdata/abacus/scf.py#L698
Added line #L698 was not covered by tests


[warning] 701-701: dpdata/abacus/scf.py#L701
Added line #L701 was not covered by tests


[warning] 705-705: dpdata/abacus/scf.py#L705
Added line #L705 was not covered by tests


[warning] 724-724: dpdata/abacus/scf.py#L724
Added line #L724 was not covered by tests


[warning] 729-729: dpdata/abacus/scf.py#L729
Added line #L729 was not covered by tests


[warning] 732-732: dpdata/abacus/scf.py#L732
Added line #L732 was not covered by tests


[warning] 736-736: dpdata/abacus/scf.py#L736
Added line #L736 was not covered by tests


[warning] 776-776: dpdata/abacus/scf.py#L776
Added line #L776 was not covered by tests


[warning] 793-793: dpdata/abacus/scf.py#L793
Added line #L793 was not covered by tests


[warning] 806-806: dpdata/abacus/scf.py#L806
Added line #L806 was not covered by tests


[warning] 844-844: dpdata/abacus/scf.py#L844
Added line #L844 was not covered by tests

Additional comments not posted (13)
tests/abacus.spin/STRU (1)

1-21: LGTM!

The STRU file for the ABACUS spin test case looks good. It follows the standard format and correctly specifies the magnetic moments and selective coordinates for the Fe atoms.

The code changes are approved.

tests/abacus.spin/INPUT.md (1)

1-34: The past review comment is still valid and applicable:

Well-structured input file for molecular dynamics simulations.

The file is well-structured and aligns with the PR's objective to enhance the handling of magnetic data in ABACUS. It would be beneficial to include documentation or comments explaining the parameters, especially the new ones related to magnetic data handling, to aid other developers or users in understanding their usage.

Would you like me to help draft the documentation for these parameters?

Tools
LanguageTool

[misspelling] ~9-~9: ‘pw’ zou fout kunnen zijn. Misschien bedoelt u: “p.w.”
Context: ...genelpa basis_type pw symmetry 0 nonco...

(NL_SIMPLE_REPLACE_PW)


[misspelling] ~29-~29: In deze afkorting horen punten.
Context: ... 3 md_type nvt md_nstep 3 md_dt ...

(NL_SIMPLE_REPLACE_NVT)


[misspelling] ~31-~31: ‘dt’ zou fout kunnen zijn. Misschien bedoelt u: “dat”, “dit”
Context: ... nvt md_nstep 3 md_dt 1 md_tfirst ...

(NL_SIMPLE_REPLACE_DT)

Markdownlint

11-11: Column: 9
Hard tabs

(MD010, no-hard-tabs)


8-8: null
No space after hash on atx style heading

(MD018, no-missing-space-atx)

dpdata/plugins/abacus.py (5)

49-60: LGTM!

The code changes are approved. The register_data function correctly registers the optional data types for spins and magnetic forces with dpdata.LabeledSystem based on their presence in the data.


68-70: LGTM!

The code changes are approved. Calling register_data after retrieving the frame data in the from_labeled_system method of AbacusSCFFormat ensures that the optional data types for spins and magnetic forces are correctly registered if present.


79-81: LGTM!

The code changes are approved. Calling register_data after retrieving the frame data in the from_labeled_system method of AbacusMDFormat ensures that the optional data types for spins and magnetic forces are correctly registered if present.


90-92: LGTM!

The code changes are approved. Calling register_data after retrieving the frame data in the from_labeled_system method of AbacusRelaxFormat ensures that the optional data types for spins and magnetic forces are correctly registered if present.


42-43: LGTM!

The code changes are approved. Deriving dest_dir from file_name using os.path.dirname in the to_system method of AbacusSTRUFormat simplifies the method's interface and enhances usability while maintaining the core functionality.

tests/test_abacus_stru_dump.py (5)

17-20: LGTM!

The changes to the tearDown method ensure proper cleanup of temporary files after tests, improving the overall test suite.


32-52: LGTM!

The new test_dumpStruLinkFile method enhances the test coverage by validating the creation of symbolic links during the structural data dump. The test checks for the existence of the temporary file and verifies that the expected symbolic links are created correctly.


54-75: LGTM!

The expanded test_dump_spinconstrain method improves the robustness of the test suite by including more parameters related to spin constraints and verifying the output against a reference file. This ensures that the functionality related to spin constraints is working as expected.


77-104: LGTM!

The new test_dump_spin method further enhances the test coverage by testing the dumping of spin data. The test sets the spins data in the system and verifies that the dumped STRU file contains the expected spin data.


107-182: LGTM!

The new TestABACUSParseStru class significantly enhances the test coverage for the parse_stru_pos function. The test_parse_stru_post method thoroughly tests the parsing of various input scenarios, ensuring that the function correctly extracts the expected values. The test_parse_stru_error method verifies that the function raises appropriate errors for invalid inputs, improving the robustness of the code. These additions greatly improve the overall quality and reliability of the codebase.

dpdata/abacus/scf.py (1)

Line range hint 269-309: Looks good! Please improve unit test coverage.

The changes to the get_coords function are well-integrated and provide a more comprehensive set of atomic properties. The use of the parse_stru_pos function simplifies the parsing logic.

However, please ensure that the new code paths are adequately covered by unit tests to maintain a high level of code quality and prevent regressions.

dpdata/abacus/scf.py Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ba7bcad and 2d85336.

Files selected for processing (1)
  • dpdata/abacus/scf.py (11 hunks)
Additional context used
Ruff
dpdata/abacus/scf.py

670-674: Use a single if statement instead of nested if statements

(SIM102)

GitHub Check: codecov/patch
dpdata/abacus/scf.py

[warning] 638-639: dpdata/abacus/scf.py#L638-L639
Added lines #L638 - L639 were not covered by tests


[warning] 642-642: dpdata/abacus/scf.py#L642
Added line #L642 was not covered by tests


[warning] 645-646: dpdata/abacus/scf.py#L645-L646
Added lines #L645 - L646 were not covered by tests


[warning] 648-648: dpdata/abacus/scf.py#L648
Added line #L648 was not covered by tests


[warning] 654-654: dpdata/abacus/scf.py#L654
Added line #L654 was not covered by tests


[warning] 659-659: dpdata/abacus/scf.py#L659
Added line #L659 was not covered by tests


[warning] 675-676: dpdata/abacus/scf.py#L675-L676
Added lines #L675 - L676 were not covered by tests


[warning] 679-679: dpdata/abacus/scf.py#L679
Added line #L679 was not covered by tests


[warning] 687-687: dpdata/abacus/scf.py#L687
Added line #L687 was not covered by tests


[warning] 698-698: dpdata/abacus/scf.py#L698
Added line #L698 was not covered by tests


[warning] 701-701: dpdata/abacus/scf.py#L701
Added line #L701 was not covered by tests


[warning] 705-705: dpdata/abacus/scf.py#L705
Added line #L705 was not covered by tests


[warning] 724-724: dpdata/abacus/scf.py#L724
Added line #L724 was not covered by tests


[warning] 729-729: dpdata/abacus/scf.py#L729
Added line #L729 was not covered by tests


[warning] 732-732: dpdata/abacus/scf.py#L732
Added line #L732 was not covered by tests


[warning] 736-736: dpdata/abacus/scf.py#L736
Added line #L736 was not covered by tests


[warning] 776-776: dpdata/abacus/scf.py#L776
Added line #L776 was not covered by tests


[warning] 789-789: dpdata/abacus/scf.py#L789
Added line #L789 was not covered by tests


[warning] 802-802: dpdata/abacus/scf.py#L802
Added line #L802 was not covered by tests


[warning] 840-840: dpdata/abacus/scf.py#L840
Added line #L840 was not covered by tests

Additional comments not posted (3)
dpdata/abacus/scf.py (3)

269-275: LGTM!

The code changes are approved. The modifications to get_coords correctly integrate the new attributes parsed by parse_stru_pos and ensure they are properly stored and returned.

Also applies to: 285-287, 301-308


115-258: Approve the implementation of parse_stru_pos but recommend refactoring, adding error handling, and improving test coverage.

The function parse_stru_pos is well-implemented with a clear parsing mechanism for various atomic properties. However, consider the following suggestions:

  1. The function is quite complex with multiple nested conditions and loops. Consider refactoring to improve readability and maintainability.

  2. There is no error handling for malformed input lines, which could lead to unexpected behavior or crashes. Add validation checks and error handling mechanisms.

  3. Given the complexity and the critical role of the function, it is crucial to have comprehensive unit tests to ensure its correctness across various input scenarios. The static analysis tool indicates that some lines (e.g., 202) are not covered by tests. Please improve the test coverage.

Would you like assistance in refactoring this function or adding unit tests and error handling?


585-591: Approve the implementation of the new parameters in make_unlabeled_stru but recommend improving test coverage and simplifying a condition.

The modifications to make_unlabeled_stru correctly handle the new parameters related to movement, velocity, magnetic moments, angles, spin constraints, and lambda values. The changes ensure that these properties are properly formatted and included in the output structure file. The function also includes appropriate error handling and validation for the new parameters.

However, please consider the following suggestions:

  1. The static analysis tool indicates that some lines related to the new parameters (e.g., 675-676, 679) are not covered by unit tests. Please improve the test coverage for these lines.

  2. At lines 670-674, consider using a single if statement instead of nested if statements to simplify the condition, as suggested by the static analysis tool.

Also applies to: 665-679, 784-840

dpdata/abacus/scf.py Show resolved Hide resolved
@Format.register("abacus/scf")
@Format.register("abacus/pw/scf")
@Format.register("abacus/lcao/scf")
class AbacusSCFFormat(Format):
# @Format.post("rot_lower_triangular")
def from_labeled_system(self, file_name, **kwargs):
return dpdata.abacus.scf.get_frame(file_name)
data = dpdata.abacus.scf.get_frame(file_name)
register_data(data)
Copy link
Contributor

Choose a reason for hiding this comment

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

do you need to register data if no magnetic label exists?

Copy link
Contributor Author

@pxlxingliang pxlxingliang Sep 14, 2024

Choose a reason for hiding this comment

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

No, in function register_data, it will register spins or mag_forces only when the key is in data.

dpdata/plugins/abacus.py Outdated Show resolved Hide resolved
Comment on lines 93 to 94
self.assertTrue(os.path.isfile(f"{self.dump_path}/set.000/spins.npy"))
self.assertTrue(os.path.isfile(f"{self.dump_path}/set.000/mag_forces.npy"))
Copy link
Member

@njzjz njzjz Sep 14, 2024

Choose a reason for hiding this comment

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

I think we use a different filename in the deepmd-kit. What is the expected behavior 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.

Hi,@wanghan-iapcm ,I need to clarify what the name should be, and they should be the same in DeepMD, LAMMPS, and ABACUS. In previous disccussion, you suggest the name "spins" and approve name "mag_forces".

Copy link
Member

Choose a reason for hiding this comment

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

We haven't had a documentation for spin data in the DeePMD-kit. xref: deepmodeling/deepmd-kit#3760

Copy link
Contributor

Choose a reason for hiding this comment

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

@pxlxingliang please follow PR #727 to support the deepmd-kit labeling files doced in deepmodeling/deepmd-kit#3762

@wanghan-iapcm wanghan-iapcm requested a review from njzjz September 20, 2024 02:06
@wanghan-iapcm wanghan-iapcm changed the title support spin for ABACUS feat: support spin for ABACUS Sep 20, 2024
np.ndarray,
(Axis.NFRAMES, Axis.NATOMS, 3),
required=False,
deepmd_name="mag_force",
Copy link
Member

Choose a reason for hiding this comment

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

It's force_mag, not mag_force

@wanghan-iapcm wanghan-iapcm merged commit c1d6c73 into deepmodeling:devel Sep 20, 2024
12 checks passed
@pxlxingliang pxlxingliang deleted the spin branch October 9, 2024 06:10
@coderabbitai coderabbitai bot mentioned this pull request Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants