From ae2e9e3cefa0461775d302c201e98528947f54f4 Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 5 Feb 2024 19:33:49 +0100 Subject: [PATCH 01/21] Adding linear to quadratic tets modeler --- ...onvert_linear_tets_to_quadratic_modeler.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py diff --git a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py new file mode 100644 index 000000000000..994f75a391fd --- /dev/null +++ b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py @@ -0,0 +1,58 @@ +import KratosMultiphysics +from KratosMultiphysics import MeshingApplication as Meshing + +class ConvertLinearTetsToQuadraticModeler(KratosMultiphysics.Modeler): + def __init__(self, model, settings): + super().__init__(model, settings) + + settings.ValidateAndAssignDefaults(self.__GetDefaultSettings()) + self.model = model + self.settings=settings + + # Converts the input model part from linear tets to quadratic + def SetupModelPart(self): + super().SetupModelPart() + + if(KratosMultiphysics.IsDistributedRun()): + raise Exception("ConvertLinearTetsToQuadraticModeler is not MPI-ready") + + model_part = self.model[self.settings["model_part_name"].GetString()] + #quick check + for elem in model_part.Elements: + if elem.GetGeometry().PointsNumber()!=4: + msg = "Only linear tets are accepted as input (4 nodes) \n" + raise Exception(msg) + + #flag elems and conds + KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( + KratosMultiphysics.SPLIT_ELEMENT, + True, + model_part.Elements) + KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( + KratosMultiphysics.SPLIT_ELEMENT, + True, + model_part.Conditions) + + #calling utility to replace the tets: + Meshing.LinearToQuadraticTetrahedraMeshConverter(model_part.GetRootModelPart()).LocalConvertLinearToQuadraticTetrahedraMesh(False,False) + + element_name = self.settings["element_name"].GetString() + + if element_name: + replace_settings = KratosMultiphysics.Parameters("{}") + replace_settings.AddString("element_name", element_name) + replace_settings.AddString("condition_name", "") + KratosMultiphysics.ReplaceElementsAndConditionsProcess(model_part,replace_settings).Execute() + + def __GetDefaultSettings(self): + default_settings = KratosMultiphysics.Parameters('''{ + "model_part_name" : "main_model_part", + "element_name" : "" + }''') + return default_settings + + def __str__(self): + return "ConvertLinearTetsToQuadraticModeler" + +def Factory(model, settings): + return ConvertLinearTetsToQuadraticModeler(model, settings) \ No newline at end of file From d66d68a87269e694e2747ed242703b2e47e735a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicente=20Mataix=20Ferr=C3=A1ndiz?= Date: Tue, 6 Feb 2024 16:28:55 +0100 Subject: [PATCH 02/21] Adding doc --- ...onvert_linear_tets_to_quadratic_modeler.py | 81 +++++++++++++++---- 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py index 994f75a391fd..8eda5b425b92 100644 --- a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py +++ b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py @@ -2,28 +2,56 @@ from KratosMultiphysics import MeshingApplication as Meshing class ConvertLinearTetsToQuadraticModeler(KratosMultiphysics.Modeler): + """ + This class is a modeler in KratosMultiphysics that converts linear tetrahedral (tet) elements to quadratic tet elements within a specified model part. + + Attributes: + model (KratosMultiphysics.Model): The Kratos model containing the model part to be modified. + settings (KratosMultiphysics.Parameters): Settings for the conversion process. + + Methods: + __init__(model, settings): Constructor for the class. + SetupModelPart(): Performs the conversion from linear to quadratic tet elements. + __GetDefaultSettings(): Provides default settings for the modeler. + __str__(): String representation of the class. + Factory(model, settings): Factory method to create an instance of the class. + """ + def __init__(self, model, settings): - super().__init__(model, settings) + """ + Constructor for the ConvertLinearTetsToQuadraticModeler class. + Args: + model (KratosMultiphysics.Model): The Kratos model containing the model part to be modified. + settings (KratosMultiphysics.Parameters): Configuration settings for the modeler. + + The constructor initializes the modeler with the provided model and settings, and validates and assigns default settings. + """ + super().__init__(model, settings) settings.ValidateAndAssignDefaults(self.__GetDefaultSettings()) self.model = model - self.settings=settings + self.settings = settings - # Converts the input model part from linear tets to quadratic def SetupModelPart(self): + """ + Converts the specified model part's linear tet elements to quadratic tets. + + Raises: + Exception: If the operation is attempted in a distributed (MPI) run or if the input elements are not linear tets (4 nodes). + + This method checks the model part for compatibility, sets necessary variables, and calls the utility function for conversion. It also handles the replacement of elements and conditions based on provided settings. + """ super().SetupModelPart() - if(KratosMultiphysics.IsDistributedRun()): + if KratosMultiphysics.IsDistributedRun(): raise Exception("ConvertLinearTetsToQuadraticModeler is not MPI-ready") model_part = self.model[self.settings["model_part_name"].GetString()] - #quick check + for elem in model_part.Elements: - if elem.GetGeometry().PointsNumber()!=4: - msg = "Only linear tets are accepted as input (4 nodes) \n" - raise Exception(msg) + if elem.GetGeometry().PointsNumber() != 4: + raise Exception("Only linear tets are accepted as input (4 nodes)") - #flag elems and conds KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( KratosMultiphysics.SPLIT_ELEMENT, True, @@ -33,26 +61,47 @@ def SetupModelPart(self): True, model_part.Conditions) - #calling utility to replace the tets: - Meshing.LinearToQuadraticTetrahedraMeshConverter(model_part.GetRootModelPart()).LocalConvertLinearToQuadraticTetrahedraMesh(False,False) + Meshing.LinearToQuadraticTetrahedraMeshConverter(model_part.GetRootModelPart()).LocalConvertLinearToQuadraticTetrahedraMesh(False, False) element_name = self.settings["element_name"].GetString() - if element_name: replace_settings = KratosMultiphysics.Parameters("{}") replace_settings.AddString("element_name", element_name) replace_settings.AddString("condition_name", "") - KratosMultiphysics.ReplaceElementsAndConditionsProcess(model_part,replace_settings).Execute() + KratosMultiphysics.ReplaceElementsAndConditionsProcess(model_part, replace_settings).Execute() def __GetDefaultSettings(self): - default_settings = KratosMultiphysics.Parameters('''{ + """ + Provides the default settings for the modeler. + + Returns: + KratosMultiphysics.Parameters: The default settings as a Parameters object. + + These settings include the name of the model part to be modified and the name of the element to use for replacement, if specified. + """ + return KratosMultiphysics.Parameters('''{ "model_part_name" : "main_model_part", "element_name" : "" }''') - return default_settings def __str__(self): + """ + String representation of the ConvertLinearTetsToQuadraticModeler class. + + Returns: + str: A string describing the class. + """ return "ConvertLinearTetsToQuadraticModeler" def Factory(model, settings): - return ConvertLinearTetsToQuadraticModeler(model, settings) \ No newline at end of file + """ + Factory method to create an instance of ConvertLinearTetsToQuadraticModeler. + + Args: + model (KratosMultiphysics.Model): The Kratos model to be modified. + settings (KratosMultiphysics.Parameters): Configuration settings for the modeler. + + Returns: + ConvertLinearTetsToQuadraticModeler: An instance of the modeler class. + """ + return ConvertLinearTetsToQuadraticModeler(model, settings) From 15b0c2f6130b8834c9cc476d79c6b08bbcb3fc5c Mon Sep 17 00:00:00 2001 From: jcotela Date: Thu, 8 Feb 2024 16:13:41 +0100 Subject: [PATCH 03/21] Expose GeometryData to Python --- kratos/python/add_geometries_to_python.cpp | 4 + kratos/python/add_geometry_data_to_python.cpp | 108 ++++++++++++++++++ kratos/python/add_geometry_data_to_python.h | 27 +++++ kratos/python/kratos_python.cpp | 2 + kratos/tests/test_geometry_data.py | 75 ++++++++++++ 5 files changed, 216 insertions(+) create mode 100644 kratos/python/add_geometry_data_to_python.cpp create mode 100644 kratos/python/add_geometry_data_to_python.h create mode 100644 kratos/tests/test_geometry_data.py diff --git a/kratos/python/add_geometries_to_python.cpp b/kratos/python/add_geometries_to_python.cpp index eea926cdedb6..c8e0ff82b30a 100644 --- a/kratos/python/add_geometries_to_python.cpp +++ b/kratos/python/add_geometries_to_python.cpp @@ -140,6 +140,10 @@ void AddGeometriesToPython(pybind11::module& m) .def("PointsNumber", &GeometryType::PointsNumber) .def("PointsNumberInDirection", &GeometryType::PointsNumberInDirection) .def("PolynomialDegree", &GeometryType::PolynomialDegree) + // Geometry data + .def("GetDefaultIntegrationMethod", &GeometryType::GetDefaultIntegrationMethod) + .def("GetGeometryFamily", &GeometryType::GetGeometryFamily) + .def("GetGeometryType", &GeometryType::GetGeometryType) // Geometry Parts .def("GetGeometryPart", [](GeometryType& self, IndexType Index) { return(self.GetGeometryPart(Index)); }) diff --git a/kratos/python/add_geometry_data_to_python.cpp b/kratos/python/add_geometry_data_to_python.cpp new file mode 100644 index 000000000000..0508a50373f4 --- /dev/null +++ b/kratos/python/add_geometry_data_to_python.cpp @@ -0,0 +1,108 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Jordi Cotela +// + +// System includes + +// External includes + +// Project includes +#include "geometries/geometry_data.h" +#include "add_geometry_data_to_python.h" + +namespace Kratos::Python +{ + +void AddGeometryDataToPython(pybind11::module& m) +{ + namespace py = pybind11; + + auto integration_method = py::enum_(m, "GeometryData_IntegrationMethod") + .value("GI_GAUSS_1", GeometryData::IntegrationMethod::GI_GAUSS_1) + .value("GI_GAUSS_2", GeometryData::IntegrationMethod::GI_GAUSS_2) + .value("GI_GAUSS_3", GeometryData::IntegrationMethod::GI_GAUSS_3) + .value("GI_GAUSS_4", GeometryData::IntegrationMethod::GI_GAUSS_4) + .value("GI_GAUSS_5", GeometryData::IntegrationMethod::GI_GAUSS_5) + .value("GI_EXTENDED_GAUSS_1", GeometryData::IntegrationMethod::GI_EXTENDED_GAUSS_1) + .value("GI_EXTENDED_GAUSS_2", GeometryData::IntegrationMethod::GI_EXTENDED_GAUSS_2) + .value("GI_EXTENDED_GAUSS_3", GeometryData::IntegrationMethod::GI_EXTENDED_GAUSS_3) + .value("GI_EXTENDED_GAUSS_4", GeometryData::IntegrationMethod::GI_EXTENDED_GAUSS_4) + .value("GI_EXTENDED_GAUSS_5", GeometryData::IntegrationMethod::GI_EXTENDED_GAUSS_5); + + auto geometry_family = py::enum_(m, "GeometryData_KratosGeometryFamily") + .value("Kratos_NoElement", GeometryData::KratosGeometryFamily::Kratos_NoElement) + .value("Kratos_Point", GeometryData::KratosGeometryFamily::Kratos_Point) + .value("Kratos_Linear", GeometryData::KratosGeometryFamily::Kratos_Linear) + .value("Kratos_Triangle", GeometryData::KratosGeometryFamily::Kratos_Triangle) + .value("Kratos_Quadrilateral", GeometryData::KratosGeometryFamily::Kratos_Quadrilateral) + .value("Kratos_Tetrahedra", GeometryData::KratosGeometryFamily::Kratos_Tetrahedra) + .value("Kratos_Hexahedra", GeometryData::KratosGeometryFamily::Kratos_Hexahedra) + .value("Kratos_Prism", GeometryData::KratosGeometryFamily::Kratos_Prism) + .value("Kratos_Pyramid", GeometryData::KratosGeometryFamily::Kratos_Pyramid) + .value("Kratos_Nurbs", GeometryData::KratosGeometryFamily::Kratos_Nurbs) + .value("Kratos_Brep", GeometryData::KratosGeometryFamily::Kratos_Brep) + .value("Kratos_Quadrature_Geometry", GeometryData::KratosGeometryFamily::Kratos_Quadrature_Geometry) + .value("Kratos_Composite", GeometryData::KratosGeometryFamily::Kratos_Composite) + .value("Kratos_generic_family", GeometryData::KratosGeometryFamily::Kratos_generic_family); + + auto geometry_type = py::enum_(m, "GeometryData_KratosGeometryType") + .value("Kratos_generic_type", GeometryData::KratosGeometryType::Kratos_generic_type) + .value("Kratos_Hexahedra3D20", GeometryData::KratosGeometryType::Kratos_Hexahedra3D20) + .value("Kratos_Hexahedra3D27", GeometryData::KratosGeometryType::Kratos_Hexahedra3D27) + .value("Kratos_Hexahedra3D8", GeometryData::KratosGeometryType::Kratos_Hexahedra3D8) + .value("Kratos_Prism3D15", GeometryData::KratosGeometryType::Kratos_Prism3D15) + .value("Kratos_Prism3D6", GeometryData::KratosGeometryType::Kratos_Prism3D6) + .value("Kratos_Pyramid3D13", GeometryData::KratosGeometryType::Kratos_Pyramid3D13) + .value("Kratos_Pyramid3D5", GeometryData::KratosGeometryType::Kratos_Pyramid3D5) + .value("Kratos_Quadrilateral2D4", GeometryData::KratosGeometryType::Kratos_Quadrilateral2D4) + .value("Kratos_Quadrilateral2D8", GeometryData::KratosGeometryType::Kratos_Quadrilateral2D8) + .value("Kratos_Quadrilateral2D9", GeometryData::KratosGeometryType::Kratos_Quadrilateral2D9) + .value("Kratos_Quadrilateral3D4", GeometryData::KratosGeometryType::Kratos_Quadrilateral3D4) + .value("Kratos_Quadrilateral3D8", GeometryData::KratosGeometryType::Kratos_Quadrilateral3D8) + .value("Kratos_Quadrilateral3D9", GeometryData::KratosGeometryType::Kratos_Quadrilateral3D9) + .value("Kratos_Tetrahedra3D10", GeometryData::KratosGeometryType::Kratos_Tetrahedra3D10) + .value("Kratos_Tetrahedra3D4", GeometryData::KratosGeometryType::Kratos_Tetrahedra3D4) + .value("Kratos_Triangle2D3", GeometryData::KratosGeometryType::Kratos_Triangle2D3) + .value("Kratos_Triangle2D6", GeometryData::KratosGeometryType::Kratos_Triangle2D6) + .value("Kratos_Triangle2D10", GeometryData::KratosGeometryType::Kratos_Triangle2D10) + .value("Kratos_Triangle2D15", GeometryData::KratosGeometryType::Kratos_Triangle2D15) + .value("Kratos_Triangle3D3", GeometryData::KratosGeometryType::Kratos_Triangle3D3) + .value("Kratos_Triangle3D6", GeometryData::KratosGeometryType::Kratos_Triangle3D6) + .value("Kratos_Line2D2", GeometryData::KratosGeometryType::Kratos_Line2D2) + .value("Kratos_Line2D3", GeometryData::KratosGeometryType::Kratos_Line2D3) + .value("Kratos_Line2D4", GeometryData::KratosGeometryType::Kratos_Line2D4) + .value("Kratos_Line2D5", GeometryData::KratosGeometryType::Kratos_Line2D5) + .value("Kratos_Line3D2", GeometryData::KratosGeometryType::Kratos_Line3D2) + .value("Kratos_Line3D3", GeometryData::KratosGeometryType::Kratos_Line3D3) + .value("Kratos_Point2D", GeometryData::KratosGeometryType::Kratos_Point2D) + .value("Kratos_Point3D", GeometryData::KratosGeometryType::Kratos_Point3D) + .value("Kratos_Sphere3D1", GeometryData::KratosGeometryType::Kratos_Sphere3D1) + .value("Kratos_Nurbs_Curve", GeometryData::KratosGeometryType::Kratos_Nurbs_Curve) + .value("Kratos_Nurbs_Surface", GeometryData::KratosGeometryType::Kratos_Nurbs_Surface) + .value("Kratos_Nurbs_Volume", GeometryData::KratosGeometryType::Kratos_Nurbs_Volume) + .value("Kratos_Nurbs_Curve_On_Surface", GeometryData::KratosGeometryType::Kratos_Nurbs_Curve_On_Surface) + .value("Kratos_Surface_In_Nurbs_Volume", GeometryData::KratosGeometryType::Kratos_Surface_In_Nurbs_Volume) + .value("Kratos_Brep_Curve", GeometryData::KratosGeometryType::Kratos_Brep_Curve) + .value("Kratos_Brep_Surface", GeometryData::KratosGeometryType::Kratos_Brep_Surface) + .value("Kratos_Brep_Curve_On_Surface", GeometryData::KratosGeometryType::Kratos_Brep_Curve_On_Surface) + .value("Kratos_Quadrature_Point_Geometry", GeometryData::KratosGeometryType::Kratos_Quadrature_Point_Geometry) + .value("Kratos_Coupling_Geometry", GeometryData::KratosGeometryType::Kratos_Coupling_Geometry) + .value("Kratos_Quadrature_Point_Curve_On_Surface_Geometry", GeometryData::KratosGeometryType::Kratos_Quadrature_Point_Curve_On_Surface_Geometry) + .value("Kratos_Quadrature_Point_Surface_In_Volume_Geometry", GeometryData::KratosGeometryType::Kratos_Quadrature_Point_Surface_In_Volume_Geometry); + + py::class_(m, "GeometryData") + .def_property_readonly_static("IntegrationMethod", [integration_method](py::object) { return integration_method; }) + .def_property_readonly_static("KratosGeometryFamily", [geometry_family](py::object) { return geometry_family; }) + .def_property_readonly_static("KratosGeometryType", [geometry_type](py::object) { return geometry_type; }); + +} + +} \ No newline at end of file diff --git a/kratos/python/add_geometry_data_to_python.h b/kratos/python/add_geometry_data_to_python.h new file mode 100644 index 000000000000..d690f579f91f --- /dev/null +++ b/kratos/python/add_geometry_data_to_python.h @@ -0,0 +1,27 @@ +// | / | +// ' / __| _` | __| _ \ __| +// . \ | ( | | ( |\__ ` +// _|\_\_| \__,_|\__|\___/ ____/ +// Multi-Physics +// +// License: BSD License +// Kratos default license: kratos/license.txt +// +// Main authors: Jordi Cotela +// + +#pragma once + +// System includes +#include + +// External includes + +// Project includes + +namespace Kratos::Python +{ + +void AddGeometryDataToPython(pybind11::module& m); + +} // namespace Kratos::Python. diff --git a/kratos/python/kratos_python.cpp b/kratos/python/kratos_python.cpp index 74486fafaa67..bc02d503c267 100644 --- a/kratos/python/kratos_python.cpp +++ b/kratos/python/kratos_python.cpp @@ -76,6 +76,7 @@ #include "add_container_expression_to_python.h" #include "add_accessors_to_python.h" #include "add_globals_to_python.h" +#include "add_geometry_data_to_python.h" namespace Kratos::Python { @@ -134,6 +135,7 @@ PYBIND11_MODULE(Kratos, m) AddConstitutiveLawToPython(m); AddSerializerToPython(m); AddTableToPython(m); + AddGeometryDataToPython(m); AddGeometriesToPython(m); AddBoundingBoxToPython(m); diff --git a/kratos/tests/test_geometry_data.py b/kratos/tests/test_geometry_data.py new file mode 100644 index 000000000000..a57120b53cc8 --- /dev/null +++ b/kratos/tests/test_geometry_data.py @@ -0,0 +1,75 @@ +import KratosMultiphysics as kratos +from KratosMultiphysics import KratosUnittest + +class TestGeometryData(KratosUnittest.TestCase): + + def setUp(self): + self.model = kratos.Model() + self.model_part = self.model.CreateModelPart("model_part") + + self.model_part.CreateNewNode(1, 0.0, 0.0, 0.0) + self.model_part.CreateNewNode(2, 1.0, 0.0, 0.0) + self.model_part.CreateNewNode(3, 1.0, 1.0, 0.0) + self.model_part.CreateNewNode(4, 0.5, 0.0, 0.0) + self.model_part.CreateNewNode(5, 0.5, 0.5, 0.0) + self.model_part.CreateNewNode(6, 0.0, 0.5, 0.0) + + self.model_part.CreateNewNode(7, 0.0, 0.0, 1.0) + + prop = self.model_part.GetProperties()[1] + + self.model_part.CreateNewElement("Element2D3N", 1, [1, 2, 3], prop) + self.model_part.CreateNewElement("Element2D6N", 2, [1, 2, 3, 4, 5, 6], prop) + self.model_part.CreateNewElement("Element3D4N", 3, [1, 2, 3, 7], prop) + + self.model_part.CreateNewCondition("LineCondition3D2N", 1, [1, 7], prop) + + + def test_geometry_data(self): + + triangle_2d_3 = self.model_part.Elements[1].GetGeometry() + self.assertEqual( + triangle_2d_3.GetDefaultIntegrationMethod(), + kratos.GeometryData.IntegrationMethod.GI_GAUSS_1) + self.assertEqual( + triangle_2d_3.GetGeometryFamily(), + kratos.GeometryData.KratosGeometryFamily.Kratos_Triangle) + self.assertEqual( + triangle_2d_3.GetGeometryType(), + kratos.GeometryData.KratosGeometryType.Kratos_Triangle2D3) + + triangle_2d_6 = self.model_part.Elements[2].GetGeometry() + self.assertEqual( + triangle_2d_6.GetDefaultIntegrationMethod(), + kratos.GeometryData.IntegrationMethod.GI_GAUSS_2) + self.assertEqual( + triangle_2d_6.GetGeometryFamily(), + kratos.GeometryData.KratosGeometryFamily.Kratos_Triangle) + self.assertEqual( + triangle_2d_6.GetGeometryType(), + kratos.GeometryData.KratosGeometryType.Kratos_Triangle2D6) + + tetrahedron_3d_4 = self.model_part.Elements[3].GetGeometry() + self.assertEqual( + tetrahedron_3d_4.GetDefaultIntegrationMethod(), + kratos.GeometryData.IntegrationMethod.GI_GAUSS_1) + self.assertEqual( + tetrahedron_3d_4.GetGeometryFamily(), + kratos.GeometryData.KratosGeometryFamily.Kratos_Tetrahedra) + self.assertEqual( + tetrahedron_3d_4.GetGeometryType(), + kratos.GeometryData.KratosGeometryType.Kratos_Tetrahedra3D4) + + line_3d_2 = self.model_part.Conditions[1].GetGeometry() + self.assertEqual( + line_3d_2.GetDefaultIntegrationMethod(), + kratos.GeometryData.IntegrationMethod.GI_GAUSS_1) + self.assertEqual( + line_3d_2.GetGeometryFamily(), + kratos.GeometryData.KratosGeometryFamily.Kratos_Linear) + self.assertEqual( + line_3d_2.GetGeometryType(), + kratos.GeometryData.KratosGeometryType.Kratos_Line3D2) + +if __name__ == "__main__": + KratosUnittest.main() \ No newline at end of file From c4864cb8b5a508c05d2cb335c25876d383aa36c2 Mon Sep 17 00:00:00 2001 From: jcotela Date: Thu, 8 Feb 2024 16:15:04 +0100 Subject: [PATCH 04/21] Use GeometryType to filter incorrect input --- .../modelers/convert_linear_tets_to_quadratic_modeler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py index 8eda5b425b92..c32c94bb05c2 100644 --- a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py +++ b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py @@ -49,7 +49,7 @@ def SetupModelPart(self): model_part = self.model[self.settings["model_part_name"].GetString()] for elem in model_part.Elements: - if elem.GetGeometry().PointsNumber() != 4: + if elem.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Tetrahedra3D4: raise Exception("Only linear tets are accepted as input (4 nodes)") KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( From e0b7169b41e134b8055002b245bc35a3b61c5fc8 Mon Sep 17 00:00:00 2001 From: jcotela Date: Thu, 15 Feb 2024 10:33:18 +0100 Subject: [PATCH 05/21] Class rename --- ...rt_linear_tetrahedra_to_quadratic_modeler.py} | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) rename applications/MeshingApplication/python_scripts/modelers/{convert_linear_tets_to_quadratic_modeler.py => convert_linear_tetrahedra_to_quadratic_modeler.py} (86%) diff --git a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py similarity index 86% rename from applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py rename to applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py index c32c94bb05c2..849200697f05 100644 --- a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tets_to_quadratic_modeler.py +++ b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py @@ -1,7 +1,7 @@ import KratosMultiphysics from KratosMultiphysics import MeshingApplication as Meshing -class ConvertLinearTetsToQuadraticModeler(KratosMultiphysics.Modeler): +class ConvertLinearTetrahedraToQuadraticModeler(KratosMultiphysics.Modeler): """ This class is a modeler in KratosMultiphysics that converts linear tetrahedral (tet) elements to quadratic tet elements within a specified model part. @@ -19,7 +19,7 @@ class ConvertLinearTetsToQuadraticModeler(KratosMultiphysics.Modeler): def __init__(self, model, settings): """ - Constructor for the ConvertLinearTetsToQuadraticModeler class. + Constructor for the ConvertLinearTetrahedraToQuadraticModeler class. Args: model (KratosMultiphysics.Model): The Kratos model containing the model part to be modified. @@ -44,7 +44,7 @@ def SetupModelPart(self): super().SetupModelPart() if KratosMultiphysics.IsDistributedRun(): - raise Exception("ConvertLinearTetsToQuadraticModeler is not MPI-ready") + raise Exception("ConvertLinearTetrahedraToQuadraticModeler is not MPI-ready") model_part = self.model[self.settings["model_part_name"].GetString()] @@ -86,22 +86,22 @@ def __GetDefaultSettings(self): def __str__(self): """ - String representation of the ConvertLinearTetsToQuadraticModeler class. + String representation of the ConvertLinearTetrahedraToQuadraticModeler class. Returns: str: A string describing the class. """ - return "ConvertLinearTetsToQuadraticModeler" + return "ConvertLinearTetrahedraToQuadraticModeler" def Factory(model, settings): """ - Factory method to create an instance of ConvertLinearTetsToQuadraticModeler. + Factory method to create an instance of ConvertLinearTetrahedraToQuadraticModeler. Args: model (KratosMultiphysics.Model): The Kratos model to be modified. settings (KratosMultiphysics.Parameters): Configuration settings for the modeler. Returns: - ConvertLinearTetsToQuadraticModeler: An instance of the modeler class. + ConvertLinearTetrahedraToQuadraticModeler: An instance of the modeler class. """ - return ConvertLinearTetsToQuadraticModeler(model, settings) + return ConvertLinearTetrahedraToQuadraticModeler(model, settings) From 7d76c4fc9a796977e831fe29d7951509150f1cae Mon Sep 17 00:00:00 2001 From: jcotela Date: Thu, 15 Feb 2024 10:44:27 +0100 Subject: [PATCH 06/21] Adding python registry to MeshingApplication --- applications/MeshingApplication/MeshingApplication.py | 4 ++++ .../MeshingApplication/python_registry_lists.py | 11 +++++++++++ 2 files changed, 15 insertions(+) create mode 100644 applications/MeshingApplication/python_registry_lists.py diff --git a/applications/MeshingApplication/MeshingApplication.py b/applications/MeshingApplication/MeshingApplication.py index 028fb7ded8d0..118c82701c0d 100644 --- a/applications/MeshingApplication/MeshingApplication.py +++ b/applications/MeshingApplication/MeshingApplication.py @@ -8,3 +8,7 @@ application_name = "KratosMeshingApplication" _ImportApplication(application, application_name) + +from KratosMultiphysics import python_registry_utilities +from . import python_registry_lists +python_registry_utilities.RegisterAll("KratosMultiphysics.MeshingApplication", python_registry_lists) \ No newline at end of file diff --git a/applications/MeshingApplication/python_registry_lists.py b/applications/MeshingApplication/python_registry_lists.py new file mode 100644 index 000000000000..967334ef8509 --- /dev/null +++ b/applications/MeshingApplication/python_registry_lists.py @@ -0,0 +1,11 @@ +python_modelers_to_be_registered = [ + "convert_linear_tetrahedra_to_quadratic_modeler.ConvertLinearTetrahedraToQuadraticModeler", +] + +python_operations_to_be_registered = [] + +python_processes_to_be_registered = [] + +python_stages_to_be_registered = [] + +python_orchestrators_to_be_registered = [] \ No newline at end of file From 0164e3e3c1f370fddbc05d62eef94c08957061ce Mon Sep 17 00:00:00 2001 From: jcotela Date: Thu, 15 Feb 2024 12:36:25 +0100 Subject: [PATCH 07/21] Install python registry file --- applications/MeshingApplication/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/applications/MeshingApplication/CMakeLists.txt b/applications/MeshingApplication/CMakeLists.txt index 43ae2f76261b..05595a319e84 100644 --- a/applications/MeshingApplication/CMakeLists.txt +++ b/applications/MeshingApplication/CMakeLists.txt @@ -219,6 +219,7 @@ endif(CMAKE_UNITY_BUILD MATCHES ON) # Add to the KratosMultiphisics Python module kratos_python_install(${INSTALL_PYTHON_USING_LINKS} ${CMAKE_CURRENT_SOURCE_DIR}/MeshingApplication.py KratosMultiphysics/MeshingApplication/__init__.py ) +kratos_python_install(${INSTALL_PYTHON_USING_LINKS} ${CMAKE_CURRENT_SOURCE_DIR}/python_registry_lists.py KratosMultiphysics/MeshingApplication/python_registry_lists.py ) # Install python files get_filename_component (CURRENT_DIR_NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME) From 7a0c5667fa992b1bd23b5adb128bcf49c9465fda Mon Sep 17 00:00:00 2001 From: jcotela Date: Fri, 16 Feb 2024 10:24:56 +0100 Subject: [PATCH 08/21] Fixing registry --- applications/MeshingApplication/python_registry_lists.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/MeshingApplication/python_registry_lists.py b/applications/MeshingApplication/python_registry_lists.py index 967334ef8509..36c2595a9771 100644 --- a/applications/MeshingApplication/python_registry_lists.py +++ b/applications/MeshingApplication/python_registry_lists.py @@ -1,5 +1,5 @@ python_modelers_to_be_registered = [ - "convert_linear_tetrahedra_to_quadratic_modeler.ConvertLinearTetrahedraToQuadraticModeler", + "modelers.convert_linear_tetrahedra_to_quadratic_modeler.ConvertLinearTetrahedraToQuadraticModeler", ] python_operations_to_be_registered = [] From 2402664ab845afd34c297fe647dc7009c566a0fa Mon Sep 17 00:00:00 2001 From: jcotela Date: Fri, 16 Feb 2024 17:08:35 +0100 Subject: [PATCH 09/21] Add tests --- .../tests/test_MeshingApplication.py | 5 +- ..._linear_tetrahedra_to_quadratic_modeler.py | 65 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py diff --git a/applications/MeshingApplication/tests/test_MeshingApplication.py b/applications/MeshingApplication/tests/test_MeshingApplication.py index 2b8b4f6b3a45..f402150e85e0 100644 --- a/applications/MeshingApplication/tests/test_MeshingApplication.py +++ b/applications/MeshingApplication/tests/test_MeshingApplication.py @@ -19,6 +19,7 @@ from test_local_refine_triangle_conditions import TestLocalRefineTriangleMeshConditions as TTestLocalRefineTriangleMeshConditions from test_local_refine_only_on_boundaries import TestLocalRefineOnlyOnBoundaries as TTestLocalRefineOnlyOnBoundaries from test_gradual_variable_interpolation_process import TestGradualVariableInterpolationProcess as TTestGradualVariableInterpolationProcess +from test_convert_linear_tetrahedra_to_quadratic_modeler import TestConvertLinearTetrahedraToQuadraticModeler ## NIGHTLY TESTS ## VALIDATION TESTS @@ -43,6 +44,8 @@ def AssembleTestSuites(): smallSuite.addTest(TTestLocalRefineTriangleMeshConditions('test_refine_condition_mesh')) smallSuite.addTest(TTestLocalRefineOnlyOnBoundaries('test_refine_on_boundary_edges')) smallSuite.addTest(TTestGradualVariableInterpolationProcess('test_gradual_variable_interpolation_process')) + smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([ + TestConvertLinearTetrahedraToQuadraticModeler])) if hasattr(MeshingApplication, "TetrahedraReconnectUtility") : smallSuite.addTest(TTestRedistance('test_refine_all')) smallSuite.addTest(TTestRedistance('test_refine_half')) @@ -107,4 +110,4 @@ def AssembleTestSuites(): KratosMultiphysics.Logger.PrintInfo("Unittests", "\nRunning python tests ...") KratosUnittest.runTests(AssembleTestSuites()) - KratosMultiphysics.Logger.PrintInfo("Unittests", "Finished python tests!") + KratosMultiphysics.Logger.PrintWarning("Unittests", "Finished python tests!") diff --git a/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py b/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py new file mode 100644 index 000000000000..b93317857b97 --- /dev/null +++ b/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py @@ -0,0 +1,65 @@ +from importlib import import_module +from pathlib import Path + +import KratosMultiphysics as kratos +import KratosMultiphysics.MeshingApplication as _ # To be sure that the app is imported and the registry is filled + +from KratosMultiphysics import KratosUnittest +from KratosMultiphysics.MeshingApplication.modelers.convert_linear_tetrahedra_to_quadratic_modeler import ConvertLinearTetrahedraToQuadraticModeler + + +class TestConvertLinearTetrahedraToQuadraticModeler(KratosUnittest.TestCase): + + def setUp(self): + kratos.Logger.GetDefaultOutput().SetSeverity(kratos.Logger.Severity.WARNING) + + self.model = kratos.Model() + model_part = self.model.CreateModelPart("main_model_part") + model_part.ProcessInfo.SetValue(kratos.DOMAIN_SIZE, 3) + + work_dir = Path(__file__).parent.absolute() + mdpa_io = kratos.ModelPartIO(work_dir / "cube_with_5_faces") + mdpa_io.ReadModelPart(model_part) + + + def make_modeler(self, parameters: kratos.Parameters): + registry_data = kratos.Registry[ + "Modelers.KratosMultiphysics.MeshingApplication.ConvertLinearTetrahedraToQuadraticModeler" + ] + module = import_module(registry_data["ModuleName"]) + return getattr(module, registry_data["ClassName"])(self.model, parameters) + + + def test_creation_from_registry(self): + modeler = self.make_modeler(kratos.Parameters('''{ + "model_part_name": "main_model_part" + }''')) + + self.assertIsInstance(modeler, ConvertLinearTetrahedraToQuadraticModeler) + + + def test_quadratic_refinement(self): + + model_part = self.model.GetModelPart("main_model_part") + + self.assertEqual(model_part.NumberOfNodes(), 64) + self.assertEqual(model_part.NumberOfElements(), 162) + self.assertEqual(model_part.NumberOfConditions(), 90) + self.assertEqual(len(model_part.Elements[1].GetNodes()), 4) + self.assertEqual(len(model_part.Conditions[163].GetNodes()), 3) + + modeler = self.make_modeler(kratos.Parameters('''{ + "model_part_name": "main_model_part" + }''')) + + modeler.SetupModelPart() + + self.assertEqual(model_part.NumberOfNodes(), 343) + self.assertEqual(model_part.NumberOfElements(), 162) + self.assertEqual(model_part.NumberOfConditions(), 90) + self.assertEqual(len(model_part.Elements[1].GetNodes()), 10) + self.assertEqual(len(model_part.Conditions[163].GetNodes()), 6) + + +if __name__ == "__main__": + KratosUnittest.main() \ No newline at end of file From 7592a65d412af2b1bf655f89da7eb70af37baa22 Mon Sep 17 00:00:00 2001 From: jcotela Date: Fri, 16 Feb 2024 17:11:36 +0100 Subject: [PATCH 10/21] Removing factory method --- ...onvert_linear_tetrahedra_to_quadratic_modeler.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py index 849200697f05..f877530de9f5 100644 --- a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py +++ b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py @@ -92,16 +92,3 @@ def __str__(self): str: A string describing the class. """ return "ConvertLinearTetrahedraToQuadraticModeler" - -def Factory(model, settings): - """ - Factory method to create an instance of ConvertLinearTetrahedraToQuadraticModeler. - - Args: - model (KratosMultiphysics.Model): The Kratos model to be modified. - settings (KratosMultiphysics.Parameters): Configuration settings for the modeler. - - Returns: - ConvertLinearTetrahedraToQuadraticModeler: An instance of the modeler class. - """ - return ConvertLinearTetrahedraToQuadraticModeler(model, settings) From a29dba67af03dc56dae713e08c67a3d76fe5d348 Mon Sep 17 00:00:00 2001 From: jcotela Date: Fri, 16 Feb 2024 17:17:09 +0100 Subject: [PATCH 11/21] Revert unwanted change in output level --- .../MeshingApplication/tests/test_MeshingApplication.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/MeshingApplication/tests/test_MeshingApplication.py b/applications/MeshingApplication/tests/test_MeshingApplication.py index f402150e85e0..29804a87e116 100644 --- a/applications/MeshingApplication/tests/test_MeshingApplication.py +++ b/applications/MeshingApplication/tests/test_MeshingApplication.py @@ -110,4 +110,4 @@ def AssembleTestSuites(): KratosMultiphysics.Logger.PrintInfo("Unittests", "\nRunning python tests ...") KratosUnittest.runTests(AssembleTestSuites()) - KratosMultiphysics.Logger.PrintWarning("Unittests", "Finished python tests!") + KratosMultiphysics.Logger.PrintInfo("Unittests", "Finished python tests!") From 8856f72005428659c9a32ca1d430dc867470b928 Mon Sep 17 00:00:00 2001 From: jcotela Date: Fri, 16 Feb 2024 17:22:13 +0100 Subject: [PATCH 12/21] Minor updates --- ...convert_linear_tetrahedra_to_quadratic_modeler.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py index f877530de9f5..691a6ed292e8 100644 --- a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py +++ b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py @@ -52,6 +52,10 @@ def SetupModelPart(self): if elem.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Tetrahedra3D4: raise Exception("Only linear tets are accepted as input (4 nodes)") + for cond in model_part.Conditions: + if cond.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Triangle3D3: + raise Exception("Only linear triangles are accepted as input condition (3 nodes)") + KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( KratosMultiphysics.SPLIT_ELEMENT, True, @@ -64,10 +68,11 @@ def SetupModelPart(self): Meshing.LinearToQuadraticTetrahedraMeshConverter(model_part.GetRootModelPart()).LocalConvertLinearToQuadraticTetrahedraMesh(False, False) element_name = self.settings["element_name"].GetString() - if element_name: + condition_name = self.settings["condition_name"].GetString() + if element_name or condition_name: replace_settings = KratosMultiphysics.Parameters("{}") replace_settings.AddString("element_name", element_name) - replace_settings.AddString("condition_name", "") + replace_settings.AddString("condition_name", condition_name) KratosMultiphysics.ReplaceElementsAndConditionsProcess(model_part, replace_settings).Execute() def __GetDefaultSettings(self): @@ -81,7 +86,8 @@ def __GetDefaultSettings(self): """ return KratosMultiphysics.Parameters('''{ "model_part_name" : "main_model_part", - "element_name" : "" + "element_name" : "", + "condition_name": "" }''') def __str__(self): From c9c6fa245db248017d4108c6edbbf97fb7305279 Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 19 Feb 2024 10:42:16 +0100 Subject: [PATCH 13/21] Using any --- ...convert_linear_tetrahedra_to_quadratic_modeler.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py index 691a6ed292e8..a7c6a8e3c566 100644 --- a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py +++ b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py @@ -48,13 +48,13 @@ def SetupModelPart(self): model_part = self.model[self.settings["model_part_name"].GetString()] - for elem in model_part.Elements: - if elem.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Tetrahedra3D4: - raise Exception("Only linear tets are accepted as input (4 nodes)") + if any(elem.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Tetrahedra3D4 \ + for elem in model_part.Elements): + raise Exception("Only linear tets are accepted as input (4 nodes)") - for cond in model_part.Conditions: - if cond.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Triangle3D3: - raise Exception("Only linear triangles are accepted as input condition (3 nodes)") + if any(cond.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Triangle3D3 \ + for cond in model_part.Conditions): + raise Exception("Only linear triangles are accepted as input condition (3 nodes)") KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( KratosMultiphysics.SPLIT_ELEMENT, From 45d86569ccb62bffe5010c8da9420e0822c5fa89 Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 19 Feb 2024 11:35:29 +0100 Subject: [PATCH 14/21] Require root model part --- ..._linear_tetrahedra_to_quadratic_modeler.py | 4 +++- ..._linear_tetrahedra_to_quadratic_modeler.py | 22 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py index a7c6a8e3c566..7df019c6dd96 100644 --- a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py +++ b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py @@ -47,6 +47,8 @@ def SetupModelPart(self): raise Exception("ConvertLinearTetrahedraToQuadraticModeler is not MPI-ready") model_part = self.model[self.settings["model_part_name"].GetString()] + if model_part != model_part.GetRootModelPart(): + raise Exception(f"A root ModelPart is required, got SubModelPart {model_part.FullName()}") if any(elem.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Tetrahedra3D4 \ for elem in model_part.Elements): @@ -65,7 +67,7 @@ def SetupModelPart(self): True, model_part.Conditions) - Meshing.LinearToQuadraticTetrahedraMeshConverter(model_part.GetRootModelPart()).LocalConvertLinearToQuadraticTetrahedraMesh(False, False) + Meshing.LinearToQuadraticTetrahedraMeshConverter(model_part).LocalConvertLinearToQuadraticTetrahedraMesh(False, False) element_name = self.settings["element_name"].GetString() condition_name = self.settings["condition_name"].GetString() diff --git a/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py b/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py index b93317857b97..f06c14dd8444 100644 --- a/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py +++ b/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py @@ -48,9 +48,11 @@ def test_quadratic_refinement(self): self.assertEqual(len(model_part.Elements[1].GetNodes()), 4) self.assertEqual(len(model_part.Conditions[163].GetNodes()), 3) - modeler = self.make_modeler(kratos.Parameters('''{ - "model_part_name": "main_model_part" - }''')) + modeler = ConvertLinearTetrahedraToQuadraticModeler( + self.model, + kratos.Parameters('''{ + "model_part_name": "main_model_part" + }''')) modeler.SetupModelPart() @@ -61,5 +63,19 @@ def test_quadratic_refinement(self): self.assertEqual(len(model_part.Conditions[163].GetNodes()), 6) + def test_root_required(self): + + modeler = ConvertLinearTetrahedraToQuadraticModeler( + self.model, + kratos.Parameters('''{ + "model_part_name": "main_model_part.MainPart.VolumeParts.Body1" + }''')) + + msg = "A root ModelPart is required, got SubModelPart main_model_part.MainPart.VolumeParts.Body1" + with self.assertRaisesRegex(Exception, msg): + modeler.SetupModelPart() + + + if __name__ == "__main__": KratosUnittest.main() \ No newline at end of file From 69e1ee05a5ed5958e1601bc7cc13ca1e4227f2bf Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 19 Feb 2024 12:05:10 +0100 Subject: [PATCH 15/21] Cleaning up --- .../test_convert_linear_tetrahedra_to_quadratic_modeler.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py b/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py index f06c14dd8444..d4e50b2ff4aa 100644 --- a/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py +++ b/applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py @@ -2,7 +2,6 @@ from pathlib import Path import KratosMultiphysics as kratos -import KratosMultiphysics.MeshingApplication as _ # To be sure that the app is imported and the registry is filled from KratosMultiphysics import KratosUnittest from KratosMultiphysics.MeshingApplication.modelers.convert_linear_tetrahedra_to_quadratic_modeler import ConvertLinearTetrahedraToQuadraticModeler @@ -11,8 +10,6 @@ class TestConvertLinearTetrahedraToQuadraticModeler(KratosUnittest.TestCase): def setUp(self): - kratos.Logger.GetDefaultOutput().SetSeverity(kratos.Logger.Severity.WARNING) - self.model = kratos.Model() model_part = self.model.CreateModelPart("main_model_part") model_part.ProcessInfo.SetValue(kratos.DOMAIN_SIZE, 3) From 996475d872965d999ec0f26a6068ee65b6f6f4ed Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 19 Feb 2024 14:01:30 +0100 Subject: [PATCH 16/21] Avoid element copy --- ...inear_to_quadratic_tetrahedra_mesh_converter_utility.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp index 6bd5784d2469..da2c8136ad0a 100644 --- a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp +++ b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp @@ -20,9 +20,9 @@ namespace Kratos { bool InterpolateInternalVariables) { //checking all elements to be refined are linear tets - block_for_each(mModelPart.Elements(), [&](Element element) { - if(element.Has(SPLIT_ELEMENT) && element.GetValue(SPLIT_ELEMENT)){ - KRATOS_ERROR_IF_NOT(element.GetGeometry().GetGeometryType() == GeometryData::KratosGeometryType::Kratos_Tetrahedra3D4) << "Element #" << element.Id() << " is not a linear tetrahedron" << std::endl; + block_for_each(mModelPart.Elements(), [&](Element& r_element) { + if(r_element.Has(SPLIT_ELEMENT) && r_element.GetValue(SPLIT_ELEMENT)){ + KRATOS_ERROR_IF_NOT(r_element.GetGeometry().GetGeometryType() == GeometryData::KratosGeometryType::Kratos_Tetrahedra3D4) << "Element #" << r_element.Id() << " is not a linear tetrahedron" << std::endl; } }); LocalRefineMesh(RefineOnReference, InterpolateInternalVariables); From e273224bee352eef61019ce06999b301db0a9bd4 Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 19 Feb 2024 14:01:46 +0100 Subject: [PATCH 17/21] Add condition check --- ...atic_tetrahedra_mesh_converter_utility.cpp | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp index da2c8136ad0a..fb34d2e95703 100644 --- a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp +++ b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp @@ -14,10 +14,10 @@ #include "custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.h" namespace Kratos { - + void LinearToQuadraticTetrahedraMeshConverter::LocalConvertLinearToQuadraticTetrahedraMesh( - bool RefineOnReference, - bool InterpolateInternalVariables) + bool RefineOnReference, + bool InterpolateInternalVariables) { //checking all elements to be refined are linear tets block_for_each(mModelPart.Elements(), [&](Element& r_element) { @@ -25,12 +25,17 @@ namespace Kratos { KRATOS_ERROR_IF_NOT(r_element.GetGeometry().GetGeometryType() == GeometryData::KratosGeometryType::Kratos_Tetrahedra3D4) << "Element #" << r_element.Id() << " is not a linear tetrahedron" << std::endl; } }); + block_for_each(mModelPart.Conditions(), [&](Condition& r_condition) { + if(r_condition.Has(SPLIT_ELEMENT) && r_condition.GetValue(SPLIT_ELEMENT)){ + KRATOS_ERROR_IF_NOT(r_condition.GetGeometry().GetGeometryType() == GeometryData::KratosGeometryType::Kratos_Tetrahedra3D4) << "Condition #" << r_condition.Id() << " is not a linear tetrahedron" << std::endl; + } + }); LocalRefineMesh(RefineOnReference, InterpolateInternalVariables); - } + } Tetrahedra3D10 LinearToQuadraticTetrahedraMeshConverter::GenerateTetrahedra( - ModelPart& rThisModelPart, - const std::vector& rNodeIds) + ModelPart& rThisModelPart, + const std::vector& rNodeIds) { unsigned int i0 = rNodeIds[0]; unsigned int i1 = rNodeIds[1]; @@ -59,8 +64,8 @@ namespace Kratos { } Triangle3D6 LinearToQuadraticTetrahedraMeshConverter::GenerateTriangle3D6( - ModelPart& rThisModelPart, - const array_1d& rNodeIds) + ModelPart& rThisModelPart, + const array_1d& rNodeIds) { unsigned int i0 = rNodeIds[0]; unsigned int i1 = rNodeIds[1]; @@ -120,7 +125,7 @@ namespace Kratos { //This method only copies the current information to the new element InterpolateInteralVariables(0, *it.base(), p_element, r_current_process_info); } - + // Transfer elemental variables to new element p_element->GetData() = it->GetData(); p_element->GetValue(SPLIT_ELEMENT) = false; @@ -168,7 +173,7 @@ namespace Kratos { // GlobalPointersVector< Element >& children = p_element->GetValue(NEIGHBOUR_ELEMENTS); auto& children = p_element->GetValue(NEIGHBOUR_ELEMENTS); p_element = children[0].shared_from_this(); - } + } } //Recursively for all subModelParts @@ -239,7 +244,7 @@ namespace Kratos { // GlobalPointersVector< Condition >& children = p_cond->GetValue(NEIGHBOUR_CONDITIONS); auto& children = p_cond->GetValue(NEIGHBOUR_CONDITIONS); p_cond = children[0].shared_from_this(); - } + } } for (ModelPart::SubModelPartIterator i_submodelpart = rThisModelPart.SubModelPartsBegin(); i_submodelpart != rThisModelPart.SubModelPartsEnd(); i_submodelpart++) From 31da75c94be157212b4adb5c42c9d81ff5ad9dcd Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 19 Feb 2024 14:28:16 +0100 Subject: [PATCH 18/21] Fix wrong check --- .../linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp index fb34d2e95703..677e794e50d0 100644 --- a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp +++ b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp @@ -27,7 +27,7 @@ namespace Kratos { }); block_for_each(mModelPart.Conditions(), [&](Condition& r_condition) { if(r_condition.Has(SPLIT_ELEMENT) && r_condition.GetValue(SPLIT_ELEMENT)){ - KRATOS_ERROR_IF_NOT(r_condition.GetGeometry().GetGeometryType() == GeometryData::KratosGeometryType::Kratos_Tetrahedra3D4) << "Condition #" << r_condition.Id() << " is not a linear tetrahedron" << std::endl; + KRATOS_ERROR_IF_NOT(r_condition.GetGeometry().GetGeometryType() == GeometryData::KratosGeometryType::Kratos_Triangle3D3) << "Condition #" << r_condition.Id() << " is not a linear tetrahedron" << std::endl; } }); LocalRefineMesh(RefineOnReference, InterpolateInternalVariables); From 8d3921e0c4f3f7ea05d3ada3bf18425219910c45 Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 19 Feb 2024 14:29:50 +0100 Subject: [PATCH 19/21] Remove duplicate check --- .../convert_linear_tetrahedra_to_quadratic_modeler.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py index 7df019c6dd96..08fc75b20ee8 100644 --- a/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py +++ b/applications/MeshingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py @@ -50,14 +50,6 @@ def SetupModelPart(self): if model_part != model_part.GetRootModelPart(): raise Exception(f"A root ModelPart is required, got SubModelPart {model_part.FullName()}") - if any(elem.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Tetrahedra3D4 \ - for elem in model_part.Elements): - raise Exception("Only linear tets are accepted as input (4 nodes)") - - if any(cond.GetGeometry().GetGeometryType() != KratosMultiphysics.GeometryData.KratosGeometryType.Kratos_Triangle3D3 \ - for cond in model_part.Conditions): - raise Exception("Only linear triangles are accepted as input condition (3 nodes)") - KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( KratosMultiphysics.SPLIT_ELEMENT, True, From 395443f390f47236207fa90f9a5ebc97a55f4bf6 Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 19 Feb 2024 20:07:54 +0100 Subject: [PATCH 20/21] Update applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp Co-authored-by: Philipp Bucher --- .../linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp index 677e794e50d0..4621f7a07848 100644 --- a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp +++ b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp @@ -20,7 +20,7 @@ namespace Kratos { bool InterpolateInternalVariables) { //checking all elements to be refined are linear tets - block_for_each(mModelPart.Elements(), [&](Element& r_element) { + block_for_each(mModelPart.Elements(), [](const Element& r_element) { if(r_element.Has(SPLIT_ELEMENT) && r_element.GetValue(SPLIT_ELEMENT)){ KRATOS_ERROR_IF_NOT(r_element.GetGeometry().GetGeometryType() == GeometryData::KratosGeometryType::Kratos_Tetrahedra3D4) << "Element #" << r_element.Id() << " is not a linear tetrahedron" << std::endl; } From dedcddd6eb762cd6c74168eebbea999a5b48d407 Mon Sep 17 00:00:00 2001 From: jcotela Date: Mon, 19 Feb 2024 20:07:59 +0100 Subject: [PATCH 21/21] Update applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp Co-authored-by: Philipp Bucher --- .../linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp index 4621f7a07848..f48f5c45b2f7 100644 --- a/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp +++ b/applications/MeshingApplication/custom_utilities/linear_to_quadratic_tetrahedra_mesh_converter_utility.cpp @@ -25,7 +25,7 @@ namespace Kratos { KRATOS_ERROR_IF_NOT(r_element.GetGeometry().GetGeometryType() == GeometryData::KratosGeometryType::Kratos_Tetrahedra3D4) << "Element #" << r_element.Id() << " is not a linear tetrahedron" << std::endl; } }); - block_for_each(mModelPart.Conditions(), [&](Condition& r_condition) { + block_for_each(mModelPart.Conditions(), [](const Condition& r_condition) { if(r_condition.Has(SPLIT_ELEMENT) && r_condition.GetValue(SPLIT_ELEMENT)){ KRATOS_ERROR_IF_NOT(r_condition.GetGeometry().GetGeometryType() == GeometryData::KratosGeometryType::Kratos_Triangle3D3) << "Condition #" << r_condition.Id() << " is not a linear tetrahedron" << std::endl; }