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 throw std::runtime_error("Grid initialization failed, no matrix size could be retrived!\n");
68 }
69
70 m_capacitance_matrix = matrix_factory->createShared();
71 m_stiffness_matrix = matrix_factory->createShared();
72
73 // CSR build-up needs to be postponed until elements could be registered.
74 if ((m_capacitance_matrix->getMatrixType() == MatrixType::Dense)
75 && (m_stiffness_matrix->getMatrixType() == MatrixType::Dense))
76 {
77 m_capacitance_matrix->buildAtRuntime(size, size);
78 m_stiffness_matrix->buildAtRuntime(size, size);
79 }
80 else if (m_capacitance_matrix->getMatrixType() == MatrixType::Dense
81 || m_stiffness_matrix->getMatrixType() == MatrixType::Dense)
82 {
83 // This case should never be accessed, as both matrices are created together in
84 std::cerr
85 << "[ElementManager] Non-supported mixing of dense and sparse matrix types!\nPleas use "
86 "either just dense or just sparse matrices for the global matrices.\n";
87 throw std::runtime_error(
88 "[ElementManager] Non-supported mixing of dense and sparse matrix types!\nPleas use "
89 "either just dense or just sparse matrices for the global matrices.\n");
90 }
91 m_forcing_vector.resize(size);
92 sortCells(all_cells, size);
93}
94
102template <typename T>
103void ElementManager<T>::sortCells(std::vector<std::unique_ptr<GenericElement<T>>>& all_cells,
104 int dim)
105{
106 // To build up a CSR matrix, the elements have to first be registered.
107 if (m_capacitance_matrix->getMatrixType() == MatrixType::Csr
108 && m_stiffness_matrix->getMatrixType() == MatrixType::Csr)
109 {
110 for (auto& cell_ptr : all_cells)
111 {
112 // All elements save the position to their entries in the matrix through reference
113 // wrappers.
114 cell_ptr->registerElementsCsr(*m_capacitance_matrix, *m_stiffness_matrix);
115 if (cell_ptr->isBoundary())
116 {
117 for (auto& [key, boundary_ptr] : cell_ptr->getBoundaryCondition())
118 {
119 boundary_ptr->registerElementsCsr(*m_capacitance_matrix, *m_stiffness_matrix);
120 }
121 }
122 }
123 m_capacitance_matrix->buildAtRuntime(dim, dim);
124 m_stiffness_matrix->buildAtRuntime(dim, dim);
125 }
126 else if (m_capacitance_matrix->getMatrixType() == MatrixType::Csr
127 || m_stiffness_matrix->getMatrixType() == MatrixType::Csr)
128 {
129 // This case should never be accessed, as both matrices are created together in
130 std::cerr
131 << "[ElementManager] Non-supported mixing of dense and sparse matrix types!\nPleas use "
132 "either just dense or just sparse matrices for the global matrices.\n";
133 throw std::runtime_error(
134 "[ElementManager] Non-supported mixing of dense and sparse matrix types!\nPleas use "
135 "either just dense or just sparse matrices for the global matrices.\n");
136 }
137 for (auto& cell_ptr : all_cells)
138 {
139 // All elements save the position to their entries in the matrix through reference wrappers.
140 cell_ptr->getReferenceToElements(*m_capacitance_matrix, *m_stiffness_matrix,
141 m_forcing_vector);
142 if (cell_ptr->isBoundary())
143 {
144 for (auto& [key, boundary_ptr] : cell_ptr->getBoundaryCondition())
145 {
146 boundary_ptr->getReferenceToElements(*m_capacitance_matrix, *m_stiffness_matrix,
147 m_forcing_vector);
148 }
149 m_boundary_cells.push_back(std::move(cell_ptr));
150 // The original cell list is made invalid here. This should also be reflected in Grid
151 cell_ptr = nullptr;
152 }
153 else
154 {
155 m_interior_cells.push_back(std::move(cell_ptr));
156 // The original cell list is made invalid here. This should also be reflected in Grid
157 cell_ptr = nullptr;
158 }
159 }
160}
161
167template <typename T>
168void ElementManager<T>::setCells(std::vector<std::unique_ptr<GenericElement<T>>>& all_cells)
169{
170 sortCells(all_cells);
171}
172
177template <typename T>
179{
180 m_stiffness_matrix->zeroMatrix();
181 m_capacitance_matrix->zeroMatrix();
182 m_forcing_vector *= 0;
183}
184
189template <typename T>
191{
192 flushMatrices();
193 for (auto& interior_cell_ptr : m_interior_cells)
194 {
195 interior_cell_ptr->writeMatrixVectorBlock();
196 }
197 for (auto& boundary_cell_ptr : m_boundary_cells)
198 {
199 boundary_cell_ptr->writeBoundaryCondition();
200 }
201}
202
203#endif
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:35
void buildAtRuntime()
Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by...
Definition CsrMatrix.h:145
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:168
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:190
std::pair< int, int > size() const
Function that returns the size of the matrix as a pair in (rows, columns) format.
Definition GenericMatrix.h:118