def __init__(self, origin_model_part, destination_model_part, settings): from KratosMultiphysics.ShapeOptimizationApplication import mapper_factory if not SpacialMapperFactory: raise Exception("InPlaneVertexMorphingMapper: MappingApplication is required!") self.settings = settings self.origin_model_part = origin_model_part self.destination_model_part = destination_model_part extracted_vm_settings = settings.Clone() extracted_vm_settings["in_plane_morphing"].SetBool(False) extracted_vm_settings.RemoveValue("in_plane_morphing_settings") self.vm_mapper = mapper_factory.CreateMapper(origin_model_part, destination_model_part, extracted_vm_settings) in_plane_settings = self.settings["in_plane_morphing_settings"] in_plane_settings.RecursivelyValidateAndAssignDefaults(self.GetDefaultInPlaneSettings()) self._background_model = KM.Model() if in_plane_settings["model_import_settings"]["input_type"].GetString() in ["mdpa", "vrml", "wrl"]: background_main_mesh = self._background_model.CreateModelPart("background_mesh") background_main_mesh.ProcessInfo.SetValue(KM.DOMAIN_SIZE, 3) background_main_mesh.AddNodalSolutionStepVariable(KM.NORMAL) background_main_mesh.AddNodalSolutionStepVariable(KSO.NORMALIZED_SURFACE_NORMAL) background_main_mesh.AddNodalSolutionStepVariable(KSO.BACKGROUND_COORDINATE) else: raise Exception("Other model part input options are not yet implemented.") self.background_mesh = None # created in Initialize self.spacial_mapper = None # created in Initialize
def InitializeOptimizationLoop(self): self.model_part_controller.Initialize() self.analyzer.InitializeBeforeOptimizationLoop() self.design_surface = self.model_part_controller.GetDesignSurface() self.mapper = mapper_factory.CreateMapper(self.design_surface, self.design_surface, self.mapper_settings) self.mapper.Initialize() self.data_logger = data_logger_factory.CreateDataLogger(self.model_part_controller, self.communicator, self.optimization_settings) self.data_logger.InitializeDataLogging() self.optimization_utilities = KSO.OptimizationUtilities
for node in plate_with_trias.Nodes: node.SetSolutionStepValue(KM.PRESSURE, (0.5 - node.X) * (0.5 - node.Y)) # ======================================================================================================= # Perform tests # ======================================================================================================= # Test matrix-free mapper mapper_settings = KM.Parameters(""" { "filter_function_type" : "linear", "filter_radius" : 0.4, "max_nodes_in_filter_radius" : 10000, "matrix_free_filtering" : true }""") matrix_mapper = mapper_factory.CreateMapper(plate_with_trias, plate_with_trias, mapper_settings) matrix_mapper.Map(KSO.CONTROL_POINT_UPDATE, KSO.CONTROL_POINT_CHANGE) matrix_mapper.InverseMap(KSO.CONTROL_POINT_CHANGE, KSO.SHAPE_UPDATE) norm_2_result = Norm2OfVectorVariable(plate_with_trias, KSO.SHAPE_UPDATE) TestCase().assertAlmostEqual(norm_2_result, 1.283132791556226, 12) # Test matrix mapper mapper_settings = KM.Parameters(""" { "filter_function_type" : "linear", "filter_radius" : 0.4, "max_nodes_in_filter_radius" : 1000 }""") matrix_mapper = mapper_factory.CreateMapper(plate_with_trias, plate_with_trias, mapper_settings)
def InitializeOptimizationLoop(self): self.model_part_controller.Initialize() self.model_part_controller.SetMinimalBufferSize(2) self.analyzer.InitializeBeforeOptimizationLoop() self.design_surface = self.model_part_controller.GetDesignSurface() self.mapper = mapper_factory.CreateMapper(self.design_surface, self.design_surface, self.mapper_settings) self.mapper.Initialize() if self.filter_penalty_term: penalty_filter_radius = self.algorithm_settings["penalty_filter_radius"].GetDouble() filter_radius = self.mapper_settings["filter_radius"].GetDouble() if abs(filter_radius - penalty_filter_radius) > 1e-9: penalty_filter_settings = self.mapper_settings.Clone() penalty_filter_settings["filter_radius"].SetDouble(self.algorithm_settings["penalty_filter_radius"].GetDouble()) self.penalty_filter = mapper_factory.CreateMapper(self.design_surface, self.design_surface, penalty_filter_settings) self.penalty_filter.Initialize() else: self.penalty_filter = self.mapper self.data_logger = data_logger_factory.CreateDataLogger(self.model_part_controller, self.communicator, self.optimization_settings) self.data_logger.InitializeDataLogging() self.optimization_utilities = KSO.OptimizationUtilities # Identify fixed design areas KM.VariableUtils().SetFlag(KM.BOUNDARY, False, self.optimization_model_part.Nodes) radius = self.mapper_settings["filter_radius"].GetDouble() search_based_functions = KSO.SearchBasedFunctions(self.design_surface) for itr in range(self.algorithm_settings["fix_boundaries"].size()): sub_model_part_name = self.algorithm_settings["fix_boundaries"][itr].GetString() node_set = self.optimization_model_part.GetSubModelPart(sub_model_part_name).Nodes search_based_functions.FlagNodesInRadius(node_set, KM.BOUNDARY, radius) # Specify bounds and assign starting values for ALPHA if self.bead_side == "positive": KM.VariableUtils().SetScalarVar(KSO.ALPHA, 0.5, self.design_surface.Nodes, KM.BOUNDARY, False) self.lower_bound = 0.0 self.upper_bound = 1.0 elif self.bead_side == "negative": KM.VariableUtils().SetScalarVar(KSO.ALPHA, -0.5, self.design_surface.Nodes, KM.BOUNDARY, False) self.lower_bound = -1.0 self.upper_bound = 0.0 elif self.bead_side == "both": KM.VariableUtils().SetScalarVar(KSO.ALPHA, 0.0, self.design_surface.Nodes, KM.BOUNDARY, False) self.lower_bound = -1.0 self.upper_bound = 1.0 else: raise RuntimeError("Specified bead direction mode not supported!") # Initialize ALPHA_MAPPED according to initial ALPHA values self.mapper.Map(KSO.ALPHA, KSO.ALPHA_MAPPED) # Specify bead direction bead_direction = self.algorithm_settings["bead_direction"].GetVector() if len(bead_direction) == 0: self.model_part_controller.ComputeUnitSurfaceNormals() for node in self.design_surface.Nodes: normalized_normal = node.GetSolutionStepValue(KSO.NORMALIZED_SURFACE_NORMAL) node.SetValue(KSO.BEAD_DIRECTION,normalized_normal) elif len(bead_direction) == 3: norm = math.sqrt(bead_direction[0]**2 + bead_direction[1]**2 + bead_direction[2]**2) normalized_bead_direction = [value/norm for value in bead_direction] KM.VariableUtils().SetNonHistoricalVectorVar(KSO.BEAD_DIRECTION, normalized_bead_direction, self.design_surface.Nodes) else: raise RuntimeError("Wrong definition of bead direction. Options are: 1) [] -> takes surface normal, 2) [x.x,x.x,x.x] -> takes specified vector.")