MoCSI API Reference
Loading...
Searching...
No Matches
ElementManager.h
Go to the documentation of this file.
1#ifndef ELEMENT_MANAGER_H
2#define ELEMENT_MANAGER_H
3
4#include <algorithm>
5#include <memory>
6#include <string>
7#include <vector>
8
9#include "GenericElement.h"
10#include "GenericMatrix.h"
11#include "MatrixFactory.h"
12#include "OneDimLinearElement.h"
13
18template <typename T>
20{
21 private:
22 std::vector<std::unique_ptr<GenericElement<T>>> m_interior_cells{};
23 std::vector<std::unique_ptr<GenericElement<T>>> m_boundary_cells{};
24
25 void sortCells(std::vector<std::unique_ptr<GenericElement<T>>>& all_cells, int size);
26 void flushMatrices();
27
28 public:
29 // It only makes sense for them to be public as I need to get references to these objects.
30 std::shared_ptr<GenericMatrix<T>> m_capacitance_matrix{};
31 std::shared_ptr<GenericMatrix<T>> m_stiffness_matrix{};
32 std::valarray<T> m_forcing_vector{};
33
34 ElementManager(int size, std::shared_ptr<MatrixFactory<T>> matrix_factory);
35 ElementManager(int size, std::vector<std::unique_ptr<GenericElement<T>>>& all_cells,
36 std::shared_ptr<MatrixFactory<T>> matrix_factory);
37
39 void setCells(std::vector<std::unique_ptr<GenericElement<T>>>& all_cells);
40};
41
45template <typename T>
47 : m_capacitance_matrix(matrix_factory->createShared()),
48 m_stiffness_matrix(matrix_factory->createShared())
49{
50 m_capacitance_matrix->buildAtRuntime(size, size);
51 m_stiffness_matrix->buildAtRuntime(size, size);
52 m_forcing_vector.resize(size);
53}
54
60template <typename T>
62 std::vector<std::unique_ptr<GenericElement<T>>>& all_cells,
63 std::shared_ptr<MatrixFactory<T>> matrix_factory)
64{
65 if (size == -1)
66 {
67 std::cerr << "[ElementManager]: Grid initialization failed, no matrix size could be retrived!\n";
68 throw std::runtime_error(static_cast<std::string>("[ElementManager]: Grid initialization failed, no matrix size could be retrived!"));
69 }
70
71 m_capacitance_matrix = matrix_factory->createShared();
72 m_stiffness_matrix = matrix_factory->createShared();
73
74 // CSR build-up needs to be postponed until elements could be registered.
75 if ((m_capacitance_matrix->getMatrixType() == MatrixType::Dense)
76 && (m_stiffness_matrix->getMatrixType() == MatrixType::Dense))
77 {
78 m_capacitance_matrix->buildAtRuntime(size, size);
79 m_stiffness_matrix->buildAtRuntime(size, size);
80 }
81 else if (m_capacitance_matrix->getMatrixType() == MatrixType::Dense
82 || m_stiffness_matrix->getMatrixType() == MatrixType::Dense)
83 {
84 // This case should never be accessed, as both matrices are created together in
85 std::cerr
86 << "[ElementManager]: Non-supported mixing of dense and sparse matrix types!\nPleas use "
87 "either just dense or just sparse matrices for the global matrices.\n";
88 throw std::runtime_error(
89 "[ElementManager]: Non-supported mixing of dense and sparse matrix types!\nPleas use "
90 "either just dense or just sparse matrices for the global matrices.");
91 }
92 m_forcing_vector.resize(size);
93 sortCells(all_cells, size);
94}
95
103template <typename T>
104void ElementManager<T>::sortCells(std::vector<std::unique_ptr<GenericElement<T>>>& all_cells,
105 int dim)
106{
107 // To build up a CSR matrix, the elements have to first be registered.
108 if (m_capacitance_matrix->getMatrixType() == MatrixType::Csr
109 && m_stiffness_matrix->getMatrixType() == MatrixType::Csr)
110 {
111 for (auto& cell_ptr : all_cells)
112 {
113 // All elements save the position to their entries in the matrix through reference
114 // wrappers.
115 cell_ptr->registerElementsCsr(*m_capacitance_matrix, *m_stiffness_matrix);
116 if (cell_ptr->isBoundary())
117 {
118 for (auto& [key, boundary_ptr] : cell_ptr->getBoundaryCondition())
119 {
120 boundary_ptr->registerElementsCsr(*m_capacitance_matrix, *m_stiffness_matrix);
121 }
122 }
123 }
124 m_capacitance_matrix->buildAtRuntime(dim, dim);
125 m_stiffness_matrix->buildAtRuntime(dim, dim);
126 }
127 else if (m_capacitance_matrix->getMatrixType() == MatrixType::Csr
128 || m_stiffness_matrix->getMatrixType() == MatrixType::Csr)
129 {
130 // This case should never be accessed, as both matrices are created together in
131 std::cerr
132 << "[ElementManager] Non-supported mixing of dense and sparse matrix types!\nPleas use "
133 "either just dense or just sparse matrices for the global matrices.\n";
134 throw std::runtime_error(
135 "[ElementManager] Non-supported mixing of dense and sparse matrix types!\nPleas use "
136 "either just dense or just sparse matrices for the global matrices.\n");
137 }
138 for (auto& cell_ptr : all_cells)
139 {
140 // All elements save the position to their entries in the matrix through reference wrappers.
141 cell_ptr->getReferenceToElements(*m_capacitance_matrix, *m_stiffness_matrix,
142 m_forcing_vector);
143 if (cell_ptr->isBoundary())
144 {
145 for (auto& [key, boundary_ptr] : cell_ptr->getBoundaryCondition())
146 {
147 boundary_ptr->getReferenceToElements(*m_capacitance_matrix, *m_stiffness_matrix,
148 m_forcing_vector);
149 }
150 m_boundary_cells.push_back(std::move(cell_ptr));
151 // The original cell list is made invalid here. This should also be reflected in Grid
152 cell_ptr = nullptr;
153 }
154 else
155 {
156 m_interior_cells.push_back(std::move(cell_ptr));
157 // The original cell list is made invalid here. This should also be reflected in Grid
158 cell_ptr = nullptr;
159 }
160 }
161}
162
168template <typename T>
169void ElementManager<T>::setCells(std::vector<std::unique_ptr<GenericElement<T>>>& all_cells)
170{
171 sortCells(all_cells);
172}
173
178template <typename T>
180{
181 m_stiffness_matrix->zeroMatrix();
182 m_capacitance_matrix->zeroMatrix();
183 m_forcing_vector *= 0;
184}
185
190template <typename T>
192{
193 flushMatrices();
194 for (auto& interior_cell_ptr : m_interior_cells)
195 {
196 interior_cell_ptr->writeMatrixVectorBlock();
197 }
198 for (auto& boundary_cell_ptr : m_boundary_cells)
199 {
200 boundary_cell_ptr->writeBoundaryCondition();
201 }
202}
203
204#endif
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:37
void buildAtRuntime()
Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by...
Definition CsrMatrix.h:147
Class that manages the building of the global capacitance and stiffness matrix and forcing vector fro...
Definition ElementManager.h:20
ElementManager(int size, std::shared_ptr< MatrixFactory< T > > matrix_factory)
Constructor for cases, where the cell list will be set later.
Definition ElementManager.h:46
std::shared_ptr< GenericMatrix< T > > m_capacitance_matrix
Definition ElementManager.h:30
std::valarray< T > m_forcing_vector
Definition ElementManager.h:32
void setCells(std::vector< std::unique_ptr< GenericElement< T > > > &all_cells)
Does the cell sorting for the cases where the ElementManager has been default constructed.
Definition ElementManager.h:169
std::shared_ptr< GenericMatrix< T > > m_stiffness_matrix
Definition ElementManager.h:31
void assembleFemMatrices()
Calls all the element matrix creation function to assemble the global FEM matrices and vectors.
Definition ElementManager.h:191
std::pair< int, int > size() const
Function that returns the size of the matrix as a pair in (rows, columns) format.
Definition GenericMatrix.h:120