-
Hi all, I am trying to get forces and energies for several subsets of particles in my simulation. The concrete code is somewhat long, so I will delineate how is done in a general way, which hopefully will be useful for others as well. I am using hoomd 4.1.0 (and somewhere in the code gsd 3.1.1, but that should not matter) I create a list of filters with hoomd.filter.Tags list_filters=[hoomd.filter.Tags([0,1,2]), hoomd.filter.Tags([3,4,5])] then I create a logger logger_local = hoomd.logging.Logger() and finally, I create a new gsd file for each filter list_writer=2*[0] This is it. I would expect that running the simulation this way, my_file0 and my_file1 would contain the energies and forces for particles [0,1,2] and [3,4,5] respectively. What I find instead is that both files contain the energies and forces for all the particles in the system, as if I had set filter=hoomd.filter.All() in hoomd.write.GSD. Is there something incorrect in the logic of this setup? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 8 replies
-
The
|
Beta Was this translation helpful? Give feedback.
-
Josh, I include two files: It is mostly boilerplate code except for a few lines where I provided a lot of comments inside the file. Still, it is significant amount of code to place inside this message, so I provide a link instead. (It is not allowed to attach *.py files). The first file runs a simple lj simulation: if at the top of the file you set This is the file I used to analyze the files, does not need to be modified whether you use method_write=True or False Does the code with method_write=True works as you expected or it is a bug? If it is a bug, what would you expect that the code described in method_write=True should give? |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot! I am sorry this last question was not a strict hoomd question, this is the first time in a very long time that I used lambda functions... It would have taken me a lot of hours to find this one, so thanks for saving the day. |
Beta Was this translation helpful? Give feedback.
-
The import copy
class LogSubset:
def __init__(self, lj, tags):
self.lj = lj
self.tags = copy.copy(tags)
@property
def energies(self):
return self.lj.energies[self.tags]
log_subset = LogSubset(lj, [0, 1, 2])
logger[('energies')] = (log_subset, 'energies', 'sequence') |
Beta Was this translation helpful? Give feedback.
-
others may find this useful, this snippet implements the discussion above using lamda functions in a way that overcome the problem discussed above
|
Beta Was this translation helpful? Give feedback.
The
filter
attribute ofGSD
applies to theparticles
section of the GSD file.GSD
writes alllogger
provided quantities exactly as they are provided. To log a subset of particle energies, provide the expected array to the logger: