Skip to content

Commit 728e8d0

Browse files
committed
Parameters: Use AMReX Parser
Use the advanced right-hand-side parser of AMReX to read user parameters in inputs files. Enable constants, temporary variables, reuse of parameters, and simple mathematical expressions.
1 parent f68e03c commit 728e8d0

File tree

16 files changed

+82
-65
lines changed

16 files changed

+82
-65
lines changed

docs/source/usage/parameters.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ Parameters: Inputs File
55

66
This documents how to use ImpactX with an input file (``impactx input_file.in``).
77

8+
.. note::
9+
10+
Input files use the AMReX `ParmParse <https://amrex-codes.github.io/amrex/docs_html/Basics.html#parmparse>`__ syntax.
11+
A `parser <https://amrex-codes.github.io/amrex/docs_html/Basics.html#parser>`__) is used for the right-hand-side of all input parameters that consist of one or more integers or floats, so expressions like ``beam.kin_energy = "2.+1."``, ``beam.lambdaY = beam.lambdaX`` and/or using user-defined constants are accepted.
12+
813

914
.. _running-cpp-parameters-particle:
1015

src/ImpactX.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace impactx {
112112
// verbosity
113113
amrex::ParmParse pp_impactx("impactx");
114114
int verbose = 1;
115-
pp_impactx.queryAdd("verbose", verbose);
115+
pp_impactx.queryAddWithParser("verbose", verbose);
116116

117117
if (verbose > 0) {
118118
std::cout << "\nGrids Summary:\n";
@@ -140,7 +140,7 @@ namespace impactx {
140140
// verbosity
141141
amrex::ParmParse pp_impactx("impactx");
142142
int verbose = 1;
143-
pp_impactx.queryAdd("verbose", verbose);
143+
pp_impactx.queryAddWithParser("verbose", verbose);
144144

145145
// a global step for diagnostics including space charge slice steps in elements
146146
// before we start the evolve loop, we are in "step 0" (initial state)
@@ -159,7 +159,7 @@ namespace impactx {
159159
int file_min_digits = 6;
160160
if (diag_enable)
161161
{
162-
pp_diag.queryAdd("file_min_digits", file_min_digits);
162+
pp_diag.queryAddWithParser("file_min_digits", file_min_digits);
163163

164164
// print initial reference particle to file
165165
diagnostics::DiagnosticOutput(*amr_data->m_particle_container,
@@ -189,7 +189,7 @@ namespace impactx {
189189

190190
// periods through the lattice
191191
int num_periods = 1;
192-
amrex::ParmParse("lattice").queryAdd("periods", num_periods);
192+
amrex::ParmParse("lattice").queryAddWithParser("periods", num_periods);
193193

194194
for (int period=0; period < num_periods; ++period) {
195195
// loop over all beamline elements

src/initialization/InitAmrCore.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ namespace details
9999

100100
// Mesh-refinement
101101
int max_level = 0;
102-
pp_amr.query("max_level", max_level);
102+
pp_amr.queryWithParser("max_level", max_level);
103103
// amrex::AmrMesh::InitAmrMesh will query amr.ref_ratio or amr.ref_ratio_vect on its own
104104
amrex::Vector<amrex::IntVect> const & ref_ratios = amrex::Vector<amrex::IntVect>();
105105

@@ -144,7 +144,7 @@ namespace details
144144

145145
// Mesh-refinement
146146
int max_level = 0;
147-
pp_amr.query("max_level", max_level);
147+
pp_amr.queryWithParser("max_level", max_level);
148148
// amrex::AmrMesh::InitAmrMesh will query amr.ref_ratio or amr.ref_ratio_vect on its own
149149
amrex::Vector<amrex::IntVect> const & ref_ratios = amrex::Vector<amrex::IntVect>();
150150

src/initialization/InitDistribution.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ namespace impactx
139139
amrex::ParticleReal alphax = 0.0, alphay = 0.0, alphat = 0.0;
140140

141141
// Reading the input Twiss parameters
142-
pp_dist.query("alphaX", alphax);
143-
pp_dist.query("alphaY", alphay);
144-
pp_dist.query("alphaT", alphat);
142+
pp_dist.queryWithParser("alphaX", alphax);
143+
pp_dist.queryWithParser("alphaY", alphay);
144+
pp_dist.queryWithParser("alphaT", alphat);
145145
pp_dist.get("betaX", betax);
146146
pp_dist.get("betaY", betay);
147147
pp_dist.get("betaT", betat);
@@ -202,9 +202,9 @@ namespace impactx
202202
pp_dist.get("lambdaPx", sigpx);
203203
pp_dist.get("lambdaPy", sigpy);
204204
pp_dist.get("lambdaPt", sigpt);
205-
pp_dist.query("muxpx", muxpx);
206-
pp_dist.query("muypy", muypy);
207-
pp_dist.query("mutpt", mutpt);
205+
pp_dist.queryWithParser("muxpx", muxpx);
206+
pp_dist.queryWithParser("muypy", muypy);
207+
pp_dist.queryWithParser("mutpt", mutpt);
208208
}
209209

210210
void ImpactX::initBeamDistributionFromInputs ()
@@ -365,9 +365,9 @@ namespace impactx
365365
kT_halo = kT;
366366
pp_dist.get("normalize", normalize);
367367
normalize_halo = normalize;
368-
pp_dist.query("kT_halo", kT_halo);
369-
pp_dist.query("normalize_halo", normalize_halo);
370-
pp_dist.query("halo", halo);
368+
pp_dist.queryWithParser("kT_halo", kT_halo);
369+
pp_dist.queryWithParser("normalize_halo", normalize_halo);
370+
pp_dist.queryWithParser("halo", halo);
371371

372372
distribution::KnownDistributions thermal(distribution::Thermal(k, kT, kT_halo, normalize, normalize_halo, halo));
373373

src/initialization/InitElement.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace detail
6262
amrex::ParticleReal ds;
6363
int nslice = nslice_default;
6464
pp_element.get("ds", ds);
65-
pp_element.queryAdd("nslice", nslice);
65+
pp_element.queryAddWithParser("nslice", nslice);
6666

6767
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(nslice > 0,
6868
pp_element.getPrefix() + ".nslice must be > 0.");
@@ -81,9 +81,9 @@ namespace detail
8181
amrex::ParticleReal dx = 0;
8282
amrex::ParticleReal dy = 0;
8383
amrex::ParticleReal rotation_degree = 0;
84-
pp_element.query("dx", dx);
85-
pp_element.query("dy", dy);
86-
pp_element.query("rotation", rotation_degree);
84+
pp_element.queryWithParser("dx", dx);
85+
pp_element.queryWithParser("dy", dy);
86+
pp_element.queryWithParser("rotation", rotation_degree);
8787

8888
std::map<std::string, amrex::ParticleReal> values = {
8989
{"dx", dx},
@@ -188,7 +188,7 @@ namespace detail
188188
amrex::ParticleReal phase = -90.0;
189189
pp_element.get("V", V);
190190
pp_element.get("freq", freq);
191-
pp_element.queryAdd("phase", phase);
191+
pp_element.queryAddWithParser("phase", phase);
192192

193193
m_lattice.emplace_back( ShortRF(V, freq, phase, a["dx"], a["dy"], a["rotation_degree"], element_name) );
194194
} else if (element_type == "multipole")
@@ -224,7 +224,7 @@ namespace detail
224224
pp_element.get("escale", escale);
225225
pp_element.get("freq", freq);
226226
pp_element.get("phase", phase);
227-
pp_element.queryAdd("mapsteps", mapsteps);
227+
pp_element.queryAddWithParser("mapsteps", mapsteps);
228228
detail::queryAddResize(pp_element, "cos_coefficients", cos_coef);
229229
detail::queryAddResize(pp_element, "sin_coefficients", sin_coef);
230230

@@ -265,8 +265,8 @@ namespace detail
265265
std::vector<amrex::ParticleReal> cos_coef = bz.default_cos_coef;
266266
std::vector<amrex::ParticleReal> sin_coef = bz.default_sin_coef;
267267
pp_element.get("bscale", bscale);
268-
pp_element.queryAdd("units", units);
269-
pp_element.queryAdd("mapsteps", mapsteps);
268+
pp_element.queryAddWithParser("units", units);
269+
pp_element.queryAddWithParser("mapsteps", mapsteps);
270270
detail::queryAddResize(pp_element, "cos_coefficients", cos_coef);
271271
detail::queryAddResize(pp_element, "sin_coefficients", sin_coef);
272272

@@ -282,7 +282,7 @@ namespace detail
282282
std::vector<amrex::ParticleReal> cos_coef = gz.default_cos_coef;
283283
std::vector<amrex::ParticleReal> sin_coef = gz.default_sin_coef;
284284
pp_element.get("gscale", gscale);
285-
pp_element.queryAdd("mapsteps", mapsteps);
285+
pp_element.queryAddWithParser("mapsteps", mapsteps);
286286
detail::queryAddResize(pp_element, "cos_coefficients", cos_coef);
287287
detail::queryAddResize(pp_element, "sin_coefficients", sin_coef);
288288

@@ -301,7 +301,7 @@ namespace detail
301301
amrex::ParticleReal k;
302302
int units = 0;
303303
pp_element.get("k", k);
304-
pp_element.queryAdd("units", units);
304+
pp_element.queryAddWithParser("units", units);
305305

306306
m_lattice.emplace_back( ChrQuad(ds, k, units, a["dx"], a["dy"], a["rotation_degree"], nslice, element_name) );
307307
} else if (element_type == "plasma_lens_chromatic")
@@ -312,7 +312,7 @@ namespace detail
312312
amrex::ParticleReal k;
313313
int units = 0;
314314
pp_element.get("k", k);
315-
pp_element.queryAdd("units", units);
315+
pp_element.queryAddWithParser("units", units);
316316

317317
m_lattice.emplace_back( ChrPlasmaLens(ds, k, units, a["dx"], a["dy"], a["rotation_degree"], nslice, element_name) );
318318
} else if (element_type == "tapered_plasma_lens")
@@ -324,7 +324,7 @@ namespace detail
324324
int units = 0;
325325
pp_element.get("k", k);
326326
pp_element.get("taper", taper);
327-
pp_element.queryAdd("units", units);
327+
pp_element.queryAddWithParser("units", units);
328328

329329
m_lattice.emplace_back( TaperedPL(k, taper, units, a["dx"], a["dy"], a["rotation_degree"], element_name) );
330330
} else if (element_type == "drift_exact")
@@ -341,7 +341,7 @@ namespace detail
341341
amrex::ParticleReal phi;
342342
amrex::ParticleReal B = 0.0;
343343
pp_element.get("phi", phi);
344-
pp_element.queryAdd("B", B);
344+
pp_element.queryAddWithParser("B", B);
345345

346346
m_lattice.emplace_back( ExactSbend(ds, phi, B, a["dx"], a["dy"], a["rotation_degree"], nslice, element_name) );
347347
} else if (element_type == "uniform_acc_chromatic")
@@ -390,8 +390,8 @@ namespace detail
390390
std::string action_str = "transmit";
391391
pp_element.get("xmax", xmax);
392392
pp_element.get("ymax", ymax);
393-
pp_element.queryAdd("repeat_x", repeat_x);
394-
pp_element.queryAdd("repeat_y", repeat_y);
393+
pp_element.queryAddWithParser("repeat_x", repeat_x);
394+
pp_element.queryAddWithParser("repeat_y", repeat_y);
395395
pp_element.queryAdd("shape", shape_str);
396396
pp_element.queryAdd("action", action_str);
397397
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(shape_str == "rectangular" || shape_str == "elliptical",
@@ -415,7 +415,7 @@ namespace detail
415415
std::string openpmd_encoding{"g"};
416416
pp_element.queryAdd("encoding", openpmd_encoding);
417417
int period_sample_intervals = 1;
418-
pp_element.queryAdd("period_sample_intervals", period_sample_intervals);
418+
pp_element.queryAddWithParser("period_sample_intervals", period_sample_intervals);
419419

420420
// optional: add and calculate additional particle properties
421421
// property: nonlinear lens invariants
@@ -424,13 +424,13 @@ namespace detail
424424
if (add_nll_invariants)
425425
{
426426
amrex::ParticleReal alpha = 0.0;
427-
pp_element.queryAdd("alpha", alpha);
427+
pp_element.queryAddWithParser("alpha", alpha);
428428
amrex::ParticleReal beta = 1.0;
429-
pp_element.queryAdd("beta", beta);
429+
pp_element.queryAddWithParser("beta", beta);
430430
amrex::ParticleReal tn = 0.4;
431-
pp_element.queryAdd("tn", tn);
431+
pp_element.queryAddWithParser("tn", tn);
432432
amrex::ParticleReal cn = 0.01;
433-
pp_element.queryAdd("cn", cn);
433+
pp_element.queryAddWithParser("cn", cn);
434434
}
435435

436436
m_lattice.emplace_back(diagnostics::BeamMonitor(openpmd_name, openpmd_backend, openpmd_encoding, period_sample_intervals));
@@ -443,7 +443,7 @@ namespace detail
443443
bool reverse = false;
444444
pp_sub_lattice.queryAdd("reverse", reverse);
445445
int repeat = 1;
446-
pp_sub_lattice.queryAdd("repeat", repeat);
446+
pp_sub_lattice.queryAddWithParser("repeat", repeat);
447447
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(repeat >= 1,
448448
element_name + ".repeat must be >= 1");
449449

@@ -471,7 +471,7 @@ namespace detail
471471

472472
// periods through the lattice
473473
int periods = 1;
474-
pp_lattice.queryAdd("periods", periods);
474+
pp_lattice.queryAddWithParser("periods", periods);
475475
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(periods >= 1,
476476
"lattice.periods must be >= 1");
477477

@@ -487,7 +487,7 @@ namespace detail
487487

488488
// Default number of slices per element
489489
int nslice_default = 1;
490-
pp_lattice.query("nslice", nslice_default);
490+
pp_lattice.queryWithParser("nslice", nslice_default);
491491

492492
// Default number of map integration steps per slice
493493
int const mapsteps_default = 10; // used only in RF cavity

src/initialization/InitMeshRefinement.H

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace impactx::initialization
3939
pp_algo.queryAdd("poisson_solver", poisson_solver);
4040

4141
int max_level = 0;
42-
pp_amr.query("max_level", max_level);
42+
pp_amr.queryWithParser("max_level", max_level);
4343

4444
if (max_level > 1 && !space_charge)
4545
throw std::runtime_error(

src/initialization/InitMeshRefinement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace detail
3737
amrex::ParmParse pp_geometry("geometry");
3838

3939
int max_level = 0;
40-
pp_amr.query("max_level", max_level);
40+
pp_amr.queryWithParser("max_level", max_level);
4141

4242
std::string poisson_solver = "multigrid";
4343
pp_algo.queryAdd("poisson_solver", poisson_solver);

src/initialization/Warnings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void ImpactX::init_warning_logger ()
2727

2828
// verbosity
2929
int verbose = 1;
30-
pp_impactx.queryAdd("verbose", verbose);
30+
pp_impactx.queryAddWithParser("verbose", verbose);
3131

3232
// Set the flag to control if ImpactX has to emit a warning message
3333
// as soon as a warning is recorded
@@ -64,7 +64,7 @@ bool ImpactX::early_param_check ()
6464
// verbosity
6565
amrex::ParmParse pp_impactx("impactx");
6666
int verbose = 1;
67-
pp_impactx.queryAdd("verbose", verbose);
67+
pp_impactx.queryAddWithParser("verbose", verbose);
6868

6969
if (verbose > 0) {
7070
amrex::Print() << "\n";

src/particles/ImpactXParticleContainer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace impactx
153153
{
154154
amrex::ParmParse const pp_algo("algo");
155155
int v = 0;
156-
bool const has_shape = pp_algo.query("particle_shape", v);
156+
bool const has_shape = pp_algo.queryWithParser("particle_shape", v);
157157
if (!has_shape)
158158
throw std::runtime_error("particle_shape is not set, cannot initialize grids with guard cells.");
159159
SetParticleShape(v);

src/particles/diagnostics/DiagnosticOutput.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ namespace impactx::diagnostics
193193
amrex::ParmParse pp_diag("diag");
194194

195195
amrex::ParticleReal alpha = 0.0;
196-
pp_diag.queryAdd("alpha", alpha);
196+
pp_diag.queryAddWithParser("alpha", alpha);
197197

198198
amrex::ParticleReal beta = 1.0;
199-
pp_diag.queryAdd("beta", beta);
199+
pp_diag.queryAddWithParser("beta", beta);
200200

201201
amrex::ParticleReal tn = 0.4;
202-
pp_diag.queryAdd("tn", tn);
202+
pp_diag.queryAddWithParser("tn", tn);
203203

204204
amrex::ParticleReal cn = 0.01;
205-
pp_diag.queryAdd("cn", cn);
205+
pp_diag.queryAddWithParser("cn", cn);
206206

207207
NonlinearLensInvariants const nonlinear_lens_invariants(alpha, beta, tn, cn);
208208

src/particles/elements/diagnostics/AdditionalProperties.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ namespace impactx::diagnostics
3939
return;
4040

4141
amrex::ParticleReal alpha = 0.0;
42-
pp_element.queryAdd("alpha", alpha);
42+
pp_element.queryAddWithParser("alpha", alpha);
4343

4444
amrex::ParticleReal beta = 1.0;
45-
pp_element.queryAdd("beta", beta);
45+
pp_element.queryAddWithParser("beta", beta);
4646

4747
amrex::ParticleReal tn = 0.4;
48-
pp_element.queryAdd("tn", tn);
48+
pp_element.queryAddWithParser("tn", tn);
4949

5050
amrex::ParticleReal cn = 0.01;
51-
pp_element.queryAdd("cn", cn);
51+
pp_element.queryAddWithParser("cn", cn);
5252

5353
NonlinearLensInvariants const nonlinear_lens_invariants(alpha, beta, tn, cn);
5454

src/particles/elements/diagnostics/openPMD.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ namespace detail
181181

182182
amrex::ParmParse pp_diag("diag");
183183
// turn filter
184-
pp_diag.queryAdd("period_sample_intervals", m_period_sample_intervals);
184+
pp_diag.queryAddWithParser("period_sample_intervals", m_period_sample_intervals);
185185
// legacy options from other diagnostics
186-
pp_diag.queryAdd("file_min_digits", m_file_min_digits);
186+
pp_diag.queryAddWithParser("file_min_digits", m_file_min_digits);
187187

188188
// Ensure m_series is the same for the same names.
189189
if (m_unique_series.count(m_series_name) == 0u) {

src/particles/spacecharge/PoissonSolve.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ namespace impactx::spacecharge
6363
// MLMG options
6464
amrex::Real mlmg_relative_tolerance = 1.e-7; // relative TODO: make smaller for SP
6565
amrex::Real mlmg_absolute_tolerance = 0.0; // ignored
66-
pp_algo.queryAdd("mlmg_relative_tolerance", mlmg_relative_tolerance);
67-
pp_algo.queryAdd("mlmg_absolute_tolerance", mlmg_absolute_tolerance);
66+
pp_algo.queryAddWithParser("mlmg_relative_tolerance", mlmg_relative_tolerance);
67+
pp_algo.queryAddWithParser("mlmg_absolute_tolerance", mlmg_absolute_tolerance);
6868

6969
int mlmg_max_iters = 100;
7070
int mlmg_verbosity = 1;
71-
pp_algo.queryAdd("mlmg_max_iters", mlmg_max_iters);
72-
pp_algo.queryAdd("mlmg_verbosity", mlmg_verbosity);
71+
pp_algo.queryAddWithParser("mlmg_max_iters", mlmg_max_iters);
72+
pp_algo.queryAddWithParser("mlmg_verbosity", mlmg_verbosity);
7373

7474
// create a vector to our fields, sorted by level
7575
amrex::Vector<amrex::MultiFab*> sorted_rho;

src/particles/wakefields/HandleWakefield.H

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace impactx::particles::wakefields
5858
#endif
5959

6060
int csr_bins = 150;
61-
pp_algo.queryAdd("csr_bins", csr_bins);
61+
pp_algo.queryAddWithParser("csr_bins", csr_bins);
6262

6363
// Measure beam size, extract the min, max of particle positions
6464
[[maybe_unused]] auto const [x_min, y_min, t_min, x_max, y_max, t_max] =

0 commit comments

Comments
 (0)