def Initialize(self): super().Initialize() self.co_sim_settings = self.project_parameters["co_sim_settings"] self.is_strong_coupling = self.co_sim_settings[ "is_strong_coupling"].GetBool() connection_settings = CoSimIO.InfoFromParameters( self.project_parameters["co_sim_settings"]["io_settings"]) info = CoSimIO.Connect(connection_settings) self.connection_name = info.GetString("connection_name") if info.GetInt( "connection_status") != CoSimIO.ConnectionStatus.Connected: raise Exception("Connecting failed!") self.communication_settings = self.co_sim_settings[ "communication_settings"] # Exporting meshes to CoSimulation for model_part_name in self.communication_settings[ "export_meshes"].GetStringArray(): info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) info.SetString("identifier", model_part_name.replace(".", "-")) CoSimIO.ExportMesh(info, self.model[model_part_name])
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 Finalize(self): disconnect_settings = CoSimIO.Info() disconnect_settings.SetString("connection_name", self.connection_name) info = CoSimIO.Disconnect(disconnect_settings) if info.GetInt( "connection_status") != CoSimIO.ConnectionStatus.Disconnected: raise Exception("Disconnecting failed!")
def Finalize(self): super(StructuralMechanicsAnalysisWithCoSimIO, self).Finalize() disconnect_settings = CoSimIO.Info() disconnect_settings.SetString("connection_name", self.connection_name) info = CoSimIO.Disconnect(disconnect_settings) if info.GetInt( "connection_status") != CoSimIO.ConnectionStatus.Disconnected: raise Exception("Disconnecting failed!")
def ExportCouplingInterface(self, interface_config): model_part_name = interface_config["model_part_name"] info = CoSimIO.Info() info.SetString("connection_name", self.solver_name) info.SetString("identifier", model_part_name) CoSimIO.ExportMesh(info, self.model[model_part_name] ) # TODO this can also be geometry at some point
def ExportCouplingInterface(self, interface_config): model_part_name = interface_config["model_part_name"] info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) info.SetString("identifier", model_part_name.replace( ".", "-")) # TODO chec if better solution can be found CoSimIO.ExportMesh(info, self.model[model_part_name] ) # TODO this can also be geometry at some point
def _ExportMesh(info): model_part_name = info.GetString("identifier") export_info = CoSimIO.Info() export_info.SetString("connection_name", self.connection_name) export_info.SetString("identifier", model_part_name.replace(".", "-")) CoSimIO.ExportMesh(export_info, self.model[model_part_name]) return CoSimIO.Info()
def __init__(self, settings, model, solver_name): super(KratosCoSimIO, self).__init__(settings, model, solver_name) connection_settings = CoSimIO.InfoFromParameters(self.settings) connection_settings.SetString("connection_name", solver_name) info = CoSimIO.Connect(connection_settings) if info.GetInt( "connection_status") != CoSimIO.ConnectionStatus.Connected: raise Exception("Connecting failed!")
def _ExportData(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.ExportData(info, model_part, variable, CoSimIO.DataLocation.NodeHistorical) return CoSimIO.Info()
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 __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_Mesh(self): model = KM.Model() model_part = model.CreateModelPart("for_test") model_part_returned = model.CreateModelPart("for_test_2") for i in range(15): model_part.CreateNewNode(i + 1, i * 1.1, 1.1**i, 0.0) props = model_part.CreateNewProperties(0) for i in range(9): # this leaves some hanging nodes model_part.CreateNewElement("Element2D2N", i + 1, [i + 1, i + 2], props) connection_settings = CoSimIO.Info() connection_settings.SetString("connection_name", "im_exp_mesh") connection_settings.SetInt("echo_level", 0) info = CoSimIO.Connect(connection_settings) self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Connected) export_info = CoSimIO.Info() export_info.SetString("connection_name", "im_exp_mesh") export_info.SetString("identifier", "mesh_exchange_1") CoSimIO.ExportMesh(export_info, model_part) RunPythonInSubProcess("import_export_mesh") import_info = CoSimIO.Info() import_info.SetString("connection_name", "im_exp_mesh") import_info.SetString("identifier", "mesh_exchange_2") CoSimIO.ImportMesh(import_info, model_part_returned) disconnect_settings = CoSimIO.Info() disconnect_settings.SetString("connection_name", "im_exp_mesh") info = CoSimIO.Disconnect(disconnect_settings) self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Disconnected) # checking the values after disconnecting to avoid deadlock self.assertEqual(model_part.NumberOfNodes(), model_part_returned.NumberOfNodes()) self.assertEqual(model_part.NumberOfElements(), model_part_returned.NumberOfElements()) for node_orig, node_exchanged in zip(model_part.Nodes, model_part_returned.Nodes): self.assertAlmostEqual(node_orig.X0, node_exchanged.X0) self.assertAlmostEqual(node_orig.Y0, node_exchanged.Y0) self.assertAlmostEqual(node_orig.Z0, node_exchanged.Z0) for elem_orig, elem_exchanged in zip(model_part.Elements, model_part_returned.Elements): self.assertEqual(len(elem_orig.GetNodes()), len(elem_exchanged.GetNodes())) for node_orig, node_exchanged in zip(elem_orig.GetNodes(), elem_exchanged.GetNodes()): self.assertAlmostEqual(node_orig.X0, node_exchanged.X0) self.assertAlmostEqual(node_orig.Y0, node_exchanged.Y0) self.assertAlmostEqual(node_orig.Z0, node_exchanged.Z0)
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_Connect_Disconnect(self): connection_settings = CoSimIO.Info() connection_settings.SetString("connection_name", "c_d_test") connection_settings.SetInt("echo_level", 0) info = CoSimIO.Connect(connection_settings) self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Connected) RunPythonInSubProcess("connect_disconnect") disconnect_settings = CoSimIO.Info() disconnect_settings.SetString("connection_name", "c_d_test") info = CoSimIO.Disconnect(disconnect_settings) self.assertEqual(info.GetInt("connection_status"), CoSimIO.ConnectionStatus.Disconnected)
def __init__(self, settings, model, solver_name): super().__init__(settings, model, solver_name) connect_to = self.settings["connect_to"].GetString() if connect_to == "": raise Exception('"connect_to" must be specified!') connection_settings = CoSimIO.InfoFromParameters(self.settings) connection_settings.SetString("my_name", solver_name) info = CoSimIO.Connect(connection_settings) if info.GetInt( "connection_status") != CoSimIO.ConnectionStatus.Connected: raise Exception("Connecting failed!") self.connection_name = info.GetString("connection_name")
def test_InfoFromParameters(self): params = KM.Parameters("""{ "some_bool" : true, "the_string" : "file", "another_string" : "i25mmk", "int_for_it" : 12, "double_val" : 0.223, "array_val_ign" : [0.923, 1.8] }""") info = CoSimIO.InfoFromParameters(params) self.assertEqual(info.Size(), 5) self.assertTrue(info.Has("some_bool")) self.assertTrue(info.Has("the_string")) self.assertTrue(info.Has("another_string")) self.assertTrue(info.Has("int_for_it")) self.assertTrue(info.Has("double_val")) # only int, bool, double & string can be converted, others are ignored self.assertFalse(info.Has("array_val_ign")) self.assertTrue(info.GetBool("some_bool")) self.assertEqual(info.GetString("the_string"), "file") self.assertEqual(info.GetString("another_string"), "i25mmk") self.assertEqual(info.GetInt("int_for_it"), 12) self.assertAlmostEqual(info.GetDouble("double_val"), 0.223)
def ExportData(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.ExportData(info, interface_data.GetModelPart(), interface_data.variable, GetDataLocation(interface_data.location)) elif data_type == "control_signal": info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) info.SetString("identifier", "run_control") info.SetString("control_signal", data_config["control_signal"]) settings = data_config.get("settings") if settings: info.SetInfo("settings", CoSimIO.InfoFromParameters(settings)) CoSimIO.ExportInfo(info) elif data_type == "repeat_time_step": info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) info.SetString("identifier", "repeat_time_step_info") info.SetBool("repeat_time_step", data_config["repeat_time_step"]) CoSimIO.ExportInfo(info) else: raise NotImplementedError( 'Exporting interface data of type "{}" is not implemented for this IO: "{}"' .format(data_type, self._ClassName()))
def test_Connect_Disconnect(self): p = self.__RunPythonInSubProcess("connect_disconnect") connection_settings = CoSimIO.Info() connection_settings.SetString("my_name", "partner_a") connection_settings.SetString("connect_to", "partner_b") 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) 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 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 ExportData(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.ExportData(info, interface_data.GetModelPart(), interface_data.variable, GetDataLocation(interface_data.location)) elif data_type == "control_signal": control_signal_key = data_config["signal"] CoSimIO.SendControlSignal(self.solver_name, data_config["identifier"], control_signal_key) elif data_type == "time": current_time = data_config["time"] CoSimIO.ExportData(self.solver_name, "time_from_co_sim", [current_time]) elif data_type == "convergence_signal": if data_config["is_converged"]: control_signal_key = CoSimIO.ControlSignal.ConvergenceAchieved else: control_signal_key = CoSimIO.ControlSignal.Dummy info = CoSimIO.Info() info.SetString("connection_name", self.solver_name) CoSimIO.SendControlSignal(info, control_signal_key) else: raise NotImplementedError( 'Exporting interface data of type "{}" is not implemented for this IO: "{}"' .format(data_type, self._ClassName()))
def RunSolutionLoop(self): """This function executes the solution loop of the AnalysisStage It can be overridden by derived classes """ while self.KeepAdvancingSolutionLoop(): self.time = self._GetSolver().AdvanceInTime(self.time) self.InitializeSolutionStep() self._GetSolver().Predict() if self.is_strong_coupling: is_converged = False while not is_converged: self.__InnerLoop() info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) is_converged_info = CoSimIO.IsConverged(info) is_converged = is_converged_info.GetBool("is_converged") else: self.__InnerLoop() self.FinalizeSolutionStep() self.OutputSolutionStep()
def RunSolutionLoop(self): """This function executes the solution loop of the AnalysisStage It can be overridden by derived classes """ while self.KeepAdvancingSolutionLoop(): self.time = self._GetSolver().AdvanceInTime(self.time) self.InitializeSolutionStep() self._GetSolver().Predict() if self.is_strong_coupling: repeat_time_step = True while repeat_time_step: self.__InnerLoop() info = CoSimIO.Info() info.SetString("connection_name", self.connection_name) info.SetString("identifier", "repeat_time_step_info") repeat_time_step_info = CoSimIO.ImportInfo(info) repeat_time_step = repeat_time_step_info.GetBool( "repeat_time_step") else: self.__InnerLoop() self.FinalizeSolutionStep() self.OutputSolutionStep()
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)
def _InitializeSolutionStep(info): self.InitializeSolutionStep() return CoSimIO.Info()
def _Predict(info): self._GetSolver().Predict() return CoSimIO.Info()
def _AdvanceInTime(info): current_time = info.GetDouble("current_time") self.time = self._GetSolver().AdvanceInTime(current_time) return CoSimIO.Info()
def _FinalizeSolutionStep(info): self.FinalizeSolutionStep() return CoSimIO.Info()
def _OutputSolutionStep(info): self.OutputSolutionStep() return CoSimIO.Info()
def Initialize(self): super().Initialize() connection_settings = CoSimIO.InfoFromParameters( self.project_parameters["co_sim_settings"]["io_settings"]) info = CoSimIO.Connect(connection_settings) self.connection_name = info.GetString("connection_name") if info.GetInt( "connection_status") != CoSimIO.ConnectionStatus.Connected: raise Exception("Connecting failed!") ## specifying which fields belong to which identifier when im-/exporting data self.data_comm_settings = {} for field_settings in chain( self.project_parameters["co_sim_settings"] ["communication_settings"]["export_fields"], self.project_parameters["co_sim_settings"] ["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) self.data_comm_settings[identifier] = { "model_part": model_part, "variable": variable } # declaring in place the functions that are registered by the CoSimIO 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 _ExportData(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.ExportData(info, model_part, variable, CoSimIO.DataLocation.NodeHistorical) return CoSimIO.Info() def _ExportMesh(info): model_part_name = info.GetString("identifier") export_info = CoSimIO.Info() export_info.SetString("connection_name", self.connection_name) export_info.SetString("identifier", model_part_name.replace(".", "-")) CoSimIO.ExportMesh(export_info, self.model[model_part_name]) return CoSimIO.Info() def _AdvanceInTime(info): current_time = info.GetDouble("current_time") self.time = self._GetSolver().AdvanceInTime(current_time) return CoSimIO.Info() def _InitializeSolutionStep(info): self.InitializeSolutionStep() return CoSimIO.Info() def _Predict(info): self._GetSolver().Predict() return CoSimIO.Info() def _SolveSolutionStep(info): self._GetSolver().SolveSolutionStep() return CoSimIO.Info() def _FinalizeSolutionStep(info): self.FinalizeSolutionStep() return CoSimIO.Info() def _OutputSolutionStep(info): self.OutputSolutionStep() return CoSimIO.Info() register_info = CoSimIO.Info() register_info.SetString("connection_name", self.connection_name) register_info.SetString("function_name", "ImportData") CoSimIO.Register(register_info, _ImportData) register_info.SetString("function_name", "ExportData") CoSimIO.Register(register_info, _ExportData) register_info.SetString("function_name", "ExportMesh") CoSimIO.Register(register_info, _ExportMesh) register_info.SetString("function_name", "AdvanceInTime") CoSimIO.Register(register_info, _AdvanceInTime) register_info.SetString("function_name", "InitializeSolutionStep") CoSimIO.Register(register_info, _InitializeSolutionStep) register_info.SetString("function_name", "Predict") CoSimIO.Register(register_info, _Predict) register_info.SetString("function_name", "SolveSolutionStep") CoSimIO.Register(register_info, _SolveSolutionStep) register_info.SetString("function_name", "FinalizeSolutionStep") CoSimIO.Register(register_info, _FinalizeSolutionStep) register_info.SetString("function_name", "OutputSolutionStep") CoSimIO.Register(register_info, _OutputSolutionStep)
def RunSolutionLoop(self): # running remote controlled simulation # deliberately not calling baseclass as this is the remote controlled case run_info = CoSimIO.Info() run_info.SetString("connection_name", self.connection_name) CoSimIO.Run(run_info)