Coverage for /dolfinx-env/lib/python3.14/site-packages/io4dolfinx/writers.py: 100%
58 statements
« prev ^ index » next coverage.py v7.18.0, created at 2026-11-30 11:58 +0000
« prev ^ index » next coverage.py v7.18.0, created at 2026-11-30 11:58 +0000
1# Copyright (C) 2024-2026 Jørgen Schartum Dokken
2#
3# This file is part of io4dolfinx
4#
5# SPDX-License-Identifier: MIT
7from pathlib import Path
8from typing import Any
10from mpi4py import MPI
12import dolfinx
13import fenicsx_compat
14import numpy as np
16from .backends import FileMode, get_backend
17from .format import stamp
18from .structures import FunctionData, MeshData
21def prepare_meshdata_for_storage(mesh: dolfinx.mesh.Mesh, store_partition_info: bool) -> MeshData:
22 """
23 Helper function for extracting the required data from a distributed
24 :class:`dolfinx.mesh.Mesh`.
26 Args:
27 mesh: The mesh
28 store_partition_info: If one should store the partitioning info
29 Returns:
30 Data-container with the info that should be stored.
31 """
33 num_xdofs_local = mesh.geometry.index_map().size_local
34 num_xdofs_global = mesh.geometry.index_map().size_global
35 geometry_range = mesh.geometry.index_map().local_range
36 gdim = mesh.geometry.dim
38 # Convert local connectivity to globa l connectivity
39 g_imap = mesh.geometry.index_map()
40 g_dmap = fenicsx_compat.dofmap(mesh)
41 num_cells_local = mesh.topology.index_map(mesh.topology.dim).size_local
42 num_cells_global = mesh.topology.index_map(mesh.topology.dim).size_global
43 cell_range = mesh.topology.index_map(mesh.topology.dim).local_range
44 cmap = fenicsx_compat.cmap(mesh)
46 geom_layout = cmap.create_dof_layout()
47 num_dofs_per_cell = len(geom_layout.entity_closure_dofs(mesh.topology.dim, 0))
48 dofs_out = np.zeros((num_cells_local, num_dofs_per_cell), dtype=np.int64)
49 assert g_dmap.shape[1] == num_dofs_per_cell
50 dofs_out[:, :] = np.asarray(
51 g_imap.local_to_global(g_dmap[:num_cells_local, :].reshape(-1))
52 ).reshape(dofs_out.shape)
54 if store_partition_info:
55 partition_processes = mesh.comm.size
57 # Get partitioning
58 cell_imap = mesh.topology.index_map(mesh.topology.dim)
59 consensus_tag = 1202
60 cell_array, cell_offsets = fenicsx_compat.index_to_dest_ranks(cell_imap, consensus_tag)
61 num_cells_local = mesh.topology.index_map(mesh.topology.dim).size_local
62 cell_offsets = cell_offsets[: num_cells_local + 1]
63 if cell_offsets[-1] == 0:
64 cell_array = np.empty(0, dtype=np.int32)
65 else:
66 cell_array = cell_array[: cell_offsets[-1]]
68 # Compute adjacency with current process as first entry
69 ownership_array = np.full(num_cells_local + cell_offsets[-1], -1, dtype=np.int32)
70 ownership_offset = (cell_offsets + np.arange(len(cell_offsets))).astype(np.int32)
71 ownership_array[ownership_offset[:-1]] = mesh.comm.rank
72 insert_position = np.flatnonzero(ownership_array == -1)
73 ownership_array[insert_position] = cell_array
75 partition_map = fenicsx_compat.create_index_map(
76 mesh.comm,
77 ownership_array.size,
78 np.empty(0, dtype=np.int64),
79 np.empty(0, dtype=np.int32),
80 )
81 ownership_offset += partition_map.local_range[0]
82 partition_range = partition_map.local_range
83 partition_global = partition_map.size_global
84 else:
85 partition_processes = None
86 ownership_array = None
87 ownership_offset = None
88 partition_range = None
89 partition_global = None
91 return MeshData(
92 metadata=stamp(),
93 local_geometry=mesh.geometry.x[:num_xdofs_local, :gdim].copy(),
94 local_geometry_pos=geometry_range,
95 num_nodes_global=num_xdofs_global,
96 local_topology=dofs_out,
97 local_topology_pos=cell_range,
98 num_cells_global=num_cells_global,
99 cell_type=mesh.topology.cell_name(),
100 degree=cmap.degree,
101 lagrange_variant=cmap.variant,
102 store_partition=store_partition_info,
103 partition_processes=partition_processes,
104 ownership_array=ownership_array,
105 ownership_offset=ownership_offset,
106 partition_range=partition_range,
107 partition_global=partition_global,
108 )
111def write_mesh(
112 filename: Path,
113 comm: MPI.Comm,
114 mesh_data: MeshData,
115 time: float = 0.0,
116 mode: FileMode = FileMode.write,
117 backend_args: dict[str, Any] | None = None,
118 backend: str | None = None,
119):
120 """
121 Write a mesh to file
123 Args:
124 filename: Path to file to write to
125 comm: MPI communicator used in storage
126 mesh_data: Internal data structure for the mesh data to save to file
127 time: Time stamp associated with mesh
128 mode: File mode to use (write or append)
129 backend_args: Arguments for the backend
130 backend: Backend to use
131 """
132 backend_cls = get_backend(backend)
133 backend_args = backend_cls.get_default_backend_args(backend_args)
134 backend_cls.write_mesh(filename, comm, mesh_data, backend_args, mode, time)
137def write_function(
138 filename: Path,
139 comm: MPI.Comm,
140 u: FunctionData,
141 time: float = 0.0,
142 mode: FileMode = FileMode.append,
143 backend_args: dict[str, Any] | None = None,
144 backend: str | None = None,
145):
146 """
147 Write a function to file
149 Args:
150 filename: Path to file to write to
151 comm: MPI communicator used in storage
152 u: Internal data structure for the function data to save to file
153 time: Time stamp associated with function
154 mode: File mode to use (write or append)
155 backend_args: Arguments for the backend
156 backend: Backend to use
157 """
158 backend_cls = get_backend(backend)
159 backend_args = backend_cls.get_default_backend_args(backend_args)
160 backend_cls.write_function(filename, comm, u=u, time=time, backend_args=backend_args, mode=mode)