Triangle.h 3.76 KB
Newer Older
1
2
3
#ifndef OERSTED_TRIANGLE_H
#define OERSTED_TRIANGLE_H

JasonPries's avatar
JasonPries committed
4
5
#include <vector>

6
7
8
#include "Eigen"
#include "Eigen/Sparse"

9
10
#include "Quadrature.hpp"

11
#include "MatrixGroup.h"
12
13
#include "Node.h"

14
// TODO: Curved elements
JasonPries's avatar
JasonPries committed
15

16
17
18
19
20
template<size_t Dimension>
class Element {
public:
    Element() : ID{0} {};
    Element(size_t id) : ID{id} {};
JasonPries's avatar
JasonPries committed
21

JasonPries's avatar
JasonPries committed
22
23
    size_t const id() const { return ID; };

JasonPries's avatar
JasonPries committed
24
protected:
25
    size_t ID;
JasonPries's avatar
JasonPries committed
26
27
};

28
class Facet : public Element<2> {
JasonPries's avatar
JasonPries committed
29
public:
30
31
    Facet() : Element{} {};
    Facet(size_t id) : Element{id} {};
32
33
};

34
template<size_t P>
35
class Triangle : public Facet {
36
public:
37
    static constexpr size_t NumNodes{(P + 1) * (P + 2) / 2};
38

39
    Triangle() : Facet{}, Node{} {};
40

JasonPries's avatar
JasonPries committed
41
    size_t const &node(size_t const &i) const { return Node[i]; };
42
43

    template<typename... Args>
44
    Triangle(size_t id, Args &&... args) : Facet{id}, Node{std::forward<Args>(args)...} {};
45
46
47
48
49

    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>
50
    void determinant(DiagonalMatrixGroup<TriangleQuadrature<Q>::size> &mat, std::vector<XY> const &nodes) const;
51
52

    template<size_t Q>
53
    void basis(SparseMatrixGroup<TriangleQuadrature<Q>::size> &mat, std::vector<XY> const &nodes) const;
54
55

    template<size_t Q>
56
    void derivative(DerivativeMatrixGroup<TriangleQuadrature<Q>::size> &df, std::vector<XY> const &nodes) const;
57
58

protected:
59
    size_t Node[NumNodes];
60
61
};

62
/*
63
64
template<>
template<>
65
inline Eigen::Matrix<double, 2, 2> Triangle<1>::jacobian<0>(std::vector<XY> const &nodes) const {
66
67
68
69
70
71
72
73
74
    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;
}
75
*/
76
77
78

template<>
template<>
79
inline Eigen::Matrix<double, 2, 2> Triangle<1>::jacobian<1>(std::vector<XY> const &nodes) const {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    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>
102
103
void Triangle<1>::determinant(DiagonalMatrixGroup<TriangleQuadrature<Q>::size> &mat, std::vector<XY> const &nodes) const {
    for (size_t i = 0; i != TriangleQuadrature<Q>::size; ++i) {
104
105
106
107
108
109
110
111
        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();
JasonPries's avatar
JasonPries committed
112
        mat(i)(ID) = xx * yy - xy * yx;
113
114
115
    }
}

116

117
118
template<>
template<size_t Q>
119
120
121
122
123
void Triangle<1>::basis(SparseMatrixGroup<TriangleQuadrature<Q>::size> &mat, std::vector<XY> const &nodes) const {
    for (size_t i = 0; i != TriangleQuadrature<Q>::size; ++i) {
        mat(i, Node[0], ID) += TriangleQuadrature<Q>::a[i];
        mat(i, Node[1], ID) += TriangleQuadrature<Q>::b[i];
        mat(i, Node[2], ID) += 1.0 - TriangleQuadrature<Q>::a[i] - TriangleQuadrature<Q>::b[i];
124
125
126
127
128
    }
}

template<>
template<size_t Q>
129
void Triangle<1>::derivative(DerivativeMatrixGroup<TriangleQuadrature<Q>::size> &df, std::vector<XY> const &nodes) const {
130
131
    auto J = jacobian<1>(nodes);

132
    for (size_t i = 0; i != TriangleQuadrature<Q>::size; ++i) {
133
134
        df.dx(i, Node[0], ID) += J(0, 0);
        df.dy(i, Node[0], ID) += J(1, 0);
135

136
137
        df.dx(i, Node[1], ID) += J(0, 1);
        df.dy(i, Node[1], ID) += J(1, 1);
138

139
140
        df.dx(i, Node[2], ID) += (-J(0, 0) - J(0, 1));
        df.dy(i, Node[2], ID) += (-J(1, 0) - J(1, 1));
141
142
143
144
    }
};

#endif //OERSTED_TRIANGLE_H