2This file contains the ParticleVTUReader class, which hosts
3methods to extract particle data from .vtu or .pvd files
10from typing
import Union
14except ModuleNotFoundError:
15 raise ModuleNotFoundError(
16 "You need the 'VTUinterface' package to use the ParticleVTUReader"
22 Clean up a string intended to be a particle attribute
23 from illegal characters.
29 cleaned attribute string.
32 cleaned_attr = attr.strip()
36 illegal_character_replacements = {
41 for key
in illegal_character_replacements:
42 cleaned_attr = cleaned_attr.replace(key, illegal_character_replacements[key])
49 Container for particle data. All particle fields shall
50 have the same name as specified in the .vtu files, and
51 be accessible as an attribute of the instantiated object.
53 The individual particle values are instances of numpy arrays.
58 get_attribute_list(self):
59 return list of all available particle fields
61 show_attribute_list(self):
62 prints out all available particle fields
65 merge another VTUParticleSet into this one.
72 particle_attributes : list of strings
73 contains names of all particle fields
81 if len(particle_attributes) == 0:
83 "Got empty particle attributes list. Can't work with that."
86 cleaned_attributes = []
87 for att
in particle_attributes:
88 if not isinstance(att, str):
89 raise ValueError(
"Attribute '", att,
"' is not a string?")
91 setattr(self, cleaned_attr,
None)
95 cleaned_attributes.append(cleaned_attr)
103 Return the list of available attributes.
109 Print the list of available attributes.
116 Merge two VTUParticleSet objects into one (into self).
120 raise ValueError(
"Unequal number of attributes in datasets, can't merge")
123 this_arr = getattr(self, attr)
124 that_arr = getattr(other, attr)
125 newattr = np.concatenate((this_arr, that_arr))
126 setattr(self, attr, newattr)
131 return "Particle Data Container"
139 Reads in particle fields from a single .vtu file, or all .vtu
140 at a given snapshot time specified in the provided .pvd file.
146 get_all_vtufiles(self):
147 Read in and return all .vtu file names from the .pvd file
148 provided by pvdfilename at initialisation.
150 get_snapshot_time(self):
151 Returns the simulation time of the read-in snapshot
153 get_snapshot_times(pvdfile):
154 Returns all simulation times stored in pvd file
157 actually read in the data, and return the particle data as a
158 VTUParticleSet object.
160 read_single_vtu_file(self, vtufilename: str):
161 Read in particle data from a single .vtu file, and return the filled
162 out VTUParticleSet object containing the particle data.
167 vtufile: Union[str,
None] =
None,
168 snapshot_time: Union[float,
None] =
None,
169 pvdfile: Union[str,
None] =
None,
170 verbose: bool =
False,
173 At initialization, either the ``vtufile`` argument needs to
174 be provided, or both the ``snapshot_time`` and ``pvdfile``
175 arguments in order to specify what data to read.
177 If ``vtufile`` is given, the ParticleVTUReader will read in a
180 If ``snapshot_time`` and ``pvdfile`` arguments are given, the
181 ParticleVTUReader will read in all .vtu files corresponding to the
182 snapshot at the provided ``snapshot_time``.
188 path to .vtu file to be read in
191 time of the snapshot to extract
194 path to .pvd file to be read in
197 how talkative the reading process should be
212 if (snapshot_time
is None)
or (pvdfile
is None):
214 "You need to specify either the `vtufile` argument, or "
215 +
"*both* the `snapshot_time` and `pvdfile` arguments so I "
216 +
"know what to read."
219 self.
_mode =
"multiple"
221 print(
"Setting mode to 'multiple'")
223 self.
_mode =
"single"
225 print(
"Setting mode to 'single'")
231 Actually read in the data.
236 particleData: VTUParticleSet
237 object containing all read-in particle data as attributes.
240 if self.
_mode ==
"single":
244 elif self.
_mode ==
"multiple":
255 partdata.merge(new_data)
260 raise ValueError(
"Unknown mode? How did we get here?")
264 Return the time/timestep information of the read snapshot.
265 This is only available if you read in the .pvd file, not
266 the .vtu file. If the .vtu file has been read, this method
267 returns `None`. Otherwise, it returns the time of the
268 snapshot, which may vary (slightly) from the snapshot time
269 you specified with when loading the data.
279 Read in and return all .vtu file names from the .pvd file
280 provided by pvdfilename at initialisation.
286 list of .vtu filenames in the .pvd file, with their
293No .pvd file has been specified.
294You need to provide a .pvd file name during initialisation of
295the ParticleVTUReader() object.
300 pvdfile = vtuIO.PVDIO(self.
_pvdfile, interpolation_backend=
"vtk")
301 allvtufiles = pvdfile.vtufilenames
307 The .pvd file will provide relative path names to its
308 own file path for the .vtu files.
309 This function returns the full path of the .vtu file.
314No .pvd file has been specified.
315You need to provide a .pvd file name during initialisation of
316the ParticleVTUReader() object.
319 pathprefix = os.path.dirname(self.
_pvdfile)
320 fullpath = os.path.join(pathprefix, vtufile)
325 Read in data from the .pvd file provided by pvdfilename.
331 filename to read in from
334 snapshot time to read in
341 list of .vtu filenames to read from
344 if not os.path.exists(pvdfilename):
345 raise FileNotFoundError(
346 errno.ENOENT, os.strerror(errno.ENOENT), pvdfilename
355 pvdfile = vtuIO.PVDIO(pvdfilename, interpolation_backend=
"vtk")
356 timesteps = pvdfile.timesteps
358 print(
"Available snapshot time steps:", np.unique(timesteps))
359 allvtufiles = pvdfile.vtufilenames
362 timediff = np.abs(timesteps - time)
363 snap_indexes = timediff == timediff.min()
366 if not snap_indexes.any():
368 Couldn't find any snapshots?
369 Looking for snashots with time={self._snapshot_time}
370 pvd file provides following times: {timesteps}
371 pvd file provides following file names: {allvtufiles}
373 raise ValueError(errmsg)
378 if timediff.min() > 1.0e-5:
385 if self.
_verbose or not close_enough:
386 print(f
"WARNING: You requested snapshot at time={self._snapshot_time}")
388 "WARNING: Closest snapshot time I found is:",
389 timesteps[snap_indexes][0],
390 "; reading that in now",
393 vtufiles = np.array(allvtufiles)[snap_indexes]
394 vtufiles = vtufiles.tolist()
404 Read in particle data from a single .vtu file, and return the filled
405 out VTUParticleSet object containing the particle data.
411 .vtu filename to read in from.
417 particleData: VTUParticleSet
418 object containing all read-in particle data as attributes.
421 if not os.path.exists(vtufilename):
422 raise FileNotFoundError(
423 errno.ENOENT, os.strerror(errno.ENOENT), vtufilename
432 vtufile = vtuIO.VTUIO(vtufilename, interpolation_backend=
"vtk")
433 point_field_names = vtufile.get_point_field_names()
435 print(
"-- Reading from", vtufilename)
437 print(
"File", vtufilename,
"has particle fields:", point_field_names)
441 for attr
in point_field_names:
442 part_fields = vtufile.get_point_field(attr)
443 setattr(data, data._attribute_translator[attr], part_fields)
459 Read in available snapshot times from the .pvd file provided by
466 filename to read in from
472 np.array of available snapshot times
475 if not os.path.exists(pvdfilename):
476 raise FileNotFoundError(
477 errno.ENOENT, os.strerror(errno.ENOENT), pvdfilename
486 pvdfile = vtuIO.PVDIO(pvdfilename, interpolation_backend=
"vtk")
487 timesteps = pvdfile.timesteps
Reads in particle fields from a single .vtu file, or all .vtu at a given snapshot time specified in t...
get_snapshot_time(self)
Return the time/timestep information of the read snapshot.
_get_full_vtufile_path(self, str vtufile)
The .pvd file will provide relative path names to its own file path for the .vtu files.
read_single_vtu_file(self, str vtufilename)
Read in particle data from a single .vtu file, and return the filled out VTUParticleSet object contai...
load(self)
Actually read in the data.
__init__(self, Union[str, None] vtufile=None, Union[float, None] snapshot_time=None, Union[str, None] pvdfile=None, bool verbose=False)
At initialization, either the vtufile argument needs to be provided, or both the snapshot_time and pv...
get_all_vtufiles(self)
Read in and return all .vtu file names from the .pvd file provided by pvdfilename at initialisation.
get_snapshot_times(str pvdfilename)
Read in available snapshot times from the .pvd file provided by pvdfilename.
_get_snapshot_vtu_files_from_pvd(self, str pvdfilename, float time)
Read in data from the .pvd file provided by pvdfilename.
Container for particle data.
__init__(self, list particle_attributes)
merge(self, other)
Merge two VTUParticleSet objects into one (into self).
show_attribute_list(self)
Print the list of available attributes.
get_attribute_list(self)
Return the list of available attributes.
_clean_attribute_string(str attr)
Clean up a string intended to be a particle attribute from illegal characters.