OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
SDDSVariable.h
Go to the documentation of this file.
1//
2// Struct SDDSVariable
3// A simple expression to get SDDS (filename) value near a
4// specific position (ref_val, default: spos) of a reference
5// variable (ref_name) for a variable (var_name). Possible
6// argument orders:
7// args = [var_name, ref_val, filename]
8// args = [var_name, ref_name, ref_val, filename]
9//
10// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
11// All rights reserved
12//
13// Implemented as part of the PhD thesis
14// "Toward massively parallel multi-objective optimization with application to
15// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
16//
17// This file is part of OPAL.
18//
19// OPAL is free software: you can redistribute it and/or modify
20// it under the terms of the GNU General Public License as published by
21// the Free Software Foundation, either version 3 of the License, or
22// (at your option) any later version.
23//
24// You should have received a copy of the GNU General Public License
25// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
26//
27#ifndef __SDDSVARIABLE_H__
28#define __SDDSVARIABLE_H__
29
30#include <string>
31
32#include "boost/variant/get.hpp"
33#include "boost/variant/variant.hpp"
34
35#include "Util/Types.h"
36#include "Util/SDDSReader.h"
39
40
42
43 static const std::string name;
44
46 switch ( args.size() ) {
47 case 3: {
48 var_name_ = boost::get<std::string>(args[0]);
49 ref_name_ = "s";
50 ref_val_ = boost::get<double>(args[1]);
51 stat_filename_ = boost::get<std::string>(args[2]);
52 break;
53 }
54 case 4: {
55 var_name_ = boost::get<std::string>(args[0]);
56 ref_name_ = boost::get<std::string>(args[1]);
57 ref_val_ = boost::get<double>(args[2]);
58 stat_filename_ = boost::get<std::string>(args[3]);
59 break;
60 }
61 default: {
62 throw OptPilotException("SDDSVariable::operator()",
63 "sddsVariableAt expects 3 or 4 arguments, " +
64 std::to_string(args.size()) + " given");
65 }
66 }
67
68 bool is_valid = true;
69
70 const std::unique_ptr<SDDSReader> sim_stats(new SDDSReader(stat_filename_));
71 try {
72 sim_stats->parseFile();
73 } catch (SDDSParserException &ex) {
74 std::cout << "Caught exception: " << ex.what() << std::endl;
75 is_valid = false;
76 }
77
78 double sim_value = 0.0;
79 try {
80 sim_stats->getInterpolatedValue(ref_name_, ref_val_, var_name_, sim_value);
81 } catch(SDDSParserException &e) {
82 std::cout << "Exception while getting value "
83 << "from SDDS file: " << e.what()
84 << std::endl;
85 is_valid = false;
86 } catch(...) {
87 std::cout << "Exception while getting '" + var_name_ + "' "
88 << "from SDDS file. "
89 << std::endl;
90 is_valid = false;
91 }
92
93 return boost::make_tuple(sim_value, is_valid);
94 }
95
96private:
97
98 std::string var_name_;
99 std::string stat_filename_;
100 std::string ref_name_;
101 double ref_val_;
102};
103
112
114 sameSDDSVariable(const std::string & base_filename) {
115 size_t pos = base_filename.find_last_of("/");
116 std::string tmplfile = base_filename;
117 if(pos != std::string::npos)
118 tmplfile = base_filename.substr(pos+1);
119 pos = tmplfile.find_last_of(".");
120 // std::string simName =
121 stat_filename_ = tmplfile.substr(0,pos) + ".stat";
122 }
123
125 if (args.size() < 2 || args.size() > 3) {
126 throw OptPilotException("sameSDDSVariable::operator()",
127 "statVariableAt expects 2 or 3 arguments, " +
128 std::to_string(args.size()) + " given");
129 }
130
131 args.push_back(stat_filename_);
132
133 return var_(args);
134 }
135
136private:
139};
140
141#endif
boost::tuple< double, bool > Result_t
Definition Expression.h:66
std::vector< argument_t > arguments_t
Definition function.hpp:19
boost::variant< double, bool, std::string > argument_t
Definition function.hpp:17
Expressions::Result_t operator()(client::function::arguments_t args)
std::string ref_name_
std::string stat_filename_
std::string var_name_
static const std::string name
client::function::argument_t stat_filename_
sameSDDSVariable(const std::string &base_filename)
Expressions::Result_t operator()(client::function::arguments_t args)
SDDSVariable var_
virtual const char * what() const