1
       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
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     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
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
     180
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     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
     314
     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
     384
     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
     439
     440
     441
     442
     443
     444
     445
     446
     447
     448
     449
     450
     451
     452
     453
     454
     455
     456
     457
     458
     459
     460
     461
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
#include <cmath>
#include <memory>
#include <vector>

#include <hpc/matvec/axpy.hpp>
#include <hpc/matvec/copy.hpp>
#include <hpc/matvec/densevector.hpp>
#include <hpc/matvec/iterators.hpp>
#include <hpc/matvec/print.hpp>
#include <hpc/matvec/traits.hpp>

//------------------------------------------------------------------------------

using namespace hpc;
using namespace hpc::matvec;


template <typename T>
const T &
assertEqual(const T &a, const T &b)
{
    assert(a==b);
    return a;
}

//------------------------------------------------------------------------------

template <
    typename F, typename G,
    typename T, template<typename> class VectorF,
                template<typename> class VectorU,
                Require< Dense<VectorF<T>>,
                         Dense<VectorU<T>> > = true
>
void
discretizeProblem(const F &f, const G &g, VectorF<T> &f_h, VectorU<T> &u_h)
{
    auto N  = assertEqual(f_h.length(), u_h.length()) - 2;
    auto h  = T(1)/(N+1);

    u_h(0)   = g(0);
    u_h(N+1) = g(1);

    for (auto [i, fi] : f_h) {
        fi = f(i*h);
    }
}

//------------------------------------------------------------------------------

template <
    typename U,
    typename T, template<typename> class VectorU,
                template<typename> class VectorE,
                Require< Dense<VectorU<T>>,
                         Dense<VectorE<T>> > = true
>
void
evalError(const U &u, const VectorU<T> &u_h, VectorE<T> &e_h)
{
    auto N  = assertEqual(u_h.length(), e_h.length()) - 2;
    auto h  = T(1)/(N+1);

    for (auto [i, ei] : e_h) {
        ei = u(i*h) - u_h(i);
    }
}

//------------------------------------------------------------------------------

template <
    typename T, template<typename> class VectorU,
                template<typename> class VectorF,
                template<typename> class VectorR,
                Require< Dense<VectorF<T>>,
                         Dense<VectorU<T>>,
                         Dense<VectorR<T>> > = true
>
void
evalResidual(const VectorU<T> &u_h, const VectorF<T> &f_h, VectorR<T> &r_h)
{
    auto N  = assertEqual(u_h.length(), r_h.length()) - 2;
    auto h  = T(1)/(N+1);
    auto h2 = h*h;

    r_h(0) = r_h(N+1) = 0;
    for (std::size_t i=1; i<=N; ++i) {
        r_h(i) = f_h(i) - 1./h2 * (2*u_h(i) - u_h(i-1) - u_h(i+1));
    }
}

//------------------------------------------------------------------------------

template <
    typename T, template<typename> class VectorR1,
                template<typename> class VectorR2,
                Require< Dense<VectorR1<T>>,
                         Dense<VectorR2<T>> > = true
>
void
restriction(const VectorR1<T> &r_h, VectorR2<T> &r_2h)
{
    auto N  = r_h.length() -2;
    auto n  = r_2h.length() -2;

    assert(2*(n+1) == N+1);

    r_2h(0) = r_2h(n+1) = 0;
    for (std::size_t i=1; i<=n; ++i) {
        r_2h(i) = 0.25*r_h(2*i-1) + 0.5*r_h(2*i) + 0.25*r_h(2*i+1);
    }
}

//------------------------------------------------------------------------------

template <
    typename T, template<typename> class VectorU1,
                template<typename> class VectorU2,
                Require< Dense<VectorU1<T>>,
                         Dense<VectorU2<T>> > = true
>
void
updateProlongation(const VectorU1<T> &u_2h, VectorU2<T> &u_h)
{
    auto N  = u_h.length() -2;
    auto n  = u_2h.length() -2;

    assert(2*(n+1) == N+1);

    for (std::size_t i=1; i<=n; ++i) {
        u_h(2*i-1) += 0.5 * u_2h(i);
        u_h(2*i  ) += 1.0 * u_2h(i);
        u_h(2*i+1) += 0.5 * u_2h(i);
    }
}

