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