diff --git a/applications/GeoMechanicsApplication/custom_processes/apply_scalar_constraints_table_process.cpp b/applications/GeoMechanicsApplication/custom_processes/apply_scalar_constraints_table_process.cpp new file mode 100644 index 000000000000..3981c7897448 --- /dev/null +++ b/applications/GeoMechanicsApplication/custom_processes/apply_scalar_constraints_table_process.cpp @@ -0,0 +1,219 @@ +// KRATOS___ +// // ) ) +// // ___ ___ +// // ____ //___) ) // ) ) +// // / / // // / / +// ((____/ / ((____ ((___/ / MECHANICS +// +// License: geo_mechanics_application/license.txt +// +// Main authors: Anne van de Graaf, +// Marjan Fathian +// +#include "apply_scalar_constraints_table_process.h" +#include "includes/model_part.h" +#include "includes/kratos_parameters.h" +#include "apply_component_table_process.hpp" +#include "processes/apply_constant_scalarvalue_process.h" +#include "apply_hydrostatic_pressure_table_process.hpp" +#include "apply_constant_phreatic_line_pressure_process.hpp" +#include "apply_phreatic_line_pressure_table_process.hpp" +#include "apply_constant_interpolate_line_pressure_process.hpp" +#include "apply_constant_phreatic_surface_pressure_process.hpp" +#include "apply_phreatic_surface_pressure_table_process.hpp" + +namespace +{ + +using namespace Kratos; + +Parameters ExtractParameters(const Parameters& rSourceParameters, + const std::vector& rNamesOfParametersToCopy) +{ + auto result = Parameters{}; + result.CopyValuesFromExistingParameters(rSourceParameters, rNamesOfParametersToCopy); + return result; +} + +void AppendParameterNameIfExists(const std::string& rParameterName, + const Parameters& rSourceParameters, + std::vector& rResult) +{ + if (rSourceParameters.Has(rParameterName)) { + rResult.emplace_back(rParameterName); + } +} + +bool HasTableAttached(const Parameters& rSettings) +{ + if (rSettings["table"].IsNumber()) { + return rSettings["table"].GetInt() != 0; + } + + KRATOS_ERROR_IF_NOT(rSettings["table"].IsArray()) << "'table' is neither a single number nor an array of numbers"; + + const auto& table = rSettings["table"]; + return std::any_of(table.begin(), table.end(), + [](const auto& value){return value.GetInt() != 0;}); +} + +} + +namespace Kratos +{ + +ApplyScalarConstraintsTableProcess::ApplyScalarConstraintsTableProcess(ModelPart& rModelPart, + const Parameters& rProcessSettings) + : Process(Flags()), + mrModelPart{rModelPart} +{ + MakeInternalProcess(rProcessSettings); +} + +ApplyScalarConstraintsTableProcess::~ApplyScalarConstraintsTableProcess() = default; + +void ApplyScalarConstraintsTableProcess::MakeInternalProcess(const Parameters& rProcessSettings) +{ + auto names_of_settings_to_copy = std::vector{"model_part_name", + "variable_name"}; + AppendParameterNameIfExists("is_fixed", rProcessSettings, names_of_settings_to_copy); + + if (rProcessSettings.Has("fluid_pressure_type")) { + MakeProcessForFluidPressureType(rProcessSettings, std::move(names_of_settings_to_copy)); + } + else { + MakeScalarConstraintsProcess(rProcessSettings, std::move(names_of_settings_to_copy)); + } +} + +void ApplyScalarConstraintsTableProcess::MakeProcessForFluidPressureType(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy) +{ + const auto fluid_pressure_type = rProcessSettings["fluid_pressure_type"].GetString(); + if (fluid_pressure_type == "Uniform") { + MakeScalarConstraintsProcess(rProcessSettings, std::move(NamesOfSettingsToCopy)); + } else if (fluid_pressure_type == "Hydrostatic") { + MakeProcessForHydrostaticFluidPressure(rProcessSettings, std::move(NamesOfSettingsToCopy)); + } else if (fluid_pressure_type == "Phreatic_Line") { + MakeProcessForPhreaticLine(rProcessSettings, std::move(NamesOfSettingsToCopy)); + } else if (fluid_pressure_type == "Interpolate_Line") { + MakeProcessForInterpolatedLine(rProcessSettings, std::move(NamesOfSettingsToCopy)); + } else if (fluid_pressure_type == "Phreatic_Surface") { + MakeProcessForPhreaticSurface(rProcessSettings, std::move(NamesOfSettingsToCopy)); + } else { + KRATOS_ERROR << "Unknown fluid_pressure_type: " << fluid_pressure_type << std::endl; + } +} + +void ApplyScalarConstraintsTableProcess::MakeScalarConstraintsProcess(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy) +{ + NamesOfSettingsToCopy.emplace_back("value"); + + if (HasTableAttached(rProcessSettings)) { + NamesOfSettingsToCopy.emplace_back("table"); + mProcess = std::make_unique(mrModelPart, + ExtractParameters(rProcessSettings, + NamesOfSettingsToCopy)); + } else { + mProcess = std::make_unique(mrModelPart, + ExtractParameters(rProcessSettings, + NamesOfSettingsToCopy)); + } +} + +void ApplyScalarConstraintsTableProcess::MakeProcessForHydrostaticFluidPressure(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy) +{ + NamesOfSettingsToCopy.insert(NamesOfSettingsToCopy.end(), {"gravity_direction", + "reference_coordinate", + "specific_weight"}); + AppendParameterNameIfExists("pressure_tension_cut_off", rProcessSettings, NamesOfSettingsToCopy); + AppendParameterNameIfExists("is_seepage", rProcessSettings, NamesOfSettingsToCopy); + + if (HasTableAttached(rProcessSettings)) { + NamesOfSettingsToCopy.emplace_back("table"); + mProcess = std::make_unique(mrModelPart, + ExtractParameters(rProcessSettings, + NamesOfSettingsToCopy)); + } else { + mProcess = std::make_unique(mrModelPart, + ExtractParameters(rProcessSettings, + NamesOfSettingsToCopy)); + } +} + +void ApplyScalarConstraintsTableProcess::MakeProcessForPhreaticLine(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy) +{ + NamesOfSettingsToCopy.insert(NamesOfSettingsToCopy.end(), {"gravity_direction", + "out_of_plane_direction", + "first_reference_coordinate", + "second_reference_coordinate", + "specific_weight"}); + AppendParameterNameIfExists("pressure_tension_cut_off", rProcessSettings, NamesOfSettingsToCopy); + AppendParameterNameIfExists("is_seepage", rProcessSettings, NamesOfSettingsToCopy); + + if (HasTableAttached(rProcessSettings)) { + NamesOfSettingsToCopy.emplace_back("table"); + mProcess = std::make_unique(mrModelPart, + ExtractParameters(rProcessSettings, + NamesOfSettingsToCopy)); + } else { + mProcess = std::make_unique(mrModelPart, + ExtractParameters(rProcessSettings, + NamesOfSettingsToCopy)); + } +} + +void ApplyScalarConstraintsTableProcess::MakeProcessForPhreaticSurface(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy) +{ + NamesOfSettingsToCopy.insert(NamesOfSettingsToCopy.end(), {"gravity_direction", + "first_reference_coordinate", + "second_reference_coordinate", + "third_reference_coordinate", + "specific_weight"}); + AppendParameterNameIfExists("pressure_tension_cut_off", rProcessSettings, NamesOfSettingsToCopy); + AppendParameterNameIfExists("is_seepage", rProcessSettings, NamesOfSettingsToCopy); + + if (HasTableAttached(rProcessSettings)) { + NamesOfSettingsToCopy.emplace_back("table"); + mProcess = std::make_unique(mrModelPart, + ExtractParameters(rProcessSettings, + NamesOfSettingsToCopy)); + } + else { + mProcess = std::make_unique(mrModelPart, + ExtractParameters(rProcessSettings, + NamesOfSettingsToCopy)); + } +} + +void ApplyScalarConstraintsTableProcess::MakeProcessForInterpolatedLine(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy) +{ + KRATOS_ERROR_IF(HasTableAttached(rProcessSettings)) << "No time dependent interpolate line pressure process available" << std::endl; + + NamesOfSettingsToCopy.insert(NamesOfSettingsToCopy.end(), {"gravity_direction", + "out_of_plane_direction"}); + AppendParameterNameIfExists("pressure_tension_cut_off", rProcessSettings, NamesOfSettingsToCopy); + AppendParameterNameIfExists("is_seepage", rProcessSettings, NamesOfSettingsToCopy); + + mProcess = std::make_unique(mrModelPart, + ExtractParameters(rProcessSettings, + NamesOfSettingsToCopy)); + +} + +void ApplyScalarConstraintsTableProcess::ExecuteInitialize() +{ + mProcess->ExecuteInitialize(); +} + +void ApplyScalarConstraintsTableProcess::ExecuteInitializeSolutionStep() +{ + mProcess->ExecuteInitializeSolutionStep(); +} + +} diff --git a/applications/GeoMechanicsApplication/custom_processes/apply_scalar_constraints_table_process.h b/applications/GeoMechanicsApplication/custom_processes/apply_scalar_constraints_table_process.h new file mode 100644 index 000000000000..ae2cbbb2d77b --- /dev/null +++ b/applications/GeoMechanicsApplication/custom_processes/apply_scalar_constraints_table_process.h @@ -0,0 +1,61 @@ +// KRATOS___ +// // ) ) +// // ___ ___ +// // ____ //___) ) // ) ) +// // / / // // / / +// ((____/ / ((____ ((___/ / MECHANICS +// +// License: geo_mechanics_application/license.txt +// +// Main authors: Anne van de Graaf, +// Marjan Fathian +// +#pragma once + +#include "processes/process.h" + +namespace Kratos +{ + +class ModelPart; +class Parameters; + + +class KRATOS_API(GEO_MECHANICS_APPLICATION) ApplyScalarConstraintsTableProcess : public Process +{ +public: + KRATOS_CLASS_POINTER_DEFINITION(ApplyScalarConstraintsTableProcess); + + ApplyScalarConstraintsTableProcess(ModelPart& rModelPart, + const Parameters& rProcessSettings); + + ~ApplyScalarConstraintsTableProcess() override; + + ApplyScalarConstraintsTableProcess(const ApplyScalarConstraintsTableProcess&) = delete; + ApplyScalarConstraintsTableProcess& operator=(const ApplyScalarConstraintsTableProcess&) = delete; + + using ProcessUniquePointer = std::unique_ptr; + + void ExecuteInitialize() override; + void ExecuteInitializeSolutionStep() override; + +private: + void MakeInternalProcess(const Parameters& rProcessSettings); + void MakeProcessForFluidPressureType(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy); + void MakeScalarConstraintsProcess(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy); + void MakeProcessForHydrostaticFluidPressure(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy); + void MakeProcessForPhreaticLine(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy); + void MakeProcessForPhreaticSurface(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy); + void MakeProcessForInterpolatedLine(const Parameters& rProcessSettings, + std::vector NamesOfSettingsToCopy); + + ModelPart& mrModelPart; + ProcessUniquePointer mProcess; +}; + +} diff --git a/applications/GeoMechanicsApplication/custom_python/add_custom_processes_to_python.cpp b/applications/GeoMechanicsApplication/custom_python/add_custom_processes_to_python.cpp index eb2127665ca1..0ba9ddac2267 100644 --- a/applications/GeoMechanicsApplication/custom_python/add_custom_processes_to_python.cpp +++ b/applications/GeoMechanicsApplication/custom_python/add_custom_processes_to_python.cpp @@ -42,6 +42,7 @@ #include "custom_processes/set_parameter_field_process.hpp" #include "custom_processes/set_multiple_moving_loads.h" #include "custom_processes/apply_vector_constraints_table_process.hpp" +#include "custom_processes/apply_scalar_constraints_table_process.h" namespace Kratos { namespace Python { @@ -141,6 +142,10 @@ void AddCustomProcessesToPython(pybind11::module& m) py::class_ (m, "ApplyVectorConstraintsTableProcess") .def(py::init()); + + py::class_ + (m, "ApplyScalarConstraintsTableProcess") + .def(py::init()); } } // Namespace Python. diff --git a/applications/GeoMechanicsApplication/custom_workflows/dgeosettlement.cpp b/applications/GeoMechanicsApplication/custom_workflows/dgeosettlement.cpp index 354be55027f1..9ee5c10b9d79 100644 --- a/applications/GeoMechanicsApplication/custom_workflows/dgeosettlement.cpp +++ b/applications/GeoMechanicsApplication/custom_workflows/dgeosettlement.cpp @@ -15,6 +15,7 @@ #include "utilities/variable_utils.h" +#include "custom_processes/apply_scalar_constraints_table_process.h" #include "custom_processes/apply_vector_constraints_table_process.hpp" #include "custom_processes/set_parameter_field_process.hpp" #include "custom_processes/apply_k0_procedure_process.hpp" @@ -47,7 +48,15 @@ KratosGeoSettlement::KratosGeoSettlement(std::unique_ptr pInputUti InitializeProcessFactory(); } -void KratosGeoSettlement::InitializeProcessFactory() { +void KratosGeoSettlement::InitializeProcessFactory() +{ + mProcessFactory->AddCreator("ApplyScalarConstraintsTableProcess", + [this](const Parameters& rParameters) + { + return std::make_unique(mModel.GetModelPart(mModelPartName), + rParameters); + }); + mProcessFactory->AddCreator("ApplyVectorConstraintsTableProcess", [this](const Parameters& rParameters) { diff --git a/applications/GeoMechanicsApplication/python_scripts/apply_scalar_constraint_table_process.py b/applications/GeoMechanicsApplication/python_scripts/apply_scalar_constraint_table_process.py index 09730c03fa89..7624a213a365 100644 --- a/applications/GeoMechanicsApplication/python_scripts/apply_scalar_constraint_table_process.py +++ b/applications/GeoMechanicsApplication/python_scripts/apply_scalar_constraint_table_process.py @@ -4,112 +4,7 @@ def Factory(settings, Model): if not isinstance(settings, KratosMultiphysics.Parameters): raise TypeError("expected input shall be a Parameters object, encapsulating a json string") - return ApplyScalarConstraintTableProcess(Model, settings["Parameters"]) -## All the python processes should be derived from "python_process" - -class ApplyScalarConstraintTableProcess(KratosMultiphysics.Process): - def __init__(self, Model, settings ): - KratosMultiphysics.Process.__init__(self) - - self.model_part = Model[settings["model_part_name"].GetString()] - - self.params = KratosMultiphysics.Parameters("{}") - self.params.AddValue("model_part_name",settings["model_part_name"]) - self.params.AddValue("variable_name",settings["variable_name"]) - if settings.Has("is_fixed"): - self.params.AddValue("is_fixed",settings["is_fixed"]) - - if settings.Has("fluid_pressure_type"): - # if settings["hydrostatic"].GetBool() == False: - if settings["fluid_pressure_type"].GetString() == "Uniform": - self.params.AddValue("value",settings["value"]) - if settings["table"].GetInt() == 0: - self.process = KratosMultiphysics.ApplyConstantScalarValueProcess(self.model_part, self.params) - else: - self.params.AddValue("table",settings["table"]) - self.process = KratosGeo.ApplyComponentTableProcess(self.model_part, self.params) - elif settings["fluid_pressure_type"].GetString() == "Hydrostatic": - self.params.AddValue("gravity_direction",settings["gravity_direction"]) - self.params.AddValue("reference_coordinate",settings["reference_coordinate"]) - self.params.AddValue("specific_weight",settings["specific_weight"]) - - if settings.Has("pressure_tension_cut_off"): - self.params.AddValue("pressure_tension_cut_off",settings["pressure_tension_cut_off"]) - - if settings.Has("is_seepage"): - self.params.AddValue("is_seepage",settings["is_seepage"]) - - if settings["table"].GetInt() == 0: - self.process = KratosGeo.ApplyConstantHydrostaticPressureProcess(self.model_part, self.params) - else: - self.params.AddValue("table",settings["table"]) - self.process = KratosGeo.ApplyHydrostaticPressureTableProcess(self.model_part, self.params) - elif settings["fluid_pressure_type"].GetString() == "Phreatic_Line": - self.params.AddValue("gravity_direction",settings["gravity_direction"]) - self.params.AddValue("out_of_plane_direction",settings["out_of_plane_direction"]) - self.params.AddValue("first_reference_coordinate",settings["first_reference_coordinate"]) - self.params.AddValue("second_reference_coordinate",settings["second_reference_coordinate"]) - self.params.AddValue("specific_weight",settings["specific_weight"]) - - if settings.Has("pressure_tension_cut_off"): - self.params.AddValue("pressure_tension_cut_off",settings["pressure_tension_cut_off"]) - - if settings.Has("is_seepage"): - self.params.AddValue("is_seepage",settings["is_seepage"]) - - if settings["table"][0].GetInt() == 0 and settings["table"][1].GetInt() == 0: - self.process = KratosGeo.ApplyConstantPhreaticLinePressureProcess(self.model_part, self.params) - else: - self.params.AddValue("table",settings["table"]) - self.process = KratosGeo.ApplyPhreaticLinePressureTableProcess(self.model_part, self.params) - elif settings["fluid_pressure_type"].GetString() == "Interpolate_Line": - self.params.AddValue("gravity_direction",settings["gravity_direction"]) - self.params.AddValue("out_of_plane_direction",settings["out_of_plane_direction"]) - - if settings.Has("pressure_tension_cut_off"): - self.params.AddValue("pressure_tension_cut_off",settings["pressure_tension_cut_off"]) - - if settings.Has("is_seepage"): - self.params.AddValue("is_seepage",settings["is_seepage"]) - - if settings["table"].GetInt() == 0: - self.process = KratosGeo.ApplyConstantInterpolateLinePressureProcess(self.model_part, self.params) - else: - raise RuntimeError("No time dependent interpolate line pressure process available") - elif settings["fluid_pressure_type"].GetString() == "Phreatic_Surface": - self.params.AddValue("gravity_direction",settings["gravity_direction"]) - self.params.AddValue("first_reference_coordinate",settings["first_reference_coordinate"]) - self.params.AddValue("second_reference_coordinate",settings["second_reference_coordinate"]) - self.params.AddValue("third_reference_coordinate",settings["third_reference_coordinate"]) - self.params.AddValue("specific_weight",settings["specific_weight"]) - - if settings.Has("pressure_tension_cut_off"): - self.params.AddValue("pressure_tension_cut_off",settings["pressure_tension_cut_off"]) - - if settings.Has("is_seepage"): - self.params.AddValue("is_seepage",settings["is_seepage"]) - - if settings["table"][0].GetInt() == 0 and settings["table"][1].GetInt() == 0 and settings["table"][2].GetInt() == 0: - self.process = KratosGeo.ApplyConstantPhreaticSurfacePressureProcess(self.model_part, self.params) - else: - self.params.AddValue("table",settings["table"]) - self.process = KratosGeo.ApplyPhreaticSurfacePressureTableProcess(self.model_part, self.params) - else: - raise Exception("unkown fluid_pressure_type!") - - else: - self.params.AddValue("value",settings["value"]) - if settings["table"].GetInt() == 0: - self.process = KratosMultiphysics.ApplyConstantScalarValueProcess(self.model_part, self.params) - else: - self.params.AddValue("table",settings["table"]) - self.process = KratosGeo.ApplyComponentTableProcess(self.model_part, self.params) - - def ExecuteInitialize(self): - - self.process.ExecuteInitialize() - - def ExecuteInitializeSolutionStep(self): - - self.process.ExecuteInitializeSolutionStep() + params = settings["Parameters"] + model_part = Model[params["model_part_name"].GetString()] + return KratosGeo.ApplyScalarConstraintsTableProcess(model_part, params)