BradyAJohnston / MolecularNodes

Toolbox for molecular animations in Blender, powered by Geometry Nodes.
https://bradyajohnston.github.io/MolecularNodes/
MIT License
839 stars 82 forks source link

Support for LAMMPS Files and Trajectories #144

Open Compizfox opened 1 year ago

Compizfox commented 1 year ago

Describe the bug When trying to load a LAMMPS topology (data file) and trajectory, MDAnalysis fails with the errors This Universe does not contain element information and This Universe does not contain name information.

FWIW, the same files open just fine in e.g. Ovito.

To Reproduce

  1. Go to 'MD Trajectory' tab
  2. Fill in the paths to the topology and trajectory files
  3. Click on "Load"
  4. See error

Expected behavior The topology and trajectory to be loaded.

Error Codes

Python: Traceback (most recent call last):
  File "/home/lars/.config/blender/3.4/scripts/addons/MolecularNodes/md.py", line 39, in load_trajectory
    elements = univ.atoms.elements.tolist()
  File "/home/lars/.local/lib/python3.10/site-packages/MDAnalysis/core/groups.py", line 2537, in __getattr__
    return super(AtomGroup, self).__getattr__(attr)
  File "/home/lars/.local/lib/python3.10/site-packages/MDAnalysis/core/groups.py", line 609, in __getattr__
    raise NoDataError(err.format(singular=cls.singular))
