BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSFieldFormat.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSFieldFormat.hh"
20#include "BDSDebug.hh"
21#include "BDSException.hh"
22#include "BDSUtilities.hh"
23
24#include "globals.hh"
25#include "G4String.hh"
26
27#include <map>
28#include <string>
29
30// dictionary for BDSFieldFormat for reflexivity
31template<>
32std::map<BDSFieldFormat, std::string>* BDSFieldFormat::dictionary =
33 new std::map<BDSFieldFormat, std::string> ({
34 {BDSFieldFormat::none, "none"},
35 {BDSFieldFormat::bdsim1d, "bdsim1d"},
36 {BDSFieldFormat::bdsim2d, "bdsim2d"},
37 {BDSFieldFormat::bdsim3d, "bdsim3d"},
38 {BDSFieldFormat::bdsim4d, "bdsim4d"},
39 {BDSFieldFormat::poisson2d, "poisson2d"},
40 {BDSFieldFormat::poisson2dquad, "poisson2dquad"},
41 {BDSFieldFormat::poisson2ddipole, "poisson2ddipole"}
42});
43
45{
46 std::map<G4String, BDSFieldFormat> formats;
47 formats["none"] = BDSFieldFormat::none;
48 formats["bdsim1d"] = BDSFieldFormat::bdsim1d;
49 formats["bdsim2d"] = BDSFieldFormat::bdsim2d;
50 formats["bdsim3d"] = BDSFieldFormat::bdsim3d;
51 formats["bdsim4d"] = BDSFieldFormat::bdsim4d;
52 formats["poisson2d"] = BDSFieldFormat::poisson2d;
53 formats["poisson2dquad"] = BDSFieldFormat::poisson2dquad;
54 formats["poisson2ddipole"] = BDSFieldFormat::poisson2ddipole;
55
56 bFormat = BDS::LowerCase(bFormat);
57
58 auto result = formats.find(bFormat);
59 if (result == formats.end())
60 {
61 // it's not a valid key
62 G4cerr << __METHOD_NAME__ << "\"" << bFormat
63 << "\" is not a valid field format" << G4endl;
64 G4cout << "Available field formats are:" << G4endl;
65 for (auto it : formats)
66 {G4cout << "\"" << it.first << "\"" << G4endl;}
67 throw BDSException(__METHOD_NAME__, "can't determine field type");
68 }
69
70#ifdef BDSDEBUG
71 G4cout << __METHOD_NAME__ << "determined field format to be " << result->second << G4endl;
72#endif
73 return result->second;
74}
75
77{
78 G4int result = 0;
79 switch (ff.underlying())
80 {
81 case BDSFieldFormat::bdsim1d:
82 {result = 1; break;}
83 case BDSFieldFormat::bdsim2d:
84 case BDSFieldFormat::poisson2d:
85 case BDSFieldFormat::poisson2dquad:
86 case BDSFieldFormat::poisson2ddipole:
87 {result = 2; break;}
88 case BDSFieldFormat::bdsim3d:
89 {result = 3; break;}
90 case BDSFieldFormat::bdsim4d:
91 {result = 4; break;}
92 case BDSFieldFormat::none:
93 default:
94 {result = 0; break;}
95 }
96 return result;
97}
General exception with possible name of object and message.
Definition: BDSException.hh:35
static std::map< BDSTypeSafeEnum< fieldformats_def, int >, std::string > * dictionary
type underlying() const
return underlying value (can be used in switch statement)
BDSFieldFormat DetermineFieldFormat(G4String fieldformat)
Function that gives corresponding enum value for string (case-insensitive)
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4int NDimensionsOfFieldFormat(const BDSFieldFormat &ff)
Report the number of dimensions for that format.