-
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 #11685 from KratosMultiphysics/med/med-import-modeler
- Loading branch information
Showing
5 changed files
with
124 additions
and
11 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
applications/MedApplication/python_scripts/modelers/import_med_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,48 @@ | ||
import KratosMultiphysics as KM | ||
import KratosMultiphysics.MedApplication as KratosMed | ||
|
||
|
||
class ImportMedModeler(KM.Modeler): | ||
def __init__(self, model, settings): | ||
super().__init__(model, settings) | ||
|
||
# Cannot validate as settings may differ among input types | ||
settings.AddMissingParameters(self.__GetDefaultSettings()) | ||
|
||
self.input_filename: str = settings["input_filename"].GetString() | ||
|
||
# Create the import destination model part | ||
# It is mandatory to do this when the modeler is instantiated to have the model part created before the solvers add the variables | ||
model_part_name: str = settings["model_part_name"].GetString() | ||
if not model_part_name: | ||
raise Exception( | ||
"Missing 'model_part_name' in input settings. This is where the imported model part is to be stored." | ||
) | ||
|
||
self.model_part: KM.ModelPart = model.CreateModelPart(model_part_name) | ||
|
||
def SetupGeometryModel(self): | ||
super().SetupGeometryModel() | ||
|
||
KratosMed.MedModelPartIO(self.input_filename, KM.IO.READ).ReadModelPart(self.model_part) | ||
|
||
def PrepareGeometryModel(self): | ||
super().PrepareGeometryModel() | ||
|
||
def SetupModelPart(self): | ||
super().SetupModelPart() | ||
|
||
@classmethod | ||
def __GetDefaultSettings(cls): | ||
default_settings = KM.Parameters( | ||
"""{ | ||
"echo_level" : 0, | ||
"input_filename" : "", | ||
"model_part_name" : "" | ||
}""" | ||
) | ||
return default_settings | ||
|
||
|
||
def Factory(model, settings): | ||
return ImportMedModeler(model, settings) |
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import KratosMultiphysics as KM | ||
from KratosMultiphysics.MedApplication import * | ||
import sys | ||
|
||
if len(sys.argv) < 2: | ||
KM.Tester.SetVerbosity(KM.Tester.Verbosity.PROGRESS) # TESTS_OUTPUTS | ||
KM.Tester.SetVerbosity(KM.Tester.Verbosity.PROGRESS) # TESTS_OUTPUTS | ||
KM.Tester.RunTestSuite("KratosMedFastSuite") | ||
else : | ||
else: | ||
KM.Tester.SetVerbosity(KM.Tester.Verbosity.TESTS_OUTPUTS) | ||
KM.Tester.RunTestCases(sys.argv[1]) |
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
61 changes: 61 additions & 0 deletions
61
applications/MedApplication/tests/test_import_med_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,61 @@ | ||
# Importing the Kratos Library | ||
import KratosMultiphysics as KM | ||
import KratosMultiphysics.KratosUnittest as KratosUnittest | ||
import KratosMultiphysics.MedApplication as KratosMed | ||
from KratosMultiphysics.MedApplication.modelers.import_med_modeler import ImportMedModeler | ||
from pathlib import Path | ||
from testing_utilities import MedModelPartIOTestCase, GetMedPath, get_num_geometries_by_type | ||
|
||
|
||
class TestImportMedModeler(MedModelPartIOTestCase): | ||
def test_import_med_modeler(self): | ||
# Set up the import model part modeler | ||
model = KM.Model() | ||
settings = KM.Parameters( | ||
"""{ | ||
"echo_level" : 0, | ||
"input_filename" : "", | ||
"model_part_name" : "Main" | ||
}""" | ||
) | ||
settings["input_filename"].SetString(str(GetMedPath(Path("hexahedral_8N")))) | ||
import_mdpa_modeler = ImportMedModeler(model, settings) | ||
|
||
# Get the model part created by the modeler | ||
model_part = model.GetModelPart(settings["model_part_name"].GetString()) | ||
|
||
# Call the modeler methods | ||
import_mdpa_modeler.SetupGeometryModel() | ||
import_mdpa_modeler.PrepareGeometryModel() | ||
import_mdpa_modeler.SetupModelPart() | ||
|
||
self._basic_checks(model_part) | ||
|
||
# Check read ModelPart | ||
self.assertEqual(model_part.NumberOfNodes(), 216) | ||
self.assertEqual(model_part.NumberOfGeometries(), 371) | ||
|
||
# check how many geoms of each type | ||
exp_geoms = {KM.Hexahedra3D8: 125, KM.Quadrilateral3D4: 150, KM.Line3D2: 60, KM.Geometry: 36} | ||
self.assertEqual(sum(exp_geoms.values()), model_part.NumberOfGeometries()) | ||
self.assertDictEqual(exp_geoms, get_num_geometries_by_type(model_part)) | ||
|
||
self.assertAlmostEqual(KratosMed.MedTestingUtilities.ComputeLength(model_part), 3200) | ||
self.assertAlmostEqual(KratosMed.MedTestingUtilities.ComputeArea(model_part), 340000) | ||
self.assertAlmostEqual(KratosMed.MedTestingUtilities.ComputeVolume(model_part), 10000000) | ||
self.assertAlmostEqual(KratosMed.MedTestingUtilities.ComputeDomainSize(model_part), 10343200) | ||
|
||
for node in model_part.Nodes: | ||
self.assertTrue(0.0 <= node.X <= 500.0) | ||
self.assertTrue(0.0 <= node.X0 <= 500.0) | ||
|
||
self.assertTrue(0.0 <= node.Y <= 100.0) | ||
self.assertTrue(0.0 <= node.Y0 <= 100.0) | ||
|
||
self.assertTrue(0.0 <= node.Z <= 200.0) | ||
self.assertTrue(0.0 <= node.Z0 <= 200.0) | ||
|
||
|
||
if __name__ == "__main__": | ||
KM.Logger.GetDefaultOutput().SetSeverity(KM.Logger.Severity.WARNING) | ||
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