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);
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 m_capacitance_matrix->buildAtRuntime(size, size);
74 m_stiffness_matrix->buildAtRuntime(size, size);
75 m_forcing_vector.resize(size);
76 sortCells(all_cells);
77}
78
85template <typename T>
86void ElementManager<T>::sortCells(std::vector<std::unique_ptr<GenericElement<T>>>& all_cells)
87{
88 for (auto& cell_ptr : all_cells)
89 {
90 // All elements save the position to their entries in the matrix through reference wrappers.
91 cell_ptr->getReferenceToElements(*m_capacitance_matrix, *m_stiffness_matrix,
92 m_forcing_vector);
93 if (cell_ptr->isBoundary())
94 {
95 for (auto& [key, boundary_ptr] : cell_ptr->getBoundaryCondition())
96 {
97 boundary_ptr->getReferenceToElements(*m_capacitance_matrix, *m_stiffness_matrix,
98 m_forcing_vector);
99 }
100 m_boundary_cells.push_back(std::move(cell_ptr));
101 // The original cell list is made invalid here. This should also be reflected in Grid
102 cell_ptr = nullptr;
103 }
104 else
105 {
106 m_interior_cells.push_back(std::move(cell_ptr));
107 // The original cell list is made invalid here. This should also be reflected in Grid
108 cell_ptr = nullptr;
109 }
110 }
111}
112
118template <typename T>
119void ElementManager<T>::setCells(std::vector<std::unique_ptr<GenericElement<T>>>& all_cells)
120{
121 sortCells(all_cells);
122}
123
128template <typename T>
130{
131 m_stiffness_matrix->zeroMatrix();
132 m_capacitance_matrix->zeroMatrix();
133 m_forcing_vector *= 0;
134}
135
140template <typename T>
142{
143 flushMatrices();
144 for (auto& interior_cell_ptr : m_interior_cells)
145 {
146 interior_cell_ptr->writeMatrixVectorBlock();
147 }
148 for (auto& boundary_cell_ptr : m_boundary_cells)
149 {
150 boundary_cell_ptr->writeBoundaryCondition();
151 }
152}
153
154#endif
Error class to throw when a module chain has circular or missing dependencies. Inherits from std::exc...
Definition ChainManager.h:28
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:119
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:141