OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
OpalSample.cpp
Go to the documentation of this file.
1 //
2 // Class OpalSample
3 // The SAMPLING definition.
4 // A SAMPLING definition is used to run the optimizer in sample mode.
5 //
6 // Copyright (c) 2018, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
7 // All rights reserved
8 //
9 // Implemented as part of the PhD thesis
10 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
11 //
12 // This file is part of OPAL.
13 //
14 // OPAL is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
21 //
22 #include "Sample/OpalSample.h"
23 
25 #include "Attributes/Attributes.h"
27 #include "Utilities/Util.h"
28 
29 #include "Sample/Uniform.h"
30 #include "Sample/Normal.h"
31 #include "Sample/SampleSequence.h"
33 #include "Sample/FromFile.h"
34 #include "Sample/LatinHyperCube.h"
36 
37 
38 // The attributes of class OpalSample.
39 namespace {
40  enum {
41  TYPE, // The type of sampling
42  VARIABLE, // name of design variable
43  SEED, // for random sample methods
44  FNAME, // file to read from sampling points
45  N,
46  RANDOM,
47  STEP,
48  SIZE
49  };
50 }
51 
53  Definition(SIZE, "SAMPLING",
54  "The \"SAMPLING\" statement defines methods used for the optimizer in sample mode.")
55  , size_m(1)
56 {
58  ("TYPE", "Distribution type.", {"UNIFORM_INT", "UNIFORM", "GAUSSIAN", "FROMFILE", "LATIN_HYPERCUBE"});
59 
61  ("VARIABLE", "Name of design variable");
62 
64  ("SEED", "seed for random sampling");
65 
67  ("FNAME", "File to read from the sampling points");
68 
70  ("N", "Number of sampling points", 1);
71 
73  ("RANDOM", "Whether sequence should be sampled randomly (default: false)", false);
74 
76  ("STEP", "Increment for randomized sequences (default: 1)", 1.0);
77 
79 }
80 
81 
82 OpalSample::OpalSample(const std::string &name, OpalSample *parent):
83  Definition(name, parent)
84 {}
85 
86 
87 OpalSample *OpalSample::clone(const std::string &name) {
88  return new OpalSample(name, this);
89 }
90 
91 
93 
94 }
95 
96 
97 OpalSample *OpalSample::find(const std::string &name) {
98  OpalSample *sampling = dynamic_cast<OpalSample *>(OpalData::getInstance()->find(name));
99 
100  if (sampling == nullptr) {
101  throw OpalException("OpalSample::find()",
102  "OpalSample \"" + name + "\" not found.");
103  }
104  return sampling;
105 }
106 
107 
108 void OpalSample::initialize(const std::string &dvarName,
109  double lower,
110  double upper,
111  size_t modulo,
112  bool /*sequence*/) {
113 
114  if ( lower >= upper )
115  throw OpalException("OpalSample::initialize()",
116  "Lower bound >= upper bound.");
117 
118  std::string type = Attributes::getString(itsAttr[TYPE]);
119 
120  int seed = Attributes::getReal(itsAttr[SEED]);
122  double step = Attributes::getReal(itsAttr[STEP]);
123 
124  bool random = Attributes::getBool(itsAttr[RANDOM]);
125 
126  if (!random) {
127  if (type == "UNIFORM_INT") {
128  sampleMethod_m.reset( new SampleSequence<int>(lower, upper, modulo, size_m) );
129  } else if (type == "UNIFORM") {
130  sampleMethod_m.reset( new SampleSequence<double>(lower, upper, modulo, size_m) );
131  } else if (type == "GAUSSIAN") {
132  sampleMethod_m.reset( new SampleGaussianSequence(lower, upper, modulo, size_m) );
133  } else if (type == "FROMFILE") {
134  std::string fname = Attributes::getString(itsAttr[FNAME]);
135  sampleMethod_m.reset( new FromFile(fname, dvarName, modulo) );
136  size_m = static_cast<FromFile*>(sampleMethod_m.get())->getSize();
137  }
138  } else {
139  if (type == "UNIFORM_INT") {
140  if (Attributes::getReal(itsAttr[SEED])) {
141  sampleMethod_m.reset( new Uniform<int>(lower, upper, seed) );
142  } else {
143  sampleMethod_m.reset( new Uniform<int>(lower, upper) );
144  }
145  } else if (type == "UNIFORM") {
146  if (Attributes::getReal(itsAttr[SEED])) {
147  sampleMethod_m.reset( new Uniform<double>(lower, upper, seed) );
148  } else {
149  sampleMethod_m.reset( new Uniform<double>(lower, upper) );
150  }
151  } else if (type == "GAUSSIAN") {
152  if (Attributes::getReal(itsAttr[SEED])) {
153  sampleMethod_m.reset( new Normal(lower, upper, seed) );
154  } else {
155  sampleMethod_m.reset( new Normal(lower, upper) );
156  }
157  } else if (type == "FROMFILE") {
158  std::string fname = Attributes::getString(itsAttr[FNAME]);
159  sampleMethod_m.reset( new FromFile(fname, dvarName, modulo) );
160  size_m = static_cast<FromFile*>(sampleMethod_m.get())->getSize();
161  } else if (type == "LATIN_HYPERCUBE") {
162  if (Attributes::getReal(itsAttr[SEED])) {
163  sampleMethod_m.reset( new LatinHyperCube(lower, upper, seed) );
164  } else {
165  sampleMethod_m.reset( new LatinHyperCube(lower, upper) );
166  }
167  } else if (type == "RANDOM_SEQUENCE_UNIFORM_INT") {
168  if (Attributes::getReal(itsAttr[SEED])) {
169  sampleMethod_m.reset(
170  new SampleRandomizedSequence<int>(lower, upper, step, seed)
171  );
172  } else {
173  sampleMethod_m.reset(
174  new SampleRandomizedSequence<int>(lower, upper, step)
175  );
176  }
177  } else if (type == "RANDOM_SEQUENCE_UNIFORM") {
178  if (Attributes::getReal(itsAttr[SEED])) {
179  sampleMethod_m.reset(
180  new SampleRandomizedSequence<double>(lower, upper, step, seed)
181  );
182  } else {
183  sampleMethod_m.reset(
184  new SampleRandomizedSequence<double>(lower, upper, step)
185  );
186  }
187  }
188  }
189 }
190 
191 
192 std::string OpalSample::getVariable() const {
193  return Attributes::getString(itsAttr[VARIABLE]);
194 }
@ SIZE
Definition: IndexMap.cpp:174
const std::string name
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:90
double getReal(const Attribute &attr)
Return real value.
Definition: Attributes.cpp:252
Attribute makePredefinedString(const std::string &name, const std::string &help, const std::initializer_list< std::string > &predefinedStrings)
Make predefined string attribute.
Definition: Attributes.cpp:409
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Definition: Attributes.cpp:240
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:100
std::string getString(const Attribute &attr)
Get string value.
Definition: Attributes.cpp:343
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
Definition: Attributes.cpp:332
int seed
The current random seed.
Definition: Options.cpp:37
boost::function< boost::tuple< double, bool >arguments_t)> type
Definition: function.hpp:21
double FromFile(std::string file, const std::vector< double > &referencePoint)
The base class for all OPAL definitions.
Definition: Definition.h:30
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:191
std::vector< Attribute > itsAttr
The object attributes.
Definition: Object.h:216
Object * find(const std::string &name)
Find entry.
Definition: OpalData.cpp:565
static OpalData * getInstance()
Definition: OpalData.cpp:195
Definition: Normal.h:27
static OpalSample * find(const std::string &name)
Find sampling method.
Definition: OpalSample.cpp:97
void initialize(const std::string &dvarName, double lower, double upper, size_t modulo=1, bool sequence=false)
Definition: OpalSample.cpp:108
unsigned int getSize() const
Definition: OpalSample.h:74
std::shared_ptr< SamplingMethod > sampleMethod_m
Definition: OpalSample.h:59
unsigned int size_m
Definition: OpalSample.h:70
virtual void execute()
Check the OpalSample data.
Definition: OpalSample.cpp:92
virtual OpalSample * clone(const std::string &name)
Make clone.
Definition: OpalSample.cpp:87
std::string getVariable() const
Definition: OpalSample.cpp:192
OpalSample()
Exemplar constructor.
Definition: OpalSample.cpp:52
The base class for all OPAL exceptions.
Definition: OpalException.h:28