38 #include <boost/filesystem.hpp>
73 "The \"SAMPLE\" command initiates sampling.") {
75 (
"INPUT",
"Path to input file");
77 (
"OUTPUT",
"Name used in output file sample");
79 (
"OUTDIR",
"Name of directory used to run and store sample output files");
81 (
"OBJECTIVES",
"List of expressions to evaluate and store");
83 (
"STOREOBJECTIVES",
"List of stat variables to store");
85 (
"DVARS",
"List of sampling variables to be used");
87 (
"SAMPLINGS",
"List of sampling methods to be used");
89 (
"NUM_MASTERS",
"Number of master nodes");
91 (
"NUM_COWORKERS",
"Number processors per worker");
93 (
"TEMPLATEDIR",
"Directory where templates are stored");
95 (
"FIELDMAPDIR",
"Directory where field maps are stored");
97 (
"DISTDIR",
"Directory where distributions are stored");
99 (
"RASTER",
"Scan full space given by design variables (default: true)",
true);
101 (
"SEED",
"Seed for global random number generator (default: 42)", 42);
103 (
"KEEP",
"List of files to keep for each simulation. (default: all files kept)");
105 (
"RESTART_FILE",
"H5 file to restart the OPAL simulations from (optional)",
"");
107 (
"RESTART_STEP",
"Restart from given H5 step (optional)",
110 (
"JSON_DUMP_FREQ",
"Defines how often new individuals are appended to the final JSON file, "
111 "i.e. every time JSON_DUMP_FREQ samples finished they are written (optional)",
129 namespace fs = boost::filesystem;
132 opal->setOptimizerFlag();
149 if ( sampling.size() != dvarsstr.size() )
151 "Number of sampling methods != number of design variables.");
153 typedef std::map< std::string, std::shared_ptr<SamplingMethod> > sampleMethods_t;
154 sampleMethods_t sampleMethods;
156 std::map<std::string, std::pair<double, double> > vars;
158 for (std::string &
name : dvarsstr) {
161 DVar* dvar =
dynamic_cast<DVar*
>(obj);
162 if (dvar ==
nullptr) {
164 "The sampling variable " +
name +
" is not known");
171 auto ret = vars.insert(std::make_pair(var, std::make_pair(lowerbound, upperbound)));
172 if (ret.second ==
false) {
174 "There is already a design variable with the variable " + var +
" defined");
177 DVar_t tmp = boost::make_tuple(var, lowerbound, upperbound);
186 funcs.insert(std::pair<std::string, client::function::type>
190 funcs.insert(std::pair<std::string, client::function::type>
194 funcs.insert(std::pair<std::string, client::function::type>
195 (
"sddsVariableAt", ff));
198 funcs.insert(std::pair<std::string, client::function::type>
202 funcs.insert(std::pair<std::string, client::function::type>
203 (
"maxNormRadialPeak", ff));
206 funcs.insert(std::pair<std::string, client::function::type>
207 (
"numberOfPeaks", ff));
210 funcs.insert(std::pair<std::string, client::function::type>
211 (
"sumErrSqRadialPeak", ff));
214 funcs.insert(std::pair<std::string, client::function::type>
215 (
"probVariableWithID", ff));
217 std::string fname = inputfile.stem().native();
219 funcs.insert(std::pair<std::string, client::function::type>
220 (
"statVariableAt", ff));
224 std::set<std::string> objExpressions;
225 for (std::string
name: objectivesstr) {
229 if (objective ==
nullptr) {
231 "The objective " +
name +
" is not known");
237 auto ret = objExpressions.insert(expr);
238 if (ret.second ==
false) {
240 "There is already a objective with the expression " + expr +
" defined");
248 std::set<std::string> names;
249 for (
size_t i = 0; i < sampling.size(); ++i) {
254 "Sampling method not found.");
259 if ( vars.find(name) == vars.end() ) {
261 "Variable '" + name +
"' not a DVAR.");
264 auto ret = names.insert(name);
265 if (ret.second ==
false) {
267 "There is already a sampling method with the variable " + name +
" defined");
295 std::vector<std::string> arguments(opal->getArguments());
296 std::vector<char*> argv;
297 std::map<unsigned int, std::string> argumentMapper({
298 {INPUT,
"inputfile"},
301 {NUMMASTERS,
"num-masters"},
302 {NUMCOWORKERS,
"num-coworkers"},
303 {RESTART_FILE,
"restartfile"},
304 {RESTART_STEP,
"restartstep"},
305 {JSON_DUMP_FREQ,
"jsonDumpFreq"}
308 auto it = argumentMapper.end();
309 for (
unsigned int i = 0; i <
SIZE; ++ i) {
310 if ((it = argumentMapper.find(i)) != argumentMapper.end()) {
312 if (type ==
"string") {
315 arguments.push_back(argument);
317 }
else if (type ==
"real") {
320 size_t last = val.find_last_not_of(
'0');
321 if (val[last] !=
'.') ++ last;
322 val.erase (last, std::string::npos );
323 std::string argument =
"--" + (*it).second +
"=" + val;
324 arguments.push_back(argument);
326 }
else if (type ==
"logical") {
329 arguments.push_back(argument);
338 arguments.push_back(
"--nsamples=" + std::to_string(nSample));
342 "The argument INPUT has to be provided");
347 if (dir.is_relative()) {
348 fs::path path = fs::path(std::string(getenv(
"PWD")));
353 if (!fs::exists(dir)) {
354 fs::create_directory(dir);
356 std::string argument =
"--simtmpdir=" + dir.native();
357 arguments.push_back(argument);
362 if (dir.is_relative()) {
363 fs::path path = fs::path(std::string(getenv(
"PWD")));
368 std::string argument =
"--templates=" + dir.native();
369 arguments.push_back(argument);
374 if (dir.is_relative()) {
375 fs::path path = fs::path(std::string(getenv(
"PWD")));
380 setenv(
"FIELDMAPS", dir.c_str(), 1);
385 if (dir.is_relative()) {
386 fs::path path = fs::path(std::string(getenv(
"PWD")));
391 setenv(
"DISTRIBUTIONS", dir.c_str(), 1);
396 size_t pos = tmplFile.find_last_of(
"/");
397 if(pos != std::string::npos)
398 tmplFile = tmplFile.substr(pos+1);
399 pos = tmplFile.find(
".");
400 tmplFile = tmplFile.substr(0,pos);
403 std::ifstream infile(tmplFile.c_str());
405 std::map<std::string, short> dvarCheck;
406 auto itr = dvars.begin();
407 for (; itr != dvars.end(); ++ itr) {
408 dvarCheck.insert(std::make_pair(boost::get<0>(itr->second), 0));
411 while(infile.good()) {
413 std::getline(infile, line,
'\n');
416 for(
auto &check: dvarCheck) {
417 size_t pos = line.find(
"_" + check.first +
"_");
418 if (pos != std::string::npos &&
419 dvarCheck.find(check.first) != dvarCheck.end()) {
420 dvarCheck.at(check.first) = 1;
426 for (
auto itr = dvarCheck.begin(); itr != dvarCheck.end(); ++ itr) {
427 if (itr->second == 0) {
429 "Couldn't find the design variable '" + itr->first +
"' in '" + tmplFile +
"'!");
435 for (
size_t i = 0; i < arguments.size(); ++ i) {
436 argv.push_back(const_cast<char*>(arguments[i].c_str()));
437 *gmsg << arguments[i] <<
" ";
446 boost::shared_ptr<Comm_t> comm(
new Comm_t(args, MPI_COMM_WORLD));
447 if (comm->isWorker())
450 if ( comm->isOptimizer() ) {
452 it != sampleMethods.end(); ++it)
454 it->second->allocate(args, comm->getBundle());
458 boost::scoped_ptr<pilot_t>
pi(
new pilot_t(args, comm, funcs, dvars,
459 objectives, sampleMethods,
460 storeobjstr, filesToKeep));
461 if (comm->isWorker())
466 MPI_Abort(MPI_COMM_WORLD, -100);
int seed
The current random seed.
virtual const char * what() const
std::string getExpression() const
std::pair< std::string, Expressions::Expr_t * > SingleNamed_t
constexpr double e
The value of .
Concrete implementation of an Opal simulation wrapper.
The base class for all OPAL actions.
std::string getVariable() const
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
The base class for all OPAL exceptions.
virtual void execute()
Execute the command.
boost::shared_ptr< CmdArguments > CmdArguments_t
std::string toUpper(const std::string &str)
Expression to be evaluated in the framework.
unsigned int getSize() const
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
double getLowerBound() const
Role assignment according to strategy (that might use hardware network information).
bool getBool(const Attribute &attr)
Return logical value.
Attribute makeStringArray(const std::string &name, const std::string &help)
Create a string array attribute.
static OpalSample * find(const std::string &name)
Find sampling method.
static OpalData * getInstance()
SampleCmd()
Exemplar constructor.
static void setGlobalSeed(unsigned int seed)
double getUpperBound() const
static void stashInstance()
std::vector< std::string > getStringArray(const Attribute &attr)
Get string array value.
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Parsing command line arguments.
std::string getVariable() const
std::shared_ptr< SamplingMethod > sampleMethod_m
boost::tuple< std::string, double, double > DVar_t
type of design variables
std::pair< std::string, DVar_t > namedDVar_t
std::map< std::string, DVar_t > DVarContainer_t
virtual SampleCmd * clone(const std::string &name)
Make clone.
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
void initialize(const std::string &dvarName, double lower, double upper, size_t modulo=1, bool sequence=false)
static OpalData * popInstance()
The base class for all OPAL objects.
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
boost::function< boost::tuple< double, bool >arguments_t)> type
The sample Pilot (Master): Coordinates requests by sampler to workers.
double FromFile(std::string file, const std::vector< double > &referencePoint)
Modeling social graph (Cartesian neighbors plus additional random link) as underlaying master network...
std::string::iterator iterator
double getReal(const Attribute &attr)
Return real value.
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
std::map< std::string, client::function::type > functionDictionary_t
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Inform & endl(Inform &inf)
std::string getString(const Attribute &attr)
Get string value.