OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
OpalSimulation Class Reference

Concrete implementation of an Opal simulation wrapper. More...

#include <OpalSimulation.h>

Inheritance diagram for OpalSimulation:
Collaboration diagram for OpalSimulation:

Public Member Functions

 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)
virtual ~OpalSimulation ()
void run ()
std::map< std::string, std::vector< double > > getData (const std::vector< std::string > &statVariables)
void collectResults ()
 Parse SDDS stat file and build up requested variable dictionary.
void cleanUp ()
void cleanUp (const std::vector< std::string > &keep)
 remove temporary files except to the ones in 'keep'
reqVarContainer_t getResults ()
 returns container containing all requested variables with results
void setFilename (int id)
 set job id (SAMPLE command)
CmdArguments_t getArgs ()

Private Member Functions

void invalidBunch ()
 mark a solution as invalid
bool hasResultsAvailable ()
 check if we already have simulated the current set of design vars
void createSymlink_m (const std::string &path)
 create symbolic links
void copyH5_m ()
 copy H5 file
void setupSimulation ()
 create directories, input files, fieldmaps...
void setupFSStructure ()
 create directories, input files, symlinks...
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
void restoreOut ()
 restore stdout and stderr to default

Private Attributes

std::string simulationName_
 identification of the simulation (corresponding to output filename)
std::string simulationDirName_
 full path of simulation directory (where simulation will be run)
std::string simTmpDir_
 temporary directory for simulation data (environment var SIMTMPDIR)
std::string pwd_
 holds current directory (for restoring)
std::streambuf * strm_buffer_
 stream buffer to redirect output
std::streambuf * strm_err_
 stream buffer to redirect stderr
std::map< std::string, std::string > userVariables_
 variable dictionary holding requested optimizer values
reqVarContainer_t requestedVars_
 holds solutions returned to the optimizer
Expressions::Named_t objectives_
Expressions::Named_t constraints_
std::set< std::string > dvarNames_
MPI_Comm comm_
std::unique_ptr< GenerateOpalSimulationgs_
 object to generate simulation input files
int id_m
 job id (SAMPLE command)
CmdArguments_t args_

Detailed Description

Concrete implementation of an Opal simulation wrapper.

See also
http://amas.web.psi.ch

In order to work properly the user must ensure that the environment variables

  • SIMTMPDIR (temporary directory for simulation input data) and

  • TEMPLATES (directory containing tmpl file)

are specified correctly.

Definition at line 30 of file OpalSimulation.h.

Constructor & Destructor Documentation

◆ OpalSimulation()

OpalSimulation::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 )

Setup OPAL run.

Parameters
[in]objectivesof the optimization problem
[in]constraintsof the optimization problem
[in]params
[in]nameof the simulation
[in]commMPI communicator used for running the simulation
[in]argscommand line arguments passed to the framework
[in]uvarsdefined in the input file (e.g. REAL a = 1.0)

Definition at line 37 of file OpalSimulation.cpp.

References comm_, constraints_, CmdArguments::getArg(), id_m, name, objectives_, simTmpDir_, and Simulation::Simulation().

Here is the call graph for this function:

◆ ~OpalSimulation()

OpalSimulation::~OpalSimulation ( )
virtual

Definition at line 134 of file OpalSimulation.cpp.

References requestedVars_, and userVariables_.

Member Function Documentation

◆ cleanUp() [1/2]

void OpalSimulation::cleanUp ( )

remove temporary simulation files (if Boost filesystem library is not available, this call do nothing).

Definition at line 622 of file OpalSimulation.cpp.

References comm_, and simulationDirName_.

Referenced by collectResults().

◆ cleanUp() [2/2]

void OpalSimulation::cleanUp ( const std::vector< std::string > & keep)

remove temporary files except to the ones in 'keep'

Definition at line 638 of file OpalSimulation.cpp.

References comm_, simulationDirName_, and Util::toUpper().

Here is the call graph for this function:

◆ collectResults()

void OpalSimulation::collectResults ( )
virtual

Parse SDDS stat file and build up requested variable dictionary.

