Skip to content

Commit f056f7f

Browse files
authored
[GeoMechanicsApplication] Improve the initialization of the list of sub-modelparts related to processes (#13913)
* added loop over processes in processes section * added check with processes_sub_model_part_list * cleaned a bit * fixed test * added check for processes * removed modelpart name * excluded modelpart name * added empty parameters * removed LINE_LOAD in STage 3 * removed debugging statements * added StringHash to remove a code smell * introduced collect_ids_from_part * reduced complexity again * made a lamda function as a free function * used std::string_view * removed const for std::string_view * passed complete project parameters * reverted changes, added new processes_sub_model_part_list * extracted function _AddProcessesSubModelPartList * removed one-line processes_sub_model_part_list * removed multi-line problem_domain_sub_model_part_list * removed processes_sub_model_part_list from unit tests * used python work flow in C++ * moved AddProcessesSubModelPartList to process_utilities * used AddProcessesSubModelPartList in python script * used Kratos style * used Kratos style 2.0 * added unit tests * adapted unit test * changed checking in the test * removed code smells * response to the reviews * removed code smells * made GenericUtilities::CollectIdsFromEntity * used static_assert * response review 2.0 * response to Anne's review * used iterator for GetIdsFromEntityContents * removed code smell
1 parent 5116b99 commit f056f7f

428 files changed

Lines changed: 267 additions & 799 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

applications/GeoMechanicsApplication/custom_elements/interface_element.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "custom_utilities/element_utilities.hpp"
1818
#include "custom_utilities/equation_of_motion_utilities.h"
1919
#include "custom_utilities/extrapolation_utilities.h"
20+
#include "custom_utilities/generic_utilities.h"
2021
#include "custom_utilities/geometry_utilities.h"
2122
#include "custom_utilities/math_utilities.h"
2223
#include "geo_aliases.h"
@@ -194,7 +195,7 @@ void InterfaceElement::Initialize(const ProcessInfo& rCurrentProcessInfo)
194195
// interface element can at maximum have 2 neighbours
195196
KRATOS_DEBUG_ERROR_IF(this->GetValue(NEIGHBOUR_ELEMENTS).size() > 2)
196197
<< "Too many neighbour elements for interface element " << this->Id() << std::endl;
197-
const auto interface_node_ids = GeometryUtilities::GetNodeIdsFromGeometry(GetGeometry());
198+
const auto interface_node_ids = GenericUtilities::GetIdsFromEntityContents(GetGeometry());
198199
std::vector<std::optional<Vector>> interface_nodal_cauchy_stresses(interface_node_ids.size());
199200
auto& r_neighbour_element = this->GetValue(NEIGHBOUR_ELEMENTS).front();
200201
std::vector<Vector> neighbour_cauchy_stresses;

applications/GeoMechanicsApplication/custom_python/add_custom_utilities_to_python.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "custom_python/add_custom_utilities_to_python.h"
1818

1919
#include "custom_utilities/node_utilities.h"
20+
#include "custom_utilities/process_utilities.h"
2021
#include "custom_workflows/custom_workflow_factory.h"
2122
#include "custom_workflows/dgeoflow.h"
2223
#include "custom_workflows/dgeosettlement.h"
@@ -43,6 +44,11 @@ void AddCustomUtilitiesToPython(const pybind11::module& rModule)
4344
pybind11::class_<KratosExecute::CallBackFunctions>(rModule, "KratosExecuteCallBackFunctions")
4445
.def(pybind11::init<std::function<void(const char*)>, std::function<void(double)>,
4546
std::function<void(const char*)>, std::function<bool()>>());
47+
48+
pybind11::class_<ProcessUtilities>(rModule, "ProcessUtilities")
49+
.def_static("AddProcessesSubModelPartListToSolverSettings",
50+
&ProcessUtilities::AddProcessesSubModelPartListToSolverSettings,
51+
pybind11::arg("project_parameters"), pybind11::arg("solver_settings"));
4652
}
4753

4854
} // Namespace Kratos::Python.

applications/GeoMechanicsApplication/custom_utilities/extrapolation_utilities.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
#include "custom_utilities/extrapolation_utilities.h"
1515

16+
#include "custom_utilities/generic_utilities.h"
1617
#include "custom_utilities/linear_nodal_extrapolator.h"
17-
#include "geometry_utilities.h"
1818
#include "includes/node.h"
1919

2020
namespace Kratos
@@ -47,7 +47,7 @@ std::vector<std::optional<Vector>> ExtrapolationUtilities::CalculateNodalVectors
4747
const std::vector<Vector>& rVectorsAtIntegrationPoints,
4848
size_t ElementId)
4949
{
50-
const auto element_node_ids = GeometryUtilities::GetNodeIdsFromGeometry(rGeometry);
50+
const auto element_node_ids = GenericUtilities::GetIdsFromEntityContents(rGeometry);
5151
const auto extrapolation_matrix = CalculateExtrapolationMatrix(rGeometry, IntegrationMethod, ElementId);
5252

5353
KRATOS_ERROR_IF_NOT(extrapolation_matrix.size2() == rVectorsAtIntegrationPoints.size())

applications/GeoMechanicsApplication/custom_utilities/generic_utilities.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// License: geo_mechanics_application/license.txt
99
//
1010
// Main authors: Mohamed Nabi
11+
// Gennady Markelov
1112
//
1213

1314
#pragma once
@@ -48,6 +49,21 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) GenericUtilities
4849
return result;
4950
}
5051

