Marine systems simulation
SparseMatrixBuilder.h
1#pragma once
2
3#include "eigen_matrix_defs.h"
4#include "ObjectFactoryStack.h"
5#include <vector>
6#include <map>
7#include "LinearOperator.h"
8#include "AsynchronousTask.h"
9#include "MatrixConnectome.h"
10#include <Eigen/StdVector>
11
12namespace CoRiBoDynamics
13{
34{
35public:
36 explicit SparseMatrixBuilder(CoreBoundThreadPool* ThreadPool);
38
39 void StartBuilding(int size);
40 void InitializeElementMatrix(int i, const mat6& M);
41 void AddToMatrixThreadSafe( int i, int j, const mat6& M );
42
43 virtual double ApplyLinearOperator( const Vector& x, Vector& y );
44 virtual double ApplyApproximateInverse( const Vector& b, Vector& x );
46
47 virtual int SystemSize();
48
49 mat6 GetElementMatrix(int index);
50
55 void SetDropSize(int size){m_connectome.set_drop_size(size);}
56
57protected:
58 void AddToMatrix(int i, int j, const mat6& M);
59
60 /*
61 Build Eigen-format subset-matrix by picking all 6x6 elements from the given iterator
62 */
63 void FillSubMatrix(SparseMat& result, std::vector<int>::iterator elements_begin, std::vector<int>::iterator elements_end);
64
65
66 /*
67 Extra-diagonal elements are split into 'm_connection_indices' and 'm_connection_matrices'
68 since m_connection_indices is kept explicitly sorted during creation, we avoid copy-move of 6x6 matrices by splitting
69 */
70 std::vector<MatrixConnectome::CRix> m_connection_indices; // list of all extra-diagonal connections in the system, with and index into m_connection_matrices to find the actual numerical values of the connection. sorted in terms of collumn index first and row index second
71 std::vector<mat6,Eigen::aligned_allocator<mat6>> m_connection_matrices; // list of all extra diagonal connection matrices
72 std::mutex m_ConnectionElementLock; // all potential concurrent access to m_connection_indices and m_connection_matrices must be protected
73
74 int m_size;
76
77 MatrixConnectome m_connectome;
78
79 mat6* m_A_0_diagonal;
81
84
85 bool* m_set; // which elements in m_A_1_diagonal are non zero?
86
91 public:
92 void Execute();
93 std::vector<int> elements;
94 Eigen::SimplicialLLT<SparseMat, Eigen::Upper, Eigen::NaturalOrdering<int>>* m_sparse_solver;
95 //Eigen::LLT<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>, Eigen::Upper> m_dense_solver;
96 SparseMat connection_matrix;
97 const Vector* global_B;
98 Vector b;
99 Vector x;
100 ConnectionSolverTask() { m_sparse_solver = nullptr; }
101 ~ConnectionSolverTask() { delete m_sparse_solver; }
102
103 double accumulator;
104 enum State {COMPUTE,SOLVE} state;
105 SparseMatrixBuilder* m_builder;
106 };
107
108 std::vector<ConnectionSolverTask> m_connection_solver;
109
118 public:
119 explicit SubMultiplicationTask();
120 void setSubMatrix(int start_index, int end_index, mat6* A0, mat6* A1);
121 void MultiplySubDomain(const Vector* x, Vector* y);
122
123 double GetAccumulator(){return m_accumulator;}
124 protected:
125 void Execute();
126
127 int m_ix0;
128 int m_ix1;
129 mat6* m_A0;
130 mat6* m_A1;
131 const Vector* m_x;
132 Vector* m_y;
133 double m_accumulator; // sub-matrix contribution to the conjugate dot product x^t * A * x
134 };
135
136
145 public:
146 explicit TridiagonalSubSolverTask();
147 void SetMemoryRoot(mat6* U0_root, mat6* U1_root);
149 void InvertSubDomain(int start_index, int end_index, mat6* A0, mat6* A1);
150 void SolveSubDomain(const Vector* b, Vector* x);
151
152 double GetAccumulator(){return m_accumulator;}
153 protected:
154 enum State {COMPUTE,SOLVE};
155 void Compute(); // 6x6 block tri-diagonal matrix decomposition
156 void Solve(); // back-substitution solution of the linear system for the current b vector
157
158 void Execute();
159
160 int m_ix0;
161 int m_ix1;
162 int m_num;
163 mat6* m_A0;
164 mat6* m_A1;
165 const Vector* m_b;
166 Vector* m_x;
167 mat6* m_U1;
168 mat6* m_U0;
169 State m_state;
170
171 mat6* m_U1_root;
172 mat6* m_U0_root;
173
174 double m_accumulator; // sub-matrix contribution to the conjugate dot product x^t * A^-1 * x
175 };
176
177 std::vector<TridiagonalSubSolverTask> m_TridiagonalSubSolverTask;
178 std::vector<SubMultiplicationTask> m_SubMultiplicationTask;
179
181
182};
183}
Definition: AsynchronousTask.h:20
Definition: AsynchronousTask.h:15
Definition: LinearOperator.h:12
Definition: MatrixConnectome.h:29
mat6 * m_A0
end index
Definition: SparseMatrixBuilder.h:129
int m_ix1
start index
Definition: SparseMatrixBuilder.h:128
mat6 * m_A1
main diagonal;
Definition: SparseMatrixBuilder.h:130
const Vector * m_x
neighbor diagonal;
Definition: SparseMatrixBuilder.h:131
int m_num
end index
Definition: SparseMatrixBuilder.h:162
int m_ix1
start index
Definition: SparseMatrixBuilder.h:161
const Vector * m_b
neighbor diagonal;
Definition: SparseMatrixBuilder.h:165
mat6 * m_A1
main diagonal;
Definition: SparseMatrixBuilder.h:164
Definition: SparseMatrixBuilder.h:34
std::mutex * m_MatrixColumnLocks
number of 6DOF elements in the system
Definition: SparseMatrixBuilder.h:75
virtual void ComputeApproximateInverse()
approximately solve the linear system Ax = b
mat6 * m_U_1_diagonal
the main diagonal blocks of the block-tridiagonal UtU decomposition
Definition: SparseMatrixBuilder.h:83
void SetDropSize(int size)
Definition: SparseMatrixBuilder.h:55
void FillSubMatrix(SparseMat &result, std::vector< int >::iterator elements_begin, std::vector< int >::iterator elements_end)
add to submatrix at index i,j
virtual double ApplyLinearOperator(const Vector &x, Vector &y)
add to submatrix at index i,j with mutex lock
virtual double ApplyApproximateInverse(const Vector &b, Vector &x)
perform matrix multiplication y = Ax
mat6 * m_U_0_diagonal
first extra-diagonal of the system matrix
Definition: SparseMatrixBuilder.h:82
bool * m_set
first extra-diagonal of the block-tridiagonal UtU decomposition
Definition: SparseMatrixBuilder.h:85
void InitializeElementMatrix(int i, const mat6 &M)
Resets matrix to zero.
CoreBoundThreadPool * m_ThreadPool
list of asynchronous matrix sub domain
Definition: SparseMatrixBuilder.h:180
mat6 * m_A_1_diagonal
the main diagonal blocks of the system matrix
Definition: SparseMatrixBuilder.h:80
std::vector< SubMultiplicationTask > m_SubMultiplicationTask
List of asynchronous block-tridiagonal sub domain solvers.
Definition: SparseMatrixBuilder.h:178
Definition: CollisionManager.h:6