Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template over mesh geometry type #2603

Merged
merged 71 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
78896d3
Template over mesh geometry type
garth-wells Mar 18, 2023
109748e
Improve type deduction
garth-wells Mar 18, 2023
bbdb316
Doc fix
garth-wells Mar 18, 2023
5740d71
Simplification
garth-wells Mar 18, 2023
00c13e3
Demo updates
garth-wells Mar 18, 2023
a893c8a
More changes
garth-wells Mar 20, 2023
38711a8
Merge remote-tracking branch 'origin/main' into garth/geometry-type
garth-wells Mar 20, 2023
991158b
Remove file
garth-wells Mar 20, 2023
3f1d872
Updates
garth-wells Mar 20, 2023
82c88eb
Test update
garth-wells Mar 20, 2023
066dd71
Updates
garth-wells Mar 21, 2023
8fbf214
Get type deduction working
garth-wells Mar 21, 2023
f5e4e94
Updates
garth-wells Mar 21, 2023
717ec32
Test update
garth-wells Mar 21, 2023
56936b7
Update demos
garth-wells Mar 21, 2023
0753479
Merge remote-tracking branch 'origin/main' into garth/geometry-type
garth-wells Mar 22, 2023
8e058e4
Simplify ADIOS2 mesh extraction
garth-wells Mar 22, 2023
d66ffd1
ADIOS2 IO re-factoring
garth-wells Mar 22, 2023
fa0ee9d
Template some IO functions
garth-wells Mar 22, 2023
c8f95c8
Move IO code
garth-wells Mar 22, 2023
40220b8
Doc fix
garth-wells Mar 22, 2023
e193745
Fix linkage
garth-wells Mar 22, 2023
00f8368
Demo update
garth-wells Mar 22, 2023
fae4daa
Get IO working for different mesh types
garth-wells Mar 23, 2023
b5766c8
Uncomment code
garth-wells Mar 23, 2023
ddc997f
Doc fix
garth-wells Mar 23, 2023
b55ea2c
Work on interpolation types
garth-wells Mar 24, 2023
81b78e4
Remove file
garth-wells Mar 24, 2023
d210d56
Add missing implementation
garth-wells Mar 24, 2023
60a9b93
Wrapper update
garth-wells Mar 24, 2023
b436d3f
More transition
garth-wells Mar 24, 2023
7796f28
Updates
garth-wells Mar 24, 2023
d65d335
Updates
garth-wells Mar 24, 2023
a40a6cb
Tidy up
garth-wells Mar 24, 2023
55d0b5e
More changes
garth-wells Mar 25, 2023
2f113d4
Tidy up
garth-wells Mar 25, 2023
58725da
Refactoring
garth-wells Mar 25, 2023
f0813df
Template fix
garth-wells Mar 25, 2023
33dd0a0
Undo some changes
garth-wells Mar 25, 2023
c2428e7
BB update
garth-wells Mar 25, 2023
e76e9a3
Fix for gcc
garth-wells Mar 25, 2023
b1015b3
Fixes
garth-wells Mar 25, 2023
48e190e
gcc updates
garth-wells Mar 25, 2023
2bfb490
Updates
garth-wells Mar 25, 2023
ec2cb33
More transition
garth-wells Mar 25, 2023
9c894aa
More generic code
garth-wells Mar 25, 2023
66f85f4
Clean up
garth-wells Mar 25, 2023
0f4c9e6
Simplify
garth-wells Mar 25, 2023
0d75b32
Fix demo
garth-wells Mar 25, 2023
268e01e
Fix doc error
garth-wells Mar 25, 2023
2ea8963
Small improvements
garth-wells Mar 25, 2023
940fbff
Tidy up
garth-wells Mar 26, 2023
af25cba
Tidy up
garth-wells Mar 26, 2023
f16de60
Small improvements
garth-wells Mar 26, 2023
3f422f2
Fix cpp/python error
garth-wells Mar 26, 2023
4f132b6
Simplifications
garth-wells Mar 26, 2023
126bc8e
Demo update
Mar 27, 2023
47f4f5d
Small demo update
Mar 27, 2023
b19f38f
Updates
garth-wells Mar 27, 2023
af00230
Template updates
garth-wells Mar 27, 2023
b8c7fce
Assembler updates
garth-wells Mar 27, 2023
155658a
Tidy up
garth-wells Mar 27, 2023
e2456ce
Generalise testing
garth-wells Mar 27, 2023
737d901
Updates
garth-wells Mar 27, 2023
8ee569a
Formatting fixes
garth-wells Mar 27, 2023
e77562a
Misc fixes
garth-wells Mar 27, 2023
440576d
Small doc update
garth-wells Mar 28, 2023
67e77a9
More concepts
garth-wells Mar 28, 2023
3dc10be
concepts updates
garth-wells Mar 28, 2023
bb46413
Merge branch 'main' into garth/geometry-type-2
garth-wells Mar 28, 2023
b908478
Merge branch 'main' into garth/geometry-type-2
garth-wells Mar 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/cmake/templates/DOLFINXConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ if(@SLEPC_FOUND@)
endif()
endif()

