def UpdateMeshAccordingInputVariable(self, variable):
        print("\n> Starting to update the mesh")
        startTime = timer.time()
        KSO.MeshControllerUtilities(self.OptimizationModelPart).UpdateMeshAccordingInputVariable(variable)
        KSO.MeshControllerUtilities(self.OptimizationModelPart).LogMeshChangeAccordingInputVariable(variable)
        print("> Time needed for updating the mesh = ",round(timer.time() - startTime,2),"s")

# ==============================================================================
    def UpdateMeshAccordingInputVariable(self, variable):
        KM.Logger.Print("")
        KM.Logger.PrintInfo("ShapeOpt", "Starting to update the mesh")
        startTime = timer.time()
        KSO.MeshControllerUtilities(self.OptimizationModelPart).UpdateMeshAccordingInputVariable(variable)
        KSO.MeshControllerUtilities(self.OptimizationModelPart).LogMeshChangeAccordingInputVariable(variable)
        KM.Logger.PrintInfo("ShapeOpt", "Time needed for updating the mesh = ",round(timer.time() - startTime,2),"s")

# ==============================================================================
    def Initialize(self):
        self.vm_mapper.Initialize()

        in_plane_settings = self.settings["in_plane_morphing_settings"]

        background_main_mesh = self._background_model.GetModelPart("background_mesh")
        if in_plane_settings["model_import_settings"]["input_type"].GetString() == "mdpa":
            model_part_io = KM.ModelPartIO(in_plane_settings["model_import_settings"]["input_filename"].GetString())
            model_part_io.ReadModelPart(background_main_mesh)
        elif in_plane_settings["model_import_settings"]["input_type"].GetString() in ["vrml", "wrl"]:
            model_part_io = WrlIO(in_plane_settings["model_import_settings"]["input_filename"].GetString())
            model_part_io.ReadModelPart(background_main_mesh)

        background_sub_model_part_name = in_plane_settings["background_sub_model_part_name"].GetString()
        if background_sub_model_part_name == "":
            self.background_mesh = background_main_mesh
        else:
            self.background_mesh = background_main_mesh.GetSubModelPart(background_sub_model_part_name)

        background_geometry_utilities = KSO.GeometryUtilities(self.background_mesh)
        background_geometry_utilities.ComputeUnitSurfaceNormals()
        self.spacial_mapper = SpacialMapperFactory.CreateMapper(
            self.background_mesh, self.destination_model_part, in_plane_settings["spacial_mapper_settings"])

        KSO.MeshControllerUtilities(self.background_mesh).WriteCoordinatesToVariable(KSO.BACKGROUND_COORDINATE)
    def UpdateMeshAccordingInputVariable(self, variable):
        KM.Logger.Print("")
        KM.Logger.PrintInfo("ShapeOpt", "Starting to update the mesh...")
        startTime = timer.time()

        time_before_update = self.OptimizationModelPart.ProcessInfo.GetValue(KM.TIME)
        step_before_update = self.OptimizationModelPart.ProcessInfo.GetValue(KM.STEP)
        delta_time_before_update = self.OptimizationModelPart.ProcessInfo.GetValue(KM.DELTA_TIME)

        # Reset step/time iterators such that they match the current iteration after calling RunSolutionLoop (which internally calls CloneTimeStep)
        self.OptimizationModelPart.ProcessInfo.SetValue(KM.STEP, step_before_update-1)
        self.OptimizationModelPart.ProcessInfo.SetValue(KM.TIME, time_before_update-1)
        self.OptimizationModelPart.ProcessInfo.SetValue(KM.DELTA_TIME, 0)

        KM.VariableUtils().CopyVectorVar(variable, KM.MESH_DISPLACEMENT, self.OptimizationModelPart.Nodes)

        if not self._mesh_moving_analysis.time < self._mesh_moving_analysis.end_time:
            self._mesh_moving_analysis.end_time += 1
        self._mesh_moving_analysis.RunSolutionLoop()

        KSO.MeshControllerUtilities(self.OptimizationModelPart).LogMeshChangeAccordingInputVariable(KM.MESH_DISPLACEMENT)

        self.OptimizationModelPart.ProcessInfo.SetValue(KM.STEP, step_before_update)
        self.OptimizationModelPart.ProcessInfo.SetValue(KM.TIME, time_before_update)
        self.OptimizationModelPart.ProcessInfo.SetValue(KM.DELTA_TIME, delta_time_before_update)

        KM.Logger.PrintInfo("ShapeOpt", "Time needed for updating the mesh = ",round(timer.time() - startTime,2),"s")
    def UpdateMeshAccordingInputVariable(self, variable):
        KM.Logger.Print("")
        KM.Logger.PrintInfo("ShapeOpt", "Starting to update the mesh...")
        startTime = timer.time()

        time_before_update = self.OptimizationModelPart.ProcessInfo.GetValue(
            KM.TIME)
        step_before_update = self.OptimizationModelPart.ProcessInfo.GetValue(
            KM.STEP)
        delta_time_before_update = self.OptimizationModelPart.ProcessInfo.GetValue(
            KM.DELTA_TIME)

        # Reset step/time iterators such that they match the current iteration after calling RunSolutionLoop (which internally calls CloneTimeStep)
        self.OptimizationModelPart.ProcessInfo.SetValue(
            KM.STEP, step_before_update - 1)
        self.OptimizationModelPart.ProcessInfo.SetValue(
            KM.TIME, time_before_update - 1)
        self.OptimizationModelPart.ProcessInfo.SetValue(KM.DELTA_TIME, 0)

        KM.VariableUtils().CopyVectorVar(variable, KM.MESH_DISPLACEMENT,
                                         self.OptimizationModelPart.Nodes)

        if self.has_automatic_boundary_process and self.is_remeshing_used:
            self.OptimizationModelPart.GetSubModelPart(
                "auto_surface_nodes").GetNodes().clear()
            KSO.GeometryUtilities(
                self.OptimizationModelPart).ExtractBoundaryNodes(
                    "auto_surface_nodes")

        if not self._mesh_moving_analysis.time < self._mesh_moving_analysis.end_time:
            self._mesh_moving_analysis.end_time += 1
        self._mesh_moving_analysis.RunSolutionLoop()

        if self.is_remeshing_used:
            self.OptimizationModelPart.Set(KM.MODIFIED, False)
            self.remeshing_process.ExecuteInitializeSolutionStep()
            self.remeshing_process.ExecuteFinalizeSolutionStep()

        KSO.MeshControllerUtilities(
            self.OptimizationModelPart).LogMeshChangeAccordingInputVariable(
                KM.MESH_DISPLACEMENT)

        self.OptimizationModelPart.ProcessInfo.SetValue(
            KM.STEP, step_before_update)
        self.OptimizationModelPart.ProcessInfo.SetValue(
            KM.TIME, time_before_update)
        self.OptimizationModelPart.ProcessInfo.SetValue(
            KM.DELTA_TIME, delta_time_before_update)

        KM.Logger.PrintInfo("ShapeOpt", "Time needed for updating the mesh = ",
                            round(timer.time() - startTime, 2), "s")
    def _CorrectOutOfPlanePart(self, destination_variable):
        geometry_utilities = KSO.GeometryUtilities(self.destination_model_part)
        mesh_utilities = KSO.MeshControllerUtilities(self.destination_model_part)

        mesh_utilities.UpdateMeshAccordingInputVariable(destination_variable)
        mesh_utilities.SetReferenceMeshToMesh()

        self.spacial_mapper.UpdateInterface()
        self.spacial_mapper.Map(KSO.BACKGROUND_COORDINATE, KSO.BACKGROUND_COORDINATE)
        mesh_utilities.SubtractCoordinatesFromVariable(KSO.BACKGROUND_COORDINATE, KSO.OUT_OF_PLANE_DELTA)
        geometry_utilities.ProjectNodalVariableOnDirection(KSO.OUT_OF_PLANE_DELTA, KSO.BACKGROUND_NORMAL)

        mesh_utilities.RevertMeshUpdateAccordingInputVariable(destination_variable)
        mesh_utilities.SetReferenceMeshToMesh()

        mesh_utilities.AddFirstVariableToSecondVariable(KSO.OUT_OF_PLANE_DELTA, destination_variable)
 def SetDeformationVariablesToZero(self):
     KSO.MeshControllerUtilities(
         self.optimization_model_part).SetDeformationVariablesToZero()
 def SetReferenceMeshToMesh(self):
     KSO.MeshControllerUtilities(
         self.optimization_model_part).SetReferenceMeshToMesh()
Exemple #9
0
    def test_simple_model_part(self):
        model = KM.Model()
        mp = model.CreateModelPart("surface")
        mp.ProcessInfo.SetValue(KM.DOMAIN_SIZE, 3)

        mp.AddNodalSolutionStepVariable(KSO.SHAPE_UPDATE)
        mp.AddNodalSolutionStepVariable(KSO.SHAPE_CHANGE)
        mp.AddNodalSolutionStepVariable(KM.NORMAL)
        mp.AddNodalSolutionStepVariable(KSO.NORMALIZED_SURFACE_NORMAL)

        mp.CreateNewNode(1, 0.0, 0.0, 0.0)
        mp.CreateNewNode(2, 1.0, 0.0, 0.0)
        mp.CreateNewNode(3, 1.0, 1.0, 0.0)
        mp.CreateNewNode(4, 0.0, 1.0, 0.0)
        mp.CreateNewNode(5, 1.0, 1.0, -1.0)
        mp.CreateNewNode(6, 1.0, 0.0, -1.0)

        prop = mp.GetProperties()[1]

        mp.CreateNewCondition("SurfaceCondition3D4N", 1, [1, 2, 3, 4], prop)
        mp.CreateNewCondition("SurfaceCondition3D4N", 2, [2, 6, 5, 3], prop)

        settings = KM.Parameters("""{
            "response_type"         : "surface_normal_shape_change",
            "model_part_name"       : "surface",
            "model_import_settings" : {
                "input_type"        : "use_input_model_part"
            }
        }""")

        response = SurfaceNormalShapeChange("surface_normal", settings, model)

        response.Initialize()
        response.InitializeSolutionStep()
        response.CalculateValue()
        response.CalculateGradient()
        value = response.GetValue()
        gradient = response.GetNodalGradient(KM.SHAPE_SENSITIVITY)

        ref_gradient = {
            1: [0.0, 0.0, 1.0],
            2: [math.sqrt(2.0) / 2.0, 0.0,
                math.sqrt(2.0) / 2.0],
            3: [math.sqrt(2.0) / 2.0, 0.0,
                math.sqrt(2.0) / 2.0],
            4: [0.0, 0.0, 1.0],
            5: [1.0, 0.0, 0.0],
            6: [1.0, 0.0, 0.0],
        }
        self.assertAlmostEqual(value, 0.0)
        for node in mp.Nodes:
            node_id = node.Id
            self.assertVectorAlmostEqual(KM.Vector(gradient[node_id]),
                                         KM.Vector(ref_gradient[node_id]))

        # update nodes and test again
        for node in mp.Nodes:
            node.SetSolutionStepValue(KSO.SHAPE_UPDATE, [1, 0, 0])
        KSO.MeshControllerUtilities(mp).UpdateMeshAccordingInputVariable(
            KSO.SHAPE_UPDATE)

        response.InitializeSolutionStep()
        response.CalculateValue()
        value = response.GetValue()
        self.assertAlmostEqual(value, 2.0 + math.sqrt(2.0))
            # Perturb
            if dim_itr == 0:
                model_part.Nodes[move_node_id].SetSolutionStepValue(
                    KSO.CONTROL_POINT_UPDATE, [current_delta, 0, 0])
            elif dim_itr == 1:
                model_part.Nodes[move_node_id].SetSolutionStepValue(
                    KSO.CONTROL_POINT_UPDATE, [0, current_delta, 0])
            elif dim_itr == 2:
                model_part.Nodes[move_node_id].SetSolutionStepValue(
                    KSO.CONTROL_POINT_UPDATE, [0, 0, current_delta])

            vm_mapper = KSO.MapperVertexMorphingMatrixFree(
                model_part, model_part, mapper_settings)
            vm_mapper.Map(KSO.CONTROL_POINT_UPDATE, KSO.SHAPE_UPDATE)

            KSO.MeshControllerUtilities(
                model_part).UpdateMeshAccordingInputVariable(KSO.SHAPE_UPDATE)
            KSO.MeshControllerUtilities(model_part).SetReferenceMeshToMesh()

            # Output to verify mapping
            OutputModelPart(model_part, "perturbed_part",
                            ["CONTROL_POINT_UPDATE", "SHAPE_UPDATE"])

            # Perform analysis
            response.InitializeSolutionStep()
            response.CalculateValue()
            response.FinalizeSolutionStep()
            response.Finalize()
            current_value = response.GetValue()

            # Evaluate fd gradients
            if dim_itr == 0: