DGEllipticAssemble.cpp 18.4 KB
Newer Older
1
#include "DGEllipticAssemble.hpp"
2
3


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const char *DGEllipticAssemble::Name() const {
    return "DGEllipticAssemble";
}

void DGEllipticAssemble::Initialize(Vector &u) const {
//    // Todo verify
//    u.ClearDirichletFlags();
//    for (cell c = u.cells(); c != u.cells_end(); ++c) {
//        RowBndValues u_c(u, c);
//        if (!u_c.onBnd()) continue;
//        DGElement elem(*disc, u, c);
//        for (int face = 0; face < c.Faces(); ++face) {
//            if (u_c.bc(face) == 1) {
//                for (int j = 0; j < disc->NodalPointsOnFace(c, face); ++j) {
//                    int k = disc->NodalPointOnFace(c, face, j);
//                    u_c(k) = problem->Solution(elem.NodalPoint(k));
//                    u_c.D(k) = true;
//                }
//            }
//        }
//    }
//    DirichletConsistent(u);
}

double DGEllipticAssemble::Energy(const Vector &u) const {
    double energy = 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);
            VectorField DU = elem.Derivative(q, u);
            Tensor K = problem->Permeability(c);
            VectorField K_DU = K * DU;
            energy += w * K_DU * DU;
        }
    }
    return sqrt(PPM->Sum(energy));
}

43
void DGEllipticAssemble::Residual(const cell &c, const Vector &u, Vector &r) const {
44
    DGElement elem(*disc, u, c);
45
46
47
48
    RowBndValues r_c(r, c);
    for (int q = 0; q < elem.nQ(); ++q) {
        double w = elem.QWeight(q);
        const Point &z = elem.QPoint(q);
49
        Tensor K = problem->Permeability(c);
niklas.baumgarten's avatar
niklas.baumgarten committed
50
        Scalar Load = problem->Load(z);
51
        VectorField K_DU = K * elem.Derivative(q, u);
niklas.baumgarten's avatar
niklas.baumgarten committed
52
        for (int i = 0; i < elem.NodalPoints(); ++i) {
53
54
            Scalar U_i = elem.Value(q, i);
            VectorField DU_i = elem.Derivative(q, i);
55
            r(c(), i) += w * (K_DU * DU_i - Load * U_i);
56
57
58
        }
    }
    for (int f = 0; f < c.Faces(); ++f) {
59
60
61
        Scalar scaledPenalty = penalty / c.FaceArea(f);
        DGFaceElement faceElem(*disc, u, c, f);
        if (r.GetMesh().onBndDG(c, f)) {
62
63
            int bnd = r_c.bc(f);
            if (bnd == 2) {
64
65
66
                for (int q = 0; q < faceElem.nQ(); ++q) {
                    double w = faceElem.QWeight(q);
                    const Point &z = faceElem.QPoint(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
67
                    Scalar F = problem->Flux(z) * faceElem.QNormal(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
68
                    for (int i = 0; i < faceElem.NodalPoints(); ++i) {
69
                        Scalar U_i = faceElem.Value(q, i);
70
71
72
73
                        r(c(), i) -= w * U_i * F;
                    }
                }
            } else if (bnd == 1) {
74
75
76
77
                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);
78
                    Tensor K = problem->Permeability(c);
79
80
                    Scalar U_diff = faceElem.Value(q, u) - problem->Solution(z);
                    VectorField K_DU = K * faceElem.Derivative(q, u);
81
                    Scalar F = K_DU * N;
niklas.baumgarten's avatar
niklas.baumgarten committed
82
                    for (int i = 0; i < faceElem.NodalPoints(); ++i) {
83
84
                        Scalar phi_i = faceElem.Value(q, i);
                        Scalar NDphi_i = (K * faceElem.Derivative(q, i)) * N;
85
                        r(c(), i) -= w * (F * phi_i +
86
                            U_diff * (NDphi_i * sign - scaledPenalty * phi_i));
87
88
89
90
                    }
                }
            }
        } else {
91
            cell cf = r.GetMesh().find_neighbour_cell(c, f);
92
            if (cf() < c()) continue;
93
94
95
96
97
            int f1 = r.GetMesh().find_neighbour_face_id(c.Face(f), cf);
            DGFaceElement otherFaceElem(*disc, r, cf, f1);
            for (int q = 0; q < faceElem.nQ(); ++q) {
                double w = faceElem.QWeight(q);
                const Point &N = faceElem.QNormal(q);
98
                Tensor K = problem->Permeability(c);
99
100
101
102
                Scalar U = faceElem.Value(q, u);
                VectorField K_DU = K * faceElem.Derivative(q, u);
                const Point &Qf_c = faceElem.QPoint(q);
                int q1 = faceElem.findQPointID(otherFaceElem, Qf_c);
103
                Tensor K_1 = problem->Permeability(cf);
104
105
                Scalar U_1 = otherFaceElem.Value(q1, u);
                VectorField K_DU_1 = K_1 * otherFaceElem.Derivative(q1, u);
niklas.baumgarten's avatar
niklas.baumgarten committed
106
                for (int i = 0; i < faceElem.NodalPoints(); ++i) {
107
108
109
110
111
112
                    Scalar phi_i = faceElem.Value(q, i);
                    Scalar NDphi_i = (K * faceElem.Derivative(q, i)) * N;
                    r(c(), i) += w * ((scaledPenalty * U - 0.5 * (K_DU * N)) * phi_i
                        - 0.5 * U * NDphi_i * sign);
                    r(c(), i) += w * ((-scaledPenalty * U_1 - 0.5 * (K_DU_1 * N)) * phi_i
                        + 0.5 * U_1 * NDphi_i * sign);
113
                }
niklas.baumgarten's avatar
niklas.baumgarten committed
114
                for (int j = 0; j < otherFaceElem.NodalPoints(); ++j) {
115
116
117
118
119
120
                    Scalar phi_j = otherFaceElem.Value(q1, j);
                    Scalar NDphi_j = (K_1 * otherFaceElem.Derivative(q1, j)) * N;
                    r(cf(), j) += w * ((0.5 * K_DU_1 * N + scaledPenalty * U_1) * phi_j
                        + 0.5 * U_1 * NDphi_j * sign);
                    r(cf(), j) += w * ((0.5 * K_DU * N - scaledPenalty * U) * phi_j
                        - 0.5 * U * NDphi_j * sign);
121
122
123
124
125
126
127
                }
            }
        }
    }
}

void DGEllipticAssemble::Jacobi(const cell &c, const Vector &u, Matrix &A) const {
128
    DGElement elem(*disc, A, c);
129
130
131
132
    DGRowEntries A_c(A, c, c);
    for (int q = 0; q < elem.nQ(); ++q) {
        double w = elem.QWeight(q);
        Point z = elem.QPoint(q);
133
        Tensor K = problem->Permeability(c);
niklas.baumgarten's avatar
niklas.baumgarten committed
134
        for (int i = 0; i < elem.NodalPoints(); ++i) {
135
            VectorField K_DU_i = K * elem.Derivative(q, i);
niklas.baumgarten's avatar
niklas.baumgarten committed
136
            for (int j = 0; j < elem.NodalPoints(); ++j) {
137
138
139
140
141
142
143
144
                VectorField DU_j = elem.Derivative(q, j);
                A_c(i, j) += w * K_DU_i * DU_j;
            }
        }
    }
    BFParts bnd(u.GetMesh(), c);
    for (int f = 0; f < c.Faces(); ++f) {
        Scalar s = penalty / c.FaceArea(f);
145
146
        DGFaceElement faceElem(*disc, u, c, f);
        if (u.GetMesh().onBndDG(c, f)) {
147
            if (bnd[f] == 1) {
148
149
150
151
                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);
152
                    Tensor K = problem->Permeability(c);
niklas.baumgarten's avatar
niklas.baumgarten committed
153
                    for (int i = 0; i < faceElem.NodalPoints(); ++i) {
154
155
                        Scalar phi_i = faceElem.Value(q, i);
                        Scalar NDphi_i = (K * faceElem.Derivative(q, i)) * N;
niklas.baumgarten's avatar
niklas.baumgarten committed
156
                        for (int j = 0; j < faceElem.NodalPoints(); ++j) {
157
158
                            Scalar phi_j = faceElem.Value(q, j);
                            Scalar NDphi_j = (K * faceElem.Derivative(q, j)) * N;
159
                            A_c(i, j) -= w * (NDphi_i * phi_j * sign +
160
                                phi_i * (NDphi_j - s * phi_j));
161
162
163
164
165
                        }
                    }
                }
            }
        } else {
166
            cell cf = u.GetMesh().find_neighbour_cell(c, f);
167
168
169
170
            if (cf() < c()) continue;
            DGRowEntries A_cf(A, c, cf);
            DGRowEntries A_fc(A, cf, c);
            DGRowEntries A_ff(A, cf, cf);
171
172
173
174
175
176
177
178
            int f1 = u.GetMesh().find_neighbour_face_id(c.Face(f), cf);
            DGFaceElement otherFaceElem(*disc, u, cf, f1);
            for (int q = 0; q < faceElem.nQ(); ++q) {
                double w = faceElem.QWeight(q);
                const Point &N = faceElem.QNormal(q);
                Tensor K = problem->Permeability(cf);
                const Point &Qf_c = faceElem.QPoint(q);
                int q1 = otherFaceElem.findQPointID(faceElem, Qf_c);
179
                Tensor K_1 = problem->Permeability(cf);
niklas.baumgarten's avatar
niklas.baumgarten committed
180
                for (int i = 0; i < faceElem.NodalPoints(); ++i) {
181
182
                    Scalar phi_i = faceElem.Value(q, i);
                    Scalar NDphi_i = (K * faceElem.Derivative(q, i)) * N;
niklas.baumgarten's avatar
niklas.baumgarten committed
183
                    for (int j = 0; j < faceElem.NodalPoints(); ++j) {
184
185
                        Scalar phi_j = faceElem.Value(q, j);
                        Scalar NDphi_j = (K * faceElem.Derivative(q, j)) * N;
186
                        A_c(i, j) += w * (-0.5 * NDphi_i * phi_j * sign
187
188
                            - 0.5 * phi_i * NDphi_j
                            + s * phi_i * phi_j);
189
                    }
niklas.baumgarten's avatar
niklas.baumgarten committed
190
                    for (int j = 0; j < otherFaceElem.NodalPoints(); ++j) {
191
192
                        Scalar phi_j = otherFaceElem.Value(q1, j);
                        Scalar NDphi_j = (K_1 * otherFaceElem.Derivative(q1, j)) * N;
193
                        A_cf(i, j) += w * (0.5 * NDphi_i * phi_j * sign
194
195
                            - phi_i * 0.5 * NDphi_j
                            - s * phi_i * phi_j);
196
                        A_fc(j, i) += w * (0.5 * NDphi_i * phi_j
197
198
                            - phi_i * 0.5 * NDphi_j * sign
                            - s * phi_i * phi_j);
199
200
                    }
                }
niklas.baumgarten's avatar
niklas.baumgarten committed
201
                for (int i = 0; i < otherFaceElem.NodalPoints(); ++i) {
202
203
                    Scalar phi_i = otherFaceElem.Value(q1, i);
                    Scalar NDphi_i = (K_1 * otherFaceElem.Derivative(q1, i)) * N;
niklas.baumgarten's avatar
niklas.baumgarten committed
204
                    for (int j = 0; j < otherFaceElem.NodalPoints(); ++j) {
205
206
                        Scalar phi_j = otherFaceElem.Value(q1, j);
                        Scalar NDphi_j = (K_1 * otherFaceElem.Derivative(q1, j)) * N;
207
                        A_ff(i, j) += w * (0.5 * NDphi_i * phi_j * sign
208
209
                            + phi_i * 0.5 * NDphi_j
                            + s * phi_i * phi_j);
210
211
212
213
214
215
216
                    }
                }
            }
        }
    }
}

217
218
219
220
221
222
223
224
225
226
double DGEllipticAssemble::EnergyError(const Vector &u) const {
    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);
            Point z = elem.QPoint(q);
            Tensor K = problem->Permeability(c);
            Tensor IK = Invert(K);
            VectorField diff = (K * elem.Derivative(q, u) -
niklas.baumgarten's avatar
niklas.baumgarten committed
227
                problem->Flux(z));
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
            err += w * (IK * diff) * diff;
        }
    }
    return sqrt(PPM->Sum(err));
}

double DGEllipticAssemble::L2(const Vector &u) const {
    double l2 = 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);
            l2 += w * U * U;
        }
    }
    return sqrt(PPM->Sum(l2));
}

double DGEllipticAssemble::H1(const Vector &u) const {
    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);
            VectorField DU = elem.Derivative(q, u);
            err += w * U * U + w * DU * DU;
        }
    }
    return sqrt(PPM->Sum(err));
}

double DGEllipticAssemble::L2Error(const Vector &u) const {
    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);
            Scalar Sol = problem->Solution(elem.QPoint(q));
            err += w * (U - Sol) * (U - Sol);
        }
    }
    return sqrt(PPM->Sum(err));
}

double DGEllipticAssemble::L2CellAverageError(const Vector &u) const {
    double err = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        DGElement elem(*disc, u, c);
        double w = 0;
        Scalar U = 0;
        Scalar Sol = 0;
        for (int q = 0; q < elem.nQ(); ++q) {
            w += elem.QWeight(q);
            U += elem.Value(q, u);
            Sol += problem->Solution(elem.QPoint(q));
        }
        err += w * (U - Sol) * (U - Sol);
    }
    return sqrt(PPM->Sum(err));
}

double DGEllipticAssemble::MaxError(const Vector &u) const {
    double err = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        DGElement elem(*disc, u, c);
        for (int q = 0; q < elem.nQ(); ++q) {
            Scalar p = elem.Value(q, u);
            err = max(err, abs(p - problem->Solution(elem.QPoint(q))));
        }
    }
    return PPM->Max(err);
}

double DGEllipticAssemble::FluxError(const Vector &u) const {
    double flux_error = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        BFParts bnd(u.GetMesh(), c);
        if (!bnd.onBnd()) continue;
        for (int face = 0; face < c.Faces(); ++face) {
            if (bnd[face] == 2) {
                DGFaceElement faceElem(*disc, u, c, face);
                for (int q = 0; q < faceElem.nQ(); ++q) {
                    double w = faceElem.QWeight(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
314
                    VectorField G = problem->Flux(faceElem.QPoint(q));
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
                    Tensor K = problem->Permeability(c);
                    VectorField DU = faceElem.Derivative(q, u);
                    Scalar F = (K * DU - G) * faceElem.QNormal(q);
                    flux_error += w * F * F;
                }
            } else if (bnd[face] == 0) {
                DGFaceElement faceElem(*disc, u, c, face);
                for (int q = 0; q < faceElem.nQ(); ++q) {
                    double w = faceElem.QWeight(q);
                    Tensor K = problem->Permeability(c);
                    VectorField DU = faceElem.Derivative(q, u);
                    Scalar F = K * DU * faceElem.QNormal(q);
                    flux_error += w * F * F;
                }
            }
        }
    }
    return sqrt(PPM->Sum(flux_error));
}

double DGEllipticAssemble::FaceError(const Vector &u) const {
    double face_error = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        for (int face = 0; face < c.Faces(); ++face) {
            DGFaceElement faceElem(*disc, u, c, face);
            for (int q = 0; q < faceElem.nQ(); ++q) {
                double w = faceElem.QWeight(q);
                Scalar U = faceElem.Value(q, u);
                Scalar Sol = problem->Solution(faceElem.QPoint(q));
                face_error += w * (U - Sol) * (U - Sol);
            }
        }
    }
    return sqrt(PPM->Sum(face_error));
}

FluxPair DGEllipticAssemble::InflowOutflow(const Vector &u) const {
    double inflow = 0;
    double outflow = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        BFParts bnd(u.GetMesh(), c);
        if (!bnd.onBnd()) continue;
        for (int face = 0; face < c.Faces(); ++face) {
            if (bnd[face] < 0) continue;
            DGFaceElement faceElem(*disc, u, c, face);
            for (int q = 0; q < faceElem.nQ(); ++q) {
                double w = faceElem.QWeight(q);
                Tensor K = problem->Permeability(c);
                VectorField Q = K * faceElem.Derivative(q, u);
                Scalar F = Q * faceElem.QNormal(q);
                if (F > 0) { outflow += w * F; }
                else { inflow += w * F; }
            }
        }
    }
    return {PPM->Sum(inflow), PPM->Sum(outflow)};
}

