OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
SDDSVariable.h
Go to the documentation of this file.
1 //
2 // Struct SDDSVariable
3 // A simple expression to get SDDS (filename) value near a
4 // specific position (ref_val, default: spos) of a reference
5 // variable (ref_name) for a variable (var_name). Possible
6 // argument orders:
7 // args = [var_name, ref_val, filename]
8 // args = [var_name, ref_name, ref_val, filename]
9 //
10 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
11 // All rights reserved
12 //
13 // Implemented as part of the PhD thesis
14 // "Toward massively parallel multi-objective optimization with application to
15 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
16 //
17 // This file is part of OPAL.
18 //
19 // OPAL is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
26 //
27 #ifndef __SDDSVARIABLE_H__
28 #define __SDDSVARIABLE_H__
29 
30 #include <string>
31 
32 #include "boost/variant/get.hpp"
33 #include "boost/variant/variant.hpp"
34 #include "boost/smart_ptr.hpp"
35 
36 #include "Util/Types.h"
37 #include "Util/SDDSReader.h"
40 
41 
42 struct SDDSVariable {
43 
44  static const std::string name;
45 
47  switch ( args.size() ) {
48  case 3: {
49  var_name_ = boost::get<std::string>(args[0]);
50  ref_name_ = "s";
51  ref_val_ = boost::get<double>(args[1]);
52  stat_filename_ = boost::get<std::string>(args[2]);
53  break;
54  }
55  case 4: {
56  var_name_ = boost::get<std::string>(args[0]);
57  ref_name_ = boost::get<std::string>(args[1]);
58  ref_val_ = boost::get<double>(args[2]);
59  stat_filename_ = boost::get<std::string>(args[3]);
60  break;
61  }
62  default: {
63  throw OptPilotException("SDDSVariable::operator()",
64  "sddsVariableAt expects 3 or 4 arguments, " +
65  std::to_string(args.size()) + " given");
66  }
67  }
68 
69  bool is_valid = true;
70 
71  boost::scoped_ptr<SDDSReader> sim_stats(new SDDSReader(stat_filename_));
72  try {
73  sim_stats->parseFile();
74  } catch (SDDSParserException &ex) {
75  std::cout << "Caught exception: " << ex.what() << std::endl;
76  is_valid = false;
77  }
78 
79  double sim_value = 0.0;
80  try {
81  sim_stats->getInterpolatedValue(ref_name_, ref_val_, var_name_, sim_value);
82  } catch(SDDSParserException &e) {
83  std::cout << "Exception while getting value "
84  << "from SDDS file: " << e.what()
85  << std::endl;
86  is_valid = false;
87  } catch(...) {
88  std::cout << "Exception while getting '" + var_name_ + "' "
89  << "from SDDS file. "
90  << std::endl;
91  is_valid = false;
92  }
93 
94  return boost::make_tuple(sim_value, is_valid);
95  }
96 
97 private:
98 
99  std::string var_name_;
100  std::string stat_filename_;
101  std::string ref_name_;
102  double ref_val_;
103 };
104 
115  sameSDDSVariable(const std::string & base_filename) {
116  size_t pos = base_filename.find_last_of("/");
117  std::string tmplfile = base_filename;
118  if(pos != std::string::npos)
119  tmplfile = base_filename.substr(pos+1);
120  pos = tmplfile.find_last_of(".");
121  // std::string simName =
122  stat_filename_ = tmplfile.substr(0,pos) + ".stat";
123  }
124 
126  if (args.size() < 2 || args.size() > 3) {
127  throw OptPilotException("sameSDDSVariable::operator()",
128  "statVariableAt expects 2 or 3 arguments, " +
129  std::to_string(args.size()) + " given");
130  }
131 
132  args.push_back(stat_filename_);
133 
134  return var_(args);
135  }
136 
137 private:
140 };
141 
142 #endif
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
boost::tuple< double, bool > Result_t
Definition: Expression.h:66
constexpr double e
The value of.
Definition: Physics.h:39
std::vector< argument_t > arguments_t
Definition: function.hpp:19
boost::variant< double, bool, std::string > argument_t
Definition: function.hpp:17
Expressions::Result_t operator()(client::function::arguments_t args)
Definition: SDDSVariable.h:46
double ref_val_
Definition: SDDSVariable.h:102
std::string ref_name_
Definition: SDDSVariable.h:101
std::string stat_filename_
Definition: SDDSVariable.h:100
std::string var_name_
Definition: SDDSVariable.h:99
static const std::string name
Definition: SDDSVariable.h:44
client::function::argument_t stat_filename_
Definition: SDDSVariable.h:138
sameSDDSVariable(const std::string &base_filename)
Definition: SDDSVariable.h:115
Expressions::Result_t operator()(client::function::arguments_t args)
Definition: SDDSVariable.h:125
SDDSVariable var_
Definition: SDDSVariable.h:139
virtual const char * what() const