//------------------------------------------------------------------------------

template <
    typename T, template<typename> class Vector,
                Require< Dense<Vector<T>> > = true
>
void
addNoise(std::size_t k, Vector<T> &x_h)
{
    auto N  = x_h.length()-2;
    auto h  = T(1)/(N+1);

    for (std::size_t i=1; i<=N; ++i) {
        x_h(i) += sin(M_PI*k*i*h);
    }
}

//------------------------------------------------------------------------------

template <
    typename T, template<typename> class Vector,
                Require< Dense<Vector<T>> > = true
>
T
lInfNorm(const Vector<T> &x_h)
{
    T res = 0;

    for (auto [i, xi] : x_h) {
        i=i;
        if (std::abs(xi) > res) {
            res = std::abs(xi);
        }
    }
    return res;
}

template <
    typename T, template<typename> class Vector1,
                template<typename> class Vector2,
                Require< Dense<Vector1<T>>,
                         Dense<Vector2<T>> > = true
>
T
lInfNorm(const Vector1<T> &x1_h, const Vector2<T> &x2_h)
{
    auto N  = assertEqual(x1_h.length(), x2_h.length());

    T res = 0;

    for (std::size_t i=0; i<N; ++i) {
        if (std::abs(x1_h(i) - x2_h(i)) > res) {
            res = std::abs(x1_h(i) - x2_h(i));
        }
    }
    return res;
}


//------------------------------------------------------------------------------

template <
    typename T, template<typename> class Vector,
                Require< Dense<Vector<T>> > = true
>
T
l1Norm(const Vector<T> &x_h)
{
    auto N  = x_h.length()-2;
    auto h  = T(1)/(N+1);

    T res = 0;

    for (std::size_t i=1; i<=N; ++i) {
        i=i;
        res += 0.5*(std::abs(x_h(i-1)) + std::abs(x_h(i)));
    }
    return h*res;
}

//------------------------------------------------------------------------------


template <
    typename T, template<typename> class VectorF,
                template<typename> class VectorU,
                template<typename> class VectorS,
                Require< Dense<VectorF<T>>,
                         Dense<VectorU<T>>,
                         Dense<VectorS<T>> > = true
>
void
jacobi_step(double omega, const VectorF<T> &f_h, const VectorU<T> &u_h,
            VectorS<T> &s_h)
{
    auto N  = assertEqual(f_h.length(), u_h.length()) - 2;
    auto h  = T(1)/(N+1);
    auto h2 = h*h;

    for (std::size_t i=1; i<=N; ++i) {
        s_h(i) = (1-omega)*u_h(i) + omega*0.5*(u_h(i-1) +u_h(i+1)+h2*f_h(i));
    }
    s_h(0)   = u_h(0);
    s_h(N+1) = u_h(N+1);
}

//------------------------------------------------------------------------------

template <
    typename T, template<typename> class VectorF,
                template<typename> class VectorU,
                Require< Dense<VectorF<T>>,
                         Dense<VectorU<T>> > = true
>
void
direct_hom_solver(const VectorF<T> &f_h, VectorU<T> &u_h)
{
    auto N  = assertEqual(f_h.length(), u_h.length()) - 2;
    auto h  = T(1)/(N+1);
    auto h2 = h*h;

    // forward substitution
    u_h(1) = h2*f_h(1);
    for (std::size_t k=2; k<N; ++k) {
        u_h(k) = h2*f_h(k) + u_h(k-1)*(k-1)/k;
    }
    u_h(N) = h2*f_h(N)+u_h(N+1) + u_h(N-1)*(N-1)/N;

    // backward substitution
    u_h(N) = u_h(N)*N/(N+1);
    for (std::size_t k=N-1; k>=1; --k) {
        u_h(k) = (u_h(k)+u_h(k+1))*k/(k+1);
    }
}

//------------------------------------------------------------------------------

