27 #include "boost/variant.hpp"
28 #include "boost/smart_ptr.hpp"
29 #include "boost/algorithm/string.hpp"
31 #include "boost/filesystem.hpp"
32 #include "boost/filesystem/operations.hpp"
45 , objectives_(objectives)
46 , constraints_(constraints)
50 namespace fs = boost::filesystem;
52 simTmpDir_ = args->getArg<std::string>(
"simtmpdir");
54 if(getenv(
"SIMTMPDIR") == NULL) {
55 std::cout <<
"Environment variable SIMTMPDIR not defined!"
65 std::vector<std::string> dict;
66 for(
auto parameter : params) {
67 std::ostringstream tmp;
69 tmp << parameter.first <<
"=" << parameter.second;
71 dict.push_back(tmp.str());
73 std::ostringstream value;
75 value << parameter.second;
77 std::pair<std::string, std::string>(parameter.first, value.str()));
87 MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
89 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
91 unsigned num_coworkers_worker_ = 0;
92 num_coworkers_worker_ = args->getArg<
size_t>(
"num-coworkers");
94 unsigned group_start = 0;
96 unsigned worker_group = ((my_rank % world_size) - 2) / num_coworkers_worker_;
98 unsigned leader_ = group_start + 2 + worker_group * num_coworkers_worker_;
99 leader_ = leader_ % world_size;
105 std::ostringstream tmp;
108 tmp <<
simTmpDir_ <<
"/" << hash <<
"_" << leader_;
112 std::string tmplDir = args->getArg<std::string>(
"templates");
114 if(getenv(
"TEMPLATES") == NULL) {
116 "Environment variable TEMPLATES not defined!");
118 tmplDir = getenv(
"TEMPLATES");
122 std::string dataFile = simulationName_ +
".data";
123 fs::path pwd = fs::current_path();
124 if (!fs::exists(dataFile))
126 "The data file '" + dataFile +
"' \n" +
127 " doesn't exist in directory '" + pwd.native() +
"'");
129 if (!fs::exists(tmplFile))
131 "The template file '" + tmplFile +
"' doesn't exit");
145 struct stat fileInfo;
147 if(stat(infile.c_str(), &fileInfo) == 0) {
148 std::cout <<
"-> Simulation input file (" << infile
149 <<
") already exist from previous run.." <<
std::endl;
158 namespace fs = boost::filesystem;
160 for (
auto &p: fs::directory_iterator(path)) {
161 fs::path source = p.path();
163 target +=source.filename();
166 fs::create_symlink(source, target);
167 }
catch (fs::filesystem_error &
e) {
168 std::cerr << e.what() <<
"\n"
169 <<
"in OpalSimulation::createSymlink()" <<
std::endl;
177 std::string restartfile = args->getArg<std::string>(
"restartfile",
"",
false);
179 if (restartfile.empty())
return;
181 namespace fs = boost::filesystem;
182 if ( !fs::exists(restartfile) ) {
183 std::cerr <<
"H5 file '" + restartfile +
"' doesn't exist." <<
"\n"
184 <<
"in OpalSimulation::copyH5_m()" <<
std::endl;
190 fs::path srcfile(restartfile);
192 fs::copy_file(srcfile, targetfile);
193 }
catch (fs::filesystem_error &ex) {
194 std::cerr << ex.what() <<
"\n"
195 <<
"in OpalSimulation::copyH5_m()" <<
std::endl;
201 namespace fs = boost::filesystem;
204 std::string restartfile = args->getArg<std::string>(
"restartfile",
"",
false);
207 std::ostringstream tmp;
217 if (getenv(
"FIELDMAPS") == NULL) {
219 "Environment variable FIELDMAPS not defined!");
231 if (!fs::exists(dataDir)) {
233 "Directory '" + dataDir +
"' doesn't exist");
236 if (!restartfile.empty() &&
244 namespace fs = boost::filesystem;
247 MPI_Comm_rank(
comm_, &rank);
248 if (rank != 0)
return;
263 }
catch (fs::filesystem_error &
e) {
264 std::cerr << e.what() <<
"\n"
265 <<
"in OpalSimulation::setupSimulation" <<
std::endl;
272 fs::create_directory(dataDir);
273 fs::permissions(dataDir,
280 }
catch (fs::filesystem_error &e) {
281 std::cerr << e.what() <<
"\n"
282 <<
"in OpalSimulation::setupSimulation" <<
std::endl;
288 gs_->writeInputFile(infile);
290 std::string fieldmapPath = getenv(
"FIELDMAPS");
293 if (getenv(
"DISTRIBUTIONS") != NULL) {
294 std::string distPath = getenv(
"DISTRIBUTIONS");
308 MPI_Comm_rank(MPI_COMM_WORLD, &world_pid);
310 std::ostringstream fname;
311 fname <<
"sim.out." << world_pid;
312 std::ofstream file(fname.str().c_str());
314 std::ofstream err(fname.str().c_str());
317 std::cout.rdbuf(file.rdbuf());
318 std::cerr.rdbuf(err.rdbuf());
329 namespace fs = boost::filesystem;
338 pwd_ = fs::current_path().native();
343 std::cout <<
"Cannot chdir to "
345 std::cout <<
"Continuing 1, disregarding this simulation.."
351 std::ostringstream inputFileName;
353 char *inputfile =
new char[inputFileName.str().size()+1] ;
354 strcpy(inputfile, inputFileName.str().c_str());
358 int restartStep= args->getArg<
int>(
"restartstep",
360 std::string restartfile = args->getArg<std::string>(
"restartfile",
"",
false);
363 if ( restartStep > -2 && restartfile.empty() ) {
365 "Restart specified but no restart H5 file available.");
368 char exe_name[] =
"opal";
369 char nocomm[] =
"--nocomminit";
370 char info[] =
"--info";
372 char warn[] =
"--warn";
374 char *
arg[] = { exe_name, inputfile, nocomm,
info, info0,
warn, warn0 };
378 #ifdef SUPRESS_OUTPUT
380 std::cout.setstate(std::ios::failbit);
387 #ifdef SUPRESS_OUTPUT
395 #ifdef SUPRESS_OUTPUT
400 std::cout <<
"Opal exception during simulation run: \n"
401 << ex->
where() <<
"\n"
403 std::cout <<
"Continuing 2, disregarding this simulation.."
409 #ifdef SUPRESS_OUTPUT
414 std::cout <<
"Classic exception during simulation run: \n"
415 << ex->
where() <<
"\n"
417 std::cout <<
"Continuing 3, disregarding this simulation.."
425 err = chdir(
pwd_.c_str());
427 std::cout <<
"Cannot chdir to "
434 std::map<std::string, std::vector<double> > ret;
437 for (
const std::string &var : statVariables) {
446 std::vector<double> values;
447 values.reserve(column.size());
449 for (
const auto val: column) {
452 ret.insert(std::make_pair(var, values));
467 std::cout <<
"Cannot chdir to "
469 std::cout <<
"Continuing, with cleanup.."
476 struct stat fileInfo;
479 if(stat(fn.c_str(), &fileInfo) != 0) {
485 if (namedIt->first ==
"dummy")
continue;
495 objective->
evaluate(variable_dictionary);
497 std::vector<double> values;
498 values.push_back(boost::get<0>(result));
499 bool is_valid = boost::get<1>(result);
503 std::pair<std::string, reqVarInfo_t>(namedIt->first, tmps));
517 constraint->
evaluate(variable_dictionary);
519 std::vector<double> values;
520 values.push_back(boost::get<0>(result));
521 bool is_valid = boost::get<1>(result);
524 std::string constr_str = constraint->
toString();
525 std::vector<std::string> split;
526 boost::split(split, constr_str, boost::is_any_of(
"<>!="),
527 boost::token_compress_on);
528 std::string lhs_constr_str = split[0];
529 std::string rhs_constr_str = split[1];
530 boost::trim_left_if(rhs_constr_str, boost::is_any_of(
"="));
533 boost::scoped_ptr<Expressions::Expr_t> lhs(
535 boost::scoped_ptr<Expressions::Expr_t> rhs(
541 values.push_back(boost::get<0>(lhs_res));
542 values.push_back(boost::get<0>(rhs_res));
546 std::pair<std::string, reqVarInfo_t>(namedIt->first, tmps));
550 std::cout <<
"Evaluation of objective or constraint " << namedIt->first <<
" threw an exception ('" << e.
what() <<
"' in " << e.
where() <<
")!" <<
std::endl;
553 std::cout <<
"Evaluation of objective or constraint " << namedIt->first <<
" threw an exception ('" << e.
what() <<
"' in " << e.
where() <<
")!" <<
std::endl;
555 }
catch(std::exception &e) {
556 std::cout <<
"Evaluation of objective or constraint " << namedIt->first <<
" threw an exception ('" << e.what() <<
"')!" <<
std::endl;
559 std::cout <<
"Evaluation of objective or constraint " << namedIt->first <<
" threw an exception!" <<
std::endl;
565 err = chdir(
pwd_.c_str());
567 std::cout <<
"Cannot chdir to "
573 const std::string& filename,
576 std::set<std::string> req_vars = expression->
getReqVars();
579 for (
auto req_it = req_vars.begin(); req_it!=req_vars.end();) {
585 double value = std::stod((*it).second);
586 dictionary.insert(std::pair<std::string, double>(*req_it, value));
587 req_it = req_vars.erase(req_it);
590 if(req_vars.empty())
return;
593 boost::scoped_ptr<SDDSReader> sddsr(
new SDDSReader(filename));
596 for(std::string req_var : req_vars) {
597 if(dictionary.count(req_var) != 0)
continue;
600 sddsr->getValue(-1 , req_var, value);
601 dictionary.insert(std::pair<std::string, double>(req_var, value));
608 std::vector<double> tmp_values;
609 tmp_values.push_back(0.0);
612 std::pair<std::string, reqVarInfo_t>(namedObjective.first, tmps));
617 namespace fs = boost::filesystem;
620 MPI_Comm_rank(
comm_, &my_rank);
625 }
catch(fs::filesystem_error &ex) {
633 namespace fs = boost::filesystem;
635 if ( keep.empty() ) {
642 MPI_Comm_rank(
comm_, &my_rank);
645 fs::directory_iterator it{p};
646 while ( it != fs::directory_iterator{} ) {
647 std::string extension =
Util::toUpper(fs::extension(it->path().filename()));
650 extension.erase(0, 1);
652 auto result =
std::find(keep.begin(), keep.end(), extension);
654 if ( result == keep.end() ) {
655 fs::remove(it->path());
660 }
catch(fs::filesystem_error &ex) {
662 <<
"', (" << ex.what() <<
")" <<
std::endl;
int seed
The current random seed.
The global OPAL structure.
virtual const char * what() const
Expressions::Named_t constraints_
int run_opal(char *arg[], std::string inputfile, int restartStep, int infoLevel, int warnLevel, MPI_Comm comm)
constexpr double e
The value of .
void setupFSStructure()
create directories, input files, symlinks...
std::string toString() const
reqVarContainer_t requestedVars_
holds solutions returned to the optimizer
virtual ~OpalSimulation()
The base class for all OPAL exceptions.
boost::shared_ptr< CmdArguments > CmdArguments_t
std::string pwd_
holds current directory (for restoring)
std::string toUpper(const std::string &str)
Expression to be evaluated in the framework.
std::string simTmpDir_
temporary directory for simulation data (environment var SIMTMPDIR)
Expressions::Named_t objectives_
ast::columnData_t getColumnData(const std::string &columnName)
std::string simulationDirName_
full path of simulation directory (where simulation will be run)
void restoreOut()
restore stdout and stderr to default
virtual const char * where() const
std::streambuf * strm_err_
stream buffer to redirect stderr
void copyH5_m()
copy H5 file
The abstract base class for all exceptions in CLASSIC.
Generates an OPAL input file from data and template file.
Abstract class every simulation has to implement to be able to work with the optimization pilot...
static OpalData * getInstance()
boost::tuple< double, bool > Result_t
std::set< std::string > getReqVars() const
namedVariableCollection_t Param_t
bool hasResultsAvailable()
check if we already have simulated the current set of design vars
virtual const std::string & what() const
Return the message string for the exception.
Implements a parser and value extractor for Probe loss files.
std::vector< variant_t > columnData_t
void invalidBunch()
mark a solution as invalid
OpalSimulation(Expressions::Named_t objectives, Expressions::Named_t constraints, Param_t params, std::string name, MPI_Comm comm, CmdArguments_t args)
const T * find(const T table[], const std::string &name)
Look up name.
void setupSimulation()
create directories, input files, fieldmaps...
std::map< std::string, Expressions::Expr_t * > Named_t
type of an expressions with a name
Expressions::Result_t evaluate(variableDictionary_t vars)
evaluate an expression given a value dictionary of free variables
std::map< std::string, std::vector< double > > getData(const std::vector< std::string > &statVariables)
void createSymlink_m(const std::string &path)
create symbolic links
T getBoostVariantValue(const ast::variant_t &val, int datatype) const
Convert value from boost variant (only numeric types) to a value of type T.
std::streambuf * strm_buffer_
stream buffer to redirect output
functionDictionary_t getRegFuncs() const
int id_m
job id (SAMPLE command)
boost::scoped_ptr< GenerateOpalSimulation > gs_
object to generate simulation input files
static std::string generate(std::vector< std::string > arguments, size_t world_pid=0)
virtual const char * what() const
std::string::iterator iterator
std::set< std::string > dvarNames_
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 ...
void redirectOutToFile()
redirect stdout and stderr to file
ast::datatype getColumnType(const std::string &col_name)
std::map< std::string, double > variableDictionary_t
std::map< std::string, std::string > userVariables_
variable dictionary holding requested optimizer values
std::string simulationName_
identification of the simulation (corresponding to output filename)
std::map< std::string, client::function::type > functionDictionary_t
void collectResults()
Parse SDDS stat file and build up requested variable dictionary.
virtual const char * where() const
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Inform & endl(Inform &inf)
virtual const std::string & where() const
Return the name of the method or function which detected the exception.