class EmbeddedVelocityInletEmulationTest(UnitTest.TestCase): def testEmbeddedVelocityInletEmulationSymbolic2D(self): self.print_output = False self.check_tolerance = 1.0e-10 self.print_reference_values = False self.work_folder = "EmbeddedVelocityInletEmulationTest" self.reference_file = "reference_embedded_symbolic_navier_stokes" self.settings = "EmbeddedVelocityInletEmulationTest.json" self.formulation_settings = KratosMultiphysics.Parameters(r'''{ "element_type" : "embedded_symbolic_navier_stokes", "is_slip" : false, "slip_length" : 1.0e8, "penalty_coefficient" : 10.0, "dynamic_tau" : 1.0 }''') self.ExecuteTest() def testEmbeddedVelocityInletEmulationEmbedded2D(self): self.print_output = False self.check_tolerance = 1.0e-10 self.print_reference_values = False self.work_folder = "EmbeddedVelocityInletEmulationTest" self.reference_file = "reference_embedded_navier_stokes" self.settings = "EmbeddedVelocityInletEmulationTest.json" self.formulation_settings = KratosMultiphysics.Parameters(r'''{ "element_type": "embedded_navier_stokes", "is_slip": false, "slip_length": 1.0e8, "penalty_coefficient": 10.0, "dynamic_tau": 1.0 }''') self.ExecuteTest() def ExecuteTest(self): self.buildSimulation() self.runTest() self.tearDown() self.checkResults() def buildSimulation(self): with UnitTest.WorkFolderScope(self.work_folder, __file__): with open(self.settings, 'r') as parameter_file: self.ProjectParameters = KratosMultiphysics.Parameters( parameter_file.read()) self.ProjectParameters["solver_settings"][ "formulation"] = self.formulation_settings if self.print_output: self.ProjectParameters[ "output_processes"] = self._get_output_process_settings( ) self.model = KratosMultiphysics.Model() self.simulation = FluidDynamicsAnalysis(self.model, self.ProjectParameters) def runTest(self): with UnitTest.WorkFolderScope(self.work_folder, __file__): self.simulation.Initialize() self._set_inlet_emulation_simulation() self.simulation.RunSolutionLoop() self.simulation.Finalize() def checkResults(self): with UnitTest.WorkFolderScope(self.work_folder, __file__): ## 2D results check main_model_part = self.simulation._GetSolver().main_model_part if (main_model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] == 2): if self.print_reference_values: with open(self.reference_file + '.csv', 'w') as ref_file: ref_file.write( "#ID, VELOCITY_X, VELOCITY_Y, PRESSURE\n") for node in main_model_part.Nodes: vel = node.GetSolutionStepValue( KratosMultiphysics.VELOCITY, 0) pres = node.GetSolutionStepValue( KratosMultiphysics.PRESSURE, 0) ref_file.write("{0}, {1}, {2}, {3}\n".format( node.Id, vel[0], vel[1], pres)) else: with open(self.reference_file + '.csv', 'r') as reference_file: reference_file.readline() # skip header line = reference_file.readline() for node in main_model_part.Nodes: values = [ float(i) for i in line.rstrip('\n ').split(',') ] reference_vel_x = values[1] reference_vel_y = values[2] reference_pres = values[3] velocity = node.GetSolutionStepValue( KratosMultiphysics.VELOCITY) pressure = node.GetSolutionStepValue( KratosMultiphysics.PRESSURE) self.assertAlmostEqual(reference_vel_x, velocity[0], delta=self.check_tolerance) self.assertAlmostEqual(reference_vel_y, velocity[1], delta=self.check_tolerance) self.assertAlmostEqual(reference_pres, pressure, delta=self.check_tolerance) line = reference_file.readline() if line != '': # If we did not reach the end of the reference file self.fail( "The number of nodes in the mdpa is smaller than the number of nodes in the output file" ) def tearDown(self): with UnitTest.WorkFolderScope(self.work_folder, __file__): KratosUtilities.DeleteFileIfExisting( self.ProjectParameters["solver_settings"] ["model_import_settings"]["input_filename"].GetString() + '.time') def _set_inlet_emulation_simulation(self): main_model_part = self.simulation._GetSolver().main_model_part # Set distance field x_level_set = 2.5 for node in main_model_part.Nodes: node.SetSolutionStepValue(KratosMultiphysics.DISTANCE, node.X - x_level_set) # Set embedded velocity in cut elements and deactivate negative distance elements embedded_velocity = KratosMultiphysics.Vector(3) embedded_velocity[0] = 1.0 embedded_velocity[1] = 0.0 embedded_velocity[2] = 0.0 deactivate_negative_elems = True for elem in main_model_part.Elements: n_pos = 0 n_neg = 0 for node in elem.GetNodes(): if node.GetSolutionStepValue( KratosMultiphysics.DISTANCE) < 0.0: n_neg += 1 else: n_pos += 1 if (n_neg != 0 and n_pos != 0): for node in elem.GetNodes(): node.SetValue(KratosMultiphysics.EMBEDDED_VELOCITY, embedded_velocity) elif (n_neg == len(elem.GetNodes()) and deactivate_negative_elems): elem.Set(KratosMultiphysics.ACTIVE, False) def _get_output_process_settings(self): return KratosMultiphysics.Parameters(r'''{ "gid_output" : [{ "python_module" : "gid_output_process", "kratos_module" : "KratosMultiphysics", "process_name" : "GiDOutputProcess", "help" : "This process writes postprocessing files for GiD", "Parameters" : { "model_part_name" : "FluidModelPart.fluid_computational_model_part", "output_name" : "embedded_velocity_inlet_emulation_test", "postprocess_parameters" : { "result_file_configuration" : { "gidpost_flags" : { "GiDPostMode" : "GiD_PostBinary", "WriteDeformedMeshFlag" : "WriteDeformed", "WriteConditionsFlag" : "WriteConditions", "MultiFileFlag" : "SingleFile" }, "file_label" : "time", "output_control_type" : "step", "output_frequency" : 1.0, "body_output" : true, "node_output" : false, "skin_output" : false, "plane_output" : [], "nodal_results" : ["VELOCITY","PRESSURE","DISTANCE"], "gauss_point_results" : [] }, "point_data_configuration" : [] } } }] }''')
class EmbeddedReservoirDiscontinuousTest(UnitTest.TestCase): def testEmbeddedReservoirDiscontinuous3D(self): self.distance = 0.99 self.slip_level_set = True self.work_folder = "EmbeddedReservoirDiscontinuousTest" self.reference_file = "reference_embedded_reservoir_discontinuous_3D" self.settings = "EmbeddedReservoirDiscontinuous3DTestParameters.json" self.ExecuteEmbeddedReservoirTest() def ExecuteEmbeddedReservoirTest(self): with UnitTest.WorkFolderScope(self.work_folder, __file__): self.setUp() self.setUpProblem() self.runTest() self.tearDown() self.checkResults() def setUp(self): self.check_tolerance = 1.0e-8 self.print_output = False self.print_reference_values = False def tearDown(self): with UnitTest.WorkFolderScope(self.work_folder, __file__): KratosUtilities.DeleteFileIfExisting( self.ProjectParameters["solver_settings"] ["model_import_settings"]["input_filename"].GetString() + '.time') def setUpProblem(self): with UnitTest.WorkFolderScope(self.work_folder, __file__): with open(self.settings, 'r') as parameter_file: self.ProjectParameters = KratosMultiphysics.Parameters( parameter_file.read()) self.Model = KratosMultiphysics.Model() self.simulation = FluidDynamicsAnalysis(self.Model, self.ProjectParameters) def setUpDistanceField(self): # Get the model part containing the domain fluid_model_part = self.simulation._GetSolver().main_model_part # Set continuous distance field for node in fluid_model_part.Nodes: d = self.distance - node.Z node.SetSolutionStepValue(KratosMultiphysics.DISTANCE, d) # Set discontinuous distance field for element in fluid_model_part.Elements: i_node = 0 elem_dist = KratosMultiphysics.Vector(4) for node in element.GetNodes(): elem_dist[i_node] = node.GetSolutionStepValue( KratosMultiphysics.DISTANCE) i_node += 1 element.SetValue(KratosMultiphysics.ELEMENTAL_DISTANCES, elem_dist) def setUpInitialCondition(self): # Set exact initial solution v_zero = KratosMultiphysics.Vector(3, 0.0) for node in self.simulation._GetSolver().main_model_part.Nodes: node.SetSolutionStepValue(KratosMultiphysics.VELOCITY, 0, v_zero) node.SetSolutionStepValue(KratosMultiphysics.VELOCITY, 1, v_zero) node.SetSolutionStepValue(KratosMultiphysics.VELOCITY, 2, v_zero) if node.GetSolutionStepValue(KratosMultiphysics.DISTANCE) < 0.0: node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 0, 1.0e+06) node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 1, 1.0e+06) node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 2, 1.0e+06) else: node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 0, 0.0) node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 1, 0.0) node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 2, 0.0) def runTest(self): with UnitTest.WorkFolderScope(self.work_folder, __file__): # Set up the test self.simulation.Initialize() self.setUpDistanceField() self.setUpInitialCondition() # Run the test self.simulation.RunSolutionLoop() # Finalize the test self.simulation.Finalize() def checkResults(self): with UnitTest.WorkFolderScope(self.work_folder, __file__): fluid_model_part = self.simulation._GetSolver().main_model_part if self.print_reference_values: with open(self.reference_file + '.csv', 'w') as ref_file: ref_file.write( "#ID, PRESSURE, VELOCITY_X, VELOCITY_Y, VELOCITY_Z\n") for node in fluid_model_part.Nodes: pres = node.GetSolutionStepValue( KratosMultiphysics.PRESSURE) v_x = node.GetSolutionStepValue( KratosMultiphysics.VELOCITY_X) v_y = node.GetSolutionStepValue( KratosMultiphysics.VELOCITY_Y) v_z = node.GetSolutionStepValue( KratosMultiphysics.VELOCITY_Z) ref_file.write("{0}, {1}, {2}, {3}, {4}\n".format( node.Id, pres, v_x, v_y, v_z)) else: with open(self.reference_file + '.csv', 'r') as reference_file: reference_file.readline() # skip header line = reference_file.readline() for node in fluid_model_part.Nodes: values = [ float(i) for i in line.rstrip('\n ').split(',') ] reference_pres = values[1] reference_v_x = values[2] reference_v_y = values[3] reference_v_z = values[4] pres = node.GetSolutionStepValue( KratosMultiphysics.PRESSURE) v_x = node.GetSolutionStepValue( KratosMultiphysics.VELOCITY_X) v_y = node.GetSolutionStepValue( KratosMultiphysics.VELOCITY_Y) v_z = node.GetSolutionStepValue( KratosMultiphysics.VELOCITY_Z) self.assertAlmostEqual(reference_v_x, v_x, delta=self.check_tolerance) self.assertAlmostEqual(reference_v_y, v_y, delta=self.check_tolerance) self.assertAlmostEqual(reference_v_z, v_z, delta=self.check_tolerance) self.assertAlmostEqual(reference_pres, pres, delta=self.check_tolerance) line = reference_file.readline() if line != '': # If we did not reach the end of the reference file self.fail( "The number of nodes in the mdpa is smaller than the number of nodes in the output file" )
class TestSerializer(KratosUnittest.TestCase): def _prepare_test(self): # Define a model and load the parameters self.pre_serialized_model = KratosMultiphysics.Model() with open( GetFilePath( "auxiliar_files_for_python_unittest/parameters_files/test_serializer.json" ), 'r') as parameter_file: parameters = KratosMultiphysics.Parameters(parameter_file.read()) file_name = parameters["solver_settings"]["model_import_settings"][ "input_filename"].GetString() parameters["solver_settings"]["model_import_settings"][ "input_filename"].SetString(GetFilePath(file_name)) # First the model is initialized self.pre_serialized_simulation = FluidDynamicsAnalysis( self.pre_serialized_model, parameters) self.pre_serialized_simulation.Initialize() # Before serializing the model, main model part is set to RESTARTED self.main_model_part_name = parameters["solver_settings"][ "model_part_name"].GetString() self.pre_serialized_model.GetModelPart( self.main_model_part_name).ProcessInfo.SetValue( KratosMultiphysics.IS_RESTARTED, True) serialized_model = KratosMultiphysics.StreamSerializer() serialized_model.Save("ModelSerialization", self.pre_serialized_model) with open( GetFilePath( "auxiliar_files_for_python_unittest/parameters_files/test_serializer.json" ), 'r') as parameter_file: self.project_parameters = KratosMultiphysics.Parameters( parameter_file.read()) # Parameters are read again and input type set to use_input_model_part since the serialized model already has the mdpa loaded self.project_parameters["solver_settings"]["model_import_settings"][ "input_type"].SetString("use_input_model_part") # Deserialize and store the new model self.current_model = KratosMultiphysics.Model() serialized_model.Load("ModelSerialization", self.current_model) def _check_results(self): pre_serialized_model_part = self.pre_serialized_model.GetModelPart( self.main_model_part_name) pre_serialized_pressure_results = [ node.GetSolutionStepValue(KratosMultiphysics.PRESSURE) for node in pre_serialized_model_part.Nodes ] pre_serialized_velocity_results = [ node.GetSolutionStepValue(KratosMultiphysics.VELOCITY) for node in pre_serialized_model_part.Nodes ] serialized_model_part = self.current_model.GetModelPart( self.main_model_part_name) serialized_pressure_results = [ node.GetSolutionStepValue(KratosMultiphysics.PRESSURE) for node in serialized_model_part.Nodes ] serialized_velocity_results = [ node.GetSolutionStepValue(KratosMultiphysics.VELOCITY) for node in serialized_model_part.Nodes ] # Comparing results before and after serializing for pre_serialized_result, serialized_result in zip( pre_serialized_pressure_results, serialized_pressure_results): self.assertAlmostEqual(pre_serialized_result, serialized_result) for pre_serialized_result, serialized_result in zip( pre_serialized_velocity_results, serialized_velocity_results): for value_pre_seralized, value_serialized in zip( pre_serialized_result, serialized_result): self.assertAlmostEqual(value_pre_seralized, value_serialized) @KratosUnittest.skipUnless(dependencies_are_available, "FluidDynamicsApplication is not available") def test_serializer_fluid_analysis(self): self._prepare_test() # Solving simulation before serializing to later check the results self.pre_serialized_simulation.RunSolutionLoop() self.pre_serialized_simulation.Finalize() # Solving simulation after serializing self.serialized_simulation = FluidDynamicsAnalysis( self.current_model, self.project_parameters) self.serialized_simulation.Run() self._check_results()