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_dof_updater.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
11 //
12 
13 #if !defined(KRATOS_TRILINOS_DOF_UPDATER_H_INCLUDED )
14 #define KRATOS_TRILINOS_DOF_UPDATER_H_INCLUDED
15 
16 // Project includes
17 #include "includes/define.h"
18 #include "includes/model_part.h"
19 #include "utilities/dof_updater.h"
20 
21 namespace Kratos
22 {
25 
28 
30 
37 template< class TSparseSpace >
38 class TrilinosDofUpdater : public DofUpdater<TSparseSpace>
39 {
40 public:
43 
46 
50 
54 
57  DofUpdater<TSparseSpace>()
58  {}
59 
61  TrilinosDofUpdater(TrilinosDofUpdater const& rOther) = delete;
62 
64  ~TrilinosDofUpdater() override {}
65 
68 
72 
74 
80  typename BaseType::UniquePointer Create() const override
81  {
82  return Kratos::make_unique<TrilinosDofUpdater>();
83  }
84 
86 
97  void Initialize(
98  const DofsArrayType& rDofSet,
99  const SystemVectorType& rDx) override
100  {
101  int system_size = TSparseSpace::Size(rDx);
102  int number_of_dofs = rDofSet.size();
103  std::vector< int > index_array(number_of_dofs);
104 
105  // filling the array with the global ids
106  unsigned int counter = 0;
107  for(typename DofsArrayType::const_iterator i_dof = rDofSet.begin() ; i_dof != rDofSet.end() ; ++i_dof)
108  {
109  int id = i_dof->EquationId();
110  if( id < system_size )
111  {
112  index_array[counter++] = id;
113  }
114  }
115 
116  std::sort(index_array.begin(),index_array.end());
117  std::vector<int>::iterator new_end = std::unique(index_array.begin(),index_array.end());
118  index_array.resize(new_end - index_array.begin());
119 
120  int check_size = -1;
121  int tot_update_dofs = index_array.size();
122  rDx.Comm().SumAll(&tot_update_dofs,&check_size,1);
123  if ( (check_size < system_size) && (rDx.Comm().MyPID() == 0) )
124  {
125  std::stringstream msg;
126  msg << "Dof count is not correct. There are less dofs then expected." << std::endl;
127  msg << "Expected number of active dofs: " << system_size << ", dofs found: " << check_size << std::endl;
128  KRATOS_ERROR << msg.str();
129  }
130 
131  // defining a map as needed
132  Epetra_Map dof_update_map(-1,index_array.size(), &(*(index_array.begin())),0,rDx.Comm() );
133 
134  // defining the import instance
135  std::unique_ptr<Epetra_Import> p_dof_import(new Epetra_Import(dof_update_map,rDx.Map()));
136  mpDofImport.swap(p_dof_import);
137 
138  mImportIsInitialized = true;
139  }
140 
142  void Clear() override
143  {
144  mpDofImport.reset();
145  mImportIsInitialized = false;
146  }
147 
149 
156  DofsArrayType& rDofSet,
157  const SystemVectorType& rDx) override
158  {
159  KRATOS_TRY;
160 
161  if (!mImportIsInitialized)
162  this->Initialize(rDofSet,rDx);
163 
164  int system_size = TSparseSpace::Size(rDx);
165 
166  // defining a temporary vector to gather all of the values needed
167  Epetra_Vector local_dx( mpDofImport->TargetMap() );
168 
169  // importing in the new temp vector the values
170  int ierr = local_dx.Import(rDx,*mpDofImport,Insert) ;
171  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found while trying to import Dx." << std::endl;
172 
173  int num_dof = rDofSet.size();
174 
175  // performing the update
176  #pragma omp parallel for
177  for(int i = 0; i < num_dof; ++i) {
178  auto it_dof = rDofSet.begin() + i;
179 
180  if (it_dof->IsFree()) {
181  int global_id = it_dof->EquationId();
182  if(global_id < system_size) {
183  double dx_i = local_dx[mpDofImport->TargetMap().LID(global_id)];
184  it_dof->GetSolutionStepValue() += dx_i;
185  }
186  }
187  }
188 
189  KRATOS_CATCH("");
190  }
191 
195 
197  std::string Info() const override
198  {
199  std::stringstream buffer;
200  buffer << "TrilinosDofUpdater" ;
201  return buffer.str();
202  }
203 
205  void PrintInfo(std::ostream& rOStream) const override
206  {
207  rOStream << this->Info() << std::endl;
208  }
209 
211  void PrintData(std::ostream& rOStream) const override
212  {
213  rOStream << this->Info() << std::endl;
214  }
215 
217 
218 private:
219 
220  //@name Member Variables
222 
224  bool mImportIsInitialized = false;
225 
227  std::unique_ptr<Epetra_Import> mpDofImport = nullptr;
228 
230 
231 }; // Class TrilinosDofUpdater
232 
236 
238 template< class TSparseSpace >
239 inline std::istream& operator >> (
240  std::istream& rIStream,
242 {
243  return rIStream;
244 }
245 
247 template< class TSparseSpace >
248 inline std::ostream& operator << (
249  std::ostream& rOStream,
251 {
252  rThis.PrintInfo(rOStream);
253  rOStream << std::endl;
254  rThis.PrintData(rOStream);
255 
256  return rOStream;
257 }
258 
260 
262 
263 } // namespace Kratos.
264 
265 #endif // KRATOS_TRILINOS_DOF_UPDATER_H_INCLUDED defined
Utility class to update the values of degree of freedom (Dof) variables after solving the system.
Definition: dof_updater.h:40
typename TSparseSpace::VectorType SystemVectorType
Definition: dof_updater.h:51
PointerVectorSet< DofType > DofsArrayType
Definition: dof_updater.h:49
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
boost::indirect_iterator< typename TContainerType::const_iterator > const_iterator
Definition: pointer_vector_set.h:96
iterator end()
Returns an iterator pointing to the end of the container.
Definition: pointer_vector_set.h:314
Utility class to update the values of degree of freedom (Dof) variables after solving the system.
Definition: trilinos_dof_updater.h:39
TrilinosDofUpdater()
Default constructor.
Definition: trilinos_dof_updater.h:56
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: trilinos_dof_updater.h:205
std::string Info() const override
Turn back information as a string.
Definition: trilinos_dof_updater.h:197
void UpdateDofs(DofsArrayType &rDofSet, const SystemVectorType &rDx) override
Calculate new values for the problem's degrees of freedom using the update vector rDx.
Definition: trilinos_dof_updater.h:155
TrilinosDofUpdater & operator=(TrilinosDofUpdater const &rOther)=delete
Deleted assignment operator.
~TrilinosDofUpdater() override
Destructor.
Definition: trilinos_dof_updater.h:64
TrilinosDofUpdater(TrilinosDofUpdater const &rOther)=delete
Deleted copy constructor.
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: trilinos_dof_updater.h:211
BaseType::UniquePointer Create() const override
Create a new instance of this class.
Definition: trilinos_dof_updater.h:80
KRATOS_CLASS_POINTER_DEFINITION(TrilinosDofUpdater)
Pointer definition of TrilinosDofUpdater.
void Initialize(const DofsArrayType &rDofSet, const SystemVectorType &rDx) override
Initialize the DofUpdater in preparation for a subsequent UpdateDofs call.
Definition: trilinos_dof_updater.h:97
void Clear() override
Free internal storage to reset the instance and/or optimize memory consumption.
Definition: trilinos_dof_updater.h:142
#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
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::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
int counter
Definition: script_THERMAL_CORRECT.py:218
integer i
Definition: TensorModule.f:17