Implements Simulation.

Definition at line 464 of file OpalSimulation.cpp.

References cleanUp(), constraints_, EVALUATE, Expression::evaluate(), Expression::getRegFuncs(), getVariableDictionary(), invalidBunch(), objectives_, pwd_, requestedVars_, simulationDirName_, simulationName_, and Expression::toString().

Here is the call graph for this function:

◆ copyH5_m()

void OpalSimulation::copyH5_m ( )
private

copy H5 file

Definition at line 172 of file OpalSimulation.cpp.

References CmdArguments::getArg(), Simulation::getArgs(), simulationDirName_, and simulationName_.

Referenced by setupFSStructure().

Here is the call graph for this function:

◆ createSymlink_m()

void OpalSimulation::createSymlink_m ( const std::string & path)
private

create symbolic links

Definition at line 154 of file OpalSimulation.cpp.

References simulationDirName_.

Referenced by setupFSStructure().

◆ getArgs()

CmdArguments_t Simulation::getArgs ( )
inlineinherited

Definition at line 48 of file Simulation.h.

References args_.

Referenced by OpalSimulation::copyH5_m(), OpalSimulation::run(), and OpalSimulation::setupSimulation().

◆ getData()

std::map< std::string, std::vector< double > > OpalSimulation::getData ( const std::vector< std::string > & statVariables)

Definition at line 439 of file OpalSimulation.cpp.

References SDDS::SDDSParser::getBoostVariantValue(), SDDS::SDDSParser::getColumnData(), SDDS::SDDSParser::getColumnType(), SDDS::SDDSParser::run(), simulationDirName_, and simulationName_.

Here is the call graph for this function:

◆ getResults()

reqVarContainer_t OpalSimulation::getResults ( )
inlinevirtual

returns container containing all requested variables with results

Implements Simulation.

Definition at line 70 of file OpalSimulation.h.

References requestedVars_.

◆ getVariableDictionary()

void OpalSimulation::getVariableDictionary ( variableDictionary_t & dictionary,
const std::string & filename,
const Expressions::Expr_t *const expression )
private

get variables for expression evaluation from SDDS file. Can throw SDDSParserException

Definition at line 578 of file OpalSimulation.cpp.

References Expression::getReqVars(), and userVariables_.

Referenced by collectResults().

Here is the call graph for this function:

◆ hasResultsAvailable()

bool OpalSimulation::hasResultsAvailable ( )
private

check if we already have simulated the current set of design vars

Definition at line 139 of file OpalSimulation.cpp.

References simulationDirName_, and simulationName_.

Referenced by run().

◆ invalidBunch()

void OpalSimulation::invalidBunch ( )
private

mark a solution as invalid

Definition at line 611 of file OpalSimulation.cpp.

References EVALUATE, objectives_, and requestedVars_.

Referenced by collectResults().

◆ redirectOutToFile()

void OpalSimulation::redirectOutToFile ( )
private

redirect stdout and stderr to file

Definition at line 298 of file OpalSimulation.cpp.

References strm_buffer_, and strm_err_.

◆ restoreOut()

void OpalSimulation::restoreOut ( )
private

restore stdout and stderr to default

Definition at line 319 of file OpalSimulation.cpp.

References strm_buffer_, and strm_err_.

◆ run()

void OpalSimulation::run ( )
virtual

Calls Opal through Opal-lib wrapper and returns when simulation has either failed or finished.

Implements Simulation.

Definition at line 325 of file OpalSimulation.cpp.

References arg(), comm_, CmdArguments::getArg(), Simulation::getArgs(), hasResultsAvailable(), Options::infoLevel, pwd_, run_opal(), Options::seed, setupSimulation(), simulationDirName_, simulationName_, Options::warnLevel, OpalException::what(), and OpalException::where().

Here is the call graph for this function:

◆ setFilename()

void OpalSimulation::setFilename ( int id)
inline

set job id (SAMPLE command)

Definition at line 73 of file OpalSimulation.h.

References id_m.

◆ setupFSStructure()

void OpalSimulation::setupFSStructure ( )
private

