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.
kratos_space.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: Riccardo Rossi
11 //
12 //
13 
14 #if !defined(KRATOS_SPACE_H_INCLUDED )
15 #define KRATOS_SPACE_H_INCLUDED
16 
17 
18 
19 // System includes
20 #include <string>
21 #include <iostream>
22 #include <cstddef>
23 #include <numeric>
24 
25 
26 
27 
28 // External includes
29 
30 
31 // Project includes
32 #include "includes/define.h"
35 #include "utilities/dof_updater.h"
36 #include "containers/csr_matrix.h"
39 
42 
43 namespace Kratos
44 {
45 
48 
52 
53 template <class TDataType, class TMatrixType, class TVectorType>
54 class KratosSpace;
55 
56 typedef std::size_t IndexType;
57 
58 template <class TDataType>
60 
61 template <class TDataType>
63 
67 
71 
75 
77 
80 template<class TDataType, class TMatrixType, class TVectorType>
82 {
83 public:
86 
89 
90  typedef TDataType DataType;
91 
92  typedef TMatrixType MatrixType;
93 
94  typedef TVectorType VectorType;
95 
96  typedef std::size_t IndexType;
97 
98  typedef std::size_t SizeType;
99 
102 
104  typedef typename DofUpdaterType::UniquePointer DofUpdaterPointerType;
105 
109 
111 
113  {
114  }
115 
117 
118  virtual ~KratosSpace()
119  {
120  }
121 
122 
126 
127 
131 
133  {
134  return MatrixPointerType(new TMatrixType(0, 0));
135  }
136 
138  {
139  return VectorPointerType(new TVectorType(0));
140  }
141 
143 
144  static IndexType Size(VectorType const& rV)
145  {
146  return rV.size();
147  }
148 
150 
151  static IndexType Size1(MatrixType const& rM)
152  {
153  return rM.size1();
154  }
155 
157 
158  static IndexType Size2(MatrixType const& rM)
159  {
160  return rM.size2();
161  }
162 
164  // This version is needed in order to take one column of multi column solve from AMatrix matrix and pass it to an ublas vector
165  template<typename TColumnType>
166  static void GetColumn(unsigned int j, Matrix& rM, TColumnType& rX)
167  {
168  if (rX.size() != rM.size1())
169  rX.resize(rM.size1(), false);
170 
171  for (IndexType i = 0; i < rM.size1(); i++)
172  {
173  rX[i] = rM(i, j);
174  }
175  }
176 
177  // This version is needed in order to take one column of multi column solve from AMatrix matrix and pass it to an ublas vector
178  template<typename TColumnType>
179  static void SetColumn(unsigned int j, Matrix& rM, TColumnType& rX)
180  {
181  for (IndexType i = 0; i < rM.size1(); i++)
182  {
183  rM(i,j) = rX[i];
184  }
185  }
186 
187 
189  static void Copy(MatrixType const& rX, MatrixType& rY)
190  {
191  rY = rX;
192  }
193 
195 
196  static void Copy(VectorType const& rX, VectorType& rY)
197  {
198  rY = rX;
199  }
200 
202 
203  static TDataType Dot(VectorType const& rX, VectorType const& rY)
204  {
205  return rX.inner_prod(rY);
206  }
207 
208 
210 
211  static TDataType TwoNorm(VectorType const& rX)
212  {
213  return std::sqrt(rX.inner_prod(rX));
214  }
215 
216  static TDataType TwoNorm(const Matrix& rA) // Frobenious norm
217  {
218  return norm_frobenius(rA);
219  }
220 
221  //TODO: should use implementation of Norm Frobenius in CSR matrix, but it does not compile
222  static TDataType TwoNorm(const CsrMatrix<TDataType>& rA) // Frobenious norm
223  {
224  return rA.NormFrobenius();
225  }
226 
227  //TODO: should use implementation of NormFrobenius within DistributedCsrMatrix once available
228  static TDataType TwoNorm(const DistributedCsrMatrix<TDataType>& rA) // Frobenious norm
229  {
230  return rA.NormFrobenius();
231  }
232 
238  static TDataType JacobiNorm(const Matrix& rA)
239  {
240  TDataType aux_sum = IndexPartition<IndexType>(rA.size1())
242  {
243  TDataType row_sum = TDataType();
244  for (IndexType j=0; j<rA.size2(); ++j)
245  if (i != j)
246  row_sum += std::abs(rA(i,j));
247  return row_sum;
248  });
249  return aux_sum;
250  }
251 
252  static TDataType JacobiNorm(const CsrMatrix<TDataType>& rA)
253  {
254  TDataType aux_sum = TDataType();
256  {
257  IndexType row_begin = rA.index1_data()[i];
258  IndexType row_end = rA.index1_data()[i+1];
259  for(IndexType k = row_begin; k < row_end; ++k)
260  {
261  IndexType col = rA.index2_data()[k];
262  if(i != col)
263  AtomicAdd(aux_sum, std::abs(rA.value_data()[k]));
264  }
265  });
266  return aux_sum;
267  }
268 
269  static void Mult(const Matrix& rA, VectorType& rX, VectorType& rY)
270  {
271  axpy_prod(rA, rX, rY, true);
272  }
273 
274  static void Mult(const CsrMatrix<TDataType>& rA, VectorType& rX, VectorType& rY)
275  {
276  // rY = rA * rX
277  rA.SpMV(rY,rX);
278  }
279 
280  template< class TOtherMatrixType >
281  static void TransposeMult(TOtherMatrixType& rA, VectorType& rX, VectorType& rY)
282  {
283  // rY = rA^t * rX
284  rA.TransposeSpMV(rY,rX);
285  }
286 
288  {
289  return rA.index1_data()[i+1] - rA.index1_data()[i];
290  }
291 
292  //this function is only implemented for the non-distributed case
293  static inline void GraphNeighbors(IndexType i,
294  const CsrMatrix<TDataType>& rA,
295  std::vector<IndexType>& neighbors)
296  {
297  neighbors.clear();
298  IndexType row_begin = rA.index1_data()[i];
299  IndexType row_end = rA.index1_data()[i+1];
300  for(IndexType j=row_begin; j<row_end; ++j)
301  neighbors.push_back(rA.index1_data()[j]);
302  }
303 
304 
305  //********************************************************************
306  //checks if a multiplication is needed and tries to do otherwise
307 
308  static void InplaceMult(VectorType& rX, const double A)
309  {
310  rX *= A;
311  }
312 
313  //********************************************************************
314  //checks if a multiplication is needed and tries to do otherwise
315  //ATTENTION it is assumed no aliasing between rX and rY
316  // X = A*y;
317 
318  static void Assign(VectorType& rX, const double A, const VectorType& rY)
319  {
320  rX = rY;
321  rX *= A;
322  }
323 
324  //********************************************************************
325  //checks if a multiplication is needed and tries to do otherwise
326  //ATTENTION it is assumed no aliasing between rX and rY
327  // X += A*y;
328 
329  static void UnaliasedAdd(VectorType& rX, const double A, const VectorType& rY)
330  {
331  rX.Add(A,rY);
332  }
333 
334  //********************************************************************
335 
336  static void ScaleAndAdd(const double A, const VectorType& rX, const double B, const VectorType& rY, VectorType& rZ) // rZ = (A * rX) + (B * rY)
337  {
338  Assign(rZ, A, rX); //rZ = A*rX
339  UnaliasedAdd(rZ, B, rY); //rZ += B*rY
340  }
341 
342  static void ScaleAndAdd(const double A, const VectorType& rX, const double B, VectorType& rY) // rY = (A * rX) + (B * rY)
343  {
344  InplaceMult(rY, B);
345  UnaliasedAdd(rY, A, rX);
346  }
347 
348 
350 
351  static double RowDot(unsigned int i, MatrixType& rA, VectorType& rX)
352  {
353  return inner_prod(row(rA, i), rX);
354  }
355 
356 
357  static void SetValue(VectorType& rX, IndexType local_i, TDataType value)
358  {
359  rX[local_i] = value;
360  }
361 
363  static void Set(VectorType& rX, TDataType A)
364  {
365  rX.SetValue(A);
366  }
367 
368  static void Resize(MatrixType& rA, SizeType m, SizeType n)
369  {
370  rA.resize(m, n, false);
371  }
372 
374  {
375  pA->resize(m, n, false);
376  }
377 
378  static void Resize(VectorType& rX, SizeType n)
379  {
380  rX.resize(n, false);
381  }
382 
383  static void Resize(VectorPointerType& pX, SizeType n)
384  {
385  pX->resize(n, false);
386  }
387 
388  static void Clear(MatrixPointerType& pA)
389  {
390  pA->clear();
391  pA->resize(0, 0, false);
392  }
393 
394  static void Clear(VectorPointerType& pX)
395  {
396  pX->clear();
397  pX->resize(0, false);
398  }
399 
400  template<class TOtherMatrixType>
401  inline static void ResizeData(TOtherMatrixType& rA, SizeType m)
402  {
403  KRATOS_ERROR << "cannot be implemented with CsrMatrix" << std::endl;
404  }
405 
406  //TODO: remove
407  inline static void ResizeData(CsrMatrix<TDataType>& rA, SizeType m)
408  {
409  rA.value_data().resize(m);
410  std::fill(rA.value_data().begin(), rA.value_data().end(), TDataType());
411  }
412 
413  //TODO remove
414  inline static void ResizeData(VectorType& rX, SizeType m)
415  {
416  rX.resize(m, false);
417  std::fill(rX.begin(), rX.end(), TDataType());
418  }
419 
420  template<class TOtherMatrixType>
421  inline static void SetToZero(TOtherMatrixType& rA)
422  {
423  rA.SetValue(TDataType());
424  }
425 
426  inline static void SetToZero(CsrMatrix<TDataType>& rA)
427  {
428  rA.SetValue(TDataType());
429  }
430 
431  inline static void SetToZero(VectorType& rX)
432  {
433  rX.SetValue(TDataType());
434  }
435 
436  template<class TOtherMatrixType, class TEquationIdVectorType>
437  inline static void AssembleLHS(
438  MatrixType& A,
439  TOtherMatrixType& LHS_Contribution,
440  TEquationIdVectorType& EquationId
441  )
442  {
443  A.Assemble(A,LHS_Contribution,EquationId);
444  }
445 
449 
450 
454 
455 
459 
461 
462  virtual std::string Info() const
463  {
464  return "KratosSpace";
465  }
466 
468 
469  virtual void PrintInfo(std::ostream& rOStream) const
470  {
471  rOStream << "KratosSpace";
472  }
473 
475 
476  virtual void PrintData(std::ostream& rOStream) const
477  {
478  }
479 
480  //***********************************************************************
481 
482  inline static constexpr bool IsDistributed()
483  {
484  return false;
485  }
486 
487  //***********************************************************************
488 
489  inline static TDataType GetValue(const VectorType& x, IndexType LocalI)
490  {
491  return x[LocalI];
492  }
493  //***********************************************************************
494 
495  static void GatherValues(const VectorType& x, const std::vector<IndexType>& IndexArray, TDataType* pValues)
496  {
497  KRATOS_TRY
498 
499  for (IndexType i = 0; i < IndexArray.size(); i++)
500  pValues[i] = x[IndexArray[i]];
501 
502  KRATOS_CATCH("")
503  }
504 
505  template< class TOtherMatrixType >
506  static bool WriteMatrixMarketMatrix(const char* pFileName, /*const*/ TOtherMatrixType& rM, const bool Symmetric)
507  {
508  // Use full namespace in call to make sure we are not calling this function recursively
509  return Kratos::WriteMatrixMarketMatrix(pFileName, rM, Symmetric);
510  }
511 
512  template< class VectorType >
513  static bool WriteMatrixMarketVector(const char* pFileName, const VectorType& rV)
514  {
515  // Use full namespace in call to make sure we are not calling this function recursively
516  return Kratos::WriteMatrixMarketVector(pFileName, rV);
517  }
518 
520  {
522  return tmp.Create();
523  }
524 
528 
529 
531 
532 protected:
535 
536 
540 
541 
545 
546 
550 
551 
555 
556 
560 
561 
565 
566 
568 
569 private:
572 
573 
577 
578 
582 
586 
587 
591 
592 
596 
597 
601 
603  KratosSpace & operator=(KratosSpace const& rOther){};
604 
606  KratosSpace(KratosSpace const& rOther){};
607 
608 
610 
611 }; // Class KratosSpace
612 
613 
614 
616 
619 
620 
624 
625 
627 // inline std::istream& operator >> (std::istream& rIStream,
628 // KratosSpace& rThis);
629 
630 // /// output stream function
631 // inline std::ostream& operator << (std::ostream& rOStream,
632 // const KratosSpace& rThis)
633 // {
634 // rThis.PrintInfo(rOStream);
635 // rOStream << std::endl;
636 // rThis.PrintData(rOStream);
637 
638 // return rOStream;
639 // }
641 
642 
643 } // namespace Kratos.
644 
645 #endif // KRATOS_SPACE_H_INCLUDED defined
This class implements "serial" CSR matrix, including capabilities for FEM assembly.
Definition: csr_matrix.h:60
Kratos::span< TDataType > & value_data()
Definition: csr_matrix.h:273
IndexType size1() const
Definition: csr_matrix.h:240
void SetValue(const TDataType value)
Definition: csr_matrix.h:232
void SpMV(const TInputVectorType &x, TOutputVectorType &y) const
Definition: csr_matrix.h:411
TDataType NormFrobenius() const
Definition: csr_matrix.h:494
Kratos::span< IndexType > & index2_data()
Definition: csr_matrix.h:269
Kratos::span< IndexType > & index1_data()
Definition: csr_matrix.h:265
This class implements "serial" CSR matrix, including capabilities for FEM assembly.
Definition: distributed_csr_matrix.h:58
TDataType NormFrobenius() const
Definition: distributed_csr_matrix.h:559
Utility class to update the values of degree of freedom (Dof) variables after solving the system.
Definition: dof_updater.h:40
This class is useful for index iteration over containers.
Definition: parallel_utilities.h:451
void for_each(TUnaryFunction &&f)
Definition: parallel_utilities.h:514
Definition: amatrix_interface.h:41
Short class definition.
Definition: kratos_space.h:82
static TDataType GetValue(const VectorType &x, IndexType LocalI)
Definition: kratos_space.h:489
static void ScaleAndAdd(const double A, const VectorType &rX, const double B, const VectorType &rY, VectorType &rZ)
Definition: kratos_space.h:336
static void GraphNeighbors(IndexType i, const CsrMatrix< TDataType > &rA, std::vector< IndexType > &neighbors)
Definition: kratos_space.h:293
static void ResizeData(VectorType &rX, SizeType m)
Definition: kratos_space.h:414
Kratos::shared_ptr< TVectorType > VectorPointerType
Definition: kratos_space.h:101
virtual std::string Info() const
Turn back information as a string.
Definition: kratos_space.h:462
static void GatherValues(const VectorType &x, const std::vector< IndexType > &IndexArray, TDataType *pValues)
Definition: kratos_space.h:495
static void UnaliasedAdd(VectorType &rX, const double A, const VectorType &rY)
Definition: kratos_space.h:329
static void Assign(VectorType &rX, const double A, const VectorType &rY)
Definition: kratos_space.h:318
static IndexType Size2(MatrixType const &rM)
return number of columns of rM
Definition: kratos_space.h:158
static void ResizeData(CsrMatrix< TDataType > &rA, SizeType m)
Definition: kratos_space.h:407
TVectorType VectorType
Definition: kratos_space.h:94
static IndexType Size(VectorType const &rV)
return size of vector rV
Definition: kratos_space.h:144
static void SetValue(VectorType &rX, IndexType local_i, TDataType value)
Definition: kratos_space.h:357
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: kratos_space.h:469
Kratos::shared_ptr< TMatrixType > MatrixPointerType
Definition: kratos_space.h:100
DofUpdater< KratosSpace< TDataType, TMatrixType, TVectorType > > DofUpdaterType
Definition: kratos_space.h:103
static DofUpdaterPointerType CreateDofUpdater()
Definition: kratos_space.h:519
static VectorPointerType CreateEmptyVectorPointer()
Definition: kratos_space.h:137
static SizeType GraphDegree(IndexType i, const CsrMatrix< TDataType > &rA)
Definition: kratos_space.h:287
TMatrixType MatrixType
Definition: kratos_space.h:92
static void TransposeMult(TOtherMatrixType &rA, VectorType &rX, VectorType &rY)
Definition: kratos_space.h:281
static TDataType TwoNorm(const CsrMatrix< TDataType > &rA)
Definition: kratos_space.h:222
static constexpr bool IsDistributed()
Definition: kratos_space.h:482
static void SetToZero(CsrMatrix< TDataType > &rA)
Definition: kratos_space.h:426
static void Copy(MatrixType const &rX, MatrixType &rY)
rY = rX
Definition: kratos_space.h:189
static void Set(VectorType &rX, TDataType A)
rX = A
Definition: kratos_space.h:363
static void SetToZero(TOtherMatrixType &rA)
Definition: kratos_space.h:421
static TDataType Dot(VectorType const &rX, VectorType const &rY)
rX * rY
Definition: kratos_space.h:203
virtual ~KratosSpace()
Destructor.
Definition: kratos_space.h:118
static void Resize(MatrixPointerType &pA, SizeType m, SizeType n)
Definition: kratos_space.h:373
static TDataType TwoNorm(const Matrix &rA)
Definition: kratos_space.h:216
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: kratos_space.h:476
static void Clear(MatrixPointerType &pA)
Definition: kratos_space.h:388
static bool WriteMatrixMarketMatrix(const char *pFileName, TOtherMatrixType &rM, const bool Symmetric)
Definition: kratos_space.h:506
KRATOS_CLASS_POINTER_DEFINITION(KratosSpace)
Pointer definition of KratosSpace.
static void Mult(const Matrix &rA, VectorType &rX, VectorType &rY)
Definition: kratos_space.h:269
static void Clear(VectorPointerType &pX)
Definition: kratos_space.h:394
static TDataType TwoNorm(VectorType const &rX)
||rX||2
Definition: kratos_space.h:211
static void Resize(VectorPointerType &pX, SizeType n)
Definition: kratos_space.h:383
static double RowDot(unsigned int i, MatrixType &rA, VectorType &rX)
rA[i] * rX
Definition: kratos_space.h:351
static void Mult(const CsrMatrix< TDataType > &rA, VectorType &rX, VectorType &rY)
Definition: kratos_space.h:274
static void SetColumn(unsigned int j, Matrix &rM, TColumnType &rX)
Definition: kratos_space.h:179
static TDataType TwoNorm(const DistributedCsrMatrix< TDataType > &rA)
Definition: kratos_space.h:228
DofUpdaterType::UniquePointer DofUpdaterPointerType
Definition: kratos_space.h:104
static void InplaceMult(VectorType &rX, const double A)
Definition: kratos_space.h:308
std::size_t SizeType
Definition: kratos_space.h:98
static IndexType Size1(MatrixType const &rM)
return number of rows of rM
Definition: kratos_space.h:151
static void Resize(MatrixType &rA, SizeType m, SizeType n)
Definition: kratos_space.h:368
static void GetColumn(unsigned int j, Matrix &rM, TColumnType &rX)
rXi = rMij
Definition: kratos_space.h:166
static void ScaleAndAdd(const double A, const VectorType &rX, const double B, VectorType &rY)
Definition: kratos_space.h:342
static TDataType JacobiNorm(const CsrMatrix< TDataType > &rA)
Definition: kratos_space.h:252
static bool WriteMatrixMarketVector(const char *pFileName, const VectorType &rV)
Definition: kratos_space.h:513
KratosSpace()
Default constructor.
Definition: kratos_space.h:112
static void AssembleLHS(MatrixType &A, TOtherMatrixType &LHS_Contribution, TEquationIdVectorType &EquationId)
Definition: kratos_space.h:437
std::size_t IndexType
Definition: kratos_space.h:96
static void SetToZero(VectorType &rX)
Definition: kratos_space.h:431
TDataType DataType
Definition: kratos_space.h:90
static TDataType JacobiNorm(const Matrix &rA)
Definition: kratos_space.h:238
static void Copy(VectorType const &rX, VectorType &rY)
rY = rX
Definition: kratos_space.h:196
static void Resize(VectorType &rX, SizeType n)
Definition: kratos_space.h:378
static MatrixPointerType CreateEmptyMatrixPointer()
Definition: kratos_space.h:132
static void ResizeData(TOtherMatrixType &rA, SizeType m)
Definition: kratos_space.h:401
utility function to do a sum reduction
Definition: reduction_utilities.h:68
Provides a SystemVector which implements FEM assemble capabilities, as well as some vector operations...
Definition: system_vector.h:61
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
#define KRATOS_ERROR
Definition: exception.h:161
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
void AtomicAdd(TDataType &target, const TDataType &value)
Definition: atomic_utilities.h:55
std::shared_ptr< T > shared_ptr
Definition: smart_pointers.h:27
bool WriteMatrixMarketVector(const char *FileName, VectorType &V)
Definition: matrix_market_interface.h:539
TExpression1Type::data_type inner_prod(AMatrix::MatrixExpression< TExpression1Type, TCategory1 > const &First, AMatrix::MatrixExpression< TExpression2Type, TCategory2 > const &Second)
Definition: amatrix_interface.h:592
bool WriteMatrixMarketMatrix(const char *FileName, CompressedMatrixType &M, bool Symmetric)
Definition: matrix_market_interface.h:308
AMatrix::MatrixRow< const TExpressionType > row(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression, std::size_t RowIndex)
Definition: amatrix_interface.h:649
TExpressionType::data_type norm_frobenius(AMatrix::MatrixExpression< TExpressionType, TCategory > const &TheExpression)
Definition: amatrix_interface.h:687
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98
int n
manufactured solution and derivatives (u=0 at z=0 dudz=0 at z=domain_height)
Definition: ode_solve.py:402
int k
Definition: quadrature.py:595
int j
Definition: quadrature.py:648
int m
Definition: run_marine_rain_substepping.py:8
A
Definition: sensitivityMatrix.py:70
x
Definition: sensitivityMatrix.py:49
B
Definition: sensitivityMatrix.py:76
integer i
Definition: TensorModule.f:17