FluxPair DGEllipticAssemble::PrescribedInflowOutflow(const Vector &u) const {
    double inflow = 0;
    double outflow = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        BFParts bnd(u.GetMesh(), c);
        if (!bnd.onBnd()) continue;
        for (int face = 0; face < c.Faces(); ++face) {
            if (bnd[face] < 2) continue;
            DGFaceElement faceElem(*disc, u, c, face);
            for (int q = 0; q < faceElem.nQ(); ++q) {
                double w = faceElem.QWeight(q);
niklas.baumgarten's avatar
niklas.baumgarten committed
384
                Scalar FN = problem->Flux(faceElem.QPoint(q)) *
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
                    faceElem.QNormal(q);
                if (FN > 0) { outflow += w * FN; }
                else { inflow += w * FN; }
            }
        }
    }
    return {PPM->Sum(inflow), PPM->Sum(outflow)};
}

FluxPair DGEllipticAssemble::OutflowLeftRight(const Vector &u) const {
    double outflowLeft = 0;
    double outflowRight = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        BFParts bnd(u.GetMesh(), c);
        if (!bnd.onBnd()) continue;
        for (int face = 0; face < c.Faces(); ++face) {
            if (bnd[face] != 1) continue;
            DGFaceElement faceElem(*disc, u, c, face);
            for (int q = 0; q < faceElem.nQ(); ++q) {
                double w = faceElem.QWeight(q);
                Tensor K = problem->Permeability(c);
                VectorField Q = K * faceElem.Derivative(q, u);
                Scalar F = Q * faceElem.QNormal(q);
                if (F > 0 && c()[0] <= 1) { outflowLeft += w * F; }
                else if (F > 0 && c()[0] >= 2) outflowRight += w * F;
            }
        }
    }
    return {PPM->Sum(outflowLeft), PPM->Sum(outflowRight)};
}

double DGEllipticAssemble::GoalFunctional(const Vector &u) const {
    double goal = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        BFParts bnd(u.GetMesh(), c);
        if (!bnd.onBnd()) continue;
        for (int face = 0; face < c.Faces(); ++face) {
            if (bnd[face] != 1) continue;
            DGFaceElement faceElem(*disc, u, c, face);
            for (int q = 0; q < faceElem.nQ(); ++q) {
                Point z = faceElem.QPoint(q);
                if (z[0] < 0.25) continue;
                if (z[0] > 0.5) continue;
                double w = faceElem.QWeight(q);
                Tensor K = problem->Permeability(c);
                VectorField Q = K * faceElem.Derivative(q, u);
                Scalar F = Q * faceElem.QNormal(q);
                goal += w * F;
            }
        }
    }
    return PPM->Sum(goal);
}

439
440
441
442
void DGEllipticAssemble::SetExactSolution(Vector &u) const {
    u = 0;
    for (cell c = u.cells(); c != u.cells_end(); ++c) {
        row r = u.find_row(c());
443
        DGElement elem(*disc, u, c);
niklas.baumgarten's avatar
niklas.baumgarten committed
444
445
        for (int j = 0; j < elem.NodalPoints(); ++j)
            u(r, j) = problem->Solution(elem.NodalPoint(j));
446
447
448
449
    }
    Accumulate(u);
}

450
451
452
void DGEllipticAssemble::SetFlux(const Vector &u, Vector &flux) {
}

453
454
455
456
457
458
459
460
461
void DGEllipticAssemble::AssembleTransfer(TransferMatrix &TM) const {
    TM = 0;
    const matrixgraph &cg = TM.CoarseMatrixGraph();
    const matrixgraph &fg = TM.FineMatrixGraph();
    const Mesh &mesh = fg.GetMesh();
    for (cell c = cg.cells(); c != cg.cells_end(); ++c)
        for (int k = 0; k < c.Children(); ++k)
            TM(c(), c.Child(k))[0] = 1;
}