WarpX
Loading...
Searching...
No Matches
ExternalField.H
Go to the documentation of this file.
1/* Copyright 2023 Luca Fedeli
2 *
3 *
4 * This file is part of WarpX.
5 *
6 * License: BSD-3-Clause-LBNL
7 */
8#ifndef WARPX_EXTERNAL_FIELD_H_
9#define WARPX_EXTERNAL_FIELD_H_
10
11#include "ExternalField_fwd.H"
12
14
15#include <AMReX_Array.H>
16#include <AMReX_GpuContainers.H>
17#include <AMReX_ParmParse.H>
18#include <AMReX_Parser.H>
19#include <AMReX_RealVect.H>
20#include <AMReX_TableData.H>
21
22#include <memory>
23#include <string>
24
33
39{
40
46
51
56
58 std::unique_ptr<amrex::Parser> Bxfield_parser;
60 std::unique_ptr<amrex::Parser> Byfield_parser;
62 std::unique_ptr<amrex::Parser> Bzfield_parser;
64 std::unique_ptr<amrex::Parser> Exfield_parser;
66 std::unique_ptr<amrex::Parser> Eyfield_parser;
68 std::unique_ptr<amrex::Parser> Ezfield_parser;
69
72};
73
75{
77 amrex::Real operator() (amrex::RealVect const& pos) const
78 {
79#if defined(AMREX_USE_GPU)
80 AMREX_IF_ON_HOST((amrex::Abort("For GPU builds, ExternalFieldView only works on device. We could make it work on host if necessary.");))
81#endif
82
83 // Get index of the external field array
84 AMREX_D_TERM(int const i0 = std::floor( (pos[0]-offset[0])/dx[0] );,
85 int const i1 = std::floor( (pos[1]-offset[1])/dx[1] );,
86 int const i2 = std::floor( (pos[2]-offset[2])/dx[2] ));
87
88 // Get coordinates of external grid point
89 AMREX_D_TERM(amrex::Real const xx0 = offset[0] + i0 * dx[0];,
90 amrex::Real const xx1 = offset[1] + i1 * dx[1];,
91 amrex::Real const xx2 = offset[2] + i2 * dx[2]);
92
93#if (AMREX_SPACEDIM == 1)
94
95 if (i0 < table.begin || i0 >= table.end-1) {
96 return 0;
97 } else {
98 return static_cast<amrex::Real>(
100 xx0, xx0+dx[0], table(i0), table(i0+1), pos[0]));
101 }
102
103#elif (AMREX_SPACEDIM == 2)
104
105 if (i0 < table.begin[0] || i0 >= table.end[0]-1 ||
106 i1 < table.begin[1] || i1 >= table.end[1]-1) {
107 return 0;
108 } else {
109 return static_cast<amrex::Real>(
111 xx0, xx0+dx[0], xx1, xx1+dx[1],
112 table(i0 ,i1 ),
113 table(i0 ,i1+1),
114 table(i0+1,i1 ),
115 table(i0+1,i1+1),
116 pos[0], pos[1]));
117 }
118
119#elif (AMREX_SPACEDIM == 3)
120
121 if (i0 < table.begin[0] || i0 >= table.end[0]-1 ||
122 i1 < table.begin[1] || i1 >= table.end[1]-1 ||
123 i2 < table.begin[2] || i2 >= table.end[2]-1) {
124 return 0;
125 } else {
126 return static_cast<amrex::Real>(
128 xx0, xx0+dx[0], xx1, xx1+dx[1], xx2, xx2+dx[2],
129 table(i0 ,i1 ,i2 ),
130 table(i0 ,i1 ,i2+1),
131 table(i0 ,i1+1,i2 ),
132 table(i0 ,i1+1,i2+1),
133 table(i0+1,i1 ,i2 ),
134 table(i0+1,i1 ,i2+1),
135 table(i0+1,i1+1,i2 ),
136 table(i0+1,i1+1,i2+1),
137 pos[0], pos[1], pos[2]));
138 }
139#endif
140 }
141
144#if (AMREX_SPACEDIM == 1)
146#elif (AMREX_SPACEDIM == 2)
148#elif (AMREX_SPACEDIM == 3)
150#endif
151};
152
154{
155public:
156 ExternalFieldReader (std::string const& read_fields_from_path,
157 std::string const& F_name, std::string const& F_component);
158
160
161private:
162 std::shared_ptr<double> m_FC_data_cpu;
165};
166
167#endif //WARPX_EXTERNAL_FIELD_H_
#define AMREX_FORCE_INLINE
#define AMREX_IF_ON_HOST(CODE)
#define AMREX_GPU_HOST_DEVICE
#define AMREX_D_TERM(a, b, c)
ExternalFieldType
Definition ExternalField.H:26
@ default_zero
Definition ExternalField.H:27
@ read_from_file
Definition ExternalField.H:30
@ constant
Definition ExternalField.H:28
@ parse_ext_grid_function
Definition ExternalField.H:29
@ load_from_python
Definition ExternalField.H:31
ExternalFieldReader(std::string const &read_fields_from_path, std::string const &F_name, std::string const &F_component)
Definition ExternalField.cpp:192
ExternalFieldView const & getView() const
Definition ExternalField.H:159
ExternalFieldView m_external_field_view
Definition ExternalField.H:164
std::shared_ptr< double > m_FC_data_cpu
Definition ExternalField.H:162
amrex::Gpu::DeviceVector< double > m_FC_data_gpu
Definition ExternalField.H:163
AMREX_GPU_DEVICE AMREX_FORCE_INLINE constexpr auto linear_interp(TCoord x0, TCoord x1, TVal f0, TVal f1, TCoord x)
Performs a linear interpolation.
Definition LinearInterpolation.H:23
AMREX_GPU_DEVICE AMREX_FORCE_INLINE constexpr auto bilinear_interp(TCoord x0, TCoord x1, TCoord y0, TCoord y1, TVal f00, TVal f01, TVal f10, TVal f11, TCoord x, TCoord y)
Performs a bilinear interpolation.
Definition LinearInterpolation.H:38
AMREX_GPU_DEVICE AMREX_FORCE_INLINE constexpr auto trilinear_interp(TCoord x0, TCoord x1, TCoord y0, TCoord y1, TCoord z0, TCoord z1, TVal f000, TVal f001, TVal f010, TVal f011, TVal f100, TVal f101, TVal f110, TVal f111, TCoord x, TCoord y, TCoord z)
Performs a trilinear interpolation.
Definition LinearInterpolation.H:56
PODVector< T, ArenaAllocator< T > > DeviceVector
RealVectND< 3 > RealVect
void Abort(const std::string &msg)
ExternalFieldType E_ext_grid_type
Initialization type for external electric field on the grid.
Definition ExternalField.H:55
std::unique_ptr< amrex::Parser > Eyfield_parser
User-defined parser to initialize y-component of the electric field on the grid.
Definition ExternalField.H:66
ExternalFieldParams(const amrex::ParmParse &pp_warpx)
The constructor reads and stores the parameters related to the external fields. "pp_warpx" must point...
Definition ExternalField.cpp:68
std::unique_ptr< amrex::Parser > Bzfield_parser
User-defined parser to initialize z-component of the magnetic field on the grid.
Definition ExternalField.H:62
std::unique_ptr< amrex::Parser > Bxfield_parser
User-defined parser to initialize x-component of the magnetic field on the grid.
Definition ExternalField.H:58
amrex::GpuArray< amrex::Real, 3 > E_external_grid
Initial electric field on the grid.
Definition ExternalField.H:48
ExternalFieldType B_ext_grid_type
Initialization type for external magnetic field on the grid.
Definition ExternalField.H:53
std::unique_ptr< amrex::Parser > Byfield_parser
User-defined parser to initialize y-component of the magnetic field on the grid.
Definition ExternalField.H:60
std::string external_fields_path
Path of the file where external fields are stored.
Definition ExternalField.H:71
std::unique_ptr< amrex::Parser > Ezfield_parser
User-defined parser to initialize z-component of the electric field on the grid.
Definition ExternalField.H:68
amrex::GpuArray< amrex::Real, 3 > B_external_grid
Initial magnetic field on the grid.
Definition ExternalField.H:50
std::unique_ptr< amrex::Parser > Exfield_parser
User-defined parser to initialize x-component of the electric field on the grid.
Definition ExternalField.H:64
Definition ExternalField.H:75
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real operator()(amrex::RealVect const &pos) const
Definition ExternalField.H:77
amrex::Table3D< double, amrex::Order::C > table
Definition ExternalField.H:149
amrex::RealVect dx
Definition ExternalField.H:142
amrex::RealVect offset
Definition ExternalField.H:143