ImpactX
InitMeshRefinement.H
Go to the documentation of this file.
1 /* Copyright 2022-2023 The Regents of the University of California, through Lawrence
2  * Berkeley National Laboratory (subject to receipt of any required
3  * approvals from the U.S. Dept. of Energy). All rights reserved.
4  *
5  * This file is part of ImpactX.
6  *
7  * Authors: Axel Huebl
8  * License: BSD-3-Clause-LBNL
9  */
10 #pragma once
11 
13 
14 #include <AMReX.H>
15 #include <AMReX_BLProfiler.H>
16 #include <AMReX_ParmParse.H>
17 #include <AMReX_REAL.H>
18 #include <AMReX_Utility.H>
19 
20 #include <limits>
21 #include <stdexcept>
22 #include <string>
23 #include <vector>
24 
25 
27 {
30  {
31  amrex::ParmParse pp_algo("algo");
32  amrex::ParmParse pp_amr("amr");
33  amrex::ParmParse pp_geometry("geometry");
34 
35  bool space_charge = false;
36  pp_algo.queryAdd("space_charge", space_charge);
37 
38  int max_level = 0;
39  pp_amr.query("max_level", max_level);
40 
41  if (max_level > 1 && !space_charge)
42  throw std::runtime_error(
43  "Mesh-refinement (amr.max_level>=0) is only supported with "
44  "space charge modeling (algo.space_charge=1).");
45 
46  // The box is expanded beyond the min and max of the particle beam.
47  amrex::Vector<amrex::Real> prob_relative(max_level + 1, 1.0);
48  prob_relative[0] = 3.0; // top/bottom pad the beam on the lowest level by default by its width
49  pp_geometry.queryarr("prob_relative", prob_relative);
50 
51  if (prob_relative[0] < 3.0 && space_charge)
53  "ImpactX::read_mr_prob_relative",
54  "Dynamic resizing of the mesh uses a geometry.prob_relative "
55  "padding of less than 3 for level 0. This might result in boundary "
56  "artifacts for space charge calculation. "
57  "There is no minimum good value for this parameter, consider "
58  "doing a convergence test.",
60  );
61 
62  if (prob_relative[0] < 1.0)
63  throw std::runtime_error("geometry.prob_relative must be >= 1.0 (the beam size) on the coarsest level");
64 
65  // check that prob_relative[0] > prob_relative[1] > prob_relative[2] ...
66  amrex::Real last_lev_rel = std::numeric_limits<amrex::Real>::max();
67  for (int lev = 0; lev <= max_level; ++lev) {
68  amrex::Real const prob_relative_lvl = prob_relative[lev];
69  if (prob_relative_lvl <= 0.0)
70  throw std::runtime_error("geometry.prob_relative must be strictly positive for all levels");
71  if (prob_relative_lvl > last_lev_rel)
72  throw std::runtime_error("geometry.prob_relative must be descending over refinement levels");
73 
74  last_lev_rel = prob_relative_lvl;
75  }
76 
77  return prob_relative;
78  }
79 } // namespace impactx::initialization
int queryarr(const char *name, std::vector< int > &ref, int start_ix=FIRST, int num_val=ALL) const
int queryAdd(const char *name, T &ref)
int query(const char *name, bool &ref, int ival=FIRST) const
void WMRecordWarning(const std::string &topic, const std::string &text, const WarnPriority &priority=WarnPriority::medium)
Definition: AmrCoreData.cpp:18
amrex::Vector< amrex::Real > read_mr_prob_relative()
Definition: InitMeshRefinement.H:29