OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
SDDSVariable.h
Go to the documentation of this file.
1 #ifndef __SDDSVARIABLE_H__
2 #define __SDDSVARIABLE_H__
3 
4 #include <string>
5 
6 #include "boost/variant/get.hpp"
7 #include "boost/variant/variant.hpp"
8 #include "boost/smart_ptr.hpp"
9 
10 #include "Util/Types.h"
11 #include "Util/SDDSReader.h"
14 
15 
24 struct SDDSVariable {
25 
26  static const std::string name;
27 
29  switch ( args.size() ) {
30  case 3: {
31  var_name_ = boost::get<std::string>(args[0]);
32  ref_name_ = "s";
33  ref_val_ = boost::get<double>(args[1]);
34  stat_filename_ = boost::get<std::string>(args[2]);
35  break;
36  }
37  case 4: {
38  var_name_ = boost::get<std::string>(args[0]);
39  ref_name_ = boost::get<std::string>(args[1]);
40  ref_val_ = boost::get<double>(args[2]);
41  stat_filename_ = boost::get<std::string>(args[3]);
42  break;
43  }
44  default: {
45  throw OptPilotException("SDDSVariable::operator()",
46  "sddsVariableAt expects 3 or 4 arguments, " +
47  std::to_string(args.size()) + " given");
48  }
49  }
50 
51  bool is_valid = true;
52 
53  boost::scoped_ptr<SDDSReader> sim_stats(new SDDSReader(stat_filename_));
54  try {
55  sim_stats->parseFile();
56  } catch (SDDSParserException &ex) {
57  std::cout << "Caught exception: " << ex.what() << std::endl;
58  is_valid = false;
59  }
60 
61  double sim_value = 0.0;
62  try {
63  sim_stats->getInterpolatedValue(ref_name_, ref_val_, var_name_, sim_value);
64  } catch(SDDSParserException &e) {
65  std::cout << "Exception while getting value "
66  << "from SDDS file: " << e.what()
67  << std::endl;
68  is_valid = false;
69  } catch(...) {
70  std::cout << "Exception while getting '" + var_name_ + "' "
71  << "from SDDS file. "
72  << std::endl;
73  is_valid = false;
74  }
75 
76  return boost::make_tuple(sim_value, is_valid);
77  }
78 
79 private:
80 
81  std::string var_name_;
82  std::string stat_filename_;
83  std::string ref_name_;
84  double ref_val_;
85 };
86 
97  sameSDDSVariable(const std::string & base_filename) {
98  size_t pos = base_filename.find_last_of("/");
99  std::string tmplfile = base_filename;
100  if(pos != std::string::npos)
101  tmplfile = base_filename.substr(pos+1);
102  pos = tmplfile.find_last_of(".");
103  // std::string simName =
104  stat_filename_ = tmplfile.substr(0,pos) + ".stat";
105  }
106 
108  if (args.size() < 2 || args.size() > 3) {
109  throw OptPilotException("sameSDDSVariable::operator()",
110  "statVariableAt expects 2 or 3 arguments, " +
111  std::to_string(args.size()) + " given");
112  }
113 
114  args.push_back(stat_filename_);
115 
116  return var_(args);
117  }
118 
119 private:
122 };
123 
124 #endif
boost::variant< double, bool, std::string > argument_t
Definition: function.hpp:17
Expressions::Result_t operator()(client::function::arguments_t args)
Definition: SDDSVariable.h:28
double ref_val_
Definition: SDDSVariable.h:84
constexpr double e
The value of .
Definition: Physics.h:40
std::string stat_filename_
Definition: SDDSVariable.h:82
std::string ref_name_
Definition: SDDSVariable.h:83
boost::tuple< double, bool > Result_t
Definition: Expression.h:37
sameSDDSVariable(const std::string &base_filename)
Definition: SDDSVariable.h:97
std::string var_name_
Definition: SDDSVariable.h:81
std::vector< argument_t > arguments_t
Definition: function.hpp:19
Implements a parser and value extractor for Probe loss files.
Definition: SDDSReader.h:6
SDDSVariable var_
Definition: SDDSVariable.h:121
client::function::argument_t stat_filename_
Definition: SDDSVariable.h:120
virtual const char * what() const
static const std::string name
Definition: SDDSVariable.h:26
Expressions::Result_t operator()(client::function::arguments_t args)
Definition: SDDSVariable.h:107
Inform & endl(Inform &inf)
Definition: Inform.cpp:42