aboutsummaryrefslogtreecommitdiff
path: root/Source/Utils/Interpolate.cpp
blob: 11d6989a05b123bff27d1733eae3d881a19807ff (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
97
98
99
100
101
102
103
104
#include "Interpolate.H"
#include "Interpolate_K.H"

namespace Interpolate
{
    using namespace amrex;

    std::unique_ptr<MultiFab>
    getInterpolatedScalar(
        const MultiFab& F_cp, const MultiFab& F_fp,
        const DistributionMapping& dm, const int r_ratio,
        const Real* /*dx*/, const int ngrow )
    {
        // Prepare the structure that will contain the returned fields
        std::unique_ptr<MultiFab> interpolated_F;
        interpolated_F.reset( new MultiFab(F_fp.boxArray(), dm, 1, ngrow) );
        interpolated_F->setVal(0.);

        // Loop through the boxes and interpolate the values from the _cp data
#ifdef _OPENMP
#pragma omp parallel
#endif
        {
            FArrayBox ffab; // Temporary array ; contains interpolated fields
            for (MFIter mfi(*interpolated_F); mfi.isValid(); ++mfi)
            {
                Box finebx = mfi.fabbox();
                finebx.coarsen(r_ratio).refine(r_ratio); // so that finebx is coarsenable

                const FArrayBox& cfab = (F_cp)[mfi];
                ffab.resize(finebx);

                // - Fully nodal
                if ( F_fp.is_nodal() ){
                    IntVect refinement_vector{AMREX_D_DECL(r_ratio, r_ratio, r_ratio)};
                    node_bilinear_interp.interp(cfab, 0, ffab, 0, 1,
                                                finebx, refinement_vector, {}, {}, {}, 0, 0, RunOn::Cpu);
                } else {
                    amrex::Abort("Unknown field staggering.");
                }

                // Add temporary array to the returned structure
                const Box& bx = (*interpolated_F)[mfi].box();
                (*interpolated_F)[mfi].plus<RunOn::Host>(ffab, bx, bx, 0, 0, 1);
            }
        }
        return interpolated_F;
    }

    std::array<std::unique_ptr<MultiFab>, 3>
    getInterpolatedVector(
        const MultiFab* Fx_cp,
        const MultiFab* Fy_cp,
        const MultiFab* Fz_cp,
        const MultiFab* Fx_fp,
        const MultiFab* Fy_fp,
        const MultiFab* Fz_fp,
        const DistributionMapping& dm, const int r_ratio,
        const Real* /*dx*/, const int ngrow )
    {

        // Prepare the structure that will contain the returned fields
        std::array<std::unique_ptr<MultiFab>, 3> interpolated_F;
        interpolated_F[0].reset( new MultiFab(Fx_fp->boxArray(), dm, 1, ngrow) );
        interpolated_F[1].reset( new MultiFab(Fy_fp->boxArray(), dm, 1, ngrow) );
        interpolated_F[2].reset( new MultiFab(Fz_fp->boxArray(), dm, 1, ngrow) );

        IntVect fx_type = interpolated_F[0]->ixType().toIntVect();
        IntVect fy_type = interpolated_F[1]->ixType().toIntVect();
        IntVect fz_type = interpolated_F[2]->ixType().toIntVect();

#ifdef _OPENMP
#pragma omp parallel
#endif
        for (MFIter mfi(*interpolated_F[0], TilingIfNotGPU()); mfi.isValid(); ++mfi)
        {
            Box const& boxx = mfi.growntilebox(fx_type);
            Box const& boxy = mfi.growntilebox(fy_type);
            Box const& boxz = mfi.growntilebox(fz_type);

            Array4<Real      > const& fx = interpolated_F[0]->array(mfi);
            Array4<Real      > const& fy = interpolated_F[1]->array(mfi);
            Array4<Real      > const& fz = interpolated_F[2]->array(mfi);
            Array4<Real const> const& cx = Fx_cp->const_array(mfi);
            Array4<Real const> const& cy = Fy_cp->const_array(mfi);
            Array4<Real const> const& cz = Fz_cp->const_array(mfi);

            amrex::ParallelFor(boxx, boxy, boxz,
                [=] AMREX_GPU_DEVICE (int j, int k, int l) noexcept
                {
                    interp(j,k,l,fx,cx,r_ratio,fx_type);
                },
                [=] AMREX_GPU_DEVICE (int j, int k, int l) noexcept
                {
                    interp(j,k,l,fy,cy,r_ratio,fy_type);
                },
                [=] AMREX_GPU_DEVICE (int j, int k, int l) noexcept
                {
                    interp(j,k,l,fz,cz,r_ratio,fz_type);
                });
        }
        return interpolated_F;
    }
}