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_assembling_utilities.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: Vicente Mataix Ferrandiz
11 //
12 
13 #pragma once
14 
15 // System includes
16 
17 // External includes
18 
19 // Project includes
20 #include "trilinos_space.h"
21 
22 namespace Kratos
23 {
24 
27 
31 
35 
39 
43 
51 {
52 public:
55 
58 
61 
64 
67 
69  using IndexType = std::size_t;
70 
72  using SizeType = std::size_t;
73 
77 
80 
84 
88 
96  inline static void AssembleRelationMatrixT(
97  MatrixType& rT,
98  const Matrix& rTContribution,
99  const std::vector<std::size_t>& rSlaveEquationId,
100  const std::vector<std::size_t>& rMasterEquationId
101  )
102  {
103  const unsigned int system_size = TrilinosSparseSpaceType::Size1(rT);
104 
105  // Count active indices
106  int slave_active_indices = 0;
107  for (unsigned int i = 0; i < rSlaveEquationId.size(); i++) {
108  if (rSlaveEquationId[i] < system_size) {
109  ++slave_active_indices;
110  }
111  }
112  int master_active_indices = 0;
113  for (unsigned int i = 0; i < rMasterEquationId.size(); i++) {
114  if (rMasterEquationId[i] < system_size) {
115  ++master_active_indices;
116  }
117  }
118 
119  if (slave_active_indices > 0 && master_active_indices > 0) {
120  std::vector<int> indices(master_active_indices);
121  std::vector<double> values(master_active_indices);
122 
123  // Fill Epetra vectors
124  for (unsigned int i = 0; i < rSlaveEquationId.size(); i++) {
125  if (rSlaveEquationId[i] < system_size) {
126  const int current_global_row = rSlaveEquationId[i];
127 
128  unsigned int loc_j = 0;
129  for (unsigned int j = 0; j < rMasterEquationId.size(); j++) {
130  if (rMasterEquationId[j] < system_size) {
131  indices[loc_j] = rMasterEquationId[j];
132  values[loc_j] = rTContribution(i, j);
133  ++loc_j;
134  }
135  }
136 
137  const int ierr = rT.SumIntoGlobalValues(current_global_row, master_active_indices, values.data(), indices.data());
138  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
139  }
140  }
141  }
142  }
143 
150  inline static void AssembleConstantVector(
151  VectorType& rC,
152  const Vector& rConstantContribution,
153  const std::vector<std::size_t>& rSlaveEquationId
154  )
155  {
156  const unsigned int system_size = TrilinosSparseSpaceType::Size(rC);
157 
158  // Count active indices
159  unsigned int slave_active_indices = 0;
160  for (unsigned int i = 0; i < rSlaveEquationId.size(); i++)
161  if (rSlaveEquationId[i] < system_size)
162  ++slave_active_indices;
163 
164  if (slave_active_indices > 0) {
165  // Size Epetra vectors
166  Epetra_IntSerialDenseVector indices(slave_active_indices);
167  Epetra_SerialDenseVector values(slave_active_indices);
168 
169  // Fill Epetra vectors
170  unsigned int loc_i = 0;
171  for (unsigned int i = 0; i < rSlaveEquationId.size(); i++) {
172  if (rSlaveEquationId[i] < system_size) {
173  indices[loc_i] = rSlaveEquationId[i];
174  values[loc_i] = rConstantContribution[i];
175  ++loc_i;
176  }
177  }
178 
179  const int ierr = rC.SumIntoGlobalValues(indices, values);
180  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
181  }
182  }
183 
190  static inline void SetGlobalValue(
191  VectorType& rX,
192  IndexType i,
193  const double Value
194  )
195  {
196  Epetra_IntSerialDenseVector indices(1);
197  Epetra_SerialDenseVector values(1);
198  indices[0] = i;
199  values[0] = Value;
200  int ierr = rX.ReplaceGlobalValues(indices, values);
201  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
202 
203  ierr = rX.GlobalAssemble(Insert,true); //Epetra_CombineMode mode=Add);
204  KRATOS_ERROR_IF(ierr < 0) << "Epetra failure when attempting to insert value in function SetValue" << std::endl;
205  }
206 
214  VectorType& rX,
215  IndexType i,
216  const double Value
217  )
218  {
219  Epetra_IntSerialDenseVector indices(1);
220  Epetra_SerialDenseVector values(1);
221  indices[0] = i;
222  values[0] = Value;
223  const int ierr = rX.ReplaceGlobalValues(indices, values);
224  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
225  }
226 
233  static inline void SetLocalValue(
234  VectorType& rX,
235  IndexType i,
236  const double Value
237  )
238  {
239  int ierr = rX.ReplaceMyValue(static_cast<int>(i), 0, Value);
240  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
241  ierr = rX.GlobalAssemble(Insert,true); //Epetra_CombineMode mode=Add);
242  KRATOS_ERROR_IF(ierr < 0) << "Epetra failure when attempting to insert value in function SetValue" << std::endl;
243  }
244 
252  VectorType& rX,
253  IndexType i,
254  const double Value
255  )
256  {
257  const int ierr = rX.ReplaceMyValue(static_cast<int>(i), 0, Value);
258  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
259  }
260 
268  static inline void SetGlobalValue(
269  MatrixType& rA,
270  IndexType i,
271  IndexType j,
272  const double Value
273  )
274  {
275  std::vector<double> values(1, Value);
276  std::vector<int> indices(1, j);
277 
278  int ierr = rA.ReplaceGlobalValues(static_cast<int>(i), 1, values.data(), indices.data());
279  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
280 
281  ierr = rA.GlobalAssemble();
282  KRATOS_ERROR_IF(ierr < 0) << "Epetra failure when attempting to insert value in function SetValue" << std::endl;
283  }
284 
293  MatrixType& rA,
294  IndexType i,
295  IndexType j,
296  const double Value
297  )
298  {
299  std::vector<double> values(1, Value);
300  std::vector<int> indices(1, j);
301 
302  const int ierr = rA.ReplaceGlobalValues(static_cast<int>(i), 1, values.data(), indices.data());
303  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
304  }
305 
313  static inline void SetLocalValue(
314  MatrixType& rA,
315  IndexType i,
316  IndexType j,
317  const double Value
318  )
319  {
320  std::vector<double> values(1, Value);
321  std::vector<int> indices(1, j);
322 
323  int ierr = rA.ReplaceMyValues(static_cast<int>(i), 1, values.data(), indices.data());
324  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
325 
326  ierr = rA.GlobalAssemble();
327  KRATOS_ERROR_IF(ierr < 0) << "Epetra failure when attempting to insert value in function SetValue" << std::endl;
328  }
329 
338  MatrixType& rA,
339  IndexType i,
340  IndexType j,
341  const double Value
342  )
343  {
344  std::vector<double> values(1, Value);
345  std::vector<int> indices(1, j);
346 
347  const int ierr = rA.ReplaceMyValues(static_cast<int>(i), 1, values.data(), indices.data());
348  KRATOS_ERROR_IF(ierr != 0) << "Epetra failure found" << std::endl;
349  }
350 
354 
358 
362 
367  virtual std::string Info() const
368  {
369  return "TrilinosAssemblingUtilities";
370  }
371 
376  virtual void PrintInfo(std::ostream& rOStream) const
377  {
378  rOStream << "TrilinosAssemblingUtilities";
379  }
380 
385  virtual void PrintData(std::ostream& rOStream) const
386  {
387  }
388 
390 private:
393 
395  TrilinosAssemblingUtilities & operator=(TrilinosAssemblingUtilities const& rOther);
396 
399 
401 }; // Class TrilinosAssemblingUtilities
402 
404 
405 } // namespace Kratos.
The Trilinos assembling utilities.
Definition: trilinos_assembling_utilities.h:51
static void AssembleRelationMatrixT(MatrixType &rT, const Matrix &rTContribution, const std::vector< std::size_t > &rSlaveEquationId, const std::vector< std::size_t > &rMasterEquationId)
Assembles the relation matrix T of the system with MPC.
Definition: trilinos_assembling_utilities.h:96
static void SetLocalValueWithoutGlobalAssembly(VectorType &rX, IndexType i, const double Value)
Sets a value in a vector (local without global assembly)
Definition: trilinos_assembling_utilities.h:251
TrilinosSparseSpaceType::VectorType VectorType
Definition of the vector type.
Definition: trilinos_assembling_utilities.h:66
static void SetLocalValueWithoutGlobalAssembly(MatrixType &rA, IndexType i, IndexType j, const double Value)
Sets a value in a matrix.
Definition: trilinos_assembling_utilities.h:337
std::size_t SizeType
Definition of the size type.
Definition: trilinos_assembling_utilities.h:72
virtual std::string Info() const
Turn back information as a string.
Definition: trilinos_assembling_utilities.h:367
static void SetLocalValue(VectorType &rX, IndexType i, const double Value)
Sets a value in a vector (local)
Definition: trilinos_assembling_utilities.h:233
TrilinosAssemblingUtilities()=delete
Default constructor.
KRATOS_CLASS_POINTER_DEFINITION(TrilinosAssemblingUtilities)
Pointer definition of TrilinosAssemblingUtilities.
std::size_t IndexType
Definition of the index type.
Definition: trilinos_assembling_utilities.h:69
TrilinosSparseSpaceType::MatrixType MatrixType
Definition of the matrix type.
Definition: trilinos_assembling_utilities.h:63
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: trilinos_assembling_utilities.h:376
static void SetLocalValue(MatrixType &rA, IndexType i, IndexType j, const double Value)
Sets a value in a matrix.
Definition: trilinos_assembling_utilities.h:313
static void SetGlobalValue(VectorType &rX, IndexType i, const double Value)
Sets a value in a vector.
Definition: trilinos_assembling_utilities.h:190
static void AssembleConstantVector(VectorType &rC, const Vector &rConstantContribution, const std::vector< std::size_t > &rSlaveEquationId)
Assembles the Constant vector of the system with MPC.
Definition: trilinos_assembling_utilities.h:150
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: trilinos_assembling_utilities.h:385
static void SetGlobalValueWithoutGlobalAssembly(VectorType &rX, IndexType i, const double Value)
Sets a value in a vector.
Definition: trilinos_assembling_utilities.h:213
static void SetGlobalValueWithoutGlobalAssembly(MatrixType &rA, IndexType i, IndexType j, const double Value)
Sets a value in a matrix.
Definition: trilinos_assembling_utilities.h:292
static void SetGlobalValue(MatrixType &rA, IndexType i, IndexType j, const double Value)
Sets a value in a matrix.
Definition: trilinos_assembling_utilities.h:268
The space adapted for Trilinos vectors and matrices.
Definition: trilinos_space.h:75
TVectorType VectorType
Definition of the vector type.
Definition: trilinos_space.h:90
static IndexType Size1(MatrixType const &rM)
Returns number of rows of rM.
Definition: trilinos_space.h:186
TMatrixType MatrixType
Definition of the matrix type.
Definition: trilinos_space.h:87
static IndexType Size(const VectorType &rV)
Returns size of vector rV.
Definition: trilinos_space.h:175
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
list values
Definition: bombardelli_test.py:42
int j
Definition: quadrature.py:648
integer i
Definition: TensorModule.f:17