MoCSI API Reference
Loading...
Searching...
No Matches
MatrixManager.h
Go to the documentation of this file.
1#ifndef MATRIX_MANAGER_H
2#define MATRIX_MANAGER_H
3
4#include <algorithm>
5#include <memory>
6#include <string>
7#include <vector>
8
9#include "GenericMatrix.h"
10#include "MatrixFactory.h"
11#include "SimulationClassBase.h"
12
17template <typename T>
19{
20 private:
21 std::vector<std::pair<int, int>> m_non_zero_elements;
23
24 void flushMatrices();
25
26 public:
27 // It only makes sense for them to be public as I need to get references to these objects.
28 std::shared_ptr<GenericMatrix<T>> lhs_matrix{};
29 std::valarray<T> rhs_vector{};
30
32 std::shared_ptr<MatrixFactory<T>> matrix_factory);
33
35 std::shared_ptr<GenericMatrix<T>> stiffness_matrix,
36 std::valarray<T>& forcing_vector);
37};
38
42template <typename T>
44 std::shared_ptr<MatrixFactory<T>> matrix_factory)
45 : m_sim{sim}, lhs_matrix(matrix_factory->createShared())
46{
47 // CSR build-up needs to be postponed until elements could be registered.
48 if (lhs_matrix->getMatrixType() == MatrixType::Dense)
49 {
50 lhs_matrix->buildAtRuntime(size, size);
51 }
52 rhs_vector.resize(size);
53}
54
63template <typename T>
65 std::shared_ptr<GenericMatrix<T>> stiffness_matrix,
66 std::valarray<T>& forcing_vector)
67{
68 if (m_non_zero_elements.empty())
69 {
72 if ((lhs_matrix->getMatrixType() == MatrixType::Csr))
73 {
74 for (const auto& pos : m_non_zero_elements)
75 {
76 lhs_matrix->registerElement(0.0, pos.first, pos.second);
77 }
79 lhs_matrix->buildAtRuntime(dim, dim);
80 }
81 }
82 T delta_t{m_sim->m_simulation_config.getDoubleParameters("time_delta")};
83 const std::valarray<T>& temperature{m_sim->m_field_map.at("Temperature")};
84 // Old dense matrix assembly only left for comparisons to MoCSI v1.0.0. Deprecated and highly
85 // inefficient!
86 if (lhs_matrix->getMatrixType() == MatrixType::Dense)
87 {
88 flushMatrices();
89 for (const auto& [row, col] : m_non_zero_elements)
90 {
91 (*lhs_matrix)(row, col) =
93 if (row == col)
94 {
95 rhs_vector[row] += forcing_vector[row] * delta_t;
96 }
97 rhs_vector[row] += (*capacitance_matrix)(row, col) * temperature[col];
98 }
99 }
100 else
101 {
102 // lhs_matrix = capacitance_matrix + stiffness_matrix * delta_t
103 lhs_matrix->overwriteMatrixValues(
105 // rhs_vector = capacitance_matrix * temperature(vector) + forcing_vector * delta_t
106 rhs_vector =
108 }
109}
110
116template <typename T>
118{
119 lhs_matrix->zeroMatrix();
120 rhs_vector *= 0.0;
121}
122
123#endif
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:35
void registerElement(T value, int rows, int cols) override
Registers the element and it's initial value within a COO style vector for dynamic creation.
Definition CsrMatrix.h:126
const std::vector< std::pair< int, int > > & getNonZeroElements() override
Definition CsrMatrix.h:73
void updateNonZeroElements() override
Sets the m_non_zero_elememts parameter of the parent class.
Definition CsrMatrix.h:577
std::valarray< T > scalarMultiply(T value) const override
Multiplication of a scalar to the matrix values element-wise.
Definition CsrMatrix.h:452
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:92
std::valarray< T > matrixVectorMultiplication(const std::valarray< T > &value) const override
Matrix-vector-multiplication for sparse matrices.
Definition CsrMatrix.h:519
std::valarray< T > vectorElemAdd(const std::valarray< T > &value) const override
Addition of a vector to the matrix values element-wise.
Definition CsrMatrix.h:473
void buildAtRuntime()
Builds the CSR matrix from the registered elements. It sorts all elements of the COO format vector by...
Definition CsrMatrix.h:145
void overwriteMatrixValues(const std::valarray< T > &value)
Overwrites the matrix values with a new valarray. This is used for optimized overwrite when arithmeti...
Definition GenericMatrix.h:140
std::vector< std::pair< int, int > > m_non_zero_elements
Definition GenericMatrix.h:25
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
Class that manages the assembly of the global linear system of equations to be passed to the solver.
Definition MatrixManager.h:19
std::valarray< T > rhs_vector
Definition MatrixManager.h:29
MatrixManager(int size, SimulationClassBase< T > *sim, std::shared_ptr< MatrixFactory< T > > matrix_factory)
Constructor for MatrixManager.
Definition MatrixManager.h:43
void assembleGlobalLse(std::shared_ptr< GenericMatrix< T > > capacitance_matrix, std::shared_ptr< GenericMatrix< T > > stiffness_matrix, std::valarray< T > &forcing_vector)
Function which assembles the global linear system of equations from the global FEM matrices.
Definition MatrixManager.h:64
std::shared_ptr< GenericMatrix< T > > lhs_matrix
Definition MatrixManager.h:28