10#include "cell_types.h"
18#include <dolfinx/graph/ordering.h>
40template <std::
floating_po
int T>
41std::tuple<std::vector<T>, std::vector<std::int64_t>>
42create_interval_cells(std::array<T, 2> p, std::int64_t n);
44template <std::
floating_po
int T>
45Mesh<T> build_tri(MPI_Comm comm, std::array<std::array<T, 2>, 2> p,
46 std::array<std::int64_t, 2> n,
51template <std::
floating_po
int T>
52Mesh<T> build_quad(MPI_Comm comm, std::array<std::array<T, 2>, 2> p,
53 std::array<std::int64_t, 2> n,
57template <std::
floating_po
int T>
58std::vector<T> create_geom(MPI_Comm comm, std::array<std::array<T, 3>, 2> p,
59 std::array<std::int64_t, 3> n);
61template <std::
floating_po
int T>
62Mesh<T> build_tet(MPI_Comm comm, MPI_Comm subcomm,
63 std::array<std::array<T, 3>, 2> p,
64 std::array<std::int64_t, 3> n,
68template <std::
floating_po
int T>
69Mesh<T> build_hex(MPI_Comm comm, MPI_Comm subcomm,
70 std::array<std::array<T, 3>, 2> p,
71 std::array<std::int64_t, 3> n,
75template <std::
floating_po
int T>
76Mesh<T> build_prism(MPI_Comm comm, MPI_Comm subcomm,
77 std::array<std::array<T, 3>, 2> p,
78 std::array<std::int64_t, 3> n,
104template <std::
floating_po
int T =
double>
106 std::array<std::array<T, 3>, 2> p,
107 std::array<std::int64_t, 3> n,
CellType celltype,
111 if (std::ranges::any_of(n, [](
auto e) {
return e < 1; }))
112 throw std::runtime_error(
"At least one cell is required.");
114 for (int32_t i = 0; i < 3; i++)
116 if (p[0][i] >= p[1][i])
117 throw std::runtime_error(
"It must hold p[0] < p[1].");
125 case CellType::tetrahedron:
126 return impl::build_tet<T>(comm, subcomm, p, n, partitioner, reorder_fn);
127 case CellType::hexahedron:
128 return impl::build_hex<T>(comm, subcomm, p, n, partitioner, reorder_fn);
129 case CellType::prism:
130 return impl::build_prism<T>(comm, subcomm, p, n, partitioner, reorder_fn);
132 throw std::runtime_error(
"Generate box mesh. Wrong cell type");
153template <std::
floating_po
int T =
double>
155 std::array<std::int64_t, 3> n,
CellType celltype,
159 return create_box<T>(comm, comm, p, n, celltype, partitioner, reorder_fn);
181template <std::
floating_po
int T =
double>
184 std::array<std::int64_t, 2> n,
CellType celltype,
186 DiagonalType diagonal = DiagonalType::right,
int gdim = 2,
189 if (gdim < 2 || gdim > 3)
190 throw std::runtime_error(
"2 <= gdim <= 3 for rectangle mesh.");
191 if (std::ranges::any_of(n, [](
auto e) {
return e < 1; }))
192 throw std::runtime_error(
"At least one cell per dimension is required.");
194 for (int32_t i = 0; i < 2; i++)
196 if (p[0][i] >= p[1][i])
197 throw std::runtime_error(
"It must hold p[0] < p[1].");
205 case CellType::triangle:
206 return impl::build_tri<T>(comm, p, n, partitioner, diagonal, reorder_fn,
208 case CellType::quadrilateral:
209 return impl::build_quad<T>(comm, p, n, partitioner, reorder_fn, gdim);
211 throw std::runtime_error(
"Generate rectangle mesh. Wrong cell type.");
231template <std::
floating_po
int T =
double>
233 std::array<std::int64_t, 2> n,
CellType celltype,
256template <std::
floating_po
int T =
double>
263 if (gdim < 1 || gdim > 3)
264 throw std::runtime_error(
"1 <= gdim <= 3 for interval mesh.");
266 throw std::runtime_error(
"At least one cell per dimension is required.");
268 const auto [a, b] = p;
270 throw std::runtime_error(
"It must hold p[0] < p[1].");
271 if (std::abs(a - b) < std::numeric_limits<T>::epsilon())
273 throw std::runtime_error(
274 "Length of interval is zero. Check your dimensions.");
283 auto [x1d, cells] = impl::create_interval_cells<T>(p, n);
284 std::size_t npts = x1d.size();
287 return create_mesh(comm, MPI_COMM_SELF, cells, element, MPI_COMM_SELF,
288 x1d, {npts, 1}, partitioner, 2, reorder_fn);
290 std::vector<T> x(npts * gdim, T(0));
291 for (std::size_t i = 0; i < npts; i++)
292 x[i * gdim] = x1d[i];
293 return create_mesh(comm, MPI_COMM_SELF, cells, element, MPI_COMM_SELF, x,
294 {npts,
static_cast<std::size_t
>(gdim)}, partitioner, 2,
299 return create_mesh(comm, MPI_COMM_NULL, {}, element, MPI_COMM_NULL,
300 std::vector<T>{}, {0,
static_cast<std::size_t
>(gdim)},
301 partitioner, 2, reorder_fn);
308template <std::
floating_po
int T>
309std::tuple<std::vector<T>, std::vector<std::int64_t>>
310create_interval_cells(std::array<T, 2> p, std::int64_t n)
312 const auto [a, b] = p;
314 const T
h = (b - a) /
static_cast<T
>(n);
317 std::vector<T> x(n + 1);
318 std::ranges::generate(x, [i = std::int64_t(0), a,
h]()
mutable
319 {
return a +
h *
static_cast<T
>(i++); });
322 std::vector<std::int64_t> cells(2 * n);
323 for (std::size_t ix = 0; ix < cells.size() / 2; ++ix)
326 cells[2 * ix + 1] = ix + 1;
329 return {std::move(x), std::move(cells)};
332template <std::
floating_po
int T>
333std::vector<T> create_geom(MPI_Comm comm, std::array<std::array<T, 3>, 2> p,
334 std::array<std::int64_t, 3> n)
338 const auto [nx, ny, nz] = n;
340 assert(std::ranges::all_of(n, [](
auto e) {
return e >= 1; }));
344 const std::array<T, 3> extents = {
345 (p1[0] - p0[0]) /
static_cast<T
>(nx),
346 (p1[1] - p0[1]) /
static_cast<T
>(ny),
347 (p1[2] - p0[2]) /
static_cast<T
>(nz),
350 if (std::ranges::any_of(
352 {
return std::abs(e) < 2.0 * std::numeric_limits<T>::epsilon(); }))
354 throw std::runtime_error(
355 "Box seems to have zero width, height or depth. Check dimensions");
358 const std::int64_t n_points = (nx + 1) * (ny + 1) * (nz + 1);
363 geom.reserve((range_end - range_begin) * 3);
364 const std::int64_t sqxy = (nx + 1) * (ny + 1);
365 for (std::int64_t v = range_begin; v < range_end; ++v)
368 const std::int64_t p = v % sqxy;
369 std::array<std::int64_t, 3> idx = {p % (nx + 1), p / (nx + 1), v / sqxy};
372 for (std::size_t i = 0; i < idx.size(); i++)
373 geom.push_back(p0[i] +
static_cast<T
>(idx[i]) * extents[i]);
379template <std::
floating_po
int T>
380Mesh<T> build_tet(MPI_Comm comm, MPI_Comm subcomm,
381 std::array<std::array<T, 3>, 2> p,
382 std::array<std::int64_t, 3> n,
386 common::Timer timer(
"Build BoxMesh (tetrahedra)");
388 std::vector<std::int64_t>
cells;
389 fem::CoordinateElement<T> element(CellType::tetrahedron, 1);
390 if (subcomm != MPI_COMM_NULL)
392 x = create_geom<T>(subcomm, p, n);
394 const auto [nx, ny, nz] = n;
395 const std::int64_t n_cells = nx * ny * nz;
399 cells.reserve(6 * (range_c[1] - range_c[0]) * 4);
402 for (std::int64_t i = range_c[0]; i < range_c[1]; ++i)
404 const std::int64_t iz = i / (nx * ny);
405 const std::int64_t j = i % (nx * ny);
406 const std::int64_t iy = j / nx;
407 const std::int64_t ix = j % nx;
408 const std::int64_t v0 = iz * (nx + 1) * (ny + 1) + iy * (nx + 1) + ix;
409 const std::int64_t v1 = v0 + 1;
410 const std::int64_t v2 = v0 + (nx + 1);
411 const std::int64_t v3 = v1 + (nx + 1);
412 const std::int64_t v4 = v0 + (nx + 1) * (ny + 1);
413 const std::int64_t v5 = v1 + (nx + 1) * (ny + 1);
414 const std::int64_t v6 = v2 + (nx + 1) * (ny + 1);
415 const std::int64_t v7 = v3 + (nx + 1) * (ny + 1);
419 {v0, v1, v3, v7, v0, v1, v7, v5, v0, v5, v7, v4,
420 v0, v3, v2, v7, v0, v6, v4, v7, v0, v2, v6, v7});
424 return create_mesh(comm, subcomm, cells, element, subcomm, x,
425 {x.size() / 3, 3}, partitioner, 2, reorder_fn);
428template <std::
floating_po
int T>
430build_hex(MPI_Comm comm, MPI_Comm subcomm, std::array<std::array<T, 3>, 2> p,
431 std::array<std::int64_t, 3> n,
433 const std::function<std::vector<std::int32_t>(
434 const graph::AdjacencyList<std::int32_t>&)>& reorder_fn)
436 common::Timer timer(
"Build BoxMesh (hexahedra)");
438 std::vector<std::int64_t>
cells;
439 fem::CoordinateElement<T> element(CellType::hexahedron, 1);
440 if (subcomm != MPI_COMM_NULL)
442 x = create_geom<T>(subcomm, p, n);
445 const auto [nx, ny, nz] = n;
446 const std::int64_t n_cells = nx * ny * nz;
449 cells.reserve((range_c[1] - range_c[0]) * 8);
450 for (std::int64_t i = range_c[0]; i < range_c[1]; ++i)
452 const std::int64_t iz = i / (nx * ny);
453 const std::int64_t j = i % (nx * ny);
454 const std::int64_t iy = j / nx;
455 const std::int64_t ix = j % nx;
457 const std::int64_t v0 = (iz * (ny + 1) + iy) * (nx + 1) + ix;
458 const std::int64_t v1 = v0 + 1;
459 const std::int64_t v2 = v0 + (nx + 1);
460 const std::int64_t v3 = v1 + (nx + 1);
461 const std::int64_t v4 = v0 + (nx + 1) * (ny + 1);
462 const std::int64_t v5 = v1 + (nx + 1) * (ny + 1);
463 const std::int64_t v6 = v2 + (nx + 1) * (ny + 1);
464 const std::int64_t v7 = v3 + (nx + 1) * (ny + 1);
465 cells.insert(
cells.end(), {v0, v1, v2, v3, v4, v5, v6, v7});
469 return create_mesh(comm, subcomm, cells, element, subcomm, x,
470 {x.size() / 3, 3}, partitioner, 2, reorder_fn);
473template <std::
floating_po
int T>
474Mesh<T> build_prism(MPI_Comm comm, MPI_Comm subcomm,
475 std::array<std::array<T, 3>, 2> p,
476 std::array<std::int64_t, 3> n,
481 std::vector<std::int64_t>
cells;
482 fem::CoordinateElement<T> element(CellType::prism, 1);
483 if (subcomm != MPI_COMM_NULL)
485 x = create_geom<T>(subcomm, p, n);
487 const std::int64_t nx = n[0];
488 const std::int64_t ny = n[1];
489 const std::int64_t nz = n[2];
490 const std::int64_t n_cells = nx * ny * nz;
493 const std::int64_t cell_range = range_c[1] - range_c[0];
496 cells.reserve(2 * cell_range * 6);
497 for (std::int64_t i = range_c[0]; i < range_c[1]; ++i)
499 const std::int64_t iz = i / (nx * ny);
500 const std::int64_t j = i % (nx * ny);
501 const std::int64_t iy = j / nx;
502 const std::int64_t ix = j % nx;
504 const std::int64_t v0 = (iz * (ny + 1) + iy) * (nx + 1) + ix;
505 const std::int64_t v1 = v0 + 1;
506 const std::int64_t v2 = v0 + (nx + 1);
507 const std::int64_t v3 = v1 + (nx + 1);
508 const std::int64_t v4 = v0 + (nx + 1) * (ny + 1);
509 const std::int64_t v5 = v1 + (nx + 1) * (ny + 1);
510 const std::int64_t v6 = v2 + (nx + 1) * (ny + 1);
511 const std::int64_t v7 = v3 + (nx + 1) * (ny + 1);
512 cells.insert(
cells.end(), {v0, v1, v2, v4, v5, v6});
513 cells.insert(
cells.end(), {v1, v2, v3, v5, v6, v7});
517 return create_mesh(comm, subcomm, cells, element, subcomm, x,
518 {x.size() / 3, 3}, partitioner, 2, reorder_fn);
521template <std::
floating_po
int T>
522Mesh<T> build_tri(MPI_Comm comm, std::array<std::array<T, 2>, 2> p,
523 std::array<std::int64_t, 2> n,
528 fem::CoordinateElement<T> element(CellType::triangle, 1);
529 if (gdim < 2 || gdim > 3)
530 throw std::runtime_error(
"2 <= gdim <= 3 for tri mesh.");
534 const auto [p0, p1] = p;
535 const auto [nx, ny] = n;
537 const auto [a, c] = p0;
538 const auto [b, d] = p1;
540 const T ab = (b - a) /
static_cast<T
>(nx);
541 const T cd = (d - c) /
static_cast<T
>(ny);
542 if (std::abs(b - a) < std::numeric_limits<T>::epsilon()
543 or std::abs(d - c) < std::numeric_limits<T>::epsilon())
545 throw std::runtime_error(
"Rectangle seems to have zero width, height or "
546 "depth. Check dimensions");
553 case DiagonalType::crossed:
554 nv = (nx + 1) * (ny + 1) + nx * ny;
558 nv = (nx + 1) * (ny + 1);
564 std::vector<std::int64_t>
cells;
565 cells.reserve(nc * 3);
568 for (std::int64_t iy = 0; iy <= ny; iy++)
570 T x1 = c + cd *
static_cast<T
>(iy);
571 for (std::int64_t ix = 0; ix <= nx; ix++)
572 x.insert(x.end(), {a + ab * static_cast<T>(ix), x1});
578 case DiagonalType::crossed:
579 for (std::int64_t iy = 0; iy < ny; iy++)
581 T x1 = c + cd * (
static_cast<T
>(iy) + 0.5);
582 for (std::int64_t ix = 0; ix < nx; ix++)
584 T x0 = a + ab * (
static_cast<T
>(ix) + 0.5);
585 x.insert(x.end(), {x0, x1});
596 case DiagonalType::crossed:
598 for (std::int64_t iy = 0; iy < ny; iy++)
600 for (std::int64_t ix = 0; ix < nx; ix++)
602 std::int64_t v0 = iy * (nx + 1) + ix;
603 std::int64_t v1 = v0 + 1;
604 std::int64_t v2 = v0 + (nx + 1);
605 std::int64_t v3 = v1 + (nx + 1);
606 std::int64_t vmid = (nx + 1) * (ny + 1) + iy * nx + ix;
609 cells.insert(
cells.end(), {v0, v1, vmid, v0, v2, vmid, v1, v3, vmid,
618 for (std::int64_t iy = 0; iy < ny; iy++)
623 case DiagonalType::right_left:
625 local_diagonal = DiagonalType::right;
627 local_diagonal = DiagonalType::left;
629 case DiagonalType::left_right:
631 local_diagonal = DiagonalType::left;
633 local_diagonal = DiagonalType::right;
638 for (std::int64_t ix = 0; ix < nx; ix++)
640 std::int64_t v0 = iy * (nx + 1) + ix;
641 std::int64_t v1 = v0 + 1;
642 std::int64_t v2 = v0 + (nx + 1);
643 std::int64_t v3 = v1 + (nx + 1);
644 switch (local_diagonal)
646 case DiagonalType::left:
648 cells.insert(
cells.end(), {v0, v1, v2, v1, v2, v3});
649 if (diagonal == DiagonalType::right_left
650 or diagonal == DiagonalType::left_right)
652 local_diagonal = DiagonalType::right;
658 cells.insert(
cells.end(), {v0, v1, v3, v0, v2, v3});
659 if (diagonal == DiagonalType::right_left
660 or diagonal == DiagonalType::left_right)
662 local_diagonal = DiagonalType::left;
671 std::size_t npts = x.size() / 2;
674 return create_mesh(comm, MPI_COMM_SELF, cells, element, MPI_COMM_SELF, x,
675 {npts, 2}, partitioner, 2, reorder_fn);
677 std::vector<T> xg(npts * gdim, T(0));
678 for (std::size_t i = 0; i < npts; i++)
680 xg[i * gdim] = x[2 * i];
681 xg[i * gdim + 1] = x[2 * i + 1];
683 return create_mesh(comm, MPI_COMM_SELF, cells, element, MPI_COMM_SELF, xg,
684 {npts,
static_cast<std::size_t
>(gdim)}, partitioner, 2,
689 return create_mesh(comm, MPI_COMM_NULL, {}, element, MPI_COMM_NULL,
690 std::vector<T>{}, {0,
static_cast<std::size_t
>(gdim)},
691 partitioner, 2, reorder_fn);
695template <std::
floating_po
int T>
696Mesh<T> build_quad(MPI_Comm comm, std::array<std::array<T, 2>, 2> p,
697 std::array<std::int64_t, 2> n,
701 if (gdim < 2 || gdim > 3)
702 throw std::runtime_error(
"2 <= gdim <= 3 for quad mesh.");
704 fem::CoordinateElement<T> element(CellType::quadrilateral, 1);
707 const auto [nx, ny] = n;
708 const auto [a, c] = p[0];
709 const auto [b, d] = p[1];
711 const T ab = (b - a) /
static_cast<T
>(nx);
712 const T cd = (d - c) /
static_cast<T
>(ny);
716 x.reserve((nx + 1) * (ny + 1) * 2);
717 for (std::int64_t ix = 0; ix <= nx; ix++)
719 T x0 = a + ab *
static_cast<T
>(ix);
720 for (std::int64_t iy = 0; iy <= ny; iy++)
721 x.insert(x.end(), {x0, c + cd * static_cast<T>(iy)});
725 std::vector<std::int64_t>
cells;
726 cells.reserve(nx * ny * 4);
727 for (std::int64_t ix = 0; ix < nx; ix++)
729 for (std::int64_t iy = 0; iy < ny; iy++)
731 std::int64_t i0 = ix * (ny + 1);
732 cells.insert(
cells.end(), {i0 + iy, i0 + iy + 1, i0 + iy + ny + 1,
737 std::size_t npts = x.size() / 2;
740 return create_mesh(comm, MPI_COMM_SELF, cells, element, MPI_COMM_SELF, x,
741 {npts, 2}, partitioner, 2, reorder_fn);
743 std::vector<T> xg(npts * gdim, T(0));
744 for (std::size_t i = 0; i < npts; i++)
746 xg[i * gdim] = x[2 * i];
747 xg[i * gdim + 1] = x[2 * i + 1];
749 return create_mesh(comm, MPI_COMM_SELF, cells, element, MPI_COMM_SELF, xg,
750 {npts,
static_cast<std::size_t
>(gdim)}, partitioner, 2,
755 return create_mesh(comm, MPI_COMM_NULL, {}, element, MPI_COMM_NULL,
756 std::vector<T>{}, {0,
static_cast<std::size_t
>(gdim)},
757 partitioner, 2, reorder_fn);
Definition CoordinateElement.h:38
A Mesh consists of a set of connected and numbered mesh topological entities, and geometry data.
Definition Mesh.h:23
Functions supporting mesh operations.
int size(MPI_Comm comm)
Definition MPI.cpp:72
int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition MPI.cpp:64
constexpr std::array< std::int64_t, 2 > local_range(int rank, std::int64_t N, int size)
Return local range for the calling process, partitioning the global [0, N - 1] range across all ranks...
Definition MPI.h:89
void cells(la::SparsityPattern &pattern, const std::pair< R0, R1 > &cells, std::array< std::reference_wrapper< const DofMap >, 2 > dofmaps)
Iterate over cells and insert entries into sparsity pattern.
Definition sparsitybuild.h:37
std::vector< std::int32_t > reorder_gps(const graph::AdjacencyList< std::int32_t > &graph)
Re-order a graph using the Gibbs-Poole-Stockmeyer algorithm.
Definition ordering.cpp:363
Mesh data structures and algorithms on meshes.
Definition DofMap.h:32
Mesh< T > create_rectangle(MPI_Comm comm, std::array< std::array< T, 2 >, 2 > p, std::array< std::int64_t, 2 > n, CellType celltype, CellPartitionFunction partitioner, DiagonalType diagonal=DiagonalType::right, int gdim=2, const CellReorderFunction &reorder_fn=graph::reorder_gps)
Create a uniform mesh::Mesh over the rectangle spanned by the two points p.
Definition generation.h:183
CellPartitionFunction create_cell_partitioner(mesh::GhostMode ghost_mode, const graph::partition_fn &partfn, std::optional< std::int32_t > max_facet_to_cell_links)
Create a function that computes destination rank for mesh cells on this rank by applying the default ...
Definition utils.cpp:100
std::function< std::vector< std::int32_t >( const graph::AdjacencyList< std::int32_t > &)> CellReorderFunction
Function that reorders (locally) cells that are owned by this process. It takes the local mesh dual g...
Definition utils.h:220
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:212
DiagonalType
Enum for different diagonal types.
Definition generation.h:29
Mesh< T > create_box(MPI_Comm comm, MPI_Comm subcomm, std::array< std::array< T, 3 >, 2 > p, std::array< std::int64_t, 3 > n, CellType celltype, CellPartitionFunction partitioner=nullptr, const CellReorderFunction &reorder_fn=graph::reorder_gps)
Create a uniform mesh::Mesh over rectangular prism spanned by the two points p.
Definition generation.h:105
CellType
Cell type identifier.
Definition cell_types.h:21
std::vector< T > h(const Mesh< T > &mesh, std::span< const std::int32_t > entities, int dim)
Compute greatest distance between any two vertices of the mesh entities (h).
Definition utils.h:414
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:1046
GhostMode
Enum for different partitioning ghost modes.
Definition utils.h:44
Mesh< T > create_interval(MPI_Comm comm, std::int64_t n, std::array< T, 2 > p, mesh::GhostMode ghost_mode=mesh::GhostMode::none, CellPartitionFunction partitioner=nullptr, int gdim=1, const CellReorderFunction &reorder_fn=graph::reorder_gps)
Interval mesh of the 1D line [a, b].
Definition generation.h:258