BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSArrayReflectionType.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 "BDSArrayReflectionType.hh"
19#include "BDSDebug.hh"
20#include "BDSException.hh"
21#include "BDSUtilities.hh"
22
23#include "globals.hh"
24#include "G4String.hh"
25
26#include <map>
27#include <set>
28#include <string>
29#include <vector>
30
31// dictionary for BDSArrayReflectionType for reflexivity
32template<>
33std::map<BDSArrayReflectionType, std::string>* BDSArrayReflectionType::dictionary =
34 new std::map<BDSArrayReflectionType, std::string> ({
35 {BDSArrayReflectionType::flipx, "flipx"},
36 {BDSArrayReflectionType::flipy, "flipy"},
37 {BDSArrayReflectionType::flipz, "flipz"},
38 {BDSArrayReflectionType::flipt, "flipt"},
39 {BDSArrayReflectionType::reflectx, "reflectx"},
40 {BDSArrayReflectionType::reflecty, "reflecty"},
41 {BDSArrayReflectionType::reflectz, "reflectz"},
42 {BDSArrayReflectionType::reflectt, "reflectt"},
43 {BDSArrayReflectionType::reflectxydipole, "reflectxydipole"},
44 {BDSArrayReflectionType::reflectxzdipole, "reflectxzdipole"},
45 {BDSArrayReflectionType::reflectyzdipole, "reflectyzdipole"},
46 {BDSArrayReflectionType::reflectzsolenoid, "reflectzsolenoid"},
47 {BDSArrayReflectionType::reflectxyquadrupole, "reflectxyquadrupole"}
48});
49
51{
52 std::map<G4String, BDSArrayReflectionType> types;
53 types["flipx"] = BDSArrayReflectionType::flipx;
54 types["flipy"] = BDSArrayReflectionType::flipy;
55 types["flipz"] = BDSArrayReflectionType::flipz;
56 types["flipt"] = BDSArrayReflectionType::flipt;
57 types["reflectx"] = BDSArrayReflectionType::reflectx;
58 types["reflecty"] = BDSArrayReflectionType::reflecty;
59 types["reflectz"] = BDSArrayReflectionType::reflectz;
60 types["reflectt"] = BDSArrayReflectionType::reflectt;
61 types["reflectxydipole"] = BDSArrayReflectionType::reflectxydipole;
62 types["reflectxzdipole"] = BDSArrayReflectionType::reflectxzdipole;
63 types["reflectyzdipole"] = BDSArrayReflectionType::reflectyzdipole;
64 types["reflectzsolenoid"] = BDSArrayReflectionType::reflectzsolenoid;
65 types["reflectxyquadrupole"] = BDSArrayReflectionType::reflectxyquadrupole;
66
67 arrayReflectionType = BDS::LowerCase(arrayReflectionType);
68
69 auto result = types.find(arrayReflectionType);
70 if (result == types.end())
71 {// it's not a valid key
72 G4String msg = "\"" + arrayReflectionType + "\" is not a valid array reflection type\n";
73 msg += "Available array reflection types are:\n";
74 for (const auto& it : types)
75 {msg += "\"" + it.first + "\"\n";}
76 throw BDSException(__METHOD_NAME__, msg);
77 }
78
79#ifdef BDSDEBUG
80 G4cout << __METHOD_NAME__ << "determined array reflection type to be " << result->second << G4endl;
81#endif
82 return result->second;
83}
84
85BDSArrayReflectionTypeSet BDS::DetermineArrayReflectionTypeSet(const G4String& arrayReflectionType)
86{
87 BDSArrayReflectionTypeSet result;
88 std::vector<G4String> words = BDS::SplitOnWhiteSpace(arrayReflectionType);
89 for (const auto& word : words)
90 {result.insert(BDS::DetermineArrayReflectionType(word));}
91 return result;
92}
93
94G4bool BDS::ProblemWithArrayReflectionCombination(const BDSArrayReflectionTypeSet& setIn, G4String* details)
95{
96 // we permit flipxyzt with a reflection
97 // we permit reflectxyzt (any combination)
98 // we permit a special reflection but not with any one of reflectxyzt
99 // therefore we substitute flipxyzt and relfectxyzt to reflectsimple here
100 const std::map<BDSArrayReflectionType, BDSArrayReflectionType> substitutions = {
101 {BDSArrayReflectionType::flipx, BDSArrayReflectionType::flipsimple},
102 {BDSArrayReflectionType::flipy, BDSArrayReflectionType::flipsimple},
103 {BDSArrayReflectionType::flipz, BDSArrayReflectionType::flipsimple},
104 {BDSArrayReflectionType::flipt, BDSArrayReflectionType::flipsimple},
105 {BDSArrayReflectionType::reflectx, BDSArrayReflectionType::reflectsimple},
106 {BDSArrayReflectionType::reflecty, BDSArrayReflectionType::reflectsimple},
107 {BDSArrayReflectionType::reflectz, BDSArrayReflectionType::reflectsimple},
108 {BDSArrayReflectionType::reflectt, BDSArrayReflectionType::reflectsimple},
109 {BDSArrayReflectionType::reflectxydipole, BDSArrayReflectionType::reflectadvanced},
110 {BDSArrayReflectionType::reflectxzdipole, BDSArrayReflectionType::reflectadvanced},
111 {BDSArrayReflectionType::reflectyzdipole, BDSArrayReflectionType::reflectadvanced},
112 {BDSArrayReflectionType::reflectzsolenoid, BDSArrayReflectionType::reflectadvanced},
113 {BDSArrayReflectionType::reflectxyquadrupole, BDSArrayReflectionType::reflectadvanced}
114 };
115
116 BDSArrayReflectionTypeSet test;
117 BDSArrayReflectionTypeSet testAdvanced;
118 for (const auto& reflection : setIn)
119 {// all reflections are included so this is safe
120 auto sub = substitutions.at(reflection);
121 test.insert(sub);
122 if (sub == BDSArrayReflectionType::reflectadvanced)
123 {testAdvanced.insert(reflection);}
124 }
125 auto testSize = test.size();
126 auto testAdvancedSize = testAdvanced.size();
127 if (testSize <= 1 && testAdvancedSize <= 1)
128 {return false;} // if only one class of operation, and max 1x advanced reflection, then it'll be fine
129 else if (testAdvancedSize > 1)
130 {// only one type of advanced reflection at a time
131 if (details)
132 {(*details) += "more than one 'specific' type of reflection - only one allowed";}
133 return true;
134 }
135 else
136 {return false;}
137}
138
139std::ostream& operator<< (std::ostream &out, BDSArrayReflectionTypeSet const& t)
140{
141 for (const auto& v : t)
142 {out << v;}
143 return out;
144}
General exception with possible name of object and message.
Improve type-safety of native enum data type in C++.
static std::map< BDSTypeSafeEnum< def, inner >, std::string > * dictionary
BDSArrayReflectionTypeSet DetermineArrayReflectionTypeSet(const G4String &arrayReflectionType)
Return a std::set of reflection types. Split string on white space.
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
BDSArrayReflectionType DetermineArrayReflectionType(G4String arrayReflectionType)
Function that gives corresponding enum value for string (case-insensitive)
G4bool ProblemWithArrayReflectionCombination(const BDSArrayReflectionTypeSet &setIn, G4String *details=nullptr)
Return true if there's a conceptual conflict with the set of field reflections requested.
std::vector< G4String > SplitOnWhiteSpace(const G4String &input)
Split a string on whitespace and return a vector of these 'words'.