OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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
39#include "Util/Types.h"
40#include "Util/PeakReader.h"
42
44
45 static const std::string name;
46
48 if (args.size() != 4) {
49 throw OptPilotException("SumErrSqRadialPeak::operator()",
50 "sumErrSqRadialPeak expects 4 arguments, " + std::to_string(args.size()) + " given");
51 }
52
53 meas_filename_ = boost::get<std::string>(args[0]);
54 sim_filename_ = boost::get<std::string>(args[1]);
55 begin_ = boost::get<double>(args[2]);
56 end_ = boost::get<double>(args[3]);
57
58 bool is_valid = true;
59
60 const std::unique_ptr<PeakReader> meas_peaks(new PeakReader(meas_filename_));
61 const std::unique_ptr<PeakReader> sim_peaks(new PeakReader(sim_filename_));
62 try {
63 sim_peaks->parseFile();
64 meas_peaks->parseFile();
65
66 if ( end_ < begin_ || end_ < 0 || begin_ < 0 )
67 throw OptPilotException("SumErrSqRadialPeak::operator()",
68 "Error check turn number range");
69
70 } catch (OptPilotException &ex) {
71 std::cout << "Caught exception: " << ex.what() << std::endl;
72 is_valid = false;
73 }
74
75 double sum = 0;
76 int nPeaks = end_ - begin_ + 1;
77
78 for (int turn = begin_; turn < end_ + 1; ++turn) {
79 double sim_value = 0.0, meas_value = 0.0;
80 try {
81 sim_peaks->getPeak(turn, sim_value);
82 meas_peaks->getPeak(turn, meas_value);
83 } catch(OptPilotException &e) {
84 std::cout << "Exception while getting value "
85 << "from peak file: " << e.what()
86 << std::endl;
87 is_valid = false;
88 }
89 double val = meas_value - sim_value;
90 sum += val * val;
91 }
92
93 return boost::make_tuple(std::sqrt(sum) / (double)nPeaks, is_valid);
94 }
95
96private:
97 std::string meas_filename_;
98 std::string sim_filename_;
99 int begin_;
100 int end_;
101
102 // define a mapping to arguments in argument vector
103 boost::tuple<std::string, std::string, int, int> argument_types;
104 // :FIXME: remove unused enum
105#if 0
106 enum {
107 meas_filename
108 , sim_filename
109 , begin
110 , end
111 } argument_type_id;
112#endif
113};
114
115#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 sum(const PETE_Expr< T > &expr)
Definition PETE.h:1111
boost::tuple< double, bool > Result_t
Definition Expression.h:66
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)
virtual const char * what() const