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");
267 unsigned int nSample = std::numeric_limits<unsigned int>::max();
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");
300 nSample = std::min(nSample, s->
getSize());
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() ) {
474 for (sampleMethods_t::iterator it = sampleMethods.begin();
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);