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

Set kpoints in from_str method as integer in auto Gamma and Monkhorst modes #3994

Merged
merged 23 commits into from
Aug 20, 2024

Conversation

DanielYang59
Copy link
Contributor

@DanielYang59 DanielYang59 commented Aug 12, 2024

Summary

It turns out typing Kpoint as a sequence of float is giving us quite some headache and confusion:

Kpoint = Union[Tuple3Floats, tuple[int,]]

It was originally done this way for compatibility with line-mode and explicit kpoint mode (when typing, float acts as Union[int, float] instead of just float). Is there any other cases where Kpoint could be float that I forgot to mention?

Inputs and comments are hugely appreciated.

@@ -1239,7 +1239,7 @@ def style(self, style) -> None:
def automatic(cls, subdivisions: int) -> Self:
"""
Constructor for a fully automatic Kpoint grid, with
gamma centered Monkhorst-Pack grids and the number of subdivisions
Copy link
Contributor Author

@DanielYang59 DanielYang59 Aug 12, 2024

Choose a reason for hiding this comment

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

It should be Gamma-centered only according to the VASP Wiki?

The following KPOINTS file generates a regular Γ \Gamma -centered k-point.

@DanielYang59 DanielYang59 marked this pull request as ready for review August 12, 2024 03:23
@DanielYang59 DanielYang59 marked this pull request as draft August 12, 2024 03:52
@DanielYang59

This comment was marked as duplicate.

DanielYang59 added a commit to DanielYang59/pymatgen that referenced this pull request Aug 12, 2024
@DanielYang59 DanielYang59 marked this pull request as ready for review August 14, 2024 08:02
shyuep pushed a commit that referenced this pull request Aug 14, 2024
…r parts (#3996)

* docstring tweaks

* fix typo

* reduce indentation level

* fix vasp case to VASP

* use walrus operator

* revert overlapping functional changes from #3994

* my bad, I get confused hopping between two PRs

* remove debug code from vasp.help

* fix `use-named-expression` with sourcery

* clean up Vasprun.as_dict

* simplify dict generation

* re-raise and update -> |=

* simplify logic conditions

* remove unused logger

* remove a lot of unused logger, wondering if they exist for a reason?

* remove some unused module_dir, they must have gone stranded

* CAPS LOCK ENGAGED: Go up! module level variables!
@DanielYang59
Copy link
Contributor Author

@janosh Could you please comment and review this PR?

Currently the type of Kpoint is a bit confusing (mainly around the existing of tuple[float, float, float] for line mode and explicit kpoint mode) and I tried to clarify as much as I could in this PR. Also is there any other cases where Kpoint could be a sequence of floats that I left out?

kpoints = Kpoints.monkhorst_automatic((2, 2, 2), [0, 0, 0])
assert kpoints.style == Kpoints.supported_modes.Monkhorst
assert kpoints.kpts == [(2, 2, 2)]
kpoints = Kpoints.automatic(100)
assert all(isinstance(kpt, int) for kpt in kpoints.kpts[0])
Copy link
Member

Choose a reason for hiding this comment

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

could be refactored to {*map(type, kpoints.kpts[0])} == {int} but maybe less readable

Copy link
Contributor Author

@DanielYang59 DanielYang59 Aug 20, 2024

Choose a reason for hiding this comment

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

Yes it looks less readable, also I guess type checking with type(var) == type_of_var feels a bit off?

I don't think kpt would be a subclass of int any time soon so it would be safe, but still not sure if we want to change this. I would let you decide :) Thanks for the comment.

Meanwhile {*map(lambda kpt: isinstance(kpt, int), kpts)} == {True} would work, but...well...It's way over-complicated ;)

if len(_kpt) != 3:
raise ValueError("Invalid Kpoint length.")
kpt: Tuple3Floats = cast(Tuple3Floats, tuple(_kpt))
kpt: Tuple3Ints = cast(Tuple3Ints, tuple(_kpt))
Copy link
Member

Choose a reason for hiding this comment

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

i think the : Tuple3Ints is redundant if you cast anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it's indeed redundant but I kept it for readability for humans (in case someone don't know about cast). Feel free to remove it if you like :)

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.

except for a minor nitpick, i think this is safe to merge

@janosh janosh merged commit 64d5890 into materialsproject:master Aug 20, 2024
33 checks passed
@janosh janosh added the types Type all the things label Aug 20, 2024
@janosh janosh changed the title Set kpoints in from_str method as integer in auto Gamma and Monkhorst modes Set kpoints in from_str method as integer in auto Gamma and Monkhorst modes Aug 20, 2024
@DanielYang59 DanielYang59 deleted the fix-kpts-as-int branch August 20, 2024 10:30
@DanielYang59
Copy link
Contributor Author

Thanks @janosh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types Type all the things
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kpoints Sampling Saved as Float When Writing Kpoints file
3 participants