OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
BinningCmd.cpp
Go to the documentation of this file.
1
3
4#include <map>
5
8
10 : Definition(
11 BINNING::SIZE, "BINNING",
12 "The \"BINNING\" statement defines adaptive binning parameters."),
13 parameterName_m("VELOCITYZ"),
14 parameterType_m(BinningParameter::VELOCITYZ) {
16 "MAXBINS",
17 "The maximum number of bins used for adaptive binning. "
18 "Default: 128.",
19 128);
20
22 "DESIREDWIDTH",
23 "A bias [0, 1] that tries to steer the bin size to the given variable. "
24 "Default: 0.1.",
25 0.1);
26
28 "BINNINGALPHA",
29 "A value [0, 1] that determines how aggressive the algorithm tries to "
30 "reduce the number of bins. Default: 1.0.",
31 1.0);
32
34 "BINNINGBETA",
35 "A value [0, 1] that determines how aggressive the algorithm tries to "
36 "use wider bins. Default: 1.5.",
37 1.5);
38
40 "PARAMETER", "The bunch attribute used for binning.",
41 {"VELOCITYZ", "POSITIONZ", "PZ", "GAMMAZ"}, "VELOCITYZ");
42
44 "ADAPTIVEBINNING",
45 "If true, merge the initial uniform bins using the adaptive binning algorithm. "
46 "If false, keep the uniform MAXBINS histogram. Default: true.",
47 true);
48
50 "DUMPBINSFILE",
51 "The json file name for dumping bin configuration to a file. Default: \"NONE\" (no "
52 "dumping).",
53 "NONE");
54
56 "DUMPBINSFREQ", "The frequency in timesteps of dumping bins to a file. Default: 1.", 1);
57
59 "TABLEPRINTFREQ",
60 "The frequency in global timesteps of printing bin statistics to console. "
61 "If 0, printing is disabled. Default: 10.",
62 10);
63}
64
65BinningCmd::BinningCmd(const std::string& name, BinningCmd* parent)
66 : Definition(name, parent),
67 parameterName_m(parent->parameterName_m),
68 parameterType_m(parent->parameterType_m) {}
69
71
72BinningCmd* BinningCmd::clone(const std::string& name) { return new BinningCmd(name, this); }
73
74BinningCmd* BinningCmd::find(const std::string& name) {
75 BinningCmd* bc = dynamic_cast<BinningCmd*>(OpalData::getInstance()->find(name));
76
77 if (bc == nullptr) {
78 throw OpalException("BinningCmd::find()", "BinningCmd \"" + name + "\" not found.");
79 }
80 return bc;
81}
82
87
91 } else {
92 parameterName_m = "VELOCITYZ";
93 }
94
95 // Sanitize dump-to-file configuration if present.
98
99 // Treat empty or "NONE" as "no dumping".
100 if (!filename.empty() && filename != "NONE") {
101 // Ensure the file name ends with ".json".
102 const std::string extension = ".json";
103 if (filename.size() < extension.size()
104 || filename.substr(filename.size() - extension.size()) != extension) {
105 filename += extension;
107 }
108
109 // Validate frequency when dumping is enabled.
110 int freq = static_cast<int>(Attributes::getReal(itsAttr[BINNING::DUMPBINSFREQ]));
111 if (freq < 1) {
112 throw OpalException(
113 "BinningCmd::update",
114 "DUMPBINSFREQ must be >= 1 when DUMPBINSFILE is set.");
115 }
116 }
117 }
118
119 // Validate console table print frequency (used in binned mode).
120 {
121 const int freq = static_cast<int>(Attributes::getReal(itsAttr[BINNING::TABLEPRINTFREQ]));
122 if (freq < 0) {
123 throw OpalException("BinningCmd::update", "TABLEPRINTFREQ must be >= 0.");
124 }
125 }
126}
127
129 // Ensure we work from the current attribute value.
131
132 static const std::map<std::string, BinningParameter> stringToParam = {
133 {"VELOCITYZ", BinningParameter::VELOCITYZ},
134 {"POSITIONZ", BinningParameter::POSITIONZ},
135 {"PZ", BinningParameter::PZ},
136 {"GAMMAZ", BinningParameter::GAMMAZ}};
137
138 auto it = stringToParam.find(parameterName_m);
139 if (it == stringToParam.end()) {
140 throw OpalException(
141 "BinningCmd::setParameterType",
142 "Unknown binning PARAMETER \"" + parameterName_m + "\"");
143 }
144
145 parameterType_m = it->second;
146}
147
148Inform& BinningCmd::printInfo(Inform& os) const {
149 os << "* ************* B I N N I N G ****************************************************** "
150 << endl;
151 os << "* BINNING " << getOpalName() << '\n'
152 << "* MAXBINS " << getMaxBins() << '\n'
153 << "* DESIREDWIDTH " << getDesiredWidth() << '\n'
154 << "* BINNINGALPHA " << getBinningAlpha() << '\n'
155 << "* BINNINGBETA " << getBinningBeta() << '\n'
156 << "* ADAPTIVEBINNING " << (getAdaptiveBinning() ? "TRUE" : "FALSE") << '\n'
157 << "* PARAMETER " << parameterName_m << endl;
158 if (dumpBinsToFile()) {
159 os << "* DUMPBINSFILE " << getDumpBinsFileName() << '\n'
160 << "* DUMPBINSFREQ " << getDumpBinsFrequency() << endl;
161 }
162 os << "* TABLEPRINTFREQ " << getTablePrintFrequency() << endl;
163 os << "* ********************************************************************************** "
164 << endl;
165
166 return os;
167}
168
170 return static_cast<int>(Attributes::getReal(itsAttr[BINNING::MAXBINS]));
171}
172
176
180
184
188
192
196
198 int freq = static_cast<int>(Attributes::getReal(itsAttr[BINNING::DUMPBINSFREQ]));
199 if (dumpBinsToFile() && freq < 1) {
200 throw OpalException(
201 "BinningCmd::getDumpBinsFrequency",
202 "DUMPBINSFREQ must be >= 1 if dumping bins to a file.");
203 }
204 return freq;
205}
206
208 const int freq = static_cast<int>(Attributes::getReal(itsAttr[BINNING::TABLEPRINTFREQ]));
209 if (freq < 0) {
210 throw OpalException("BinningCmd::getTablePrintFrequency", "TABLEPRINTFREQ must be >= 0.");
211 }
212 return freq;
213}
214
216 const std::string filename = Attributes::getString(itsAttr[BINNING::DUMPBINSFILE]);
217 return !filename.empty() && filename != "NONE";
218}
219
BinningParameter
The parameter that is used for binning.
Definition BinningCmd.h:35
@ SIZE
Definition IndexMap.cpp:179
double getDesiredWidth() const
void setParameterType()
Recompute the enum-valued parameter from the string attribute.
int getDumpBinsFrequency() const
Get the frequency of dumping bins to a file.
double getBinningAlpha() const
virtual void update()
Update the binning data (internal cache of attributes).
int getTablePrintFrequency() const
int getMaxBins() const
bool getAdaptiveBinning() const
BinningParameter getParameterType() const
bool dumpBinsToFile() const
Check if dumping bins to a file is enabled.
virtual void execute()
Execute the BINNING command (currently a thin wrapper around update()).
std::string getParameter()
BinningParameter parameterType_m
Definition BinningCmd.h:108
virtual BinningCmd * clone(const std::string &name)
Make clone.
static BinningCmd * find(const std::string &name)
Find named BINNING command.
std::string getDumpBinsFileName() const
Get the file name for dumping bins to a file.
std::string parameterName_m
Definition BinningCmd.h:107
double getBinningBeta() const
Inform & printInfo(Inform &os) const
virtual ~BinningCmd()
BinningCmd()
Exemplar constructor.
Definition BinningCmd.cpp:9
The base class for all OPAL definitions.
Definition Definition.h:29
const std::string & getOpalName() const
Return object name.
Definition Object.cpp:267
std::vector< Attribute > itsAttr
The object attributes.
Definition Object.h:210
Object * find(const std::string &name)
Find entry.
Definition OpalData.cpp:477
static OpalData * getInstance()
Definition OpalData.cpp:193
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
double getReal(const Attribute &attr)
Return real value.
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.
void setString(Attribute &attr, const std::string &val)
Set string value.
bool getBool(const Attribute &attr)
Return logical value.
std::string getString(const Attribute &attr)
Get string value.
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
@ DESIREDWIDTH
Definition BinningCmd.h:41
@ BINNINGBETA
Definition BinningCmd.h:43
@ ADAPTIVEBINNING
Definition BinningCmd.h:45
@ BINNINGALPHA
Definition BinningCmd.h:42
@ DUMPBINSFILE
Definition BinningCmd.h:46
@ TABLEPRINTFREQ
Definition BinningCmd.h:48
@ PARAMETER
Definition BinningCmd.h:44
@ DUMPBINSFREQ
Definition BinningCmd.h:47