diff --git a/applications/MedApplication/python_scripts/modelers/import_med_modeler.py b/applications/MedApplication/python_scripts/modelers/import_med_modeler.py new file mode 100644 index 000000000000..007920c895de --- /dev/null +++ b/applications/MedApplication/python_scripts/modelers/import_med_modeler.py @@ -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) diff --git a/applications/MedApplication/tests/run_cpp_unit_tests.py b/applications/MedApplication/tests/run_cpp_unit_tests.py index f0d8e31cfa96..b04313406f21 100644 --- a/applications/MedApplication/tests/run_cpp_unit_tests.py +++ b/applications/MedApplication/tests/run_cpp_unit_tests.py @@ -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]) diff --git a/applications/MedApplication/tests/test_MedApplication.py b/applications/MedApplication/tests/test_MedApplication.py index 068e617fe356..2b10fd953c0c 100644 --- a/applications/MedApplication/tests/test_MedApplication.py +++ b/applications/MedApplication/tests/test_MedApplication.py @@ -2,9 +2,11 @@ # Import the tests of test-classes to create the suites import test_med_model_part_io +import test_import_med_modeler + def AssembleTestSuites(): - ''' Populates the test suites to run. + """Populates the test suites to run. Populates the test suites to run. At least, it should pupulate the suites: "small", "nighlty" and "all" @@ -14,31 +16,34 @@ def AssembleTestSuites(): suites: A dictionary of suites The set of suites with its test_cases added. - ''' + """ suites = KratosUnittest.KratosSuites # Create a test suit with the selected tests (Small tests): # smallSuite will contain the following tests: # - testSmallExample - smallSuite = suites['small'] + smallSuite = suites["small"] smallSuite.addTests(KratosUnittest.TestLoader().loadTestsFromTestCases([test_med_model_part_io.TestMedModelPartIO])) + smallSuite.addTests( + KratosUnittest.TestLoader().loadTestsFromTestCases([test_import_med_modeler.TestImportMedModeler]) + ) # Create a test suit with the selected tests # nightSuite will contain the following tests: # - testSmallExample # - testNightlyFirstExample # - testNightlySecondExample - nightSuite = suites['nightly'] + nightSuite = suites["nightly"] nightSuite.addTests(smallSuite) # Create a test suit that contains all the tests from every testCase # in the list: - allSuite = suites['all'] + allSuite = suites["all"] allSuite.addTests(nightSuite) return suites -if __name__ == '__main__': +if __name__ == "__main__": KratosUnittest.runTests(AssembleTestSuites()) diff --git a/applications/MedApplication/tests/test_import_med_modeler.py b/applications/MedApplication/tests/test_import_med_modeler.py new file mode 100644 index 000000000000..e79ca5ee78fb --- /dev/null +++ b/applications/MedApplication/tests/test_import_med_modeler.py @@ -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() diff --git a/applications/MedApplication/tests/test_med_model_part_io.py b/applications/MedApplication/tests/test_med_model_part_io.py index 2a5215dd6f84..9867a208e451 100644 --- a/applications/MedApplication/tests/test_med_model_part_io.py +++ b/applications/MedApplication/tests/test_med_model_part_io.py @@ -39,9 +39,7 @@ def _execute_tests(self, med_path, check_fct, print_vtk=False): med_io_read_2.ReadModelPart(self.mp_read_2) KratosMed.MedTestingUtilities.CheckModelPartsAreEqual( - self.mp_read_1, - self.mp_read_2, - check_sub_model_parts=False # until writing of SMPs is implemented + self.mp_read_1, self.mp_read_2, check_sub_model_parts=False # until writing of SMPs is implemented ) def test_empty_med_file(self):