def setUpClass(cls): cls.model = Kratos.Model() cls.model_part = cls.model.CreateModelPart("test") # add required variables to solution step list cls.model_part.AddNodalSolutionStepVariable(Kratos.VELOCITY) cls.model_part.AddNodalSolutionStepVariable(Kratos.NORMAL) cls.model_part.AddNodalSolutionStepVariable(Kratos.DENSITY) cls.model_part.AddNodalSolutionStepVariable(Kratos.PRESSURE) cls.model_part.AddNodalSolutionStepVariable(Kratos.REACTION) cls.model_part.AddNodalSolutionStepVariable(Kratos.KINEMATIC_VISCOSITY) cls.model_part.AddNodalSolutionStepVariable(Kratos.DISTANCE) cls.model_part.AddNodalSolutionStepVariable(Kratos.VISCOSITY) cls.model_part.AddNodalSolutionStepVariable(Kratos.FLAG_VARIABLE) cls.model_part.AddNodalSolutionStepVariable(Kratos.TURBULENT_VISCOSITY) cls.model_part.AddNodalSolutionStepVariable(KratosRANS.RANS_Y_PLUS) cls.model_part.AddNodalSolutionStepVariable( KratosRANS.TURBULENT_KINETIC_ENERGY) cls.model_part.AddNodalSolutionStepVariable( KratosRANS.TURBULENT_ENERGY_DISSIPATION_RATE) cls.model_part.AddNodalSolutionStepVariable( KratosRANS.TURBULENT_SPECIFIC_ENERGY_DISSIPATION_RATE) cls.model_part.AddNodalSolutionStepVariable(Kratos.FLAG_VARIABLE) cls.model_part.ProcessInfo.SetValue(Kratos.DOMAIN_SIZE, 2) cls.model_part.ProcessInfo.SetValue(Kratos.STEP, 1) ReadModelPart("BackwardFacingStepTest/backward_facing_step", cls.model_part)
def SetupModelPart(cls, mdpa_name, domain_size): cls.current_model = KratosMultiphysics.Model() cls.model_part = cls.current_model.CreateModelPart("Main") cls.model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = domain_size cls.model_part.AddNodalSolutionStepVariable(KratosMultiphysics.NORMAL) cls.model_part.AddNodalSolutionStepVariable( KratosMultiphysics.DISPLACEMENT) cls.model_part.AddNodalSolutionStepVariable( KratosMultiphysics.BULK_MODULUS) cls.model_part.AddNodalSolutionStepVariable(KratosMultiphysics.NODAL_VAUX) cls.model_part.AddNodalSolutionStepVariable( KratosMultiphysics.EXTERNAL_FORCES_VECTOR) cls.model_part.AddNodalSolutionStepVariable( KratosMultiphysics.LOCAL_AXES_MATRIX) cls.mdpa_name = GetFilePath(mdpa_name) ReadModelPart(cls.mdpa_name, cls.model_part) KratosMultiphysics.VariableUtils().SetFlag(KratosMultiphysics.SLIP, False, cls.model_part.Nodes) for condition in cls.model_part.Conditions: condition.Set(KratosMultiphysics.SLIP, True) for node in condition.GetGeometry(): node.Set(KratosMultiphysics.SLIP, True) cls.model_part.GetCommunicator().SynchronizeOrNodalFlags( KratosMultiphysics.SLIP)
def test_mpi_hessian(self): KratosMultiphysics.Logger.GetDefaultOutput().SetSeverity(KratosMultiphysics.Logger.Severity.WARNING) # We create the model part current_model = KratosMultiphysics.Model() main_model_part = current_model.CreateModelPart("MainModelPart", 2) main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DOMAIN_SIZE, 2) # We add the variables needed main_model_part.AddNodalSolutionStepVariable(KratosMultiphysics.DISTANCE) # We import the model main_model_part file_path = GetFilePath("/mmg_lagrangian_test/remesh_rectangle") ReadModelPart(file_path, main_model_part) # We calculate the gradient of the distance variable find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess(main_model_part) find_nodal_h.Execute() KratosMultiphysics.VariableUtils().SetNonHistoricalVariable(KratosMultiphysics.NODAL_AREA, 0.0, main_model_part.Nodes) ## Read reference file_name = "mmg_lagrangian_test/rectangle_distance_values.json" distance_values_file_name = GetFilePath(file_name) with open(distance_values_file_name, 'r') as f: distance_values = json.load(f) for node in main_model_part.Nodes: distance_value = distance_values[str(node.Id)] node.SetSolutionStepValue(KratosMultiphysics.DISTANCE, distance_value) metric_parameters = KratosMultiphysics.Parameters(""" { "hessian_strategy_parameters" :{ "estimate_interpolation_error" : false, "interpolation_error" : 1.0e-6, "mesh_dependent_constant" : 0.28125 }, "minimal_size" : 0.015, "maximal_size" : 0.5, "enforce_current" : false, "anisotropy_remeshing" : false, "enforce_anisotropy_relative_variable" : false } """) metric_process = MeshingApplication.ComputeHessianSolMetricProcess(main_model_part, KratosMultiphysics.DISTANCE, metric_parameters) metric_process.Execute() ## Read reference file_name = "mmg_lagrangian_test/rectangle_pre_metric_result.json" reference_file_name = GetFilePath(file_name) with open(reference_file_name, 'r') as f: reference_values = json.load(f) for node in main_model_part.Nodes: metric_tensor = node.GetValue(MeshingApplication.METRIC_TENSOR_2D) ref_gradient = reference_values[str(node.Id)] for tensor_i, tensor_i_ref in zip(metric_tensor, ref_gradient): self.assertAlmostEqual(tensor_i, tensor_i_ref)
def setUpClass(cls): cls.current_model = KratosMultiphysics.Model() cls.model_part = cls.current_model.CreateModelPart("Main") cls.model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = 3 cls.model_part.AddNodalSolutionStepVariable(KratosMultiphysics.NORMAL) cls.mdpa_name = GetFilePath( "auxiliar_files_for_python_unittest/mdpa_files/quad_sphere") ReadModelPart(cls.mdpa_name, cls.model_part)
def setUpClass(cls): cls.current_model = KratosMultiphysics.Model() cls.model_part = cls.current_model.CreateModelPart("Main") cls.model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = 2 cls.mdpa_name = GetFilePath( "auxiliar_files_for_python_unittest/mdpa_files/two_dim_symmetrical_square" ) ReadModelPart(cls.mdpa_name, cls.model_part)
def test_ReadSubSubModelParts(self): """Checks that all processor have entities from the given list of sub-sub model parts. """ self.work_folder = "" self.file_name = "cube" current_model = KratosMultiphysics.Model() model_part = current_model.CreateModelPart("Main") model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = 3 settings = KratosMultiphysics.Parameters("""{ "model_import_settings" : { "input_type": "mdpa", "input_filename" : \"""" + GetFilePath(self.file_name) + """\", "partition_in_memory" : false, "sub_model_part_list" : ["ingate", "mainPart", "submodelpart_solid"] }, "echo_level" : 0 }""") results = { "Main.submodelpart_liquid.ingate": [81, 188, 110], "Main.submodelpart_liquid.mainPart": [85, 193, 118], "Main.submodelpart_solid": [280, 810, 552] } ReadModelPart(self.file_name, model_part, settings) for submodel_part_name in results: submodel_part = current_model[submodel_part_name] local_number_nodes = submodel_part.GetCommunicator().LocalMesh( ).NumberOfNodes() local_number_elements = submodel_part.GetCommunicator().LocalMesh( ).NumberOfElements() local_number_conditions = submodel_part.GetCommunicator( ).LocalMesh().NumberOfConditions() self.assertTrue(local_number_nodes > 0) self.assertTrue(local_number_elements > 0) self.assertTrue(local_number_conditions > 0) total_nodes = submodel_part.GetCommunicator().GetDataCommunicator( ).SumAll(local_number_nodes) total_elements = submodel_part.GetCommunicator( ).GetDataCommunicator().SumAll(local_number_elements) total_conditions = submodel_part.GetCommunicator( ).GetDataCommunicator().SumAll(local_number_conditions) self.assertEqual(total_nodes, results.get(submodel_part_name)[0]) self.assertEqual(total_elements, results.get(submodel_part_name)[1]) self.assertEqual(total_conditions, results.get(submodel_part_name)[2]) total_main_nodes = model_part.GetCommunicator().GlobalNumberOfNodes() total_main_elements = model_part.GetCommunicator( ).GlobalNumberOfElements() total_main_conditions = model_part.GetCommunicator( ).GlobalNumberOfConditions() self.assertEqual(total_main_nodes, 413) self.assertEqual(total_main_elements, 1191) self.assertEqual(total_main_conditions, 780)
def test_model_part_io_read_entity_data(self): # testing if the assignment of entity data works correctly in serial and MPI current_model = KratosMultiphysics.Model() model_part = current_model.CreateModelPart("Main") model_part.AddNodalSolutionStepVariable(KratosMultiphysics.BULK_MODULUS) model_part.AddNodalSolutionStepVariable(KratosMultiphysics.NODAL_VAUX) model_part.AddNodalSolutionStepVariable(KratosMultiphysics.EXTERNAL_FORCES_VECTOR) model_part.AddNodalSolutionStepVariable(KratosMultiphysics.LOCAL_AXES_MATRIX) model_part.AddNodalSolutionStepVariable(KratosMultiphysics.PARTITION_INDEX) model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = 3 ReadModelPart(GetFilePath("auxiliar_files_for_python_unittest/mdpa_files/coarse_sphere_with_conditions"), model_part) def GetScalar(entity_id): return entity_id**2 def GetArray3(entity_id): return [entity_id+1.5, entity_id-23, entity_id*2] def GetVector(entity_id): vec = [entity_id+1, entity_id-23, entity_id*2] if entity_id > 10: vec.append(entity_id*1.5) if entity_id > 25: vec.append(entity_id**2) if entity_id > 60: vec.append(entity_id/2.5) return vec def GetMatrix(entity_id): mat = KratosMultiphysics.Matrix(2,3) for i in range(mat.Size1()): for j in range(mat.Size2()): mat[i,j] = i+j+entity_id return mat for node in model_part.Nodes: if node.Id in [1,10,55,81]: self.assertAlmostEqual(node.GetSolutionStepValue(KratosMultiphysics.BULK_MODULUS), GetScalar(node.Id)) self.assertVectorAlmostEqual(node.GetSolutionStepValue(KratosMultiphysics.NODAL_VAUX), GetArray3(node.Id)) self.assertVectorAlmostEqual(node.GetSolutionStepValue(KratosMultiphysics.EXTERNAL_FORCES_VECTOR), GetVector(node.Id)) self.assertMatrixAlmostEqual(node.GetSolutionStepValue(KratosMultiphysics.LOCAL_AXES_MATRIX), GetMatrix(node.Id)) def CheckEntities(entites, ids_to_check): for ent in entites: if ent.Id in ids_to_check: self.assertAlmostEqual(ent.GetValue(KratosMultiphysics.TEMPERATURE), GetScalar(ent.Id)) self.assertVectorAlmostEqual(ent.GetValue(KratosMultiphysics.MESH_VELOCITY), GetArray3(ent.Id)) self.assertVectorAlmostEqual(ent.GetValue(KratosMultiphysics.INITIAL_STRAIN), GetVector(ent.Id)) self.assertMatrixAlmostEqual(ent.GetValue(KratosMultiphysics.LOCAL_INERTIA_TENSOR), GetMatrix(ent.Id)) elem_ids_to_check = [5,64,33,214] cond_ids_to_check = [2,13,22,121] CheckEntities(model_part.Elements, elem_ids_to_check) CheckEntities(model_part.Conditions, cond_ids_to_check)
def SetupModelParts(self): self.model = KM.Model() self.model_part_origin = self.model.CreateModelPart("origin") self.model_part_destination = self.model.CreateModelPart("destination") self.model_part_origin.ProcessInfo[KM.DOMAIN_SIZE] = 3 self.model_part_destination.ProcessInfo[KM.DOMAIN_SIZE] = 3 self.model_part_origin.AddNodalSolutionStepVariable(KM.DISPLACEMENT) self.model_part_origin.AddNodalSolutionStepVariable(KM.FORCE) self.model_part_destination.AddNodalSolutionStepVariable(KM.DISPLACEMENT) self.model_part_destination.AddNodalSolutionStepVariable(KM.FORCE) origin_mdpa_file_name = "cube_tri" destination_mdpa_file_name = "cube_quad" ReadModelPart(GetFilePath(origin_mdpa_file_name), self.model_part_origin) ReadModelPart(GetFilePath(destination_mdpa_file_name), self.model_part_destination)
def setUpClass(cls): cls.model = Kratos.Model() cls.model_part = cls.model.CreateModelPart("FluidModelPart") # add required variables to solution step list cls.model_part.AddNodalSolutionStepVariable(Kratos.VELOCITY) cls.model_part.AddNodalSolutionStepVariable(Kratos.NORMAL) cls.model_part.AddNodalSolutionStepVariable(Kratos.DENSITY) cls.model_part.AddNodalSolutionStepVariable(Kratos.PRESSURE) cls.model_part.AddNodalSolutionStepVariable(Kratos.REACTION) cls.model_part.AddNodalSolutionStepVariable(Kratos.DISTANCE) cls.model_part.AddNodalSolutionStepVariable(Kratos.NODAL_AREA) cls.model_part.AddNodalSolutionStepVariable(Kratos.VISCOSITY) cls.model_part.AddNodalSolutionStepVariable(Kratos.FLAG_VARIABLE) cls.model_part.AddNodalSolutionStepVariable(Kratos.TURBULENT_VISCOSITY) cls.model_part.AddNodalSolutionStepVariable(KratosRANS.RANS_Y_PLUS) cls.model_part.AddNodalSolutionStepVariable( KratosRANS.TURBULENT_KINETIC_ENERGY) cls.model_part.AddNodalSolutionStepVariable( KratosRANS.TURBULENT_ENERGY_DISSIPATION_RATE) cls.model_part.AddNodalSolutionStepVariable( KratosRANS.TURBULENT_SPECIFIC_ENERGY_DISSIPATION_RATE) cls.model_part.AddNodalSolutionStepVariable(Kratos.FLAG_VARIABLE) cls.model_part.ProcessInfo.SetValue(Kratos.DOMAIN_SIZE, 2) cls.model_part.ProcessInfo.SetValue(Kratos.STEP, 1) with UnitTest.WorkFolderScope(".", __file__): ReadModelPart("BackwardFacingStepTest/backward_facing_step", cls.model_part) CheckAndPrepareModelProcess( cls.model_part, Kratos.Parameters("""{ "volume_model_part_name": "Parts_fluid", "skin_parts" : ["AutomaticInlet2D_inlet", "Outlet2D_outlet", "Slip2D"], "assign_neighbour_elements_to_conditions": true }""")).Execute() # Add constitutive laws and material properties from json file to model parts. material_settings = Kratos.Parameters("""{ "Parameters": { "materials_filename": "BackwardFacingStepTest/backward_facing_step_material_properties.json" } }""") Kratos.ReadMaterialsUtility(material_settings, cls.model) KratosRANS.RansVariableUtilities.SetElementConstitutiveLaws( cls.model_part.Elements)
def test_NodesAreNotBeingReordered(self): """Checks that all processor have entities of main model part if sub_model_parts_list is empty. """ self.work_folder = "" self.file_name = "cube" current_model = KratosMultiphysics.Model() model_part = current_model.CreateModelPart("Main") model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = 3 settings = KratosMultiphysics.Parameters("""{ "model_import_settings" : { "input_type": "mdpa", "input_filename" : \"""" + GetFilePath(self.file_name) + """\", "partition_in_memory" : false, "sub_model_part_list" : ["ingate", "mainPart", "submodelpart_solid"] }, "echo_level" : 0 }""") ReadModelPart(self.file_name, model_part, settings) results = { 1: [0.0, 0.0508782, 0.0514686], 20: [0.0, 0.0176281, 0.0138362], 50: [-0.0248201, 0.025, 0.1], 80: [-0.01241, 0.0375, 0.0], 100: [0.0244074, 0.025, 0.0], 120: [0.0244074, 0.0, 0.05], 140: [0.0244074, 0.0725731, 0.0483441], 170: [-0.0373201, 0.0619352, 0.0700256], 200: [-0.0373201, 0.1125, 0.034375], 220: [-0.000206333, 0.1125, -0.0125], 240: [0.0369074, 0.1125, 0.1125], 260: [0.0369074, -0.0125, 0.01875], 280: [0.0369074, 0.0947413, 0.0296595], 300: [-0.0248201, 0.025, 0.1], 330: [-0.000103589, 0.0983335, 0.1125], 360: [-0.0128035, 0.0, 0.00872692], 393: [-0.0159504, 0.0707544, -0.0125], 413: [-0.01241, 0.1, 0.0375] } for node in model_part.Nodes: if node in results: self.assertAlmostEqual(results.get(node.Id)[0], node.X, 7) self.assertAlmostEqual(results.get(node.Id)[1], node.Y, 7) self.assertAlmostEqual(results.get(node.Id)[2], node.Z, 7)
def setUpClass(cls): cls.current_model = KratosMultiphysics.Model() cls.model_part = cls.current_model.CreateModelPart("Main") cls.model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = 3 cls.model_part.AddNodalSolutionStepVariable(KratosMultiphysics.NORMAL) cls.model_part.AddNodalSolutionStepVariable( KratosMultiphysics.BULK_MODULUS) cls.model_part.AddNodalSolutionStepVariable( KratosMultiphysics.NODAL_VAUX) cls.model_part.AddNodalSolutionStepVariable( KratosMultiphysics.EXTERNAL_FORCES_VECTOR) cls.model_part.AddNodalSolutionStepVariable( KratosMultiphysics.LOCAL_AXES_MATRIX) cls.mdpa_name = GetFilePath( "auxiliar_files_for_python_unittest/mdpa_files/coarse_sphere_with_conditions" ) ReadModelPart(cls.mdpa_name, cls.model_part)
def test_global_neighbour_pointers(self): kratos_comm = KratosMultiphysics.DataCommunicator.GetDefault() current_model = KratosMultiphysics.Model() main_model_part = current_model.CreateModelPart("main_model_part") main_model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE]=2 ## Add variables to the model part main_model_part.AddNodalSolutionStepVariable(KratosMultiphysics.DENSITY) main_model_part.AddNodalSolutionStepVariable(KratosMultiphysics.VISCOSITY) main_model_part.AddNodalSolutionStepVariable(KratosMultiphysics.DISPLACEMENT) main_model_part.AddNodalSolutionStepVariable(KratosMultiphysics.PARTITION_INDEX) ## Serial partition of the original .mdpa file input_filename = GetFilePath("test_mpi_communicator") ReadModelPart(input_filename, main_model_part) #compute nodal neighbours neighbour_finder = KratosMultiphysics.FindGlobalNodalElementalNeighboursProcess(kratos_comm, main_model_part) neighbour_finder.Execute() #obtain the ids of all the neighbours found_ids = neighbour_finder.GetNeighbourIds(main_model_part.Nodes) reference_ids = { 1 : [1,2], 2 : [2,3,4], 3 : [4], 4 : [1,5,6], 5 : [1,2,3,6,7,8], 6 : [3,4,8], 7 : [5], 8 : [5,6,7], 9 : [7,8] } #do the check for key,values in found_ids.items(): ref_values = reference_ids[key] for i in range(len(ref_values)): self.assertEqual(values[i],ref_values[i])
def testDiscontinuousDistanceProcessCutOnEdge2D(self): communicator = KratosMultiphysics.Testing.GetDefaultDataCommunicator() model = KratosMultiphysics.Model() main_model_part = model.CreateModelPart('main') main_model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = 2 main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.PARTITION_INDEX) main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.DISTANCE) input_filename = GetFilePath( "auxiliar_files_for_python_unittest/mdpa_files/test_mpi_distance") ReadModelPart(input_filename, main_model_part) skin_model_part = model.CreateModelPart('skin') skin_model_part.CreateNewNode(1, -0.25, 0.0, 0.0) skin_model_part.CreateNewNode(2, 0.4, 0.0, 0.0) skin_model_part.CreateNewElement("Element2D2N", 1, [1, 2], KratosMultiphysics.Properties(0)) flags = KratosMultiphysics.CalculateDiscontinuousDistanceToSkinProcess2D.CALCULATE_ELEMENTAL_EDGE_DISTANCES | \ KratosMultiphysics.CalculateDiscontinuousDistanceToSkinProcess2D.USE_POSITIVE_EPSILON_FOR_ZERO_VALUES KratosMultiphysics.CalculateDiscontinuousDistanceToSkinProcess2D( main_model_part, skin_model_part, flags).Execute() n_intersected, n_incised = _GetNumberIncisedAndIntersectedElements( main_model_part, 3) n_intersected = communicator.SumAll(n_intersected) n_incised = communicator.SumAll(n_incised) self.assertEqual(n_intersected, 1) self.assertEqual(n_incised, 2) for elem in main_model_part.Elements: edge_distances = elem.GetValue( KratosMultiphysics.ELEMENTAL_EDGE_DISTANCES) if elem.Id == 2: self.assertVectorAlmostEqual(edge_distances, [1.0, 0.0, -1.0]) if elem.Id == 5: self.assertVectorAlmostEqual(edge_distances, [-1.0, 0.0, -1.0])
def _read_model_part_mpi(self, main_model_part): if KratosMultiphysics.DataCommunicator.GetDefault().Size() == 1: self.skipTest( "Test can be run only using more than one mpi process") ## Add variables to the model part main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.DENSITY) main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.VISCOSITY) main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.DISPLACEMENT) main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.PARTITION_INDEX) ReadModelPart(GetFilePath("test_mpi_communicator"), main_model_part) ## Check submodelpart of each main_model_part of each processor self.assertTrue(main_model_part.HasSubModelPart("Skin")) skin_sub_model_part = main_model_part.GetSubModelPart("Skin")
def test_ReadWithoutSubModelParts(self): """Checks that all processor have entities of main model part if sub_model_parts_list is empty. """ self.work_folder = "" self.file_name = "cube" current_model = KratosMultiphysics.Model() model_part = current_model.CreateModelPart("Main") model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = 3 settings = KratosMultiphysics.Parameters("""{ "model_import_settings" : { "input_type": "mdpa", "input_filename" : \"""" + GetFilePath(self.file_name) + """\", "partition_in_memory" : false }, "echo_level" : 0 }""") ReadModelPart(self.file_name, model_part, settings) local_main_number_nodes = model_part.GetCommunicator().LocalMesh( ).NumberOfNodes() local_main_number_elements = model_part.GetCommunicator().LocalMesh( ).NumberOfElements() local_main_number_conditions = model_part.GetCommunicator().LocalMesh( ).NumberOfConditions() self.assertTrue(local_main_number_nodes > 0) self.assertTrue(local_main_number_elements > 0) self.assertTrue(local_main_number_conditions > 0) total_main_nodes = model_part.GetCommunicator().GlobalNumberOfNodes() total_main_elements = model_part.GetCommunicator( ).GlobalNumberOfElements() total_main_conditions = model_part.GetCommunicator( ).GlobalNumberOfConditions() self.assertEqual(total_main_nodes, 413) self.assertEqual(total_main_elements, 1191) self.assertEqual(total_main_conditions, 780)
def test_mpi_sphere(self): KratosMultiphysics.Logger.GetDefaultOutput().SetSeverity(KratosMultiphysics.Logger.Severity.WARNING) # We create the model part current_model = KratosMultiphysics.Model() main_model_part = current_model.CreateModelPart("MainModelPart") main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DOMAIN_SIZE, 3) # We add the variables needed main_model_part.AddNodalSolutionStepVariable(KratosMultiphysics.DISTANCE) main_model_part.AddNodalSolutionStepVariable(KratosMultiphysics.DISTANCE_GRADIENT) # We import the model main_model_part file_path = GetFilePath("/parmmg_eulerian_test/background_mesh_sphere") ReadModelPart(file_path, main_model_part) communicator = main_model_part.GetCommunicator().GetDataCommunicator() for node in main_model_part.Nodes: distance = math.sqrt(node.X**2+node.Y**2+node.Z**2) - 1.0/2.0 node.SetSolutionStepValue(KratosMultiphysics.DISTANCE,distance) ##COMPUTE DISTANCE GRADIENT AND NODAL_H local_gradient = KratosMultiphysics.ComputeNodalGradientProcess3D(main_model_part, KratosMultiphysics.DISTANCE, KratosMultiphysics.DISTANCE_GRADIENT, KratosMultiphysics.NODAL_AREA) local_gradient.Execute() find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess(main_model_part) find_nodal_h.Execute() ##COMPUTE LEVEL SET METRIC metric_parameters = KratosMultiphysics.Parameters(""" { "minimal_size" : 0.5, "sizing_parameters": { "reference_variable_name" : "DISTANCE", "boundary_layer_max_distance" : 2.0, "interpolation" : "constant" }, "enforce_current" : false, "anisotropy_remeshing" : false } """) metric_process = KratosMultiphysics.MeshingApplication.ComputeLevelSetSolMetricProcess3D( main_model_part, KratosMultiphysics.DISTANCE_GRADIENT, metric_parameters) metric_process.Execute() ##PERFORM REMESHING pmmg_parameters = KratosMultiphysics.Parameters(""" { "filename" : "output", "save_external_files" : true, "save_colors_files" : true, "initialize_entities" : false, "preserve_flags" : false, "echo_level" : 0, "advanced_parameters" : { "number_of_iterations" : 4 } } """) pmmg_parameters["filename"].SetString(GetFilePath(pmmg_parameters["filename"].GetString())) pmmg_process = KratosMultiphysics.MeshingApplication.ParMmgProcess3D(main_model_part.GetRootModelPart(), pmmg_parameters) pmmg_process.Execute() reference_file_name = GetFilePath("parmmg_eulerian_test/cond_ref_map.json") result_file_name = GetFilePath("output_step=0_"+str(communicator.Rank())+".cond.ref.json") self._CompareColorFiles(reference_file_name, result_file_name) reference_file_name = GetFilePath("parmmg_eulerian_test/elem_ref_map.json") result_file_name = GetFilePath("output_step=0_"+str(communicator.Rank())+".elem.ref.json") self._CompareColorFiles(reference_file_name, result_file_name) result_dict_file_name=GetFilePath("parmmg_eulerian_test/reference_parmmg_spehere_mdpa_hierarchy.json") with open(result_dict_file_name, 'r') as f: reference_hierarchy = json.load(f) self.CheckModelPartHierarchie(main_model_part, reference_hierarchy[str(communicator.Size())]) for file_name in os.listdir(GetFilePath("")): if file_name.endswith(".json") or file_name.endswith(".mdpa") or file_name.endswith(".mesh") or file_name.endswith(".sol"): kratos_utilities.DeleteFileIfExisting(GetFilePath(file_name)) kratos_utilities.DeleteTimeFiles(os.getcwd())
def testComputeNodalGradientProcess(self): current_model = KratosMultiphysics.Model() main_model_part = current_model.CreateModelPart("main_model_part") main_model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] = 2 ## Add variables to the model part main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.PARTITION_INDEX) main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.DISTANCE) main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.DISTANCE_GRADIENT) ## Serial partition of the original .mdpa file input_filename = GetFilePath( "auxiliar_files_for_python_unittest/mdpa_files/test_mpi_serializer" ) ReadModelPart(input_filename, main_model_part) for node in main_model_part.Nodes: distance = node.X**2 + node.Y**2 + node.Z**2 - 1 node.SetSolutionStepValue(KratosMultiphysics.DISTANCE, distance) node.SetValue(KratosMultiphysics.DISTANCE, distance) ## Read reference file_name = "auxiliar_files_for_python_unittest/reference_files/test_compute_nodal_gradient_process_results.json" reference_file_name = GetFilePath(file_name) with open(reference_file_name, 'r') as f: reference_values = json.load(f) gradient_process_hist_hist = KratosMultiphysics.ComputeNodalGradientProcess( main_model_part, KratosMultiphysics.DISTANCE, KratosMultiphysics.DISTANCE_GRADIENT, KratosMultiphysics.NODAL_AREA) gradient_process_hist_hist.Execute() for node in main_model_part.Nodes: distance_gradient = node.GetSolutionStepValue( KratosMultiphysics.DISTANCE_GRADIENT) ref_gradient = reference_values[str(node.Id)] for gradient_i, gradient_i_ref in zip(distance_gradient, ref_gradient): self.assertAlmostEqual(gradient_i, gradient_i_ref) non_historical_origin_variable = True gradient_process_non_hist_hist = KratosMultiphysics.ComputeNodalGradientProcess( main_model_part, KratosMultiphysics.DISTANCE, KratosMultiphysics.DISTANCE_GRADIENT, KratosMultiphysics.NODAL_AREA, non_historical_origin_variable) gradient_process_non_hist_hist.Execute() for node in main_model_part.Nodes: distance_gradient = node.GetSolutionStepValue( KratosMultiphysics.DISTANCE_GRADIENT) ref_gradient = reference_values[str(node.Id)] for gradient_i, gradient_i_ref in zip(distance_gradient, ref_gradient): self.assertAlmostEqual(gradient_i, gradient_i_ref) gradient_parameters = KratosMultiphysics.Parameters(""" { "origin_variable" : "DISTANCE", "gradient_variable" : "DISTANCE_GRADIENT", "non_historical_origin_variable" : false } """) gradient_process = KratosMultiphysics.ComputeNodalGradientProcess( main_model_part, gradient_parameters) gradient_process.Execute() for node in main_model_part.Nodes: distance_gradient = node.GetSolutionStepValue( KratosMultiphysics.DISTANCE_GRADIENT) ref_gradient = reference_values[str(node.Id)] for i_gradient, i_reference in zip(distance_gradient, ref_gradient): self.assertAlmostEqual(i_gradient, i_reference) gradient_parameters = KratosMultiphysics.Parameters(""" { "origin_variable" : "DISTANCE", "gradient_variable" : "DISTANCE_GRADIENT", "non_historical_origin_variable" : true } """) gradient_process = KratosMultiphysics.ComputeNodalGradientProcess( main_model_part, gradient_parameters) gradient_process.Execute() for node in main_model_part.Nodes: distance_gradient = node.GetSolutionStepValue( KratosMultiphysics.DISTANCE_GRADIENT) ref_gradient = reference_values[str(node.Id)] for i_gradient, i_reference in zip(distance_gradient, ref_gradient): self.assertAlmostEqual(i_gradient, i_reference)