59 #include <boost/filesystem.hpp>
94 "The \"SAMPLE\" command initiates sampling.") {
96 (
"INPUT",
"Path to input file");
98 (
"OUTPUT",
"Name used in output file sample");
100 (
"OUTDIR",
"Name of directory used to run and store sample output files");
102 (
"OBJECTIVES",
"List of expressions to evaluate and store");
104 (
"STOREOBJECTIVES",
"List of stat variables to store");
106 (
"DVARS",
"List of sampling variables to be used");
108 (
"SAMPLINGS",
"List of sampling methods to be used");
110 (
"NUM_MASTERS",
"Number of master nodes");
112 (
"NUM_COWORKERS",
"Number processors per worker");
114 (
"TEMPLATEDIR",
"Directory where templates are stored");
116 (
"FIELDMAPDIR",
"Directory where field maps are stored");
118 (
"DISTDIR",
"Directory where distributions are stored");
120 (
"RASTER",
"Scan full space given by design variables (default: true)",
true);
122 (
"SEED",
"Seed for global random number generator (default: 42)", 42);
124 (
"KEEP",
"List of files to keep for each simulation. (default: all files kept)");
126 (
"RESTART_FILE",
"H5 file to restart the OPAL simulations from (optional)",
"");
128 (
"RESTART_STEP",
"Restart from given H5 step (optional)",
131 (
"JSON_DUMP_FREQ",
"Defines how often new individuals are appended to the final JSON file, "
132 "i.e. every time JSON_DUMP_FREQ samples finished they are written (optional)",
150 namespace fs = boost::filesystem;
153 opal->setOptimizerFlag();
170 if ( sampling.size() != dvarsstr.size() )
172 "Number of sampling methods != number of design variables.");
174 typedef std::map< std::string, std::shared_ptr<SamplingMethod> > sampleMethods_t;
175 sampleMethods_t sampleMethods;
177 std::map<std::string, std::pair<double, double> > vars;
179 for (std::string &
name : dvarsstr) {
181 DVar* dvar =
dynamic_cast<DVar*
>(obj);
182 if (dvar ==
nullptr) {
184 "The sampling variable " +
name +
" is not known");
191 auto ret = vars.insert(std::make_pair(var, std::make_pair(lowerbound, upperbound)));
192 if (ret.second ==
false) {
194 "There is already a design variable with the variable " + var +
" defined");
197 DVar_t tmp = boost::make_tuple(var, lowerbound, upperbound);
206 funcs.insert(std::pair<std::string, client::function::type>
210 funcs.insert(std::pair<std::string, client::function::type>
214 funcs.insert(std::pair<std::string, client::function::type>
215 (
"sddsVariableAt", ff));
218 funcs.insert(std::pair<std::string, client::function::type>
222 funcs.insert(std::pair<std::string, client::function::type>
223 (
"maxNormRadialPeak", ff));
226 funcs.insert(std::pair<std::string, client::function::type>
227 (
"numberOfPeaks", ff));
230 funcs.insert(std::pair<std::string, client::function::type>
231 (
"sumErrSqRadialPeak", ff));
234 funcs.insert(std::pair<std::string, client::function::type>
235 (
"probVariableWithID", ff));
237 std::string fname = inputfile.stem().native();
239 funcs.insert(std::pair<std::string, client::function::type>
240 (
"statVariableAt", ff));
243 funcs.insert(std::pair<std::string, client::function::type>
248 std::set<std::string> objExpressions;
249 for (std::string
name: objectivesstr) {
252 if (objective ==
nullptr) {
254 "The objective " +
name +
" is not known");
260 auto ret = objExpressions.insert(expr);
261 if (ret.second ==
false) {
263 "There is already a objective with the expression " + expr +
" defined");
271 std::set<std::string> names;
272 for (
size_t i = 0; i < sampling.size(); ++i) {
277 "Sampling method not found.");
282 if ( vars.find(
name) == vars.end() ) {
284 "Variable '" +
name +
"' not a DVAR.");
287 auto ret = names.insert(
name);
288 if (ret.second ==
false) {
290 "There is already a sampling method with the variable " +
name +
" defined");
318 std::vector<std::string> arguments(opal->getArguments());
319 std::vector<char*> argv;
320 std::map<unsigned int, std::string> argumentMapper({
321 {INPUT,
"inputfile"},
324 {NUMMASTERS,
"num-masters"},
325 {NUMCOWORKERS,
"num-coworkers"},
326 {RESTART_FILE,
"restartfile"},
327 {RESTART_STEP,
"restartstep"},
328 {JSON_DUMP_FREQ,
"jsonDumpFreq"}
331 auto it = argumentMapper.end();
332 for (
unsigned int i = 0; i <
SIZE; ++ i) {
333 if ((it = argumentMapper.find(i)) != argumentMapper.end()) {
335 if (
type ==
"string") {
338 arguments.push_back(argument);
340 }
else if (
type ==
"real") {
343 size_t last = val.find_last_not_of(
'0');
344 if (val[last] !=
'.') ++ last;
345 val.erase (last, std::string::npos );
346 std::string argument =
"--" + (*it).second +
"=" + val;
347 arguments.push_back(argument);
349 }
else if (
type ==
"logical") {
352 arguments.push_back(argument);
361 arguments.push_back(
"--nsamples=" + std::to_string(nSample));
365 "The argument INPUT has to be provided");
370 if (dir.is_relative()) {
371 fs::path path = fs::path(std::string(getenv(
"PWD")));
376 if (!fs::exists(dir)) {
377 fs::create_directory(dir);
379 std::string argument =
"--simtmpdir=" + dir.native();
380 arguments.push_back(argument);
385 if (dir.is_relative()) {
386 fs::path path = fs::path(std::string(getenv(
"PWD")));
391 std::string argument =
"--templates=" + dir.native();
392 arguments.push_back(argument);
397 if (dir.is_relative()) {
398 fs::path path = fs::path(std::string(getenv(
"PWD")));
403 setenv(
"FIELDMAPS", dir.c_str(), 1);
408 if (dir.is_relative()) {
409 fs::path path = fs::path(std::string(getenv(
"PWD")));
414 setenv(
"DISTRIBUTIONS", dir.c_str(), 1);
419 size_t pos = tmplFile.find_last_of(
"/");
420 if(pos != std::string::npos)
421 tmplFile = tmplFile.substr(pos+1);
422 pos = tmplFile.find(
".");
423 tmplFile = tmplFile.substr(0,pos);
426 std::ifstream infile(tmplFile.c_str());
428 std::map<std::string, short> dvarCheck;
429 auto itr = dvars.begin();
430 for (; itr != dvars.end(); ++ itr) {
431 dvarCheck.insert(std::make_pair(boost::get<0>(itr->second), 0));
434 while(infile.good()) {
436 std::getline(infile, line,
'\n');
439 for(
auto &check: dvarCheck) {
440 size_t pos = line.find(
"_" + check.first +
"_");
441 if (pos != std::string::npos &&
442 dvarCheck.find(check.first) != dvarCheck.end()) {
443 dvarCheck.at(check.first) = 1;
449 for (
auto itr = dvarCheck.begin(); itr != dvarCheck.end(); ++ itr) {
450 if (itr->second == 0) {
452 "Couldn't find the design variable '" + itr->first +
"' in '" + tmplFile +
"'!");
458 for (
size_t i = 0; i < arguments.size(); ++ i) {
459 argv.push_back(
const_cast<char*
>(arguments[i].c_str()));
460 *
gmsg << arguments[i] <<
" ";
464 std::map<std::string, std::string> userVariables = opal->getVariableData();
471 boost::shared_ptr<Comm_t> comm(
new Comm_t(args, MPI_COMM_WORLD));
472 if (comm->isWorker())
475 if ( comm->isOptimizer() ) {
477 it != sampleMethods.end(); ++it)
479 it->second->allocate(args, comm->getBundle());
483 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm, funcs, dvars,
484 objectives, sampleMethods,
485 storeobjstr, filesToKeep, userVariables));
486 if (comm->isWorker())
490 std::cout <<
"Exception caught: " <<
e.what() <<
std::endl;
491 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)
std::map< std::string, client::function::type > functionDictionary_t
boost::shared_ptr< CmdArguments > CmdArguments_t
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::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.