def testOneElement(self): with ControlledExecutionScope( os.path.dirname(os.path.realpath(__file__))): # solve fluid self.solve('AdjointVMSSensitivity2DTest/one_element_test') # solve adjoint test = AdjointFluidAnalysis( Model(), self._readParameters( 'AdjointVMSSensitivity2DTest/one_element_test_adjoint')) test.Run() Sensitivity = [[]] Sensitivity[0].append(test._GetSolver().main_model_part.GetNode( 1).GetSolutionStepValue(SHAPE_SENSITIVITY_X)) Sensitivity[0].append(test._GetSolver().main_model_part.GetNode( 1).GetSolutionStepValue(SHAPE_SENSITIVITY_Y)) # calculate sensitivity by finite difference step_size = 0.00000001 FDSensitivity = self._computeFiniteDifferenceDragSensitivity( [1], step_size, './AdjointVMSSensitivity2DTest/one_element_test', [1.0, 0.0, 0.0], './MainModelPart.Structure_drag.dat') self.assertAlmostEqual(Sensitivity[0][0], FDSensitivity[0][0], 4) self.assertAlmostEqual(Sensitivity[0][1], FDSensitivity[0][1], 4) self._removeH5Files("MainModelPart") kratos_utils.DeleteFileIfExisting( "./AdjointVMSSensitivity2DTest/one_element_test.time") kratos_utils.DeleteFileIfExisting("./Structure_drag.dat") kratos_utils.DeleteFileIfExisting("./one_element.post.bin") kratos_utils.DeleteFileIfExisting("./tests.post.lst")
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 _createAdjointTest(self, parameter_file_name): test = AdjointFluidAnalysis(Model(), self._readParameters(parameter_file_name)) return test
def SolveAdjointProblem(kratos_parameters): test = AdjointFluidAnalysis(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")