Coverage for /dolfinx-env/lib/python3.14/site-packages/io4dolfinx/original_checkpoint.py: 100%
56 statements
« prev ^ index » next coverage.py v7.18.0, created at 2026-11-30 12:02 +0000
« prev ^ index » next coverage.py v7.18.0, created at 2026-11-30 12:02 +0000
1# Copyright (C) 2024 Jørgen Schartum Dokken
2#
3# This file is part of io4dolfinx
4#
5# SPDX-License-Identifier: MIT
7from __future__ import annotations
9import logging
10import typing
12from mpi4py import MPI
14import dolfinx
15import fenicsx_compat
16import numpy as np
18from .comm_helpers import send_to_input_order
19from .format import stamp
20from .structures import FunctionData, MeshData
21from .utils import input_cell_index, unroll_dofmap
23__all__ = ["create_original_mesh_data", "create_function_data_on_original_mesh"]
24logger = logging.getLogger(__name__)
27def create_original_mesh_data(mesh: dolfinx.mesh.Mesh) -> MeshData:
28 """
29 Store data locally on output process
30 """
31 assert isinstance(mesh.comm, MPI.Intracomm)
33 # 1. Send the topology of each owned cell, in original node indices, to the process
34 # owning its original cell index
35 num_owned_cells = mesh.topology.index_map(mesh.topology.dim).size_local
36 num_cells_global = mesh.topology.index_map(mesh.topology.dim).size_global
37 original_cell_index = input_cell_index(mesh)[:num_owned_cells]
38 original_node_index = mesh.geometry.input_global_indices
39 local_geometry_dofmap = fenicsx_compat.dofmap(mesh)[:num_owned_cells, :]
40 global_geometry_dofmap = original_node_index[local_geometry_dofmap]
41 (sorted_recv_dofmap,), local_cell_range = send_to_input_order(
42 mesh.comm, original_cell_index, [global_geometry_dofmap], num_cells_global
43 )
45 # 2. Send the coordinates of each owned node to the process owning its original index
46 gdim = mesh.geometry.dim
47 num_owned_nodes = mesh.geometry.index_map().size_local
48 num_nodes_global = mesh.geometry.index_map().size_global
49 (geometry,), local_node_range = send_to_input_order(
50 mesh.comm,
51 original_node_index[:num_owned_nodes],
52 [mesh.geometry.x[:num_owned_nodes, :gdim]],
53 num_nodes_global,
54 )
55 cmap = fenicsx_compat.cmap(mesh)
57 # NOTE: Could in theory store partitioning information, but would not work nicely
58 # as one would need to read this data rather than the xdmffile.
59 # NOTE: Local geometry type hint skip is only required on DOLFINX<0.10 where
60 # proper `dolfinx.mesh.Geometry` wrapper doesn't exist
61 return MeshData(
62 metadata=stamp(),
63 local_geometry=geometry, # type: ignore[arg-type]
64 local_geometry_pos=local_node_range,
65 num_nodes_global=num_nodes_global,
66 local_topology=sorted_recv_dofmap,
67 local_topology_pos=local_cell_range,
68 num_cells_global=num_cells_global,
69 cell_type=mesh.topology.cell_name(),
70 degree=cmap.degree,
71 lagrange_variant=cmap.variant,
72 store_partition=False,
73 partition_processes=None,
74 ownership_array=None,
75 ownership_offset=None,
76 partition_range=None,
77 partition_global=None,
78 )
81def create_function_data_on_original_mesh(
82 u: dolfinx.fem.Function, name: typing.Optional[str] = None
83) -> FunctionData:
84 """
85 Create data object to save with ADIOS2
86 """
87 mesh = u.function_space.mesh
88 assert isinstance(mesh.comm, MPI.Intracomm)
89 num_owned_cells = mesh.topology.index_map(mesh.topology.dim).size_local
90 num_cells_global = mesh.topology.index_map(mesh.topology.dim).size_global
91 original_cell_index = input_cell_index(mesh)[:num_owned_cells]
93 # Send the permutation info and the dofs, as global dof indices, of each owned cell
94 # to the process owning its original cell index. The dof values themselves stay in
95 # the DOLFINx ordering.
96 dofmap = u.function_space.dofmap
97 index_map_bs = dofmap.index_map_bs
98 unrolled_dofmap = unroll_dofmap(dofmap.list[:num_owned_cells, :], dofmap.bs)
99 dmap_loc = (unrolled_dofmap // index_map_bs).reshape(-1)
100 dmap_rem = (unrolled_dofmap % index_map_bs).reshape(-1)
101 imap_global = dofmap.index_map.local_to_global(dmap_loc)
102 num_dofs_per_cell = unrolled_dofmap.shape[1]
103 dofmap_global = (imap_global * index_map_bs + dmap_rem).reshape(-1, num_dofs_per_cell)
104 cell_permutations = fenicsx_compat.cell_permutation_info(mesh.topology)[:num_owned_cells]
105 (cell_permutation_info, cell_dofs), local_cell_range = send_to_input_order(
106 mesh.comm, original_cell_index, [cell_permutations, dofmap_global], num_cells_global
107 )
108 final_dofmap = cell_dofs.reshape(-1)
110 # Get offsets of dofmap
111 num_cells_local = local_cell_range[1] - local_cell_range[0]
112 num_dofs_local_dmap = num_cells_local * num_dofs_per_cell
113 dofmap_imap = fenicsx_compat.create_index_map(
114 mesh.comm,
115 num_dofs_local_dmap,
116 np.empty(0, dtype=np.int64),
117 np.empty(0, dtype=np.int32),
118 )
119 local_dofmap_offsets = np.arange(num_cells_local + 1, dtype=np.int64)
120 local_dofmap_offsets[:] *= num_dofs_per_cell
121 local_dofmap_offsets[:] += dofmap_imap.local_range[0]
123 num_dofs_local = dofmap.index_map.size_local * dofmap.index_map_bs
124 num_dofs_global = dofmap.index_map.size_global * dofmap.index_map_bs
125 dof_start, dof_end = (int(i) * dofmap.index_map_bs for i in dofmap.index_map.local_range)
126 func_name = name if name is not None else u.name
127 return FunctionData(
128 metadata=stamp(),
129 cell_permutations=cell_permutation_info,
130 local_cell_range=local_cell_range,
131 num_cells_global=num_cells_global,
132 dofmap_array=final_dofmap,
133 dofmap_offsets=local_dofmap_offsets,
134 values=u.x.array[:num_dofs_local].copy(),
135 dof_range=(dof_start, dof_end),
136 num_dofs_global=num_dofs_global,
137 dofmap_range=dofmap_imap.local_range,
138 global_dofs_in_dofmap=dofmap_imap.size_global,
139 name=func_name,
140 )