def __InnerLoop(self): # Import fields for field_settings in self.communication_settings["import_fields"]: identifier = field_settings["identifier"].GetString() model_part_name = field_settings["model_part_name"].GetString() model_part = self.model[model_part_name] variable_name = field_settings["variable_name"].GetString() variable = KM.KratosGlobals.GetVariable(variable_name) info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) info.SetString("identifier", identifier) CoSimIO.ImportData(info, model_part, variable, CoSimIO.DataLocation.NodeHistorical) self._GetSolver().SolveSolutionStep() # Export fields for field_settings in self.communication_settings["export_fields"]: identifier = field_settings["identifier"].GetString() model_part_name = field_settings["model_part_name"].GetString() model_part = self.model[model_part_name] variable_name = field_settings["variable_name"].GetString() variable = KM.KratosGlobals.GetVariable(variable_name) info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) info.SetString("identifier", identifier) CoSimIO.ExportData(info, model_part, variable, CoSimIO.DataLocation.NodeHistorical)
def __ExportImportDataOnModelPart(self, model_part, var_export, var_import, data_location): p = self.__RunPythonInSubProcess("import_export_data") connection_settings = CoSimIO.Info() connection_settings.SetString("my_name", "ExpImp") connection_settings.SetString("connect_to", "impExp") connection_settings.SetInt("echo_level", 0) info = CoSimIO.Connect(connection_settings) connection_name = info.GetString("connection_name") self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Connected) export_info = CoSimIO.Info() export_info.SetString("connection_name", connection_name) export_info.SetString("identifier", "data_exchange_1") CoSimIO.ExportData(export_info, model_part, var_export, data_location) import_info = CoSimIO.Info() import_info.SetString("connection_name", connection_name) import_info.SetString("identifier", "data_exchange_2") CoSimIO.ImportData(import_info, model_part, var_import, data_location) disconnect_settings = CoSimIO.Info() disconnect_settings.SetString("connection_name", connection_name) info = CoSimIO.Disconnect(disconnect_settings) self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Disconnected) self.__CheckSubProcess(p)
def test_Export_Import_Data_raw_values(self): connection_settings = CoSimIO.Info() connection_settings.SetString("connection_name", "im_exp_data") connection_settings.SetInt("echo_level", 0) info = CoSimIO.Connect(connection_settings) self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Connected) values = [1.0, 2.5, 3.3, -9.4] export_info = CoSimIO.Info() export_info.SetString("connection_name", "im_exp_data") export_info.SetString("identifier", "data_exchange_1") CoSimIO.ExportData(export_info, values) RunPythonInSubProcess("import_export_data") import_info = CoSimIO.Info() import_info.SetString("connection_name", "im_exp_data") import_info.SetString("identifier", "data_exchange_2") imported_values = CoSimIO.ImportData(import_info) disconnect_settings = CoSimIO.Info() disconnect_settings.SetString("connection_name", "im_exp_data") info = CoSimIO.Disconnect(disconnect_settings) self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Disconnected) # checking the values after disconnecting to avoid deadlock self.assertVectorAlmostEqual(KM.Vector(values), KM.Vector(imported_values))
def _ImportData(info): identifier = info.GetString("identifier") model_part = self.data_comm_settings[identifier]["model_part"] variable = self.data_comm_settings[identifier]["variable"] info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) info.SetString("identifier", identifier) CoSimIO.ImportData(info, model_part, variable, CoSimIO.DataLocation.NodeHistorical) return CoSimIO.Info()
def ImportData(self, data_config): data_type = data_config["type"] if data_type == "coupling_interface_data": interface_data = data_config["interface_data"] info = CoSimIO.Info() info.SetString("connection_name", self.solver_name) info.SetString("identifier", interface_data.name) CoSimIO.ImportData(info, interface_data.GetModelPart(), interface_data.variable, GetDataLocation(interface_data.location)) elif data_type == "time": time_list = CoSimIO.ImportData(self.solver_name, "time_to_co_sim") if len(time_list) != 1: raise Exception("Wrong size received!") data_config["time"] = time_list[0] else: raise NotImplementedError( 'Exporting interface data of type "{}" is not implemented for this IO: "{}"' .format(data_type, self._ClassName()))
def ImportData(self, data_config): data_type = data_config["type"] if data_type == "coupling_interface_data": interface_data = data_config["interface_data"] info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) info.SetString("identifier", interface_data.name) CoSimIO.ImportData(info, interface_data.GetModelPart(), interface_data.variable, GetDataLocation(interface_data.location)) else: raise NotImplementedError( 'Exporting interface data of type "{}" is not implemented for this IO: "{}"' .format(data_type, self._ClassName()))
def test_Export_Import_Data_raw_values(self): p = self.__RunPythonInSubProcess("import_export_data") connection_settings = CoSimIO.Info() connection_settings.SetString("my_name", "ExpImp") connection_settings.SetString("connect_to", "impExp") connection_settings.SetInt("echo_level", 0) info = CoSimIO.Connect(connection_settings) connection_name = info.GetString("connection_name") self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Connected) values = CoSimIO.DoubleVector([1.0, 2.5, 3.3, -9.4]) export_info = CoSimIO.Info() export_info.SetString("connection_name", connection_name) export_info.SetString("identifier", "data_exchange_1") CoSimIO.ExportData(export_info, values) import_info = CoSimIO.Info() import_info.SetString("connection_name", connection_name) import_info.SetString("identifier", "data_exchange_2") imported_values = CoSimIO.DoubleVector() CoSimIO.ImportData(import_info, imported_values) disconnect_settings = CoSimIO.Info() disconnect_settings.SetString("connection_name", connection_name) info = CoSimIO.Disconnect(disconnect_settings) self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Disconnected) # checking the values after disconnecting to avoid deadlock self.assertVectorAlmostEqual(values, imported_values) self.__CheckSubProcess(p)
from KratosMultiphysics.CoSimulationApplication import CoSimIO connection_settings = CoSimIO.Info() connection_settings.SetString("my_name", "impExp") connection_settings.SetString("connect_to", "ExpImp") connection_settings.SetInt("echo_level", 0) info = CoSimIO.Connect(connection_settings) connection_name = info.GetString("connection_name") if info.GetInt("connection_status") != CoSimIO.ConnectionStatus.Connected: raise Exception("Connecting failed") import_info = CoSimIO.Info() import_info.SetString("connection_name", connection_name) import_info.SetString("identifier", "data_exchange_1") imported_values = CoSimIO.DoubleVector() CoSimIO.ImportData(import_info, imported_values) # print(imported_values) export_info = CoSimIO.Info() export_info.SetString("connection_name", connection_name) export_info.SetString("identifier", "data_exchange_2") CoSimIO.ExportData(export_info, imported_values) disconnect_settings = CoSimIO.Info() disconnect_settings.SetString("connection_name", connection_name) info = CoSimIO.Disconnect(disconnect_settings) if info.GetInt("connection_status") != CoSimIO.ConnectionStatus.Disconnected: raise Exception("Disconnecting failed")
# input for the mapper is a Kratos::Parameters object mapper_settings = KM.Parameters("""{ "mapper_type": "nearest_neighbor", "echo_level" : 0 }""") # creating the mapper using the mapper factory mapper = KratosMapping.MapperFactory.CreateMapper(model_part_origin, model_part_destination, mapper_settings) # import data to be mapped import_data_info = CoSimIO.Info() import_data_info.SetString("connection_name", "mesh_mapping") import_data_info.SetString("identifier", "data_to_map") CoSimIO.ImportData(import_data_info, model_part_origin, KM.TEMPERATURE, CoSimIO.DataLocation.NodeHistorical) # map scalar quantities mapper.Map(KM.TEMPERATURE, KM.AMBIENT_TEMPERATURE) # map vector quantities mapper.Map(KM.VELOCITY, KM.MESH_VELOCITY) # export mapped data export_data_info = CoSimIO.Info() export_data_info.SetString("connection_name", "mesh_mapping") export_data_info.SetString("identifier", "mapped_data") CoSimIO.ExportData(export_data_info, model_part_destination, KM.AMBIENT_TEMPERATURE, CoSimIO.DataLocation.NodeHistorical) # disconnect from CoSimIO