92 "The \"SAMPLE\" command initiates sampling.") {
94 (
"INPUT",
"Path to input file");
96 (
"OUTPUT",
"Name used in output file sample");
98 (
"OUTDIR",
"Name of directory used to run and store sample output files");
100 (
"OBJECTIVES",
"List of expressions to evaluate and store");
102 (
"STOREOBJECTIVES",
"List of stat variables to store");
104 (
"DVARS",
"List of sampling variables to be used");
106 (
"SAMPLINGS",
"List of sampling methods to be used");
108 (
"NUM_MASTERS",
"Number of master nodes");
110 (
"NUM_COWORKERS",
"Number processors per worker");
112 (
"TEMPLATEDIR",
"Directory where templates are stored");
114 (
"FIELDMAPDIR",
"Directory where field maps are stored");
116 (
"DISTDIR",
"Directory where distributions are stored");
118 (
"RASTER",
"Scan full space given by design variables (default: true)",
true);
120 (
"SEED",
"Seed for global random number generator (default: 42)", 42);
122 (
"KEEP",
"List of files to keep for each simulation. (default: all files kept)");
124 (
"RESTART_FILE",
"H5 file to restart the OPAL simulations from (optional)",
"");
126 (
"RESTART_STEP",
"Restart from given H5 step (optional)",
129 (
"JSON_DUMP_FREQ",
"Defines how often new individuals are appended to the final JSON file, "
130 "i.e. every time JSON_DUMP_FREQ samples finished they are written (optional)",
148 namespace fs = std::filesystem;
151 opal->setOptimizerFlag();
168 if ( sampling.size() != dvarsstr.size() )
170 "Number of sampling methods != number of design variables.");
172 typedef std::map< std::string, std::shared_ptr<SamplingMethod> > sampleMethods_t;
173 sampleMethods_t sampleMethods;
175 std::map<std::string, std::pair<double, double> > vars;
177 for (std::string &
name : dvarsstr) {
179 DVar* dvar =
dynamic_cast<DVar*
>(obj);
180 if (dvar ==
nullptr) {
182 "The sampling variable " +
name +
" is not known");
189 auto ret = vars.insert(std::make_pair(var, std::make_pair(lowerbound, upperbound)));
190 if (ret.second ==
false) {
192 "There is already a design variable with the variable " + var +
" defined");
195 DVar_t tmp = boost::make_tuple(var, lowerbound, upperbound);
204 funcs.insert(std::pair<std::string, client::function::type>
208 funcs.insert(std::pair<std::string, client::function::type>
212 funcs.insert(std::pair<std::string, client::function::type>
213 (
"sddsVariableAt", ff));
216 funcs.insert(std::pair<std::string, client::function::type>
220 funcs.insert(std::pair<std::string, client::function::type>
221 (
"maxNormRadialPeak", ff));
224 funcs.insert(std::pair<std::string, client::function::type>
225 (
"numberOfPeaks", ff));
228 funcs.insert(std::pair<std::string, client::function::type>
229 (
"sumErrSqRadialPeak", ff));
232 funcs.insert(std::pair<std::string, client::function::type>
233 (
"probVariableWithID", ff));
235 std::string fname = inputfile.stem().native();
237 funcs.insert(std::pair<std::string, client::function::type>
238 (
"statVariableAt", ff));
241 funcs.insert(std::pair<std::string, client::function::type>
246 std::set<std::string> objExpressions;
247 for (std::string
name: objectivesstr) {
250 if (objective ==
nullptr) {
252 "The objective " +
name +
" is not known");
258 auto ret = objExpressions.insert(expr);
259 if (ret.second ==
false) {
261 "There is already a objective with the expression " + expr +
" defined");
269 std::set<std::string>
names;
270 for (
size_t i = 0; i < sampling.size(); ++i) {
275 "Sampling method not found.");
280 if ( vars.find(name) == vars.end() ) {
282 "Variable '" + name +
"' not a DVAR.");
285 auto ret = names.insert(name);
286 if (ret.second ==
false) {
288 "There is already a sampling method with the variable " + name +
" defined");
316 std::vector<std::string> arguments(opal->getArguments());
317 std::vector<char*> argv;
318 std::map<unsigned int, std::string> argumentMapper({
319 {INPUT,
"inputfile"},
322 {NUMMASTERS,
"num-masters"},
323 {NUMCOWORKERS,
"num-coworkers"},
324 {RESTART_FILE,
"restartfile"},
325 {RESTART_STEP,
"restartstep"},
326 {JSON_DUMP_FREQ,
"jsonDumpFreq"}
329 auto it = argumentMapper.end();
330 for (
unsigned int i = 0; i <
SIZE; ++ i) {
331 if ((it = argumentMapper.find(i)) != argumentMapper.end()) {
333 if (type ==
"string") {
336 arguments.push_back(argument);
338 }
else if (type ==
"real") {
341 size_t last = val.find_last_not_of(
'0');
342 if (val[last] !=
'.') ++ last;
343 val.erase (last, std::string::npos );
344 std::string argument =
"--" + (*it).second +
"=" + val;
345 arguments.push_back(argument);
347 }
else if (type ==
"logical") {
350 arguments.push_back(argument);
359 arguments.push_back(
"--nsamples=" + std::to_string(nSample));
363 "The argument INPUT has to be provided");
368 if (dir.is_relative()) {
369 fs::path path = fs::path(std::string(getenv(
"PWD")));
374 if (!fs::exists(dir)) {
375 fs::create_directory(dir);
377 std::string argument =
"--simtmpdir=" + dir.native();
378 arguments.push_back(argument);
383 if (dir.is_relative()) {
384 fs::path path = fs::path(std::string(getenv(
"PWD")));
389 std::string argument =
"--templates=" + dir.native();
390 arguments.push_back(argument);
395 if (dir.is_relative()) {
396 fs::path path = fs::path(std::string(getenv(
"PWD")));
401 setenv(
"FIELDMAPS", dir.c_str(), 1);
406 if (dir.is_relative()) {
407 fs::path path = fs::path(std::string(getenv(
"PWD")));
412 setenv(
"DISTRIBUTIONS", dir.c_str(), 1);
417 size_t pos = tmplFile.find_last_of(
"/");
418 if(pos != std::string::npos)
419 tmplFile = tmplFile.substr(pos+1);
420 pos = tmplFile.find(
".");
421 tmplFile = tmplFile.substr(0,pos);
424 std::ifstream infile(tmplFile.c_str());
426 std::map<std::string, short> dvarCheck;
427 auto itr = dvars.begin();
428 for (; itr != dvars.end(); ++ itr) {
429 dvarCheck.insert(std::make_pair(boost::get<0>(itr->second), 0));
432 while(infile.good()) {
434 std::getline(infile, line,
'\n');
437 for(
auto &check: dvarCheck) {
438 size_t pos = line.find(
"_" + check.first +
"_");
439 if (pos != std::string::npos &&
440 dvarCheck.find(check.first) != dvarCheck.end()) {
441 dvarCheck.at(check.first) = 1;
447 for (
auto itr = dvarCheck.begin(); itr != dvarCheck.end(); ++ itr) {
448 if (itr->second == 0) {
450 "Couldn't find the design variable '" + itr->first +
"' in '" + tmplFile +
"'!");
456 for (
size_t i = 0; i < arguments.size(); ++ i) {
457 argv.push_back(const_cast<char*>(arguments[i].c_str()));
458 *gmsg << arguments[i] <<
" ";
462 std::map<std::string, std::string> userVariables = opal->getVariableData();
469 std::shared_ptr<Comm_t> comm(
new Comm_t(args, MPI_COMM_WORLD));
470 if (comm->isWorker())
473 if ( comm->isOptimizer() ) {
475 it != sampleMethods.end(); ++
it)
477 it->second->allocate(args, comm->getBundle());
481 const std::unique_ptr<pilot_t>
pi(
new pilot_t(args, comm, funcs, dvars,
482 objectives, sampleMethods,
483 storeobjstr, filesToKeep, userVariables));
484 if (comm->isWorker())
489 MPI_Abort(MPI_COMM_WORLD, -100);
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
static OpalData * getInstance()
unsigned int getSize() const
int seed
The current random seed.
std::string getVariable() const
The base class for all OPAL objects.
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
std::string getString(const Attribute &attr)
Get string value.
boost::function< boost::tuple< double, bool >arguments_t)> type
static OpalData * popInstance()
virtual const char * what() const
virtual void execute()
Execute the command.
double getUpperBound() const
std::map< std::string, DVar_t > DVarContainer_t
Inform & endl(Inform &inf)
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
std::shared_ptr< SamplingMethod > sampleMethod_m
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
std::string getVariable() const
SampleCmd()
Exemplar constructor.
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
std::string getExpression() const
bool getBool(const Attribute &attr)
Return logical value.
std::string::iterator iterator
std::pair< std::string, DVar_t > namedDVar_t
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
double FromFile(std::string file, const std::vector< double > &referencePoint)
void initialize(const std::string &dvarName, double lower, double upper, size_t modulo=1, bool sequence=false)
boost::tuple< std::string, double, double > DVar_t
type of design variables
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
The base class for all OPAL exceptions.
Attribute makeUpperCaseStringArray(const std::string &name, const std::string &help)
Make uppercase string array attribute.
Attribute makeStringArray(const std::string &name, const std::string &help)
Create a string array attribute.
std::shared_ptr< CmdArguments > CmdArguments_t
static void stashInstance()
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
virtual SampleCmd * clone(const std::string &name)
Make clone.
std::map< std::string, client::function::type > functionDictionary_t
static OpalSample * find(const std::string &name)
Find sampling method.
std::vector< Attribute > itsAttr
The object attributes.
double getLowerBound() const
double getReal(const Attribute &attr)
Return real value.
std::vector< std::string > getStringArray(const Attribute &attr)
Get string array value.
constexpr double e
The value of .
static void setGlobalSeed(unsigned int seed)
The base class for all OPAL actions.
std::pair< std::string, Expressions::Expr_t * > SingleNamed_t
Concrete implementation of an Opal simulation wrapper.