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)
Beispiel #2
0
    def ImportCouplingInterface(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.ImportMesh(info, self.model[model_part_name]
                           )  # TODO this can also be geometry at some point
    def ImportCouplingInterface(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.ImportMesh(info, self.model[model_part_name]
                           )  # TODO this can also be geometry at some point
Beispiel #4
0
from KratosMultiphysics.CoSimulationApplication import CoSimIO

model = KM.Model()
model_part = model.CreateModelPart("mp_test")

connection_settings = CoSimIO.Info()
connection_settings.SetString("connection_name", "im_exp_mesh")
connection_settings.SetInt("echo_level", 0)
info = CoSimIO.Connect(connection_settings)
if info.GetInt("connection_status") != CoSimIO.ConnectionStatus.Connected:
    raise Exception("Connecting failed")

import_info = CoSimIO.Info()
import_info.SetString("connection_name", "im_exp_mesh")
import_info.SetString("identifier", "mesh_exchange_1")
CoSimIO.ImportMesh(import_info, model_part)

# print(model_part)

export_info = CoSimIO.Info()
export_info.SetString("connection_name", "im_exp_mesh")
export_info.SetString("identifier", "mesh_exchange_2")
CoSimIO.ExportMesh(export_info, model_part)

disconnect_settings = CoSimIO.Info()
disconnect_settings.SetString("connection_name", "im_exp_mesh")

info = CoSimIO.Disconnect(disconnect_settings)
if info.GetInt("connection_status") != CoSimIO.ConnectionStatus.Disconnected:
    raise Exception("Disconnecting failed")
model_part_origin.AddNodalSolutionStepVariable(KM.VELOCITY)
model_part_destination.AddNodalSolutionStepVariable(KM.MESH_VELOCITY)

# connect to CoSimIO
connection_settings = CoSimIO.Info()
connection_settings.SetString("connection_name", "mesh_mapping")
connection_settings.SetInt("echo_level", 0)
info = CoSimIO.Connect(connection_settings)
if info.GetInt("connection_status") != CoSimIO.ConnectionStatus.Connected:
    raise Exception("Connecting failed")

# import meshes
import_mesh_info_o = CoSimIO.Info()
import_mesh_info_o.SetString("connection_name", "mesh_mapping")
import_mesh_info_o.SetString("identifier", "mesh_origin")
CoSimIO.ImportMesh(import_mesh_info_o, model_part_origin)

import_mesh_info_d = CoSimIO.Info()
import_mesh_info_d.SetString("connection_name", "mesh_mapping")
import_mesh_info_d.SetString("identifier", "mesh_destination")
CoSimIO.ImportMesh(import_mesh_info_d, model_part_destination)

# printing the ModelParts for debugging:
# print(model_part_origin)
# print(model_part_destination)

# input for the mapper is a Kratos::Parameters object
mapper_settings = KM.Parameters("""{
    "mapper_type": "nearest_neighbor",
    "echo_level" : 0
}""")