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 _runTest(self,settings_file_name): model = KM.Model() with open(settings_file_name,'r') as settings_file: settings = KM.Parameters(settings_file.read()) # to check the results: add output settings block if needed if self.print_vtk_output: settings["output_processes"].AddValue("vtk_output", KM.Parameters(R'''[{ "python_module" : "vtk_output_process", "kratos_module" : "KratosMultiphysics", "process_name" : "VtkOutputProcess", "help" : "This process writes postprocessing files for Paraview", "Parameters" : { "model_part_name" : "FluidModelPart.domain", "output_control_type" : "step", "output_frequency" : 1, "file_format" : "binary", "output_precision" : 7, "output_sub_model_parts" : false, "folder_name" : "vtk_output", "save_output_files_in_folder" : true, "nodal_solution_step_data_variables" : ["VELOCITY","PRESSURE","MESH_DISPLACEMENT","MESH_VELOCITY"], "nodal_data_value_variables" : [], "element_data_value_variables" : [], "condition_data_value_variables" : [] } }]''')) if self.print_gid_output: settings["output_processes"].AddValue("gid_output", KM.Parameters(R'''[{ "python_module" : "gid_output_process", "kratos_module" : "KratosMultiphysics", "process_name" : "GiDOutputProcess", "help" : "This process writes postprocessing files for GiD", "Parameters" : { "model_part_name" : "FluidModelPart.domain", "output_name" : "ale_fluid_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, "nodal_results" : ["VELOCITY","PRESSURE","MESH_DISPLACEMENT","MESH_VELOCITY"] } } } }]''')) analysis = FluidDynamicsAnalysis(model,settings) analysis.Run()
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 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()
def testCylinder(self): with ControlledExecutionScope( os.path.dirname(os.path.realpath(__file__))): # solve fluid model = Kratos.Model() with open( 'AdjointVMSSensitivity2DTest/mpi_cylinder_test_parameters.json', 'r') as parameter_file: project_parameters = Kratos.Parameters(parameter_file.read()) parameter_file.close() primal_simulation = FluidDynamicsAnalysis(model, project_parameters) primal_simulation.Run() Kratos.DataCommunicator.GetDefault().Barrier() # solve adjoint with open( 'AdjointVMSSensitivity2DTest/mpi_cylinder_test_adjoint_parameters.json', 'r') as parameter_file: project_parameters = Kratos.Parameters(parameter_file.read()) parameter_file.close() adjoint_model = Kratos.Model() adjoint_simulation = AdjointFluidAnalysis(adjoint_model, project_parameters) adjoint_simulation.Run() Kratos.DataCommunicator.GetDefault().Barrier() rank = adjoint_simulation._solver.main_model_part.GetCommunicator( ).MyPID() # remove files if rank == 0: self._remove_file( "./AdjointVMSSensitivity2DTest/mpi_cylinder_test_probe1.dat" ) self._remove_file( "./AdjointVMSSensitivity2DTest/mpi_cylinder_test_probe2.dat" ) self._remove_file( "./AdjointVMSSensitivity2DTest/mpi_cylinder_test_adjoint_probe1.dat" ) self._remove_file( "./AdjointVMSSensitivity2DTest/mpi_cylinder_test_adjoint_probe2.dat" ) self._remove_file( "./AdjointVMSSensitivity2DTest/mpi_cylinder_test_adjoint_probe3.dat" ) self._remove_file( "./AdjointVMSSensitivity2DTest/cylinder_test.time") self._remove_h5_files("primal") self._remove_file("./AdjointVMSSensitivity2DTest/cylinder_test_" + str(rank) + ".time") self._remove_file("./AdjointVMSSensitivity2DTest/cylinder_test_" + str(rank) + ".mdpa")
def testInputOutput(self): with ControlledExecutionScope(self.work_folder): # run simulation and write to hdf5 file model_out = Model() with open("output_test_parameters.json", 'r') as parameter_file: project_parameters = Parameters(parameter_file.read()) test = FluidDynamicsAnalysis(model_out, project_parameters) test.Run() # start new simulation and read from hdf5 file model_in = Model() with open("input_test_parameters.json", 'r') as parameter_file: project_parameters = Parameters(parameter_file.read()) test = FluidAnalysisWithoutSolution(model_in, project_parameters) test.Run()
def runTest(self): # If required, add the output process to the test settings if self.print_output: self._AddOutput() # If required, add the reference values output process to the test settings if self.print_reference_values: self._AddReferenceValuesOutput() else: self._AddReferenceValuesCheck() # Create the test simulation with KratosUnittest.WorkFolderScope(self.work_folder, __file__): self.model = KratosMultiphysics.Model() simulation = FluidDynamicsAnalysis(self.model, self.parameters) simulation.Run()
def _runTest(self, settings_file_name): model = km.Model() with open(settings_file_name, 'r') as settings_file: settings = km.Parameters(settings_file.read()) # to check the results: add output settings block if needed if self.print_output: settings.AddValue( "output_processes", km.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" : "fluid_computational_model_part", "output_name" : "interface_test", "postprocess_parameters" : { "result_file_configuration" : { "gidpost_flags" : { "GiDPostMode" : "GiD_PostBinary", "WriteDeformedMeshFlag" : "WriteUndeformed", "WriteConditionsFlag" : "WriteElementsOnly", "MultiFileFlag" : "SingleFile" }, "file_label" : "time", "output_control_type" : "step", "output_frequency" : 1, "body_output" : true, "node_output" : false, "skin_output" : false, "plane_output" : [], "nodal_results" : ["VELOCITY","PRESSURE"], "gauss_point_results" : [] }, "point_data_configuration" : [] } } }] }''')) analysis = FluidDynamicsAnalysis(model, settings) analysis.Run()
def _prepare_fluid_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) self.serialized_model = KratosMultiphysics.StreamSerializer() self.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() self.serialized_model.Load("ModelSerialization", self.current_model)
def SolvePrimalProblem(kratos_parameters): test = FluidDynamicsAnalysis(Kratos.Model(), kratos_parameters) test.Run() return test
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" : [] } } }] }''')
def _run_test(self, primal_parameter_file_name, adjoint_parameter_file_name): model = km.Model() settings = km.Parameters(r'''{}''') with open(primal_parameter_file_name, 'r') as primal_parameter_file: settings.AddValue("primal_settings", km.Parameters(primal_parameter_file.read())) with open(adjoint_parameter_file_name, 'r') as adjoint_parameter_file: settings.AddValue("adjoint_settings", km.Parameters(adjoint_parameter_file.read())) # Add hdf5 output to the primal problem settings["primal_settings"]["processes"][ "auxiliar_process_list"].Append( km.Parameters(r'''{ "kratos_module" : "KratosMultiphysics.HDF5Application", "python_module" : "single_mesh_primal_output_process", "Parameters" : { "model_part_name" : "MainModelPart", "file_settings" : { "file_name" : "primal_output-<time>", "file_access_mode" : "truncate" }, "model_part_output_settings" : { "prefix" : "/ModelData" }, "nodal_solution_step_data_settings" : { "list_of_variables": ["VELOCITY", "ACCELERATION", "PRESSURE"] }, "output_time_settings" : { "step_frequency": 1 } } }''')) # to check the results: add output settings block if needed if self.print_output: settings["adjoint_settings"].AddValue( "output_processes", km.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" : "MainModelPart", "output_name" : "interface_test", "postprocess_parameters" : { "result_file_configuration" : { "gidpost_flags" : { "GiDPostMode" : "GiD_PostBinary", "WriteDeformedMeshFlag" : "WriteUndeformed", "WriteConditionsFlag" : "WriteElementsOnly", "MultiFileFlag" : "SingleFile" }, "file_label" : "time", "output_control_type" : "step", "output_interval" : 1, "body_output" : true, "node_output" : false, "skin_output" : false, "plane_output" : [], "nodal_results" : ["VELOCITY","PRESSURE","ADJOINT_FLUID_VECTOR_1","ADJOINT_FLUID_SCALAR_1","SHAPE_SENSITIVITY"], "gauss_point_results" : [] }, "point_data_configuration" : [] } } }] }''')) primal_analysis = FluidDynamicsAnalysis(model, settings["primal_settings"]) primal_analysis.Run() adjoint_model = km.Model() adjoint_analysis = AdjointFluidAnalysis(adjoint_model, settings["adjoint_settings"]) adjoint_analysis.Run() self._remove_h5_files("primal_output")
def _createFluidTest(self, parameter_file_name): test = FluidDynamicsAnalysis(Model(), self._readParameters(parameter_file_name)) return test
err_msg = 'Unexpected amount of input arguments!\n' err_msg += 'To run the primal fluid problem followed by the adjoint solution, provide both parameter files:\n' err_msg += ' "python adjoint_fluid_analysis.py <primal-parameter-file>.json <adjoint-parameter-file>.json"\n' err_msg += 'To run only the adjoint problem, provide only the adjoint parameter file:\n' err_msg += ' "python adjoint_fluid_analysis.py <adjoint-parameter-file>.json"\n' raise Exception(err_msg) if primal_parameter_file_name is not None: with open(primal_parameter_file_name, 'r') as primal_parameter_file: parameters.AddValue( "primal_settings", Kratos.Parameters(primal_parameter_file.read())) else: parameters.AddEmptyValue("primal_settings") with open(adjoint_parameter_file_name, 'r') as adjoint_parameter_file: parameters.AddValue("adjoint_settings", Kratos.Parameters(adjoint_parameter_file.read())) model = Kratos.Model() if primal_parameter_file_name is not None: primal_simulation = FluidDynamicsAnalysis( model, parameters["primal_settings"]) primal_simulation.Run() adjoint_model = Kratos.Model() adjoint_simulation = AdjointFluidAnalysis(adjoint_model, parameters["adjoint_settings"]) adjoint_simulation.Run()
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()
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" )
def _RunSodShockTubeTest(self): # Create the test simulation with KratosUnittest.WorkFolderScope(self.work_folder, __file__): self.model = KratosMultiphysics.Model() simulation = FluidDynamicsAnalysis(self.model, self.parameters) simulation.Run()
from __future__ import print_function, absolute_import, division #makes KratosMultiphysics backward compatible with python 2.6 and 2.7 import KratosMultiphysics as Kratos from KratosMultiphysics.FluidDynamicsApplication.fluid_dynamics_analysis import FluidDynamicsAnalysis if __name__ == '__main__': from sys import argv if len(argv) > 2: err_msg = 'Too many input arguments!\n' err_msg += 'Use this script in the following way:\n' err_msg += '- With default parameter file (assumed to be called "ProjectParameters.json"):\n' err_msg += ' "python fluid_dynamics_analysis.py"\n' err_msg += '- With custom parameter file:\n' err_msg += ' "python fluid_dynamics_analysis.py <my-parameter-file>.json"\n' raise Exception(err_msg) if len(argv) == 2: # ProjectParameters is being passed from outside parameter_file_name = argv[1] else: # using default name parameter_file_name = "ProjectParameters.json" with open(parameter_file_name, 'r') as parameter_file: parameters = Kratos.Parameters(parameter_file.read()) model = Kratos.Model() simulation = FluidDynamicsAnalysis(model, parameters) simulation.Run()
def _CreateAnalysisStage(self): return FluidDynamicsAnalysis(self.model, self.project_parameters)
def _CreateFluidTest(self): with open(self.project_parameters_file_name, 'r') as parameter_file: project_parameters = Kratos.Parameters(parameter_file.read()) parameter_file.close() test = FluidDynamicsAnalysis(Kratos.Model(), project_parameters) return test