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,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 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 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 _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()
def SolvePrimalProblem(kratos_parameters): test = FluidDynamicsAnalysis(Kratos.Model(), kratos_parameters) test.Run() return test
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")
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()
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()