OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
GlobalFunctions.h
Go to the documentation of this file.
1//
2// Namespace GlobalFunctions
3// Defines multiple expressions to be evaluated in
4// objectives.
5//
6// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
7// All rights reserved
8//
9// Implemented as part of the PhD thesis
10// "Toward massively parallel multi-objective optimization with application to
11// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
12//
13// This file is part of OPAL.
14//
15// OPAL is free software: you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation, either version 3 of the License, or
18// (at your option) any later version.
19//
20// You should have received a copy of the GNU General Public License
21// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
22//
23#ifndef __GLOBAL_FUNCTIONS_H__
24#define __GLOBAL_FUNCTIONS_H__
25
26#include <string>
27#include <cmath>
28
29#include "boost/tuple/tuple.hpp"
30#include "boost/variant/get.hpp"
31#include "boost/variant/variant.hpp"
32
33#include "Util/Types.h"
34#include "Util/SDDSReader.h"
38
39namespace GlobalFunctions {
40
41//FIXME
42/*
43struct _stat_sum {
44
45 double operator()(client::function::arguments_t args) const {
46
47 // get all arguments
48 double start = boost::get<double>(args[0]);
49 double end = boost::get<double>(args[1]);
50 double step = boost::get<double>(args[2]);
51 std::string name = boost::get<std::string>(args[3]);
52
53 boost::scoped_ptr<SDDSReader> sim_stats(new SDDSReader(stat_filename_));
54 try {
55 sim_stats->parseFile();
56 } catch (OptPilotException &ex) {
57 std::cout << "Caught exception: " << ex.what() << std::endl;
58 }
59
60 double sum = 0.0;
61 bool is_valid = true;
62 for(size_t i = static_cast<size_t>(start);
63 i < static_cast<size_t>(end); i++) {
64
65 double sim_value;
66 try {
67 sim_stats->getInterpolatedValue(i, name, sim_value);
68 } catch(OptPilotException &e) {
69 std::cout << "Exception while getting value "
70 << "from SDDS file: " << e.what()
71 << std::endl;
72 is_valid = false;
73 }
74
75 sum += sim_value;
76 }
77
78 return boost::make_tuple(value, is_valid);
79 }
80};
81*/
82
83struct _sqrt {
85 if (args.size() != 1) {
86 throw OptPilotException("_sqrt::operator()",
87 "sqrt expects 1 arguments, " + std::to_string(args.size()) + " given");
88 }
89
90 double value = boost::get<double>(args[0]);
91 return boost::make_tuple(sqrt(value), true);
92 }
93};
94
95struct _sq {
97 if (args.size() != 1) {
98 throw OptPilotException("_sq::operator()",
99 "sq expects 1 arguments, " + std::to_string(args.size()) + " given");
100 }
101
102 double value = boost::get<double>(args[0]);
103 return boost::make_tuple(value * value, true);
104 }
105};
106
107struct _pow {
109 if (args.size() != 2) {
110 throw OptPilotException("_pow::operator()",
111 "pow expects 2 arguments, " + std::to_string(args.size()) + " given");
112 }
113
114 double value = boost::get<double>(args[0]);
115 double exp = boost::get<double>(args[1]);
116 return boost::make_tuple(pow(value, exp), true);
117 }
118};
119
120struct _exp {
122 if (args.size() != 1) {
123 throw OptPilotException("_exp::operator()",
124 "exp expects 1 arguments, " + std::to_string(args.size()) + " given");
125 }
126
127 double value = boost::get<double>(args[0]);
128 return boost::make_tuple(exp(value), true);
129 }
130};
131
132struct _log {
134 if (args.size() != 1) {
135 throw OptPilotException("_log::operator()",
136 "log expects 1 arguments, " + std::to_string(args.size()) + " given");
137 }
138
139 double value = boost::get<double>(args[0]);
140 return boost::make_tuple(log(value), true);
141 }
142};
143
144
145
146struct _ceil {
148 if (args.size() != 1) {
149 throw OptPilotException("_ceil::operator()",
150 "ceil expects 1 arguments, " + std::to_string(args.size()) + " given");
151 }
152
153 double value = boost::get<double>(args[0]);
154 return boost::make_tuple(ceil(value), true);
155 }
156};
157
158struct _fabs {
160 if (args.size() != 1) {
161 throw OptPilotException("_fabs::operator()",
162 "fabs expects 1 arguments, " + std::to_string(args.size()) + " given");
163 }
164
165 double value = boost::get<double>(args[0]);
166 return boost::make_tuple(fabs(value), true);
167 }
168};
169
170struct _floor {
172 if (args.size() != 1) {
173 throw OptPilotException("_floor::operator()",
174 "floor expects 1 arguments, " + std::to_string(args.size()) + " given");
175 }
176
177 double value = boost::get<double>(args[0]);
178 return boost::make_tuple(floor(value), true);
179 }
180};
181
182struct _fmod {
184 if (args.size() != 2) {
185 throw OptPilotException("_fmod::operator()",
186 "fmod expects 2 arguments, " + std::to_string(args.size()) + " given");
187 }
188
189 double value = boost::get<double>(args[0]);
190 double val2 = boost::get<double>(args[1]);
191 return boost::make_tuple(fmod(value, val2), true);
192 }
193};
194
195
196
197struct _sin {
199 if (args.size() != 1) {
200 throw OptPilotException("_sin::operator()",
201 "sin expects 1 arguments, " + std::to_string(args.size()) + " given");
202 }
203
204 double value = boost::get<double>(args[0]);
205 return boost::make_tuple(sin(value), true);
206 }
207};
208
209struct _asin {
211 if (args.size() != 1) {
212 throw OptPilotException("_asin::operator()",
213 "asin expects 1 arguments, " + std::to_string(args.size()) + " given");
214 }
215
216 double value = boost::get<double>(args[0]);
217 return boost::make_tuple(asin(value), true);
218 }
219};
220
221struct _cos {
223 if (args.size() != 1) {
224 throw OptPilotException("_cos::operator()",
225 "cos expects 1 arguments, " + std::to_string(args.size()) + " given");
226 }
227
228 double value = boost::get<double>(args[0]);
229 return boost::make_tuple(cos(value), true);
230 }
231};
232
233struct _acos {
235 if (args.size() != 1) {
236 throw OptPilotException("_acos::operator()",
237 "acos expects 1 arguments, " + std::to_string(args.size()) + " given");
238 }
239
240 double value = boost::get<double>(args[0]);
241 return boost::make_tuple(acos(value), true);
242 }
243};
244
245struct _tan {
247 if (args.size() != 1) {
248 throw OptPilotException("_tan::operator()",
249 "tan expects 1 arguments, " + std::to_string(args.size()) + " given");
250 }
251
252 double value = boost::get<double>(args[0]);
253 return boost::make_tuple(tan(value), true);
254 }
255};
256
257struct _atan {
259 if (args.size() != 1) {
260 throw OptPilotException("_atan::operator()",
261 "atan expects 1 arguments, " + std::to_string(args.size()) + " given");
262 }
263
264 double value = boost::get<double>(args[0]);
265 return boost::make_tuple(atan(value), true);
266 }
267};
268
269
270static functionDictionary_t get() {
271
272 typedef std::pair<std::string, client::function::type> funcEntry_t;
274
275 client::function::type sqrt_; sqrt_ = _sqrt();
276 funcs.insert(funcEntry_t("sqrt", sqrt_));
277 client::function::type sq_; sq_ = _sq();
278 funcs.insert(funcEntry_t("sq", sq_));
279 client::function::type pow_; pow_ = _pow();
280 funcs.insert(funcEntry_t("pow", pow_));
281 client::function::type exp_; exp_ = _exp();
282 funcs.insert(funcEntry_t("exp", exp_));
283 client::function::type log_; log_ = _log();
284 funcs.insert(funcEntry_t("log", log_));
285
286
287 client::function::type ceil_; ceil_ = _ceil();
288 funcs.insert(funcEntry_t("ceil", ceil_));
289 client::function::type fabs_; fabs_ = _fabs();
290 funcs.insert(funcEntry_t("fabs", fabs_));
291 client::function::type floor_; floor_ = _floor();
292 funcs.insert(funcEntry_t("floor", floor_));
293 client::function::type fmod_; fmod_ = _fmod();
294 funcs.insert(funcEntry_t("fmod", fmod_));
295
296
297 client::function::type sin_; sin_ = _sin();
298 funcs.insert(funcEntry_t("sin", sin_));
299 client::function::type asin_; asin_ = _asin();
300 funcs.insert(funcEntry_t("asin", asin_));
301 client::function::type cos_; cos_ = _cos();
302 funcs.insert(funcEntry_t("cos", cos_));
303 client::function::type acos_; acos_ = _acos();
304 funcs.insert(funcEntry_t("acos", acos_));
305 client::function::type tan_; tan_ = _tan();
306 funcs.insert(funcEntry_t("tan", tan_));
307 client::function::type atan_; atan_ = _atan();
308 funcs.insert(funcEntry_t("atan", atan_));
309
310 return funcs;
311}
312
313}
314
315#endif
Tps< T > log(const Tps< T > &x)
Natural logarithm.
Definition: TpsMath.h:182
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition: TpsMath.h:129
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition: TpsMath.h:76
Tps< T > tan(const Tps< T > &x)
Tangent.
Definition: TpsMath.h:147
Tps< T > exp(const Tps< T > &x)
Exponential.
Definition: TpsMath.h:165
Tps< T > sin(const Tps< T > &x)
Sine.
Definition: TpsMath.h:111
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
PETE_TUTree< FnCeil, typename T::PETE_Expr_t > ceil(const PETE_Expr< T > &l)
Definition: PETE.h:728
PETE_TUTree< FnArcCos, typename T::PETE_Expr_t > acos(const PETE_Expr< T > &l)
Definition: PETE.h:725
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition: PETE.h:732
PETE_TUTree< FnArcSin, typename T::PETE_Expr_t > asin(const PETE_Expr< T > &l)
Definition: PETE.h:726
PETE_TUTree< FnArcTan, typename T::PETE_Expr_t > atan(const PETE_Expr< T > &l)
Definition: PETE.h:727
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition: PETE.h:733
PETE_TBTree< FnFmod, PETE_Scalar< Vektor< T1, Dim > >, typename T2::PETE_Expr_t > fmod(const Vektor< T1, Dim > &l, const PETE_Expr< T2 > &r)
std::map< std::string, client::function::type > functionDictionary_t
Definition: Expression.h:56
boost::tuple< double, bool > Result_t
Definition: Expression.h:66
std::vector< argument_t > arguments_t
Definition: function.hpp:19
boost::function< boost::tuple< double, bool >(arguments_t)> type
Definition: function.hpp:21
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)