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.
151 actually read in the data, and return the particle data as a
152 VTUParticleSet object.
154 read_single_vtu_file(self, vtufilename: str):
155 Read in particle data from a single .vtu file, and return the filled
156 out VTUParticleSet object containing the particle data.
161 vtufile: Union[str,
None] =
None,
162 snapshot_time: Union[float,
None] =
None,
163 pvdfile: Union[str,
None] =
None,
164 verbose: bool =
False,
167 At initialization, either the ``vtufile`` argument needs to
168 be provided, or both the ``snapshot_time`` and ``pvdfile``
169 arguments in order to specify what data to read.
171 If ``vtufile`` is given, the ParticleVTUReader will read in a
174 If ``snapshot_time`` and ``pvdfile`` arguments are given, the
175 ParticleVTUReader will read in all .vtu files corresponding to the
176 snapshot at the provided ``snapshot_time``.
182 path to .vtu file to be read in
185 time of the snapshot to extract
188 path to .pvd file to be read in
191 how talkative the reading process should be
206 if (snapshot_time
is None)
or (pvdfile
is None):
208 "You need to specify either the `vtufile` argument, or "
209 +
"*both* the `snapshot_time` and `pvdfile` arguments so I "
210 +
"know what to read."
213 self.
_mode =
"multiple"
215 print(
"Setting mode to 'multiple'")
217 self.
_mode =
"single"
219 print(
"Setting mode to 'single'")
225 Actually read in the data.
230 particleData: VTUParticleSet
231 object containing all read-in particle data as attributes.
234 if self.
_mode ==
"single":
238 elif self.
_mode ==
"multiple":
249 partdata.merge(new_data)
254 raise ValueError(
"Unknown mode? How did we get here?")
258 Return the time/timestep information of the read snapshot.
259 This is only available if you read in the .pvd file, not
260 the .vtu file. If the .vtu file has been read, this method
261 returns `None`. Otherwise, it returns the time of the
262 snapshot, which may vary (slightly) from the snapshot time
263 you specified with when loading the data.
273 Read in and return all .vtu file names from the .pvd file
274 provided by pvdfilename at initialisation.
280 list of .vtu filenames in the .pvd file, with their
287No .pvd file has been specified.
288You need to provide a .pvd file name during initialisation of
289the ParticleVTUReader() object.
294 pvdfile = vtuIO.PVDIO(self.
_pvdfile, interpolation_backend=
"vtk")
295 allvtufiles = pvdfile.vtufilenames
301 The .pvd file will provide relative path names to its
302 own file path for the .vtu files.
303 This function returns the full path of the .vtu file.
308No .pvd file has been specified.
309You need to provide a .pvd file name during initialisation of
310the ParticleVTUReader() object.
313 pathprefix = os.path.dirname(self.
_pvdfile)
314 fullpath = os.path.join(pathprefix, vtufile)
319 Read in data from the .pvd file provided by pvdfilename.
325 filename to read in from
328 snapshot time to read in
335 list of .vtu filenames to read from
338 if not os.path.exists(pvdfilename):
339 raise FileNotFoundError(
340 errno.ENOENT, os.strerror(errno.ENOENT), pvdfilename
349 pvdfile = vtuIO.PVDIO(pvdfilename, interpolation_backend=
"vtk")
350 timesteps = pvdfile.timesteps
352 print(
"Available snapshot time steps:", np.unique(timesteps))
353 allvtufiles = pvdfile.vtufilenames
356 timediff = np.abs(timesteps - time)
357 snap_indexes = timediff == timediff.min()
360 if not snap_indexes.any():
362 Couldn't find any snapshots?
363 Looking for snashots with time={self._snapshot_time}
364 pvd file provides following times: {timesteps}
365 pvd file provides following file names: {allvtufiles}
367 raise ValueError(errmsg)
372 if timediff.min() > 1.0e-5:
379 if self.
_verbose or not close_enough:
380 print(f
"WARNING: You requested snapshot at time={self._snapshot_time}")
382 "WARNING: Closest snapshot time I found is:",
383 timesteps[snap_indexes][0],
384 "; reading that in now",
387 vtufiles = np.array(allvtufiles)[snap_indexes]
388 vtufiles = vtufiles.tolist()
398 Read in particle data from a single .vtu file, and return the filled
399 out VTUParticleSet object containing the particle data.
405 .vtu filename to read in from.
411 particleData: VTUParticleSet
412 object containing all read-in particle data as attributes.
415 if not os.path.exists(vtufilename):
416 raise FileNotFoundError(
417 errno.ENOENT, os.strerror(errno.ENOENT), vtufilename
426 vtufile = vtuIO.VTUIO(vtufilename, interpolation_backend=
"vtk")
427 point_field_names = vtufile.get_point_field_names()
429 print(
"-- Reading from", vtufilename)
431 print(
"File", vtufilename,
"has particle fields:", point_field_names)
435 for attr
in point_field_names:
436 part_fields = vtufile.get_point_field(attr)
437 setattr(data, data._attribute_translator[attr], part_fields)
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_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.