template <typename F, typename G, typename U>
void
test_jacobi_solver(std::size_t N, std::size_t k, double omega,
                   const F &f, const G &g, const U &u)
{
    DenseVector<double>     f_h(N+2), u_h(N+2), s_h(N+2), e_h(N+2), r_h(N+2);

    discretizeProblem(f, g, f_h, u_h);

    // initialize u_h with noised solution
    evalError(u, u_h, e_h);
    axpy(1.0, e_h, u_h);
    addNoise(k, u_h);

    for (int it=0; it<25; ++it) {
        jacobi_step(omega, f_h, u_h, s_h);
        evalError(u, u_h, e_h);
        fmt::printf("it = %2d, L1-err = %f\n", it, lInfNorm(e_h));
        copy(s_h, u_h);
        evalResidual(u_h, f_h, r_h);
    }
}

//------------------------------------------------------------------------------

void
js_test1()
{
    auto f = [](double x) { return -2; };
    auto g = [](double x) { return 1 + 3*x; };
    auto u = [](double x) { return (x+1)*(x+1); };

    std::size_t N = 10;

    for (std::size_t k=1; k<=N; ++k) {
        fmt::printf("k = %2d\n", k);
        test_jacobi_solver(N, k, 0.5, f, g, u);
    }
}

void
js_test2()
{
    auto f = [](double x) { return -exp(x); };
    auto g = [](double x) { return exp(x); };
    auto u = [](double x) { return exp(x); };

    std::size_t N = 10;

    for (std::size_t k=1; k<=N; ++k) {
        fmt::printf("k = %2d\n", k);
        test_jacobi_solver(N, k, 0.5, f, g, u);
    }
}

void
coarseGridCorrection()
{
    auto f = [](double x) { return 2; };
    auto g = [](double x) { return 0; };
    auto u = [](double x) { return x*(1-x); };

    std::size_t N = 1023, n = 511;
    double      omega = 0.5;

    DenseVector<double>  f_h(N+2), u_h(N+2), s_h(N+2), e_h(N+2), r_h(N+2);
    DenseVector<double>  f_2h(n+2), u_2h(n+2), s_2h(n+2), r_2h(n+2);

    discretizeProblem(f, g, f_h, u_h);

    for (auto [i,xi] : u_2h) {
        i=i;
        xi = 0;
    }

    // initialize u_h with noised solution
    evalError(u, u_h, e_h);
    axpy(1.0, e_h, u_h);
    addNoise(3, u_h);
    addNoise(9, u_h);
    addNoise(12, u_h);
    copy(u_h, s_h);

    evalError(u, u_h, e_h);
    evalResidual(u_h, f_h, r_h);

    fmt::printf("lInf-err = %5.2e, l1Norm-res = %.18e\n",
                lInfNorm(e_h), l1Norm(r_h));

    for (int it=0; it<30; ++it) {
        // (pre) smooth
        jacobi_step(omega, f_h, u_h, s_h);
        jacobi_step(omega, f_h, s_h, u_h);

        // coarse grid correction
        evalResidual(u_h, f_h, r_h);
        restriction(r_h, f_2h);
        direct_hom_solver(f_2h, u_2h);
        updateProlongation(u_2h, u_h);

        // (post) smooth
        jacobi_step(omega, f_h, u_h, s_h);
        jacobi_step(omega, f_h, s_h, u_h);

        evalError(u, u_h, e_h);
        evalResidual(u_h, f_h, r_h);

        fmt::printf("%3d: lInf-err = %5.2e, l1Norm-res = %.18e\n",
                    it+1, lInfNorm(e_h), l1Norm(r_h));
    }
}

