OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
FieldSolver.cpp
Go to the documentation of this file.
1//
2// Class FieldSolver
3// The class for the OPAL FIELDSOLVER command.
4// A FieldSolver definition is used by most physics commands to define the
5// particle charge and the reference momentum, together with some other data.
6//
7// Copyright (c) 200x - 2022, Paul Scherrer Institut, Villigen PSI, Switzerland
8//
9// All rights reserved
10//
11// This file is part of OPAL.
12//
13// OPAL is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// You should have received a copy of the GNU General Public License
19// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20//
22
30#include "Physics/Physics.h"
33#ifdef HAVE_SAAMG_SOLVER
35#endif
39
40#ifdef ENABLE_AMR
41 #include "Amr/AmrBoxLib.h"
42#ifdef AMREX_ENABLE_FBASELIB
44#endif
46 #include "Amr/AmrDefs.h"
48#endif
49
50#ifdef HAVE_AMR_MG_SOLVER
52#endif
53
54#include <map>
55
56using namespace Expressions;
57
58//TODO: o add a FIELD for DISCRETIZATION, MAXITERS, TOL...
59
60// The attributes of class FieldSolver.
61namespace {
62 namespace deprecated {
63 enum {
64 BCFFTT,
65 SIZE
66 };
67 }
68 enum {
69 FSTYPE = deprecated::SIZE, // The field solver name
70 // FOR FFT BASED SOLVER
71 MX, // mesh sixe in x
72 MY, // mesh sixe in y
73 MT, // mesh sixe in z
74 PARFFTX, // parallelized grid in x
75 PARFFTY, // parallelized grid in y
76 PARFFTT, // parallelized grid in z
77 BCFFTX, // boundary condition in x [FFT + AMR_MG only]
78 BCFFTY, // boundary condition in y [FFT + AMR_MG only]
79 BCFFTZ, // boundary condition in z [FFT + AMR_MG only]
80 GREENSF, // holds greensfunction to be used [FFT + P3M only]
81 BBOXINCR, // how much the boundingbox is increased
82 GEOMETRY, // geometry of boundary [SAAMG only]
83 ITSOLVER, // iterative solver [SAAMG + AMR_MG]
84 INTERPL, // interpolation used for boundary points [SAAMG only]
85 TOL, // tolerance of the SAAMG preconditioned solver [SAAMG only]
86 MAXITERS, // max number of iterations [SAAMG only]
87 PRECMODE, // preconditioner mode [SAAMG only]
88 RC, // cutoff radius for PP interactions
89 ALPHA, // Green’s function splitting parameter
90 MESHFIT, // particles the mesh is fitted to [FFT in OPAL-cycl only]
91 MESHFITNSIGMA, // core half-width in clipped rms widths [MESHFIT=CORE]
92 MESHFITCLIP, // clip half-width of the clipped rms [MESHFIT=CORE]
93 MESHFITMAXFAR, // far charge fraction for the full box, % [MESHFIT=CORE]
94 MESHFITFARFIELD, // field of the particles outside the mesh [MESHFIT=CORE]
95#ifdef ENABLE_AMR
96 AMR_MAXLEVEL, // AMR, maximum refinement level
97 AMR_REFX, // AMR, refinement ratio in x
98 AMR_REFY, // AMR, refinement ratio in y
99 AMR_REFZ, // AMR, refinement ration in z
100 AMR_MAXGRIDX, // AMR, maximum grid size in x (default: 16)
101 AMR_MAXGRIDY, // AMR, maximum grid size in y (default: 16)
102 AMR_MAXGRIDZ, // AMR, maximum grid size in z (default: 16)
103 AMR_BFX, // AMR, blocking factor in x (maxgrid needs to be a multiple, default: 8)
104 AMR_BFY, // AMR, blocking factor in y (maxgrid needs to be a multiple, default: 8)
105 AMR_BFZ, // AMR, blocking factor in z (maxgrid needs to be a multiple, default: 8)
106 AMR_TAGGING,
107 AMR_DENSITY,
108 AMR_MAX_NUM_PART,
109 AMR_MIN_NUM_PART,
110 AMR_SCALING,
111 AMR_DOMAIN_RATIO,
112#endif
113#ifdef HAVE_AMR_MG_SOLVER
114 // AMR_MG = Adaptive-Mesh-Refinement Multi-Grid
115 AMR_MG_SMOOTHER, // AMR, smoother for level solution
116 AMR_MG_NSWEEPS, // AMR, number of smoothing sweeps
117 AMR_MG_PREC, // AMR, preconditioner for bottom solver
118 AMR_MG_INTERP, // AMR, interpolater for solution from level l to l+1
119 AMR_MG_NORM, // AMR, norm convergence criteria
120 AMR_MG_VERBOSE, // AMR, enable solver info writing (SDDS file)
121 AMR_MG_REBALANCE, // AMR, rebalance smoothed aggregation (SA) preconditioner
122 AMR_MG_REUSE, // AMR, reuse type of SA (NONE, RP, RAP, SYMBOLIC or FULL)
123 AMR_MG_TOL, // AMR, tolerance of solver
124#endif
125 // FOR XXX BASED SOLVER
126 SIZE
127 };
128}
129
130
132 Definition(SIZE, "FIELDSOLVER",
133 "The \"FIELDSOLVER\" statement defines data for a the field solver") {
134
135 itsAttr[FSTYPE] = Attributes::makePredefinedString("FSTYPE", "Name of the attached field solver.",
136 {"FFT", "FFTPERIODIC", "SAAMG", "FMG", "ML", "AMR_MG", "NONE","P3M"});
137
138 itsAttr[MX] = Attributes::makeReal("MX", "Meshsize in x");
139 itsAttr[MY] = Attributes::makeReal("MY", "Meshsize in y");
140 itsAttr[MT] = Attributes::makeReal("MT", "Meshsize in z(t)");
141
142 itsAttr[PARFFTX] = Attributes::makeBool("PARFFTX",
143 "True, dimension 0 i.e x is parallelized",
144 false);
145
146 itsAttr[PARFFTY] = Attributes::makeBool("PARFFTY",
147 "True, dimension 1 i.e y is parallelized",
148 false);
149
150 itsAttr[PARFFTT] = Attributes::makeBool("PARFFTT",
151 "True, dimension 2 i.e z(t) is parallelized",
152 true);
153
154 //FFT ONLY:
155 itsAttr[BCFFTX] = Attributes::makePredefinedString("BCFFTX",
156 "Boundary conditions in x.",
157 {"OPEN", "DIRICHLET", "PERIODIC"},
158 "OPEN");
159
160 itsAttr[BCFFTY] = Attributes::makePredefinedString("BCFFTY",
161 "Boundary conditions in y.",
162 {"OPEN", "DIRICHLET", "PERIODIC"},
163 "OPEN");
164
165 itsAttr[BCFFTZ] = Attributes::makePredefinedString("BCFFTZ",
166 "Boundary conditions in z(t).",
167 {"OPEN", "DIRICHLET", "PERIODIC"},
168 "OPEN");
169
170 itsAttr[deprecated::BCFFTT] = Attributes::makePredefinedString("BCFFTT",
171 "Boundary conditions in z(t).",
172 {"OPEN", "DIRICHLET", "PERIODIC"},
173 "OPEN");
174
175 //GREENSF is also used in P3M
176 itsAttr[GREENSF] = Attributes::makePredefinedString("GREENSF",
177 "Which Greensfunction to be used.",
178 {"STANDARD", "INTEGRATED"},
179 "INTEGRATED");
180
181 itsAttr[BBOXINCR] = Attributes::makeReal("BBOXINCR",
182 "Increase of bounding box in % ",
183 2.0);
184
185 // FFT in OPAL-cycl only:
186 itsAttr[MESHFIT] = Attributes::makePredefinedString("MESHFIT",
187 "Particles the space-charge mesh is fitted to: "
188 "ALL, or the CORE of the bunch.",
189 {"ALL", "CORE"},
190 "ALL");
191
192 itsAttr[MESHFITNSIGMA] = Attributes::makeReal("MESHFITNSIGMA",
193 "MESHFIT=CORE: half-width of the core in clipped "
194 "rms widths, in every axis of the local frame",
195 6.0);
196
197 itsAttr[MESHFITCLIP] = Attributes::makeReal("MESHFITCLIP",
198 "MESHFIT=CORE: clip half-width of the clipped rms "
199 "in rms widths",
200 4.0);
201
202 itsAttr[MESHFITMAXFAR] = Attributes::makeReal("MESHFITMAXFAR",
203 "MESHFIT=CORE: far charge fraction in % above "
204 "which the mesh is fitted to all particles",
205 5.0);
206
207 itsAttr[MESHFITFARFIELD] = Attributes::makePredefinedString("MESHFITFARFIELD",
208 "MESHFIT=CORE: field of the particles "
209 "outside the core mesh.",
210 {"FULL", "QUADRUPOLE", "MONOPOLE"},
211 "FULL");
212
213 // P3M only:
214 itsAttr[RC] = Attributes::makeReal("RC",
215 "cutoff radius for PP interactions",
216 0.0);
217
219 "Standard Ewald Green’s function splitting parameter",
220 1e8);
221 //SAAMG and in case of FFT with dirichlet BC in x and y
222 itsAttr[GEOMETRY] = Attributes::makeUpperCaseString("GEOMETRY",
223 "GEOMETRY to be used as domain boundary",
224 "");
225
226 itsAttr[ITSOLVER] = Attributes::makePredefinedString("ITSOLVER",
227 "Type of iterative solver.",
228 {"CG",
229 "BICGSTAB",
230 "GMRES",
231 "MINRES",
232 "PCPG",
233 "STOCHASTIC_CG",
234 "RECYCLING_CG",
235 "RECYCLING_GMRES",
236 "KLU2",
237 "SUPERLU",
238 "UMFPACK",
239 "PARDISO_MKL",
240 "MUMPS",
241 "LAPACK",
242 "SA"},
243 "CG");
244
245 itsAttr[INTERPL] = Attributes::makePredefinedString("INTERPL",
246 "interpolation used for boundary points.",
247 {"CONSTANT", "LINEAR", "QUADRATIC"},
248 "LINEAR");
249
250 itsAttr[TOL] = Attributes::makeReal("TOL",
251 "Tolerance for iterative solver",
252 1e-8);
253
254 itsAttr[MAXITERS] = Attributes::makeReal("MAXITERS",
255 "Maximum number of iterations of iterative solver",
256 100);
257
258 itsAttr[PRECMODE] = Attributes::makePredefinedString("PRECMODE",
259 "Preconditioner Mode.",
260 {"STD", "HIERARCHY", "REUSE"},
261 "HIERARCHY");
262
263 // AMR
264#ifdef ENABLE_AMR
265 itsAttr[AMR_MAXLEVEL] = Attributes::makeReal("AMR_MAXLEVEL",
266 "Maximum number of levels in AMR",
267 0);
268
269 itsAttr[AMR_REFX] = Attributes::makeReal("AMR_REFX",
270 "Refinement ration in x-direction in AMR",
271 2);
272
273 itsAttr[AMR_REFY] = Attributes::makeReal("AMR_REFY",
274 "Refinement ration in y-direction in AMR",
275 2);
276
277 itsAttr[AMR_REFZ] = Attributes::makeReal("AMR_REFZ",
278 "Refinement ration in z-direction in AMR",
279 2);
280
281 itsAttr[AMR_MAXGRIDX] = Attributes::makeReal("AMR_MAXGRIDX",
282 "Maximum grid size in x for AMR",
283 16);
284
285 itsAttr[AMR_MAXGRIDY] = Attributes::makeReal("AMR_MAXGRIDY",
286 "Maximum grid size in y for AMR",
287 16);
288
289 itsAttr[AMR_MAXGRIDZ] = Attributes::makeReal("AMR_MAXGRIDZ",
290 "Maximum grid size in z for AMR",
291 16);
292
293 itsAttr[AMR_BFX] = Attributes::makeReal("AMR_BFX",
294 "Blocking factor in x for AMR (AMR_MAXGRIDX needs to be a multiple)",
295 8);
296
297 itsAttr[AMR_BFY] = Attributes::makeReal("AMR_BFY",
298 "Blocking factor in y for AMR (AMR_MAXGRIDY needs to be a multiple)",
299 8);
300
301 itsAttr[AMR_BFZ] = Attributes::makeReal("AMR_BFZ",
302 "Blocking factor in y for AMR (AMR_MAXGRIDZ needs to be a multiple)",
303 8);
304
305 itsAttr[AMR_TAGGING] = Attributes::makeUpperCaseString("AMR_TAGGING",
306 "Refinement criteria [CHARGE_DENSITY | POTENTIAL | EFIELD | MIN_NUM_PARTICLES | MAX_NUM_PARTICLES]",
307 "CHARGE_DENSITY");
308
309 itsAttr[AMR_DENSITY] = Attributes::makeReal("AMR_DENSITY",
310 "Tagging value for charge density refinement [C / cell volume]",
311 1.0e-14);
312
313 itsAttr[AMR_MAX_NUM_PART] = Attributes::makeReal("AMR_MAX_NUM_PART",
314 "Tagging value for max. #particles",
315 1);
316
317 itsAttr[AMR_MIN_NUM_PART] = Attributes::makeReal("AMR_MIN_NUM_PART",
318 "Tagging value for min. #particles",
319 1);
320
321 itsAttr[AMR_SCALING] = Attributes::makeReal("AMR_SCALING",
322 "Scaling value for maximum value tagging "
323 "(only POTENTIAL / CHARGE_DENSITY / "
324 "MOMENTA)", 0.75);
325
326 itsAttr[AMR_DOMAIN_RATIO] = Attributes::makeRealArray("AMR_DOMAIN_RATIO",
327 "Box ratio of AMR computation domain. Default: [-1, 1]^3");
328
329 // default
330 Attributes::setRealArray(itsAttr[AMR_DOMAIN_RATIO], {1.0, 1.0, 1.0});
331#endif
332
333#ifdef HAVE_AMR_MG_SOLVER
334 itsAttr[AMR_MG_SMOOTHER] = Attributes::makePredefinedString("AMR_MG_SMOOTHER",
335 "Smoothing of level solution.",
336 {"GS", "SGS", "JACOBI"}, "GS");
337
338 itsAttr[AMR_MG_NSWEEPS] = Attributes::makeReal("AMR_MG_NSWEEPS",
339 "Number of relaxation steps",
340 8);
341
342 itsAttr[AMR_MG_PREC] = Attributes::makePredefinedString("AMR_MG_PREC",
343 "Preconditioner of bottom solver.",
344 {"NONE", "ILUT", "CHEBYSHEV", "RILUK", "JACOBI", "BLOCK_JACOBI", "GS", "BLOCK_GS", "SA"},
345 "NONE");
346
347 itsAttr[AMR_MG_INTERP] = Attributes::makePredefinedString("AMR_MG_INTERP",
348 "Interpolater between levels.",
349 {"TRILINEAR", "LAGRANGE", "PC"},
350 "PC");
351
352 itsAttr[AMR_MG_NORM] = Attributes::makePredefinedString("AMR_MG_NORM",
353 "Norm for convergence criteria.",
354 {"L1_NORM", "L2_NORM", "LINF_NORM"},
355 "LINF_NORM");
356
357 itsAttr[AMR_MG_VERBOSE] = Attributes::makeBool("AMR_MG_VERBOSE",
358 "Write solver info in SDDS format (*.solver)",
359 false);
360
361 itsAttr[AMR_MG_REBALANCE] = Attributes::makeBool("AMR_MG_REBALANCE",
362 "Rebalancing of Smoothed Aggregation "
363 "Preconditioner",
364 false);
365
366 itsAttr[AMR_MG_REUSE] = Attributes::makePredefinedString("AMR_MG_REUSE",
367 "Reuse type of Smoothed Aggregation.",
368 {"NONE", "RP", "RAP", "SYMBOLIC", "FULL"},
369 "RAP");
370
371 itsAttr[AMR_MG_TOL] = Attributes::makeReal("AMR_MG_TOL",
372 "AMR MG solver tolerance (default: 1.0e-10)",
373 1.0e-10);
374#endif
375
376 mesh_m = 0;
377 FL_m = 0;
378 PL_m.reset(nullptr);
379 solver_m = 0;
380
382}
383
384
385FieldSolver::FieldSolver(const std::string& name, FieldSolver* parent):
386 Definition(name, parent)
387{
388 mesh_m = 0;
389 FL_m = 0;
390 PL_m.reset(nullptr);
391 solver_m = 0;
392}
393
394
396 if (mesh_m) {
397 delete mesh_m;
398 mesh_m = 0;
399 }
400 if (FL_m) {
401 delete FL_m;
402 FL_m = 0;
403 }
404 if (solver_m) {
405 delete solver_m;
406 solver_m = 0;
407 }
408}
409
410FieldSolver* FieldSolver::clone(const std::string& name) {
411 return new FieldSolver(name, this);
412}
413
418
419FieldSolver* FieldSolver::find(const std::string& name) {
420 FieldSolver* fs = dynamic_cast<FieldSolver*>(OpalData::getInstance()->find(name));
421
422 if (fs == 0) {
423 throw OpalException("FieldSolver::find()", "FieldSolver \"" + name + "\" not found.");
424 }
425 return fs;
426}
427
428std::string FieldSolver::getType() {
429 return Attributes::getString(itsAttr[FSTYPE]);
430}
431
432double FieldSolver::getMX() const {
433 return Attributes::getReal(itsAttr[MX]);
434}
435
436double FieldSolver::getMY() const {
437 return Attributes::getReal(itsAttr[MY]);
438}
439
440double FieldSolver::getMT() const {
441 return Attributes::getReal(itsAttr[MT]);
442}
443
444void FieldSolver::setMX(double value) {
445 Attributes::setReal(itsAttr[MX], value);
446}
447
448void FieldSolver::setMY(double value) {
449 Attributes::setReal(itsAttr[MY], value);
450}
451
452void FieldSolver::setMT(double value) {
453 Attributes::setReal(itsAttr[MT], value);
454}
455
457
458}
459
461
462 e_dim_tag decomp[3] = {SERIAL, SERIAL, SERIAL};
463
464 NDIndex<3> domain;
465 domain[0] = Index((int)getMX() + 1);
466 domain[1] = Index((int)getMY() + 1);
467 domain[2] = Index((int)getMT() + 1);
468
469 if (Attributes::getBool(itsAttr[PARFFTX]))
470 decomp[0] = PARALLEL;
471 if (Attributes::getBool(itsAttr[PARFFTY]))
472 decomp[1] = PARALLEL;
473 if (Attributes::getBool(itsAttr[PARFFTT]))
474 decomp[2] = PARALLEL;
475
476 if (Attributes::getString(itsAttr[FSTYPE]) == "FFTPERIODIC") {
477 decomp[0] = decomp[1] = SERIAL;
478 decomp[2] = PARALLEL;
479 }
480 // create prototype mesh and layout objects for this problem domain
481
482 if ( !isAmrSolverType() ) {
483 mesh_m = new Mesh_t(domain);
484 FL_m = new FieldLayout_t(*mesh_m, decomp);
485 PL_m.reset(new Layout_t(*FL_m, *mesh_m));
486 // OpalData::getInstance()->setMesh(mesh_m);
487 // OpalData::getInstance()->setFieldLayout(FL_m);
488 // OpalData::getInstance()->setLayout(PL_m);
489 }
490}
491
493 if (itsAttr[BCFFTZ].defaultUsed() &&
494 !itsAttr[deprecated::BCFFTT].defaultUsed()) {
495 return itsAttr[deprecated::BCFFTT];
496 }
497
498 return itsAttr[BCFFTZ];
499}
500
502 std::string bcz = Attributes::getString(getBCZAttribute());
503
504 return (bcz == "PERIODIC");
505}
506
511 throw OpalException("FieldSolver::isAmrSolverType",
512 "The attribute \"FSTYPE\" has not correct type for AMR solver");
513 } else {
514 return Options::amr;
515 }
516}
517
519 static const std::map<std::string, FieldSolverType> stringFSType_s = {
520 {"NONE", FieldSolverType::NONE},
521 {"FFT", FieldSolverType::FFT},
522 {"FFTBOX", FieldSolverType::FFTBOX},
523 {"SAAMG", FieldSolverType::SAAMG},
524 {"P3M", FieldSolverType::P3M},
525 {"FMG", FieldSolverType::FMG},
526 {"ML", FieldSolverType::ML},
527 {"AMR_MG", FieldSolverType::AMRMG},
528 {"HYPRE", FieldSolverType::HYPRE},
529 {"HPGMG", FieldSolverType::HPGMG}
530 };
531 fsName_m = getType();
532 if (fsName_m.empty()) {
533 throw OpalException("FieldSolver::setFieldSolverType",
534 "The attribute \"FSTYPE\" isn't set for \"FIELDSOLVER\"!");
535 } else {
536 fsType_m = stringFSType_s.at(fsName_m);
537 }
538}
539
541 itsBunch_m = b;
542
543 std::string greens = Attributes::getString(itsAttr[GREENSF]);
544 std::string bcx = Attributes::getString(itsAttr[BCFFTX]);
545 std::string bcy = Attributes::getString(itsAttr[BCFFTY]);
546 std::string bcz = Attributes::getString(getBCZAttribute());
547
548 if ( isAmrSolverType() ) {
549 Inform m("FieldSolver::initAmrSolver");
550
551#ifdef ENABLE_AMR
553
555#endif
556 } else if (fsType_m == FieldSolverType::FFT) {
557 bool sinTrafo = ((bcx == "DIRICHLET") && (bcy == "DIRICHLET") && (bcz == "DIRICHLET"));
558 if (sinTrafo) {
559 std::cout << "FFTBOX ACTIVE" << std::endl;
560 //we go over all geometries and add the Geometry Elements to the geometry list
561 std::string geoms = Attributes::getString(itsAttr[GEOMETRY]);
562 std::string tmp;
563 //split and add all to list
564 std::vector<BoundaryGeometry*> geometries;
565 for (unsigned int i = 0; i <= geoms.length(); i++) {
566 if (i == geoms.length() || geoms[i] == ',') {
568 if (geom != 0)
569 geometries.push_back(geom);
570 tmp.clear();
571 } else {
572 tmp += geoms[i];
573 }
574 }
575 BoundaryGeometry* ttmp = geometries[0];
576 solver_m = new FFTBoxPoissonSolver(mesh_m, FL_m, greens, ttmp->getA());
578 fsName_m = "FFTBOX";
580 } else {
581 solver_m = new FFTPoissonSolver(mesh_m, FL_m, greens, bcz);
583 }
584 } else if (fsType_m == FieldSolverType::P3M) {
586 FL_m,
589 greens);
590
592
593 } else if (fsType_m == FieldSolverType::SAAMG) {
594#ifdef HAVE_SAAMG_SOLVER
595 //we go over all geometries and add the Geometry Elements to the geometry list
596 std::string geoms = Attributes::getString(itsAttr[GEOMETRY]);
597 std::string tmp;
598 //split and add all to list
599 std::vector<BoundaryGeometry*> geometries;
600 for (unsigned int i = 0; i <= geoms.length(); i++) {
601 if (i == geoms.length() || geoms[i] == ',') {
603 if (geom != 0) {
604 geometries.push_back(geom);
605 }
606 tmp.clear();
607 } else
608 tmp += geoms[i];
609 }
610 solver_m = new MGPoissonSolver(dynamic_cast<PartBunch*>(itsBunch_m), mesh_m, FL_m,
611 geometries,
615 Attributes::getReal(itsAttr[MAXITERS]),
616 Attributes::getString(itsAttr[PRECMODE]));
618#else
619 throw OpalException("FieldSolver::initSolver",
620 "SAAMG Solver not enabled! Please build OPAL with -DENABLE_SAAMG_SOLVER=1");
621#endif
622 } else {
623 solver_m = 0;
624 INFOMSG("No solver attached" << endl);
625 }
626
628}
629
631 MeshFitParameters meshFit;
632 if (Attributes::getString(itsAttr[MESHFIT]) == "ALL") {
633 return meshFit;
634 }
635
636 static const std::map<std::string, FarFieldModel> stringFarField_s = {
637 {"MONOPOLE", FarFieldModel::MONOPOLE},
638 {"QUADRUPOLE", FarFieldModel::QUADRUPOLE},
639 {"FULL", FarFieldModel::FULL}
640 };
641 meshFit.type = MeshFitType::CORE;
642 meshFit.nSigma = Attributes::getReal(itsAttr[MESHFITNSIGMA]);
643 meshFit.clip = Attributes::getReal(itsAttr[MESHFITCLIP]);
644 meshFit.maxFarFraction = Attributes::getReal(itsAttr[MESHFITMAXFAR]) / 100.0;
645 meshFit.farField = stringFarField_s.at(Attributes::getString(itsAttr[MESHFITFARFIELD]));
646
647 if (!(meshFit.nSigma > 0.0) || !(meshFit.clip > 0.0)) {
648 throw OpalException("FieldSolver::initSolver",
649 "MESHFITNSIGMA and MESHFITCLIP must be positive.");
650 }
651 if (!(meshFit.maxFarFraction > 0.0 && meshFit.maxFarFraction <= 1.0)) {
652 throw OpalException("FieldSolver::initSolver",
653 "MESHFITMAXFAR must be larger than 0 and at most 100 (%).");
654 }
655
656 // The core-fitted mesh exists for the single-bunch solve of OPAL-cycl with the FFT
657 // solver and open boundary conditions; the tracker refuses it in multi-bunch mode.
658 if (!OpalData::getInstance()->isInOPALCyclMode()) {
659 throw OpalException("FieldSolver::initSolver",
660 "MESHFIT=\"CORE\" is only available in OPAL-cycl "
661 "(METHOD=\"CYCLOTRON-T\").");
662 }
664 throw OpalException("FieldSolver::initSolver",
665 "MESHFIT=\"CORE\" is only available with FSTYPE=\"FFT\", "
666 "not with \"" + fsName_m + "\".");
667 }
668 if (Attributes::getString(getBCZAttribute()) == "PERIODIC") {
669 // a periodic z would wrap the particles outside the mesh into it
670 throw OpalException("FieldSolver::initSolver",
671 "MESHFIT=\"CORE\" is not available with BCFFTZ=\"PERIODIC\".");
672 }
674 throw OpalException("FieldSolver::initSolver",
675 "MESHFIT=\"CORE\" is not available with a fixed grid "
676 "or an emitting beam.");
677 }
678
679 return meshFit;
680}
681
683 return (solver_m != 0);
684}
685
687 os << "* ************* F I E L D S O L V E R ********************************************** " << endl;
688 os << "* FIELDSOLVER " << getOpalName() << '\n'
689 << "* TYPE " << fsName_m << '\n'
690 << "* N-PROCESSORS " << Ippl::getNodes() << '\n'
691 << "* MX " << Attributes::getReal(itsAttr[MX]) << '\n'
692 << "* MY " << Attributes::getReal(itsAttr[MY]) << '\n'
693 << "* MT " << Attributes::getReal(itsAttr[MT]) << '\n'
694 << "* BBOXINCR " << Attributes::getReal(itsAttr[BBOXINCR]) << endl;
696 os << "* RC " << Attributes::getReal(itsAttr[RC]) << '\n'
697 << "* ALPHA " << Attributes::getReal(itsAttr[ALPHA]) << '\n'
698 << "* GREENSF " << Attributes::getString(itsAttr[GREENSF]) << endl;
699 } else if (fsType_m == FieldSolverType::FFT) {
700 os << "* GREENSF " << Attributes::getString(itsAttr[GREENSF]) << '\n'
701 << "* BCFFTX " << Attributes::getString(itsAttr[BCFFTX]) << '\n'
702 << "* BCFFTY " << Attributes::getString(itsAttr[BCFFTY]) << '\n'
703 << "* BCFFTZ " << Attributes::getString(getBCZAttribute()) << '\n'
704 << "* MESHFIT " << Attributes::getString(itsAttr[MESHFIT]) << endl;
705 if (Attributes::getString(itsAttr[MESHFIT]) == "CORE") {
706 os << "* MESHFITNSIGMA " << Attributes::getReal(itsAttr[MESHFITNSIGMA]) << '\n'
707 << "* MESHFITCLIP " << Attributes::getReal(itsAttr[MESHFITCLIP]) << '\n'
708 << "* MESHFITMAXFAR " << Attributes::getReal(itsAttr[MESHFITMAXFAR]) << " %\n"
709 << "* MESHFITFARFIELD " << Attributes::getString(itsAttr[MESHFITFARFIELD]) << endl;
710 }
711 } else if (fsType_m == FieldSolverType::SAAMG) {
712 os << "* GEOMETRY " << Attributes::getString(itsAttr[GEOMETRY]) << '\n'
713 << "* ITSOLVER " << Attributes::getString(itsAttr[ITSOLVER]) << '\n'
714 << "* INTERPL " << Attributes::getString(itsAttr[INTERPL]) << '\n'
715 << "* TOL " << Attributes::getReal(itsAttr[TOL]) << '\n'
716 << "* MAXITERS " << Attributes::getReal(itsAttr[MAXITERS]) << '\n'
717 << "* PRECMODE " << Attributes::getString(itsAttr[PRECMODE]) << endl;
718 } else if (Options::amr) {
719#ifdef ENABLE_AMR
720 os << "* AMR_MAXLEVEL " << Attributes::getReal(itsAttr[AMR_MAXLEVEL]) << '\n'
721 << "* AMR_REFX " << Attributes::getReal(itsAttr[AMR_REFX]) << '\n'
722 << "* AMR_REFY " << Attributes::getReal(itsAttr[AMR_REFY]) << '\n'
723 << "* AMR_REFZ " << Attributes::getReal(itsAttr[AMR_REFZ]) << '\n'
724 << "* AMR_MAXGRIDX " << Attributes::getReal(itsAttr[AMR_MAXGRIDX]) << '\n'
725 << "* AMR_MAXGRIDY " << Attributes::getReal(itsAttr[AMR_MAXGRIDY]) << '\n'
726 << "* AMR_MAXGRIDZ " << Attributes::getReal(itsAttr[AMR_MAXGRIDZ]) << '\n'
727 << "* AMR_BFX " << Attributes::getReal(itsAttr[AMR_BFX]) << '\n'
728 << "* AMR_BFY " << Attributes::getReal(itsAttr[AMR_BFY]) << '\n'
729 << "* AMR_BFZ " << Attributes::getReal(itsAttr[AMR_BFZ]) << '\n'
730 << "* AMR_TAGGING " << this->getTagging_m() <<'\n'
731 << "* AMR_DENSITY " << Attributes::getReal(itsAttr[AMR_DENSITY]) << '\n'
732 << "* AMR_MAX_NUM_PART " << Attributes::getReal(itsAttr[AMR_MAX_NUM_PART]) << '\n'
733 << "* AMR_MIN_NUM_PART " << Attributes::getReal(itsAttr[AMR_MIN_NUM_PART]) << '\n'
734 << "* AMR_DENSITY " << Attributes::getReal(itsAttr[AMR_DENSITY]) << '\n'
735 << "* AMR_SCALING " << Attributes::getReal(itsAttr[AMR_SCALING]) << endl;
736 auto length = Attributes::getRealArray(itsAttr[AMR_DOMAIN_RATIO]);
737 os << "* AMR_DOMAIN_RATIO ( ";
738 for (auto& l : length) {
739 os << l << " ";
740 }
741 os << ")" << endl;
742#endif
743 }
744
745#ifdef HAVE_AMR_MG_SOLVER
747 os << "* ITSOLVER (AMR_MG) "
748 << Attributes::getString(itsAttr[ITSOLVER]) << '\n'
749 << "* AMR_MG_PREC "
750 << Attributes::getString(itsAttr[AMR_MG_PREC]) << '\n'
751 << "* AMR_MG_REBALANCE "
752 << Attributes::getBool(itsAttr[AMR_MG_REBALANCE]) << '\n'
753 << "* AMR_MG_REUSE "
754 << Attributes::getString(itsAttr[AMR_MG_REUSE]) << '\n'
755 << "* AMR_MG_SMOOTHER "
756 << Attributes::getString(itsAttr[AMR_MG_SMOOTHER]) << '\n'
757 << "* AMR_MG_NSWEEPS "
758 << Attributes::getReal(itsAttr[AMR_MG_NSWEEPS]) << '\n'
759 << "* AMR_MG_INTERP "
760 << Attributes::getString(itsAttr[AMR_MG_INTERP]) << '\n'
761 << "* AMR_MG_NORM "
762 << Attributes::getString(itsAttr[AMR_MG_NORM]) << '\n'
763 << "* AMR_MG_TOL "
764 << Attributes::getReal(itsAttr[AMR_MG_TOL]) << '\n'
765 << "* AMR_MG_VERBOSE "
766 << Attributes::getBool(itsAttr[AMR_MG_VERBOSE]) << '\n'
767 << "* BCFFTX "
768 << Attributes::getString(itsAttr[BCFFTX]) << '\n'
769 << "* BCFFTY "
770 << Attributes::getString(itsAttr[BCFFTY]) << '\n'
771 << "* BCFFTZ "
773 }
774#endif
775
776 if (Attributes::getBool(itsAttr[PARFFTX])) {
777 os << "* XDIM parallel " << endl;
778 } else {
779 os << "* XDIM serial " << endl;
780 }
781
782 if (Attributes::getBool(itsAttr[PARFFTY])) {
783 os << "* YDIM parallel " << endl;
784 } else {
785 os << "* YDIM serial " << endl;
786 }
787
788 if (Attributes::getBool(itsAttr[PARFFTT])) {
789 os << "* Z(T)DIM parallel " << endl;
790 }else {
791 os << "* Z(T)DIM serial " << endl;
792 }
793
794 if ( !isAmrSolverType() ) {
795 INFOMSG(level3 << *mesh_m << endl);
796 INFOMSG(level3 << *PL_m << endl);
797 }
798
799 if (solver_m)
800 os << *solver_m << endl;
801 os << "* ********************************************************************************** " << endl;
802 return os;
803}
804
805#ifdef ENABLE_AMR
806std::string FieldSolver::getTagging_m() const {
807 std::string tagging = Attributes::getString(itsAttr[AMR_TAGGING]);
808
809 // This conversion was introduced to allow numbers for tagging (useful for the sampler).
810 if ( Util::isAllDigits(tagging) ) {
811 tagging = AmrObject::getTaggingString(std::stoi(tagging));
812 }
813 return tagging;
814}
815
816
818
819 auto domain = Attributes::getRealArray(itsAttr[AMR_DOMAIN_RATIO]);
820 dynamic_cast<AmrPartBunch*>(itsBunch_m)->setAmrDomainRatio(
821 Attributes::getRealArray(itsAttr[AMR_DOMAIN_RATIO])
822 );
824
825 // setup initial info for creating the object
827 info.grid[0] = (int)this->getMX();
828 info.grid[1] = (int)this->getMY();
829 info.grid[2] = (int)this->getMT();
830 info.maxgrid[0] = Attributes::getReal(itsAttr[AMR_MAXGRIDX]);
831 info.maxgrid[1] = Attributes::getReal(itsAttr[AMR_MAXGRIDY]);
832 info.maxgrid[2] = Attributes::getReal(itsAttr[AMR_MAXGRIDZ]);
833 info.bf[0] = Attributes::getReal(itsAttr[AMR_BFX]);
834 info.bf[1] = Attributes::getReal(itsAttr[AMR_BFY]);
835 info.bf[2] = Attributes::getReal(itsAttr[AMR_BFZ]);
836 info.maxlevel = Attributes::getReal(itsAttr[AMR_MAXLEVEL]);
837 info.refratio[0] = Attributes::getReal(itsAttr[AMR_REFX]);
838 info.refratio[1] = Attributes::getReal(itsAttr[AMR_REFY]);
839 info.refratio[2] = Attributes::getReal(itsAttr[AMR_REFZ]);
840
842
843 itsAmrObject_mp->setTagging( this->getTagging_m() );
844
845 itsAmrObject_mp->setScalingFactor( Attributes::getReal(itsAttr[AMR_SCALING]) );
846
847 itsAmrObject_mp->setChargeDensity( Attributes::getReal(itsAttr[AMR_DENSITY]) );
848
849 itsAmrObject_mp->setMaxNumParticles(
850 Attributes::getReal(itsAttr[AMR_MAX_NUM_PART])
851 );
852
853 itsAmrObject_mp->setMinNumParticles(
854 Attributes::getReal(itsAttr[AMR_MIN_NUM_PART])
855 );
856}
857
858
860
862 if ( dynamic_cast<AmrBoxLib*>( itsAmrObject_mp.get() ) == 0 )
863 throw OpalException("FieldSolver::initAmrSolver_m()",
864 "ML solver requires AMReX.");
865 solver_m = new MLPoissonSolver(static_cast<AmrBoxLib*>(itsAmrObject_mp.get()));
866#ifdef AMREX_ENABLE_FBASELIB
867 } else if (fsType_m == FieldSolverType::FMG) {
868
869 if ( dynamic_cast<AmrBoxLib*>( itsAmrObject_mp.get() ) == 0 )
870 throw OpalException("FieldSolver::initAmrSolver_m()",
871 "FMultiGrid solver requires AMReX.");
872
873 solver_m = new FMGPoissonSolver(static_cast<AmrBoxLib*>(itsAmrObject_mp.get()));
874#endif
875 } else if (fsType_m == FieldSolverType::HYPRE) {
876 throw OpalException("FieldSolver::initAmrSolver_m()",
877 "HYPRE solver not yet implemented.");
878 } else if (fsType_m == FieldSolverType::HPGMG) {
879 throw OpalException("FieldSolver::initAmrSolver_m()",
880 "HPGMG solver not yet implemented.");
881 } else if (fsType_m == FieldSolverType::AMRMG) {
882#ifdef HAVE_AMR_MG_SOLVER
883 if ( dynamic_cast<AmrBoxLib*>( itsAmrObject_mp.get() ) == 0 )
884 throw OpalException("FieldSolver::initAmrSolver_m()",
885 "FMultiGrid solver requires AMReX.");
886
887 std::string bcz = Attributes::getString(getBCZAttribute());
888
889 solver_m = new AmrMultiGrid(static_cast<AmrBoxLib*>(itsAmrObject_mp.get()),
891 Attributes::getString(itsAttr[AMR_MG_PREC]),
892 Attributes::getBool(itsAttr[AMR_MG_REBALANCE]),
893 Attributes::getString(itsAttr[AMR_MG_REUSE]),
896 bcz,
897 Attributes::getString(itsAttr[AMR_MG_SMOOTHER]),
898 Attributes::getReal(itsAttr[AMR_MG_NSWEEPS]),
899 Attributes::getString(itsAttr[AMR_MG_INTERP]),
900 Attributes::getString(itsAttr[AMR_MG_NORM]));
901
902 dynamic_cast<AmrMultiGrid*>(solver_m)->setVerbose(
903 Attributes::getBool(itsAttr[AMR_MG_VERBOSE]));
904
905 dynamic_cast<AmrMultiGrid*>(solver_m)->setTolerance(
906 Attributes::getReal(itsAttr[AMR_MG_TOL]));
907#else
908 throw OpalException("FieldSolver::initAmrSolver_m()",
909 "Multigrid solver not enabled! "
910 "Please build OPAL with -DENABLE_AMR_MG_SOLVER=1");
911#endif
912 } else
913 throw OpalException("FieldSolver::initAmrSolver_m()",
914 "Unknown solver " + getType() + ".");
915}
916#endif
UniformCartesian< 3, double > Mesh_t
Definition PBunchDefs.h:22
ParticleSpatialLayout< double, 3, Mesh_t > Layout_t
Definition PBunchDefs.h:24
CenteredFieldLayout< 3, Mesh_t, Center_t > FieldLayout_t
Definition PBunchDefs.h:28
@ SIZE
Definition IndexMap.cpp:178
e_dim_tag
Definition FieldLayout.h:55
@ PARALLEL
Definition FieldLayout.h:55
@ SERIAL
Definition FieldLayout.h:55
#define INFOMSG(msg)
Definition IpplInfo.h:348
Inform & endl(Inform &inf)
Definition Inform.cpp:42
Inform & level3(Inform &inf)
Definition Inform.cpp:47
const std::string name
Representation objects and parsers for attribute expressions.
Definition Expressions.h:64
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
double getReal(const Attribute &attr)
Return real value.
void setRealArray(Attribute &attr, const std::vector< double > &value)
Set array value.
Attribute makeUpperCaseString(const std::string &name, const std::string &help)
Make uppercase string attribute.
Attribute makePredefinedString(const std::string &name, const std::string &help, const std::initializer_list< std::string > &predefinedStrings)
Make predefined string attribute.
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
bool getBool(const Attribute &attr)
Return logical value.
void setReal(Attribute &attr, double val)
Set real value.
Attribute makeRealArray(const std::string &name, const std::string &help)
Create real array attribute.
std::vector< double > getRealArray(const Attribute &attr)
Get array value.
std::string getString(const Attribute &attr)
Get string value.
bool amr
Enable AMR if true.
Definition Options.cpp:97
bool isAllDigits(const std::string &str)
Definition Util.cpp:225
A representation of an Object attribute.
Definition Attribute.h:52
The base class for all OPAL definitions.
Definition Definition.h:30
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition Object.cpp:191
const std::string & getOpalName() const
Return object name.
Definition Object.cpp:310
void clear()
Clear the occurrence counter.
Definition Object.cpp:341
std::vector< Attribute > itsAttr
The object attributes.
Definition Object.h:216
virtual void set_meshEnlargement(double dh)
void setMeshFit(const MeshFitParameters &meshFit)
FIELDSOLVER, MESHFIT: the particles the space-charge mesh is fitted to.
bool getIfBeamEmitting()
bool isGridFixed() const
Object * find(const std::string &name)
Find entry.
Definition OpalData.cpp:571
static OpalData * getInstance()
Definition OpalData.cpp:196
BoundaryGeometry * getGlobalGeometry()
Definition OpalData.cpp:461
static std::unique_ptr< AmrBoxLib > create(const AmrInfo &info, AmrPartBunch *bunch_p)
Definition AmrBoxLib.cpp:74
static std::string getTaggingString(int number)
int grid[3]
Number of grid points in x-, y- and z-direction.
Definition AmrObject.h:54
int maxgrid[3]
Maximum grid size in x-, y- and z-direction.
Definition AmrObject.h:55
static BoundaryGeometry * find(const std::string &name)
Parameters of the mesh fit, passed to the bunch (PartBunchBase::setMeshFit())
Definition FieldSolver.h:66
FarFieldModel farField
Definition FieldSolver.h:71
MeshFitType type
Definition FieldSolver.h:67
MeshFitParameters getMeshFitParameters_m() const
MESHFIT and its parameters; throws if they are invalid or CORE is not available.
virtual void update()
Update the field solver data.
std::unique_ptr< Layout_t > PL_m
The particle layout.
FieldSolverType fsType_m
void setMT(double)
Store emittance for mode 3.
void initCartesianFields()
double getMY() const
Return meshsize.
std::string getTagging_m() const
Mesh_t * mesh_m
The cartesian mesh.
std::string fsName_m
PartBunchBase< double, 3 > * itsBunch_m
all the particles are here ...
std::string getType()
PoissonSolver * solver_m
the actual solver, should be a base object
Inform & printInfo(Inform &os) const
bool isAmrSolverType() const
static FieldSolver * find(const std::string &name)
Find named FieldSolver.
FieldSolver()
Exemplar constructor.
void setFieldSolverType()
virtual void execute()
Execute (init) the field solver data.
double getMX() const
Return meshsize.
FieldLayout_t * FL_m
The field layout f.
bool hasPeriodicZ()
void initAmrObject_m()
std::unique_ptr< AmrObject > itsAmrObject_mp
const Attribute & getBCZAttribute() const
void setMY(double)
Store emittance for mode 2.
double getMT() const
Return meshsize.
virtual ~FieldSolver()
bool hasValidSolver()
void setMX(double)
Store emittance for mode 1.
virtual FieldSolver * clone(const std::string &name)
Make clone.
void initSolver(PartBunchBase< double, 3 > *b)
void initAmrSolver_m()
The base class for all OPAL exceptions.
Definition Index.h:237
static int getNodes()
Definition IpplInfo.cpp:689