NuTo
Numerics Tool
DofMatrixContainer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <vector>
5 #include <eigen3/Eigen/Core>
7 
8 namespace NuTo
9 {
10 
12 template <typename T>
14 {
15 public:
17  {
18  return this->mData[std::make_pair(d0, d1)];
19  }
20 
21  const T& operator()(DofType d0, DofType d1) const
22  {
23  return this->mData.at(std::make_pair(d0, d1));
24  }
25 
28  {
29  for (auto& entry : rhs.mData)
30  {
31  if (mData.find(entry.first) != mData.end())
32  mData[entry.first] += entry.second;
33  else
34  mData[entry.first] = entry.second;
35  }
36  return *this;
37  }
38 
40  {
41  for (auto& entry : mData)
42  entry.second *= scalar;
43  return *this;
44  }
45 
47  {
48  lhs += rhs;
49  return lhs;
50  }
51 
52  friend DofMatrixContainer operator*(DofMatrixContainer lhs, double scalar)
53  {
54  lhs *= scalar;
55  return lhs;
56  }
57 
61  void AddScaled(const DofMatrixContainer& rhs, double scalar)
62  {
63  for (auto& entry : rhs.mData)
64  {
65  if (mData.find(entry.first) != mData.end())
66  mData[entry.first] += entry.second * scalar;
67  else
68  mData[entry.first] = entry.second * scalar;
69  }
70  }
71 
72  friend std::ostream& operator<<(std::ostream& out, const DofMatrixContainer<T>& dofMatrix)
73  {
74  for (auto& entry : dofMatrix.mData)
75  {
76  auto& dofs = entry.first;
77  out << "=== " << dofs.first.GetName() << " " << dofs.second.GetName() << " ===" << std::endl;
78  out << entry.second << std::endl;
79  }
80  out << "====" << std::endl;
81  return out;
82  }
83 
84  std::vector<DofType> DofTypes() const
85  {
86  std::vector<DofType> dofTypes;
87  for (const auto& data : mData)
88  {
89  AddUnique(&dofTypes, data.first.first);
90  AddUnique(&dofTypes, data.first.second);
91  }
92  return dofTypes;
93  }
94 
95 private:
96  static void AddUnique(std::vector<DofType>* dofTypes, DofType dofType)
97  {
98  bool isNew = true;
99  for (const auto& d : *dofTypes)
100  if (d.Id() == dofType.Id())
101  {
102  isNew = false;
103  break;
104  }
105  if (isNew)
106  dofTypes->push_back(dofType);
107  }
108 
109  using DofPair = std::pair<DofType, DofType>;
110  struct CompareDofPairs
111  {
112  bool operator()(const DofPair& a, const DofPair& b) const
113  {
114  // check for equiv(a.first, b.first)
115  if (a.first.Id() == b.first.Id())
116  return a.second.Id() < b.second.Id();
117  else
118  return a.first.Id() < b.first.Id();
119  }
120  };
121  std::map<DofPair, T, CompareDofPairs> mData;
122 };
123 } /* NuTo */
const NuTo::DofType d("...", 1)
T & operator()(DofType d0, DofType d1)
Definition: DofMatrixContainer.h:16
Definition: DofType.h:8
void AddScaled(const DofMatrixContainer &rhs, double scalar)
calculates (*this) += rhs * scalar
Definition: DofMatrixContainer.h:61
DofMatrixContainer & operator*=(double scalar)
Definition: DofMatrixContainer.h:39
int Id() const
Definition: UniqueId.h:18
std::vector< DofType > DofTypes() const
Definition: DofMatrixContainer.h:84
friend DofMatrixContainer operator+(DofMatrixContainer lhs, const DofMatrixContainer &rhs)
Definition: DofMatrixContainer.h:46
const T & operator()(DofType d0, DofType d1) const
Definition: DofMatrixContainer.h:21
DofMatrixContainer & operator+=(const DofMatrixContainer &rhs)
performs uninitialized addition that resizes the data to the length of rhs
Definition: DofMatrixContainer.h:27
dof container that is also capable of performing calculations.
Definition: DofMatrixContainer.h:13
Definition: Exception.h:6
rhs
Definition: SparseDirectSolverMKLDSS.py:46
friend DofMatrixContainer operator*(DofMatrixContainer lhs, double scalar)
Definition: DofMatrixContainer.h:52