MDAnalysis.exceptions.NoDataError: This Universe does not contain element information

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lars/.config/blender/3.4/scripts/addons/MolecularNodes/ui.py", line 99, in execute
    mol_object, coll_frames = md.load_trajectory(
  File "/home/lars/.config/blender/3.4/scripts/addons/MolecularNodes/md.py", line 41, in load_trajectory
    elements = [mda.topology.guessers.guess_atom_element(x) for x in univ.atoms.names]
  File "/home/lars/.local/lib/python3.10/site-packages/MDAnalysis/core/groups.py", line 2537, in __getattr__
    return super(AtomGroup, self).__getattr__(attr)
  File "/home/lars/.local/lib/python3.10/site-packages/MDAnalysis/core/groups.py", line 609, in __getattr__
    raise NoDataError(err.format(singular=cls.singular))
MDAnalysis.exceptions.NoDataError: This Universe does not contain name information

Desktop:

BradyAJohnston commented 1 year ago

Thanks for the bug report! I have an idea of what should be the solution for this but I don't personally have access to some LAMMPS files to test with. Do you have a link to some publically available demo files that I could debug with?

Compizfox commented 1 year ago

Thanks!

Sure, I'm not aware of any official demo files but I can send you some:

Archive.zip

BradyAJohnston commented 1 year ago

Are you able to share what you use as topology / trajectory? I haven't worked with LAMMPS before, and I can't seem to be able to open the files that you shared without MDAnalysis saying it doesn't recognise the formats. (after unzipping etc)

Compizfox commented 1 year ago

data.equi is the topology (data) file, and dump.atom.gz is the (gzipped) trajectory (dump) file.

I did have to reverse the filename of data.equi to equi.data for MDAnalysis was able to recognise it. Oh, and I also encountered the issue that MDAnalysis did not seem to want to open the data file with the pair, bond, and angle coefficients in there (lines 21-42 in the file I sent you), even though that is perfectly valid (so that's a MDAnalysis bug I'd say). After I removed those sections, MDAnalysis was able to open the file.

Sorry, I should have fixed (or at least mentioned) that in the first place...

BradyAJohnston commented 1 year ago

I am able to get it importing by additionally renaming atoms.dump to atoms.lammpsdump which MDAnlaysis now recognises.

In terms of changes to get it importing into MolecularNodes, making the attempt to get element information optional works.

image

None of the attributes that I currently have set up are imported however, just positions.

Would you expect such a small amount of information? If not, would you be able to provide examples of what information you would expect to be able to extract from the LAMMPS files and what code to use in MDAnalysis?

Compizfox commented 1 year ago

That looks promising!

Apart from positions, the topology file should also contain bond information. And along with the position data, every particle also has a molecule-id, atom-type, and charge.

For comparison, this is how the same system can look in Ovito with bonds drawn and particles coloured based on atom type:

image

BradyAJohnston commented 1 year ago

Okay I've got atom types and bonds importing. You'll have to explain how to extract the molecule-id and charge from the MDAnalysis universe because in my testing I wasn't able to figure it out.

Should be simple enough to implement into the import function.

Are you able to explain the atom types more? Will this numbering be consistent across files? I'm assuming these correspond to some elements somehow?

image

Compizfox commented 1 year ago

Great, thanks!

You'll have to explain how to extract the molecule-id and charge from the MDAnalysis universe because in my testing I wasn't able to figure it out.

I've not used MDAnalysis before, so I'll have to look into that.

Are you able to explain the atom types more? Will this numbering be consistent across files? I'm assuming these correspond to some elements somehow?

It's just an arbitrary number number for the type of particle. These are coarse-grained simulations, not atomistic ones, so they don't directly correspond to elements, and are only consistent across files if they are of simulations using the same coarse-grained model.

Compizfox commented 1 year ago

You'll have to explain how to extract the molecule-id and charge from the MDAnalysis universe because in my testing I wasn't able to figure it out.

I was able to get these out from the AtomGroup attributes:

>>> import MDAnalysis as mda
>>> psf = mda.Universe('/home/lars/Downloads/2022-06-03/equi.data')
>>> psf.atoms.charges
array([0., 0., 0., ..., 0., 0., 0.])
>>> psf.atoms.bonds
<TopologyGroup containing 20000 bonds>
>>> psf.atoms.angles
<TopologyGroup containing 29600 angles>
>>> psf.atoms.resids
array([  1,   1,   1, ..., 100, 100, 100])
BradyAJohnston commented 1 year ago

Okay that should be able to be integrated. Would there be a trajectory associated with these files? Multiple timepoints? Do you have an example file if so?

Compizfox commented 1 year ago

Yes, the trajectory is in the .lammpsdump file. The one I sent you only contains two timesteps, but it's a trajectory nonetheless ;)

BradyAJohnston commented 1 year ago

I have just released MolecularNodes 2.3.0, which now enables the parsing of the topology and trajectory files into Blender. You will still have to delete the lines of code that you mentioned above, but alas that is an MDAanlysis issue.

Currently the only imported properties are the res_id and the atom_types, along with the XYZ positions of the atoms which change throughtout the trajectory. The bonds are also imported.

This creates a mesh which has a vertex for each point, and an edge for bond.

image

So far I haven't created any nodes for handling this data, as it is all non-standard compared to importing atoms etc. You will have to create your own node trees to handle the data for now. I have created an example node tree below:

image

Which does at least get you started with spheres and bonds:

image

The animation should also playback as well, moving the atoms and bonds around.

If you can provide a list of things that you would like to do with the data, I can provide more purpose-built LAMMPS nodes, for dealing with this particular kind of data.

Compizfox commented 1 year ago

I have just released MolecularNodes 2.3.0, which now enables the parsing of the topology and trajectory files into Blender. You will still have to delete the lines of code that you mentioned above, but alas that is an MDAanlysis issue.

Amazing, thanks a lot!

Currently the only imported properties are the res_id and the atom_types, along with the XYZ positions of the atoms which change throughtout the trajectory. The bonds are also imported.

This creates a mesh which has a vertex for each point, and an edge for bond.

So far I haven't created any nodes for handling this data, as it is all non-standard compared to importing atoms etc. You will have to create your own node trees to handle the data for now. I have created an example node tree below:

If you can provide a list of things that you would like to do with the data, I can provide more purpose-built LAMMPS nodes, for dealing with this particular kind of data.

I didn't realise that the workflow for LAMMPS data (once it's imported) would be different from what already exists in Molecular Nodes. Or is it because MolecularNodes is designed with the assumption of operating on atomistic simulations? (LAMMPS is not exclusively for coarse-grained simulations, it can do atomistic ones as well)

In any case, I have zero experience with Blender (or Molecular Nodes) so far, so I'll just give it a try and see if I can get it working.

BradyAJohnston commented 1 year ago

Just that most of the nodes for displaying atoms & bonds etc assume the existence of atomic_number, which at least this LAMMPS data doesn't have. I should be able to create some general purpose nodes that don't require it, and can work from atom_type instead.

Compizfox commented 1 year ago

Just that most of the nodes for displaying atoms & bonds etc assume the existence of atomic_number, which at least this LAMMPS data doesn't have.

Ah, I see.

I should be able to create some general purpose nodes that don't require it, and can work from atom_type instead.

That would be perfect, even for atomistic simulations in LAMMPS, since those use the atom_type attribute as well if I'm not mistaken.