create directories, input files, symlinks...

Definition at line 240 of file OpalSimulation.cpp.

References comm_, copyH5_m(), createSymlink_m(), gs_, simulationDirName_, and simulationName_.

Referenced by setupSimulation().

Here is the call graph for this function:

◆ setupSimulation()

void OpalSimulation::setupSimulation ( )
private

create directories, input files, fieldmaps...

Definition at line 197 of file OpalSimulation.cpp.

References comm_, CmdArguments::getArg(), Simulation::getArgs(), OpalData::getInstance(), id_m, OpalData::setOptimizerFlag(), setupFSStructure(), simTmpDir_, simulationDirName_, and simulationName_.

Referenced by run().

Here is the call graph for this function:

Member Data Documentation

◆ args_

CmdArguments_t Simulation::args_
privateinherited

Definition at line 73 of file Simulation.h.

Referenced by getArgs(), and Simulation().

◆ comm_

MPI_Comm OpalSimulation::comm_
private

◆ constraints_

Expressions::Named_t OpalSimulation::constraints_
private

Definition at line 99 of file OpalSimulation.h.

Referenced by collectResults(), and OpalSimulation().

◆ dvarNames_

std::set<std::string> OpalSimulation::dvarNames_
private

Definition at line 100 of file OpalSimulation.h.

◆ gs_

std::unique_ptr<GenerateOpalSimulation> OpalSimulation::gs_
private

object to generate simulation input files

Definition at line 105 of file OpalSimulation.h.

Referenced by setupFSStructure().

◆ id_m

int OpalSimulation::id_m
private

job id (SAMPLE command)

Definition at line 108 of file OpalSimulation.h.

Referenced by OpalSimulation(), setFilename(), and setupSimulation().

◆ objectives_

Expressions::Named_t OpalSimulation::objectives_
private

Definition at line 98 of file OpalSimulation.h.

Referenced by collectResults(), invalidBunch(), and OpalSimulation().

◆ pwd_

std::string OpalSimulation::pwd_
private

holds current directory (for restoring)

Definition at line 85 of file OpalSimulation.h.

Referenced by collectResults(), and run().

◆ requestedVars_

reqVarContainer_t OpalSimulation::requestedVars_
private

holds solutions returned to the optimizer

Definition at line 96 of file OpalSimulation.h.

Referenced by collectResults(), getResults(), invalidBunch(), and ~OpalSimulation().

◆ simTmpDir_

std::string OpalSimulation::simTmpDir_
private

temporary directory for simulation data (environment var SIMTMPDIR)

Definition at line 82 of file OpalSimulation.h.

Referenced by OpalSimulation(), and setupSimulation().

◆ simulationDirName_

std::string OpalSimulation::simulationDirName_
private

full path of simulation directory (where simulation will be run)

Definition at line 80 of file OpalSimulation.h.

Referenced by cleanUp(), cleanUp(), collectResults(), copyH5_m(), createSymlink_m(), getData(), hasResultsAvailable(), run(), setupFSStructure(), and setupSimulation().

◆ simulationName_

std::string OpalSimulation::simulationName_
private

identification of the simulation (corresponding to output filename)

Definition at line 78 of file OpalSimulation.h.

Referenced by collectResults(), copyH5_m(), getData(), hasResultsAvailable(), run(), setupFSStructure(), and setupSimulation().

◆ strm_buffer_

std::streambuf* OpalSimulation::strm_buffer_
private

stream buffer to redirect output

Definition at line 88 of file OpalSimulation.h.

Referenced by redirectOutToFile(), and restoreOut().

◆ strm_err_

std::streambuf* OpalSimulation::strm_err_
private

stream buffer to redirect stderr

Definition at line 90 of file OpalSimulation.h.

Referenced by redirectOutToFile(), and restoreOut().

◆ userVariables_

std::map<std::string, std::string> OpalSimulation::userVariables_
private

variable dictionary holding requested optimizer values

Definition at line 93 of file OpalSimulation.h.

Referenced by getVariableDictionary(), and ~OpalSimulation().


The documentation for this class was generated from the following files: