Physics.h 9.82 KB
Newer Older
1
2
3
4
#ifndef OERSTED_PHYSICS_H
#define OERSTED_PHYSICS_H

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

JasonPries's avatar
JasonPries committed
7
#include "Eigen/IterativeLinearSolvers"
JasonPries's avatar
JasonPries committed
8
#include "Eigen/SparseCholesky"
JasonPries's avatar
JasonPries committed
9

JasonPries's avatar
JasonPries committed
10
#include "BoundaryCondition.h"
11
#include "FiniteElementMesh.h"
JasonPries's avatar
JasonPries committed
12
#include "Forcing.h"
JasonPries's avatar
JasonPries committed
13
14
15
16
#include "Node.h"
#include "PhysicsConstants.h"
#include "QuadratureRule.h"
#include "Triangle.h"
17

JasonPries's avatar
JasonPries committed
18
19
20
21
// TODO: These classes should be generic (e.g. CurlCurl, ScalarLaplacian, VectorLaplacian)
// TODO:    Higher level classes should implement interfaces with appropriate physics grammar
// TODO:    Implement material property dependencies using pointers to virtual member functions

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
enum class Variable {
    MagneticVectorPotential = 1,
    A = 1,
    MagneticFluxDensity = 2,
    B = 2,
    ElectricFluxDensity = 4,
    D = 4,
    ElectricFieldIntensity = 5,
    E = 5,
    MagneticFieldIntensity = 8,
    H = 8,
    ElectricCurrentDensity = 10,
    J = 10,
    ElectricScalarPotential = 26 + 21,
    Phi = 26 + 21,
    MagneticScalarPotential = 26 + 23,
    Psi = 26 + 23
};

JasonPries's avatar
JasonPries committed
41
42
43
44
45
46
47
48
49
class PhysicsInterface {
public:
    double time() { return Time; };

    void time(double t) { Time = t; };

protected:
    double Time;

JasonPries's avatar
JasonPries committed
50
51
    std::vector<std::shared_ptr<Forcing>> ForcingCondtions;
    std::vector<std::shared_ptr<BoundaryCondition>> BoundaryConditions;
JasonPries's avatar
JasonPries committed
52
53
};

JasonPries's avatar
JasonPries committed
54
template<size_t Dimension, size_t ElementOrder, size_t QuadratureOrder>
JasonPries's avatar
JasonPries committed
55
class PhysicsData {
JasonPries's avatar
JasonPries committed
56
57
58
public:
    static constexpr size_t QuadraturePoints = TriangleQuadratureRule<QuadratureOrder>::size;

JasonPries's avatar
JasonPries committed
59
    /* TODO: template<size_t Dimension, size_t ElementOrder, size_t QuadratureOrder> class PhysicsData : public PhysicsInterface {...};
JasonPries's avatar
JasonPries committed
60
61
62
63
64
65
66
    virtual Eigen::VectorXd init_residual() = 0;
    virtual Eigen::SparseMatrix<double> init_jacobian() = 0;
    virtual void residual(Eigen::SparseMatrix<double> &J, Eigen::VectorXd &r) = 0;
    virtual void build_matrices() = 0;
    virtual void apply_conditions() = 0;
     */

JasonPries's avatar
JasonPries committed
67
    PhysicsData(FiniteElementMesh<2, ElementOrder> &d) : Domain{d},
JasonPries's avatar
JasonPries committed
68
69
70
                                                         ElementWeights{d.triangles().size()},
                                                         Derivatives{d.nodes().size(), d.triangles().size(), Triangle<ElementOrder>::NumNodes},
                                                         Basis{d.nodes().size(), d.triangles().size(), Triangle<ElementOrder>::NumNodes} {};
JasonPries's avatar
JasonPries committed
71

JasonPries's avatar
JasonPries committed
72
    FiniteElementMesh<Dimension, ElementOrder> const &domain() const { return Domain; };
JasonPries's avatar
JasonPries committed
73
74
75
76
77
78
79
80
81
82
83
84
85

