Debian Codegraph @

src/adios4dolfinx/writers.py

raw
1 # Copyright (C) 2024 Jørgen Schartum Dokken
2 #
3 # This file is part of adios4dolfinx
4 #
5 # SPDX-License-Identifier: MIT
6
7
8 import warnings
9 from pathlib import Path
10
11 from mpi4py import MPI
12
13 import adios2
14 import numpy as np
15
16 from .adios2_helpers import ADIOSFile, check_variable_exists, resolve_adios_scope
17 from .structures import FunctionData, MeshData
18
19 adios2 = resolve_adios_scope(adios2)
20
21
22 def write_mesh(
23 filename: Path,
24 comm: MPI.Intracomm,
25 mesh: MeshData,
26 engine: str = "BP4",
27 mode: adios2.Mode = adios2.Mode.Write,
28 time: float = 0.0,
29 io_name: str = "MeshWriter",
30 ):
31 """
32 Write a mesh to file using ADIOS2
33
34 Parameters:
35 comm: MPI communicator used in storage
36 mesh: Internal data structure for the mesh data to save to file
37 filename: Path to file to write to
38 engine: ADIOS2 engine to use
39 mode: ADIOS2 mode to use (write or append)
40 io_name: Internal name used for the ADIOS IO object
41 """
42
43 gdim = mesh.local_geometry.shape[1]
44 adios = adios2.ADIOS(comm)
45 with ADIOSFile(
46 adios=adios, filename=filename, mode=mode, engine=engine, io_name=io_name, comm=comm
47 ) as adios_file:
48 adios_file.file.BeginStep()
49 # Write geometry
50 pointvar = adios_file.io.DefineVariable(
51 "Points",
52 mesh.local_geometry,
53 shape=[mesh.num_nodes_global, gdim],
54 start=[mesh.local_geometry_pos[0], 0],
55 count=[mesh.local_geometry_pos[1] - mesh.local_geometry_pos[0], gdim],
56 )
57 adios_file.file.Put(pointvar, mesh.local_geometry, adios2.Mode.Sync)
58
59 if mode == adios2.Mode.Write:
60 adios_file.io.DefineAttribute("CellType", mesh.cell_type)
61 adios_file.io.DefineAttribute("Degree", np.array([mesh.degree], dtype=np.int32))
62 adios_file.io.DefineAttribute(
63 "LagrangeVariant", np.array([mesh.lagrange_variant], dtype=np.int32)
64 )
65 # Write topology (on;y on first write as topology is constant)
66 num_dofs_per_cell = mesh.local_topology.shape[1]
67 dvar = adios_file.io.DefineVariable(
68 "Topology",
69 mesh.local_topology,
70 shape=[mesh.num_cells_global, num_dofs_per_cell],
71 start=[mesh.local_topology_pos[0], 0],
72 count=[
73 mesh.local_topology_pos[1] - mesh.local_topology_pos[0],
74 num_dofs_per_cell,
75 ],
76 )
77 adios_file.file.Put(dvar, mesh.local_topology)
78
79 # Add partitioning data
80 if mesh.store_partition:
81 assert mesh.partition_range is not None
82 par_data = adios_file.io.DefineVariable(
83 "PartitioningData",
84 mesh.ownership_array,
85 shape=[mesh.partition_global],
86 start=[mesh.partition_range[0]],
87 count=[
88 mesh.partition_range[1] - mesh.partition_range[0],
89 ],
90 )
91 adios_file.file.Put(par_data, mesh.ownership_array)
92 assert mesh.ownership_offset is not None
93 par_offset = adios_file.io.DefineVariable(
94 "PartitioningOffset",
95 mesh.ownership_offset,
96 shape=[mesh.num_cells_global + 1],
97 start=[mesh.local_topology_pos[0]],
98 count=[mesh.local_topology_pos[1] - mesh.local_topology_pos[0] + 1],
99 )
100 adios_file.file.Put(par_offset, mesh.ownership_offset)
101 assert mesh.partition_processes is not None
102 adios_file.io.DefineAttribute(
103 "PartitionProcesses", np.array([mesh.partition_processes], dtype=np.int32)
104 )
105 if mode == adios2.Mode.Append and mesh.store_partition:
106 warnings.warn("Partitioning data is not written in append mode")
107
108 # Add time step to file
109 t_arr = np.array([time], dtype=np.float64)
110 time_var = adios_file.io.DefineVariable(
111 "MeshTime",
112 t_arr,
113 shape=[1],
114 start=[0],
115 count=[1 if comm.rank == 0 else 0],
116 )
117 adios_file.file.Put(time_var, t_arr)
118
119 adios_file.file.PerformPuts()
120 adios_file.file.EndStep()
121
122
123 def write_function(
124 filename: Path,
125 comm: MPI.Intracomm,
126 u: FunctionData,
127 engine: str = "BP4",
128 mode: adios2.Mode = adios2.Mode.Append,
129 time: float = 0.0,
130 io_name: str = "FunctionWriter",
131 ):
132 """
133 Write a function to file using ADIOS2
134
135 Parameters:
136 comm: MPI communicator used in storage
137 u: Internal data structure for the function data to save to file
138 filename: Path to file to write to
139 engine: ADIOS2 engine to use
140 mode: ADIOS2 mode to use (write or append)
141 time: Time stamp associated with function
142 io_name: Internal name used for the ADIOS IO object
143 """
144 adios = adios2.ADIOS(comm)
145
146 cell_permutations_exists = False
147 dofmap_exists = False
148 XDofmap_exists = False
149 if mode == adios2.Mode.Append:
150 cell_permutations_exists = check_variable_exists(
151 adios, filename, "CellPermutations", engine=engine
152 )
153 dofmap_exists = check_variable_exists(adios, filename, f"{u.name}_dofmap", engine=engine)
154 XDofmap_exists = check_variable_exists(adios, filename, f"{u.name}_XDofmap", engine=engine)
155
156 with ADIOSFile(
157 adios=adios, filename=filename, mode=mode, engine=engine, io_name=io_name, comm=comm
158 ) as adios_file:
159 adios_file.file.BeginStep()
160
161 if not cell_permutations_exists:
162 # Add mesh permutations
163 pvar = adios_file.io.DefineVariable(
164 "CellPermutations",
165 u.cell_permutations,
166 shape=[u.num_cells_global],
167 start=[u.local_cell_range[0]],
168 count=[u.local_cell_range[1] - u.local_cell_range[0]],
169 )
170 adios_file.file.Put(pvar, u.cell_permutations)
171
172 if not dofmap_exists:
173 # Add dofmap
174 dofmap_var = adios_file.io.DefineVariable(
175 f"{u.name}_dofmap",
176 u.dofmap_array,
177 shape=[u.global_dofs_in_dofmap],
178 start=[u.dofmap_range[0]],
179 count=[u.dofmap_range[1] - u.dofmap_range[0]],
180 )
181 adios_file.file.Put(dofmap_var, u.dofmap_array)
182
183 if not XDofmap_exists:
184 # Add XDofmap
185 xdofmap_var = adios_file.io.DefineVariable(
186 f"{u.name}_XDofmap",
187 u.dofmap_offsets,
188 shape=[u.num_cells_global + 1],
189 start=[u.local_cell_range[0]],
190 count=[u.local_cell_range[1] - u.local_cell_range[0] + 1],
191 )
192 adios_file.file.Put(xdofmap_var, u.dofmap_offsets)
193
194 val_var = adios_file.io.DefineVariable(
195 f"{u.name}_values",
196 u.values,
197 shape=[u.num_dofs_global],
198 start=[u.dof_range[0]],
199 count=[u.dof_range[1] - u.dof_range[0]],
200 )
201 adios_file.file.Put(val_var, u.values)
202
203 # Add time step to file
204 t_arr = np.array([time], dtype=np.float64)
205 time_var = adios_file.io.DefineVariable(
206 f"{u.name}_time",
207 t_arr,
208 shape=[1],
209 start=[0],
210 count=[1 if comm.rank == 0 else 0],
211 )
212 adios_file.file.Put(time_var, t_arr)
213 adios_file.file.PerformPuts()
214 adios_file.file.EndStep()