Loading...
Searching...
No Matches
refine.h
// Copyright (C) 2010-2024 Garth N. Wells and Paul T. Kühner
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include "dolfinx/common/MPI.h"
#include "dolfinx/graph/AdjacencyList.h"
#include "dolfinx/graph/partitioners.h"
#include "dolfinx/mesh/Mesh.h"
#include "dolfinx/mesh/Topology.h"
#include "dolfinx/mesh/cell_types.h"
#include "dolfinx/mesh/graphbuild.h"
#include "dolfinx/mesh/utils.h"
#include "interval.h"
#include "plaza.h"
#include <algorithm>
#include <concepts>
#include <mpi.h>
#include <optional>
#include <spdlog/spdlog.h>
#include <stdexcept>
#include <utility>
#include <variant>
namespace dolfinx::refinement
{
template <std::floating_point T>
mesh::CellPartitionFunction
create_identity_partitioner(const mesh::Mesh<T>& parent_mesh,
std::span<std::int32_t> parent_cell)
{
// TODO: optimize for non ghosted mesh?
return [&parent_mesh,
parent_cell](MPI_Comm comm, int /*nparts*/,
std::vector<mesh::CellType> cell_types,
std::vector<std::span<const std::int64_t>> cells)
-> graph::AdjacencyList<std::int32_t>
{
auto parent_cell_im
= parent_mesh.topology()->index_map(parent_mesh.topology()->dim());
std::int32_t parent_num_cells = parent_cell_im->size_local();
std::span parent_cell_owners = parent_cell_im->owners();
std::int32_t num_cells
= cells.front().size() / mesh::num_cell_vertices(cell_types.front());
std::vector<std::int32_t> destinations(num_cells);
int rank = dolfinx::MPI::rank(comm);
for (std::size_t i = 0; i < destinations.size(); i++)
{
bool parent_is_ghost_cell = parent_cell[i] > parent_num_cells;
if (parent_is_ghost_cell)
destinations[i] = parent_cell_owners[parent_cell[i] - parent_num_cells];
else
destinations[i] = rank;
}
if (comm == MPI_COMM_NULL)
return graph::regular_adjacency_list(std::move(destinations), 1);
auto dual_graph = mesh::build_dual_graph(comm, cell_types, cells);
std::vector<std::int32_t> node_disp(MPI::size(comm) + 1, 0);
std::int32_t local_size = dual_graph.num_nodes();
MPI_Allgather(&local_size, 1, dolfinx::MPI::mpi_t<std::int32_t>,
node_disp.data() + 1, 1, dolfinx::MPI::mpi_t<std::int32_t>,
comm);
std::partial_sum(node_disp.begin(), node_disp.end(), node_disp.begin());
return compute_destination_ranks(comm, dual_graph, node_disp, destinations);
};
}
struct IdentityPartitionerPlaceholder
{
};
template <std::floating_point T>
std::tuple<mesh::Mesh<T>, std::optional<std::vector<std::int32_t>>,
std::optional<std::vector<std::int8_t>>>
refine(const mesh::Mesh<T>& mesh,
std::optional<std::span<const std::int32_t>> edges,
std::variant<IdentityPartitionerPlaceholder, mesh::CellPartitionFunction>
partitioner
= IdentityPartitionerPlaceholder(),
Option option = Option::parent_cell)
{
auto topology = mesh.topology();
assert(topology);
if (!mesh::is_simplex(topology->cell_type()))
throw std::runtime_error("Refinement only defined for simplices");
auto [cell_adj, new_vertex_coords, xshape, parent_cell, parent_facet]
= (topology->cell_type() == mesh::CellType::interval)
? interval::compute_refinement_data(mesh, edges, option)
: plaza::compute_refinement_data(mesh, edges, option);
if (std::holds_alternative<IdentityPartitionerPlaceholder>(partitioner))
{
if (!parent_cell)
{
throw std::runtime_error(
"Identity partitioner relies on parent cell computation");
}
assert(parent_cell);
partitioner = create_identity_partitioner(mesh, parent_cell.value());
}
assert(std::holds_alternative<mesh::CellPartitionFunction>(partitioner));
mesh::Mesh<T> mesh1 = mesh::create_mesh(
mesh.comm(), mesh.comm(), cell_adj.array(), mesh.geometry().cmap(),
mesh.comm(), new_vertex_coords, xshape,
std::get<mesh::CellPartitionFunction>(partitioner));
// Report the number of refined cells
const int D = topology->dim();
const std::int64_t n0 = topology->index_map(D)->size_global();
const std::int64_t n1 = mesh1.topology()->index_map(D)->size_global();
spdlog::info(
"Number of cells increased from {} to {} ({}% increase).", n0, n1,
100.0 * (static_cast<double>(n1) / static_cast<double>(n0) - 1.0));
return {std::move(mesh1), std::move(parent_cell), std::move(parent_facet)};
}
} // namespace dolfinx::refinement
dolfinx::graph::AdjacencyList
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:38
dolfinx::mesh::Mesh
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
dolfinx::mesh::Mesh::topology
std::shared_ptr< Topology > topology()
Get mesh topology.
Definition Mesh.h:69
utils.h
Functions supporting mesh operations.
dolfinx::MPI::mpi_t
MPI_Datatype mpi_t
Retrieves the MPI data type associated to the provided type.
Definition MPI.h:280
dolfinx::MPI::size
int size(MPI_Comm comm)
Definition MPI.cpp:72
dolfinx::MPI::rank
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:64
dolfinx::graph::regular_adjacency_list
AdjacencyList< typename std::decay_t< U >::value_type, V > regular_adjacency_list(U &&data, int degree)
Construct a constant degree (valency) adjacency list.
Definition AdjacencyList.h:248
dolfinx::mesh
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
dolfinx::mesh::num_cell_vertices
int num_cell_vertices(CellType type)
Definition cell_types.cpp:147
dolfinx::mesh::create_mesh
Mesh< typename std::remove_reference_t< typename U::value_type > > create_mesh(MPI_Comm comm, MPI_Comm commt, std::vector< std::span< const std::int64_t > > cells, const std::vector< fem::CoordinateElement< typename std::remove_reference_t< typename U::value_type > > > &elements, MPI_Comm commg, const U &x, std::array< std::size_t, 2 > xshape, const CellPartitionFunction &partitioner, std::optional< std::int32_t > max_facet_to_cell_links, const CellReorderFunction &reorder_fn=graph::reorder_gps)
Create a distributed mesh::Mesh from mesh data and using the provided graph partitioning function for...
Definition utils.h:1025
dolfinx::mesh::is_simplex
bool is_simplex(CellType type)
Definition cell_types.cpp:145
dolfinx::mesh::CellPartitionFunction
std::function< graph::AdjacencyList< std::int32_t >( MPI_Comm comm, int nparts, const std::vector< CellType > &cell_types, const std::vector< std::span< const std::int64_t > > &cells)> CellPartitionFunction
Signature for the cell partitioning function. Function that implement this interface compute the dest...
Definition utils.h:209
dolfinx::mesh::build_dual_graph
graph::AdjacencyList< std::int64_t > build_dual_graph(MPI_Comm comm, std::span< const CellType > celltypes, const std::vector< std::span< const std::int64_t > > &cells, std::optional< std::int32_t > max_facet_to_cell_links=2)
Build distributed mesh dual graph (cell-cell connections via facets) from minimal mesh data.
Definition graphbuild.cpp:732
dolfinx::refinement::plaza::compute_refinement_data
std::tuple< graph::AdjacencyList< std::int64_t >, std::vector< T >, std::array< std::size_t, 2 >, std::optional< std::vector< std::int32_t > >, std::optional< std::vector< std::int8_t > > > compute_refinement_data(const mesh::Mesh< T > &mesh, std::optional< std::span< const std::int32_t > > edges, Option option)
Definition plaza.h:462
dolfinx::refinement
Mesh refinement algorithms.
Definition dolfinx_refinement.h:8
dolfinx::refinement::refine
std::tuple< mesh::Mesh< T >, std::optional< std::vector< std::int32_t > >, std::optional< std::vector< std::int8_t > > > refine(const mesh::Mesh< T > &mesh, std::optional< std::span< const std::int32_t > > edges, std::variant< IdentityPartitionerPlaceholder, mesh::CellPartitionFunction > partitioner=IdentityPartitionerPlaceholder(), Option option=Option::parent_cell)
Refine a mesh with markers.
Definition refine.h:123
dolfinx::refinement::create_identity_partitioner
mesh::CellPartitionFunction create_identity_partitioner(const mesh::Mesh< T > &parent_mesh, std::span< std::int32_t > parent_cell)
Create a cell partitioner which maintains the partition of a coarse mesh.
Definition refine.h:44
dolfinx::refinement::Option
Option
Options for data to compute during mesh refinement.
Definition option.h:16
dolfinx::refinement::Option::parent_cell
@ parent_cell
Definition option.h:20
dolfinx::refinement::Option::parent_facet
@ parent_facet
Definition option.h:18
dolfinx::refinement::IdentityPartitionerPlaceholder
Placeholder for the creation of an identity partitioner in refine.
Definition refine.h:90