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.
trilinos_residualbased_incremental_aitken_static_scheme.h
Go to the documentation of this file.
1 // KRATOS _____ _ _ _
2 // |_ _| __(_) (_)_ __ ___ ___
3 // | || '__| | | | '_ \ / _ \/ __|
4 // | || | | | | | | | | (_) \__
5 // |_||_| |_|_|_|_| |_|\___/|___/ APPLICATION
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Riccardo Rossi
11 //
12 
13 
14 #ifndef KRATOS_TRILINOS_RESIDUALBASED_INCREMENTAL_AITKEN_STATIC_SCHEME_H
15 #define KRATOS_TRILINOS_RESIDUALBASED_INCREMENTAL_AITKEN_STATIC_SCHEME_H
16 
17 // System includes
18 
19 // External includes
20 
21 // Project includes
23 
24 
25 namespace Kratos
26 {
29 
32 
36 
40 
44 
48 
50 
52 template< class TSparseSpace,class TDenseSpace >
54 {
55 public:
58 
61 
63 
64  typedef typename BaseType::TDataType TDataType;
65 
67 
69 
71 
73 
74  //typedef typename BaseType::LocalSystemVectorType LocalSystemVectorType;
75  //typedef typename BaseType::LocalSystemMatrixType LocalSystemMatrixType;
76 
80 
82 
85  ResidualBasedIncrementalUpdateStaticScheme<TSparseSpace,TDenseSpace>(),
86  mDefaultOmega(DefaultOmega),
87  mOldOmega(DefaultOmega)
88  {}
89 
92 
93 
97 
98 
102 
104 
110  void InitializeSolutionStep(ModelPart &r_model_part,
112  TSystemVectorType &Dx,
113  TSystemVectorType &b) override
114  {
115  BaseType::InitializeSolutionStep(r_model_part,A,Dx,b);
116  mIterationCounter = 0;
117  }
118 
120 
126  void Update(ModelPart &r_model_part,
127  DofsArrayType &rDofSet,
129  TSystemVectorType &Dx,
130  TSystemVectorType &b) override
131  {
132  KRATOS_TRY;
133 
134  mIterationCounter++;
135  double Omega;
136 
137  if (mIterationCounter > 1)
138  {
139  // Compute relaxation factor
140  Omega = this->CalculateOmega(Dx);
141  }
142  else
143  {
144  // Initialize the process using min(DefaultOmega,Omega_from_last_step)
145  if (mOldOmega < mDefaultOmega)
146  Omega = mOldOmega;
147  else
148  Omega = mDefaultOmega;
149  }
150 
151 
152 // KRATOS_WATCH(Omega);
153 
154  this->UpdateWithRelaxation(rDofSet,Dx,Omega);
155 
156  // Store results for next iteration
158  mpPreviousDx.swap(Tmp);
159  mOldOmega = Omega;
160 
161  KRATOS_CATCH("");
162  }
163 
164  void Clear() override
165  {
166  mpDofImporter.reset();
167  mImporterIsInitialized = false;
168  }
169 
173 
174 
178 
179 
183 
184 
188 
189 
191 
192 protected:
195 
196 
200 
201 
205 
206 
210 
213  {
214  KRATOS_TRY;
215 
216  double Num = 0.0;
217  double Den = 0.0;
218 
219  int MyLength = Dx.MyLength();
220 
221  // Safety check, to be sure that we didn't move nodes along processors
222  if (MyLength != mpPreviousDx->MyLength())
223  {
224  KRATOS_THROW_ERROR(std::runtime_error,"Unexpected error in Trilinos Aitken iterations: the Dx vector has a different size than in previous iteration.","");
225  }
226 
227 // int NumVectors = Dx.NumVectors();
228 
229  // These point to data held by the Epetra_Vector, do not delete[] here.
230  double* pDxValues;
231  int DxLDA;
232  double* pOldDxValues;
233  int OldDxLDA;
234 
235  Dx.ExtractView(&pDxValues,&DxLDA);
236  mpPreviousDx->ExtractView(&pOldDxValues,&OldDxLDA);
237 
238  for (int i = 0; i < MyLength; i++)
239  {
240  double Diff = pDxValues[i] - pOldDxValues[i];
241  Num += pOldDxValues[i] * Diff;
242  Den += Diff * Diff;
243  }
244 
245  Dx.Comm().Barrier();
246 
247  // Sum values across processors
248  double SendBuff[2] = {Num,Den};
249  double RecvBuff[2];
250 
251  Dx.Comm().SumAll(&SendBuff[0],&RecvBuff[0],2);
252 
253  Num = RecvBuff[0];
254  Den = RecvBuff[1];
255 
256  double Omega = - mOldOmega * Num / Den;
257  return Omega;
258 
259  KRATOS_CATCH("");
260  }
261 
263  TSystemVectorType& Dx,
264  const double Omega)
265  {
266  KRATOS_TRY;
267 
268  if (!this->DofImporterIsInitialized())
269  this->InitializeDofImporter(rDofSet,Dx);
270 
272 
273  int system_size = TSparseSpace::Size(Dx);
274 
275  //defining a temporary vector to gather all of the values needed
276  Epetra_Vector temp( pImporter->TargetMap() );
277 
278  //importing in the new temp vector the values
279  int ierr = temp.Import(Dx,*pImporter,Insert);
280  if(ierr != 0) KRATOS_THROW_ERROR(std::logic_error,"Epetra failure found","");
281 
282  double* temp_values; //DO NOT make delete of this one!!
283  temp.ExtractView( &temp_values );
284 
285  Dx.Comm().Barrier();
286 
287  //performing the update
288  auto dof_begin = rDofSet.begin();
289  for(unsigned int iii=0; iii<rDofSet.size(); iii++)
290  {
291  int global_id = (dof_begin+iii)->EquationId();
292  if(global_id < system_size)
293  {
294  double aaa = temp[pImporter->TargetMap().LID(global_id)];
295  (dof_begin+iii)->GetSolutionStepValue() += Omega * aaa;
296  }
297  }
298 
299  KRATOS_CATCH("");
300  }
301 
302 
303  virtual void InitializeDofImporter(DofsArrayType& rDofSet,
304  TSystemVectorType& Dx)
305  {
306  int system_size = TSparseSpace::Size(Dx);
307  int number_of_dofs = rDofSet.size();
308  std::vector< int > index_array(number_of_dofs);
309 
310  //filling the array with the global ids
311  int counter = 0;
312  for(auto i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
313  {
314  int id = i_dof->EquationId();
315  if( id < system_size )
316  {
317  index_array[counter] = id;
318  counter += 1;
319  }
320  }
321 
322  std::sort(index_array.begin(),index_array.end());
323  std::vector<int>::iterator NewEnd = std::unique(index_array.begin(),index_array.end());
324  index_array.resize(NewEnd-index_array.begin());
325 
326  int check_size = -1;
327  int tot_update_dofs = index_array.size();
328  Dx.Comm().SumAll(&tot_update_dofs,&check_size,1);
329  KRATOS_ERROR_IF( (check_size < system_size) && (Dx.Comm().MyPID() == 0) )
330  << "Dof count is not correct. There are less dofs then expected." << std::endl
331  << "Expected number of active dofs = " << system_size << " dofs found = " << check_size << std::endl;
332 
333  //defining a map as needed
334  Epetra_Map dof_update_map(-1,index_array.size(), &(*(index_array.begin())),0,Dx.Comm() );
335 
336  //defining the importer class
337  Kratos::shared_ptr<Epetra_Import> pDofImporter = Kratos::make_shared<Epetra_Import>(dof_update_map,Dx.Map());
338  mpDofImporter.swap(pDofImporter);
339 
340  mImporterIsInitialized = true;
341  }
342 
346 
348 
354  {
355  return mpDofImporter;
356  }
357 
361 
363  {
364  return mImporterIsInitialized;
365  }
366 
370 
371 
373 
374 private:
377 
378 
382 
384  unsigned int mIterationCounter;
385 
387 
390  const double mDefaultOmega;
391 
393  double mOldOmega;
394 
396  SystemVectorPointerType mpPreviousDx;
397 
398  bool mImporterIsInitialized = false;
399 
400  Kratos::shared_ptr<Epetra_Import> mpDofImporter = nullptr;
401 
405 
406 
410 
411 
415 
416 
420 
421 
425 
428 
431 
432 
434 
435 }; // Class TrilinosResidualBasedIncrementalAitkenStaticScheme
436 
438 
441 
442 
446 
447 
449 
451 
452 } // namespace Kratos.
453 
454 
455 #endif // KRATOS_TRILINOS_RESIDUALBASED_INCREMENTAL_AITKEN_STATIC_SCHEME_H
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
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
size_type size() const
Returns the number of elements in the container.
Definition: pointer_vector_set.h:502
iterator begin()
Returns an iterator pointing to the beginning of the container.
Definition: pointer_vector_set.h:278
iterator end()
Returns an iterator pointing to the end of the container.
Definition: pointer_vector_set.h:314
This class provides the implementation of a static scheme.
Definition: residualbased_incrementalupdate_static_scheme.h:57
BaseType::TSystemMatrixType TSystemMatrixType
Matrix type definition.
Definition: residualbased_incrementalupdate_static_scheme.h:78
BaseType::TSystemVectorType TSystemVectorType
Vector type definition.
Definition: residualbased_incrementalupdate_static_scheme.h:80
typename TSparseSpace::MatrixType TSystemMatrixType
Matrix type definition.
Definition: scheme.h:71
virtual void EquationId(const Element &rElement, Element::EquationIdVectorType &rEquationId, const ProcessInfo &rCurrentProcessInfo)
This method gets the eqaution id corresponding to the current element.
Definition: scheme.h:636
typename TSparseSpace::VectorType TSystemVectorType
Vector type definition.
Definition: scheme.h:74
virtual void InitializeSolutionStep(ModelPart &rModelPart, TSystemMatrixType &A, TSystemVectorType &Dx, TSystemVectorType &b)
Function called once at the beginning of each solution step.
Definition: scheme.h:272
typename TSparseSpace::DataType TDataType
Data type definition.
Definition: scheme.h:68
A scheme for the solution of a problem using Aitken iterations.
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:54
void InitializeSolutionStep(ModelPart &r_model_part, TSystemMatrixType &A, TSystemVectorType &Dx, TSystemVectorType &b) override
Initialize the iteration counter at the begining of each solution step.
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:110
BaseType::TDataType TDataType
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:64
ResidualBasedIncrementalUpdateStaticScheme< TSparseSpace, TDenseSpace > BaseType
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:62
BaseType::TSystemVectorType TSystemVectorType
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:70
Kratos::shared_ptr< TSystemVectorType > SystemVectorPointerType
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:72
BaseType::TSystemMatrixType TSystemMatrixType
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:68
void Update(ModelPart &r_model_part, DofsArrayType &rDofSet, TSystemMatrixType &A, TSystemVectorType &Dx, TSystemVectorType &b) override
Update the degrees of freedom of the problem using Aitken's accelerator.
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:126
void UpdateWithRelaxation(DofsArrayType &rDofSet, TSystemVectorType &Dx, const double Omega)
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:262
bool DofImporterIsInitialized() const
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:362
TrilinosResidualBasedIncrementalAitkenStaticScheme(double DefaultOmega)
Default constructor.
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:84
Kratos::shared_ptr< Epetra_Import > pGetImporter()
Get pointer Epetra_Import instance that can be used to import values from Dx to the owner of each Dof...
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:353
KRATOS_CLASS_POINTER_DEFINITION(TrilinosResidualBasedIncrementalAitkenStaticScheme)
Pointer definition of TrilinosResidualBasedIncrementalAitkenStaticScheme.
virtual void InitializeDofImporter(DofsArrayType &rDofSet, TSystemVectorType &Dx)
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:303
virtual ~TrilinosResidualBasedIncrementalAitkenStaticScheme()
Destructor.
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:91
BaseType::DofsArrayType DofsArrayType
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:66
double CalculateOmega(TSystemVectorType &Dx)
Calculate the value of the Aitken relaxation factor for the current iteration.
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:212
void Clear() override
Liberate internal storage.
Definition: trilinos_residualbased_incremental_aitken_static_scheme.h:164
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
TSpaceType::IndexType Size(TSpaceType &dummy, typename TSpaceType::VectorType const &rV)
Definition: add_strategies_to_python.cpp:111
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::shared_ptr< T > shared_ptr
Definition: smart_pointers.h:27
def GetSolutionStepValue(entity, variable, solution_step_index)
Definition: coupling_interface_data.py:253
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
float temp
Definition: rotating_cone.py:85
int counter
Definition: script_THERMAL_CORRECT.py:218
A
Definition: sensitivityMatrix.py:70
integer i
Definition: TensorModule.f:17