    DiagonalMatrixGroup<QuadraturePoints> const &weights() { return ElementWeights; };

    SparseMatrixGroup<QuadraturePoints> const &basis() { return Basis; };

    DerivativeMatrixGroup<QuadraturePoints> const &derivatives() { return Derivatives; };

protected:
    FiniteElementMesh<Dimension, ElementOrder> &Domain;

    DiagonalMatrixGroup<QuadraturePoints> ElementWeights;
    SparseMatrixGroup<QuadraturePoints> Basis;
    DerivativeMatrixGroup<QuadraturePoints> Derivatives;
86
87
};

JasonPries's avatar
JasonPries committed
88
template<size_t Dimension, size_t ElementOrder, size_t QuadratureOrder, Variable V>
JasonPries's avatar
JasonPries committed
89
class Magnetostatic : public PhysicsData<Dimension, ElementOrder, QuadratureOrder>, public PhysicsInterface {
JasonPries's avatar
JasonPries committed
90
91
92
};

template<size_t ElementOrder, size_t QuadratureOrder>
JasonPries's avatar
JasonPries committed
93
class Magnetostatic<2, ElementOrder, QuadratureOrder, Variable::MagneticVectorPotential> : public PhysicsData<2, ElementOrder, QuadratureOrder>, public PhysicsInterface {
94
public:
JasonPries's avatar
JasonPries committed
95
    using PhysicsData<2, ElementOrder, QuadratureOrder>::QuadraturePoints;
96

JasonPries's avatar
JasonPries committed
97
98
99
100
    using PhysicsData<2, ElementOrder, QuadratureOrder>::domain;
    using PhysicsData<2, ElementOrder, QuadratureOrder>::derivatives;
    using PhysicsData<2, ElementOrder, QuadratureOrder>::weights;
    using PhysicsData<2, ElementOrder, QuadratureOrder>::basis;
JasonPries's avatar
JasonPries committed
101

JasonPries's avatar
JasonPries committed
102
    Magnetostatic(FiniteElementMesh<2, ElementOrder> &d) : PhysicsData<2, ElementOrder, QuadratureOrder>{d}, Unknowns{d.size_nodes()}, Elements{d.size_elements()} {};
103

JasonPries's avatar
JasonPries committed
104
    Eigen::SparseMatrix<double> init_unknown_matrix() const { return Eigen::SparseMatrix<double>(Unknowns, Unknowns); };
105

JasonPries's avatar
JasonPries committed
106
    Eigen::DiagonalMatrix<double, Eigen::Dynamic> init_element_matrix() const { return Eigen::DiagonalMatrix<double, Eigen::Dynamic>(Elements); }
107

JasonPries's avatar
JasonPries committed
108
109
110
    Eigen::VectorXd init_unknown_vector() const { return Eigen::VectorXd(Unknowns); };

    Eigen::VectorXd init_element_vector() const { return Eigen::VectorXd(Elements); };
111

JasonPries's avatar
JasonPries committed
112
113
114
115
    Eigen::ArrayXd init_unknown_array() const { return Eigen::ArrayXd(Unknowns); };

    Eigen::ArrayXd init_element_array() const { return Eigen::ArrayXd(Elements); };

JasonPries's avatar
JasonPries committed
116
    void linearize(Eigen::SparseMatrix<double> &J, Eigen::VectorXd &v, Eigen::VectorXd &r, Eigen::VectorXd &f,
JasonPries's avatar
JasonPries committed
117
                   Eigen::ArrayXd &Fx, Eigen::ArrayXd &Fy, Eigen::ArrayXd &dFxdx, Eigen::ArrayXd &dFydy, Eigen::ArrayXd &dFxdy) {
JasonPries's avatar
JasonPries committed
118
119

        r = -f;
JasonPries's avatar
JasonPries committed
120
        J.setZero();
121
        for (size_t i = 0; i != TriangleQuadratureRule<QuadratureOrder>::size; ++i) {
JasonPries's avatar
JasonPries committed
122
            // Caclulate flux density
JasonPries's avatar
JasonPries committed
123
124
            Fx = Derivatives.dy(i).transpose() * v;
            Fy = -Derivatives.dx(i).transpose() * v;
JasonPries's avatar
JasonPries committed
125
126

            // Calculate polarization
JasonPries's avatar
JasonPries committed
127
128
            for (auto &dr : Domain.regions()) {
                dr->material().h_from_b(dr->elements(), Fx, Fy, dFxdx, dFydy, dFxdy);
JasonPries's avatar
JasonPries committed
129
130
131
            }

            // Calculate residual
JasonPries's avatar
JasonPries committed
132
133
            r += Derivatives.dy(i) * (ElementWeights(i) * Fx).matrix()
                 - Derivatives.dx(i) * (ElementWeights(i) * Fy).matrix(); // note: weak form introduces a negative sign into double-curl operator
JasonPries's avatar
JasonPries committed
134
135

            // Calculate jacobian
JasonPries's avatar
JasonPries committed
136
137
138
139
140
            J += Derivatives.dx(i) * (ElementWeights(i) * dFydy).matrix().asDiagonal() * Derivatives.dx(i).transpose()
                 + Derivatives.dy(i) * (ElementWeights(i) * dFxdx).matrix().asDiagonal() * Derivatives.dy(i).transpose()
                 + Derivatives.dx(i) * (ElementWeights(i) * dFxdy).matrix().asDiagonal() * Derivatives.dy(i).transpose()
                 + Derivatives.dy(i) * (ElementWeights(i) * dFxdy).matrix().asDiagonal() * Derivatives.dx(i).transpose();

141
142
143
144
145
146
147
148
149
        }
    };

    void build_matrices() {
        ElementWeights = Domain.template determinant<QuadratureOrder>();
        Basis = Domain.template basis<QuadratureOrder>();
        Derivatives = Domain.template derivative<QuadratureOrder>();

        for (size_t i = 0; i != TriangleQuadratureRule<QuadratureOrder>::size; ++i) {
JasonPries's avatar
JasonPries committed
150
            ElementWeights(i) *= TriangleQuadratureRule<QuadratureOrder>::w[i];
151
152
153
        }
    };

JasonPries's avatar
JasonPries committed
154
155
    void calculate_forcing(Eigen::VectorXd &f) {
        f.setZero();
JasonPries's avatar
JasonPries committed
156
        for (size_t i = 0; i != ForcingCondtions.size(); ++i) {
JasonPries's avatar
JasonPries committed
157
158
159
160
            f += ForcingCondtions[i]->operator()(Time);
        }
    }

JasonPries's avatar
JasonPries committed
161
    void add_current_density(std::function<double(double)> func, std::vector<std::shared_ptr<Region<2>>> regions) {
JasonPries's avatar
JasonPries committed
162
        ForcingCondtions.push_back(std::make_shared<HomogeneousForcing<2, ElementOrder, QuadratureOrder>>(func, regions, Basis, ElementWeights));
JasonPries's avatar
JasonPries committed
163
164
165
166
167
168
    }

    void remove_current_density(size_t i) {
        ForcingCondtions.erase(ForcingCondtions.begin() + i);
    }

JasonPries's avatar
JasonPries committed
169
    void add_magnetic_insulation(std::vector<std::shared_ptr<Boundary<2>>> boundaries) { // TODO: should boundaries be unique_ptrs?
JasonPries's avatar
JasonPries committed
170
        BoundaryConditions.push_back(std::make_shared<ZeroDirichlet<2, ElementOrder, QuadratureOrder>>(boundaries));
JasonPries's avatar
JasonPries committed
171
172
    }

JasonPries's avatar
JasonPries committed
173
    void add_periodic_boundary(std::vector<BoundaryMap> map, bool antiperiodic) {
JasonPries's avatar
JasonPries committed
174
175
176
177
178
179
180
181
        BoundaryConditions.push_back(std::make_shared<PeriodicBoundaryCondition<2, ElementOrder, QuadratureOrder>>(map, antiperiodic));
    }

