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())
488 std::cout <<
"Exception caught: " <<
e.what() <<
std::endl;
489 MPI_Abort(MPI_COMM_WORLD, -100);
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Inform & endl(Inform &inf)
boost::tuple< std::string, double, double > DVar_t
type of design variables
std::map< std::string, DVar_t > DVarContainer_t
std::pair< std::string, DVar_t > namedDVar_t
std::shared_ptr< CmdArguments > CmdArguments_t
std::map< std::string, client::function::type > functionDictionary_t
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
std::pair< std::string, Expressions::Expr_t * > SingleNamed_t
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
double getReal(const Attribute &attr)
Return real value.
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.
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
bool getBool(const Attribute &attr)
Return logical value.
std::vector< std::string > getStringArray(const Attribute &attr)
Get string array value.
std::string getString(const Attribute &attr)
Get string value.
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
constexpr double e
The value of.
std::string::iterator iterator
int seed
The current random seed.
boost::function< boost::tuple< double, bool >(arguments_t)> type
double FromFile(std::string file, const std::vector< double > &referencePoint)
The base class for all OPAL actions.
The base class for all OPAL objects.
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
std::vector< Attribute > itsAttr
The object attributes.
static void stashInstance()
static OpalData * getInstance()
static OpalData * popInstance()
std::string getVariable() const
double getUpperBound() const
double getLowerBound() const
std::string getExpression() const
Concrete implementation of an Opal simulation wrapper.
static OpalSample * find(const std::string &name)
Find sampling method.
void initialize(const std::string &dvarName, double lower, double upper, size_t modulo=1, bool sequence=false)
unsigned int getSize() const
std::shared_ptr< SamplingMethod > sampleMethod_m
std::string getVariable() const
static void setGlobalSeed(unsigned int seed)
virtual void execute()
Execute the command.
SampleCmd()
Exemplar constructor.
virtual SampleCmd * clone(const std::string &name)
Make clone.
The base class for all OPAL exceptions.
A simple expression to get the n-th peak of a radial probe.
A simple expression to get value from stat file near a specific position (ref_val,...