|
| 1 | +""" |
| 2 | +VTK Exporter for parcel PySDM simulations. |
| 3 | +
|
| 4 | +This module defines `VTKExporterParcel`, a subclass of `PySDM.exporters.VTKExporter`, |
| 5 | +that writes simulation outputs to VTK format using `pyevtk`. It exports product |
| 6 | +profiles (e.g., relative humidity) as unstructured grids and particle attributes |
| 7 | +as point clouds, along with `.pvd` collection files for time-series visualization |
| 8 | +in ParaView. |
| 9 | +""" |
| 10 | + |
| 11 | +from pyevtk.hl import unstructuredGridToVTK, pointsToVTK |
| 12 | +from pyevtk.vtk import VtkHexahedron, VtkGroup |
| 13 | +import numpy as np |
| 14 | + |
| 15 | +from PySDM.exporters.vtk_exporter import VTKExporter |
| 16 | + |
| 17 | + |
| 18 | +# pylint: disable=too-many-instance-attributes |
| 19 | +class VTKExporterParcel(VTKExporter): |
| 20 | + """ |
| 21 | + Custom VTK exporter for parcel PySDM, exporting products as grids |
| 22 | + and attributes as point clouds for ParaView visualization. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, n_sd, output, mass_of_dry_air): |
| 26 | + super().__init__() |
| 27 | + self.output = output |
| 28 | + self.coords = { |
| 29 | + "x": np.random.random(n_sd), |
| 30 | + "y": np.random.random(n_sd), |
| 31 | + "z": np.random.random(n_sd), |
| 32 | + } |
| 33 | + self.half_diagonal = [] |
| 34 | + self.n_levels = len(self.output["products"]["z"]) |
| 35 | + |
| 36 | + volume = mass_of_dry_air / output["products"]["rhod"][0] |
| 37 | + delta_z = output["products"]["z"][1] - output["products"]["z"][0] |
| 38 | + for level in range(self.n_levels): |
| 39 | + if level > 0: |
| 40 | + prev_to_curr_density_ratio = ( |
| 41 | + output["products"]["rhod"][level - 1] |
| 42 | + / output["products"]["rhod"][level] |
| 43 | + ) |
| 44 | + volume *= prev_to_curr_density_ratio |
| 45 | + delta_z = ( |
| 46 | + output["products"]["z"][level] - output["products"]["z"][level - 1] |
| 47 | + ) |
| 48 | + area = volume / delta_z |
| 49 | + self.half_diagonal.append((2 * area) ** 0.5) |
| 50 | + |
| 51 | + def write_pvd(self): |
| 52 | + pvd_attributes = VtkGroup(self.attributes_file_path) |
| 53 | + for key, value in self.exported_times["attributes"].items(): |
| 54 | + pvd_attributes.addFile(key + ".vtu", sim_time=value) |
| 55 | + pvd_attributes.save() |
| 56 | + |
| 57 | + pvd_products = VtkGroup(self.products_file_path) |
| 58 | + for key, value in self.exported_times["products"].items(): |
| 59 | + pvd_products.addFile(key + ".vtu", sim_time=value) |
| 60 | + pvd_products.save() |
| 61 | + |
| 62 | + def export_products( |
| 63 | + self, step, simulation |
| 64 | + ): # pylint: disable=arguments-differ, too-many-locals |
| 65 | + path = self.products_file_path + "_num" + self.add_leading_zeros(step) |
| 66 | + self.exported_times["products"][path] = step * simulation.particulator.dt |
| 67 | + |
| 68 | + n_vertices = 4 * self.n_levels |
| 69 | + x_vertices = np.zeros(n_vertices) |
| 70 | + y_vertices = np.zeros(n_vertices) |
| 71 | + z_vertices = np.zeros(n_vertices) |
| 72 | + connectivity = [0, 1, 2, 3] |
| 73 | + cell_type = np.zeros(self.n_levels - 1) |
| 74 | + cell_type[:] = VtkHexahedron.tid |
| 75 | + |
| 76 | + for level in range(self.n_levels): |
| 77 | + half_diag = self.half_diagonal[level] |
| 78 | + current_z = self.output["products"]["z"][level] |
| 79 | + idx = level * 4 |
| 80 | + x_vertices[idx], y_vertices[idx], z_vertices[idx] = ( |
| 81 | + -half_diag, |
| 82 | + -half_diag, |
| 83 | + current_z, |
| 84 | + ) |
| 85 | + idx += 1 |
| 86 | + x_vertices[idx], y_vertices[idx], z_vertices[idx] = ( |
| 87 | + -half_diag, |
| 88 | + half_diag, |
| 89 | + current_z, |
| 90 | + ) |
| 91 | + idx += 1 |
| 92 | + x_vertices[idx], y_vertices[idx], z_vertices[idx] = ( |
| 93 | + half_diag, |
| 94 | + half_diag, |
| 95 | + current_z, |
| 96 | + ) |
| 97 | + idx += 1 |
| 98 | + x_vertices[idx], y_vertices[idx], z_vertices[idx] = ( |
| 99 | + half_diag, |
| 100 | + -half_diag, |
| 101 | + current_z, |
| 102 | + ) |
| 103 | + connectivity += [*range(4 * (level + 1), 4 * (level + 2))] * 2 |
| 104 | + connectivity = np.asarray(connectivity[:-4]) |
| 105 | + offset = np.asarray(range(8, 8 * self.n_levels, 8)) |
| 106 | + |
| 107 | + _ = {"test_pd": np.array([44] * n_vertices)} |
| 108 | + |
| 109 | + relative_humidity = self.output["products"]["S_max_percent"] |
| 110 | + cell_data = { |
| 111 | + "RH": np.full(shape=(len(relative_humidity) - 1,), fill_value=np.nan) |
| 112 | + } |
| 113 | + cell_data["RH"][:step] = ( |
| 114 | + np.array(relative_humidity[:-1] + np.diff(relative_humidity) / 2) |
| 115 | + )[:step] |
| 116 | + unstructuredGridToVTK( |
| 117 | + path, |
| 118 | + x_vertices, |
| 119 | + y_vertices, |
| 120 | + z_vertices, |
| 121 | + connectivity=connectivity, |
| 122 | + offsets=offset, |
| 123 | + cell_types=cell_type, |
| 124 | + cellData=cell_data, |
| 125 | + ) |
| 126 | + |
| 127 | + def export_attributes(self, step, simulation): # pylint: disable=arguments-differ |
| 128 | + path = self.attributes_file_path + "_num" + self.add_leading_zeros(step) |
| 129 | + self.exported_times["attributes"][path] = step * simulation.particulator.dt |
| 130 | + |
| 131 | + payload = {} |
| 132 | + |
| 133 | + for key in self.output["attributes"].keys(): |
| 134 | + payload[key] = np.asarray(self.output["attributes"][key])[:, step].copy() |
| 135 | + if step != 0: |
| 136 | + delta_z = ( |
| 137 | + self.output["products"]["z"][step] |
| 138 | + - self.output["products"]["z"][step - 1] |
| 139 | + ) |
| 140 | + if step == 1: |
| 141 | + self.coords["z"] *= delta_z |
| 142 | + else: |
| 143 | + self.coords["z"] += delta_z |
| 144 | + |
| 145 | + pointsToVTK( |
| 146 | + path, |
| 147 | + 2 * (self.coords["x"] - 0.5) * self.half_diagonal[step], |
| 148 | + 2 * (self.coords["y"] - 0.5) * self.half_diagonal[step], |
| 149 | + self.coords["z"], |
| 150 | + data=payload, |
| 151 | + ) |
0 commit comments