OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
MaxNormRadialPeak.h
Go to the documentation of this file.
1 //
2 // Struct MaxNormRadialPeak
3 // A simple expression computing the maximum norm 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 = ||measurement_i - value_i||_\infty
9 // \f]
10 //
11 // Copyright (c) 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 __MAX_NORM_RADIALPEAK_H__
31 #define __MAX_NORM_RADIALPEAK_H__
32 
33 #include <string>
34 
35 #include <cmath>
36 
37 #include "boost/type_traits/remove_cv.hpp"
38 #include "boost/variant/get.hpp"
39 #include "boost/variant/variant.hpp"
40 #include "boost/smart_ptr.hpp"
41 
42 #include "Util/Types.h"
43 #include "Util/PeakReader.h"
45 
47 
48  static const std::string name;
49 
51  if (args.size() != 4) {
52  throw OptPilotException("MaxNormRadialPeak::operator()",
53  "maxNormRadialPeak expects 4 arguments, " + std::to_string(args.size()) + " given");
54  }
55 
56  meas_filename_ = boost::get<std::string>(args[0]);
57  sim_filename_ = boost::get<std::string>(args[1]);
58  begin_ = boost::get<double>(args[2]);
59  end_ = boost::get<double>(args[3]);
60 
61  bool is_valid = true;
62 
63  boost::scoped_ptr<PeakReader> meas_peaks(new PeakReader(meas_filename_));
64  boost::scoped_ptr<PeakReader> sim_peaks(new PeakReader(sim_filename_));
65  try {
66  sim_peaks->parseFile();
67  meas_peaks->parseFile();
68 
69  if ( end_ < begin_ || end_ < 0 || begin_ < 0 )
70  throw OptPilotException("MaxNormRadialPeak::operator()",
71  "Error check turn number range");
72 
73  } catch (OptPilotException &ex) {
74  std::cout << "Caught exception: " << ex.what() << std::endl;
75  is_valid = false;
76  }
77 
78  double maximum = -1.0;
79 
80  for (int turn = begin_; turn < end_ + 1; ++turn) {
81  double sim_value = 0.0, meas_value = 0.0;
82  try {
83  sim_peaks->getPeak(turn, sim_value);
84  meas_peaks->getPeak(turn, meas_value);
85  } catch(OptPilotException &e) {
86  std::cout << "Exception while getting value "
87  << "from peak file: " << e.what()
88  << std::endl;
89  is_valid = false;
90  }
91  double val = std::abs(meas_value - sim_value);
92  maximum = std::max(maximum, val);
93  }
94 
95  return boost::make_tuple(maximum, is_valid);
96  }
97 
98 private:
99  std::string meas_filename_;
100  std::string sim_filename_;
101  int begin_;
102  int end_;
103 
104  // define a mapping to arguments in argument vector
105  boost::tuple<std::string, std::string, int, int> argument_types;
106  // :FIXME: remove unused enum
107 #if 0
108  enum {
109  meas_filename
110  , sim_filename
111  , begin
112  , end
113  } argument_type_id;
114 #endif
115 };
116 
117 #endif
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 max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
PETE_TUTree< FnAbs, typename T::PETE_Expr_t > abs(const PETE_Expr< T > &l)
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
std::string meas_filename_
boost::tuple< std::string, std::string, int, int > argument_types
static const std::string name
std::string sim_filename_
Expressions::Result_t operator()(client::function::arguments_t args)
virtual const char * what() const