BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSInterpolatorType.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSDebug.hh"
19#include "BDSException.hh"
20#include "BDSInterpolatorType.hh"
21#include "BDSUtilities.hh"
22
23#include "globals.hh"
24#include "G4String.hh"
25
26#include <map>
27#include <string>
28#include <utility>
29
30// dictionary for BDSInterpolatorType for reflexivity
31template<>
32std::map<BDSInterpolatorType, std::string>* BDSInterpolatorType::dictionary =
33 new std::map<BDSInterpolatorType, std::string> ({
34 {BDSInterpolatorType::nearestauto,"nearestauto"},
35 {BDSInterpolatorType::nearest1d, "nearest1d"},
36 {BDSInterpolatorType::nearest2d, "nearest2d"},
37 {BDSInterpolatorType::nearest3d, "nearest3d"},
38 {BDSInterpolatorType::nearest4d, "nearest4d"},
39 {BDSInterpolatorType::linearauto, "linearauto"},
40 {BDSInterpolatorType::linear1d, "linear1d"},
41 {BDSInterpolatorType::linear2d, "linear2d"},
42 {BDSInterpolatorType::linear3d, "linear3d"},
43 {BDSInterpolatorType::linear4d, "linear4d"},
44 {BDSInterpolatorType::linearauto, "linearmagauto"},
45 {BDSInterpolatorType::linear1d, "linearmag1d"},
46 {BDSInterpolatorType::linear2d, "linearmag2d"},
47 {BDSInterpolatorType::linear3d, "linearmag3d"},
48 {BDSInterpolatorType::linear4d, "linearmag4d"},
49 {BDSInterpolatorType::cubicauto, "cubicauto"},
50 {BDSInterpolatorType::cubic1d, "cubic1d"},
51 {BDSInterpolatorType::cubic2d, "cubic2d"},
52 {BDSInterpolatorType::cubic3d, "cubic3d"},
53 {BDSInterpolatorType::cubic4d, "cubic4d"}
54 });
55
57{
58 std::map<G4String, BDSInterpolatorType> types;
59 types["nearest"] = BDSInterpolatorType::nearestauto;
60 types["nearest1d"] = BDSInterpolatorType::nearest1d;
61 types["nearest2d"] = BDSInterpolatorType::nearest2d;
62 types["nearest3d"] = BDSInterpolatorType::nearest3d;
63 types["nearest4d"] = BDSInterpolatorType::nearest4d;
64 types["linear"] = BDSInterpolatorType::linearauto;
65 types["linear1d"] = BDSInterpolatorType::linear1d;
66 types["linear2d"] = BDSInterpolatorType::linear2d;
67 types["linear3d"] = BDSInterpolatorType::linear3d;
68 types["linear4d"] = BDSInterpolatorType::linear4d;
69 types["linearmag"] = BDSInterpolatorType::linearmagauto;
70 types["linearmag1d"] = BDSInterpolatorType::linearmag1d;
71 types["linearmag2d"] = BDSInterpolatorType::linearmag2d;
72 types["linearmag3d"] = BDSInterpolatorType::linearmag3d;
73 types["linearmag4d"] = BDSInterpolatorType::linearmag4d;
74 types["cubic"] = BDSInterpolatorType::cubicauto;
75 types["cubic1d"] = BDSInterpolatorType::cubic1d;
76 types["cubic2d"] = BDSInterpolatorType::cubic2d;
77 types["cubic3d"] = BDSInterpolatorType::cubic3d;
78 types["cubic4d"] = BDSInterpolatorType::cubic4d;
79
80 interpolatorType = BDS::LowerCase(interpolatorType);
81
82 auto result = types.find(interpolatorType);
83 if (result == types.end())
84 {// it's not a valid key
85 G4String msg = "\"" + interpolatorType + "\" is not a valid field type\n";
86 msg += "Available interpolator types are:\n";
87 for (const auto& it : types)
88 {msg += "\"" + it.first + "\"\n";}
89 throw BDSException(__METHOD_NAME__, msg);
90 }
91
92#ifdef BDSDEBUG
93 G4cout << __METHOD_NAME__ << "determined interpolator type to be " << result->second << G4endl;
94#endif
95 return result->second;
96}
97
99{
100 G4int result = 0;
101 switch (it.underlying())
102 {
103 case BDSInterpolatorType::nearest1d:
104 case BDSInterpolatorType::linear1d:
105 case BDSInterpolatorType::linearmag1d:
106 case BDSInterpolatorType::cubic1d:
107 {result = 1; break;}
108 case BDSInterpolatorType::nearest2d:
109 case BDSInterpolatorType::linear2d:
110 case BDSInterpolatorType::linearmag2d:
111 case BDSInterpolatorType::cubic2d:
112 {result = 2; break;}
113 case BDSInterpolatorType::nearest3d:
114 case BDSInterpolatorType::linear3d:
115 case BDSInterpolatorType::linearmag3d:
116 case BDSInterpolatorType::cubic3d:
117 {result = 3; break;}
118 case BDSInterpolatorType::nearest4d:
119 case BDSInterpolatorType::linear4d:
120 case BDSInterpolatorType::linearmag4d:
121 case BDSInterpolatorType::cubic4d:
122 {result = 4; break;}
123 default:
124 {result = 0; break;}
125 }
126 return result;
127}
128
130{
131 G4bool result = false;
132 switch (typeIn.underlying())
133 {
134 case BDSInterpolatorType::nearestauto:
135 case BDSInterpolatorType::linearauto:
136 case BDSInterpolatorType::linearmagauto:
137 case BDSInterpolatorType::cubicauto:
138 {result = true; break;}
139 default:
140 {break;}
141 }
142 return result;
143}
144
146 BDSInterpolatorType autoType)
147{
148 std::map<std::pair<G4int, BDSInterpolatorType>, BDSInterpolatorType> mapping = {
149 {std::make_pair(1, BDSInterpolatorType::nearestauto), BDSInterpolatorType::nearest1d},
150 {std::make_pair(2, BDSInterpolatorType::nearestauto), BDSInterpolatorType::nearest2d},
151 {std::make_pair(3, BDSInterpolatorType::nearestauto), BDSInterpolatorType::nearest3d},
152 {std::make_pair(4, BDSInterpolatorType::nearestauto), BDSInterpolatorType::nearest4d},
153 {std::make_pair(1, BDSInterpolatorType::linearauto), BDSInterpolatorType::linear1d},
154 {std::make_pair(2, BDSInterpolatorType::linearauto), BDSInterpolatorType::linear2d},
155 {std::make_pair(3, BDSInterpolatorType::linearauto), BDSInterpolatorType::linear3d},
156 {std::make_pair(4, BDSInterpolatorType::linearauto), BDSInterpolatorType::linear4d},
157 {std::make_pair(1, BDSInterpolatorType::linearmagauto), BDSInterpolatorType::linearmag1d},
158 {std::make_pair(2, BDSInterpolatorType::linearmagauto), BDSInterpolatorType::linearmag2d},
159 {std::make_pair(3, BDSInterpolatorType::linearmagauto), BDSInterpolatorType::linearmag3d},
160 {std::make_pair(4, BDSInterpolatorType::linearmagauto), BDSInterpolatorType::linearmag4d},
161 {std::make_pair(1, BDSInterpolatorType::cubicauto), BDSInterpolatorType::cubic1d},
162 {std::make_pair(2, BDSInterpolatorType::cubicauto), BDSInterpolatorType::cubic2d},
163 {std::make_pair(3, BDSInterpolatorType::cubicauto), BDSInterpolatorType::cubic3d},
164 {std::make_pair(4, BDSInterpolatorType::cubicauto), BDSInterpolatorType::cubic4d},
165 };
166 auto key = std::make_pair(nDimension, autoType);
167 auto search = mapping.find(key);
168 if (search != mapping.end())
169 {return search->second;}
170 else
171 {throw BDSException(__METHOD_NAME__, "invalid number of dimensions in auto-mapping to interpolators.");}
172}
General exception with possible name of object and message.
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.