-
Notifications
You must be signed in to change notification settings - Fork 663
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
Add writing u.trajectory.ts.data['molecule_tag'] as molecule_tag atom attribute to LAMMPS datafile #4114
Add writing u.trajectory.ts.data['molecule_tag'] as molecule_tag atom attribute to LAMMPS datafile #4114
Conversation
Linter Bot Results:Hi @mglagolev! Thanks for making this PR. We linted your code and found the following: Some issues were found with the formatting of your code.
Please have a look at the Please note: The |
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## develop #4114 +/- ##
===========================================
+ Coverage 93.59% 93.61% +0.02%
===========================================
Files 192 192
Lines 25134 25140 +6
Branches 4056 4056
===========================================
+ Hits 23524 23536 +12
+ Misses 1092 1088 -4
+ Partials 518 516 -2
☔ View full report in Codecov by Sentry. |
@hmacdope do you have expertise with LAMMPS to review this PR, or can you suggest someone else? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few small comments. I'll leave it to LAMMPS experts for a more in-depth review.
LAMMPSdata, | ||
LAMMPSdata_mini, | ||
LAMMPScnt, | ||
LAMMPShyd, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the code coverage comment above, it seems that none of those covers the case where there is no charge. Can we add one such case?
Co-authored-by: Rocco Meli <[email protected]>
@hmacdope Shall I improve something in this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mglagolev sorry for the delay, looks good, if you could address @RMeli's last comments about f-strings and a test case coverage for a case with no carge we should be good to go
Thank you @hmacdope and, once again, @RMeli! Here's a simple script to test this behavior:
All the attributes are set just to get the simplest datafile written without an error. |
Maybe @fenilsuchak can help? |
Sounds odd, I'll have to double check this behaviour. Thanks for checking and reporting. A possible workaround/alternative to get this PR done is to use In [1]: from MDAnalysisTests.datafiles import LAMMPSdata
In [2]: import MDAnalysis as mda
In [3]: u = mda.Universe(LAMMPSdata)
In [4]: u.atoms.charges
Out[4]: array([0., 0., 0., ..., 0., 0., 0.])
In [5]: u.del_TopologyAttr('charges')
In [6]: u.atoms.charges
---------------------------------------------------------------------------
NoDataError Traceback (most recent call last)
Cell In[6], line 1
----> 1 u.atoms.charges
File ~/Documents/git/mdanalysis/package/MDAnalysis/core/groups.py:2539, in AtomGroup.__getattr__(self, attr)
2537 elif attr == 'positions':
2538 raise NoDataError('This Universe has no coordinates')
-> 2539 return super(AtomGroup, self).__getattr__(attr)
File ~/Documents/git/mdanalysis/package/MDAnalysis/core/groups.py:614, in GroupBase.__getattr__(self, attr)
612 else:
613 err = 'This Universe does not contain {singular} information'
--> 614 raise NoDataError(err.format(singular=cls.singular))
615 else:
616 return super(GroupBase, self).__getattr__(attr)
NoDataError: This Universe does not contain charge information |
@RMeli, thanks for the idea! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mglagolev. Small nitpick (you can safely ignore it), otherwise LGTM.
Co-authored-by: Rocco Meli <[email protected]>
@IAlibay, sorry for bothering, is anything else required to merge this? |
Thanks for the contribution @mglagolev, and sorry if it took quite long to get it merged. |
Great! Thanks to helpful people here for making this small contribution possible:-) By the way, @RMeli did you reproduce the issue with compressed filenames? |
@mglagolev unfortunately no, I forgot. Thanks for reminding me! If you don't mind, could you please open an issue with your observation? Otherwise I'll open one. |
Fixes #3548
Being able to write the molecule id in a LAMMPS data file is crucial for using the MDAnalysis as a generator of LAMMPS initial configurations, for example, in machine learning workflows.
Currently, the molecule_tag field reads as atoms.resids, but when writing a data file, zero values are hardcoded.
Following the discussion in #3548, I've implemented writing ts.data['molecule_tag'] into this field.
Changes made in this Pull Request:
Added data as input parameter for DATAWriter._write_atoms
If data['molecule_tag'] is present, _write_atoms will write its values as the molecule_tag attribute of the Atoms section of the data file. Otherwise, it will fill molecule_tag with zeroes, consistent with the previous behavior.
Added special read-write-read function to the tests, which sets data['molecule_tag'] values from atoms.resids after reading the data file. Added the class to check the resids after using the aforementioned function.
PR Checklist
📚 Documentation preview 📚: https://readthedocs-preview--4114.org.readthedocs.build/en/4114/