aboutsummaryrefslogtreecommitdiff
path: root/Source/Diagnostics/FieldIO.cpp
blob: f20b756fa05e4655ca5ae56ac001b5bdf7968e2d (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
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
/* Copyright 2019-2020 Andrew Myers, Axel Huebl, David Grote, Maxence Thevenet,
 * Remi Lehe, Revathi Jambunathan, Weiqun Zhang, Edoardo Zoni
 *
 * This file is part of WarpX.
 *
 * License: BSD-3-Clause-LBNL
 */
#include "FieldIO.H"
#include "WarpX.H"
#include "Utils/CoarsenIO.H"
#include "Utils/WarpXUtil.H"

#ifdef WARPX_USE_PSATD
#   include "FieldSolver/SpectralSolver/SpectralSolver.H"
#endif

#ifdef WARPX_USE_OPENPMD
#   include <openPMD/openPMD.hpp>
#endif

using namespace amrex;

/** \brief
 * Convert an IntVect to a std::vector<std::uint64_t>
 * and reverse the order of the elements
 * (used for compatibility with the openPMD API)
 */
std::vector<std::uint64_t>
getReversedVec( const IntVect& v )
{
  // Convert the IntVect v to and std::vector u
  std::vector<std::uint64_t> u = {
    AMREX_D_DECL(
                 static_cast<std::uint64_t>(v[0]),
                 static_cast<std::uint64_t>(v[1]),
                 static_cast<std::uint64_t>(v[2])
                 )
  };
  // Reverse the order of elements, if v corresponds to the indices of a
  // Fortran-order array (like an AMReX FArrayBox)
  // but u is intended to be used with a C-order API (like openPMD)
  std::reverse( u.begin(), u.end() );

  return u;
}

/** \brief
 * Convert Real* pointer to a std::vector<double>,
 * and reverse the order of the elements
 * (used for compatibility with the openPMD API)
 */
std::vector<double>
getReversedVec( const Real* v )
{
  // Convert Real* v to and std::vector u
  std::vector<double> u = {
    AMREX_D_DECL(
                 static_cast<double>(v[0]),
                 static_cast<double>(v[1]),
                 static_cast<double>(v[2])
                 )
  };
  // Reverse the order of elements, if v corresponds to the indices of a
  // Fortran-order array (like an AMReX FArrayBox)
  // but u is intended to be used with a C-order API (like openPMD)
  std::reverse( u.begin(), u.end() );

  return u;
}

#ifdef WARPX_DIM_RZ
void
ConstructTotalRZVectorField (const std::array< std::unique_ptr<MultiFab>, 3 >& vector_total,
                             const std::array< std::unique_ptr<MultiFab>, 3 >& vector_field)
{
    // Sum over the real components, giving quantity at theta=0
    MultiFab::Copy(*vector_total[0], *vector_field[0], 0, 0, 1, vector_field[0]->nGrowVect());
    MultiFab::Copy(*vector_total[1], *vector_field[1], 0, 0, 1, vector_field[1]->nGrowVect());
    MultiFab::Copy(*vector_total[2], *vector_field[2], 0, 0, 1, vector_field[2]->nGrowVect());
    for (int ic=1 ; ic < vector_field[0]->nComp() ; ic += 2) {
        MultiFab::Add(*vector_total[0], *vector_field[0], ic, 0, 1, vector_field[0]->nGrowVect());
        MultiFab::Add(*vector_total[1], *vector_field[1], ic, 0, 1, vector_field[1]->nGrowVect());
        MultiFab::Add(*vector_total[2], *vector_field[2], ic, 0, 1, vector_field[2]->nGrowVect());
    }
}

void
ConstructTotalRZScalarField (MultiFab& scalar_total,
                            const MultiFab& scalar_field)
{
    // Sum over the real components, giving quantity at theta=0
    MultiFab::Copy(scalar_total, scalar_field, 0, 0, 1, scalar_field.nGrowVect());
    for (int ic=1 ; ic < scalar_field.nComp() ; ic += 2) {
        MultiFab::Add(scalar_total, scalar_field, ic, 0, 1, scalar_field.nGrowVect());
    }
}
#endif

/** \brief Takes an array of 3 MultiFab `vector_field`
 * (representing the x, y, z components of a vector),
 * averages it to the cell center, and stores the
 * resulting MultiFab in mf_avg (in the components dcomp to dcomp+2)
 * Should only be used for BTD now.
 */
void
AverageAndPackVectorField( MultiFab& mf_avg,
                           const std::array< std::unique_ptr<MultiFab>, 3 >& vector_field,
                           const DistributionMapping& dm,
                           const int dcomp, const int ngrow )
{
#ifndef WARPX_DIM_RZ
    (void)dm;
#endif

#ifdef WARPX_DIM_RZ
    // Note that vector_total is declared in the same way as
    // vector_field so that it can be handled the same way.
    std::array<std::unique_ptr<MultiFab>,3> vector_total;
    if (vector_field[0]->nComp() > 1) {
        // With the RZ solver, if there are more than one component, the total
        // fields needs to be constructed in temporary MultiFabs.
        vector_total[0].reset(new MultiFab(vector_field[0]->boxArray(), dm, 1, vector_field[0]->nGrowVect()));
        vector_total[1].reset(new MultiFab(vector_field[1]->boxArray(), dm, 1, vector_field[1]->nGrowVect()));
        vector_total[2].reset(new MultiFab(vector_field[2]->boxArray(), dm, 1, vector_field[2]->nGrowVect()));
        ConstructTotalRZVectorField(vector_total, vector_field);
    } else {
        // Create aliases of the MultiFabs
        vector_total[0].reset(new MultiFab(*vector_field[0], amrex::make_alias, 0, 1));
        vector_total[1].reset(new MultiFab(*vector_field[1], amrex::make_alias, 0, 1));
        vector_total[2].reset(new MultiFab(*vector_field[2], amrex::make_alias, 0, 1));
    }
#else
    const std::array<std::unique_ptr<MultiFab>,3> &vector_total = vector_field;
#endif

    CoarsenIO::Coarsen( mf_avg, *(vector_total[0]), dcomp  , 0, 1, ngrow );
    CoarsenIO::Coarsen( mf_avg, *(vector_total[1]), dcomp+1, 0, 1, ngrow );
    CoarsenIO::Coarsen( mf_avg, *(vector_total[2]), dcomp+2, 0, 1, ngrow );
}

/** \brief Take a MultiFab `scalar_field`
 * averages it to the cell center, and stores the
 * resulting MultiFab in mf_avg (in the components dcomp)
 */
void
AverageAndPackScalarField (MultiFab& mf_avg,
                           const MultiFab & scalar_field,
                           const DistributionMapping& dm,
                           const int dcomp, const int ngrow )
{

#ifdef WARPX_DIM_RZ
    MultiFab *scalar_total;
    if (scalar_field.nComp() > 1) {
        // With the RZ solver, there are more than one component, so the total
        // fields needs to be constructed in temporary a MultiFab.
        scalar_total = new MultiFab(scalar_field.boxArray(), dm, 1, scalar_field.nGrowVect());
        ConstructTotalRZScalarField(*scalar_total, scalar_field);
    } else {
        scalar_total = new MultiFab(scalar_field, amrex::make_alias, 0, 1);
    }
#else
    const MultiFab *scalar_total = &scalar_field;
#endif

    // Check the type of staggering of the 3-component `vector_field`
    // and average accordingly:
    // - Fully cell-centered field (no average needed; simply copy)
    if ( scalar_total->is_cell_centered() ){
        MultiFab::Copy( mf_avg, *scalar_total, 0, dcomp, 1, ngrow);
    } else if ( scalar_total->is_nodal() ){
        // - Fully nodal
        CoarsenIO::Coarsen( mf_avg, *scalar_total, dcomp, 0, 1, ngrow );
    } else {
        amrex::Abort("Unknown staggering.");
    }
}