Triangle.h 3.81 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

41 42 43 44 45
    Triangle(size_t id, std::array<size_t, NumNodes> const &n) : Facet{id} {
        for (size_t i = 0; i != NumNodes; ++i) {
            Node[i] = n[i];
        }
    };
46

47
    size_t const &node(size_t const &i) const { return Node[i]; };
48 49 50 51 52

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

    template<size_t Q>
56
    void basis(SparseMatrixGroup<TriangleQuadrature<Q>::size> &mat, std::vector<XY> const &nodes) const;
57 58

    template<size_t Q>
59
    void derivative(DerivativeMatrixGroup<TriangleQuadrature<Q>::size> &df, std::vector<XY> const &nodes) const;
60 61

protected:
62
    size_t Node[NumNodes];
63 64
};

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

template<>
template<>
82
inline Eigen::Matrix<double, 2, 2> Triangle<1>::jacobian<1>(std::vector<XY> const &nodes) const {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    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>
105 106
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) {
107 108 109 110 111 112 113 114
        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
115
        mat(i)(ID) = xx * yy - xy * yx;
116 117 118
    }
}

119

120 121
template<>
template<size_t Q>
122 123 124 125 126
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];
127 128 129 130 131
    }
}

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

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

139 140
        df.dx(i, Node[1], ID) += J(0, 1);
        df.dy(i, Node[1], ID) += J(1, 1);
141

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

#endif //OERSTED_TRIANGLE_H