BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
BDSInterpolatorType.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 "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSInterpolatorType.hh"
22#include "BDSUtilities.hh"
23
24#include "globals.hh"
25#include "G4String.hh"
26
27#include <map>
28#include <string>
29#include <utility>
30
31// dictionary for BDSInterpolatorType for reflexivity
32template<>
33std::map<BDSInterpolatorType, std::string>* BDSInterpolatorType::dictionary =
34 new std::map<BDSInterpolatorType, std::string> ({
35 {BDSInterpolatorType::nearestauto,"nearestauto"},
36 {BDSInterpolatorType::nearest1d, "nearest1d"},
37 {BDSInterpolatorType::nearest2d, "nearest2d"},
38 {BDSInterpolatorType::nearest3d, "nearest3d"},
39 {BDSInterpolatorType::nearest4d, "nearest4d"},
40 {BDSInterpolatorType::linearauto, "linearauto"},
41 {BDSInterpolatorType::linear1d, "linear1d"},
42 {BDSInterpolatorType::linear2d, "linear2d"},
43 {BDSInterpolatorType::linear3d, "linear3d"},
44 {BDSInterpolatorType::linear4d, "linear4d"},
45 {BDSInterpolatorType::linearauto, "linearmagauto"},
46 {BDSInterpolatorType::linear1d, "linearmag1d"},
47 {BDSInterpolatorType::linear2d, "linearmag2d"},
48 {BDSInterpolatorType::linear3d, "linearmag3d"},
49 {BDSInterpolatorType::linear4d, "linearmag4d"},
50 {BDSInterpolatorType::cubicauto, "cubicauto"},
51 {BDSInterpolatorType::cubic1d, "cubic1d"},
52 {BDSInterpolatorType::cubic2d, "cubic2d"},
53 {BDSInterpolatorType::cubic3d, "cubic3d"},
54 {BDSInterpolatorType::cubic4d, "cubic4d"}
55 });
56
58{
59 std::map<G4String, BDSInterpolatorType> types;
60 types["nearest"] = BDSInterpolatorType::nearestauto;
61 types["nearest1d"] = BDSInterpolatorType::nearest1d;
62 types["nearest2d"] = BDSInterpolatorType::nearest2d;
63 types["nearest3d"] = BDSInterpolatorType::nearest3d;
64 types["nearest4d"] = BDSInterpolatorType::nearest4d;
65 types["linear"] = BDSInterpolatorType::linearauto;
66 types["linear1d"] = BDSInterpolatorType::linear1d;
67 types["linear2d"] = BDSInterpolatorType::linear2d;
68 types["linear3d"] = BDSInterpolatorType::linear3d;
69 types["linear4d"] = BDSInterpolatorType::linear4d;
70 types["linearmag"] = BDSInterpolatorType::linearmagauto;
71 types["linearmag1d"] = BDSInterpolatorType::linearmag1d;
72 types["linearmag2d"] = BDSInterpolatorType::linearmag2d;
73 types["linearmag3d"] = BDSInterpolatorType::linearmag3d;
74 types["linearmag4d"] = BDSInterpolatorType::linearmag4d;
75 types["cubic"] = BDSInterpolatorType::cubicauto;
76 types["cubic1d"] = BDSInterpolatorType::cubic1d;
77 types["cubic2d"] = BDSInterpolatorType::cubic2d;
78 types["cubic3d"] = BDSInterpolatorType::cubic3d;
79 types["cubic4d"] = BDSInterpolatorType::cubic4d;
80
81 interpolatorType = BDS::LowerCase(interpolatorType);
82
83 auto result = types.find(interpolatorType);
84 if (result == types.end())
85 {// it's not a valid key
86 G4String msg = "\"" + interpolatorType + "\" is not a valid field type\n";
87 msg += "Available interpolator types are:\n";
88 for (const auto& it : types)
89 {msg += "\"" + it.first + "\"\n";}
90 throw BDSException(__METHOD_NAME__, msg);
91 }
92
93#ifdef BDSDEBUG
94 G4cout << __METHOD_NAME__ << "determined interpolator type to be " << result->second << G4endl;
95#endif
96 return result->second;
97}
98
100{
101 G4int result = 0;
102 switch (it.underlying())
103 {
104 case BDSInterpolatorType::nearest1d:
105 case BDSInterpolatorType::linear1d:
106 case BDSInterpolatorType::linearmag1d:
107 case BDSInterpolatorType::cubic1d:
108 {result = 1; break;}
109 case BDSInterpolatorType::nearest2d:
110 case BDSInterpolatorType::linear2d:
111 case BDSInterpolatorType::linearmag2d:
112 case BDSInterpolatorType::cubic2d:
113 {result = 2; break;}
114 case BDSInterpolatorType::nearest3d:
115 case BDSInterpolatorType::linear3d:
116 case BDSInterpolatorType::linearmag3d:
117 case BDSInterpolatorType::cubic3d:
118 {result = 3; break;}
119 case BDSInterpolatorType::nearest4d:
120 case BDSInterpolatorType::linear4d:
121 case BDSInterpolatorType::linearmag4d:
122 case BDSInterpolatorType::cubic4d:
123 {result = 4; break;}
124 default:
125 {result = 0; break;}
126 }
127 return result;
128}
129
131{
132 G4bool result = false;
133 switch (typeIn.underlying())
134 {
135 case BDSInterpolatorType::nearestauto:
136 case BDSInterpolatorType::linearauto:
137 case BDSInterpolatorType::linearmagauto:
138 case BDSInterpolatorType::cubicauto:
139 {result = true; break;}
140 default:
141 {break;}
142 }
143 return result;
144}
145
147 BDSInterpolatorType autoType)
148{
149 std::map<std::pair<G4int, BDSInterpolatorType>, BDSInterpolatorType> mapping = {
150 {std::make_pair(1, BDSInterpolatorType::nearestauto), BDSInterpolatorType::nearest1d},
151 {std::make_pair(2, BDSInterpolatorType::nearestauto), BDSInterpolatorType::nearest2d},
152 {std::make_pair(3, BDSInterpolatorType::nearestauto), BDSInterpolatorType::nearest3d},
153 {std::make_pair(4, BDSInterpolatorType::nearestauto), BDSInterpolatorType::nearest4d},
154 {std::make_pair(1, BDSInterpolatorType::linearauto), BDSInterpolatorType::linear1d},
155 {std::make_pair(2, BDSInterpolatorType::linearauto), BDSInterpolatorType::linear2d},
156 {std::make_pair(3, BDSInterpolatorType::linearauto), BDSInterpolatorType::linear3d},
157 {std::make_pair(4, BDSInterpolatorType::linearauto), BDSInterpolatorType::linear4d},
158 {std::make_pair(1, BDSInterpolatorType::linearmagauto), BDSInterpolatorType::linearmag1d},
159 {std::make_pair(2, BDSInterpolatorType::linearmagauto), BDSInterpolatorType::linearmag2d},
160 {std::make_pair(3, BDSInterpolatorType::linearmagauto), BDSInterpolatorType::linearmag3d},
161 {std::make_pair(4, BDSInterpolatorType::linearmagauto), BDSInterpolatorType::linearmag4d},
162 {std::make_pair(1, BDSInterpolatorType::cubicauto), BDSInterpolatorType::cubic1d},
163 {std::make_pair(2, BDSInterpolatorType::cubicauto), BDSInterpolatorType::cubic2d},
164 {std::make_pair(3, BDSInterpolatorType::cubicauto), BDSInterpolatorType::cubic3d},
165 {std::make_pair(4, BDSInterpolatorType::cubicauto), BDSInterpolatorType::cubic4d},
166 };
167 auto key = std::make_pair(nDimension, autoType);
168 auto search = mapping.find(key);
169 if (search != mapping.end())
170 {return search->second;}
171 else
172 {throw BDSException(__METHOD_NAME__, "invalid number of dimensions in auto-mapping to interpolators.");}
173}
General exception with possible name of object and message.
Definition: BDSException.hh:35
static std::map< BDSTypeSafeEnum< interpolatortypes_def, int >, std::string > * dictionary
type underlying() const
return underlying value (can be used in switch statement)
BDSInterpolatorType InterpolatorTypeSpecificFromAuto(G4int nDimension, BDSInterpolatorType autoType)
G4bool InterpolatorTypeIsAuto(BDSInterpolatorType typeIn)
Return true if the type is one containing 'auto'.
BDSInterpolatorType DetermineInterpolatorType(G4String interpolatorType)
Function that determines enum from string (case-insensitive).
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
G4int NDimensionsOfInterpolatorType(const BDSInterpolatorType &it)
Report the number of dimensions for that interpolator type.