예제 #1
0
    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])
예제 #2
0
    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)
예제 #3
0
    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()))
예제 #4
0
    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!")
예제 #5
0
    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")
예제 #6
0
    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)