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.
residualbased_DEM_coupled_newton_raphson_strategy.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: Alejandro Cornejo
11 //
12 
13 #if !defined(KRATOS_RESIDUALBASED_DEM_COUPLED_NEWTON_RAPHSON_STRATEGY)
14 #define KRATOS_RESIDUALBASED_DEM_COUPLED_NEWTON_RAPHSON_STRATEGY
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "includes/define.h"
26 
29 
30 //default builder and solver
32 
33 namespace Kratos
34 {
35 
38 
42 
44 
47 
51 
55 
63 template <class TSparseSpace,
64  class TDenseSpace, // = DenseSpace<double>,
65  class TLinearSolver //= LinearSolver<TSparseSpace,TDenseSpace>
66  >
68  : public ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>
69 {
70  public:
74 
76 
77  // Counted pointer of ClassName
79 
83  typedef typename BaseType::TDataType TDataType;
84  typedef TSparseSpace SparseSpaceType;
93 
97 
102  {
103  }
104 
117  ModelPart& rModelPart,
118  ExplicitSolverStrategyType::Pointer pDEMStrategy,
119  typename TSchemeType::Pointer pScheme,
120  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
121  typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver,
122  int MaxIterations = 30,
123  bool CalculateReactions = false,
124  bool ReformDofSetAtEachStep = false,
125  bool MoveMeshFlag = false)
126  : BaseType(rModelPart,
127  pScheme,
128  pNewConvergenceCriteria,
129  pNewBuilderAndSolver,
130  MaxIterations,
131  CalculateReactions,
132  ReformDofSetAtEachStep,
133  MoveMeshFlag),
134  mpDEMStrategy(pDEMStrategy)
135  {
136  }
137 
138 
144  {
145  auto p_builder_and_solver = this->GetBuilderAndSolver();
146  if (p_builder_and_solver != nullptr) {
147  p_builder_and_solver->Clear();
148  }
149 
150  this->mpA.reset();
151  this->mpDx.reset();
152  this->mpb.reset();
153 
154  this->Clear();
155  }
156 
157  //*********************************************************************************
164  void Initialize() override
165  {
166  KRATOS_TRY;
168  mpDEMStrategy->Initialize();
169  KRATOS_CATCH("");
170  }
171 
176  void InitializeSolutionStep() override
177  {
178  KRATOS_TRY;
180  mpDEMStrategy->InitializeSolutionStep();
183  KRATOS_CATCH("");
184  }
185 
190  void FinalizeSolutionStep() override
191  {
192  KRATOS_TRY;
194  mpDEMStrategy->FinalizeSolutionStep();
195  KRATOS_CATCH("");
196  }
197 
201  bool SolveSolutionStep() override
202  {
203  // We compute the contact forces with the DEM
204  auto update_dem_kinematics_process = UpdateDemKinematicsProcess(this->GetModelPart());
205  update_dem_kinematics_process.Execute();
206  mpDEMStrategy->SolveSolutionStep();
207  auto transfer_process = TransferNodalForcesToFem(this->GetModelPart(), false);
208  transfer_process.Execute();
209  update_dem_kinematics_process.Execute();
210 
211  // Pointers needed in the solution
212  ModelPart& r_model_part = BaseType::GetModelPart();
213  typename TSchemeType::Pointer p_scheme = this->GetScheme();
214  typename TBuilderAndSolverType::Pointer p_builder_and_solver = this->GetBuilderAndSolver();
215  auto& r_dof_set = p_builder_and_solver->GetDofSet();
216 
217  TSystemMatrixType& rA = *this->mpA;
218  TSystemVectorType& rDx = *this->mpDx;
219  TSystemVectorType& rb = *this->mpb;
220 
221  //initializing the parameters of the Newton-Raphson cycle
222  unsigned int iteration_number = 1;
223  r_model_part.GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
224  bool residual_is_updated = false;
225  p_scheme->InitializeNonLinIteration(r_model_part, rA, rDx, rb);
226  this->mpConvergenceCriteria->InitializeNonLinearIteration(r_model_part, r_dof_set, rA, rDx, rb);
227  bool is_converged = this->mpConvergenceCriteria->PreCriteria(r_model_part, r_dof_set, rA, rDx, rb);
228 
229  // Function to perform the building and the solving phase.
231  TSparseSpace::SetToZero(rA);
232  TSparseSpace::SetToZero(rDx);
233  TSparseSpace::SetToZero(rb);
234 
236  p_builder_and_solver->BuildAndSolveLinearizedOnPreviousIteration(p_scheme, r_model_part, rA, rDx, rb, BaseType::MoveMeshFlag());
237  } else {
238  p_builder_and_solver->BuildAndSolve(p_scheme, r_model_part, rA, rDx, rb);
239  }
240  } else {
241  TSparseSpace::SetToZero(rDx); // Dx = 0.00;
242  TSparseSpace::SetToZero(rb);
243 
244  p_builder_and_solver->BuildRHSAndSolve(p_scheme, r_model_part, rA, rDx, rb);
245  }
246 
247  // Debugging info
248  this->EchoInfo(iteration_number);
249 
250  // Updating the results stored in the database
251  this->UpdateDatabase(rA, rDx, rb, BaseType::MoveMeshFlag());
252 
253  p_scheme->FinalizeNonLinIteration(r_model_part, rA, rDx, rb);
254  this->mpConvergenceCriteria->FinalizeNonLinearIteration(r_model_part, r_dof_set, rA, rDx, rb);
255 
256  if (is_converged) {
257  if (this->mpConvergenceCriteria->GetActualizeRHSflag()) {
258  TSparseSpace::SetToZero(rb);
259  p_builder_and_solver->BuildRHS(p_scheme, r_model_part, rb);
260  }
261  is_converged = this->mpConvergenceCriteria->PostCriteria(r_model_part, r_dof_set, rA, rDx, rb);
262  }
263 
264  //Iteration Cycle... performed only for NonLinearProblems
265  while (is_converged == false && iteration_number++ < this->mMaxIterationNumber) {
266  // We compute the contact forces with the DEM
267  update_dem_kinematics_process.Execute();
268  mpDEMStrategy->SolveSolutionStep();
269  auto transfer_process_damped = TransferNodalForcesToFem(this->GetModelPart(), true);
270  transfer_process_damped.Execute();
271  update_dem_kinematics_process.Execute();
272 
273  //setting the number of iteration
274  r_model_part.GetProcessInfo()[NL_ITERATION_NUMBER] = iteration_number;
275 
276  p_scheme->InitializeNonLinIteration(r_model_part, rA, rDx, rb);
277  this->mpConvergenceCriteria->InitializeNonLinearIteration(r_model_part, r_dof_set, rA, rDx, rb);
278 
279  is_converged = this->mpConvergenceCriteria->PreCriteria(r_model_part, r_dof_set, rA, rDx, rb);
280 
281  //call the linear system solver to find the correction mDx for the
282  //it is not called if there is no system to solve
283  if (SparseSpaceType::Size(rDx) != 0) {
285  if (this->GetKeepSystemConstantDuringIterations() == false) {
286  //A = 0.00;
287  TSparseSpace::SetToZero(rA);
288  TSparseSpace::SetToZero(rDx);
289  TSparseSpace::SetToZero(rb);
290 
291  p_builder_and_solver->BuildAndSolve(p_scheme, r_model_part, rA, rDx, rb);
292  } else {
293  TSparseSpace::SetToZero(rDx);
294  TSparseSpace::SetToZero(rb);
295 
296  p_builder_and_solver->BuildRHSAndSolve(p_scheme, r_model_part, rA, rDx, rb);
297  }
298  } else {
299  TSparseSpace::SetToZero(rDx);
300  TSparseSpace::SetToZero(rb);
301 
302  p_builder_and_solver->BuildRHSAndSolve(p_scheme, r_model_part, rA, rDx, rb);
303  }
304  } else {
305  KRATOS_WARNING("NO DOFS") << "ATTENTION: no free DOFs!! " << std::endl;
306  }
307 
308  // Debugging info
309  this->EchoInfo(iteration_number);
310 
311  // Updating the results stored in the database
312  this->UpdateDatabase(rA, rDx, rb, BaseType::MoveMeshFlag());
313 
314  p_scheme->FinalizeNonLinIteration(r_model_part, rA, rDx, rb);
315  this->mpConvergenceCriteria->FinalizeNonLinearIteration(r_model_part, r_dof_set, rA, rDx, rb);
316 
317  residual_is_updated = false;
318 
319  if (is_converged == true) {
320  if (this->mpConvergenceCriteria->GetActualizeRHSflag() == true) {
321  TSparseSpace::SetToZero(rb);
322  p_builder_and_solver->BuildRHS(p_scheme, r_model_part, rb);
323  residual_is_updated = true;
324  }
325  is_converged = this->mpConvergenceCriteria->PostCriteria(r_model_part, r_dof_set, rA, rDx, rb);
326  }
327  }
328 
329  //plots a warning if the maximum number of iterations is exceeded
330  if (iteration_number >= this->mMaxIterationNumber) {
331  this->MaxIterationsExceeded();
332  } else {
333  KRATOS_INFO_IF("ResidualBasedDEMCoupledNewtonRaphsonStrategy", this->GetEchoLevel() > 0)
334  << "Convergence achieved after " << iteration_number << " / "
335  << this->mMaxIterationNumber << " iterations" << std::endl;
336  }
337 
338  // Calculate reactions if required
339  if (this->mCalculateReactionsFlag == true)
340  p_builder_and_solver->CalculateReactions(p_scheme, r_model_part, rA, rDx, rb);
341 
342  return is_converged;
343  }
344 
349  void Predict() override
350  {
351  KRATOS_TRY
353  //OPERATIONS THAT SHOULD BE DONE ONCE - internal check to avoid repetitions
354  //if the operations needed were already performed this does nothing
355  if (this->mInitializeWasPerformed == false)
357 
358  //initialize solution step
360  mpDEMStrategy->InitializeSolutionStep();
361 
362  TSystemMatrixType& rA = *this->mpA;
363  TSystemVectorType& rDx = *this->mpDx;
364  TSystemVectorType& rb = *this->mpb;
365 
366  DofsArrayType& r_dof_set = this->GetBuilderAndSolver()->GetDofSet();
367 
368  this->GetScheme()->Predict(BaseType::GetModelPart(), r_dof_set, rA, rDx, rb);
369 
370  // Applying constraints if needed
371  auto& r_constraints_array = BaseType::GetModelPart().MasterSlaveConstraints();
372  const int local_number_of_constraints = r_constraints_array.size();
373  const int global_number_of_constraints = r_comm.SumAll(local_number_of_constraints);
374  if (global_number_of_constraints != 0) {
375  const auto& r_process_info = BaseType::GetModelPart().GetProcessInfo();
376 
377  const auto it_const_begin = r_constraints_array.begin();
378 
379  #pragma omp parallel for
380  for (int i=0; i<static_cast<int>(local_number_of_constraints); ++i)
381  (it_const_begin + i)->ResetSlaveDofs(r_process_info);
382 
383  #pragma omp parallel for
384  for (int i=0; i<static_cast<int>(local_number_of_constraints); ++i)
385  (it_const_begin + i)->Apply(r_process_info);
386 
387  // The following is needed since we need to eventually compute time derivatives after applying
388  // Master slave relations
389  TSparseSpace::SetToZero(rDx);
390  this->GetScheme()->Update(BaseType::GetModelPart(), r_dof_set, rA, rDx, rb);
391  }
392 
393  // Move the mesh if needed
394  if (this->MoveMeshFlag() == true)
396 
397  KRATOS_CATCH("")
398  }
399 
402 
404 
408 
411 
413 
414 
418 
422 
424  std::string Info() const override
425  {
426  return "ResidualBasedDEMCoupledNewtonRaphsonStrategy";
427  }
428 
429 
433 
435 
436  private:
439 
443 
447 
451 
455 
459 
463 
465 
466  protected:
467 
470 
474 
475  typename ExplicitSolverStrategy::Pointer mpDEMStrategy = nullptr;
476 
480 
484 
488 
492 
496 
502 
504 
505 }; /* Class ResidualBasedDEMCoupledNewtonRaphsonStrategy */
506 
508 
511 
513 
514 } /* namespace Kratos. */
515 
516 #endif /* KRATOS_RESIDUALBASED_DEM_COUPLED_NEWTON_RAPHSON_STRATEGY defined */
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
This is the base class to define the different convergence criterion considered.
Definition: convergence_criteria.h:58
Serial (do-nothing) version of a wrapper class for MPI communication.
Definition: data_communicator.h:318
iterator begin()
Iterator pointing to the beginning of the container.
Definition: data_value_container.h:209
Definition: explicit_solver_strategy.h:70
int mRebuildLevel
Definition: implicit_solving_strategy.h:263
bool mStiffnessMatrixIsBuilt
The current rebuild level.
Definition: implicit_solving_strategy.h:264
BaseType::TSystemMatrixType TSystemMatrixType
Definition: implicit_solving_strategy.h:70
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MasterSlaveConstraintContainerType & MasterSlaveConstraints(IndexType ThisIndex=0)
Definition: model_part.h:654
Communicator & GetCommunicator()
Definition: model_part.h:1821
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
A sorted associative container similar to an STL set, but uses a vector to store pointers to its data...
Definition: pointer_vector_set.h:72
This is the base Newton Raphson strategy coupled with the DEM strategy.
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:69
ExplicitSolverStrategy ExplicitSolverStrategyType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:75
bool SolveSolutionStep() override
Solves the current step. This function returns true if a solution has been found, false otherwise.
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:201
BaseType::TDataType TDataType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:83
ConvergenceCriteria< TSparseSpace, TDenseSpace > TConvergenceCriteriaType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:73
ResidualBasedDEMCoupledNewtonRaphsonStrategy(ModelPart &rModelPart, ExplicitSolverStrategyType::Pointer pDEMStrategy, typename TSchemeType::Pointer pScheme, typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria, typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver, int MaxIterations=30, bool CalculateReactions=false, bool ReformDofSetAtEachStep=false, bool MoveMeshFlag=false)
Constructor specifying the builder and solver.
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:116
void InitializeSolutionStep() override
Performs all the required operations that should be done (for each step) before solving the solution ...
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:176
BaseType::LocalSystemVectorType LocalSystemVectorType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:89
BaseType::TSystemVectorType TSystemVectorType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:88
BaseType::TSchemeType TSchemeType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:85
ResidualBasedDEMCoupledNewtonRaphsonStrategy< TSparseSpace, TDenseSpace, TLinearSolver > ClassType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:81
ResidualBasedNewtonRaphsonStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:80
ResidualBasedDEMCoupledNewtonRaphsonStrategy()
Default constructor.
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:101
BaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:90
BaseType::TSystemMatrixType TSystemMatrixType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:87
TSparseSpace SparseSpaceType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:84
std::string Info() const override
Turn back information as a string.
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:424
~ResidualBasedDEMCoupledNewtonRaphsonStrategy() override
Destructor.
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:143
ExplicitSolverStrategy::Pointer mpDEMStrategy
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:475
void Initialize() override
Initialization of member variables and prior operations.
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:164
void Predict() override
Operation to predict the solution ... if it is not called a trivial predictor is used in which the va...
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:349
ResidualBasedDEMCoupledNewtonRaphsonStrategy(const ResidualBasedDEMCoupledNewtonRaphsonStrategy &Other)
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:501
void FinalizeSolutionStep() override
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:190
BaseType::TSystemVectorPointerType TSystemVectorPointerType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:92
BaseType::DofsArrayType DofsArrayType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:86
BaseType::TSystemMatrixPointerType TSystemMatrixPointerType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:91
BaseType::TBuilderAndSolverType TBuilderAndSolverType
Definition: residualbased_DEM_coupled_newton_raphson_strategy.h:82
KRATOS_CLASS_POINTER_DEFINITION(ResidualBasedDEMCoupledNewtonRaphsonStrategy)
This is the base Newton Raphson strategy.
Definition: residualbased_newton_raphson_strategy.h:66
bool mUseOldStiffnessInFirstIteration
Flag telling if a full update of the database will be performed at the first iteration.
Definition: residualbased_newton_raphson_strategy.h:1259
TBuilderAndSolverType::Pointer GetBuilderAndSolver()
Get method for the builder and solver.
Definition: residualbased_newton_raphson_strategy.h:493
virtual void UpdateDatabase(TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb, const bool MoveMesh)
Here the database is updated.
Definition: residualbased_newton_raphson_strategy.h:1278
unsigned int mMaxIterationNumber
Definition: residualbased_newton_raphson_strategy.h:1261
TSchemeType::Pointer GetScheme()
Get method for the time scheme.
Definition: residualbased_newton_raphson_strategy.h:475
TSystemVectorPointerType mpb
The increment in the solution.
Definition: residualbased_newton_raphson_strategy.h:1237
TSystemMatrixPointerType mpA
The RHS vector of the system of equations.
Definition: residualbased_newton_raphson_strategy.h:1238
virtual void MaxIterationsExceeded()
This method prints information after reach the max number of iterations.
Definition: residualbased_newton_raphson_strategy.h:1340
bool mCalculateReactionsFlag
Flag telling if it is needed or not to compute the reactions.
Definition: residualbased_newton_raphson_strategy.h:1253
TSystemVectorPointerType mpDx
The pointer to the convergence criteria employed.
Definition: residualbased_newton_raphson_strategy.h:1236
BaseType::TSchemeType TSchemeType
Definition: residualbased_newton_raphson_strategy.h:87
void Initialize() override
Initialization of member variables and prior operations.
Definition: residualbased_newton_raphson_strategy.h:672
void InitializeSolutionStep() override
Performs all the required operations that should be done (for each step) before solving the solution ...
Definition: residualbased_newton_raphson_strategy.h:781
BaseType::TBuilderAndSolverType TBuilderAndSolverType
Definition: residualbased_newton_raphson_strategy.h:81
bool GetKeepSystemConstantDuringIterations()
Get method for the flag mKeepSystemConstantDuringIterations.
Definition: residualbased_newton_raphson_strategy.h:1158
void Clear() override
Clears the internal storage.
Definition: residualbased_newton_raphson_strategy.h:707
virtual void EchoInfo(const unsigned int IterationNumber)
This method returns the components of the system of equations depending of the echo level.
Definition: residualbased_newton_raphson_strategy.h:1298
TConvergenceCriteriaType::Pointer mpConvergenceCriteria
The pointer to the builder and solver employed.
Definition: residualbased_newton_raphson_strategy.h:1234
bool mInitializeWasPerformed
The maximum number of iterations, 30 by default.
Definition: residualbased_newton_raphson_strategy.h:1263
void FinalizeSolutionStep() override
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: residualbased_newton_raphson_strategy.h:848
Solving strategy base class This is the base class from which we will derive all the strategies (impl...
Definition: solving_strategy.h:64
TSparseSpace::VectorPointerType TSystemVectorPointerType
Definition: solving_strategy.h:77
TDenseSpace::MatrixType LocalSystemMatrixType
Definition: solving_strategy.h:79
TSparseSpace::DataType TDataType
Definition: solving_strategy.h:69
ModelPart & GetModelPart()
Operations to get the pointer to the model.
Definition: solving_strategy.h:350
int GetEchoLevel()
This returns the level of echo for the solving strategy.
Definition: solving_strategy.h:271
TSparseSpace::MatrixType TSystemMatrixType
Definition: solving_strategy.h:71
TSparseSpace::MatrixPointerType TSystemMatrixPointerType
Definition: solving_strategy.h:75
bool MoveMeshFlag()
This function returns the flag that says if the mesh is moved.
Definition: solving_strategy.h:290
TSparseSpace::VectorType TSystemVectorType
Definition: solving_strategy.h:73
virtual void MoveMesh()
This function is designed to move the mesh.
Definition: solving_strategy.h:330
TDenseSpace::VectorType LocalSystemVectorType
Definition: solving_strategy.h:81
Definition: transfer_nodal_forces_to_fem.h:32
void Execute() override
Execute method is used to execute the Process algorithms.
Definition: transfer_nodal_forces_to_fem.cpp:32
static IndexType Size(VectorType const &rV)
return size of vector rV
Definition: ublas_space.h:190
Definition: update_dem_kinematics_process.h:29
void Execute() override
Execute method is used to execute the Process algorithms.
Definition: update_dem_kinematics_process.cpp:29
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_INFO_IF(label, conditional)
Definition: logger.h:251
#define KRATOS_WARNING(label)
Definition: logger.h:265
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
integer i
Definition: TensorModule.f:17