-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12023 from KratosMultiphysics/feature/linear_to_q…
…uadratic_tetrahedra_modeler [Meshing] Adding linear to quadratic tets modeler
- Loading branch information
Showing
12 changed files
with
426 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
python_modelers_to_be_registered = [ | ||
"modelers.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 = [] |
94 changes: 94 additions & 0 deletions
94
...hingApplication/python_scripts/modelers/convert_linear_tetrahedra_to_quadratic_modeler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import KratosMultiphysics | ||
from KratosMultiphysics import MeshingApplication as Meshing | ||
|
||
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. | ||
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): | ||
""" | ||
Constructor for the ConvertLinearTetrahedraToQuadraticModeler 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 | ||
|
||
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(): | ||
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()}") | ||
|
||
KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( | ||
KratosMultiphysics.SPLIT_ELEMENT, | ||
True, | ||
model_part.Elements) | ||
KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( | ||
KratosMultiphysics.SPLIT_ELEMENT, | ||
True, | ||
model_part.Conditions) | ||
|
||
Meshing.LinearToQuadraticTetrahedraMeshConverter(model_part).LocalConvertLinearToQuadraticTetrahedraMesh(False, False) | ||
|
||
element_name = self.settings["element_name"].GetString() | ||
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", condition_name) | ||
KratosMultiphysics.ReplaceElementsAndConditionsProcess(model_part, replace_settings).Execute() | ||
|
||
def __GetDefaultSettings(self): | ||
""" | ||
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" : "", | ||
"condition_name": "" | ||
}''') | ||
|
||
def __str__(self): | ||
""" | ||
String representation of the ConvertLinearTetrahedraToQuadraticModeler class. | ||
Returns: | ||
str: A string describing the class. | ||
""" | ||
return "ConvertLinearTetrahedraToQuadraticModeler" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
applications/MeshingApplication/tests/test_convert_linear_tetrahedra_to_quadratic_modeler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from importlib import import_module | ||
from pathlib import Path | ||
|
||
import KratosMultiphysics as kratos | ||
|
||
from KratosMultiphysics import KratosUnittest | ||
from KratosMultiphysics.MeshingApplication.modelers.convert_linear_tetrahedra_to_quadratic_modeler import ConvertLinearTetrahedraToQuadraticModeler | ||
|
||
|
||
class TestConvertLinearTetrahedraToQuadraticModeler(KratosUnittest.TestCase): | ||
|
||
def setUp(self): | ||
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 = ConvertLinearTetrahedraToQuadraticModeler( | ||
self.model, | ||
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) | ||
|
||
|
||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.