def test_DefaultProcess(self): model = KratosMultiphysics.Model() model_part = model.CreateModelPart('test') parent_settings = KratosMultiphysics.Parameters(''' { "list_of_controllers" : [ { "model_part_name" : "test" } ] } ''') process = core.Factory( parent_settings['list_of_controllers'], model) patcher1 = patch( 'KratosMultiphysics.HDF5Application.core.file_io.KratosHDF5.HDF5FileSerial', autospec=True) patcher2 = patch( 'KratosMultiphysics.HDF5Application.core.operations.KratosHDF5.HDF5ModelPartIO', autospec=True) patcher1.start() MockHDF5ModelPartIO = patcher2.start() process.ExecuteInitialize() model_part_io = MockHDF5ModelPartIO.return_value model_part_io.WriteModelPart.assert_called_once_with(model_part) patcher1.stop() patcher2.stop()
def test_DefaultSettings(self): model = KratosMultiphysics.Model() model.CreateModelPart('test') parent_settings = KratosMultiphysics.Parameters(''' { "list_of_controllers" : [ { "model_part_name" : "test" } ] } ''') parent_settings = ParametersWrapper(parent_settings) core.Factory(parent_settings['list_of_controllers'], model) settings = parent_settings['list_of_controllers'][0] self.assertTrue(settings.Has('model_part_name')) self.assertTrue(settings.Has('process_step')) self.assertTrue(settings.Has('controller_settings')) self.assertTrue(settings.Has('io_settings')) self.assertTrue(settings.Has('list_of_operations')) self.assertTrue(settings['list_of_operations'].IsArray()) self.assertEqual(settings['list_of_operations'].size(), 1) self.assertTrue( settings['list_of_operations'][0].Has('operation_type')) self.assertTrue(settings['list_of_operations'][0].Has('prefix'))
def test_EmptyArraySettings(self): model = KratosMultiphysics.Model() settings = KratosMultiphysics.Parameters(''' { "list_of_controllers" : [] } ''') with self.assertRaisesRegex(RuntimeError, '"PLEASE_SPECIFY_MODEL_PART_NAME" was not found'): core.Factory(settings['list_of_controllers'], model)
def Factory(process_settings, Model): """Return a process to read a transient solution from HDF5.""" default_settings = KratosMultiphysics.Parameters(""" { "model_part_name" : "MainModelPart", "file_settings" : {}, "nodal_solution_step_data_settings" : {}, "nodal_data_value_settings": {}, "element_data_value_settings" : {} } """) new_settings = KratosMultiphysics.Parameters(''' { "list_of_controllers": [{ "model_part_name" : "", "process_step": "initialize_solution_step", "io_settings": { "io_type": "serial_hdf5_file_io", "file_name": "<identifier>-<time>.h5", "file_access_mode": "read_only" }, "list_of_operations": [{ "operation_type": "nodal_solution_step_data_input" },{ "operation_type": "nodal_data_value_input" },{ "operation_type": "element_data_value_input" }] }] } ''') settings = process_settings["Parameters"] if settings.Has('file_name'): _utils.CheckForDeprecatedFilename( settings, __name__, new_settings["list_of_controllers"][0]["io_settings"]) settings.RemoveValue('file_name') if settings.Has('time_tag_precision'): depr_msg = '\nDEPRECATION-WARNING: "time_tag_precision" is ignored, please use "time_format" in "file_settings"!\n' KratosMultiphysics.Logger.PrintWarning(__name__, depr_msg) settings.RemoveValue('time_tag_precision') settings.ValidateAndAssignDefaults(default_settings) model_part_name = settings["model_part_name"].GetString() results_settings = new_settings["list_of_controllers"][0] results_settings["model_part_name"].SetString(model_part_name) _utils.InsertSettings(settings["file_settings"], results_settings["io_settings"]) if _utils.IsDistributed(): results_settings["io_settings"]["io_type"].SetString( "parallel_hdf5_file_io") _utils.InsertArrayOfSettings([ settings["nodal_solution_step_data_settings"], settings["nodal_data_value_settings"], settings["element_data_value_settings"] ], results_settings["list_of_operations"]) return _core.Factory(new_settings["list_of_controllers"], Model)
def Factory(process_settings, Model): """Return a process for initializing a model part from an existing HDF5 output file.""" default_settings = KratosMultiphysics.Parameters(""" { "model_part_name" : "MainModelPart", "file_settings" : {}, "nodal_solution_step_data_settings" : {}, "nodal_data_value_settings": {}, "element_data_value_settings" : {} } """) settings = process_settings["Parameters"] settings.ValidateAndAssignDefaults(default_settings) new_settings = KratosMultiphysics.Parameters(''' { "list_of_controllers": [{ "model_part_name" : "", "process_step": "initialize", "io_settings": { "io_type": "serial_hdf5_file_io", "file_name": "<identifier>.h5", "file_access_mode": "read_only" }, "list_of_operations": [{ "operation_type": "nodal_solution_step_data_input" },{ "operation_type": "nodal_data_value_input" },{ "operation_type": "element_data_value_input" }] }] } ''') model_part_name = settings["model_part_name"].GetString() new_settings["list_of_controllers"][0]["model_part_name"].SetString( model_part_name) results_settings = new_settings["list_of_controllers"][0] _utils.InsertSettings(settings["file_settings"], results_settings["io_settings"]) if _utils.IsDistributed(): results_settings["io_settings"]["io_type"].SetString( "parallel_hdf5_file_io") _utils.InsertArrayOfSettings([ settings["nodal_solution_step_data_settings"], settings["nodal_data_value_settings"], settings["element_data_value_settings"] ], results_settings["list_of_operations"]) return _core.Factory(new_settings["list_of_controllers"], Model)
def SingleMeshTemporalOutputProcessFactory(core_settings, Model): return core.Factory(core_settings, Model)
def SingleMeshXdmfOutputProcessFactory(core_settings, Model): return core.Factory(core_settings, Model)
def test_NonArraySettings(self): model = KratosMultiphysics.Model() settings = KratosMultiphysics.Parameters() with self.assertRaisesRegex(ValueError, r'Expected settings as an array'): core.Factory(settings, model)
def InitializationFromHDF5ProcessFactory(core_settings, Model): return core.Factory(core_settings, Model)
def Factory(process_settings, Model): """Return a process for writing simulation results for a single mesh to HDF5. It also creates the xdmf-file on the fly """ if not isinstance(process_settings, KratosMultiphysics.Parameters): raise Exception( "expected input shall be a Parameters object, encapsulating a json string" ) default_settings = KratosMultiphysics.Parameters(""" { "model_part_name" : "MainModelPart", "file_settings" : {}, "model_part_output_settings" : {}, "nodal_solution_step_data_settings" : {}, "nodal_data_value_settings": {}, "element_data_value_settings" : {}, "output_time_settings" : {} } """) settings = process_settings["Parameters"] settings.ValidateAndAssignDefaults(default_settings) new_settings = KratosMultiphysics.Parameters(''' { "list_of_controllers": [{ "model_part_name" : "", "process_step": "initialize", "io_settings": { "io_type": "mock_hdf5_file_io", "file_name": "<identifier>.h5" }, "list_of_operations": [{ "module_name": "operations.system", "operation_type": "delete_old_h5_files" }] },{ "model_part_name" : "", "process_step": "before_solution_loop", "io_settings": { "io_type": "serial_hdf5_file_io", "file_name": "<identifier>.h5", "file_access_mode": "truncate" }, "list_of_operations": [{ "operation_type": "model_part_output" },{ "operation_type": "nodal_solution_step_data_output" },{ "operation_type": "nodal_data_value_output" },{ "operation_type": "element_data_value_output" }] },{ "model_part_name" : "", "process_step": "finalize_solution_step", "controller_settings": { "controller_type": "temporal_controller" }, "io_settings": { "io_type": "serial_hdf5_file_io", "file_name": "<identifier>-<time>.h5", "file_access_mode": "truncate" }, "list_of_operations": [{ "operation_type": "nodal_solution_step_data_output" },{ "operation_type": "nodal_data_value_output" },{ "operation_type": "element_data_value_output" }] },{ "model_part_name" : "", "process_step": "finalize_solution_step", "controller_settings": { "controller_type": "temporal_controller" }, "io_settings": { "io_type": "mock_hdf5_file_io", "file_name": "<identifier>.h5" }, "list_of_operations": [{ "module_name": "operations.xdmf", "operation_type": "xdmf_output" }] }] } ''') model_part_name = settings["model_part_name"].GetString() for current_settings in new_settings["list_of_controllers"]: current_settings["model_part_name"].SetString(model_part_name) model_part_settings = new_settings["list_of_controllers"][1] results_settings = new_settings["list_of_controllers"][2] for io_settings in [ model_part_settings["io_settings"], results_settings["io_settings"] ]: _utils.InsertSettings(settings["file_settings"], io_settings) if _utils.IsDistributed(): io_settings["io_type"].SetString("parallel_hdf5_file_io") _utils.InsertArrayOfSettings([ settings["model_part_output_settings"], settings["nodal_solution_step_data_settings"], settings["nodal_data_value_settings"], settings["element_data_value_settings"] ], model_part_settings["list_of_operations"]) _utils.InsertArrayOfSettings([ settings["nodal_solution_step_data_settings"], settings["nodal_data_value_settings"], settings["element_data_value_settings"] ], results_settings["list_of_operations"]) output_time_settings = settings["output_time_settings"] _utils.InsertSettings(output_time_settings, results_settings["controller_settings"]) _utils.CheckForDeprecatedFilename(output_time_settings, __name__, model_part_settings["io_settings"], results_settings["io_settings"]) _utils.CheckForDeprecatedTemporalSettings( output_time_settings, __name__, results_settings["controller_settings"]) return _core.Factory(new_settings["list_of_controllers"], Model)
def Factory(process_settings, Model): """Return a process for writing simulation results for multiple meshes to HDF5.""" default_settings = KratosMultiphysics.Parameters(""" { "model_part_name" : "MainModelPart", "file_settings" : {}, "model_part_output_settings" : {}, "nodal_solution_step_data_settings" : {}, "nodal_data_value_settings": {}, "element_data_value_settings" : {}, "output_time_settings" : {} } """) settings = process_settings["Parameters"] settings.ValidateAndAssignDefaults(default_settings) new_settings = KratosMultiphysics.Parameters(''' { "list_of_controllers": [{ "model_part_name" : "", "process_step": "before_solution_loop", "io_settings": { "io_type": "serial_hdf5_file_io", "file_name": "<identifier>-<time>.h5" }, "list_of_operations": [{ "operation_type": "model_part_output" },{ "operation_type": "nodal_solution_step_data_output" },{ "operation_type": "nodal_data_value_output" },{ "operation_type": "element_data_value_output" }] },{ "model_part_name" : "", "process_step": "finalize_solution_step", "controller_settings": { "controller_type": "temporal_controller" }, "io_settings": { "io_type": "serial_hdf5_file_io", "file_name": "<identifier>-<time>.h5" }, "list_of_operations": [{ "operation_type": "model_part_output" },{ "operation_type": "nodal_solution_step_data_output" },{ "operation_type": "nodal_data_value_output" },{ "operation_type": "element_data_value_output" }] }] } ''') model_part_name = settings["model_part_name"].GetString() for current_settings in new_settings["list_of_controllers"]: current_settings["model_part_name"].SetString(model_part_name) model_part_settings = new_settings["list_of_controllers"][0] results_settings = new_settings["list_of_controllers"][1] for io_settings in [ model_part_settings["io_settings"], results_settings["io_settings"] ]: _utils.InsertSettings(settings["file_settings"], io_settings) if _utils.IsDistributed(): io_settings["io_type"].SetString("parallel_hdf5_file_io") list_of_operations_settings = [ settings["model_part_output_settings"], settings["nodal_solution_step_data_settings"], settings["nodal_data_value_settings"], settings["element_data_value_settings"] ] _utils.InsertArrayOfSettings(list_of_operations_settings, model_part_settings["list_of_operations"]) _utils.InsertArrayOfSettings(list_of_operations_settings, results_settings["list_of_operations"]) output_time_settings = settings["output_time_settings"] _utils.InsertSettings(output_time_settings, results_settings["controller_settings"]) _utils.CheckForDeprecatedFilename(output_time_settings, __name__, model_part_settings["io_settings"], results_settings["io_settings"]) _utils.CheckForDeprecatedTemporalSettings( output_time_settings, __name__, results_settings["controller_settings"]) return _core.Factory(new_settings["list_of_controllers"], Model)
def Factory(settings, Model): """Return a user-defined input/output process for HDF5. The input settings are a json array of parameters which maps to the structure of the HDF5 IO python core. The settings of each array item are given in the following table: +-----------------------+------------+-------------------------------------------+ | Setting | Type | Default Value | +-----------------------+------------+-------------------------------------------+ | "model_part_name" | String | "PLEASE_SPECIFY_MODEL_PART_NAME" | +-----------------------+------------+-------------------------------------------+ | "process_step" | String | "initialize" | +-----------------------+------------+-------------------------------------------+ | "controller_settings" | Parameters | { | | | | "controller_type": "default_controller" | | | | } | +-----------------------+------------+-------------------------------------------+ | "io_settings" | Parameters | "echo_level": 0 | | | | "file_access_mode": "exclusive" | | | | "file_driver": "sec2" | | | | "file_name": "kratos" | | | | "max_files_to_keep": "unlimited" | | | | "io_type": "serial_hdf5_file_io" | +-----------------------+------------+-------------------------------------------+ | "list_of_operations" | Parameters | [{ | | | Array | "operation_type": "model_part_output" | | | | "prefix": "/ModelData" | | | | }] | +-----------------------+------------+-------------------------------------------+ For example: ''' [{ "model_part_name" : "MainModelPart", "process_step": "finalize_solution_step", "controller_settings": { "controller_type": "temporal_controller", "time_frequency": 0.5 }, "io_settings": { "file_name": "results/<model_part_name>-<time>.h5" }, "list_of_operations": [{ "operation_type": "model_part_output" },{ "operation_type": "nodal_solution_step_data_output", "list_of_variables": ["DISPLACEMENT"] }] }] ''' will store the model part and displacement every 0.5s in the following directory tree structure: ./results/MainModelPart-0.000.h5 ./results/MainModelPart-0.500.h5 ./results/MainModelPart-1.000.h5 ... and internal file tree structure in each .h5 file: /ModelData/Conditions /ModelData/Elements ... /ResultsData/NodalSolutionStepData/DISPLACEMENT In the above example, the nonterminal symbols <model_part_name> and <time> are automatically replaced by the name of the model part and the current time. Alternatively, the simulations results can be stored in a single .h5 file containing the directory tree structure of the above example: ''' [{ "model_part_name" : "MainModelPart", "process_step": "finalize_solution_step", "controller_settings": { "controller_type": "temporal_controller", "time_frequency": 0.5 }, "io_settings": { "file_name": "results.h5", "file_access_mode": "read_write" }, "list_of_operations": [{ "prefix": "/<time>/<model_part_name>/ModelData", "operation_type": "model_part_output" },{ "prefix": "/<time>/<model_part_name>/ResultsData", "operation_type": "nodal_solution_step_data_output", "list_of_variables": ["DISPLACEMENT"] }] }] ''' will store the model part and displacement every 0.5s in the following directory tree structure: ./results.h5 and internal file tree structure in each .h5 file: /0.000/MainModelPart/ModelData/... /0.000/MainModelPart/ResultsData/... ... /0.500/MainModelPart/ModelData/... /0.500/MainModelPart/ResultsData/... ... /1.000/MainModelPart/ModelData/... /1.000/MainModelPart/ResultsData/... Different groupings of model parts, files, locations within the solution algorithm, frequencies and IO operations can be configured by appending additional parameters to the json array. """ return core.Factory(ParametersWrapper(settings["Parameters"]), Model)
def Factory(process_settings, Model): """Return a process for writing Bossak primal results for a single mesh to HDF5.""" default_settings = KratosMultiphysics.Parameters(""" { "model_part_name" : "MainModelPart", "file_settings" : {}, "model_part_output_settings" : {}, "nodal_solution_step_data_settings" : {}, "nodal_data_value_settings": {}, "element_data_value_settings" : {}, "output_time_settings" : {} } """) settings = process_settings["Parameters"] if settings.Has("alpha_bossak"): depr_msg = '\nDEPRECATION-WARNING: "alpha_bossak" should be specified in "nodal_solution_step_data_settings". This will be removed in the future!\n' KratosMultiphysics.Logger.PrintWarning(__name__, depr_msg) _utils.InsertSingleSetting( settings["nodal_solution_step_data_settings"], "alpha_bossak", settings["alpha_bossak"]) settings.RemoveValue("alpha_bossak") settings.ValidateAndAssignDefaults(default_settings) new_settings = KratosMultiphysics.Parameters(''' { "list_of_controllers": [{ "model_part_name" : "", "process_step": "before_solution_loop", "io_settings": { "io_type": "serial_hdf5_file_io", "file_name": "<identifier>.h5" }, "list_of_operations": [{ "operation_type": "model_part_output" },{ "operation_type": "primal_bossak_output" },{ "operation_type": "nodal_data_value_output" },{ "operation_type": "element_data_value_output" }] },{ "model_part_name" : "", "process_step": "finalize_solution_step", "controller_settings": { "controller_type": "temporal_controller" }, "io_settings": { "io_type": "serial_hdf5_file_io", "file_name": "<identifier>-<time>.h5" }, "list_of_operations": [{ "operation_type": "primal_bossak_output" },{ "operation_type": "nodal_data_value_output" },{ "operation_type": "element_data_value_output" }] }] } ''') model_part_name = settings["model_part_name"].GetString() for current_settings in new_settings["list_of_controllers"]: current_settings["model_part_name"].SetString(model_part_name) model_part_settings = new_settings["list_of_controllers"][0] results_settings = new_settings["list_of_controllers"][1] for io_settings in [model_part_settings["io_settings"], results_settings["io_settings"]]: _utils.InsertSettings(settings["file_settings"], io_settings) if _utils.IsDistributed(): io_settings["io_type"].SetString("parallel_hdf5_file_io") _utils.InsertArrayOfSettings( [settings["model_part_output_settings"], settings["nodal_solution_step_data_settings"], settings["nodal_data_value_settings"], settings["element_data_value_settings"]], model_part_settings["list_of_operations"]) _utils.InsertArrayOfSettings([settings["nodal_solution_step_data_settings"], settings["nodal_data_value_settings"], settings["element_data_value_settings"]], results_settings["list_of_operations"]) output_time_settings = settings["output_time_settings"] _utils.InsertSettings(output_time_settings, results_settings["controller_settings"]) _utils.CheckForDeprecatedFilename( output_time_settings, __name__, model_part_settings["io_settings"], results_settings["io_settings"]) _utils.CheckForDeprecatedTemporalSettings( output_time_settings, __name__, results_settings["controller_settings"]) return _core.Factory(new_settings["list_of_controllers"], Model)
def Factory(process_settings, Model): """Construct a user-defined input/output process for HDF5.""" return _core.Factory(process_settings["Parameters"], Model)