OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
AmrMultiGridLevel.hpp
Go to the documentation of this file.
1//
2// Class AmrMultiGridLevel
3// This class represents a single AMR level, i.e. it stores all matrices
4// and vectors of a level.
5//
6// Copyright (c) 2017 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
7// All rights reserved
8//
9// Implemented as part of the PhD thesis
10// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
11//
12// This file is part of OPAL.
13//
14// OPAL is free software: you can redistribute it and/or modify
15// it under the terms of the GNU General Public License as published by
16// the Free Software Foundation, either version 3 of the License, or
17// (at your option) any later version.
18//
19// You should have received a copy of the GNU General Public License
20// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
21//
22#define AMR_NO_SCALE false
23
24
25template <class MatrixType, class VectorType>
26AmrMultiGridLevel<MatrixType,
27 VectorType>::AmrMultiGridLevel(const Vector_t& meshScaling,
28 const amrex::BoxArray& _grids,
29 const amrex::DistributionMapping& _dmap,
30 const AmrGeometry_t& _geom,
31 const AmrIntVect_t& rr,
32 const boundary_t* bc,
33 const Teuchos::RCP<comm_t>& comm)
34 : grids(_grids),
35 dmap(_dmap),
36 geom(_geom),
37 map_p(Teuchos::null),
38 Anf_p(Teuchos::null),
39 R_p(Teuchos::null),
40 I_p(Teuchos::null),
41 Bcrse_p(Teuchos::null),
42 Bfine_p(Teuchos::null),
43 Awf_p(Teuchos::null),
44 rho_p(Teuchos::null),
45 phi_p(Teuchos::null),
46 residual_p(Teuchos::null),
47 error_p(Teuchos::null),
48 UnCovered_p(Teuchos::null),
49 refmask(nullptr),
50 crsemask(nullptr),
51 rr_m(rr)
52{
53 for (int j = 0; j < AMREX_SPACEDIM; ++j) {
54 G_p[j] = Teuchos::null;
55
56 nr_m[j] = _geom.Domain().length(j);
57
58#if AMR_NO_SCALE
59 // mesh spacing in particle rest frame
60 dx_m[j] = geom.CellSize(j);
61 invdx_m[j] = geom.InvCellSize(j);
62#else
63 // mesh spacing in particle rest frame
64 dx_m[j] = meshScaling[j] * geom.CellSize(j);
65 invdx_m[j] = meshScaling[j] * geom.InvCellSize(j);
66#endif
67
68 bc_mp[j] = bc[j];
69 }
70
71 this->buildLevelMask();
72
73 this->buildMap(comm);
74
75 residual_p = Teuchos::rcp( new vector_t(map_p, false) );
76 error_p = Teuchos::rcp( new vector_t(map_p, false) );
77}
78
79
80template <class MatrixType, class VectorType>
82{
83 map_p = Teuchos::null;
84
85 Anf_p = Teuchos::null;
86 R_p = Teuchos::null;
87 I_p = Teuchos::null;
88 Bcrse_p = Teuchos::null;
89 Bfine_p = Teuchos::null;
90 Awf_p = Teuchos::null;
91
92 for (int j = 0; j < AMREX_SPACEDIM; ++j)
93 G_p[j] = Teuchos::null;
94
95 UnCovered_p = Teuchos::null;
96
97 rho_p = Teuchos::null;
98 phi_p = Teuchos::null;
99 residual_p = Teuchos::null;
100 error_p = Teuchos::null;
101}
102
103
104template <class MatrixType, class VectorType>
107#if AMREX_SPACEDIM == 3
108 return iv[0] + (iv[1] + nr_m[1] * iv[2]) * nr_m[0];
109#else
110 return iv[0] + iv[1] * nr_m[0];
111#endif
112}
113
114
115template <class MatrixType, class VectorType>
117 // it doesn't matter with which direction we check, since it checks all
118 return bc_mp[0]->isBoundary(iv, &nr_m[0]);
119}
120
121
122template <class MatrixType, class VectorType>
124 umap_t& map,
125 const scalar_t& value)
126{
127 bool applied = false;
128 for (int d = 0; d < AMREX_SPACEDIM; ++d) {
129 if ( bc_mp[d]->isBoundary(iv, d, &nr_m[0]) ) {
130 applied = true;
131 bc_mp[d]->apply(iv, d, map, value, this, &nr_m[0]);
132 }
133 }
134 return applied;
135}
136
137
138template <class MatrixType, class VectorType>
140 const basefab_t& fab,
141 umap_t& map,
142 const scalar_t& value)
143{
144 if ( fab(iv) != Mask::PHYSBNDRY )
145 return false;
146
147 bool applied = false;
148 for (int d = 0; d < AMREX_SPACEDIM; ++d) {
149 if ( bc_mp[d]->isBoundary(iv, d, &nr_m[0]) ) {
150 applied = true;
151 bc_mp[d]->apply(iv, d, map, value, this, &nr_m[0]);
152 }
153 }
154 return applied;
155}
156
157
158template <class MatrixType, class VectorType>
160 const lo_t& dir,
161 umap_t& map,
162 const scalar_t& value)
163{
164 bc_mp[dir]->apply(iv, dir, map, value, this, &nr_m[0]);
165}
166
167
168template <class MatrixType, class VectorType>
170 amrex::Periodicity period(AmrIntVect_t(D_DECL(0, 0, 0)));
171 mask.reset(new mask_t(grids, dmap, 1, 1));
172 mask->BuildMask(geom.Domain(), period,
173 Mask::COVERED, Mask::BNDRY,
174 Mask::PHYSBNDRY, Mask::INTERIOR);
175 mask->FillBoundary(period);
176}
177
178
179template <class MatrixType, class VectorType>
183
184
185template <class MatrixType, class VectorType>
189
190
191template <class MatrixType, class VectorType>
193 return dx_m[dir];
194}
195
196
197template <class MatrixType, class VectorType>
201
202
203template <class MatrixType, class VectorType>
205 return invdx_m[dir];
206}
207
208
209template <class MatrixType, class VectorType>
211 return ( iv.allGT(AmrIntVect_t(D_DECL(-1, -1, -1))) &&
212 iv.allLT(AmrIntVect_t(D_DECL(nr_m[0], nr_m[1], nr_m[2]))) );
213}
214
215
216template <class MatrixType, class VectorType>
217void AmrMultiGridLevel<MatrixType, VectorType>::buildMap(const Teuchos::RCP<comm_t>& comm)
218{
219 Teuchos::Array<go_t> globalindices;
220
221 for (amrex::MFIter mfi(grids, dmap, true); mfi.isValid(); ++mfi) {
222 const amrex::Box& tbx = mfi.tilebox();
223 const int* lo = tbx.loVect();
224 const int* hi = tbx.hiVect();
225
226 for (int i = lo[0]; i <= hi[0]; ++i) {
227 for (int j = lo[1]; j <= hi[1]; ++j) {
228#if AMREX_SPACEDIM == 3
229 for (int k = lo[2]; k <= hi[2]; ++k) {
230#endif
231 AmrIntVect_t iv(D_DECL(i, j, k));
232
233 go_t globalidx = serialize(iv);
234
235 globalindices.push_back(globalidx);
236
237#if AMREX_SPACEDIM == 3
238 }
239#endif
240 }
241 }
242 }
243
244 /*
245 * create map that specifies which processor gets which data
246 */
247
248 // get smallest global index of this level
249 amrex::Box bx = grids.minimalBox();
250 const int* lo = bx.loVect();
251 AmrIntVect_t lowcorner(D_DECL(lo[0], lo[1], lo[2]));
252
253 // where to start indexing
254 go_t baseIndex = serialize(lowcorner);
255
256 // numGlobalElements == N
257 go_t N = grids.numPts();
258
259 map_p = Teuchos::rcp( new dmap_t(N, globalindices, baseIndex, comm) );
260}
void serialize(Param_t params, std::ostringstream &os)
serializes params to a text stream, replaces boost::serialization
Definition MPIHelper.cpp:46
amrex::IntVect AmrIntVect_t
Definition AmrDefs.h:48
double scalar_t
Teuchos::RCP< matrix_t > G_p[AMREX_SPACEDIM]
gradient matrices in x, y, and z to compute electric field
std::unordered_map< go_t, scalar_t > umap_t
amrex::FabArray< basefab_t > mask_t
amr::local_ordinal_t lo_t
scalar_t invdx_m[AMREX_SPACEDIM]
inverse cell size in particle rest frame
bool isValid(const AmrIntVect_t &iv) const
bool applyBoundary(const AmrIntVect_t &iv, umap_t &map, const scalar_t &value)
amrex::BaseFab< int > basefab_t
Teuchos::RCP< vector_t > residual_p
residual over all cells
Teuchos::RCP< vector_t > error_p
error over all cells
amr::AmrIntVect_t AmrIntVect_t
bool isBoundary(const AmrIntVect_t &iv) const
amr::AmrGeometry_t AmrGeometry_t
amr::scalar_t scalar_t
const AmrIntVect_t & refinement() const
go_t nr_m[AMREX_SPACEDIM]
number of grid points
const AmrGeometry_t & geom
geometry of this problem
const scalar_t * invCellSize() const
amr::global_ordinal_t go_t
boundary_t bc_mp[AMREX_SPACEDIM]
boundary conditions
std::shared_ptr< AmrBoundary< AmrMultiGridLevel< MatrixType, VectorType > > > boundary_t
const scalar_t * cellSize() const
scalar_t dx_m[AMREX_SPACEDIM]
cell size in particle rest frame
go_t serialize(const AmrIntVect_t &iv) const
void buildMap(const Teuchos::RCP< comm_t > &comm)
Teuchos::RCP< dmap_t > map_p
Tpetra core map.