KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
distance_smoothing_process.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Mohammad R. Hashemi
11 //
12 
13 #ifndef KRATOS_DISTANCE_SMOOTHING_H
14 #define KRATOS_DISTANCE_SMOOTHING_H
15 
16 // System includes
17 #include <string>
18 #include <iostream>
19 
20 // Project includes
21 #include "processes/process.h"
25 #include "includes/define.h"
26 #include "includes/checks.h"
28 #include "containers/model.h"
34 #include "spaces/ublas_space.h"
40 
41 // Application includes
44 
45 namespace Kratos
46 {
49 
52 
56 
60 
64 
68 
73 template< unsigned int TDim, class TSparseSpace, class TDenseSpace, class TLinearSolver>
74 class KRATOS_API(FLUID_DYNAMICS_APPLICATION) DistanceSmoothingProcess : public Process
75 {
76 public:
79 
82 
84  typedef typename SchemeType::Pointer SchemePointerType;
87 
91 
93  // Set AllConditionsAsBoundary = false if distance gradient should be imposed only on the conditiones priorly marked as CONTACT.
95  ModelPart& rModelPart,
96  typename TLinearSolver::Pointer pLinearSolver,
97  const bool AllConditionsAsBoundary = true)
98  : Process(),
99  mrModelPart(rModelPart),
100  mrModel(rModelPart.GetModel()),
101  mAuxModelPartName("smoothing_model_part"),
102  mAllConditionsAsBoundary(AllConditionsAsBoundary),
103  mAuxModelPartIsInitialized(false)
104  {
105  // Generate an auxilary model part and populate it by elements of type DistanceSmoothingElement
106  CreateAuxModelPart();
107 
108  auto p_builder_solver = Kratos::make_shared<ResidualBasedBlockBuilderAndSolver<TSparseSpace, TDenseSpace, TLinearSolver> >(pLinearSolver);
109 
110  CreateSolutionStrategy(p_builder_solver);
111  }
112 
115  ModelPart& rModelPart,
116  typename TLinearSolver::Pointer pLinearSolver,
117  BuilderSolverPointerType pBuilderAndSolver,
118  const bool AllConditionsAsBoundary = true)
119  : Process(),
120  mrModelPart(rModelPart),
121  mrModel(rModelPart.GetModel()),
122  mAuxModelPartName("smoothing_model_part"),
123  mAllConditionsAsBoundary(AllConditionsAsBoundary),
124  mAuxModelPartIsInitialized(false)
125  {
126  // Generate an auxilary model part and populate it by elements of type DistanceSmoothingElement
127  CreateAuxModelPart();
128 
129  CreateSolutionStrategy(pBuilderAndSolver);
130  }
131 
134  ModelPart& rModelPart,
137  rModelPart,
138  LinearSolverFactory<TSparseSpace, TDenseSpace>().Create(Parameters["linear_solver_settings"]),
139  Parameters["all_conditions_as_boundaries"].GetBool()
140  ){}
141 
144  Model& rModel,
147  rModel.GetModelPart(Parameters["model_part_name"].GetString()),
148  Parameters
149  ){}
150 
153  {
154  Clear();
155  }
156 
160 
161  void Execute() override
162  {
163  KRATOS_TRY;
164 
165  if(mAuxModelPartIsInitialized == false){
166  CreateAuxModelPart();
167  }
168 
169  auto& r_smoothing_model_part = mrModel.GetModelPart( mAuxModelPartName );
170 
171  block_for_each(r_smoothing_model_part.Nodes(), [&](Node& rNode){
172  rNode.Free(DISTANCE);
173  const double distance = rNode.FastGetSolutionStepValue(DISTANCE);
174  rNode.FastGetSolutionStepValue(DISTANCE, 1) = distance;
175  });
176 
177  mp_solving_strategy->Solve();
178 
179  block_for_each(r_smoothing_model_part.Nodes(), [&](Node& rNode){
180  rNode.SetValue( DISTANCE, rNode.FastGetSolutionStepValue(DISTANCE)
181  - rNode.FastGetSolutionStepValue(DISTANCE, 1) ); // Corrected distance difference
182  });
183 
184  auto& r_data_comm = r_smoothing_model_part.GetCommunicator().GetDataCommunicator();
186 
187  const unsigned int num_nodes = r_smoothing_model_part.NumberOfNodes();
188 
189  for (unsigned int i_node = 0; i_node < num_nodes; ++i_node){
190  auto it_node = r_smoothing_model_part.NodesBegin() + i_node;
191  auto& global_pointer_list = it_node->GetValue(NEIGHBOUR_NODES);
192 
193  for (unsigned int j = 0; j< global_pointer_list.size(); ++j)
194  {
195  auto& global_pointer = global_pointer_list(j);
196  gp_list.push_back(global_pointer);
197  }
198  }
199 
200  GlobalPointerCommunicator< Node > pointer_comm(r_data_comm, gp_list);
201 
202  auto combined_proxy = pointer_comm.Apply(
203  [&](GlobalPointer<Node> &global_pointer) -> std::pair<double, array_1d<double,3>> {
204  return std::make_pair(
205  global_pointer->GetValue(DISTANCE),
206  global_pointer->Coordinates());
207  });
208 
209  auto contact_proxy = pointer_comm.Apply(
210  [&](GlobalPointer<Node >& global_pointer) -> bool
211  {
212  return global_pointer->Is(CONTACT);
213  }
214  );
215 
216  auto &r_communicator = r_smoothing_model_part.GetCommunicator();
217  r_communicator.GetDataCommunicator().Barrier();
218 
219  block_for_each(r_smoothing_model_part.Nodes(), [&](Node& rNode){
220  const array_1d<double,3>& x_i = rNode.Coordinates();
221 
222  double weight = 0.0;
223  double dist_diff_avg = 0.0;
224 
225  GlobalPointersVector< Node >& global_pointer_list = rNode.GetValue(NEIGHBOUR_NODES);
226  array_1d<double,3> dx;
227 
228  for (unsigned int j = 0; j< global_pointer_list.size(); ++j)
229  {
230  auto& global_pointer = global_pointer_list(j);
231 
232  if (contact_proxy.Get(global_pointer) == rNode.Is(CONTACT)){
233 
234  auto result = combined_proxy.Get(global_pointer);
235  const array_1d<double,3>& x_j = std::get<1>(result);
236 
237  dx = x_i - x_j;
238 
239  const double distance_ij = norm_2(dx);
240 
241 #ifdef KRATOS_DEBUG
242  KRATOS_WARNING_IF("DistanceSmoothingProcess", distance_ij < 1.0e-12)
243  << "WARNING: Neighbouring nodes are almost coinciding" << std::endl;
244 #endif
245 
246  if (distance_ij > 1.0e-12){
247  weight += 1.0/distance_ij;
248  const double distance_j = std::get<0>(result);
249  dist_diff_avg += distance_j/distance_ij;
250  }
251  }
252  }
253 
254 #ifdef KRATOS_DEBUG
255  KRATOS_WARNING_IF("DistanceSmoothingProcess", weight < 1.0e-12)
256  << "WARNING: Correction is not done due to a zero weight" <<std::endl;
257 #endif
258 
259  if (weight > 1.0e-12)
260  rNode.FastGetSolutionStepValue(DISTANCE) -= dist_diff_avg/weight;
261  });
262 
263  r_communicator.SynchronizeCurrentDataToMin(DISTANCE);
264 
265  KRATOS_CATCH("");
266  }
267 
268  void Clear() override
269  {
270  if(mrModel.HasModelPart( mAuxModelPartName ))
271  mrModel.DeleteModelPart( mAuxModelPartName );
272  mAuxModelPartIsInitialized = false;
273 
274  mp_solving_strategy->Clear();
275  }
276 
280 
284 
288 
292 
294  std::string Info() const override
295  {
296  std::stringstream buffer;
297  buffer << "Distance Smoothing Process" ;
298  return buffer.str();
299  }
300 
302  void PrintInfo(std::ostream& rOStream) const override {rOStream << "Distance Smoothing Process";}
303 
305  void PrintData(std::ostream& rOStream) const override {}
306 
310 
312 
313 private:
316 
320 
321  ModelPart& mrModelPart;
322  Model& mrModel;
323  std::string mAuxModelPartName;
324  bool mAllConditionsAsBoundary;
325  bool mAuxModelPartIsInitialized;
326 
327  typename SolvingStrategyType::UniquePointer mp_solving_strategy;
328 
332 
336 
342  void CreateSolutionStrategy(BuilderSolverPointerType pBuilderAndSolver)
343  {
344  // Generate a linear solver strategy
345  auto p_scheme = Kratos::make_shared< ResidualBasedIncrementalUpdateStaticScheme< TSparseSpace,TDenseSpace > >();
346 
347  auto& r_smoothing_model_part = mrModel.GetModelPart( mAuxModelPartName );
348 
349  const bool calculate_reactions = false;
350  const bool reform_dof_at_each_iteration = false;
351  const bool calculate_norm_dx_flag = false;
352 
353  mp_solving_strategy = Kratos::make_unique<ResidualBasedLinearStrategy<TSparseSpace, TDenseSpace, TLinearSolver> >(
354  r_smoothing_model_part,
355  p_scheme,
356  pBuilderAndSolver,
357  calculate_reactions,
358  reform_dof_at_each_iteration,
359  calculate_norm_dx_flag);
360 
361  mp_solving_strategy->Initialize();
362  mp_solving_strategy->SetEchoLevel(0);
363  mp_solving_strategy->Check();
364  }
365 
370  void CreateAuxModelPart()
371  {
372  KRATOS_TRY
373 
374  if(mrModel.HasModelPart( mAuxModelPartName ))
375  mrModel.DeleteModelPart( mAuxModelPartName );
376 
377  // Ensure that the nodes have distance as a DOF
378  VariableUtils().AddDof<Variable<double> >(DISTANCE, mrModelPart);
379 
380  // Generate AuxModelPart
381  auto& r_smoothing_model_part = mrModel.CreateModelPart( mAuxModelPartName );
382 
383  auto p_smoothing_element = Kratos::make_intrusive<DistanceSmoothingElement<TDim>>();
384 
385  r_smoothing_model_part.GetNodalSolutionStepVariablesList() = mrModelPart.GetNodalSolutionStepVariablesList();
386 
387  ConnectivityPreserveModeler modeler;
388  modeler.GenerateModelPart(mrModelPart, r_smoothing_model_part, *p_smoothing_element);
389 
390  const unsigned int buffer_size = r_smoothing_model_part.GetBufferSize();
391  KRATOS_ERROR_IF(buffer_size < 2) << "Buffer size should be at least 2" << std::endl;
392 
393  FindGlobalNodalNeighboursProcess nodal_neighbour_process_new(r_smoothing_model_part);
394  nodal_neighbour_process_new.Execute();
395 
396  GenericFindElementalNeighboursProcess neighbour_elements_finder_new(r_smoothing_model_part);
397  neighbour_elements_finder_new.ExecuteInitialize();
398 
399  if (mAllConditionsAsBoundary)
400  {
401  VariableUtils().SetFlag(CONTACT, true, r_smoothing_model_part.Conditions());
402  }
403 
404  mAuxModelPartIsInitialized = true;
405 
406  KRATOS_CATCH("")
407  }
408 
412 
413 
417 
418 
422 
424  DistanceSmoothingProcess() = delete;
425 
427  DistanceSmoothingProcess& operator=(DistanceSmoothingProcess const& rOther) = delete;
428 
430  DistanceSmoothingProcess(DistanceSmoothingProcess const& rOther) = delete;
431 
433 
434 }; // Class DistanceSmoothingProcess
435 
439 
443 
445 
447 
448 }; // namespace Kratos.
449 
450 #endif // KRATOS_DISTANCE_SMOOTHING__H
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
Current class provides an implementation for the base builder and solving operations.
Definition: builder_and_solver.h:64
Definition: distance_smoothing_process.h:75
ImplicitSolvingStrategy< TSparseSpace, TDenseSpace, TLinearSolver > SolvingStrategyType
Definition: distance_smoothing_process.h:86
DistanceSmoothingProcess(ModelPart &rModelPart, typename TLinearSolver::Pointer pLinearSolver, BuilderSolverPointerType pBuilderAndSolver, const bool AllConditionsAsBoundary=true)
Constructor.
Definition: distance_smoothing_process.h:114
DistanceSmoothingProcess(Model &rModel, Parameters Parameters)
Constructor with Kratos model.
Definition: distance_smoothing_process.h:143
DistanceSmoothingProcess(ModelPart &rModelPart, Parameters Parameters)
Constructor with Kratos parameters.
Definition: distance_smoothing_process.h:133
~DistanceSmoothingProcess() override
Destructor.
Definition: distance_smoothing_process.h:152
BuilderAndSolver< TSparseSpace, TDenseSpace, TLinearSolver >::Pointer BuilderSolverPointerType
Definition: distance_smoothing_process.h:85
SchemeType::Pointer SchemePointerType
Definition: distance_smoothing_process.h:84
std::string Info() const override
Turn back information as a string.
Definition: distance_smoothing_process.h:294
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: distance_smoothing_process.h:305
Scheme< TSparseSpace, TDenseSpace > SchemeType
Definition: distance_smoothing_process.h:83
KRATOS_CLASS_POINTER_DEFINITION(DistanceSmoothingProcess)
Pointer definition of DistanceSmoothingProcess.
void Execute() override
Execute method is used to execute the Process algorithms.
Definition: distance_smoothing_process.h:161
DistanceSmoothingProcess(ModelPart &rModelPart, typename TLinearSolver::Pointer pLinearSolver, const bool AllConditionsAsBoundary=true)
Constructor.
Definition: distance_smoothing_process.h:94
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: distance_smoothing_process.h:302
void Clear() override
This method clears the assignation of the conditions.
Definition: distance_smoothing_process.h:268
A template class for handling communication related to global pointers.
Definition: pointer_communicator.h:178
ResultsProxy< TPointerDataType, TFunctorType > Apply(TFunctorType &&UserFunctor)
Applies a user-provided function to the global pointers and return a proxy to the results.
Definition: pointer_communicator.h:266
This class is a wrapper for a pointer to a data that is located in a different rank.
Definition: global_pointer.h:44
This class is a vector which stores global pointers.
Definition: global_pointers_vector.h:61
void push_back(TPointerType x)
Definition: global_pointers_vector.h:322
Implicit solving strategy base class This is the base class from which we will derive all the implici...
Definition: implicit_solving_strategy.h:61
Here we add the functions needed for the registration of linear solvers.
Definition: linear_solver_factory.h:62
This class aims to manage different model parts across multi-physics simulations.
Definition: model.h:60
ModelPart & GetModelPart(const std::string &rFullModelPartName)
This method returns a model part given a certain name.
Definition: model.cpp:107
ModelPart & CreateModelPart(const std::string &ModelPartName, IndexType NewBufferSize=1)
This method creates a new model part contained in the current Model with a given name and buffer size...
Definition: model.cpp:37
void DeleteModelPart(const std::string &ModelPartName)
This method deletes a modelpart with a given name.
Definition: model.cpp:64
bool HasModelPart(const std::string &rFullModelPartName) const
This method checks if a certain a model part exists given a certain name.
Definition: model.cpp:178
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
virtual int Check() const
run input validation
Definition: model_part.cpp:2204
VariablesList & GetNodalSolutionStepVariablesList()
Definition: model_part.h:549
This class defines the node.
Definition: node.h:65
TVariableType::Type & FastGetSolutionStepValue(const TVariableType &rThisVariable)
Definition: node.h:435
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
The base class for all processes in Kratos.
Definition: process.h:49
This class provides the implementation of the basic tasks that are needed by the solution strategy.
Definition: scheme.h:56
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
#define KRATOS_WARNING_IF(label, conditional)
Definition: logger.h:266
Modeler::Pointer Create(const std::string &ModelerName, Model &rModel, const Parameters ModelParameters)
Checks if the modeler is registered.
Definition: modeler_factory.cpp:30
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void block_for_each(TIterator itBegin, TIterator itEnd, TFunction &&rFunction)
Execute a functor on all items of a range in parallel.
Definition: parallel_utilities.h:299
int j
Definition: quadrature.py:648