def PerformRemeshingIfNecessary(self): debug_metric = False if debug_metric: params = KratosMultiphysics.Parameters("""{}""") KratosFemDem.ComputeNormalizedFreeEnergyOnNodesProcess(self.FEM_Solution.main_model_part, self.FEM_Solution.ProjectParameters["AMR_data"]["hessian_variable_parameters"]).Execute() MeshingApplication.ComputeHessianSolMetricProcess(self.FEM_Solution.main_model_part, KratosFemDem.EQUIVALENT_NODAL_STRESS, params).Execute() if self.DoRemeshing: is_remeshing = self.CheckIfHasRemeshed() if is_remeshing: if self.echo_level > 0: KratosMultiphysics.Logger.PrintInfo("FEM-DEM:: ComputeNormalizedFreeEnergyOnNodesProcess") # Extrapolate the free energy as a remeshing criterion parameters = self.FEM_Solution.ProjectParameters["AMR_data"]["hessian_variable_parameters"] KratosFemDem.ComputeNormalizedFreeEnergyOnNodesProcess(self.FEM_Solution.main_model_part, parameters).Execute() # we eliminate the nodal DEM forces self.RemoveDummyNodalForces() # Perform remeshing self.RemeshingProcessMMG.ExecuteInitializeSolutionStep() if is_remeshing: if self.echo_level > 0: KratosMultiphysics.Logger.PrintInfo("FEM-DEM:: InitializeSolutionAfterRemeshing") self.InitializeSolutionAfterRemeshing() neighbour_elemental_finder = KratosMultiphysics.FindElementalNeighboursProcess(self.FEM_Solution.main_model_part, 2, 5) neighbour_elemental_finder.ClearNeighbours() neighbour_elemental_finder.Execute()
def test_mpi_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) # We add the variables needed main_model_part.AddNodalSolutionStepVariable(KratosMultiphysics.DISTANCE) # We import the model main_model_part file_path = GetFilePath("/mmg_lagrangian_test/remesh_rectangle") ReadModelPart(file_path, 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) ## Read reference file_name = "mmg_lagrangian_test/rectangle_distance_values.json" distance_values_file_name = GetFilePath(file_name) with open(distance_values_file_name, 'r') as f: distance_values = json.load(f) for node in main_model_part.Nodes: distance_value = distance_values[str(node.Id)] node.SetSolutionStepValue(KratosMultiphysics.DISTANCE, distance_value) metric_parameters = 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, "enforce_current" : false, "anisotropy_remeshing" : false, "enforce_anisotropy_relative_variable" : false } """) metric_process = MeshingApplication.ComputeHessianSolMetricProcess(main_model_part, KratosMultiphysics.DISTANCE, metric_parameters) metric_process.Execute() ## Read reference file_name = "mmg_lagrangian_test/rectangle_pre_metric_result.json" reference_file_name = GetFilePath(file_name) with open(reference_file_name, 'r') as f: reference_values = json.load(f) for node in main_model_part.Nodes: metric_tensor = node.GetValue(MeshingApplication.METRIC_TENSOR_2D) ref_gradient = reference_values[str(node.Id)] for tensor_i, tensor_i_ref in zip(metric_tensor, ref_gradient): self.assertAlmostEqual(tensor_i, tensor_i_ref)
def _CreateMetricsProcess(self): self.metric_processes = [] if self.strategy == "LevelSet": level_set_parameters = KratosMultiphysics.Parameters("""{}""") level_set_parameters.AddValue("minimal_size",self.settings["minimal_size"]) level_set_parameters.AddValue("maximal_size",self.settings["maximal_size"]) level_set_parameters.AddValue("sizing_parameters",self.settings["sizing_parameters"]) level_set_parameters.AddValue("enforce_current",self.settings["enforce_current"]) level_set_parameters.AddValue("anisotropy_remeshing",self.settings["anisotropy_remeshing"]) level_set_parameters.AddValue("anisotropy_parameters",self.settings["anisotropy_parameters"]) level_set_parameters["anisotropy_parameters"].RemoveValue("boundary_layer_min_size_ratio") if self.domain_size == 2: self.metric_processes.append(MeshingApplication.ComputeLevelSetSolMetricProcess2D(self.main_model_part, self.gradient_variable, level_set_parameters)) else: self.metric_processes.append(MeshingApplication.ComputeLevelSetSolMetricProcess3D(self.main_model_part, self.gradient_variable, level_set_parameters)) elif self.strategy == "Hessian": hessian_parameters = KratosMultiphysics.Parameters("""{}""") hessian_parameters.AddValue("minimal_size",self.settings["minimal_size"]) hessian_parameters.AddValue("maximal_size",self.settings["maximal_size"]) hessian_parameters.AddValue("enforce_current",self.settings["enforce_current"]) hessian_parameters.AddValue("hessian_strategy_parameters",self.settings["hessian_strategy_parameters"]) hessian_parameters["hessian_strategy_parameters"].RemoveValue("metric_variable") hessian_parameters.AddValue("anisotropy_remeshing",self.settings["anisotropy_remeshing"]) hessian_parameters.AddValue("enforce_anisotropy_relative_variable",self.settings["enforce_anisotropy_relative_variable"]) hessian_parameters.AddValue("enforced_anisotropy_parameters",self.settings["anisotropy_parameters"]) hessian_parameters["enforced_anisotropy_parameters"].RemoveValue("boundary_layer_min_size_ratio") for current_metric_variable in self.metric_variable: self.metric_processes.append(MeshingApplication.ComputeHessianSolMetricProcess(self.main_model_part, current_metric_variable, hessian_parameters)) elif self.strategy == "superconvergent_patch_recovery": if not structural_dependencies: raise Exception("You need to compile the StructuralMechanicsApplication in order to use this criteria") # We compute the error error_compute_parameters = KratosMultiphysics.Parameters("""{}""") error_compute_parameters.AddValue("stress_vector_variable", self.settings["compute_error_extra_parameters"]["stress_vector_variable"]) error_compute_parameters.AddValue("echo_level", self.settings["echo_level"]) if self.domain_size == 2: self.error_compute = StructuralMechanicsApplication.SPRErrorProcess2D(self.main_model_part, error_compute_parameters) else: self.error_compute = StructuralMechanicsApplication.SPRErrorProcess3D(self.main_model_part, error_compute_parameters) # Now we compute the metric error_metric_parameters = KratosMultiphysics.Parameters("""{}""") error_metric_parameters.AddValue("minimal_size",self.settings["minimal_size"]) error_metric_parameters.AddValue("maximal_size",self.settings["maximal_size"]) error_metric_parameters.AddValue("target_error",self.settings["error_strategy_parameters"]["error_metric_parameters"]["interpolation_error"]) error_metric_parameters.AddValue("set_target_number_of_elements", self.settings["error_strategy_parameters"]["set_target_number_of_elements"]) error_metric_parameters.AddValue("target_number_of_elements", self.settings["error_strategy_parameters"]["target_number_of_elements"]) error_metric_parameters.AddValue("perform_nodal_h_averaging", self.settings["error_strategy_parameters"]["perform_nodal_h_averaging"]) error_metric_parameters.AddValue("echo_level", self.settings["echo_level"]) if self.domain_size == 2: self.metric_process = MeshingApplication.MetricErrorProcess2D(self.main_model_part, error_metric_parameters) else: self.metric_process = MeshingApplication.MetricErrorProcess3D(self.main_model_part, error_metric_parameters)
def __ComputeHessianMetric(self): find_nodal_h = KratosMultiphysics.FindNodalHNonHistoricalProcess( self.main_model_part) find_nodal_h.Execute() metric_x = KratosMeshing.ComputeHessianSolMetricProcess( self.main_model_part, KratosMultiphysics.VELOCITY_X, self.metric_parameters) metric_x.Execute() metric_y = KratosMeshing.ComputeHessianSolMetricProcess( self.main_model_part, KratosMultiphysics.VELOCITY_Y, self.metric_parameters) metric_y.Execute() if (self.domain_size == 3): metric_z = KratosMeshing.ComputeHessianSolMetricProcess( self.main_model_part, KratosMultiphysics.VELOCITY_Z, self.metric_parameters) metric_z.Execute()
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 PerformRemeshingIfNecessary(self): debug_metric = False if debug_metric: params = KratosMultiphysics.Parameters("""{}""") KratosFemDem.ComputeNormalizedFreeEnergyOnNodesProcess( self.FEM_Solution.main_model_part, self.FEM_Solution.ProjectParameters["AMR_data"] ["hessian_variable_parameters"]).Execute() MeshingApplication.ComputeHessianSolMetricProcess( self.FEM_Solution.main_model_part, KratosFemDem.EQUIVALENT_NODAL_STRESS, params).Execute() if self.DoRemeshing: is_remeshing = self.CheckIfHasRemeshed() if is_remeshing: if self.echo_level > 0: KratosMultiphysics.Logger.PrintInfo( "FEM-DEM:: ComputeNormalizedFreeEnergyOnNodesProcess") # Extrapolate the VonMises normalized stress to nodes (remeshing) parameters = self.FEM_Solution.ProjectParameters["AMR_data"][ "hessian_variable_parameters"] KratosFemDem.ComputeNormalizedFreeEnergyOnNodesProcess( self.FEM_Solution.main_model_part, parameters).Execute() # we eliminate the nodal DEM forces self.RemoveDummyNodalForces() # Perform remeshing self.RemeshingProcessMMG.ExecuteInitializeSolutionStep() if is_remeshing: if self.echo_level > 0: KratosMultiphysics.Logger.PrintInfo( "FEM-DEM:: InitializeSolutionAfterRemeshing") self.RefineMappedVariables() self.InitializeSolutionAfterRemeshing() self.nodal_neighbour_finder = KratosMultiphysics.FindNodalNeighboursProcess( self.FEM_Solution.main_model_part, 4, 5) self.nodal_neighbour_finder.Execute() # We assign the flag to recompute neighbours inside the 3D elements utils = KratosMultiphysics.VariableUtils() utils.SetNonHistoricalVariable( KratosFemDem.RECOMPUTE_NEIGHBOURS, True, self.FEM_Solution.main_model_part.Elements)
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 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 _CreateMetricsProcess(self): """ This method is responsible to create the metrics of the process Keyword arguments: self -- It signifies an instance of a class. """ self.metric_processes = [] if self.strategy == "LevelSet": level_set_parameters = KratosMultiphysics.Parameters("""{}""") level_set_parameters.AddValue("minimal_size",self.settings["minimal_size"]) level_set_parameters.AddValue("maximal_size",self.settings["maximal_size"]) level_set_parameters.AddValue("sizing_parameters",self.settings["sizing_parameters"]) level_set_parameters.AddValue("enforce_current",self.settings["enforce_current"]) level_set_parameters.AddValue("anisotropy_remeshing",self.settings["anisotropy_remeshing"]) level_set_parameters.AddValue("anisotropy_parameters",self.settings["anisotropy_parameters"]) level_set_parameters["anisotropy_parameters"].RemoveValue("boundary_layer_min_size_ratio") if self.domain_size == 2: self.metric_processes.append(MeshingApplication.ComputeLevelSetSolMetricProcess2D(self.main_model_part, self.gradient_variable, level_set_parameters)) else: self.metric_processes.append(MeshingApplication.ComputeLevelSetSolMetricProcess3D(self.main_model_part, self.gradient_variable, level_set_parameters)) elif self.strategy == "Hessian": hessian_parameters = KratosMultiphysics.Parameters("""{}""") hessian_parameters.AddValue("minimal_size",self.settings["minimal_size"]) hessian_parameters.AddValue("maximal_size",self.settings["maximal_size"]) hessian_parameters.AddValue("enforce_current",self.settings["enforce_current"]) hessian_parameters.AddValue("hessian_strategy_parameters",self.settings["hessian_strategy_parameters"]) hessian_parameters["hessian_strategy_parameters"].RemoveValue("metric_variable") hessian_parameters["hessian_strategy_parameters"].RemoveValue("non_historical_metric_variable") hessian_parameters["hessian_strategy_parameters"].AddEmptyValue("non_historical_metric_variable") hessian_parameters["hessian_strategy_parameters"].RemoveValue("normalization_factor") hessian_parameters["hessian_strategy_parameters"].AddEmptyValue("normalization_factor") hessian_parameters["hessian_strategy_parameters"].RemoveValue("normalization_alpha") hessian_parameters["hessian_strategy_parameters"].AddEmptyValue("normalization_alpha") hessian_parameters["hessian_strategy_parameters"].RemoveValue("normalization_method") hessian_parameters["hessian_strategy_parameters"].AddEmptyValue("normalization_method") hessian_parameters.AddValue("anisotropy_remeshing",self.settings["anisotropy_remeshing"]) hessian_parameters.AddValue("enforce_anisotropy_relative_variable",self.settings["enforce_anisotropy_relative_variable"]) hessian_parameters.AddValue("enforced_anisotropy_parameters",self.settings["anisotropy_parameters"]) hessian_parameters["enforced_anisotropy_parameters"].RemoveValue("boundary_layer_min_size_ratio") for current_metric_variable, non_historical_metric_variable, normalization_factor, normalization_alpha, normalization_method in zip(self.metric_variables, self.non_historical_metric_variable, self.normalization_factor, self.normalization_alpha, self.normalization_method): hessian_parameters["hessian_strategy_parameters"]["non_historical_metric_variable"].SetBool(non_historical_metric_variable) hessian_parameters["hessian_strategy_parameters"]["normalization_factor"].SetDouble(normalization_factor) hessian_parameters["hessian_strategy_parameters"]["normalization_alpha"].SetDouble(normalization_alpha) hessian_parameters["hessian_strategy_parameters"]["normalization_method"].SetString(normalization_method) self.metric_processes.append(MeshingApplication.ComputeHessianSolMetricProcess(self.main_model_part, current_metric_variable, hessian_parameters)) elif self.strategy == "superconvergent_patch_recovery" or self.strategy == "SPR": # Generate SPR process self.error_compute = self._GenerateErrorProcess() # Now we compute the metric error_metric_parameters = KratosMultiphysics.Parameters("""{"error_strategy_parameters":{}}""") error_metric_parameters.AddValue("minimal_size",self.settings["minimal_size"]) error_metric_parameters.AddValue("maximal_size",self.settings["maximal_size"]) error_metric_parameters["error_strategy_parameters"].AddValue("target_error",self.settings["error_strategy_parameters"]["error_metric_parameters"]["interpolation_error"]) error_metric_parameters["error_strategy_parameters"].AddValue("set_target_number_of_elements", self.settings["error_strategy_parameters"]["set_target_number_of_elements"]) error_metric_parameters["error_strategy_parameters"].AddValue("target_number_of_elements", self.settings["error_strategy_parameters"]["target_number_of_elements"]) error_metric_parameters["error_strategy_parameters"].AddValue("perform_nodal_h_averaging", self.settings["error_strategy_parameters"]["perform_nodal_h_averaging"]) error_metric_parameters.AddValue("echo_level", self.settings["echo_level"]) if self.domain_size == 2: self.metric_process = MeshingApplication.MetricErrorProcess2D(self.main_model_part, error_metric_parameters) else: self.metric_process = MeshingApplication.MetricErrorProcess3D(self.main_model_part, error_metric_parameters)
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