if(@ADIOS2_FOUND@)
find_dependency(ADIOS2 2.8.1)
endif()
jorgensd marked this conversation as resolved.
Show resolved Hide resolved

if(NOT TARGET dolfinx)
include("${CMAKE_CURRENT_LIST_DIR}/DOLFINXTargets.cmake")
endif()
Expand Down
12 changes: 7 additions & 5 deletions cpp/demo/biharmonic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
#include "biharmonic.h"
#include <cmath>
#include <dolfinx.h>
#include <dolfinx/common/types.h>
#include <dolfinx/fem/Constant.h>
#include <dolfinx/fem/petsc.h>
#include <numbers>
Expand All @@ -149,7 +150,7 @@ int main(int argc, char* argv[])
{
// Create mesh
auto part = mesh::create_cell_partitioner(mesh::GhostMode::shared_facet);
auto mesh = std::make_shared<mesh::Mesh>(
auto mesh = std::make_shared<mesh::Mesh<double>>(
mesh::create_rectangle(MPI_COMM_WORLD, {{{0.0, 0.0}, {1.0, 1.0}}},
{32, 32}, mesh::CellType::triangle, part));

Expand All @@ -159,7 +160,7 @@ int main(int argc, char* argv[])
// .. code-block:: cpp

// Create function space
auto V = std::make_shared<fem::FunctionSpace>(
auto V = std::make_shared<fem::FunctionSpace<double>>(
fem::create_functionspace(functionspace_form_biharmonic_a, "u", mesh));

// The source function ::math:`f` and the penalty term
Expand Down Expand Up @@ -219,7 +220,8 @@ int main(int argc, char* argv[])
}
return marker;
});
const auto bdofs = fem::locate_dofs_topological({*V}, 1, facets);
const auto bdofs = fem::locate_dofs_topological(
V->mesh()->topology_mutable(), *V->dofmap(), 1, facets);
auto bc = std::make_shared<const fem::DirichletBC<T>>(0.0, bdofs, V);

// Now, we have specified the variational forms and can consider the
Expand Down Expand Up @@ -249,9 +251,9 @@ int main(int argc, char* argv[])

b.set(0.0);
fem::assemble_vector(b.mutable_array(), *L);
fem::apply_lifting(b.mutable_array(), {a}, {{bc}}, {}, T(1.0));
fem::apply_lifting<T, double>(b.mutable_array(), {a}, {{bc}}, {}, T(1.0));
b.scatter_rev(std::plus<T>());
fem::set_bc(b.mutable_array(), {bc});
fem::set_bc<T, double>(b.mutable_array(), {bc});

la::petsc::KrylovSolver lu(MPI_COMM_WORLD);
la::petsc::options::set("ksp_type", "preonly");
Expand Down
18 changes: 10 additions & 8 deletions cpp/demo/hyperelasticity/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ int main(int argc, char* argv[])
// .. code-block:: cpp

