NuTo
Numerics Tool
DofMatrixSparseConvertEigen.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace NuTo
6 {
7 template <typename T>
8 inline int TotalRows(const DofMatrixSparse<T>& v, std::vector<DofType> dofs)
9 {
10  int total = 0;
11  for (auto dof : dofs)
12  total += v(dof, dof).rows();
13  return total;
14 }
15 
16 template <typename T>
17 inline int TotalCols(const DofMatrixSparse<T>& v, std::vector<DofType> dofs)
18 {
19  int total = 0;
20  for (auto dof : dofs)
21  total += v(dof, dof).cols();
22  return total;
23 }
24 
25 template <typename T>
26 inline int TotalNonZeros(const DofMatrixSparse<T>& v, std::vector<DofType> dofs)
27 {
28  int total = 0;
29  for (auto dof0 : dofs)
30  for (auto dof1 : dofs)
31  total += v(dof0, dof1).nonZeros();
32  return total;
33 }
34 
40 template <typename T>
41 inline Eigen::SparseMatrix<T> ToEigen(const DofMatrixSparse<T>& v, std::vector<DofType> dofs)
42 {
43  if (dofs.size() == 1)
44  return v(dofs.front(), dofs.front());
45 
46  std::vector<Eigen::Triplet<T>> triplets;
47  triplets.reserve(TotalNonZeros(v, dofs));
48  int startRow = 0;
49  for (auto dofRow : dofs)
50  {
51  int startCol = 0;
52  for (auto dofCol : dofs)
53  {
54  const auto& mat = v(dofRow, dofCol);
55  for (int k = 0; k < mat.outerSize(); ++k)
56  for (Eigen::SparseMatrix<double>::InnerIterator it(mat, k); it; ++it)
57  triplets.emplace_back(Eigen::Triplet<T>(it.row() + startRow, it.col() + startCol, it.value()));
58 
59  startCol += mat.cols();
60  }
61  startRow += v(dofRow, dofRow).rows();
62  }
63  Eigen::SparseMatrix<T> combined(TotalRows(v, dofs), TotalCols(v, dofs));
64  combined.setFromTriplets(triplets.begin(), triplets.end());
65  combined.makeCompressed();
66  return combined;
67 }
68 }
int TotalCols(const DofMatrixSparse< T > &v, std::vector< DofType > dofs)
Definition: DofMatrixSparseConvertEigen.h:17
int TotalNonZeros(const DofMatrixSparse< T > &v, std::vector< DofType > dofs)
Definition: DofMatrixSparseConvertEigen.h:26
int TotalRows(const DofMatrixSparse< T > &v, std::vector< DofType > dofs)
Definition: DofMatrixSparseConvertEigen.h:8
Eigen::SparseMatrix< T > ToEigen(const DofMatrixSparse< T > &v, std::vector< DofType > dofs)
export the dofs entries of a DofMatrixSparse to a Eigen::SparseMatrix
Definition: DofMatrixSparseConvertEigen.h:41
int v
Definition: Quad2DPatchTest.py:9
dof container that is also capable of performing calculations.
Definition: DofMatrixContainer.h:13
Definition: Exception.h:6