52+
template <class ContainerType, class OutputIt>
53+
static void GetIdsFromEntityContents(const ContainerType& rContainer, OutputIt Out)
54+
{
55+
std::ranges::transform(rContainer, Out, [](const auto& rItem) { return rItem.Id(); });
56+
}
57+
58+
template <class ContainerType>
59+
static auto GetIdsFromEntityContents(const ContainerType& rContainer)
60+
{
61+
std::vector<std::size_t> result;
62+
result.reserve(rContainer.size());
63+
GetIdsFromEntityContents(rContainer, std::back_inserter(result));
64+
return result;
65+
}
66+
5167
}; // class GenericUtilities
5268

5369
} // namespace Kratos

applications/GeoMechanicsApplication/custom_utilities/geometry_utilities.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,6 @@ Matrix GeometryUtilities::Calculate3DRotationMatrixForPlaneGeometry(const Geomet
138138
return result;
139139
}
140140

141-
std::vector<std::size_t> GeometryUtilities::GetNodeIdsFromGeometry(const Geometry<Node>& rGeometry)
142-
{
143-
std::vector<std::size_t> result;
144-
result.reserve(rGeometry.size());
145-
std::ranges::transform(rGeometry, std::back_inserter(result),
146-
[](const auto& rNode) { return rNode.Id(); });
147-
return result;
148-
}
149-
150141
void GeometryUtilities::ReverseNodes(PointerVector<Node>& rNodes,
151142
GeometryData::KratosGeometryFamily GeometryFamily,
152143
GeometryData::KratosGeometryOrderType GeometryOrderType)

applications/GeoMechanicsApplication/custom_utilities/geometry_utilities.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) GeometryUtilities
2828
const array_1d<double, 3>& rLocalCoordinate);
2929
static Matrix Calculate3DRotationMatrixForPlaneGeometry(const Geometry<Node>& rGeometry,
3030
const array_1d<double, 3>& rLocalCoordinate);
31-
static std::vector<std::size_t> GetNodeIdsFromGeometry(const Geometry<Node>& rGeometry);
32-
static void ReverseNodes(PointerVector<Node>& rNodes,
33-
GeometryData::KratosGeometryFamily GeometryFamily,
34-
GeometryData::KratosGeometryOrderType GeometryOrderType);
35-
static void ReverseNodes(std::vector<std::size_t>& rNodeIds,
36-
GeometryData::KratosGeometryFamily GeometryFamily,
37-
GeometryData::KratosGeometryOrderType GeometryOrderType);
31+
static void ReverseNodes(PointerVector<Node>& rNodes,
32+
GeometryData::KratosGeometryFamily GeometryFamily,
33+
GeometryData::KratosGeometryOrderType GeometryOrderType);
34+
static void ReverseNodes(std::vector<std::size_t>& rNodeIds,
35+
GeometryData::KratosGeometryFamily GeometryFamily,
36+
GeometryData::KratosGeometryOrderType GeometryOrderType);
3837
};
3938

4039
} // namespace Kratos

applications/GeoMechanicsApplication/custom_utilities/neighbouring_element_finder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void NeighbouringElementFinder::AddNeighbouringElementsBasedOnBoundaryGeometry(E
6060
const Geometry<Node>& rBoundaryGeometry,
6161
bool ReverseSearch)
6262
{
63-
auto element_boundary_node_ids = GeometryUtilities::GetNodeIdsFromGeometry(rBoundaryGeometry);
63+
auto element_boundary_node_ids = GenericUtilities::GetIdsFromEntityContents(rBoundaryGeometry);
6464
if (ReverseSearch) {
6565
GeometryUtilities::ReverseNodes(element_boundary_node_ids, rBoundaryGeometry.GetGeometryFamily(),
6666
rBoundaryGeometry.GetGeometryOrderType());

applications/GeoMechanicsApplication/custom_utilities/neighbouring_element_finder.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#pragma once
1414

1515
#include "boundary_generators.h"
16-
#include "geometry_utilities.h"
16+
#include "generic_utilities.h"
1717
#include "includes/geometrical_object.h"
1818
#include "includes/key_hash.h"
1919
#include "includes/kratos_export_api.h"
@@ -88,7 +88,7 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) NeighbouringElementFinder
8888

8989
for (const auto& r_boundary_geometry : r_boundary_generator(r_entity.GetGeometry())) {
9090
mGeometryNodeIdsToEntities.insert(
91-
{GeometryUtilities::GetNodeIdsFromGeometry(r_boundary_geometry), {&r_entity}});
91+
{GenericUtilities::GetIdsFromEntityContents(r_boundary_geometry), {&r_entity}});
9292
}
9393
}
9494

