Code indexing in gitaly is broken and leads to code not being visible to the user. We work on the issue with highest priority.

Skip to content
Snippets Groups Projects
Commit d0e7237d authored by frey_m's avatar frey_m
Browse files

Merge branch '318-optimizer-generalizing-objectives-with-sddsvariableat' into 'master'

more general sddsVariableAt and statVariableAt

Closes #318

See merge request !115
parents 12b90c5c c5d47d25
No related branches found
No related tags found
1 merge request!115more general sddsVariableAt and statVariableAt
......@@ -14,24 +14,40 @@
/**
* A simple expression to get SDDS (filename = third value) value near a
* specific spos (second argument) for a variable (name = first argument).
* A simple expression to get SDDS (filename) value near a
* specific position (ref_val, default: spos) of a reference
* variable (ref_name) for a variable (var_name). Possible
* argument orders:
* args = [var_name, ref_val, filename]
* args = [var_name, ref_name, ref_val, filename]
*/
struct SDDSVariable {
static const std::string name;
Expressions::Result_t operator()(client::function::arguments_t args) {
if (args.size() != 3) {
throw OptPilotException("SDDSVariable::operator()",
"sddsVariableAt expects 3 arguments, " + std::to_string(args.size()) + " given");
switch ( args.size() ) {
case 3: {
var_name_ = boost::get<std::string>(args[0]);
ref_name_ = "s";
ref_val_ = boost::get<double>(args[1]);
stat_filename_ = boost::get<std::string>(args[2]);
break;
}
case 4: {
var_name_ = boost::get<std::string>(args[0]);
ref_name_ = boost::get<std::string>(args[1]);
ref_val_ = boost::get<double>(args[2]);
stat_filename_ = boost::get<std::string>(args[3]);
break;
}
default: {
throw OptPilotException("SDDSVariable::operator()",
"sddsVariableAt expects 3 or 4 arguments, " +
std::to_string(args.size()) + " given");
}
}
var_name_ = boost::get<std::string>(args[0]);
spos_ = boost::get<double>(args[1]);
stat_filename_ = boost::get<std::string>(args[2]);
bool is_valid = true;
boost::scoped_ptr<SDDSReader> sim_stats(new SDDSReader(stat_filename_));
......@@ -44,7 +60,7 @@ struct SDDSVariable {
double sim_value = 0.0;
try {
sim_stats->getInterpolatedValue(spos_, var_name_, sim_value);
sim_stats->getInterpolatedValue(ref_name_, ref_val_, var_name_, sim_value);
} catch(SDDSParserException &e) {
std::cout << "Exception while getting value "
<< "from SDDS file: " << e.what()
......@@ -64,12 +80,17 @@ private:
std::string var_name_;
std::string stat_filename_;
double spos_;
std::string ref_name_;
double ref_val_;
};
/**
* A simple expression to get value from stat file near a
* specific spos (second argument) for a variable (name = first argument).
* specific position (ref_val, default: spos) of a reference
* variable (ref_var) for a variable (var_name). Possible
* argument orders:
* args = [var_name, ref_val]
* args = [var_name, ref_name, ref_val]
*/
struct sameSDDSVariable {
......@@ -84,12 +105,14 @@ struct sameSDDSVariable {
}
Expressions::Result_t operator()(client::function::arguments_t args) {
if (args.size() != 2) {
if (args.size() < 2 || args.size() > 3) {
throw OptPilotException("sameSDDSVariable::operator()",
"statVariableAt expects 2 arguments, " + std::to_string(args.size()) + " given");
"statVariableAt expects 2 or 3 arguments, " +
std::to_string(args.size()) + " given");
}
args.push_back(stat_filename_);
return var_(args);
}
......@@ -98,4 +121,4 @@ private:
SDDSVariable var_;
};
#endif
\ No newline at end of file
#endif
......@@ -89,34 +89,35 @@ namespace SDDS {
/**
* Converts the string value of a parameter at a position spos to a value
* Converts the string value of a parameter to a value
* of type T.
*
* @param spos interpolate value at spos
* @param ref_name reference quantity (e.g. spos)
* @param ref_val interpolate value of reference quantity (e.g. spos)
* @param col_name parameter name
* @param nval store result of type T in nval
*/
template <typename T>
void getInterpolatedValue(double spos, std::string col_name, T& nval) {
void getInterpolatedValue(std::string ref_name, double ref_val,
std::string col_name, T& nval) {
T value_before = 0;
T value_after = 0;
double value_before_spos = 0;
double value_after_spos = 0;
double value_before_ref = 0;
double value_after_ref = 0;
size_t col_idx_spos = getColumnIndex("s");
ast::columnData_t &spos_values = sddsData_m.sddsColumns_m[col_idx_spos].values_m;
size_t col_idx_ref = getColumnIndex(ref_name);
ast::columnData_t &ref_values = sddsData_m.sddsColumns_m[col_idx_ref].values_m;
int index = getColumnIndex(col_name);
ast::columnData_t &col_values = sddsData_m.sddsColumns_m[index].values_m;
size_t this_row = 0;
size_t num_rows = spos_values.size();
size_t num_rows = ref_values.size();
int datatype = (int)getColumnType(col_name);
for(this_row = 0; this_row < num_rows; this_row++) {
value_after_spos = boost::get<double>(spos_values[this_row]);
value_after_ref = boost::get<double>(ref_values[this_row]);
if(spos < value_after_spos) {
if(ref_val < value_after_ref) {
size_t prev_row = 0;
if(this_row > 0) prev_row = this_row - 1;
......@@ -124,8 +125,8 @@ namespace SDDS {
value_before = getBoostVariantValue<T>(col_values[prev_row], datatype);
value_after = getBoostVariantValue<T>(col_values[this_row], datatype);
value_before_spos = boost::get<double>(spos_values[prev_row]);
value_after_spos = boost::get<double>(spos_values[this_row]);
value_before_ref = boost::get<double>(ref_values[prev_row]);
value_after_ref = boost::get<double>(ref_values[this_row]);
break;
}
......@@ -133,15 +134,29 @@ namespace SDDS {
if(this_row == num_rows)
throw SDDSParserException("SDDSParser::getInterpolatedValue",
"all values < specified spos");
"all values < specified reference value");
// simple linear interpolation
if(spos - value_before_spos < 1e-8)
if(ref_val - value_before_ref < 1e-8)
nval = value_before;
else
nval = value_before + (spos - value_before_spos)
nval = value_before + (ref_val - value_before_ref)
* (value_after - value_before)
/ (value_after_spos - value_before_spos);
/ (value_after_ref - value_before_ref);
}
/**
* Converts the string value of a parameter at a position spos to a value
* of type T.
*
* @param spos interpolate value at spos
* @param col_name parameter name
* @param nval store result of type T in nval
*/
template <typename T>
void getInterpolatedValue(double spos, std::string col_name, T& nval) {
getInterpolatedValue("s", spos, col_name, nval);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment