DGTransportAssemble.cpp 14 KB
Newer Older
1
#include "DGTransportAssemble.hpp"
niklas.baumgarten's avatar
piles    
niklas.baumgarten committed
2
#include "Plotting.hpp"
3
4


niklas.baumgarten's avatar
niklas.baumgarten committed
5
void DGLinearTransportAssemble::MassMatrix(Matrix &massMatrix) const {
6
7
8
9
10
11
    massMatrix = 0;
    for (cell c = massMatrix.cells(); c != massMatrix.cells_end(); ++c) {
        DGElement elem(*disc, massMatrix, c);
        DGRowEntries M_c(massMatrix, c, c);
        for (int q = 0; q < elem.nQ(); ++q) {
            double w = elem.QWeight(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
12
            for (int i = 0; i < elem.NodalPoints(); ++i) {
13
                Scalar Phi_i = elem.Value(q, i);
niklas.baumgarten's avatar
niklas.baumgarten committed
14
                for (int j = 0; j < elem.NodalPoints(); ++j) {
15
16
17
18
19
20
21
22
                    Scalar Phi_j = elem.Value(q, j);
                    M_c(i, j) += w * Phi_i * Phi_j;
                }
            }
        }
    }
}

niklas.baumgarten's avatar
niklas.baumgarten committed
23
void DGLinearTransportAssemble::SystemMatrix(Matrix &systemMatrix) const {
24
25
26
27
28
29
    systemMatrix = 0;
    for (cell c = systemMatrix.cells(); c != systemMatrix.cells_end(); ++c) {
        DGElement elem(*disc, systemMatrix, c);
        DGRowEntries A_c(systemMatrix, c, c);
        for (int q = 0; q < elem.nQ(); ++q) {
            double w = elem.QWeight(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
30
            VectorField B = problem->CellFlux(c, elem.QPoint(q));
niklas.baumgarten's avatar
niklas.baumgarten committed
31
            for (int i = 0; i < elem.NodalPoints(); ++i) {
32
                Scalar Phi_i = elem.Value(q, i);
niklas.baumgarten's avatar
niklas.baumgarten committed
33
                for (int j = 0; j < elem.NodalPoints(); ++j) {
34
35
36
37
38
39
40
                    VectorField gradPhi_j = elem.Derivative(q, j);
                    A_c(i, j) -= w * (gradPhi_j * B * Phi_i);
                }
            }
        }
        for (int f = 0; f < c.Faces(); ++f) {
            DGFaceElement faceElem(*disc, systemMatrix, c, f);
41
            if (systemMatrix.GetMesh().onBndDG(c, f)) {
42
43
44
                for (int q = 0; q < faceElem.nQ(); ++q) {
                    const Point &Qf_c = faceElem.QPoint(q);
                    VectorField Nq = faceElem.QNormal(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
45
                    Scalar BN = problem->FaceNormalFlux(c, f, Nq, Qf_c);
46
47
                    if (BN > 0) continue;
                    double w = faceElem.QWeight(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
48
                    for (int i = 0; i < faceElem.NodalPoints(); ++i) {
49
                        Scalar phi_i = faceElem.Value(q, i);
niklas.baumgarten's avatar
niklas.baumgarten committed
50
                        for (int j = 0; j < faceElem.NodalPoints(); ++j) {
51
52
53
54
55
56
                            Scalar phi_j = faceElem.Value(q, j);
                            A_c(i, j) += w * BN * phi_j * phi_i;
                        }
                    }
                }
            } else {
57
58
                cell cf = systemMatrix.GetMesh().find_neighbour_cell(c, f);
                int f1 = systemMatrix.GetMesh().find_neighbour_face_id(c.Face(f), cf);
59
60
61
62
63
                DGFaceElement felem_1(*disc, systemMatrix, cf, f1);
                DGRowEntries A_cf(systemMatrix, c, cf);
                for (int q = 0; q < faceElem.nQ(); ++q) {
                    const Point &Qf_c = faceElem.QPoint(q);
                    VectorField Nq = faceElem.QNormal(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
64
                    Scalar BN = problem->FaceNormalFlux(c, f, Nq, Qf_c);
65
                    if (BN > 0) continue;
66
                    int q1 = felem_1.findQPointID(faceElem, Qf_c);
67
                    double w = faceElem.QWeight(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
68
                    for (int i = 0; i < faceElem.NodalPoints(); ++i) {
69
                        Scalar phi_i = faceElem.Value(q, i);
niklas.baumgarten's avatar
niklas.baumgarten committed
70
                        for (int j = 0; j < faceElem.NodalPoints(); ++j) {
71
72
73
                            Scalar phi_j = faceElem.Value(q, j);
                            A_c(i, j) += w * BN * phi_j * phi_i;
                        }
niklas.baumgarten's avatar
niklas.baumgarten committed
74
                        for (int j = 0; j < felem_1.NodalPoints(); ++j) {
75
76
77
78
79
80
81
82
83
84
85
86
87
                            Scalar phi_j = felem_1.Value(q1, j);
                            A_cf(i, j) -= w * BN * phi_j * phi_i;
                        }
                    }
                }
                if (cf() < c()) continue;
                DGRowEntries A_fc(systemMatrix, cf, c);
                DGRowEntries A_ff(systemMatrix, cf, cf);
                for (int q = 0; q < faceElem.nQ(); ++q) {
                    double w = faceElem.QWeight(q);
                    const Point &z = faceElem.QPoint(q);
                    const Point &N = faceElem.QNormal(q);
                    const Point &Qf_c = faceElem.QPoint(q);
88
                    int q1 = felem_1.findQPointID(faceElem, Qf_c);
89
                    double s = 1;
niklas.baumgarten's avatar
niklas.baumgarten committed
90
                    for (int i = 0; i < faceElem.NodalPoints(); ++i) {
91
92
93
                        Scalar phi_i = faceElem.Value(q, i);
                        Scalar NDphi_i = diffusion *
                            (faceElem.Derivative(q, i) * N);
niklas.baumgarten's avatar
niklas.baumgarten committed
94
                        for (int j = 0; j < faceElem.NodalPoints(); ++j) {
95
96
97
98
99
100
101
                            Scalar phi_j = faceElem.Value(q, j);
                            Scalar NDphi_j =
                                diffusion * (faceElem.Derivative(q, j) * N);
                            A_c(i, j) -= w * (-0.5 * NDphi_i * phi_j
                                - 0.5 * phi_i * NDphi_j
                                + s * phi_i * phi_j);
                        }
niklas.baumgarten's avatar
niklas.baumgarten committed
102
                        for (int j = 0; j < felem_1.NodalPoints(); ++j) {
103
104
105
106
107
108
109
110
111
112
113
                            Scalar phi_j = felem_1.Value(q1, j);
                            Scalar NDphi_j =
                                diffusion * (felem_1.Derivative(q1, j) * N);
                            A_cf(i, j) -= w * (0.5 * NDphi_i * phi_j
                                - phi_i * 0.5 * NDphi_j
                                - s * phi_i * phi_j);
                            A_fc(j, i) -= w * (0.5 * NDphi_i * phi_j
                                - phi_i * 0.5 * NDphi_j
                                - s * phi_i * phi_j);
                        }
                    }
niklas.baumgarten's avatar
niklas.baumgarten committed
114
                    for (int i = 0; i < felem_1.NodalPoints(); ++i) {
115
116
117
                        Scalar phi_i = felem_1.Value(q1, i);
                        Scalar NDphi_i = diffusion
                            * (felem_1.Derivative(q1, i) * N);
niklas.baumgarten's avatar
niklas.baumgarten committed
118
                        for (int j = 0; j < felem_1.NodalPoints(); ++j) {
119
120
121
122
123
124
125
126
127
128
129
130
131
132
                            Scalar phi_j = felem_1.Value(q1, j);
                            Scalar NDphi_j = diffusion
                                * (felem_1.Derivative(q1, j) * N);
                            A_ff(i, j) -= w * (0.5 * NDphi_i * phi_j
                                + phi_i * 0.5 * NDphi_j
                                + s * phi_i * phi_j);
                        }
                    }
                }
            }
        }
    }
}

niklas.baumgarten's avatar
niklas.baumgarten committed
133
void DGLinearTransportAssemble::RHS(double t, Vector &rhs) const {
134
    rhs = 0;
niklas.baumgarten's avatar
niklas.baumgarten committed
135
    if (!problem->RHS()) return;
136
137
138
    for (cell c = rhs.cells(); c != rhs.cells_end(); ++c) {
        row r = rhs.find_row(c());
        for (int f = 0; f < c.Faces(); ++f) {
139
            if (!rhs.GetMesh().onBndDG(c, f)) continue;
140
141
142
143
144
            DGFaceElement felem(*disc, rhs, c, f);
            for (int q = 0; q < felem.nQ(); ++q) {
                const Point &z = felem.QPoint(q);
                double w = felem.QWeight(q);
                VectorField N = felem.QNormal(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
145
                Scalar BN = problem->FaceNormalFlux(c, f, N, z);
146
                if (BN > 0) continue;
niklas.baumgarten's avatar
niklas.baumgarten committed
147
                Scalar U = problem->Solution(t, z);
niklas.baumgarten's avatar
niklas.baumgarten committed
148
                for (int i = 0; i < felem.NodalPoints(); ++i) {
149
150
151
152
153
154
155
156
                    Scalar Phi_i = felem.Value(q, i);
                    rhs(r, i) -= w * U * BN * Phi_i;
                }
            }
        }
    }
}

niklas.baumgarten's avatar
niklas.baumgarten committed
157
double DGLinearTransportAssemble::Energy(const Vector &u) const {
158
159
160
161
162
163
164
165
166
167
168
169
    double energy = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        DGElement elem(*disc, u, c);
        for (int q = 0; q < elem.nQ(); ++q) {
            double w = elem.QWeight(q);
            Scalar U = elem.Value(q, u);
            energy += w * (U * U);
        }
    }
    return 0.5 * PPM->Sum(energy);
}

niklas.baumgarten's avatar
niklas.baumgarten committed
170
void DGNonLinearTransportAssemble::Energy(const cell &c, const Vector &u, double &energy) const {
171
172
173
174
175
176
177
178
    DGElement elem(*disc, u, c);
    for (int q = 0; q < elem.nQ(); ++q) {
        double w = elem.QWeight(q);
        Scalar U = elem.Value(q, u);
        energy += w * (U * U);
    }
}

niklas.baumgarten's avatar
niklas.baumgarten committed
179
double DGLinearTransportAssemble::Error(double t, const Vector &u) const {
180
181
182
183
184
185
    double err = 0.0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        DGElement elem(*disc, u, c);
        for (int q = 0; q < elem.nQ(); ++q) {
            double w = elem.QWeight(q);
            Scalar U = elem.Value(q, u);
niklas.baumgarten's avatar
niklas.baumgarten committed
186
            Scalar Sol = problem->Solution(t, elem.QPoint(q));
187
188
189
190
191
192
            err += w * (U - Sol) * (U - Sol);
        }
    }
    return sqrt(PPM->Sum(err));
}

niklas.baumgarten's avatar
niklas.baumgarten committed
193
std::pair<double, double> DGLinearTransportAssemble::InFlowOutFlowRate(const Vector &u) const {
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    double inflow = 0.0;
    double outflow = 0.0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        BFParts bnd(u.GetMesh(), c);
        if (!bnd.onBnd()) continue;
        DGElement elem(*disc, u, c);
        double U = 0;
        double area = 0;
        for (int q = 0; q < elem.nQ(); ++q) {
            double w = elem.QWeight(q);
            U += w * elem.Value(q, u);
            area += w;
        }
        U *= (1 / area);
        for (int f = 0; f < c.Faces(); ++f) {
            if (bnd[f] == -1) continue;
            DGFaceElement faceElem(*disc, u, c, f);
            for (int q = 0; q < faceElem.nQ(); ++q) {
                double w = faceElem.QWeight(q);
                VectorField Nq = faceElem.QNormal(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
214
                Scalar BN = problem->FaceNormalFlux(c, f, Nq, elem.QPoint(q));
215
216
217
218
219
220
221
222
223
224
225
226
227
228
                if (BN > 0) outflow += w * BN * U;
                else inflow += w * BN * U;
            }
        }
    }
    inflow = PPM->Sum(inflow);
    outflow = PPM->Sum(outflow);
    if (inflow < 1e-7)
        inflow = 0.0;
    if (outflow < 1e-7)
        outflow = 0.0;
    return {inflow, outflow};
}

niklas.baumgarten's avatar
niklas.baumgarten committed
229
void DGLinearTransportAssemble::SetExactSolution(double t, Vector &u_ex) const {
230
231
232
    u_ex = 0;
    for (cell c = u_ex.cells(); c != u_ex.cells_end(); ++c) {
        row r = u_ex.find_row(c());
niklas.baumgarten's avatar
niklas.baumgarten committed
233
234
235
        DGElement Elem(*disc, u_ex, c);
        for (int j = 0; j < Elem.NodalPoints(); ++j)
            u_ex(r, j) = problem->Solution(t, Elem.NodalPoint(j));
236
    }
237
    u_ex.Accumulate();
238
239
}

niklas.baumgarten's avatar
niklas.baumgarten committed
240
double DGLinearTransportAssemble::Mass(const Vector &u) const {
241
242
243
244
245
246
247
248
249
250
251
252
    Scalar e = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        DGElement elem(*disc, u, c);
        for (int q = 0; q < elem.nQ(); ++q) {
            double w = elem.QWeight(q);
            Scalar U = elem.Value(q, u);
            e += w * U;
        }
    }
    return PPM->Sum(e);
}

niklas.baumgarten's avatar
niklas.baumgarten committed
253
void DGLinearTransportAssemble::SetInitialValue(Vector &u) const {
254
255
256
257
258
259
260
261
262
    u = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        row r = u.find_row(c());
        DGElement Elem(*disc, u, c);
        double lambda = 1e-9;
        for (int j = 0; j < Elem.NodalPoints(); ++j)
            u(r, j) =
                problem->Solution(0, lambda * c() + (1 - lambda) * Elem.NodalPoint(j));
    }
263
    u.Accumulate();
264
265
}

niklas.baumgarten's avatar
niklas.baumgarten committed
266
void DGLinearTransportAssemble::PrintMatrixInfo(Matrix &A, int diagonal) const {
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    for (cell c = A.cells(); c != A.cells_end(); ++c) {
        DGElement elem(*disc, A, c);
        DGRowEntries A_c(A, c, c);
        for (int i = 0; i < elem.NodalPoints(); ++i) {
            for (int j = 0; j < elem.NodalPoints(); ++j)
                mout << A_c(i, j) << " ";
            mout << endl;
        }
        mout << endl;
        if (diagonal) continue;

        for (int f = 0; f < c.Faces(); ++f) {
            cell cf = c;
            if (A.GetMesh().onBndDG(c, f))
                continue;
            else
                cf = A.GetMesh().find_neighbour_cell(c, f);
            DGRowEntries A_cf(A, c, cf);
            for (int i = 0; i < elem.NodalPoints(); ++i) {
                for (int j = 0; j < elem.NodalPoints(); ++j)
                    mout << A_cf(i, j) << " ";
                mout << endl;
            }
            mout << endl;
        }
        mout << "------------------------" << endl;
    }
}

niklas.baumgarten's avatar
niklas.baumgarten committed
296
297
298
void DGLinearTransportAssemble::VtkPlotting_cell(double t,
                                                 const Vector &u,
                                                 char *filename) const {
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    Vector u_tmp(u);
    for (cell c = u_tmp.cells(); c != u_tmp.cells_end(); ++c) {
        DGElement elem(*disc, u_tmp, c);
        Scalar U = 0.0;
        double a = 0;
        for (int q = 0; q < elem.nQ(); ++q) {
            double w = elem.QWeight(q);
            U += w * elem.Value(q, u);
            a += w;
        }
        U *= (1 / a);
        row r = u.find_row(c());
        u_tmp(r)[0] = U;
    }
niklas.baumgarten's avatar
piles    
niklas.baumgarten committed
313
    mpp::plot("U") << u_tmp << mpp::save_plot(NumberName("U", filename, step));
314
315
}

niklas.baumgarten's avatar
niklas.baumgarten committed
316
void DGLinearTransportAssemble::VtkPlotting(double t, const Vector &u) const {
317
318
319
320
    char filename[128];
    VtkPlotting_cell(t, u, filename);
}

niklas.baumgarten's avatar
niklas.baumgarten committed
321
void DGLinearTransportAssemble::PrintInfo() const {
322
323
324
325
326
327
    mout.PrintInfo("Assemble", verbose,
                   PrintInfoEntry("Name", Name()),
                   PrintInfoEntry("Problem", problem->Name()),
                   PrintInfoEntry("Discretization", disc->DiscName()));
}

niklas.baumgarten's avatar
niklas.baumgarten committed
328
void DGLinearTransportAssemble::FinishTimeStep(double t, Vector &u) {
329
330
331
    step++;
    t = t;
    PrintInfo(u);
niklas.baumgarten's avatar
niklas.baumgarten committed
332
    VtkPlotting(t, u);
333
334
}

niklas.baumgarten's avatar
niklas.baumgarten committed
335
void DGLinearTransportAssemble::Initialize(Vector &u) {
336
337
338
    u = 0;
    SetInitialValue(u);
    PrintInfo(u);
niklas.baumgarten's avatar
niklas.baumgarten committed
339
    VtkPlotting(timeSeries.FirstTStep(), u);
340
341
}

niklas.baumgarten's avatar
niklas.baumgarten committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

double DGNonLinearTransportAssemble::Residual(const Vector &u, Vector &b) const {
    b = 0;

    auto *massMatrix = new Matrix(u);
    MassMatrix(*massMatrix);

    auto *fluxMatrix = new Matrix(u);
    SystemMatrix(*fluxMatrix);

    Vector fluxMatrixU(u);
    fluxMatrixU = *fluxMatrix * u;
    fluxMatrixU *= -dt_;
    b = (*massMatrix * u + fluxMatrixU);
    b -= *massMatrix * U_old();

    Vector rhs(b);
    RHS(t_, rhs);
    b -= dt_ * rhs;

    b.ClearDirichletValues();
363
    b.Collect();
niklas.baumgarten's avatar
niklas.baumgarten committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    delete fluxMatrix;
    delete massMatrix;
    return b.norm();
}

void DGNonLinearTransportAssemble::Jacobi(const Vector &u, Matrix &A) const {
    Matrix *massMatrix = new Matrix(u);
    MassMatrix(*massMatrix);

    Matrix *fluxMatrix = new Matrix(u);
    SystemMatrix(*fluxMatrix);
    A = *massMatrix;
    A += -dt_ * (*fluxMatrix);
    delete fluxMatrix;
    delete massMatrix;
}