void
multigrid()
{
    using DenseVectorPtr = std::shared_ptr<DenseVector<double>>;

    auto f = [](double x) { return -2; };
    auto g = [](double x) { return 1 + 3*x; };
    auto u = [](double x) { return (x+1)*(x+1); };

    /*
    auto f = [](double x) { return -exp(x); };
    auto g = [](double x) { return exp(x); };
    auto u = [](double x) { return exp(x); };
    */

    /*
    auto f = [](double x) { return 2; };
    auto g = [](double x) { return 0; };
    auto u = [](double x) { return x*(1-x); };
    */

    // Setup grid hierarchy
    // --------------------
    std::size_t L     = 13;             // number of levels
    std::size_t N0    = 1024;              // free nodes on coarsest grid
    double      omega = 0.5;            // smoothing parameter
    std::size_t S     = 1;              // smoothing parameter

    std::vector<DenseVectorPtr>  f_ptr(L), u_ptr(L), s_ptr(L), r_ptr(L);

    for (std::size_t l=0; l<L; ++l) {   // allocate memory for each level
        std::size_t n = N0 << l;

        fmt::printf("%2d: %5d\n", l, n+1);

        f_ptr[l] = std::make_shared<DenseVector<double>>(n+1);
        u_ptr[l] = std::make_shared<DenseVector<double>>(n+1);
        s_ptr[l] = std::make_shared<DenseVector<double>>(n+1);
        r_ptr[l] = std::make_shared<DenseVector<double>>(n+1);

    }

    // Initialize problem
    // ------------------

    discretizeProblem(f, g, *f_ptr[L-1], *u_ptr[L-1]);

    // discrete solution
    DenseVector<double> d_h(u_ptr[L-1]->length());
    DenseVector<double> dr_h(u_ptr[L-1]->length());
    direct_hom_solver(*f_ptr[L-1], d_h);
    evalResidual(d_h, *f_ptr[L-1], dr_h);
    fmt::printf("res for d_h = %.9e\n", l1Norm(dr_h));

    // initialize u_h with noised solution
    DenseVector<double> e_h(u_ptr[L-1]->length());
    evalError(u, *u_ptr[L-1], e_h);
    axpy(1.0, e_h, *u_ptr[L-1]);
    addNoise(3, *u_ptr[L-1]);
    addNoise(9, *u_ptr[L-1]);
    addNoise(12, *u_ptr[L-1]);
    evalError(u, *u_ptr[L-1], e_h);
    evalResidual(*u_ptr[L-1], *f_ptr[L-1], *r_ptr[L-1]);

    fmt::printf("lInf-err = %5.2e, l1Norm-res = %.18e\n",
                 lInfNorm(e_h), l1Norm(*r_ptr[L-1]));


    // Multigrid iterations
    // --------------------

    for (int v=0; v<40; ++v) {

        // V-cycle
        // -------

        for (std::size_t l=L-1; l>=1; --l) {
            // (pre) smooth
            auto &f_h  = *f_ptr[l];
            auto &u_h  = *u_ptr[l];
            auto &s_h  = *s_ptr[l];

            for (std::size_t s=0; s<S; ++s) {
                jacobi_step(omega, f_h, u_h, s_h);
                jacobi_step(omega, f_h, s_h, u_h);
            }

            // restrict to lower level
            auto &r_h  = *r_ptr[l];
            auto &f_2h = *f_ptr[l-1];
            auto &u_2h = *u_ptr[l-1];

            evalResidual(u_h, f_h, r_h);
            restriction(r_h, f_2h);

            for (auto [i,xi] : u_2h) {
                i=i;
                xi = 0;
            }
        }

        // direct solver on coarsest grid
        direct_hom_solver(*f_ptr[0], *u_ptr[0]);

        for (std::size_t l=0; l<L-1; ++l) {

            // update
            auto &u_2h = *u_ptr[l];
            auto &u_h  = *u_ptr[l+1];

            updateProlongation(u_2h, u_h);

            // (post) smooth
            auto &s_h  = *s_ptr[l+1];
            auto &f_h  = *f_ptr[l+1];

            for (std::size_t s=0; s<S; ++s) {
                jacobi_step(omega, f_h, u_h, s_h);
                jacobi_step(omega, f_h, s_h, u_h);
            }
        }

        evalError(u, *u_ptr[L-1], e_h);
        evalResidual(*u_ptr[L-1], *f_ptr[L-1], *r_ptr[L-1]);
        fmt::printf("%3d: lInf-err = %5.2e, l1Norm-res = %.18e, d-err = %.8e\n",
                    v, lInfNorm(e_h), l1Norm(*r_ptr[L-1]),
                    lInfNorm(*u_ptr[L-1], d_h));
    }
}

int
main()
{
    fmt::printf("Multi grid:\n");
    multigrid();

    /*
    fmt::printf("Coarse grid:\n");
    coarseGridCorrection();
    */
}