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

Store unit system in H5MD file #3751

Merged
merged 11 commits into from
Jun 12, 2020
Merged

Conversation

jkrajniak
Copy link
Contributor

Add option to define and store the unit system used in the simulation according to the H5MD module units defined here: https://nongnu.org/h5md/modules/units.html#unit-string

Description of changes:

  • add new argument to the class h5md.H5md: unit_system
  • enable H5MD module units if the unit system is set

@jngrad jngrad self-requested a review June 8, 2020 16:30
jkrajniak and others added 4 commits June 8, 2020 18:33
@codecov
Copy link

codecov bot commented Jun 8, 2020

Codecov Report

Merging #3751 into python will decrease coverage by 0%.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           python   #3751   +/-   ##
======================================
- Coverage      89%     89%   -1%     
======================================
  Files         552     552           
  Lines       24532   24560   +28     
======================================
+ Hits        21861   21880   +19     
- Misses       2671    2680    +9     
Impacted Files Coverage Δ
src/core/io/writer/h5md_core.cpp 89% <100%> (+1%) ⬆️
src/core/io/writer/h5md_core.hpp 72% <100%> (+18%) ⬆️
src/script_interface/h5md/h5md.hpp 100% <100%> (ø)
src/core/polymer.cpp 92% <0%> (-7%) ⬇️
src/core/electrostatics_magnetostatics/p3m.cpp 86% <0%> (-1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eb54eda...a2d42e1. Read the comment docs.

@jngrad
Copy link
Member

jngrad commented Jun 8, 2020

Several CI images are missing the dataclasses python module. I'll have a look this week.

junghans
junghans previously approved these changes Jun 8, 2020
Copy link
Member

@junghans junghans left a comment

Choose a reason for hiding this comment

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

Great job.

jngrad
jngrad previously approved these changes Jun 10, 2020
Copy link
Member

@jngrad jngrad left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks!

@jngrad jngrad added this to the Espresso 4.2 milestone Jun 10, 2020
@jngrad
Copy link
Member

jngrad commented Jun 10, 2020

@KaiSzuttor would you like to sign off on these changes? This PR makes use of dataclasses, which offer the same safety checks as a NamedTuple while providing default values. This is part of the Python Standard Library in python3.7. There is a backport for python3.6, so we can update our Ubuntu 18.04 CI images accordingly (jngrad/espresso-docker@f674421b3), as well as the macOS image.

If we don't want to add this dataclasses requirement, we could rewrite the UnitSystem class as a regular class, without the benefit of argument name validation. I wouldn't mind adding this requirement: since python3.6 will be dropped from NEP 29 on June 23 2020, people still using python3.6 will have more to worry about than just installing a backported package via pip.

Copy link
Member

@KaiSzuttor KaiSzuttor left a comment

Choose a reason for hiding this comment

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

In general I think we should not write our own unit system code. So what may make sense in the future is to use Pint and derive the strings that need to be written to the file automatically.
Looks good to me for now. Thanks for the contribution!

@@ -18,7 +18,7 @@ repos:
always_run: false
files: '.*\.(py|pyx|pxd)'
exclude: '\.pylintrc|.*.\.py\.in'
args: ["--ignore=E266,W291,W293", "--in-place", "--aggressive"]
args: ["--ignore=E266,E701,W291,W293", "--in-place", "--aggressive"]
Copy link
Member

Choose a reason for hiding this comment

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

why is this needed?

Copy link
Member

Choose a reason for hiding this comment

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

E701 causes dataclasses members to be formatted on a new line:

-        force: str = dataclasses.field(
-            init=False, default='')
+        force:
+            str = dataclasses.field(
+                init=False, default='')

This change causes a syntax error:

1559   File "/builds/espressomd/espresso/build/src/python/espressomd/io/writer/h5md.py", line 36
1560     force:
1561          ^
1562 SyntaxError: invalid syntax

E701 cannot be disabled on a per-line basis. I tried using line continuation symbols \ to work around it, without success. I think we should permanently disable it, even if we remove dataclasses, because it might cause us trouble again in the future, and troubleshooting this error took me a while.

id (implies serial write).
id (implies serial write)

- ``unit_system``: optionally, physical units for time, mass, length and
Copy link
Member

Choose a reason for hiding this comment

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

what about other SI base units that are defined in the H5MD documentation?

Copy link
Member

Choose a reason for hiding this comment

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

The other SI units (current, temperature, moles, candela) do not seem to apply for espresso particles. The per-particle temperature is more of a trick for tailored Langevin dynamics. Same thing for derived units. Torque could have been relevant, but isn't defined.

@KaiSzuttor
Copy link
Member

Concerning the dataclasses, I think it's not worth the dependency overall because the respective constructor would not be a lot of additional code and the other provided functions like __repr__ and the comparison functions are not needed.

This commit can be reverted once ESPResSo requires python 3.7 as
the minimal version. In 3.7, dataclasses is part of the Python
Standard Library.

def __post_init__(self):

def __init__(self, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

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

you could still use type annotations if you wanted to...

Copy link
Member

Choose a reason for hiding this comment

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

there's no need to re-implement dataclasses :) providing a different type already throws a meaningful error message: RuntimeError: Provided argument of type int is not convertible to std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >

@jngrad jngrad added the automerge Merge with kodiak label Jun 12, 2020
@kodiakhq kodiakhq bot merged commit cc9c050 into espressomd:python Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants