FiniteElementMesh.h 3.77 KB
Newer Older
1
2
3
#ifndef OERSTED_FINITEELEMENTMESH_H
#define OERSTED_FINITEELEMENTMESH_H

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

7
8
#include "Mesh.hpp"

9
#include "Boundary.h"
10
#include "MatrixGroup.h"
11
12
#include "Node.h"
#include "Triangle.h"
JasonPries's avatar
JasonPries committed
13
#include "Region.h"
14

JasonPries's avatar
JasonPries committed
15
template<size_t Dimension, size_t Order>
16
17
class FiniteElementMesh {
};
18

JasonPries's avatar
JasonPries committed
19
template<size_t Order>
20
class FiniteElementMesh<2, Order> {
21
public:
JasonPries's avatar
JasonPries committed
22
23
    FiniteElementMesh() {};

24
25
26
27
    FiniteElementMesh(std::vector<XY> nodes, std::vector<Triangle<Order>> tris, std::vector<std::shared_ptr<Region<2>>> r, std::vector<std::shared_ptr<Boundary<2>>> b) : Nodes(nodes), Triangles(tris), Regions(r), Boundaries(b) {
        auto comp = [](std::shared_ptr<Boundary<2>> const &x, std::shared_ptr<Boundary<2>> const &y) { return (size_t) (x->curve().get()) < (size_t) (y->curve().get()); };
        std::sort(Boundaries.begin(), Boundaries.end(), comp);
    };
28
29

    FiniteElementMesh(Mesh &m);
30

JasonPries's avatar
JasonPries committed
31
32
    size_t size_nodes() const { return Nodes.size(); };

JasonPries's avatar
JasonPries committed
33
34
    size_t size_elements() const { return Triangles.size(); };

JasonPries's avatar
JasonPries committed
35
    std::vector<Triangle<Order>> const &triangles() const { return Triangles; };
JasonPries's avatar
JasonPries committed
36

JasonPries's avatar
JasonPries committed
37
    Triangle<Order> const &triangle(size_t i) const { return Triangles[i]; };
JasonPries's avatar
JasonPries committed
38

39
    auto &boundaries() { return Boundaries; };
JasonPries's avatar
JasonPries committed
40

41
    auto &boundary(size_t i) { return Boundaries[i]; };
JasonPries's avatar
JasonPries committed
42

43
44
45
46
47
48
49
50
51
52
53
54
55
    std::shared_ptr<Boundary<2>> boundary(std::shared_ptr<Curve const> const &c) const {
        auto comp = [](std::shared_ptr<Boundary<2>> const &x, size_t const &y) { return (size_t) (x->curve().get()) < y; };

        auto result = std::lower_bound(Boundaries.begin(), Boundaries.end(), (size_t) c.get(), comp);

        if (result != Boundaries.end()) {
            return *result;
        } else {
            std::shared_ptr<Boundary<2>> null;
            return null;
        }
    };

56
    auto const &boundaries() const { return Boundaries; };
JasonPries's avatar
JasonPries committed
57

58
    auto const &nodes() const { return Nodes; };
JasonPries's avatar
JasonPries committed
59

60
    auto const &node(size_t i) const { return Nodes[i]; };
JasonPries's avatar
JasonPries committed
61

62
    auto &regions() { return Regions; };
JasonPries's avatar
JasonPries committed
63

64
    auto &region(size_t i) { return Regions[i]; };
JasonPries's avatar
JasonPries committed
65

66
67
68
    auto const &regions() const { return Regions; };

    auto const &region(size_t i) const { return Regions[i]; };
JasonPries's avatar
JasonPries committed
69

70
    template<size_t Q>
71
72
    DiagonalMatrixGroup<TriangleQuadrature<Q>::size> determinant() const {
        DiagonalMatrixGroup<TriangleQuadrature<Q>::size> mat(Triangles.size());
73
74
75
76
77
78
79
80

        for (size_t i = 0; i != Triangles.size(); ++i) {
            Triangles[i].determinant<Q>(mat, Nodes);
        }

        return mat;
    }

JasonPries's avatar
JasonPries committed
81
    template<size_t Q>
82
83
    SparseMatrixGroup<TriangleQuadrature<Q>::size> basis() const {
        SparseMatrixGroup<TriangleQuadrature<Q>::size> mat(Nodes.size(), Triangles.size(), Triangle<Order>::NumNodes);
JasonPries's avatar
JasonPries committed
84
85

        for (size_t i = 0; i != Triangles.size(); ++i) {
86
            Triangles[i].basis<Q>(mat, Nodes);
JasonPries's avatar
JasonPries committed
87
88
89
        }

        return mat;
90
    }
JasonPries's avatar
JasonPries committed
91
92

    template<size_t Q>
93
94
    DerivativeMatrixGroup<TriangleQuadrature<Q>::size> derivative() const {
        DerivativeMatrixGroup<TriangleQuadrature<Q>::size> df(Nodes.size(), Triangles.size(), Triangle<Order>::NumNodes);
JasonPries's avatar
JasonPries committed
95

96
        for (size_t i = 0; i != Triangles.size(); ++i) {
97
            Triangles[i].derivative<Q>(df, Nodes);
JasonPries's avatar
JasonPries committed
98
        }
99

JasonPries's avatar
JasonPries committed
100
101
        return df;
    }
102

103
104
105
106
107
    template<size_t Q>
    std::vector<std::vector<XY>> quadrature_points() const {
        std::vector<std::vector<XY>> qp;
        qp.reserve(Triangles.size());

108
        for (size_t i = 0; i != Triangles.size(); ++i) {
109
110
111
112
113
114
            qp.push_back(Triangles[i].quadrature_points<Q>(Nodes));
        }

        return qp;
    }

115
116
protected:
    std::vector<XY> Nodes;
JasonPries's avatar
JasonPries committed
117
118
    std::vector<Triangle<Order>> Triangles;

JasonPries's avatar
JasonPries committed
119
120
    std::vector<std::shared_ptr<Region<2>>> Regions; // Contains vector of size_t referencing Triangles (and later Quadrilaterals)
    std::vector<std::shared_ptr<Boundary<2>>> Boundaries;
121
122
123
};

#endif //OERSTED_FINITEELEMENTMESH_H