// Create mesh and define function space
auto mesh = std::make_shared<mesh::Mesh>(
auto mesh = std::make_shared<mesh::Mesh<double>>(
mesh::create_box(MPI_COMM_WORLD, {{{0.0, 0.0, 0.0}, {1.0, 1.0, 1.0}}},
{10, 10, 10}, mesh::CellType::tetrahedron,
mesh::create_cell_partitioner(mesh::GhostMode::none)));

auto V = std::make_shared<fem::FunctionSpace>(fem::create_functionspace(
functionspace_form_hyperelasticity_F_form, "u", mesh));
auto V = std::make_shared<fem::FunctionSpace<double>>(
fem::create_functionspace(functionspace_form_hyperelasticity_F_form,
"u", mesh));

// Define solution function
auto u = std::make_shared<fem::Function<T>>(V);
Expand Down Expand Up @@ -178,7 +179,7 @@ int main(int argc, char* argv[])

// Create Dirichlet boundary conditions
auto bdofs_left = fem::locate_dofs_geometrical(
{*V},
*V,
[](auto x)
{
constexpr double eps = 1.0e-8;
Expand All @@ -191,7 +192,7 @@ int main(int argc, char* argv[])
return marker;
});
auto bdofs_right = fem::locate_dofs_geometrical(
{*V},
*V,
[](auto x)
{
constexpr double eps = 1.0e-8;
Expand Down Expand Up @@ -228,10 +229,11 @@ int main(int argc, char* argv[])
const basix::FiniteElement S_element = basix::create_element(
family, cell_type, k, basix::element::lagrange_variant::unset,
basix::element::dpc_variant::unset, discontinuous);
auto S = std::make_shared<fem::FunctionSpace>(fem::create_functionspace(
mesh, S_element, pow(mesh->geometry().dim(), 2)));
auto S = std::make_shared<fem::FunctionSpace<double>>(
fem::create_functionspace(mesh, S_element,
pow(mesh->geometry().dim(), 2)));

const auto sigma_expression = fem::create_expression<T>(
auto sigma_expression = fem::create_expression<T, double>(
*expression_hyperelasticity_sigma, {{"u", u}}, {}, mesh);

auto sigma = fem::Function<T>(S);
Expand Down
59 changes: 38 additions & 21 deletions cpp/demo/interpolation-io/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <dolfinx/mesh/Mesh.h>
#include <dolfinx/mesh/cell_types.h>
#include <dolfinx/mesh/generation.h>
#include <dolfinx/mesh/utils.h>
#include <filesystem>
#include <mpi.h>
#include <numbers>
Expand All @@ -24,9 +25,9 @@ using namespace dolfinx;
// This function interpolations a function is a finite element space and
// outputs the finite element function to a VTK file for visualisation.
// It also shows how to create a finite element using Basix.
template <typename T>
void interpolate_scalar(std::shared_ptr<mesh::Mesh> mesh,
std::filesystem::path filename)
template <typename T, typename U>
void interpolate_scalar(std::shared_ptr<mesh::Mesh<U>> mesh,
[[maybe_unused]] std::filesystem::path filename)
{
// Create a Basix continuous Lagrange element of degree 1
basix::FiniteElement e = basix::create_element(
Expand All @@ -36,7 +37,7 @@ void interpolate_scalar(std::shared_ptr<mesh::Mesh> mesh,
basix::element::dpc_variant::unset, false);

// Create a scalar function space
auto V = std::make_shared<fem::FunctionSpace>(
auto V = std::make_shared<fem::FunctionSpace<U>>(
fem::create_functionspace(mesh, e, 1));

// Create a finite element Function
Expand All @@ -53,18 +54,21 @@ void interpolate_scalar(std::shared_ptr<mesh::Mesh> mesh,
return {f, {f.size()}};
});

// Write the function to a VTK file for visualisation, e.g. using
#ifdef HAS_ADIOS2
// Write the function to a VTX file for visualisation, e.g. using
// ParaView
io::VTKFile file(mesh->comm(), filename.replace_extension("pvd"), "w");
file.write<T>({*u}, 0.0);
io::VTXWriter<U> outfile(mesh->comm(), filename.replace_extension("bp"), {u});
outfile.write(0.0);
outfile.close();
#endif
}

// This function interpolations a function is a H(curl) finite element
// space. To visualise the function, it interpolates the H(curl) finite
// element function in a discontinuous Lagrange space and outputs the
// Lagrange finite element function to a VTX file for visualisation.
template <typename T>
void interpolate_nedelec(std::shared_ptr<mesh::Mesh> mesh,
template <typename T, typename U>
void interpolate_nedelec(std::shared_ptr<mesh::Mesh<U>> mesh,
[[maybe_unused]] std::filesystem::path filename)
{
// Create a Basix Nedelec (first kind) element of degree 2 (dim=6 on triangle)
Expand All @@ -75,7 +79,7 @@ void interpolate_nedelec(std::shared_ptr<mesh::Mesh> mesh,
basix::element::dpc_variant::unset, false);

// Create a Nedelec function space
auto V = std::make_shared<fem::FunctionSpace>(
auto V = std::make_shared<fem::FunctionSpace<U>>(
fem::create_functionspace(mesh, e, 1));

// Create a Nedelec finite element Function
Expand Down Expand Up @@ -145,7 +149,7 @@ void interpolate_nedelec(std::shared_ptr<mesh::Mesh> mesh,
basix::element::dpc_variant::unset, true);

// Create a function space
auto V_l = std::make_shared<fem::FunctionSpace>(
auto V_l = std::make_shared<fem::FunctionSpace<U>>(
fem::create_functionspace(mesh, e_l, 2));

auto u_l = std::make_shared<fem::Function<T>>(V_l);
Expand All @@ -154,13 +158,14 @@ void interpolate_nedelec(std::shared_ptr<mesh::Mesh> mesh,
// space:
u_l->interpolate(*u);

// Output the discontinuous Lagrange space in VTK format. When
// Output the discontinuous Lagrange space in VTX format. When
// plotting the x0 component the field will appear discontinuous at x0
// = 0.5 (jump in the normal component between cells) and the x1
// component will appear continuous (continuous tangent component
// between cells).
#ifdef HAS_ADIOS2
io::VTXWriter outfile(mesh->comm(), filename.replace_extension("bp"), {u_l});
io::VTXWriter<U> outfile(mesh->comm(), filename.replace_extension("bp"),
{u_l});
outfile.write(0.0);
outfile.close();
#endif
Expand All @@ -179,21 +184,33 @@ int main(int argc, char* argv[])
{
// Create a mesh. For what comes later in this demo we need to
// ensure that a boundary between cells is located at x0=0.5
auto mesh = std::make_shared<mesh::Mesh>(mesh::create_rectangle(
MPI_COMM_WORLD, {{{0.0, 0.0}, {1.0, 1.0}}}, {32, 4},
mesh::CellType::triangle,
mesh::create_cell_partitioner(mesh::GhostMode::none)));

auto mesh0
jorgensd marked this conversation as resolved.
Show resolved Hide resolved
= std::make_shared<mesh::Mesh<float>>(mesh::create_rectangle<float>(
MPI_COMM_WORLD, {{{0.0, 0.0}, {1.0, 1.0}}}, {32, 4},
mesh::CellType::triangle,
mesh::create_cell_partitioner(mesh::GhostMode::none)));

auto mesh1
= std::make_shared<mesh::Mesh<double>>(mesh::create_rectangle<double>(
MPI_COMM_WORLD, {{{0.0, 0.0}, {1.0, 1.0}}}, {32, 4},
mesh::CellType::triangle,
mesh::create_cell_partitioner(mesh::GhostMode::none)));

// Interpolate a function in a scalar Lagrange space and output the
// result to file for visualisation
interpolate_scalar<double>(mesh, "u");
interpolate_scalar<std::complex<double>>(mesh, "u_complex");
interpolate_scalar<float>(mesh0, "u32");
interpolate_scalar<double>(mesh1, "u64");
interpolate_scalar<std::complex<float>>(mesh0, "u_complex64");
interpolate_scalar<std::complex<double>>(mesh1, "u_complex128");

// Interpolate a function in a H(curl) finite element space, and
// then interpolate the H(curl) function in a discontinuous Lagrange
// space for visualisation
interpolate_nedelec<double>(mesh, "u_nedelec");
interpolate_nedelec<std::complex<double>>(mesh, "u_nedelec_complex");
interpolate_nedelec<float>(mesh0, "u_nedelec32");
interpolate_nedelec<double>(mesh1, "u_nedelec64");
interpolate_nedelec<std::complex<float>>(mesh0, "u_nedelec_complex64");
interpolate_nedelec<std::complex<double>>(mesh1, "u_nedelec_complex12");
}

MPI_Finalize();
Expand Down
20 changes: 14 additions & 6 deletions cpp/demo/interpolation_different_meshes/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,25 @@ int main(int argc, char* argv[])
MPI_Comm comm{MPI_COMM_WORLD};

// Create a tetrahedral mesh
auto mesh_tet = std::make_shared<mesh::Mesh>(
auto mesh_tet = std::make_shared<mesh::Mesh<double>>(
mesh::create_box(comm, {{{0, 0, 0}, {1, 1, 1}}}, {20, 20, 20},
mesh::CellType::tetrahedron));

// Create a hexahedral mesh
auto mesh_hex = std::make_shared<mesh::Mesh>(
auto mesh_hex = std::make_shared<mesh::Mesh<double>>(
mesh::create_box(comm, {{{0, 0, 0}, {1, 1, 1}}}, {15, 15, 15},
mesh::CellType::hexahedron));

basix::FiniteElement element_tet = basix::element::create_lagrange(
mesh::cell_type_to_basix_type(mesh_tet->topology().cell_type()), 1,
basix::element::lagrange_variant::equispaced, false);
auto V_tet = std::make_shared<fem::FunctionSpace>(
auto V_tet = std::make_shared<fem::FunctionSpace<double>>(
fem::create_functionspace(mesh_tet, element_tet, 3));

basix::FiniteElement element_hex = basix::element::create_lagrange(
mesh::cell_type_to_basix_type(mesh_hex->topology().cell_type()), 2,
basix::element::lagrange_variant::equispaced, false);
auto V_hex = std::make_shared<fem::FunctionSpace>(
auto V_hex = std::make_shared<fem::FunctionSpace<double>>(
fem::create_functionspace(mesh_hex, element_hex, 3));

auto u_tet = std::make_shared<fem::Function<T>>(V_tet);
Expand Down Expand Up @@ -72,11 +73,18 @@ int main(int argc, char* argv[])
u_hex->interpolate(*u_tet, nmm_interpolation_data);

#ifdef HAS_ADIOS2
io::VTXWriter write_tet(mesh_tet->comm(), "u_tet.vtx", {u_tet});
io::VTXWriter<double> write_tet(mesh_tet->comm(), "u_tet.vtx", {u_tet});
write_tet.write(0.0);

io::VTXWriter write_hex(mesh_hex->comm(), "u_hex.vtx", {u_hex});
io::VTXWriter<double> write_hex(mesh_hex->comm(), "u_hex.vtx", {u_hex});
jorgensd marked this conversation as resolved.
Show resolved Hide resolved
write_hex.write(0.0);

auto mesh
= std::make_shared<mesh::Mesh<float>>(mesh::create_rectangle<float>(
comm, {{{0, 0}, {1, 1}}}, {20, 20}, mesh::CellType::triangle));
io::VTXWriter write_msh(mesh->comm(), "foo.vtx", mesh);
write_msh.write(0.0);
jorgensd marked this conversation as resolved.
Show resolved Hide resolved

#endif
}
MPI_Finalize();
Expand Down
20 changes: 11 additions & 9 deletions cpp/demo/poisson/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ int main(int argc, char* argv[])
{
// Create mesh and function space
auto part = mesh::create_cell_partitioner(mesh::GhostMode::shared_facet);
auto mesh = std::make_shared<mesh::Mesh>(
auto mesh = std::make_shared<mesh::Mesh<double>>(
mesh::create_rectangle(MPI_COMM_WORLD, {{{0.0, 0.0}, {2.0, 1.0}}},
{32, 16}, mesh::CellType::triangle, part));

auto V = std::make_shared<fem::FunctionSpace>(
auto V = std::make_shared<fem::FunctionSpace<double>>(
fem::create_functionspace(functionspace_form_poisson_a, "u", mesh));

// Next, we define the variational formulation by initializing the
Expand Down Expand Up @@ -166,17 +166,19 @@ int main(int argc, char* argv[])
*mesh, 1,
[](auto x)
{
constexpr double eps = 1.0e-8;
using U = typename decltype(x)::value_type;
constexpr U eps = 1.0e-8;
std::vector<std::int8_t> marker(x.extent(1), false);
for (std::size_t p = 0; p < x.extent(1); ++p)
{
double x0 = x(0, p);
auto x0 = x(0, p);
if (std::abs(x0) < eps or std::abs(x0 - 2) < eps)
marker[p] = true;
}
return marker;
});
const auto bdofs = fem::locate_dofs_topological({*V}, 1, facets);
const auto bdofs = fem::locate_dofs_topological(
V->mesh()->topology_mutable(), *V->dofmap(), 1, facets);
auto bc = std::make_shared<const fem::DirichletBC<T>>(0.0, bdofs, V);

f->interpolate(
Expand All @@ -185,8 +187,8 @@ int main(int argc, char* argv[])
std::vector<T> f;
for (std::size_t p = 0; p < x.extent(1); ++p)
{
double dx = (x(0, p) - 0.5) * (x(0, p) - 0.5);
double dy = (x(1, p) - 0.5) * (x(1, p) - 0.5);
auto dx = (x(0, p) - 0.5) * (x(0, p) - 0.5);
auto dy = (x(1, p) - 0.5) * (x(1, p) - 0.5);
f.push_back(10 * std::exp(-(dx + dy) / 0.02));
}

Expand Down Expand Up @@ -229,9 +231,9 @@ int main(int argc, char* argv[])

b.set(0.0);
fem::assemble_vector(b.mutable_array(), *L);
fem::apply_lifting(b.mutable_array(), {a}, {{bc}}, {}, T(1));
fem::apply_lifting<T, double>(b.mutable_array(), {a}, {{bc}}, {}, T(1));
b.scatter_rev(std::plus<T>());
fem::set_bc(b.mutable_array(), {bc});
fem::set_bc<T, double>(b.mutable_array(), {bc});

la::petsc::KrylovSolver lu(MPI_COMM_WORLD);
la::petsc::options::set("ksp_type", "preonly");
Expand Down
Loading