Skip to content

fix(utils): correctly round cube root in auto_grid_detection - #183

Open
Davis Rens (davrens101) wants to merge 1 commit into
microsoft:mainfrom
davrens101:fix-auto-grid-detection-cube-root
Open

Davis Rens (davrens101) wants to merge 1 commit into
microsoft:mainfrom
davrens101:fix-auto-grid-detection-cube-root

Conversation

@davrens101

Copy link
Copy Markdown

Summary

In mattersim.utils.supercell_utils.auto_grid_detection, calculating the number of supercell replicas along lattice directions was written as:

number_of_replicas = int(np.round(max_atoms / (len(atom))) ** (1 / 3))

Because the closing parenthesis of np.round was placed before ** (1 / 3), np.round was called on max_atoms / len(atom) instead of the cube root result.

In Python floating-point arithmetic, taking the cube root of perfect cubes often produces values slightly below integers (for example, 64 ** (1 / 3) == 3.9999999999999996 and 216 ** (1 / 3) == 5.999999999999999). Casting this with int(...) truncated the value, resulting in:

  • max_atoms = 64 (with 1-atom unit cell) yielding (3, 3, 3) (27 atoms) instead of (4, 4, 4) (64 atoms).
  • max_atoms = 216 yielding (5, 5, 5) (125 atoms) instead of (6, 6, 6) (216 atoms).

Similarly, in Case 2 (asymmetric lengths beyond tolerance), asymmetric_replica was computed with int(...) without rounding the cube root.

Changes

  • Fixed parentheses so np.round operates on the cube root: int(np.round((max_atoms / len(atom)) ** (1 / 3))).
  • Applied np.round to asymmetric_replica calculation as well.
  • Added unit tests in tests/utils/test_supercell_utils.py verifying detection on cubic systems and within ratio_tolerance.

Signed-off-by: Davis Rens <166565828+davrens101@users.noreply.github.com>
@davrens101

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree [company=]

@microsoft-github-policy-service

Copy link
Copy Markdown

Davis Rens (@davrens101) the command you issued was incorrect. Please try again.

Examples are:

@microsoft-github-policy-service agree

and

@microsoft-github-policy-service agree company="your company"

@davrens101

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch has not been deployed

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

1 participant