27#include "boost/algorithm/string.hpp"
39 std::map<std::string, std::string> uvars)
41 , objectives_(objectives)
42 , constraints_(constraints)
46 namespace fs = std::filesystem;
48 simTmpDir_ = args->getArg<std::string>(
"simtmpdir");
50 if(getenv(
"SIMTMPDIR") ==
nullptr) {
51 std::cout <<
"Environment variable SIMTMPDIR not defined!"
61 std::vector<std::string> dict;
62 for(
auto parameter : params) {
63 std::ostringstream tmp;
65 tmp << parameter.first <<
"=" << parameter.second;
67 dict.push_back(tmp.str());
69 std::ostringstream value;
71 value << parameter.second;
73 std::pair<std::string, std::string>(parameter.first, value.str()));
83 MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
85 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
87 unsigned num_coworkers_worker_ = 0;
88 num_coworkers_worker_ = args->getArg<
size_t>(
"num-coworkers");
90 unsigned group_start = 0;
92 unsigned worker_group = ((my_rank % world_size) - 2) / num_coworkers_worker_;
94 unsigned leader_ = group_start + 2 + worker_group * num_coworkers_worker_;
95 leader_ = leader_ % world_size;
101 std::ostringstream tmp;
104 tmp <<
simTmpDir_ <<
"/" << hash <<
"_" << leader_;
108 std::string tmplDir = args->getArg<std::string>(
"templates");
109 if (tmplDir.empty()) {
110 if(getenv(
"TEMPLATES") ==
nullptr) {
112 "Environment variable TEMPLATES not defined!");
114 tmplDir = getenv(
"TEMPLATES");
120 if (!fs::exists(tmplFile))
122 "The template file '" + tmplFile +
"' doesn't exit");
125 uvars[uvar.first] = uvar.second;
140 struct stat fileInfo;
142 if(stat(infile.c_str(), &fileInfo) == 0) {
143 std::cout <<
"-> Simulation input file (" << infile
144 <<
") already exist from previous run.." << std::endl;
153 namespace fs = std::filesystem;
155 for (
auto &p: fs::directory_iterator(path)) {
156 fs::path source = p.path();
158 target +=source.filename();
161 fs::create_symlink(source, target);
162 }
catch (fs::filesystem_error &e) {
163 std::cerr << e.what() <<
"\n"
164 <<
"in OpalSimulation::createSymlink()" << std::endl;
172 std::string restartfile = args->getArg<std::string>(
"restartfile",
"",
false);
174 if (restartfile.empty())
return;
176 namespace fs = std::filesystem;
177 if ( !fs::exists(restartfile) ) {
178 std::cerr <<
"H5 file '" + restartfile +
"' doesn't exist." <<
"\n"
179 <<
"in OpalSimulation::copyH5_m()" << std::endl;
185 fs::path srcfile(restartfile);
187 fs::copy_file(srcfile, targetfile);
188 }
catch (fs::filesystem_error &ex) {
189 std::cerr << ex.what() <<
"\n"
190 <<
"in OpalSimulation::copyH5_m()" << std::endl;
196 namespace fs = std::filesystem;
199 std::string restartfile = args->getArg<std::string>(
"restartfile",
"",
false);
202 std::ostringstream tmp;
212 if (getenv(
"FIELDMAPS") ==
nullptr) {
214 "Environment variable FIELDMAPS not defined!");
226 if (!fs::exists(dataDir)) {
228 "Directory '" + dataDir +
"' doesn't exist");
231 if (!restartfile.empty() &&
239 namespace fs = std::filesystem;
242 MPI_Comm_rank(
comm_, &rank);
243 if (rank != 0)
return;
252 fs::perms::owner_all |
253 fs::perms::group_read |
254 fs::perms::group_exec |
255 fs::perms::others_read |
256 fs::perms::others_exec);
258 }
catch (fs::filesystem_error &e) {
259 std::cerr << e.what() <<
"\n"
260 <<
"in OpalSimulation::setupSimulation" << std::endl;
267 fs::create_directory(dataDir);
268 fs::permissions(dataDir,
269 fs::perms::owner_all |
270 fs::perms::group_read |
271 fs::perms::group_exec |
272 fs::perms::others_read |
273 fs::perms::others_exec);
275 }
catch (fs::filesystem_error &e) {
276 std::cerr << e.what() <<
"\n"
277 <<
"in OpalSimulation::setupSimulation" << std::endl;
283 gs_->writeInputFile(infile);
285 std::string fieldmapPath = getenv(
"FIELDMAPS");
288 if (getenv(
"DISTRIBUTIONS") !=
nullptr) {
289 std::string distPath = getenv(
"DISTRIBUTIONS");
303 MPI_Comm_rank(MPI_COMM_WORLD, &world_pid);
305 std::ostringstream fname;
306 fname <<
"sim.out." << world_pid;
307 std::ofstream file(fname.str().c_str());
309 std::ofstream err(fname.str().c_str());
312 std::cout.rdbuf(file.rdbuf());
313 std::cerr.rdbuf(err.rdbuf());
324 namespace fs = std::filesystem;
333 pwd_ = fs::current_path().native();
338 std::cout <<
"Cannot chdir to "
340 std::cout <<
"Continuing 1, disregarding this simulation.."
346 std::ostringstream inputFileName;
348 char *inputfile =
new char[inputFileName.str().size()+1] ;
349 strcpy(inputfile, inputFileName.str().c_str());
353 int restartStep= args->getArg<
int>(
"restartstep",
354 std::numeric_limits<int>::min(),
false);
355 std::string restartfile = args->getArg<std::string>(
"restartfile",
"",
false);
358 if ( restartStep > -2 && restartfile.empty() ) {
360 "Restart specified but no restart H5 file available.");
363 char exe_name[] =
"opal";
364 char nocomm[] =
"--nocomminit";
365 char info[] =
"--info";
367 char warn[] =
"--warn";
369 char *
arg[] = { exe_name, inputfile, nocomm, info, info0, warn, warn0 };
375 std::cout.setstate(std::ios::failbit);
392 std::cerr <<
"Opal exception during simulation run: \n"
393 << ex->
where() <<
"\n"
394 << ex->
what() << std::endl;
395 std::cerr <<
"Continuing, disregarding this simulation.."
405 std::cerr <<
"Classic exception during simulation run: \n"
406 << ex->
where() <<
"\n"
407 << ex->
what() << std::endl;
408 std::cerr <<
"Continuing, disregarding this simulation.."
410 }
catch(std::exception &ex) {
414 std::cerr <<
"Exception occured during simulation run: \n"
415 << ex.
what() << std::endl
416 <<
"Continuing, disregarding this simulation.." << std::endl;
421 std::cerr <<
"Unknown exception occured during simulation run.\n"
422 <<
"Continuing, disregarding this simulation.." << std::endl;
429 err = chdir(
pwd_.c_str());
431 std::cerr <<
"Cannot chdir to "
432 <<
pwd_ << std::endl;
438 std::map<std::string, std::vector<double> > ret;
441 for (
const std::string &var : statVariables) {
446 std::cout <<
"failed to read data: " << e.what() <<
" in " << e.where() << std::endl;
450 std::vector<double> values;
451 values.reserve(column.size());
453 for (
const auto& val: column) {
456 ret.insert(std::make_pair(var, values));
471 std::cout <<
"Cannot chdir to "
473 std::cout <<
"Continuing, with cleanup.."
480 struct stat fileInfo;
483 if(stat(fn.c_str(), &fileInfo) != 0) {
486 Expressions::Named_t::iterator namedIt;
489 if (namedIt->first ==
"dummy")
continue;
499 objective->
evaluate(variable_dictionary);
501 std::vector<double> values;
502 values.push_back(std::get<0>(result));
503 bool is_valid = std::get<1>(result);
507 std::pair<std::string, reqVarInfo_t>(namedIt->first, tmps));
521 constraint->
evaluate(variable_dictionary);
523 std::vector<double> values;
524 values.push_back(std::get<0>(result));
525 bool is_valid = std::get<1>(result);
528 std::string constr_str = constraint->
toString();
529 std::vector<std::string> split;
530 boost::split(split, constr_str, boost::is_any_of(
"<>!="),
531 boost::token_compress_on);
532 std::string lhs_constr_str = split[0];
533 std::string rhs_constr_str = split[1];
534 boost::trim_left_if(rhs_constr_str, boost::is_any_of(
"="));
537 const std::unique_ptr<Expressions::Expr_t> lhs(
539 const std::unique_ptr<Expressions::Expr_t> rhs(
545 values.push_back(std::get<0>(lhs_res));
546 values.push_back(std::get<0>(rhs_res));
550 std::pair<std::string, reqVarInfo_t>(namedIt->first, tmps));
554 std::cout <<
"Evaluation of objective or constraint " << namedIt->first <<
" threw an exception ('" << e.what() <<
"' in " << e.where() <<
")!" << std::endl;
557 std::cout <<
"Evaluation of objective or constraint " << namedIt->first <<
" threw an exception ('" << e.what() <<
"' in " << e.where() <<
")!" << std::endl;
559 }
catch(std::exception &e) {
560 std::cout <<
"Evaluation of objective or constraint " << namedIt->first <<
" threw an exception ('" << e.what() <<
"')!" << std::endl;
563 std::cout <<
"Evaluation of objective or constraint " << namedIt->first <<
" threw an exception!" << std::endl;
569 err = chdir(
pwd_.c_str());
571 std::cout <<
"Cannot chdir to "
577 const std::string& filename,
580 std::set<std::string> req_vars = expression->
getReqVars();
583 for (
auto req_it = req_vars.begin(); req_it!=req_vars.end();) {
589 double value = std::stod((*it).second);
590 dictionary.insert(std::pair<std::string, double>(*req_it, value));
591 req_it = req_vars.erase(req_it);
594 if(req_vars.empty())
return;
597 const std::unique_ptr<SDDSReader> sddsr(
new SDDSReader(filename));
600 for(std::string req_var : req_vars) {
601 if(dictionary.count(req_var) != 0)
continue;
604 sddsr->getValue(-1 , req_var, value);
605 dictionary.insert(std::pair<std::string, double>(req_var, value));
612 std::vector<double> tmp_values;
613 tmp_values.push_back(0.0);
616 std::pair<std::string, reqVarInfo_t>(namedObjective.first, tmps));
621 namespace fs = std::filesystem;
624 MPI_Comm_rank(
comm_, &my_rank);
629 }
catch(fs::filesystem_error &ex) {
630 std::cout <<
"Can't remove directory '" <<
simulationDirName_ <<
"', (" << ex.what() <<
")" << std::endl;
637 namespace fs = std::filesystem;
639 if ( keep.empty() ) {
646 MPI_Comm_rank(
comm_, &my_rank);
652 fs::directory_iterator it{p};
653 while (it != fs::directory_iterator{}) {
654 std::string extension =
Util::toUpper(it->path().extension().string());
657 extension.erase(0, 1);
659 auto result = std::find(keep.begin(), keep.end(), extension);
661 if ( result == keep.end() && ! fs::is_directory(it->path())) {
662 fs::remove(it->path());
668 fs::directory_iterator it{p};
669 while (it != fs::directory_iterator{}) {
670 if (fs::is_directory(it->path()) && fs::is_empty(it->path())) {
671 fs::remove(it->path());
676 }
catch(fs::filesystem_error &ex) {
678 <<
"', (" << ex.what() <<
")" << std::endl;
int run_opal(char *[], std::string inputfile, int restartStep, int infoLevel, int warnLevel, MPI_Comm comm)
std::shared_ptr< CmdArguments > CmdArguments_t
namedVariableCollection_t Param_t
std::map< std::string, client::function::type > functionDictionary_t
std::map< std::string, double > variableDictionary_t
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
std::tuple< double, bool > Result_t
int seed
The current random seed.
std::string toUpper(const std::string &str)
std::vector< variant_t > columnData_t
The global OPAL structure.
static OpalData * getInstance()
The abstract base class for all exceptions in CLASSIC.
std::string pwd_
holds current directory (for restoring)
void invalidBunch()
mark a solution as invalid
virtual ~OpalSimulation()
std::map< std::string, std::vector< double > > getData(const std::vector< std::string > &statVariables)
void getVariableDictionary(variableDictionary_t &dictionary, const std::string &filename, const Expressions::Expr_t *const expression)
get variables for expression evaluation from SDDS file. Can throw SDDSParserException
reqVarContainer_t requestedVars_
holds solutions returned to the optimizer
void setupSimulation()
create directories, input files, fieldmaps...
void copyH5_m()
copy H5 file
void redirectOutToFile()
redirect stdout and stderr to file
std::unique_ptr< GenerateOpalSimulation > gs_
object to generate simulation input files
Expressions::Named_t objectives_
std::streambuf * strm_err_
stream buffer to redirect stderr
std::string simulationDirName_
full path of simulation directory (where simulation will be run)
std::set< std::string > dvarNames_
Expressions::Named_t constraints_
std::map< std::string, std::string > userVariables_
variable dictionary holding requested optimizer values
std::string simTmpDir_
temporary directory for simulation data (environment var SIMTMPDIR)
std::streambuf * strm_buffer_
stream buffer to redirect output
void createSymlink_m(const std::string &path)
create symbolic links
std::string simulationName_
identification of the simulation (corresponding to output filename)
bool hasResultsAvailable()
check if we already have simulated the current set of design vars
void setupFSStructure()
create directories, input files, symlinks...
void collectResults()
Parse SDDS stat file and build up requested variable dictionary.
void restoreOut()
restore stdout and stderr to default
int id_m
job id (SAMPLE command)
OpalSimulation(Expressions::Named_t objectives, Expressions::Named_t constraints, Param_t params, std::string name, MPI_Comm comm, CmdArguments_t args, std::map< std::string, std::string > uvars)
The base class for all OPAL exceptions.
virtual const std::string & what() const
Return the message string for the exception.
virtual const std::string & where() const
Return the name of the method or function which detected the exception.
functionDictionary_t getRegFuncs() const
const std::string & toString() const
Expressions::Result_t evaluate(const variableDictionary_t &vars)
evaluate an expression given a value dictionary of free variables
const std::set< std::string > & getReqVars() const
static std::string generate(std::vector< std::string > arguments, size_t world_pid=0)
ast::columnData_t getColumnData(const std::string &columnName)
ast::datatype getColumnType(const std::string &col_name)
T getBoostVariantValue(const ast::variant_t &val, int datatype) const
Convert value from boost variant (only numeric types) to a value of type T.