    template <typename... Args>
    auto add_sliding_interface(Args&&... args) {
        auto condition = std::make_shared<SlidingInterface<2, ElementOrder, QuadratureOrder>>(std::forward<Args>(args)...);
        BoundaryConditions.push_back(condition);
        return condition;
JasonPries's avatar
JasonPries committed
182
183
    }

JasonPries's avatar
JasonPries committed
184
    void apply_conditions() {
JasonPries's avatar
JasonPries committed
185
        // TODO: This is a general method that should be in a superclass
JasonPries's avatar
JasonPries committed
186
187
188
        std::vector<Eigen::Triplet<double>> triplets;
        triplets.reserve(Domain.nodes().size());

JasonPries's avatar
JasonPries committed
189
190
        for (size_t i = 0; i != Domain.nodes().size(); ++i) {
            triplets.push_back(Eigen::Triplet<double>(i, i, 1.0));
JasonPries's avatar
JasonPries committed
191
192
193
194
195
196
        }

        for (size_t i = 0; i != BoundaryConditions.size(); ++i) {
            BoundaryConditions[i]->apply(triplets);
        }

JasonPries's avatar
JasonPries committed
197
        std::set<size_t, std::less<size_t>> index;
JasonPries's avatar
JasonPries committed
198
199
200
201
        for (size_t i = 0; i != BoundaryConditions.size(); ++i) {
            BoundaryConditions[i]->reduce(index);
        }

JasonPries's avatar
JasonPries committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
        // TODO: Does this handle arbitrary number of entries in the b.c. "permutation" matrix correctly?
        // TODO: BEGIN
        // Sort triplet list by rows
        auto row_lt = [](Eigen::Triplet<double> const &x, Eigen::Triplet<double> const &y) { return x.row() < y.row(); };
        std::sort(triplets.begin(), triplets.end(), row_lt);

        size_t num_erased{0};
        auto iter{triplets.begin()};
        for (size_t const &i : index) {
            // Renumber triplets based on number of rows previously erased
            while (iter != triplets.end() && iter->row() < i) {
                *iter = Eigen::Triplet<double>(iter->row() - num_erased, iter->col(), iter->value());
                ++iter;
            }
JasonPries's avatar
JasonPries committed
216

JasonPries's avatar
JasonPries committed
217
218
219
            // Erase all rows associated with the
            while (iter != triplets.end() && iter->row() == i) {
                iter = triplets.erase(iter);
JasonPries's avatar
JasonPries committed
220
221
            }

JasonPries's avatar
JasonPries committed
222
            ++num_erased;
JasonPries's avatar
JasonPries committed
223
224
        }

JasonPries's avatar
JasonPries committed
225
226
227
        size_t cols{Domain.nodes().size()};
        size_t rows{cols - num_erased};
        // TODO: END
JasonPries's avatar
JasonPries committed
228

JasonPries's avatar
JasonPries committed
229
230
        Eigen::SparseMatrix<double> bc_matrix(rows, cols);
        bc_matrix.setFromTriplets(triplets.begin(), triplets.end());
JasonPries's avatar
JasonPries committed
231
232
233
234
235

        Basis.transform(bc_matrix);
        Derivatives.transform(bc_matrix);
        Unknowns = rows;
    };
236

JasonPries's avatar
JasonPries committed
237
protected:
JasonPries's avatar
JasonPries committed
238
239
240
241
242
243
244
    using PhysicsData<2, ElementOrder, QuadratureOrder>::Domain;
    using PhysicsData<2, ElementOrder, QuadratureOrder>::Derivatives;
    using PhysicsData<2, ElementOrder, QuadratureOrder>::ElementWeights;
    using PhysicsData<2, ElementOrder, QuadratureOrder>::Basis;

private:
    size_t Unknowns;
JasonPries's avatar
JasonPries committed
245
    size_t Elements;
246
247
248
};

#endif //OERSTED_PHYSICS_H