aboutsummaryrefslogtreecommitdiff
path: root/Source/Filter/BilinearFilter.cpp
blob: c8e921aad31544afb2e8f8c21958243c7d646040 (plain) (blame)
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
/* Copyright 2019 Andrew Myers, Maxence Thevenet, Weiqun Zhang
 *
 *
 * This file is part of WarpX.
 *
 * License: BSD-3-Clause-LBNL
 */
#include "BilinearFilter.H"

#include "Utils/WarpXProfilerWrapper.H"

#include <AMReX_Config.H>
#include <AMReX_Dim3.H>
#include <AMReX_GpuContainers.H>
#include <AMReX_GpuDevice.H>
#include <AMReX_IntVect.H>
#include <AMReX_REAL.H>
#include <AMReX_Vector.H>

#include <array>
#include <vector>

using namespace amrex;

namespace {
    void compute_stencil(Gpu::DeviceVector<Real> &stencil, unsigned int npass)
    {
        Vector<Real> old_s(1u+npass,0.);
        Vector<Real> new_s(1u+npass,0.);

        old_s.at(0) = 1._rt;
        int jmax = 1;
        // Convolve the filter with itself npass times
        int const lastpass = static_cast<int>(npass+1u);
        for(int ipass=1; ipass< lastpass; ipass++){
            // element 0 has to be treated in its own way
            new_s.at(0) = 0.5_rt * old_s.at(0);
            if (1<jmax) new_s.at(0) += 0.5_rt * old_s.at(1);
            amrex::Real loc = 0._rt;
            // For each element j, apply the filter to
            // old_s to get new_s[j]. loc stores the tmp
            // filtered value.
            for(int j=1; j<jmax+1; j++){
                loc = 0.5_rt * old_s[j];
                loc += 0.25_rt * old_s[j-1];
                if (j<jmax) loc += 0.25_rt * old_s.at(j+1);
                new_s.at(j) = loc;
            }
            // copy new_s into old_s
            old_s = new_s;
            // extend the stencil length for next iteration
            jmax += 1;
        }
        // we use old_s here to make sure the stencil
        // is corrent even when npass = 0
        old_s.at(0) *= 0.5_rt; // because we will use it twice
        stencil.resize(old_s.size());
        Gpu::copyAsync(Gpu::hostToDevice,old_s.begin(),old_s.end(),stencil.begin());
        amrex::Gpu::synchronize();
    }
}

void BilinearFilter::ComputeStencils(){
    WARPX_PROFILE("BilinearFilter::ComputeStencils()");
    int i = 0;
    for (auto el : npass_each_dir )
        stencil_length_each_dir[i++] = el;
    stencil_length_each_dir += 1.;
#if defined(WARPX_DIM_3D)
    // npass_each_dir = npass_x npass_y npass_z
    stencil_x.resize( 1u + npass_each_dir[0] );
    stencil_y.resize( 1u + npass_each_dir[1] );
    stencil_z.resize( 1u + npass_each_dir[2] );
    compute_stencil(stencil_x, npass_each_dir[0]);
    compute_stencil(stencil_y, npass_each_dir[1]);
    compute_stencil(stencil_z, npass_each_dir[2]);
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
    // npass_each_dir = npass_x npass_z
    stencil_x.resize( 1u + npass_each_dir[0] );
    stencil_z.resize( 1u + npass_each_dir[1] );
    compute_stencil(stencil_x, npass_each_dir[0]);
    compute_stencil(stencil_z, npass_each_dir[1]);
#elif defined(WARPX_DIM_1D_Z)
    // npass_each_dir = npass_z
    stencil_z.resize( 1u + npass_each_dir[0] );
    compute_stencil(stencil_z, npass_each_dir[0]);
#endif
    slen = stencil_length_each_dir.dim3();
#if defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
    slen.z = 1;
#endif
#if defined(WARPX_DIM_1D_Z)
    slen.y = 1;
    slen.z = 1;
#endif
}