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_mixed_generic_criteria.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: Jordi Cotela, Riccardo Rossi, Carlos Roig and Ruben Zorrilla
11 //
12 
13 #ifndef KRATOS_TRILINOS_MIXED_GENERIC_CRITERIA_H
14 #define KRATOS_TRILINOS_MIXED_GENERIC_CRITERIA_H
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "includes/define.h"
22 #include "includes/model_part.h"
24 
25 // Application includes
26 
27 namespace Kratos
28 {
31 
34 
36 
41 template< class TSparseSpace, class TDenseSpace >
42 class TrilinosMixedGenericCriteria : public MixedGenericCriteria< TSparseSpace, TDenseSpace >
43 {
44 public:
47 
49 
51 
52  typedef typename BaseType::TDataType TDataType;
53 
55 
57 
59 
61 
62  typedef typename BaseType::KeyType KeyType;
63 
67 
69 
77  : MixedGenericCriteria<TSparseSpace, TDenseSpace>(rConvergenceVariablesList)
78  {}
79 
82  {}
83 
87 
88 
92 
94 
103  ModelPart& rModelPart,
104  DofsArrayType& rDofSet,
105  const TSystemMatrixType& A,
106  const TSystemVectorType& Dx,
107  const TSystemVectorType& b) override
108  {
109  // If required, initialize the Epetra vector import
110  if(!mEpetraImportIsInitialized) {
111  InitializeEpetraImport(rDofSet, Dx);
112  }
113 
114  // Serial base implementation convergence check call
115  return BaseType::PostCriteria(rModelPart, rDofSet, A, Dx, b);
116  }
117 
121 
122 
126 
127 
131 
132 
134 private:
137 
138 
142 
143  bool mEpetraImportIsInitialized = false;
144  std::unique_ptr<Epetra_Import> mpDofImport = nullptr;
145 
149 
150 
154 
155  void GetNormValues(
156  const ModelPart& rModelPart,
157  const DofsArrayType& rDofSet,
158  const TSystemVectorType& rDx,
159  std::vector<int>& rDofsCount,
160  std::vector<TDataType>& rSolutionNormsVector,
161  std::vector<TDataType>& rIncreaseNormsVector) const override
162  {
163  int n_dofs = rDofSet.size();
164  const auto& r_data_comm = rModelPart.GetCommunicator().GetDataCommunicator();
165  const int rank = r_data_comm.Rank();
166 
167  // Do the local Dx vector import
168  Epetra_Vector local_dx(mpDofImport->TargetMap());
169  int i_err = local_dx.Import(rDx, *mpDofImport, Insert);
170  KRATOS_ERROR_IF_NOT(i_err == 0) << "Local Dx import failed!" << std::endl;
171 
172  // Local thread variables
173  int dof_id;
174  TDataType dof_dx;
175 
176  // Loop over Dofs
177  for (int i = 0; i < n_dofs; i++) {
178  auto it_dof = rDofSet.begin() + i;
179  if (it_dof->IsFree() && it_dof->GetSolutionStepValue(PARTITION_INDEX) == rank) {
180  dof_id = it_dof->EquationId();
181  const TDataType& r_dof_value = it_dof->GetSolutionStepValue(0);
182  dof_dx = local_dx[mpDofImport->TargetMap().LID(dof_id)];
183 
184  int var_local_key;
185  bool key_found = BaseType::FindVarLocalKey(it_dof,var_local_key);
186  if (!key_found) {
187  // the dof does not belong to the list of variables
188  // we are checking for convergence, so we skip it
189  continue;
190  }
191 
192  rSolutionNormsVector[var_local_key] += r_dof_value * r_dof_value;
193  rIncreaseNormsVector[var_local_key] += dof_dx * dof_dx;
194  rDofsCount[var_local_key]++;
195  }
196  }
197  }
198 
212  void InitializeEpetraImport(
213  const DofsArrayType &rDofSet,
214  const TSystemVectorType &rDx)
215  {
216  int number_of_dofs = rDofSet.size();
217  int system_size = TSparseSpace::Size(rDx);
218  std::vector<int> index_array(number_of_dofs);
219 
220  // Filling the array with the global ids
221  unsigned int counter = 0;
222  for (typename DofsArrayType::const_iterator i_dof = rDofSet.begin(); i_dof != rDofSet.end(); ++i_dof) {
223  const int id = i_dof->EquationId();
224  if (id < system_size) {
225  index_array[counter++] = id;
226  }
227  }
228 
229  std::sort(index_array.begin(), index_array.end());
230  std::vector<int>::iterator new_end = std::unique(index_array.begin(), index_array.end());
231  index_array.resize(new_end - index_array.begin());
232 
233  int check_size = -1;
234  int tot_update_dofs = index_array.size();
235  rDx.Comm().SumAll(&tot_update_dofs, &check_size, 1);
236  KRATOS_ERROR_IF(check_size < system_size)
237  << "DOF count is not correct. There are less dofs then expected.\n"
238  << "Expected number of active dofs: " << system_size << ", DOFs found: " << check_size << std::endl;
239 
240  // Defining a map as needed
241  Epetra_Map dof_update_map(-1, index_array.size(), &(*(index_array.begin())), 0, rDx.Comm());
242 
243  // Defining the import instance
244  std::unique_ptr<Epetra_Import> p_dof_import(new Epetra_Import(dof_update_map, rDx.Map()));
245  mpDofImport.swap(p_dof_import);
246 
247  mEpetraImportIsInitialized = true;
248  }
249 
253 
254 
258 
259 
263 
264 
266 };
268 
270 }
271 
272 #endif // KRATOS_TRILINOS_MIXED_GENERIC_CRITERIA_H
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
TSparseSpace::MatrixType TSystemMatrixType
Matrix type definition.
Definition: convergence_criteria.h:72
TSparseSpace::DataType TDataType
Data type definition.
Definition: convergence_criteria.h:70
TSparseSpace::VectorType TSystemVectorType
Vector type definition.
Definition: convergence_criteria.h:74
virtual int Rank() const
Get the parallel rank for this DataCommunicator.
Definition: data_communicator.h:587
Convergence criteria for mixed vector-scalar problems.
Definition: mixed_generic_criteria.h:42
bool FindVarLocalKey(typename DofsArrayType::const_iterator itDof, int &rVarLocalKey) const
Finds the var local key in the mLocalKeyMap for a gifen DOF. If the variable does not exist in mLocal...
Definition: mixed_generic_criteria.h:415
std::vector< std::tuple< const VariableData *, TDataType, TDataType > > ConvergenceVariableListType
Definition: mixed_generic_criteria.h:61
std::size_t KeyType
Definition: mixed_generic_criteria.h:63
bool PostCriteria(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &A, const TSystemVectorType &Dx, const TSystemVectorType &b) override
Compute relative and absolute error.
Definition: mixed_generic_criteria.h:166
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Communicator & GetCommunicator()
Definition: model_part.h:1821
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::const_iterator > const_iterator
Definition: pointer_vector_set.h:96
Convergence criteria for mixed vector-scalar problems.
Definition: trilinos_mixed_generic_criteria.h:43
BaseType::DofsArrayType DofsArrayType
Definition: trilinos_mixed_generic_criteria.h:54
bool PostCriteria(ModelPart &rModelPart, DofsArrayType &rDofSet, const TSystemMatrixType &A, const TSystemVectorType &Dx, const TSystemVectorType &b) override
Compute relative and absoute error.
Definition: trilinos_mixed_generic_criteria.h:102
BaseType::ConvergenceVariableListType ConvergenceVariableListType
Definition: trilinos_mixed_generic_criteria.h:60
MixedGenericCriteria< TSparseSpace, TDenseSpace > BaseType
Definition: trilinos_mixed_generic_criteria.h:50
KRATOS_CLASS_POINTER_DEFINITION(TrilinosMixedGenericCriteria)
BaseType::TDataType TDataType
Definition: trilinos_mixed_generic_criteria.h:52
BaseType::TSystemMatrixType TSystemMatrixType
Definition: trilinos_mixed_generic_criteria.h:56
TrilinosMixedGenericCriteria(const ConvergenceVariableListType &rConvergenceVariablesList)
Constructor.
Definition: trilinos_mixed_generic_criteria.h:76
BaseType::TSystemVectorType TSystemVectorType
Definition: trilinos_mixed_generic_criteria.h:58
~TrilinosMixedGenericCriteria() override
Destructor.
Definition: trilinos_mixed_generic_criteria.h:81
BaseType::KeyType KeyType
Definition: trilinos_mixed_generic_criteria.h:62
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
#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
n_dofs
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:151
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
int counter
Definition: script_THERMAL_CORRECT.py:218
A
Definition: sensitivityMatrix.py:70
integer i
Definition: TensorModule.f:17