OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
SumErrSq.h
Go to the documentation of this file.
1 //
2 // Struct SumErrSq
3 // A simple expression computing the sum of all measurement errors (given as
4 // first and third argument) for a variable (second argument) according to
5 //
6 // \f[
7 // result = \frac{1}{n} * \sqrt{\sum_{i=0}^n (measurement_i - value_i)^2}
8 // \f]
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 __SUMERRSQ_H__
28 #define __SUMERRSQ_H__
29 
30 #include <map>
31 #include <string>
32 #include <fstream>
33 #include <iterator>
34 
35 #include "boost/type_traits/remove_cv.hpp"
36 #include "boost/variant/get.hpp"
37 #include "boost/variant/variant.hpp"
38 #include "boost/smart_ptr.hpp"
39 
40 #include "Util/Types.h"
41 #include "Util/SDDSReader.h"
43 
44 class Measurement {
45 public:
46  double spos;
47  double measurement;
48 
49  friend std::istream & operator>>(std::istream & stream, Measurement & measurement);
50 };
51 
52 
53 struct SumErrSq {
54 
55  static const std::string name;
56 
58 
59  if (args.size() != 3) {
60  throw OptPilotException("SumErrSq::operator()",
61  "sumErrSq expects 3 arguments, " + std::to_string(args.size()) + " given");
62  }
63 
64  std::string measurement_filename = boost::get<std::string>(args[0]);
65  var_name_ = boost::get<std::string>(args[1]);
66  stat_filename_ = boost::get<std::string>(args[2]);
67 
68  //FIXME: we could assume measurements don't change
69  parseMeasurements(measurement_filename);
70  bool is_valid = true;
71 
72  boost::scoped_ptr<SDDSReader> sim_stats(new SDDSReader(stat_filename_));
73  try {
74  sim_stats->parseFile();
75  } catch (OptPilotException &ex) {
76  std::cout << "Caught exception: " << ex.what() << std::endl;
77  is_valid = false;
78  }
79 
80  double sum = 0;
81 
82  for(Measurement measurement : measurements_) {
83  double sim_value = 0.0;
84  try {
85  sim_stats->getInterpolatedValue(
86  measurement.spos, var_name_, sim_value);
87  } catch(OptPilotException &e) {
88  std::cout << "Exception while getting value "
89  << "from SDDS file: " << e.what()
90  << std::endl;
91  is_valid = false;
92  }
93  double val = measurement.measurement - sim_value;
94  sum += val * val;
95  }
96 
97  return boost::make_tuple(sqrt(sum/measurements_.size()), is_valid);
98  }
99 
100 private:
101 
102  std::vector<Measurement> measurements_;
103 
104  std::string var_name_;
105  std::string stat_filename_;
106 
107  void parseMeasurements(std::string measurement_filename);
108 
109  // define a mapping to arguments in argument vector
110  boost::tuple<std::string, std::string, std::string> argument_types;
111 };
112 
113 #endif
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1111
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
friend std::istream & operator>>(std::istream &stream, Measurement &measurement)
Definition: SumErrSq.cpp:5
double spos
Definition: SumErrSq.h:46
double measurement
Definition: SumErrSq.h:47
Expressions::Result_t operator()(client::function::arguments_t args)
Definition: SumErrSq.h:57
static const std::string name
Definition: SumErrSq.h:55
std::vector< Measurement > measurements_
Definition: SumErrSq.h:102
void parseMeasurements(std::string measurement_filename)
Definition: SumErrSq.cpp:13
std::string var_name_
Definition: SumErrSq.h:104
std::string stat_filename_
Definition: SumErrSq.h:105
boost::tuple< std::string, std::string, std::string > argument_types
Definition: SumErrSq.h:110
virtual const char * what() const