FiniteElementMesh.h 5.27 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    //void save_as(std::string path, std::string file_name) const;
    void write_scalar(Eigen::VectorXd const &v, std::string path, std::string file_name) const {
        if (!boost::filesystem::exists(path)) {
            boost::filesystem::create_directories(path);
        }

        std::fstream fs;
        fs.open(path + file_name + ".oesc", std::fstream::out);

        // Write header
        size_t loc = 3;
        fs << loc << ',' << Triangles.size() << std::endl;
        loc += Triangles.size();

        fs << loc << ',' << Nodes.size() << std::endl;
        loc += Nodes.size();

        fs << loc << ',' << v.size() << std::endl;

        // Write triangles
        for (Triangle<Order> const &t : Triangles) {
            for (size_t i = 0; i != Triangle<Order>::NumNodes - 1; ++i) {
                fs << t.node(i) << ',';
            }
            fs << t.node(Triangle<Order>::NumNodes - 1) << std::endl;
        }

        // Write nodes
        for (XY const &n : Nodes) {
            fs << n.x() << ',' << n.y() << std::endl;
        }

        // Write values
        for (size_t i = 0; i!=v.size(); ++i){
            fs << v(i) << std::endl;
        }

         /*
        for (size_t e = 0; e != Edges.size(); ++e) {
            Point const v0 = base(e);
            Point const v1 = base(next(e));
            Point const v2 = base(next(next(e)));
            fs << v0.X << ',' << v1.X << ',' << v2.X << ',' << v0.Y << ',' << v1.Y << ',' << v2.Y << '\n';
        }
        */


        fs.close();
    }

165 166
protected:
    std::vector<XY> Nodes;
JasonPries's avatar
JasonPries committed
167 168
    std::vector<Triangle<Order>> Triangles;

JasonPries's avatar
JasonPries committed
169 170
    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;
171 172 173
};

#endif //OERSTED_FINITEELEMENTMESH_H