Triangle.h 3.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
#ifndef OERSTED_TRIANGLE_H
#define OERSTED_TRIANGLE_H

#include "Eigen"
#include "Eigen/Sparse"

#include "QuadratureRule.h"
#include "Node.h"

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
template<size_t Q>
class SparseMatrixGroup {
public:
    SparseMatrixGroup(size_t const rows, size_t const cols, Eigen::VectorXi const &&nnz) {
        for (size_t i = 0; i != Q; ++i) {
            Mat[i].resize(rows, cols);
            Mat[i].reserve(nnz);
        }
    };

    auto &operator[](size_t const i) { return Mat[i]; };

protected:
    std::array<Eigen::SparseMatrix<double>, Q> Mat;
};

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// TODO: Curved elements

template<size_t P>
class Triangle {
public:
    static constexpr size_t N{(P + 1) * (P + 2) / 2};

    Triangle() : Node{} {};

    size_t const &node(size_t const &i) { return Node[i]; };

    template<typename... Args>
    Triangle(Args &&... args) : Node{std::forward<Args>(args)...} {};

    template<size_t D>
    Eigen::Matrix<double, 2, 2> jacobian(std::vector<XY> const &nodes) const; // TODO: Accept Eigen::Matrix<double,2,2> as input

    template<size_t Q>
44
    void basis(SparseMatrixGroup<TriangleQuadratureRule<Q>::size> &mat, size_t const &tri) const;
45
46

    template<size_t Q>
47
    void derivative(SparseMatrixGroup<TriangleQuadratureRule<Q>::size> &dx, SparseMatrixGroup<TriangleQuadratureRule<Q>::size> &dy, std::vector<XY> const &nodes, size_t const &tri) const;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

protected:
    size_t Node[N];
};

template<>
template<>
Eigen::Matrix<double, 2, 2> Triangle<1>::jacobian<0>(std::vector<XY> const &nodes) const {
    Eigen::Matrix<double, 2, 2> value;

    value(0, 0) = 1.0;
    value(0, 1) = 0.0;
    value(1, 0) = 0.0;
    value(1, 1) = 1.0;

    return value;
}

template<>
template<>
Eigen::Matrix<double, 2, 2> Triangle<1>::jacobian<1>(std::vector<XY> const &nodes) const {
    Eigen::Matrix<double, 2, 2> value;

    XY const &p0 = nodes[Node[0]];
    XY const &p1 = nodes[Node[1]];
    XY const &p2 = nodes[Node[2]];

    double xx = p0.x() - p2.x();
    double xy = p0.y() - p2.y();
    double yx = p1.x() - p2.x();
    double yy = p1.y() - p2.y();
    double det = xx * yy - xy * yx;

    value(0, 0) = yy / det;
    value(0, 1) = -xy / det;
    value(1, 0) = -yx / det;
    value(1, 1) = xx / det;

    return value;
};

template<>
template<size_t Q>
91
void Triangle<1>::basis(SparseMatrixGroup<TriangleQuadratureRule<Q>::size> &mat, size_t const &tri) const {
92
    for (size_t i = 0; i != TriangleQuadratureRule<Q>::size; ++i) {
93
94
95
        mat[i].coeffRef(Node[0], tri) += TriangleQuadratureRule<Q>::a[i];
        mat[i].coeffRef(Node[1], tri) += TriangleQuadratureRule<Q>::b[i];
        mat[i].coeffRef(Node[2], tri) += 1.0 - TriangleQuadratureRule<Q>::a[i] - TriangleQuadratureRule<Q>::b[i];
96
97
98
99
100
    }
}

template<>
template<size_t Q>
101
void Triangle<1>::derivative(SparseMatrixGroup<TriangleQuadratureRule<Q>::size> &dx, SparseMatrixGroup<TriangleQuadratureRule<Q>::size> &dy, std::vector<XY> const &nodes, size_t const &tri) const {
102
103
104
    auto J = jacobian<1>(nodes);

    for (size_t i = 0; i != TriangleQuadratureRule<Q>::size; ++i) {
105
106
        dx[i].coeffRef(Node[0], tri) += J(0, 0);
        dy[i].coeffRef(Node[0], tri) += J(1, 0);
107

108
109
        dx[i].coeffRef(Node[1], tri) += J(0, 1);
        dy[i].coeffRef(Node[1], tri) += J(1, 1);
110

111
112
        dx[i].coeffRef(Node[2], tri) += (-J(0, 0) - J(0, 1));
        dy[i].coeffRef(Node[2], tri) += (-J(1, 0) - J(1, 1));
113
114
115
116
    }
};

#endif //OERSTED_TRIANGLE_H