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

Clarify argument shift for SlabGenerator.get_slab #3748

Open
wants to merge 57 commits into
base: master
Choose a base branch
from

Conversation

DanielYang59
Copy link
Contributor

@DanielYang59 DanielYang59 commented Apr 12, 2024

Summary

A follow-up PR for #3691.

  • Clarify misleading arg name shift for private method SlabGenerator.get_slab
  • Fix typing of slab in function center_slab (should be Structure instead of Slab)

Rationale

  • This method should be a private method (as per the docstring).
  • The arg name shift for SlabGenerator.get_slab corresponds to the desired termination coordinate (instead of the shift vector to get the Slab), which could be misleading:
    shift (float): The shift value along the lattice c direction in Angstrom.

    Which might cause confusion in understanding the implementation:
    frac_coords = np.array(frac_coords) + np.array([0, 0, -shift])[None, :]
image

Clarify shift attribute of Slab

Similarly, the description of shift attribute of Slab is incorrect, it is NOT the shift but the negative shift.

shift (float): The shift in the c-direction applied to get the
termination.

TODO for future PRs

In future PRs

  1. Function center_slab:

    # Get all site indices
    all_indices = list(range(len(slab)))
    # Get a reasonable cutoff radius to sample neighbors
    bond_dists = sorted(nn[1] for nn in slab.get_neighbors(slab[0], 10) if nn[1] > 0)
    # TODO (@DanielYang59): magic number for cutoff radius (would 3 be too large?)
    cutoff_radius = bond_dists[0] * 3
    # TODO (@DanielYang59): do we need the following complex method?
    # Why don't we just calculate the center of the Slab and move it to z=0.5?
    # Before moving we need to ensure there is only one Slab layer though
    # If structure is case 2, shift all the sites
    # to the other side until it is case 1
    for site in slab: # DEBUG (@DanielYang59): Slab position changes during loop?
    # DEBUG (@DanielYang59): sites below z=0 is not considered (only check coord > c)
    if any(nn[1] >= slab.lattice.c for nn in slab.get_neighbors(site, cutoff_radius)):
    # TODO (@DanielYang59): the magic offset "0.05" seems unnecessary,
    # as the Slab would be centered later anyway
    shift = 1 - site.frac_coords[2] + 0.05
    slab.translate_sites(all_indices, [0, 0, shift])
    # Now the slab is case 1, move it to the center
    weights = [site.species.weight for site in slab]
    center_of_mass = np.average(slab.frac_coords, weights=weights, axis=0)
    shift = 0.5 - center_of_mass[2]
    slab.translate_sites(all_indices, [0, 0, shift])
    return slab

    a. During the for site in slab: loop, site coordinates would change.
    b. This method to put the Slab to the center seems overly complex, can we just calculate the z-center of the Slab, and move it to z=0.5 (fractional)?
    c. If not b, Sites with z<0 is not considered (it would only move nn[1] >= slab.lattice.c).
    d. A magic number for determining near neighbors by cutoff radius: cutoff_radius = bond_dists[0] * 3.

  2. The move_to_other_side method of SlabGenerator:

    # Calculate Slab height
    height: float = self._proj_height
    # Scale height if using number of hkl planes
    if self.in_unit_planes:
    height /= self.parent.lattice.d_hkl(self.miller_index)
    # Calculate the moving distance as the fractional height
    # of the Slab inside the cell
    # DEBUG(@DanielYang59): the use actually sizes for slab/vac
    # instead of the input arg (min_slab/vac_size)
    n_layers_slab: int = math.ceil(self.min_slab_size / height)
    n_layers_vac: int = math.ceil(self.min_vac_size / height)
    n_layers: int = n_layers_slab + n_layers_vac
    frac_dist: float = n_layers_slab / n_layers

    Clean up core.surface comments and docstrings #3691 (comment)

  3. Currently there are three very similar methods/functions that need to clarify in docstring:
    a. get_slab of SlabGenerator: The actual method to generate Slab, should be a private method (as per the docstring, but have already been used externally).
    b. get_slabs of SlabGenerator: A wrapper method on top of get_slab to generate possible shifts and then call get_slab.
    c. generate_all_slabls: A wrapper function on top of get_slabs, which generate slabs up to a given Miller index.

Next up: Fix some Slab related issues

Don't think I could resolve them in one shot, but good to collect them:

Copy link

coderabbitai bot commented Apr 12, 2024

Important

Auto Review Skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.


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

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

@DanielYang59
Copy link
Contributor Author

I would need discussion/confirmation/suggestion on these proposed changes, as they more or less seem to introduce functional changes. @janosh @shyuep

Also trying to ping the original author (I guess) @CifLord in case you have any comment 😃

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.

thanks @DanielYang59 and sorry for the slow reply. this all looks good. just had 1 question

@DanielYang59

This comment was marked as resolved.

@janosh
Copy link
Member

janosh commented Apr 30, 2024

Sorry it seems I wasn't making my intention clear enough, as I haven't actually started making changes yet. I proposed #3748 (comment) but I want to hear about your comments/suggestions before getting my hands on it, because most of them seem more or less breaking (breaking in a way to fix issues, not for enhancement).

that i understood. but i was tentatively advising against all but one of the breaking changes you suggested (the one you clearly identified as a bug). how much does that leave to change here?

@DanielYang59

This comment was marked as resolved.

pymatgen/core/surface.py Outdated Show resolved Hide resolved
@DanielYang59 DanielYang59 changed the title [Breaking] Revert shift direction in private method SlabGenerator.get_slab [Deprecation] Rename misleading arg name shift to termination for SlabGenerator.get_slab May 13, 2024
@DanielYang59 DanielYang59 changed the title [Deprecation] Rename misleading arg name shift to termination for SlabGenerator.get_slab Rename misleading arg name shift to termination for SlabGenerator.get_slab May 13, 2024
@DanielYang59 DanielYang59 marked this pull request as ready for review May 13, 2024 04:29
@DanielYang59

This comment was marked as outdated.

@DanielYang59 DanielYang59 changed the title Rename misleading arg name shift to termination for SlabGenerator.get_slab Deprecate misleading arg name shift for termination for SlabGenerator.get_slab May 17, 2024
@DanielYang59

This comment was marked as outdated.

@DanielYang59 DanielYang59 marked this pull request as draft May 18, 2024 02:09
@DanielYang59 DanielYang59 changed the title Deprecate misleading arg name shift for termination for SlabGenerator.get_slab Clarify misleading arg name shift for SlabGenerator.get_slab May 18, 2024
@DanielYang59 DanielYang59 marked this pull request as ready for review May 18, 2024 02:30
@DanielYang59
Copy link
Contributor Author

@janosh I decide to minimize (potentially) breaking changes and just clarify in the docstring.

@DanielYang59 DanielYang59 changed the title Clarify misleading arg name shift for SlabGenerator.get_slab Clarify argument shift for SlabGenerator.get_slab May 27, 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.

None yet

2 participants