-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow specifying attribute name when reading meshtag (#3257)
* Safe default value for unbound variables in dolfinx.conf * Allow to specify attribute name when reading meshtag * Add python interface * Disambiguate functions * Fix Python wrapper * Applying review changes * Linting * Only one function in the python wrapper and interface * Applied code review changes * Added unit test * Fix comments * Removed unused variables * Using ASCII file to circumvent CI issue * Specify ASCII encoding * Fix a bug and create mesh within test * Dodge issue 3316 * Applied review suggestions * Fix missing renaming * Fix python wrapper * Simplify code * Update cpp/dolfinx/io/XDMFFile.h Co-authored-by: Jørgen Schartum Dokken <[email protected]> * Update cpp/dolfinx/io/XDMFFile.h Co-authored-by: Jørgen Schartum Dokken <[email protected]> * Sprinkling some const * Apply revision * Fix doc * Fix doc * Ruff format * Const and ref fixes * Ruff fixes * Update python/dolfinx/io/utils.py Co-authored-by: Jørgen Schartum Dokken <[email protected]> * Add type hints * Fix dispatch * Fixes * Add missing include * Tidy cmake order * Minor fixes * Added test * Ruff fixes * Ruff fixes --------- Co-authored-by: Jørgen Schartum Dokken <[email protected]> Co-authored-by: Garth N. Wells <[email protected]>
- Loading branch information
1 parent
c0e48a1
commit 13bc37f
Showing
7 changed files
with
189 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (C) 2024 Massimiliano Leoni | ||
// | ||
// This file is part of DOLFINx (https://www.fenicsproject.org) | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
// | ||
// Unit tests for reading meshtags by name | ||
|
||
#include <catch2/catch_template_test_macros.hpp> | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <dolfinx/common/IndexMap.h> | ||
#include <dolfinx/common/MPI.h> | ||
#include <dolfinx/io/XDMFFile.h> | ||
#include <dolfinx/la/Vector.h> | ||
#include <dolfinx/mesh/MeshTags.h> | ||
#include <dolfinx/mesh/generation.h> | ||
#include <dolfinx/mesh/utils.h> | ||
#include <numeric> | ||
#include <string> | ||
|
||
using namespace dolfinx; | ||
|
||
namespace | ||
{ | ||
void test_read_named_meshtags() | ||
{ | ||
const std::string mesh_file_name = "Domain.xdmf"; | ||
constexpr std::int32_t domain_value = 1; | ||
constexpr std::int32_t material_value = 2; | ||
|
||
// Create mesh | ||
auto part = mesh::create_cell_partitioner(mesh::GhostMode::none); | ||
auto mesh = std::make_shared<mesh::Mesh<double>>( | ||
mesh::create_rectangle(MPI_COMM_WORLD, {{{0.0, 0.0}, {1.0, 1.0}}}, {3, 3}, | ||
mesh::CellType::triangle, part)); | ||
|
||
const std::int32_t n_cells = mesh->topology()->index_map(2)->size_local(); | ||
std::vector<std::int32_t> indices(n_cells); | ||
std::iota(std::begin(indices), std::end(indices), 0); | ||
|
||
std::vector<std::int32_t> domain_values(n_cells, domain_value); | ||
std::vector<std::int32_t> material_values(n_cells, material_value); | ||
|
||
mesh::MeshTags<std::int32_t> mt_domains(mesh->topology(), 2, indices, | ||
domain_values); | ||
mt_domains.name = "domain"; | ||
|
||
mesh::MeshTags<std::int32_t> mt_materials(mesh->topology(), 2, indices, | ||
material_values); | ||
mt_materials.name = "material"; | ||
|
||
io::XDMFFile file(mesh->comm(), mesh_file_name, "w", io::XDMFFile::Encoding::HDF5); | ||
file.write_mesh(*mesh); | ||
file.write_meshtags(mt_domains, mesh->geometry(), | ||
"/Xdmf/Domain/mesh/Geometry"); | ||
file.write_meshtags(mt_materials, mesh->geometry(), | ||
"/Xdmf/Domain/Grid/Geometry"); | ||
file.close(); | ||
|
||
io::XDMFFile mesh_file(MPI_COMM_WORLD, mesh_file_name, "r", | ||
io::XDMFFile::Encoding::HDF5); | ||
mesh = std::make_shared<mesh::Mesh<double>>(mesh_file.read_mesh( | ||
fem::CoordinateElement<double>(mesh::CellType::triangle, 1), | ||
mesh::GhostMode::none, "mesh")); | ||
|
||
mesh::MeshTags<std::int32_t> mt_first | ||
= mesh_file.read_meshtags(*mesh, "material", {}); | ||
CHECK(mt_first.values().front() == material_value); | ||
|
||
mesh::MeshTags<std::int32_t> mt_domain | ||
= mesh_file.read_meshtags(*mesh, "domain", "domain", "/Xdmf/Domain"); | ||
CHECK(mt_domain.values().front() == domain_value); | ||
|
||
mesh::MeshTags<std::int32_t> mt_material | ||
= mesh_file.read_meshtags(*mesh, "material", "material", "/Xdmf/Domain"); | ||
CHECK(mt_material.values().front() == material_value); | ||
|
||
CHECK_THROWS(mesh_file.read_meshtags(*mesh, "mesh", "missing")); | ||
mesh_file.close(); | ||
} | ||
} // namespace | ||
|
||
TEST_CASE("Read meshtag by name", "[read_meshtag_by_name]") | ||
{ | ||
test_read_named_meshtags(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters