Skip to content

Commit

Permalink
Merge branch 'AddMaterialIDOptionToAddLayer' into 'master'
Browse files Browse the repository at this point in the history
[Tool] Add material id option to AddLayer

See merge request ogs/ogs!5040
  • Loading branch information
endJunction committed Aug 6, 2024
2 parents 2553cc3 + 3c46855 commit fc4d513
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Applications/DataExplorer/DataView/MeshView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ void MeshView::openAddLayerDialog()
bool const copy_material_ids = false;
double const thickness(dlg.getThickness());
std::unique_ptr<MeshLib::Mesh> result(MeshToolsLib::addLayerToMesh(
*mesh, thickness, dlg.getName(), dlg.isTopLayer(), copy_material_ids));
*mesh, thickness, dlg.getName(), dlg.isTopLayer(), copy_material_ids,
std::nullopt));

if (result)
{
Expand Down
23 changes: 22 additions & 1 deletion Applications/Utils/MeshEdit/AddLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,22 @@ int main(int argc, char* argv[])
false);
cmd.add(copy_material_ids_arg);

TCLAP::ValueArg<int> set_material_arg("", "set-material-id",
"the material id of the new layer",
false, 0, "integer value");
cmd.add(set_material_arg);
cmd.parse(argc, argv);

if (set_material_arg.isSet() && copy_material_ids_arg.isSet())
{
ERR("It is not possible to set both options '--copy-material-ids' and "
"'--set-material-id'.");
#ifdef USE_PETSC
MPI_Finalize();
#endif
return EXIT_FAILURE;
}

#ifdef USE_PETSC
MPI_Init(&argc, &argv);
#endif
Expand All @@ -87,9 +101,16 @@ int main(int argc, char* argv[])
}
INFO("done.");

std::optional<int> layer_id;
if (set_material_arg.isSet())
{
layer_id = set_material_arg.getValue();
}

std::unique_ptr<MeshLib::Mesh> result(MeshToolsLib::addLayerToMesh(
*subsfc_mesh, layer_thickness_arg.getValue(), mesh_out_arg.getValue(),
layer_position_arg.getValue(), copy_material_ids_arg.getValue()));
layer_position_arg.getValue(), copy_material_ids_arg.getValue(),
layer_id));
if (!result)
{
ERR("Failure while adding layer.");
Expand Down
59 changes: 59 additions & 0 deletions Applications/Utils/Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,65 @@ AddTest(
DIFF_DATA AREHS_3D_AABB_inverted.vtu AREHS_3D_AABB_inverted.vtu 1.e-16
)

AddTest(
NAME AddLayer_test_set-material-id_option
PATH MeshGeoToolsLib/Naegelstedt
WORKING_DIRECTORY ${Data_SOURCE_DIR}/<PATH>
EXECUTABLE AddLayer
EXECUTABLE_ARGS -i SmallTest.vtu
--add-layer-on-bottom
-t 10
--set-material-id 10
-o ${Data_BINARY_DIR}/<PATH>/SmallTest_WithAdditionalBottomLayer.vtu
REQUIREMENTS NOT (OGS_USE_MPI)
TESTER vtkdiff-mesh
DIFF_DATA SmallTest_WithAdditionalBottomLayer.vtu SmallTest_WithAdditionalBottomLayer.vtu 1.e-16
)

AddTest( # with option --copy-material_ids instead of --set-material-id
NAME AddLayer_test_copy-material-ids_option
PATH MeshGeoToolsLib/Naegelstedt
WORKING_DIRECTORY ${Data_SOURCE_DIR}/<PATH>
EXECUTABLE AddLayer
EXECUTABLE_ARGS -i SmallTest.vtu
--add-layer-on-bottom
-t 10
--copy-material-ids
-o ${Data_BINARY_DIR}/<PATH>/SmallTest_WithAdditionalBottomLayer.vtu
REQUIREMENTS NOT (OGS_USE_MPI)
TESTER vtkdiff-mesh
DIFF_DATA SmallTest_WithAdditionalBottomLayer.vtu SmallTest_WithAdditionalBottomLayer.vtu 1.e-16
)

AddTest( # without options --copy-material_ids and --set-material-id
NAME AddLayer_test_without_copy-material-ids_and_set-material-id_options
PATH MeshGeoToolsLib/Naegelstedt
WORKING_DIRECTORY ${Data_SOURCE_DIR}/<PATH>
EXECUTABLE AddLayer
EXECUTABLE_ARGS -i SmallTest.vtu
--add-layer-on-bottom
-t 10
-o ${Data_BINARY_DIR}/<PATH>/SmallTest_WithAdditionalBottomLayer.vtu
REQUIREMENTS NOT (OGS_USE_MPI)
TESTER vtkdiff-mesh
DIFF_DATA SmallTest_WithAdditionalBottomLayer.vtu SmallTest_WithAdditionalBottomLayer.vtu 1.e-16
)
AddTest( # with options --copy-material_ids and --set-material-id 10 should fail
NAME AddLayer_test_set-material-id_and_copy-material-ids_failing
PATH MeshGeoToolsLib/Naegelstedt
WORKING_DIRECTORY ${Data_SOURCE_DIR}/<PATH>
EXECUTABLE AddLayer
EXECUTABLE_ARGS -i SmallTest.vtu
--add-layer-on-bottom
-t 10
--set-material-id 10
--copy-material-ids
-o ${Data_BINARY_DIR}/<PATH>/SmallTest_WithAdditionalBottomLayer.vtu
REQUIREMENTS NOT (OGS_USE_MPI)
PROPERTIES
PASS_REGULAR_EXPRESSION "It is not possible to set both options"
)

if(OGS_USE_PETSC)
NotebookTest(NOTEBOOKFILE Utils/partmesh/partmesh_roundtrip.md RUNTIME 10 SKIP_WEB)
endif()
14 changes: 9 additions & 5 deletions MeshToolsLib/MeshEditing/AddLayerToMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ MeshLib::Element* extrudeElement(
return nullptr;
}

MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double thickness,
std::string const& name, bool on_top,
bool copy_material_ids)
MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double const thickness,
std::string const& name, bool const on_top,
bool const copy_material_ids,
std::optional<int> const layer_id)
{
INFO("Extracting top surface of mesh '{:s}' ... ", mesh.getName());
double const flag = on_top ? -1.0 : 1.0;
Expand Down Expand Up @@ -199,8 +200,11 @@ MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double thickness,
}
else
{
int const new_layer_id(
*(std::max_element(materials->cbegin(), materials->cend())) + 1);
int const new_layer_id =
layer_id.has_value()
? layer_id.value()
: *(std::max_element(materials->cbegin(), materials->cend())) +
1;
auto const n_new_props(subsfc_elements.size() -
mesh.getNumberOfElements());
std::fill_n(std::back_inserter(*new_materials), n_new_props,
Expand Down
8 changes: 5 additions & 3 deletions MeshToolsLib/MeshEditing/AddLayerToMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#pragma once

#include <optional>
#include <string>
#include <vector>

Expand All @@ -30,8 +31,9 @@ namespace MeshToolsLib

/// Adds a layer to the mesh. If on_top is true, the layer is added on top,
/// if it is false, the layer is added at the bottom.
MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double thickness,
std::string const& name, bool on_top,
bool copy_material_ids);
MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double const thickness,
std::string const& name, bool const on_top,
bool const copy_material_ids,
std::optional<int> const layer_id);

} // namespace MeshToolsLib
3 changes: 2 additions & 1 deletion MeshToolsLib/MeshGenerators/MeshGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,8 @@ MeshLib::Mesh* MeshGenerator::generateRegularPrismMesh(
for (std::size_t i = 0; i < n_z_cells; ++i)
{
mesh.reset(MeshToolsLib::addLayerToMesh(*mesh, cell_size_z, mesh_name,
true, copy_material_ids));
true, copy_material_ids,
std::nullopt));
}
std::vector<std::size_t> elem_ids(n_tris);
std::iota(elem_ids.begin(), elem_ids.end(), 0);
Expand Down

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions Tests/MeshLib/TestAddLayerToMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ TEST(MeshLib, AddTopLayerToLineMesh)
double const height(1);
constexpr bool const copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh, height, "mesh", true, copy_material_ids));
*mesh, height, "mesh", true, copy_material_ids, std::nullopt));

ASSERT_EQ(2 * mesh->getNumberOfNodes(), result->getNumberOfNodes());
ASSERT_EQ(2 * mesh->getNumberOfElements(), result->getNumberOfElements());
Expand All @@ -96,7 +96,7 @@ TEST(MeshLib, AddBottomLayerToLineMesh)
double const height(1);
constexpr bool const copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh, height, "mesh", false, copy_material_ids));
*mesh, height, "mesh", false, copy_material_ids, std::nullopt));

ASSERT_EQ(2 * mesh->getNumberOfNodes(), result->getNumberOfNodes());
ASSERT_EQ(2 * mesh->getNumberOfElements(), result->getNumberOfElements());
Expand Down Expand Up @@ -131,7 +131,7 @@ TEST(MeshLib, AddTopLayerToTriMesh)
double const height(1);
constexpr bool copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh, height, "mesh", true, copy_material_ids));
*mesh, height, "mesh", true, copy_material_ids, std::nullopt));

ASSERT_EQ(2 * mesh->getNumberOfNodes(), result->getNumberOfNodes());
ASSERT_EQ(2 * mesh->getNumberOfElements(), result->getNumberOfElements());
Expand Down Expand Up @@ -162,7 +162,7 @@ TEST(MeshLib, AddBottomLayerToTriMesh)
double const height(1);
constexpr bool copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh, height, "mesh", false, copy_material_ids));
*mesh, height, "mesh", false, copy_material_ids, std::nullopt));

ASSERT_EQ(2 * mesh->getNumberOfNodes(), result->getNumberOfNodes());
ASSERT_EQ(2 * mesh->getNumberOfElements(), result->getNumberOfElements());
Expand All @@ -183,7 +183,7 @@ TEST(MeshLib, AddTopLayerToQuadMesh)
double const height(1);
constexpr bool copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh, height, "mesh", true, copy_material_ids));
*mesh, height, "mesh", true, copy_material_ids, std::nullopt));

