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.
residual_based_bdf_scheme.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: Vicente Mataix Ferrandiz
11 //
12 
13 
14 #if !defined(KRATOS_RESIDUAL_BASED_BDF_SCHEME )
15 #define KRATOS_RESIDUAL_BASED_BDF_SCHEME
16 
17 /* System includes */
18 
19 /* External includes */
20 
21 /* Project includes */
22 #include "includes/checks.h"
25 
26 namespace Kratos
27 {
42 
80 template<class TSparseSpace, class TDenseSpace>
82  : public ResidualBasedImplicitTimeScheme<TSparseSpace, TDenseSpace>
83 {
84 public:
88 
90 
91  typedef typename BaseType::Pointer BaseTypePointer;
92 
94 
96 
98 
100 
102 
104 
106 
108 
110 
112  static constexpr double ZeroTolerance = std::numeric_limits<double>::epsilon();
113 
117 
123  explicit ResidualBasedBDFScheme(const std::size_t Order = 2)
124  :ImplicitBaseType(),
125  mOrder(Order),
126  mpBDFUtility(Kratos::make_unique<TimeDiscretization::BDF>(Order))
127  {
128  // Allocate auxiliary memory
129  const std::size_t num_threads = ParallelUtilities::GetNumThreads();
130 
131  mVector.dotun0.resize(num_threads);
132  mVector.dot2un0.resize(num_threads);
133 
134  // Doing a minimal check
135  KRATOS_ERROR_IF(mOrder < 1) << "ERROR:: Not possible to compute a BDF of order less than 1" << std::endl;
136 
137  // We resize the BDF coefficients
138  if (mBDF.size() != (mOrder + 1))
139  mBDF.resize(mOrder + 1);
140  }
141 
145  :ImplicitBaseType(rOther)
146  ,mOrder(rOther.mOrder)
147  ,mBDF(rOther.mBDF)
148  ,mVector(rOther.mVector)
149  ,mpBDFUtility(nullptr)
150  {
151  Kratos::unique_ptr<TimeDiscretization::BDF> auxiliar_pointer = Kratos::make_unique<TimeDiscretization::BDF>(mOrder);
152  mpBDFUtility.swap(auxiliar_pointer);
153  }
154 
159  {
160  return BaseTypePointer( new ResidualBasedBDFScheme(*this) );
161  }
162 
166  () override {}
167 
171 
175 
186  void Update(
187  ModelPart& rModelPart,
188  DofsArrayType& rDofSet,
189  TSystemMatrixType& rA,
190  TSystemVectorType& rDx,
192  ) override
193  {
194  KRATOS_TRY;
195 
196  // Update of displacement (by DOF)
197  mpDofUpdater->UpdateDofs(rDofSet, rDx);
198 
199  UpdateDerivatives(rModelPart, rDofSet, rA, rDx, rb);
200 
201  KRATOS_CATCH( "" );
202  }
203 
213  void Predict(
214  ModelPart& rModelPart,
215  DofsArrayType& rDofSet,
216  TSystemMatrixType& rA,
217  TSystemVectorType& rDx,
219  ) override
220  {
221  KRATOS_TRY;
222 
223  KRATOS_ERROR << "Calling base BDF class" << std::endl;
224 
225  KRATOS_CATCH( "" );
226  }
227 
237  ModelPart& rModelPart,
238  TSystemMatrixType& rA,
239  TSystemVectorType& rDx,
241  ) override
242  {
243  KRATOS_TRY;
244 
245  ProcessInfo& r_current_process_info = rModelPart.GetProcessInfo();
246 
247  ImplicitBaseType::InitializeSolutionStep(rModelPart, rA, rDx, rb);
248 
249  mpBDFUtility->ComputeAndSaveBDFCoefficients(r_current_process_info);
250  mBDF = r_current_process_info[BDF_COEFFICIENTS];
251 
252  const double dt_0 = r_current_process_info[DELTA_TIME];
253  const double dt_1 = r_current_process_info.GetPreviousTimeStepInfo(1)[DELTA_TIME];
254  KRATOS_ERROR_IF(mOrder > 2 && std::abs(dt_0 - dt_1) > 1e-10*(dt_0 + dt_1))
255  << "ResidualBasedBDFScheme. For higher orders than 2 the time step must be constant.\nPrevious time step : " << dt_1 << "\nCurrent time step : " << dt_0 << std::endl;
256 
257  KRATOS_CATCH( "" );
258  }
259 
266  int Check(const ModelPart& rModelPart) const override
267  {
268  KRATOS_TRY;
269 
270  const int err = ImplicitBaseType::Check(rModelPart);
271  if(err!=0) return err;
272 
273  // Check for minimum value of the buffer index
274  // Verify buffer size
275  KRATOS_ERROR_IF(rModelPart.GetBufferSize() < mOrder + 1) << "Insufficient buffer size. Buffer size should be greater than " << mOrder + 1 << ". Current size is " << rModelPart.GetBufferSize() << std::endl;
276 
277  KRATOS_CATCH( "" );
278 
279  return 0;
280  }
281 
283  void Clear() override
284  {
285  this->mpDofUpdater->Clear();
286  }
287 
291 
295 
299 
305  {
306  Parameters default_parameters = Parameters(R"(
307  {
308  "name" : "base_bdf_scheme",
309  "integration_order" : 2
310  })");
311 
312  // Getting base class default parameters
313  const Parameters base_default_parameters = ImplicitBaseType::GetDefaultParameters();
314  default_parameters.RecursivelyAddMissingParameters(base_default_parameters);
315  return default_parameters;
316  }
317 
319  std::string Info() const override
320  {
321  return "ResidualBasedBDFScheme";
322  }
323 
325  void PrintInfo(std::ostream& rOStream) const override
326  {
327  rOStream << Info();
328  }
329 
331  void PrintData(std::ostream& rOStream) const override
332  {
333  rOStream << Info();
334  }
335 
339 
340 protected:
341 
344 
348 
350  {
351  std::vector< Vector > dotun0;
352  std::vector< Vector > dot2un0;
353  };
354 
355  const std::size_t mOrder;
359 
360 
364 
368 
377  inline void UpdateDerivatives(
378  ModelPart& rModelPart,
379  DofsArrayType& rDofSet,
380  TSystemMatrixType& rA,
381  TSystemVectorType& rDx,
383  )
384  {
385  // Updating time derivatives (nodally for efficiency)
386  const int num_nodes = static_cast<int>( rModelPart.Nodes().size() );
387 
388  // Getting first node iterator
389  const auto it_node_begin = rModelPart.Nodes().begin();
390 
391  IndexPartition<std::size_t>(num_nodes).for_each([&](std::size_t Index){
392  auto it_node = it_node_begin + Index;
393 
394  UpdateFirstDerivative(it_node);
395  UpdateSecondDerivative(it_node);
396  });
397  }
398 
404  {
405  KRATOS_ERROR << "Calling base BDF class" << std::endl;
406  }
407 
413  {
414  KRATOS_ERROR << "Calling base BDF class" << std::endl;
415  }
416 
426  LocalSystemMatrixType& rLHS_Contribution,
429  const ProcessInfo& rCurrentProcessInfo
430  ) override
431  {
432  // Adding mass contribution to the dynamic stiffness
433  if (rM.size1() != 0) { // if M matrix declared
434  noalias(rLHS_Contribution) += rM * std::pow(mBDF[0], 2);
435  }
436 
437  // Adding damping contribution
438  if (rD.size1() != 0) { // if D matrix declared
439  noalias(rLHS_Contribution) += rD * mBDF[0];
440  }
441  }
442 
452  template <class TObjectType>
454  TObjectType& rObject,
455  LocalSystemVectorType& rRHS_Contribution,
458  const ProcessInfo& rCurrentProcessInfo
459  )
460  {
461  const std::size_t this_thread = OpenMPUtils::ThisThread();
462  const auto& r_const_obj_ref = rObject;
463 
464  // Adding inertia contribution
465  if (rM.size1() != 0) {
466  r_const_obj_ref.GetSecondDerivativesVector(mVector.dot2un0[this_thread], 0);
467  noalias(rRHS_Contribution) -= prod(rM, mVector.dot2un0[this_thread]);
468  }
469 
470  // Adding damping contribution
471  if (rD.size1() != 0) {
472  r_const_obj_ref.GetFirstDerivativesVector(mVector.dotun0[this_thread], 0);
473  noalias(rRHS_Contribution) -= prod(rD, mVector.dotun0[this_thread]);
474  }
475  }
476 
487  Element& rElement,
488  LocalSystemVectorType& rRHS_Contribution,
491  const ProcessInfo& rCurrentProcessInfo
492  ) override
493  {
494  TemplateAddDynamicsToRHS<Element>(rElement, rRHS_Contribution, rD, rM, rCurrentProcessInfo);
495  }
496 
507  Condition& rCondition,
508  LocalSystemVectorType& rRHS_Contribution,
511  const ProcessInfo& rCurrentProcessInfo
512  ) override
513  {
514  TemplateAddDynamicsToRHS<Condition>(rCondition, rRHS_Contribution, rD, rM, rCurrentProcessInfo);
515  }
516 
520 
524 
529 
530 private:
531 
537 
539  typename TSparseSpace::DofUpdaterPointerType mpDofUpdater = TSparseSpace::CreateDofUpdater();
540 
544 
548 
553 
557 
564 }; /* Class ResidualBasedBDFScheme */
572 } /* namespace Kratos.*/
573 
574 #endif /* KRATOS_RESIDUAL_BASED_BDF_SCHEME defined */
Base class for all Conditions.
Definition: condition.h:59
Base class for all Elements.
Definition: element.h:60
std::vector< DofType::Pointer > DofsVectorType
Definition: element.h:100
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
void resize(std::size_t NewSize1, std::size_t NewSize2, bool preserve=0)
Definition: amatrix_interface.h:224
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
IndexType GetBufferSize() const
This method gets the suffer size of the model part database.
Definition: model_part.h:1876
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
MeshType::NodesContainerType NodesContainerType
Nodes container. Which is a vector set of nodes with their Id's as key.
Definition: model_part.h:128
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
static int ThisThread()
Wrapper for omp_get_thread_num().
Definition: openmp_utils.h:108
static int GetNumThreads()
Returns the current number of threads.
Definition: parallel_utilities.cpp:34
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
void RecursivelyAddMissingParameters(const Parameters &rDefaultParameters)
This function is designed to verify that the parameters under testing contain at least all parameters...
Definition: kratos_parameters.cpp:1457
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
boost::indirect_iterator< typename TContainerType::iterator > iterator
Definition: pointer_vector_set.h:95
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
ProcessInfo & GetPreviousTimeStepInfo(IndexType StepsBefore=1)
Definition: process_info.h:187
BDF integration scheme (for dynamic problems)
Definition: residual_based_bdf_scheme.h:83
void Update(ModelPart &rModelPart, DofsArrayType &rDofSet, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb) override
Performing the update of the solution.
Definition: residual_based_bdf_scheme.h:186
void Predict(ModelPart &rModelPart, DofsArrayType &rDofSet, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb) override
Performing the prediction of the solution.
Definition: residual_based_bdf_scheme.h:213
ImplicitBaseType::DofsArrayType DofsArrayType
Definition: residual_based_bdf_scheme.h:97
std::string Info() const override
Turn back information as a string.
Definition: residual_based_bdf_scheme.h:319
void AddDynamicsToRHS(Condition &rCondition, LocalSystemVectorType &rRHS_Contribution, LocalSystemMatrixType &rD, LocalSystemMatrixType &rM, const ProcessInfo &rCurrentProcessInfo) override
It adds the dynamic RHS contribution of the condition.
Definition: residual_based_bdf_scheme.h:506
ImplicitBaseType::TDataType TDataType
Definition: residual_based_bdf_scheme.h:95
void TemplateAddDynamicsToRHS(TObjectType &rObject, LocalSystemVectorType &rRHS_Contribution, LocalSystemMatrixType &rD, LocalSystemMatrixType &rM, const ProcessInfo &rCurrentProcessInfo)
It adds the dynamic RHS contribution of the objects.
Definition: residual_based_bdf_scheme.h:453
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: residual_based_bdf_scheme.h:304
ResidualBasedImplicitTimeScheme< TSparseSpace, TDenseSpace > ImplicitBaseType
Definition: residual_based_bdf_scheme.h:93
GeneralVectors mVector
The BDF coefficients.
Definition: residual_based_bdf_scheme.h:357
Kratos::unique_ptr< TimeDiscretization::BDF > mpBDFUtility
The structure containing the derivatives.
Definition: residual_based_bdf_scheme.h:358
ResidualBasedBDFScheme(const std::size_t Order=2)
Constructor. The BDF method.
Definition: residual_based_bdf_scheme.h:123
ResidualBasedBDFScheme(ResidualBasedBDFScheme &rOther)
Definition: residual_based_bdf_scheme.h:144
ImplicitBaseType::LocalSystemVectorType LocalSystemVectorType
Definition: residual_based_bdf_scheme.h:105
static constexpr double ZeroTolerance
Definition of epsilon.
Definition: residual_based_bdf_scheme.h:112
BaseTypePointer Clone() override
Definition: residual_based_bdf_scheme.h:158
void InitializeSolutionStep(ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb) override
It initializes time step solution. Only for reasons if the time step solution is restarted.
Definition: residual_based_bdf_scheme.h:236
virtual void UpdateSecondDerivative(NodesArrayType::iterator itNode)
Updating second time derivative (acceleration)
Definition: residual_based_bdf_scheme.h:412
Scheme< TSparseSpace, TDenseSpace > BaseType
Definition: residual_based_bdf_scheme.h:89
void AddDynamicsToLHS(LocalSystemMatrixType &rLHS_Contribution, LocalSystemMatrixType &rD, LocalSystemMatrixType &rM, const ProcessInfo &rCurrentProcessInfo) override
It adds the dynamic LHS contribution of the elements.
Definition: residual_based_bdf_scheme.h:425
~ResidualBasedBDFScheme() override
Definition: residual_based_bdf_scheme.h:166
void AddDynamicsToRHS(Element &rElement, LocalSystemVectorType &rRHS_Contribution, LocalSystemMatrixType &rD, LocalSystemMatrixType &rM, const ProcessInfo &rCurrentProcessInfo) override
It adds the dynamic RHS contribution of the elements.
Definition: residual_based_bdf_scheme.h:486
BaseType::Pointer BaseTypePointer
Definition: residual_based_bdf_scheme.h:91
ImplicitBaseType::TSystemVectorType TSystemVectorType
Definition: residual_based_bdf_scheme.h:103
Vector mBDF
The integration order.
Definition: residual_based_bdf_scheme.h:356
void UpdateDerivatives(ModelPart &rModelPart, DofsArrayType &rDofSet, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb)
Performing the update of the derivatives.
Definition: residual_based_bdf_scheme.h:377
ImplicitBaseType::LocalSystemMatrixType LocalSystemMatrixType
Definition: residual_based_bdf_scheme.h:107
void Clear() override
Free memory allocated by this class.
Definition: residual_based_bdf_scheme.h:283
KRATOS_CLASS_POINTER_DEFINITION(ResidualBasedBDFScheme)
virtual void UpdateFirstDerivative(NodesArrayType::iterator itNode)
Updating first time derivative (velocity)
Definition: residual_based_bdf_scheme.h:403
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: residual_based_bdf_scheme.h:325
const std::size_t mOrder
Definition: residual_based_bdf_scheme.h:355
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: residual_based_bdf_scheme.h:331
ModelPart::NodesContainerType NodesArrayType
Definition: residual_based_bdf_scheme.h:109
ImplicitBaseType::TSystemMatrixType TSystemMatrixType
Definition: residual_based_bdf_scheme.h:101
int Check(const ModelPart &rModelPart) const override
This function is designed to be called once to perform all the checks needed on the input provided.
Definition: residual_based_bdf_scheme.h:266
Element::DofsVectorType DofsVectorType
Definition: residual_based_bdf_scheme.h:99
This is the base class for the implicit time schemes.
Definition: residual_based_implicit_time_scheme.h:55
Parameters GetDefaultParameters() const override
This method provides the defaults parameters to avoid conflicts between the different constructors.
Definition: residual_based_implicit_time_scheme.h:289
int Check(const ModelPart &rModelPart) const override
This function is designed to be called once to perform all the checks needed on the input provided.
Definition: residual_based_implicit_time_scheme.h:273
void InitializeSolutionStep(ModelPart &rModelPart, TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb) override
It initializes time step solution. Only for reasons if the time step solution is restarted.
Definition: residual_based_implicit_time_scheme.h:245
BaseType::TDataType TDataType
Data type definition.
Definition: residual_based_implicit_time_scheme.h:71
BaseType::TSystemMatrixType TSystemMatrixType
Matrix type definition.
Definition: residual_based_implicit_time_scheme.h:73
BaseType::LocalSystemMatrixType LocalSystemMatrixType
Local system vector type definition.
Definition: residual_based_implicit_time_scheme.h:79
BaseType::LocalSystemVectorType LocalSystemVectorType
Local system matrix type definition.
Definition: residual_based_implicit_time_scheme.h:77
BaseType::TSystemVectorType TSystemVectorType
Vector type definition.
Definition: residual_based_implicit_time_scheme.h:75
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
Definition: exception.h:161
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::unique_ptr< T > unique_ptr
Definition: smart_pointers.h:33
unique_ptr< C > make_unique(Args &&...args)
Definition: smart_pointers.h:45
AMatrix::MatrixProductExpression< TExpression1Type, TExpression2Type > prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:568
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
def Index()
Definition: hdf5_io_tools.py:38
def num_threads
Definition: script.py:75
e
Definition: run_cpp_mpi_tests.py:31
Definition: residual_based_bdf_scheme.h:350
std::vector< Vector > dot2un0
First derivative.
Definition: residual_based_bdf_scheme.h:352
std::vector< Vector > dotun0
Definition: residual_based_bdf_scheme.h:351