applications/GeoMechanicsApplication/custom_utilities/process_utilities.cpp

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
#include "containers/model.h"
1717
#include "includes/kratos_parameters.h"
1818

19-
namespace Kratos
19+
namespace
2020
{
21-
std::vector<std::reference_wrapper<ModelPart>> ProcessUtilities::GetModelPartsFromSettings(
22-
Model& rModel, const Parameters& rProcessSettings, const std::string& rProcessInfo)
21+
22+
std::vector<std::string> GetProcessModelPartNames(const Kratos::Parameters& rProcessSettings,
23+
const std::string& rProcessInfo)
2324
{
2425
KRATOS_ERROR_IF_NOT(rProcessSettings.Has("model_part_name") ||
2526
rProcessSettings.Has("model_part_name_list"))
@@ -30,9 +31,35 @@ std::vector<std::reference_wrapper<ModelPart>> ProcessUtilities::GetModelPartsFr
3031
<< "The parameters 'model_part_name' and 'model_part_name_list' are mutually exclusive for "
3132
<< rProcessInfo;
3233

33-
const auto model_part_names = rProcessSettings.Has("model_part_name_list")
34-
? rProcessSettings["model_part_name_list"].GetStringArray()
35-
: std::vector{rProcessSettings["model_part_name"].GetString()};
34+
return rProcessSettings.Has("model_part_name_list")
35+
? rProcessSettings["model_part_name_list"].GetStringArray()
36+
: std::vector{rProcessSettings["model_part_name"].GetString()};
37+
}
38+
39+
std::set<std::string, std::less<>> ExtractModelPartNames(const auto& rProcessList,
40+
std::string_view RootName,
41+
std::string_view Prefix)
42+
{
43+
std::set<std::string, std::less<>> result;
44+
for (const auto& r_process : rProcessList) {
45+
if (!r_process.Has("Parameters")) continue;
46+
const auto model_part_names = GetProcessModelPartNames(r_process["Parameters"], {});
47+
for (auto model_part_name : model_part_names) {
48+
if (model_part_name == RootName) continue;
49+
if (model_part_name.starts_with(Prefix)) model_part_name.erase(0, Prefix.size());
50+
result.insert(model_part_name);
51+
}
52+
}
53+
return result;
54+
};
55+
} // namespace
56+
57+
namespace Kratos
58+
{
59+
std::vector<std::reference_wrapper<ModelPart>> ProcessUtilities::GetModelPartsFromSettings(
60+
Model& rModel, const Parameters& rProcessSettings, const std::string& rProcessInfo)
61+
{
62+
const auto model_part_names = GetProcessModelPartNames(rProcessSettings, rProcessInfo);
3663
KRATOS_ERROR_IF(model_part_names.empty()) << "The parameters 'model_part_name_list' needs "
3764
"to contain at least one model part name for "
3865
<< rProcessInfo;
@@ -45,4 +72,31 @@ std::vector<std::reference_wrapper<ModelPart>> ProcessUtilities::GetModelPartsFr
4572
return result;
4673
}
4774

75+
void ProcessUtilities::AddProcessesSubModelPartListToSolverSettings(const Parameters& rProjectParameters,
76+
Parameters& rSolverSettings)
77+
{
78+
std::set<std::string, std::less<>> domain_condition_names;
79+
const auto root_name = rSolverSettings["model_part_name"].GetString();
80+
const auto prefix = root_name + ".";
81+
82+
if (rProjectParameters.Has("processes")) {
83+
for (const auto& r_process : rProjectParameters["processes"]) {
84+
const auto modelpart_names = ExtractModelPartNames(r_process, root_name, prefix);
85+
domain_condition_names.insert(modelpart_names.begin(), modelpart_names.end());
86+
}
87+
}
88+
if (rSolverSettings.Has("processes_sub_model_part_list")) {
89+
KRATOS_WARNING("ProcessUtilities")
90+
<< "'processes_sub_model_part_list' is deprecated. This list is built automatically "
91+
"from the model parts used in all processes."
92+
<< std::endl;
93+
rSolverSettings.RemoveValue("processes_sub_model_part_list");
94+
}
95+
rSolverSettings.AddEmptyArray("processes_sub_model_part_list");
96+
97+
for (const auto& r_name : domain_condition_names) {
98+
rSolverSettings["processes_sub_model_part_list"].Append(r_name);
99+
}
100+
}
101+
48102
}; // namespace Kratos

applications/GeoMechanicsApplication/custom_utilities/process_utilities.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) ProcessUtilities
2929
public:
3030
static std::vector<std::reference_wrapper<ModelPart>> GetModelPartsFromSettings(
3131
Model& rModel, const Parameters& rProcessSettings, const std::string& rProcessInfo);
32+
33+
static void AddProcessesSubModelPartListToSolverSettings(const Parameters& rProjectParameters,
34+
Parameters& rSolverSettings);
3235
};
3336
}; // namespace Kratos

0 commit comments

Comments
 (0)