ASSERT_EQ(2 * mesh->getNumberOfNodes(), result->getNumberOfNodes());
ASSERT_EQ(2 * mesh->getNumberOfElements(), result->getNumberOfElements());
Expand All @@ -204,7 +204,7 @@ TEST(MeshLib, AddBottomLayerToQuadMesh)
double const height(1);
constexpr bool copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh, height, "mesh", false, copy_material_ids));
*mesh, height, "mesh", false, copy_material_ids, std::nullopt));

ASSERT_EQ(2 * mesh->getNumberOfNodes(), result->getNumberOfNodes());
ASSERT_EQ(2 * mesh->getNumberOfElements(), result->getNumberOfElements());
Expand All @@ -225,7 +225,7 @@ TEST(MeshLib, AddTopLayerToHexMesh)
double const height(1);
constexpr bool copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh, height, "mesh", true, copy_material_ids));
*mesh, height, "mesh", true, copy_material_ids, std::nullopt));

ASSERT_EQ(mesh->getNumberOfNodes(), result->getNumberOfNodes() - 36);
ASSERT_EQ(mesh->getNumberOfElements(), result->getNumberOfElements() - 25);
Expand All @@ -250,7 +250,7 @@ TEST(MeshLib, AddBottomLayerToHexMesh)
double const height(1);
constexpr bool copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh, height, "mesh", false, copy_material_ids));
*mesh, height, "mesh", false, copy_material_ids, std::nullopt));

ASSERT_EQ(mesh->getNumberOfNodes(), result->getNumberOfNodes() - 36);
ASSERT_EQ(mesh->getNumberOfElements(), result->getNumberOfElements() - 25);
Expand All @@ -274,10 +274,10 @@ TEST(MeshLib, AddTopLayerToPrismMesh)
MeshToolsLib::MeshGenerator::generateRegularTriMesh(5, 5));
constexpr bool copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const mesh2(MeshToolsLib::addLayerToMesh(
*mesh, 5, "mesh", true, copy_material_ids));
*mesh, 5, "mesh", true, copy_material_ids, std::nullopt));
double const height(1);
std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh2, height, "mesh", true, copy_material_ids));
*mesh2, height, "mesh", true, copy_material_ids, std::nullopt));

ASSERT_EQ(mesh2->getNumberOfNodes() / 2.0 * 3, result->getNumberOfNodes());
ASSERT_EQ(mesh2->getNumberOfElements() / 2.0 * 3,
Expand All @@ -303,7 +303,7 @@ TEST(MeshLib, AddBottomLayerToPrismMesh)
MeshToolsLib::MeshGenerator::generateRegularTriMesh(5, 5));
constexpr bool copy_material_ids = false;
std::unique_ptr<MeshLib::Mesh> const mesh2(MeshToolsLib::addLayerToMesh(
*mesh, 5, "mesh", true, copy_material_ids));
*mesh, 5, "mesh", true, copy_material_ids, std::nullopt));
double const height(1);
std::string const& mat_name("MaterialIDs");
auto* const mats = mesh2->getProperties().createNewPropertyVector<int>(
Expand All @@ -314,7 +314,7 @@ TEST(MeshLib, AddBottomLayerToPrismMesh)
}

std::unique_ptr<MeshLib::Mesh> const result(MeshToolsLib::addLayerToMesh(
*mesh2, height, "mesh", false, copy_material_ids));
*mesh2, height, "mesh", false, copy_material_ids, std::nullopt));
ASSERT_EQ(mesh2->getNumberOfNodes() / 2.0 * 3, result->getNumberOfNodes());
ASSERT_EQ(mesh2->getNumberOfElements() / 2.0 * 3,
result->getNumberOfElements());
Expand Down
8 changes: 6 additions & 2 deletions web/content/docs/tools/meshing/addlayer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ returns the newly generated mesh `output-mesh` that has an new layer.

One might want to take care that the material groups are reduced, e.g. material
groups should not be [0,2,5], but [0,1,2]. The new layer will have the material
group number of the highest material group +1. With the switch
`--copy-material-ids` the MaterialIDs of the extruded layer will be kept.
group ID of the highest material group +1 if not otherwise specified. With
the switch `--copy-material-ids` the material group IDs of the extruded layer will be
kept. With the switch `--set-material-id` the material group id of the new layer can be set
to a different value than the default value of 'highest material group number plus one'.

The tool requires the [OGS-6 node ordering](http://doxygen.opengeosys.org/index.html) in the elements. A different node ordering may lead to unexpected results. In case one might try to change the ordering using the tool `NodeReordering`.

Expand All @@ -41,3 +43,5 @@ AddLayer -i quad.vtu -o quad_with_new_top_layer.vtu -t 1
The tool was used to add a "soil" layer to the hydro-geological model of the Unstrut catchment within the INFLUINS project:

{{< bib "fischer:2015" >}}

The tool is used in the OGS-GIScape workflow.

0 comments on commit fc4d513

Please sign in to comment.