OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
SumErrSqRadialPeak.h
Go to the documentation of this file.
1 //
2 // Struct SumErrSqRadialPeak
3 // A simple expression computing the sum of all peak errors (given as
4 // first and second argument) for a range of peaks (third argument and fourth argument)
5 // according to
6 //
7 // \f[
8 // result = \frac{1}{n} * \sqrt{\sum_{i=start}^end (measurement_i - value_i)^2}
9 // \f]
10 //
11 // Copyright (c) 2017 - 2018, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
12 // All rights reserved
13 //
14 // Implemented as part of the PhD thesis
15 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
16 // and the paper
17 // "Matching of turn pattern measurements for cyclotrons using multiobjective optimization"
18 // (https://doi.org/10.1103/PhysRevAccelBeams.22.064602)
19 //
20 // This file is part of OPAL.
21 //
22 // OPAL is free software: you can redistribute it and/or modify
23 // it under the terms of the GNU General Public License as published by
24 // the Free Software Foundation, either version 3 of the License, or
25 // (at your option) any later version.
26 //
27 // You should have received a copy of the GNU General Public License
28 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
29 //
30 #ifndef __SUMERRSQRADIALPEAK_H__
31 #define __SUMERRSQRADIALPEAK_H__
32 
33 #include <string>
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/PeakReader.h"
43 
45 
46  static const std::string name;
47 
49  if (args.size() != 4) {
50  throw OptPilotException("SumErrSqRadialPeak::operator()",
51  "sumErrSqRadialPeak expects 4 arguments, " + std::to_string(args.size()) + " given");
52  }
53 
54  meas_filename_ = boost::get<std::string>(args[0]);
55  sim_filename_ = boost::get<std::string>(args[1]);
56  begin_ = boost::get<double>(args[2]);
57  end_ = boost::get<double>(args[3]);
58 
59  bool is_valid = true;
60 
61  boost::scoped_ptr<PeakReader> meas_peaks(new PeakReader(meas_filename_));
62  boost::scoped_ptr<PeakReader> sim_peaks(new PeakReader(sim_filename_));
63  try {
64  sim_peaks->parseFile();
65  meas_peaks->parseFile();
66 
67  if ( end_ < begin_ || end_ < 0 || begin_ < 0 )
68  throw OptPilotException("SumErrSqRadialPeak::operator()",
69  "Error check turn number range");
70 
71  } catch (OptPilotException &ex) {
72  std::cout << "Caught exception: " << ex.what() << std::endl;
73  is_valid = false;
74  }
75 
76  double sum = 0;
77  int nPeaks = end_ - begin_ + 1;
78 
79  for (int turn = begin_; turn < end_ + 1; ++turn) {
80  double sim_value = 0.0, meas_value = 0.0;
81  try {
82  sim_peaks->getPeak(turn, sim_value);
83  meas_peaks->getPeak(turn, meas_value);
84  } catch(OptPilotException &e) {
85  std::cout << "Exception while getting value "
86  << "from peak file: " << e.what()
87  << std::endl;
88  is_valid = false;
89  }
90  double val = meas_value - sim_value;
91  sum += val * val;
92  }
93 
94  return boost::make_tuple(std::sqrt(sum) / (double)nPeaks, is_valid);
95  }
96 
97 private:
98  std::string meas_filename_;
99  std::string sim_filename_;
100  int begin_;
101  int end_;
102 
103  // define a mapping to arguments in argument vector
104  boost::tuple<std::string, std::string, int, int> argument_types;
105  // :FIXME: remove unused enum
106 #if 0
107  enum {
108  meas_filename
109  , sim_filename
110  , begin
111  , end
112  } argument_type_id;
113 #endif
114 };
115 
116 #endif
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
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
boost::tuple< std::string, std::string, int, int > argument_types
static const std::string name
Expressions::Result_t operator()(client::function::arguments_t args)
std::string meas_filename_
virtual const char * what() const