def ExecuteInitialize(self): # NOTE: Add more model part if interested submodelpartslist = self.__generate_submodelparts_list_from_input(self.params["fix_contour_model_parts"]) for submodelpart in submodelpartslist: for node in submodelpart.Nodes: node.Set(KratosMultiphysics.BLOCKED, True) if (self.strategy == "LevelSet"): self._CreateGradientProcess() if (self.dim == 2): self.initialize_metric = MeshingApplication.MetricFastInit2D(self.Model[self.model_part_name]) else: self.initialize_metric = MeshingApplication.MetricFastInit3D(self.Model[self.model_part_name]) self.initialize_metric.Execute() self._CreateMetricsProcess() mmg_parameters = KratosMultiphysics.Parameters("""{}""") mmg_parameters.AddValue("filename",self.params["filename"]) mmg_parameters.AddValue("framework",self.params["framework"]) mmg_parameters.AddValue("internal_variables_parameters",self.params["internal_variables_parameters"]) mmg_parameters.AddValue("save_external_files",self.params["save_external_files"]) mmg_parameters.AddValue("max_number_of_searchs",self.params["max_number_of_searchs"]) mmg_parameters.AddValue("echo_level",self.params["echo_level"]) if (self.dim == 2): self.MmgProcess = MeshingApplication.MmgProcess2D(self.Model[self.model_part_name], mmg_parameters) else: self.MmgProcess = MeshingApplication.MmgProcess3D(self.Model[self.model_part_name], mmg_parameters) if (self.initial_remeshing == True): self._ExecuteRefinement()
def _create_remeshing_process(self): if (self.main_model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] == 2): remeshing_process = MeshingApplication.MmgProcess2D(self.main_model_part, self.adaptative_remesh_parameters["remeshing_parameters"]) else: remeshing_process = MeshingApplication.MmgProcess3D(self.main_model_part, self.adaptative_remesh_parameters["remeshing_parameters"]) return remeshing_process
def _RefineMesh(self): ''' This function remeshes the main_model_part according to the distance, using the MMG process from the MeshingApplication. In order to perform the refinement, it is needed to calculate the distance gradient, the initial nodal_h and the level_set metric. ''' ini_time=time.time() local_gradient = KratosMultiphysics.ComputeNodalGradientProcess2D(self.main_model_part, KratosMultiphysics.DISTANCE, KratosMultiphysics.DISTANCE_GRADIENT, KratosMultiphysics.NODAL_AREA) local_gradient.Execute() find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess(self.main_model_part) find_nodal_h.Execute() KratosMultiphysics.VariableUtils().SetNonHistoricalVariableToZero(KratosMultiphysics.MeshingApplication.METRIC_TENSOR_2D,self.main_model_part.Nodes) metric_process = MeshingApplication.ComputeLevelSetSolMetricProcess2D(self.main_model_part, KratosMultiphysics.DISTANCE_GRADIENT, self.metric_parameters) metric_process.Execute() mmg_parameters = KratosMultiphysics.Parameters(""" { "discretization_type" : "STANDARD", "save_external_files" : false, "initialize_entities" : false, "echo_level" : 0 } """) mmg_process = MeshingApplication.MmgProcess2D(self.main_model_part, mmg_parameters) mmg_process.Execute() KratosMultiphysics.Logger.PrintInfo('LevelSetRemeshing','Remesh time: ',time.time()-ini_time)
def __ExecuteRefinement(self): if (self.domain_size == 2): MmgProcess = KratosMeshing.MmgProcess2D(self.main_model_part, self.mmg_parameters) MmgProcess.Execute() elif (self.domain_size == 3): MmgProcess = KratosMeshing.MmgProcess3D(self.main_model_part, self.mmg_parameters) MmgProcess.Execute()
def _create_remeshing_process(self): if (self.main_model_part.ProcessInfo[KM.DOMAIN_SIZE] == 2): remeshing_process = MA.MmgProcess2D( self.main_model_part, self.adaptative_remesh_parameters["remeshing_parameters"]) else: remeshing_process = MA.MmgProcess3D( self.main_model_part, self.adaptative_remesh_parameters["remeshing_parameters"]) return remeshing_process
def compute_refinement_hessian_metric(simulation_coarse, minimal_size_value, maximal_size_value, metric_param, remesh_param): simulation_coarse._GetSolver().print_on_rank_zero( "::[compute_refinement]:: ", "refinement started") '''set NODAL_AREA and NODAL_H as non historical variables''' KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( KratosMultiphysics.NODAL_AREA, 0.0, simulation_coarse._GetSolver().main_model_part.Nodes) KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( KratosMultiphysics.NODAL_H, 0.0, simulation_coarse._GetSolver().main_model_part.Nodes) '''calculate NODAL_H''' find_nodal_h = KratosMultiphysics.FindNodalHProcess( simulation_coarse._GetSolver().main_model_part) find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess( simulation_coarse._GetSolver().main_model_part) find_nodal_h.Execute() '''prepare parameters to calculate the gradient of the designed variable''' local_gradient_variable_string = metric_param[ "local_gradient_variable"].GetString() local_gradient_variable = KratosMultiphysics.KratosGlobals.GetVariable( metric_param["local_gradient_variable"].GetString()) metric_param.RemoveValue("local_gradient_variable") metric_param.AddEmptyValue("minimal_size") metric_param["minimal_size"].SetDouble(minimal_size_value) metric_param.AddEmptyValue("maximal_size") metric_param["maximal_size"].SetDouble(maximal_size_value) '''calculate the gradient of the variable''' local_gradient = KratosMeshing.ComputeHessianSolMetricProcess( simulation_coarse._GetSolver().main_model_part, local_gradient_variable, metric_param) local_gradient.Execute() '''add again the removed variable parameter''' metric_param.AddEmptyValue("local_gradient_variable") metric_param["local_gradient_variable"].SetString( local_gradient_variable_string) '''create the remeshing process''' MmgProcess = KratosMeshing.MmgProcess2D( simulation_coarse._GetSolver().main_model_part, remesh_param) MmgProcess.Execute() '''the refinement process empties the coarse model part object and fill it with the refined model part the solution on the refined grid is obtained from the interpolation of the coarse solution there are not other operations, so to build the new model we just need to take the updated coarse model''' simulation_coarse._GetSolver().print_on_rank_zero( "::[compute_refinement]:: ", "start saving refined model and parameters") current_model_refined = simulation_coarse.model return current_model_refined
def CreateRemeshingProcess(main_model_part, adaptative_remesh_parameters): if main_model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] == 2: remeshing_process = MeshingApplication.MmgProcess2D(main_model_part, adaptative_remesh_parameters["remeshing_parameters"]) else: is_surface = False for elem in main_model_part.Elements: geom = elem.GetGeometry() if geom.WorkingSpaceDimension() != geom.LocalSpaceDimension(): is_surface = True break if is_surface: remeshing_process = MeshingApplication.MmgProcess3DSurfaces(main_model_part, adaptative_remesh_parameters["remeshing_parameters"]) else: remeshing_process = MeshingApplication.MmgProcess3D(main_model_part, adaptative_remesh_parameters["remeshing_parameters"]) return remeshing_process
def compute_refinement_hessian_metric(model_coarse,parameters_coarse,minimal_size_value,maximal_size_value,metric_param,remesh_param): model_part_name = parameters_coarse["problem_data"]["model_part_name"].GetString() '''set NODAL_AREA and NODAL_H as non historical variables''' KratosMultiphysics.VariableUtils().SetNonHistoricalVariable(KratosMultiphysics.NODAL_AREA, 0.0, model_coarse.GetModelPart(model_part_name).Nodes) KratosMultiphysics.VariableUtils().SetNonHistoricalVariable(KratosMultiphysics.NODAL_H, 0.0, model_coarse.GetModelPart(model_part_name).Nodes) '''calculate NODAL_H''' find_nodal_h = KratosMultiphysics.FindNodalHProcess(model_coarse.GetModelPart(model_part_name)) find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess(model_coarse.GetModelPart(model_part_name)) find_nodal_h.Execute() '''prepare parameters to calculate the gradient of the designed variable''' local_gradient_variable_string = metric_param["local_gradient_variable"].GetString() local_gradient_variable = KratosMultiphysics.KratosGlobals.GetVariable(metric_param["local_gradient_variable"].GetString()) metric_param.RemoveValue("local_gradient_variable") metric_param.AddEmptyValue("minimal_size") metric_param["minimal_size"].SetDouble(minimal_size_value) metric_param.AddEmptyValue("maximal_size") metric_param["maximal_size"].SetDouble(maximal_size_value) '''calculate the gradient of the variable''' local_gradient = KratosMeshing.ComputeHessianSolMetricProcess(model_coarse.GetModelPart(model_part_name),local_gradient_variable,metric_param) local_gradient.Execute() '''add again the removed variable parameter''' metric_param.AddEmptyValue("local_gradient_variable") metric_param["local_gradient_variable"].SetString(local_gradient_variable_string) '''create the remeshing process''' MmgProcess = KratosMeshing.MmgProcess2D(model_coarse.GetModelPart(model_part_name),remesh_param) MmgProcess.Execute() '''the refinement process empties the coarse model part object and fill it with the refined model part the solution on the refined grid is obtained from the interpolation of the coarse solution there are not other operations, so to build the new model we just need to take the updated coarse model''' current_model_refined = model_coarse current_parameters_refined = parameters_coarse return current_model_refined,current_parameters_refined
def ExecuteInitialize(self): # Calculate NODAL_H self.find_nodal_h = KratosMultiphysics.FindNodalHProcess( self.model_part) self.find_nodal_h.Execute() # Calculate the parameters of automatic remeshing if (self.settings["automatic_remesh"].GetBool() == True): import statistics as stat nodal_h_values = [] for node in self.model_part.Nodes: nodal_h_values.append( node.GetSolutionStepValue(KratosMultiphysics.NODAL_H)) # Calculate the minimum size if (self.settings["automatic_remesh_parameters"] ["automatic_remesh_type"].GetString() == "Ratio"): # NOTE: For mode: https://docs.python.org/3/library/statistics.html if (self.settings["automatic_remesh_parameters"] ["refer_type"].GetString() == "Mean"): ref = stat.mean(nodal_h_values) elif (self.settings["automatic_remesh_parameters"] ["refer_type"].GetString() == "Median"): ref = stat.median(nodal_h_values) self.settings["minimal_size"].SetDouble( ref * (self.settings["automatic_remesh_parameters"] ["min_size_ratio"].GetDouble())) self.settings["maximal_size"].SetDouble( ref * (self.settings["automatic_remesh_parameters"] ["max_size_ratio"].GetDouble())) elif (self.settings["automatic_remesh_parameters"] ["automatic_remesh_type"].GetString() == "Percentage"): mean = stat.mean(nodal_h_values) stdev = stat.stdev(nodal_h_values) prob = (self.settings["automatic_remesh_parameters"] ["min_size_current_percentage"].GetDouble()) / 100 self.settings["minimal_size"].SetDouble( _normvalf(prob, mean, stdev) ) # Using normal normal distribution to get the minimal size as a stadistical meaninful value prob = (self.settings["automatic_remesh_parameters"] ["max_size_current_percentage"].GetDouble()) / 100 self.settings["maximal_size"].SetDouble( _normvalf(prob, mean, stdev) ) # Using normal normal distribution to get the maximal size as a stadistical meaninful value # Anisotropic remeshing parameters self.anisotropy_remeshing = self.settings[ "anisotropy_remeshing"].GetBool() if (self.anisotropy_remeshing == True): if (self.settings["automatic_remesh"].GetBool() == True): self.settings["anisotropy_parameters"][ "boundary_layer_max_distance"].SetDouble( self.settings["minimal_size"].GetDouble() * self.settings["anisotropy_parameters"] ["boundary_layer_min_size_ratio"].GetDouble()) # Select the remeshing strategy self.strategy = self.settings["strategy"].GetString() if (self.strategy == "LevelSet"): self.scalar_variable = KratosMultiphysics.KratosGlobals.GetVariable( self.settings["level_set_strategy_parameters"] ["scalar_variable"].GetString()) self.gradient_variable = KratosMultiphysics.KratosGlobals.GetVariable( self.settings["level_set_strategy_parameters"] ["gradient_variable"].GetString()) elif (self.strategy == "Hessian"): self.metric_variable = self.__generate_variable_list_from_input( self.settings["hessian_strategy_parameters"] ["metric_variable"]) mesh_dependent_constant = self.settings[ "hessian_strategy_parameters"][ "mesh_dependent_constant"].GetDouble() if (mesh_dependent_constant == 0.0): self.settings["hessian_strategy_parameters"][ "mesh_dependent_constant"].SetDouble( 0.5 * (self.dim / (self.dim + 1))**2.0) self.internal_variable_interpolation_list = self.__generate_internal_variable_list_from_input( self.settings["internal_variables_parameters"] ["internal_variable_interpolation_list"]) # NOTE: Add more model part if interested submodelpartslist = self.__generate_submodelparts_list_from_input( self.settings["fix_contour_model_parts"]) for submodelpart in submodelpartslist: for node in submodelpart.Nodes: node.Set(KratosMultiphysics.BLOCKED, True) if (self.strategy == "LevelSet"): self._CreateGradientProcess() if (self.dim == 2): self.initialize_metric = MeshingApplication.MetricFastInit2D( self.model_part) else: self.initialize_metric = MeshingApplication.MetricFastInit3D( self.model_part) self.initialize_metric.Execute() self._CreateMetricsProcess() mmg_parameters = KratosMultiphysics.Parameters( """{"force_sizes":{}}""") mmg_parameters.AddValue("filename", self.settings["filename"]) mmg_parameters.AddValue("framework", self.settings["framework"]) mmg_parameters.AddValue("internal_variables_parameters", self.settings["internal_variables_parameters"]) mmg_parameters.AddValue("save_external_files", self.settings["save_external_files"]) mmg_parameters.AddValue("max_number_of_searchs", self.settings["max_number_of_searchs"]) mmg_parameters["force_sizes"].AddValue("force_min", self.settings["force_min"]) mmg_parameters["force_sizes"].AddValue("minimal_size", self.settings["maximal_size"]) mmg_parameters["force_sizes"].AddValue("force_max", self.settings["force_max"]) mmg_parameters["force_sizes"].AddValue("maximal_size", self.settings["maximal_size"]) mmg_parameters.AddValue("advanced_parameters", self.settings["advanced_parameters"]) mmg_parameters.AddValue("echo_level", self.settings["echo_level"]) if (self.dim == 2): self.mmg_process = MeshingApplication.MmgProcess2D( self.model_part, mmg_parameters) else: self.mmg_process = MeshingApplication.MmgProcess3D( self.model_part, mmg_parameters) # We reset the step self.step = 0 # We compute initial remeshing is desired if (self.initial_remeshing == True): if (self.model_part.Is(KratosMultiphysics.MODIFIED) == False): self._ExecuteRefinement() else: self.model_part.Set(KratosMultiphysics.MODIFIED, False)
def ExecuteInitialize(self): """ This method is executed at the begining to initialize the process Keyword arguments: self -- It signifies an instance of a class. """ # Calculate NODAL_H KratosMultiphysics.VariableUtils().SetNonHistoricalVariable(KratosMultiphysics.NODAL_H, 0.0, self.main_model_part.Nodes) KratosMultiphysics.VariableUtils().SetNonHistoricalVariable(KratosMultiphysics.NODAL_AREA, 0.0, self.main_model_part.Nodes) self.find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess(self.main_model_part) self.find_nodal_h.Execute() # Calculate the parameters of automatic remeshing if self.settings["automatic_remesh"].GetBool(): import statistics as stat nodal_h_values = [] for node in self.main_model_part.Nodes: nodal_h_values.append(node.GetValue(KratosMultiphysics.NODAL_H)) # Calculate the minimum size if self.settings["automatic_remesh_parameters"]["automatic_remesh_type"].GetString() == "Ratio": # NOTE: For mode: https://docs.python.org/3/library/statistics.html if self.settings["automatic_remesh_parameters"]["refer_type"].GetString() == "Mean": ref = stat.mean(nodal_h_values) elif self.settings["automatic_remesh_parameters"]["refer_type"].GetString() == "Median": ref = stat.median(nodal_h_values) self.settings["minimal_size"].SetDouble(ref * (self.settings["automatic_remesh_parameters"]["min_size_ratio"].GetDouble())) self.settings["maximal_size"].SetDouble(ref * (self.settings["automatic_remesh_parameters"]["max_size_ratio"].GetDouble())) elif self.settings["automatic_remesh_parameters"]["automatic_remesh_type"].GetString() == "Percentage": mean = stat.mean(nodal_h_values) stdev = stat.stdev(nodal_h_values) prob = (self.settings["automatic_remesh_parameters"]["min_size_current_percentage"].GetDouble())/100 self.settings["minimal_size"].SetDouble(_normvalf(prob, mean, stdev)) # Using normal normal distribution to get the minimal size as a stadistical meaninful value prob = (self.settings["automatic_remesh_parameters"]["max_size_current_percentage"].GetDouble())/100 self.settings["maximal_size"].SetDouble(_normvalf(prob, mean, stdev)) # Using normal normal distribution to get the maximal size as a stadistical meaninful value # Anisotropic remeshing parameters self.anisotropy_remeshing = self.settings["anisotropy_remeshing"].GetBool() if self.anisotropy_remeshing: if self.settings["automatic_remesh"].GetBool(): self.settings["anisotropy_parameters"]["boundary_layer_max_distance"].SetDouble(self.settings["minimal_size"].GetDouble() * self.settings["anisotropy_parameters"]["boundary_layer_min_size_ratio"].GetDouble()) # Select the remeshing strategy self.strategy = self.settings["strategy"].GetString() if self.strategy == "LevelSet": self.scalar_variable = KratosMultiphysics.KratosGlobals.GetVariable( self.settings["level_set_strategy_parameters"]["scalar_variable"].GetString() ) self.gradient_variable = KratosMultiphysics.KratosGlobals.GetVariable( self.settings["level_set_strategy_parameters"]["gradient_variable"].GetString() ) elif self.strategy == "Hessian": self.metric_variable = self.__generate_variable_list_from_input(self.settings["hessian_strategy_parameters"]["metric_variable"]) mesh_dependent_constant = self.settings["hessian_strategy_parameters"]["mesh_dependent_constant"].GetDouble() if mesh_dependent_constant == 0.0: self.settings["hessian_strategy_parameters"]["mesh_dependent_constant"].SetDouble(0.5 * (self.domain_size/(self.domain_size + 1))**2.0) elif self.strategy == "superconvergent_patch_recovery": self.error_threshold = self.settings["error_strategy_parameters"]["error_metric_parameters"]["error_threshold"].GetDouble() self.estimated_error = 0 self.remeshing_cycle = 0 self.main_model_part.ProcessInfo[MeshingApplication.EXECUTE_REMESHING] = True self.internal_variable_interpolation_list = self.__generate_internal_variable_list_from_input(self.settings["internal_variables_parameters"]["internal_variable_interpolation_list"]) # Model parts to fix the nodes fix_contour_model_parts = self.__generate_submodelparts_list_from_input(self.settings["fix_contour_model_parts"]) # Setting flag BLOCKED to the non nodes for submodelpart in fix_contour_model_parts: KratosMultiphysics.VariableUtils().SetFlag(KratosMultiphysics.BLOCKED, True, submodelpart.Nodes) # Model parts to fix the conditions fix_conditions_model_parts = self.__generate_submodelparts_list_from_input(self.settings["fix_conditions_model_parts"]) # Setting flag BLOCKED to the non conditions for submodelpart in fix_conditions_model_parts: KratosMultiphysics.VariableUtils().SetFlag(KratosMultiphysics.BLOCKED, True, submodelpart.Conditions) # Model parts to fix the nodes fix_elements_model_parts = self.__generate_submodelparts_list_from_input(self.settings["fix_elements_model_parts"]) # Setting flag BLOCKED to the non elements for submodelpart in fix_elements_model_parts: KratosMultiphysics.VariableUtils().SetFlag(KratosMultiphysics.BLOCKED, True, submodelpart.Elements) if self.strategy == "LevelSet": self._CreateGradientProcess() if self.domain_size == 2: self.initialize_metric = MeshingApplication.MetricFastInit2D(self.main_model_part) else: self.initialize_metric = MeshingApplication.MetricFastInit3D(self.main_model_part) self.initialize_metric.Execute() self._CreateMetricsProcess() mmg_parameters = KratosMultiphysics.Parameters("""{"force_sizes":{}}""") mmg_parameters.AddValue("filename",self.settings["filename"]) mmg_parameters.AddValue("framework",self.settings["framework"]) mmg_parameters.AddValue("discretization_type",self.settings["discretization_type"]) mmg_parameters.AddValue("isosurface_parameters",self.settings["isosurface_parameters"]) mmg_parameters.AddValue("internal_variables_parameters",self.settings["internal_variables_parameters"]) mmg_parameters.AddValue("save_external_files",self.settings["save_external_files"]) mmg_parameters.AddValue("save_colors_files",self.settings["save_colors_files"]) mmg_parameters.AddValue("save_mdpa_file",self.settings["save_mdpa_file"]) mmg_parameters.AddValue("max_number_of_searchs",self.settings["max_number_of_searchs"]) mmg_parameters.AddValue("preserve_flags",self.settings["preserve_flags"]) mmg_parameters.AddValue("interpolate_non_historical",self.settings["interpolate_non_historical"]) mmg_parameters.AddValue("extrapolate_contour_values",self.settings["extrapolate_contour_values"]) mmg_parameters.AddValue("search_parameters",self.settings["search_parameters"]) mmg_parameters["force_sizes"].AddValue("force_min",self.settings["force_min"]) mmg_parameters["force_sizes"].AddValue("minimal_size",self.settings["minimal_size"]) mmg_parameters["force_sizes"].AddValue("force_max",self.settings["force_max"]) mmg_parameters["force_sizes"].AddValue("maximal_size",self.settings["maximal_size"]) mmg_parameters.AddValue("advanced_parameters",self.settings["advanced_parameters"]) mmg_parameters.AddValue("debug_result_mesh",self.settings["debug_result_mesh"]) mmg_parameters.AddValue("initialize_entities",self.settings["initialize_entities"]) mmg_parameters.AddValue("echo_level",self.settings["echo_level"]) if self.domain_size == 2: self.mmg_process = MeshingApplication.MmgProcess2D(self.main_model_part, mmg_parameters) else: # Differentiate between 3D volumes and 3D surfaces if self.is_surface: self.mmg_process = MeshingApplication.MmgProcess3DSurfaces(self.main_model_part, mmg_parameters) else: self.mmg_process = MeshingApplication.MmgProcess3D(self.main_model_part, mmg_parameters) # We reset the step self.step = 0 # We compute initial remeshing is desired if self.initial_remeshing: if not self.main_model_part.Is(KratosMultiphysics.MODIFIED): self._ExecuteRefinement() else: self.main_model_part.Set(KratosMultiphysics.MODIFIED, False)
def test_remesh_rectangle_hessian(self): KratosMultiphysics.Logger.GetDefaultOutput().SetSeverity( KratosMultiphysics.Logger.Severity.WARNING) # We create the model part current_model = KratosMultiphysics.Model() main_model_part = current_model.CreateModelPart("MainModelPart", 2) main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DOMAIN_SIZE, 2) main_model_part.ProcessInfo.SetValue(KratosMultiphysics.TIME, 0.0) main_model_part.ProcessInfo.SetValue(KratosMultiphysics.DELTA_TIME, 1.0) #main_model_part.ProcessInfo.SetValue(KratosMultiphysics.STEP, 1) # We add the variables needed main_model_part.AddNodalSolutionStepVariable( KratosMultiphysics.DISTANCE) # We import the model main_model_part file_path = os.path.dirname(os.path.realpath(__file__)) KratosMultiphysics.ModelPartIO(file_path + "/mmg_lagrangian_test/remesh_rectangle" ).ReadModelPart(main_model_part) # We calculate the gradient of the distance variable find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess( main_model_part) find_nodal_h.Execute() KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( KratosMultiphysics.NODAL_AREA, 0.0, main_model_part.Nodes) main_model_part.Nodes[1].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 9.86358e-08) main_model_part.Nodes[2].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 4.88117e-07) main_model_part.Nodes[3].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 8.21649e-07) main_model_part.Nodes[4].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 2.98426e-06) main_model_part.Nodes[5].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 1.9462e-06) main_model_part.Nodes[6].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 3.20072e-06) main_model_part.Nodes[7].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 8.13038e-07) main_model_part.Nodes[8].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 1.74975e-06) main_model_part.Nodes[9].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 8.66819e-06) main_model_part.Nodes[10].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 6.24329e-06) main_model_part.Nodes[11].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 1.10461e-05) main_model_part.Nodes[12].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 7.01043e-07) main_model_part.Nodes[13].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 1.90256e-05) main_model_part.Nodes[14].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 2.51694e-06) main_model_part.Nodes[15].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 1.51568e-05) main_model_part.Nodes[16].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 3.0177e-05) main_model_part.Nodes[17].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 8.48471e-06) main_model_part.Nodes[18].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 8.37749e-08) main_model_part.Nodes[19].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 4.64653e-07) main_model_part.Nodes[20].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 2.64854e-05) main_model_part.Nodes[21].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 3.71692e-05) main_model_part.Nodes[22].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 3.28506e-06) main_model_part.Nodes[23].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 1.97626e-05) main_model_part.Nodes[24].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 6.49145e-05) main_model_part.Nodes[25].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 1, 20e-02) main_model_part.Nodes[26].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 4.05303e-05) main_model_part.Nodes[27].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 6.49053e-05) main_model_part.Nodes[28].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 3.71567e-05) main_model_part.Nodes[29].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 3.01782e-05) main_model_part.Nodes[30].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000118602) main_model_part.Nodes[31].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 5.74756e-05) main_model_part.Nodes[32].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 6.26979e-05) main_model_part.Nodes[33].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 6.33597e-05) main_model_part.Nodes[34].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000101589) main_model_part.Nodes[35].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000192486) main_model_part.Nodes[36].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 9.93803e-05) main_model_part.Nodes[37].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000116324) main_model_part.Nodes[38].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 7.68763e-05) main_model_part.Nodes[39].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000149051) main_model_part.Nodes[40].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000289484) main_model_part.Nodes[41].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000192959) main_model_part.Nodes[42].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000145925) main_model_part.Nodes[43].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 9.79472e-05) main_model_part.Nodes[44].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000201518) main_model_part.Nodes[45].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000401176) main_model_part.Nodes[46].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000291243) main_model_part.Nodes[47].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000205528) main_model_part.Nodes[48].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000119595) main_model_part.Nodes[49].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000262135) main_model_part.Nodes[50].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000415149) main_model_part.Nodes[51].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.00053798) main_model_part.Nodes[52].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000268931) main_model_part.Nodes[53].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000139653) main_model_part.Nodes[54].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000331001) main_model_part.Nodes[55].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000546243) main_model_part.Nodes[56].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000675476) main_model_part.Nodes[57].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000327043) main_model_part.Nodes[58].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000158685) main_model_part.Nodes[59].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.00039571) main_model_part.Nodes[60].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000651727) main_model_part.Nodes[61].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000370579) main_model_part.Nodes[62].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000809699) main_model_part.Nodes[63].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000170989) main_model_part.Nodes[64].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000445764) main_model_part.Nodes[65].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000784852) main_model_part.Nodes[66].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000433683) main_model_part.Nodes[67].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000914023) main_model_part.Nodes[68].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000179197) main_model_part.Nodes[69].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.00047077) main_model_part.Nodes[70].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000904417) main_model_part.Nodes[71].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000464902) main_model_part.Nodes[72].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000994274) main_model_part.Nodes[73].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000173952) main_model_part.Nodes[74].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000466276) main_model_part.Nodes[75].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000971188) main_model_part.Nodes[76].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000474027) main_model_part.Nodes[77].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000946149) main_model_part.Nodes[78].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000160469) main_model_part.Nodes[79].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000397979) main_model_part.Nodes[80].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000996743) main_model_part.Nodes[81].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000455294) main_model_part.Nodes[82].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000925016) main_model_part.Nodes[83].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000132904) main_model_part.Nodes[84].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000373155) main_model_part.Nodes[85].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000963824) main_model_part.Nodes[86].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000386671) main_model_part.Nodes[87].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000858186) main_model_part.Nodes[88].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 9.47787e-05) main_model_part.Nodes[89].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000851174) main_model_part.Nodes[90].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000282759) main_model_part.Nodes[91].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000285314) main_model_part.Nodes[92].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000688848) main_model_part.Nodes[93].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 4.35228e-05) main_model_part.Nodes[94].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000686866) main_model_part.Nodes[95].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000179265) main_model_part.Nodes[96].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000175715) main_model_part.Nodes[97].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000386179) main_model_part.Nodes[98].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 1.59849e-05) main_model_part.Nodes[99].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 1.7304e-05) main_model_part.Nodes[100].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0.000382607) main_model_part.Nodes[101].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0) main_model_part.Nodes[102].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0) main_model_part.Nodes[103].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0) main_model_part.Nodes[104].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0) main_model_part.Nodes[105].SetSolutionStepValue( KratosMultiphysics.DISTANCE, 0) # We set to zero the metric ZeroVector = KratosMultiphysics.Vector(3) ZeroVector[0] = 0.0 ZeroVector[1] = 0.0 ZeroVector[2] = 0.0 for node in main_model_part.Nodes: node.SetValue(MeshingApplication.METRIC_TENSOR_2D, ZeroVector) # We define a metric using the ComputeLevelSetSolMetricProcess MetricParameters = KratosMultiphysics.Parameters(""" { "hessian_strategy_parameters" :{ "estimate_interpolation_error" : false, "interpolation_error" : 1.0e-6, "mesh_dependent_constant" : 0.28125 }, "minimal_size" : 0.015, "maximal_size" : 0.5, "sizing_parameters": { "boundary_layer_max_distance" : 1.0, "interpolation" : "constant" }, "enforce_current" : false, "anisotropy_remeshing" : false, "enforce_anisotropy_relative_variable" : false } """) metric_process = MeshingApplication.ComputeHessianSolMetricProcess( main_model_part, KratosMultiphysics.DISTANCE, MetricParameters) metric_process.Execute() mmg_parameters = KratosMultiphysics.Parameters(""" { "filename" : "mmg_lagrangian_test/remesh_rectangle", "save_external_files" : true, "echo_level" : 0 } """) # We create the remeshing utility mmg_parameters["filename"].SetString( file_path + "/" + mmg_parameters["filename"].GetString()) mmg_process = MeshingApplication.MmgProcess2D(main_model_part, mmg_parameters) # We remesh mmg_process.Execute() ## Finally we export to GiD #from gid_output_process import GiDOutputProcess #gid_output = GiDOutputProcess(main_model_part, #"gid_output", #KratosMultiphysics.Parameters(""" #{ #"result_file_configuration" : { #"gidpost_flags": { #"GiDPostMode": "GiD_PostBinary", #"WriteDeformedMeshFlag": "WriteUndeformed", #"WriteConditionsFlag": "WriteConditions", #"MultiFileFlag": "SingleFile" #}, #"nodal_results" : ["DISTANCE"], #"nodal_nonhistorical_results": ["METRIC_TENSOR_2D","AUXILIAR_GRADIENT","AUXILIAR_HESSIAN"] #} #} #""") #) #gid_output.ExecuteInitialize() #gid_output.ExecuteBeforeSolutionLoop() #gid_output.ExecuteInitializeSolutionStep() #gid_output.PrintOutput() #gid_output.ExecuteFinalizeSolutionStep() #gid_output.ExecuteFinalize() import KratosMultiphysics.from_json_check_result_process as from_json_check_result_process json_check_parameters = KratosMultiphysics.Parameters(""" { "check_variables" : ["METRIC_TENSOR_2D"], "input_file_name" : "mmg_lagrangian_test/remesh_rectangle_post_metric.json", "model_part_name" : "MainModelPart", "historical_value" : false, "time_frequency" : 0.0 } """) json_check_parameters["input_file_name"].SetString( file_path + "/" + json_check_parameters["input_file_name"].GetString()) json_check = from_json_check_result_process.FromJsonCheckResultProcess( current_model, json_check_parameters) json_check.ExecuteInitialize() json_check.ExecuteFinalizeSolutionStep() # We check the solution check_parameters = KratosMultiphysics.Parameters(""" { "reference_file_name" : "mmg_lagrangian_test/remesh_rectangle_result.sol", "output_file_name" : "mmg_lagrangian_test/remesh_rectangle_step=0.sol", "dimension" : 2, "comparison_type" : "sol_file" } """) check_parameters["reference_file_name"].SetString( file_path + "/" + check_parameters["reference_file_name"].GetString()) check_parameters["output_file_name"].SetString( file_path + "/" + check_parameters["output_file_name"].GetString()) check_files = CompareTwoFilesCheckProcess(check_parameters) check_files.ExecuteInitialize() check_files.ExecuteBeforeSolutionLoop() check_files.ExecuteInitializeSolutionStep() check_files.ExecuteFinalizeSolutionStep() check_files.ExecuteFinalize()
def ComputeAdaptiveRefinement(self): parameters_coarse = self.parameters_coarse model_coarse = self.model_coarse metric_param = self.metric_param remesh_param = self.remesh_param problem_type = self.problem_type current_level = self.current_level if (self.metric is "hessian"): original_interp_error = metric_param[ "hessian_strategy_parameters"][ "interpolation_error"].GetDouble() # problem dependent section if (problem_type == "potential_flow"): model_part_name = parameters_coarse["solver_settings"][ "model_part_name"].GetString() # set NODAL_AREA and NODAL_H as non historical variables KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( KratosMultiphysics.NODAL_AREA, 0.0, model_coarse.GetModelPart(model_part_name).Nodes) KratosMultiphysics.VariableUtils().SetNonHistoricalVariable( KratosMultiphysics.NODAL_H, 0.0, model_coarse.GetModelPart(model_part_name).Nodes) # Setting Metric Tensor to 0 KratosMultiphysics.VariableUtils( ).SetNonHistoricalVariableToZero( KratosMultiphysics.MeshingApplication.METRIC_TENSOR_2D, model_coarse.GetModelPart(model_part_name).Nodes) # calculate NODAL_H find_nodal_h = KratosMultiphysics.FindNodalHProcess( model_coarse.GetModelPart(model_part_name)) find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess( model_coarse.GetModelPart(model_part_name)) find_nodal_h.Execute() custom_gradient = KratosMultiphysics.CompressiblePotentialFlowApplication.ComputeCustomNodalGradientProcess( model_coarse.GetModelPart(model_part_name), KratosMultiphysics.VELOCITY, KratosMultiphysics.NODAL_AREA) custom_gradient.Execute() if metric_param.Has("local_gradient_variable"): metric_param.RemoveValue("local_gradient_variable") if current_level > 0: coefficient_interp_error = metric_param[ "hessian_strategy_parameters"][ "coefficient_interpolation_error"].GetDouble() metric_param["hessian_strategy_parameters"].RemoveValue( "coefficient_interpolation_error") # interp_error = original_interp_error*(coefficient_interp_error)**(-current_level) interp_error = original_interp_error / ( coefficient_interp_error * current_level) metric_param["hessian_strategy_parameters"][ "interpolation_error"].SetDouble(interp_error) local_gradient = KratosMeshing.ComputeHessianSolMetricProcess( model_coarse.GetModelPart(model_part_name), KratosMultiphysics.VELOCITY_X, metric_param) local_gradient.Execute() local_gradient = KratosMeshing.ComputeHessianSolMetricProcess( model_coarse.GetModelPart(model_part_name), KratosMultiphysics.VELOCITY_Y, metric_param) local_gradient.Execute() # #### OLD APPROACH # for node in model_coarse.GetModelPart(model_part_name).Nodes: # vector=node.GetValue(KratosMultiphysics.VELOCITY) # norm=np.linalg.norm(vector) # node.SetSolutionStepValue(KratosMultiphysics.TEMPERATURE,norm) # prepare parameters to calculate the gradient of the designed variable # local_gradient_variable_string = metric_param["local_gradient_variable"].GetString() # local_gradient_variable = KratosMultiphysics.KratosGlobals.GetVariable(metric_param["local_gradient_variable"].GetString()) # set interpolation error value (level dependent) # calculate the gradient of the variable # local_gradient = KratosMeshing.ComputeHessianSolMetricProcess(model_coarse.GetModelPart(model_part_name),local_gradient_variable,metric_param) # local_gradient.Execute() # # add again the removed variable parameter # metric_param.AddEmptyValue("local_gradient_variable") # metric_param["local_gradient_variable"].SetString(local_gradient_variable_string) #### OLD APPROACH elif (problem_type == "monolithic"): if metric_param.Has("local_gradient_variable"): metric_param.RemoveValue("local_gradient_variable") if current_level > 0: coefficient_interp_error = metric_param[ "hessian_strategy_parameters"][ "coefficient_interpolation_error"].GetDouble() metric_param["hessian_strategy_parameters"].RemoveValue( "coefficient_interpolation_error") # interp_error = original_interp_error*(coefficient_interp_error)**(-current_level) interp_error = original_interp_error / ( coefficient_interp_error * current_level) metric_param["hessian_strategy_parameters"][ "interpolation_error"].SetDouble(interp_error) model_part_name = parameters_coarse["solver_settings"][ "model_part_name"].GetString() # Setting Metric Tensor to 0 KratosMultiphysics.VariableUtils( ).SetNonHistoricalVariableToZero( KratosMultiphysics.MeshingApplication.METRIC_TENSOR_2D, model_coarse.GetModelPart(model_part_name).Nodes) # calculate NODAL_H find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess( model_coarse.GetModelPart(model_part_name)) find_nodal_h.Execute() local_gradient = KratosMeshing.ComputeHessianSolMetricProcess( model_coarse.GetModelPart(model_part_name), KratosFluid.AVERAGE_VELOCITY_X, metric_param) local_gradient.Execute() local_gradient = KratosMeshing.ComputeHessianSolMetricProcess( model_coarse.GetModelPart(model_part_name), KratosFluid.AVERAGE_VELOCITY_Y, metric_param) local_gradient.Execute() elif (problem_type == "stationary"): if current_level > 0: coefficient_interp_error = metric_param[ "hessian_strategy_parameters"][ "coefficient_interpolation_error"].GetDouble() metric_param["hessian_strategy_parameters"].RemoveValue( "coefficient_interpolation_error") interp_error = original_interp_error * ( coefficient_interp_error)**(-current_level) metric_param["hessian_strategy_parameters"][ "interpolation_error"].SetDouble(interp_error) model_part_name = parameters_coarse["solver_settings"][ "model_part_name"].GetString() # Setting Metric Tensor to 0 KratosMultiphysics.VariableUtils( ).SetNonHistoricalVariableToZero( KratosMultiphysics.MeshingApplication.METRIC_TENSOR_2D, model_coarse.GetModelPart(model_part_name).Nodes) # calculate NODAL_H find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess( model_coarse.GetModelPart(model_part_name)) find_nodal_h.Execute() local_gradient = KratosMeshing.ComputeHessianSolMetricProcess( model_coarse.GetModelPart(model_part_name), KratosMultiphysics.TEMPERATURE, metric_param) local_gradient.Execute() # create the remeshing process MmgProcess = KratosMeshing.MmgProcess2D( model_coarse.GetModelPart(model_part_name), remesh_param) MmgProcess.Execute() # reset variables if needed model_coarse.GetModelPart(model_part_name).ProcessInfo.SetValue( KratosMultiphysics.TIME, 0.0) model_coarse.GetModelPart(model_part_name).ProcessInfo.SetValue( KratosMultiphysics.STEP, 0) """ the refinement process empties the coarse model part object and fill it with the refined model part the solution on the refined grid is obtained from the interpolation of the coarse solution there are not other operations, therefore to build the new model we just need to take the updated coarse model """ current_model_refined = model_coarse current_parameters_refined = parameters_coarse return current_model_refined, current_parameters_refined
"enforce_current" : false, "anisotropy_remeshing" : true, "anisotropy_parameters":{ "reference_variable_name" : "DISTANCE", "hmin_over_hmax_anisotropic_ratio" : 0.15, "boundary_layer_max_distance" : 1.0, "interpolation" : "Linear" } }""" ) local_gradient = MeshingApplication.ComputeHessianSolMetricProcess2D(main_model_part, KratosMultiphysics.DISTANCE, metric_param) local_gradient.Execute() # We create the remeshing process remesh_param = KratosMultiphysics.Parameters("""{ }""") MmgProcess = MeshingApplication.MmgProcess2D(main_model_part, remesh_param) MmgProcess.Execute() # Finally we export to GiD from gid_output_process import GiDOutputProcess gid_output = GiDOutputProcess(main_model_part, "gid_output", KratosMultiphysics.Parameters(""" { "result_file_configuration" : { "gidpost_flags": { "GiDPostMode": "GiD_PostBinary", "WriteDeformedMeshFlag": "WriteUndeformed", "WriteConditionsFlag": "WriteConditions", "MultiFileFlag": "SingleFile" },