Commit e7648a59 authored by frey_m's avatar frey_m
Browse files

Merge branch '521-upper-case-string-attributes' into 'master'

Resolve "Upper case string attributes"

Closes #521

See merge request !341
parents b6e2278d 93a66763
......@@ -349,8 +349,6 @@ void MultiBunchHandler::setMode(const std::string& mbmode) {
void MultiBunchHandler::setBinning(std::string binning) {
binning = Util::toUpper(binning);
if ( binning.compare("BUNCH") == 0 ) {
*gmsg << "Use 'BUNCH' injection for binnning." << endl;
binning_m = MB_BINNING::BUNCH;
......
......@@ -28,9 +28,7 @@ void AmrObject::setTagging(TaggingCriteria tagging) {
}
void AmrObject::setTagging(std::string tagging) {
tagging = Util::toUpper(tagging);
void AmrObject::setTagging(const std::string& tagging) {
if ( tagging == "POTENTIAL" )
tagging_m = TaggingCriteria::POTENTIAL;
else if (tagging == "EFIELD" )
......
......@@ -4,7 +4,6 @@
#include "Index/NDIndex.h"
#include "Algorithms/PBunchDefs.h"
#include "Utilities/Util.h"
// #include "Algorithms/AmrPartBunch.h"
......@@ -88,7 +87,7 @@ public:
* Is used in src/Structure/FieldSolver.cpp
* @param tagging strategy
*/
void setTagging(std::string tagging);
void setTagging(const std::string& tagging);
/*!
* Scaling factor for tagging.
......
......@@ -27,6 +27,8 @@
#include "Attributes/Reference.h"
#include "Attributes/opalstr.h"
#include "Attributes/StringArray.h"
#include "Attributes/UpperCaseString.h"
#include "Attributes/UpperCaseStringArray.h"
#include "Attributes/TableRow.h"
#include "Attributes/TokenList.h"
#include "Attributes/TokenListArray.h"
......@@ -308,7 +310,8 @@ namespace Attributes {
if(attr.isBaseAllocated()) {
AttributeBase *base = &attr.getBase();
std::string expr;
if(dynamic_cast<String *>(&attr.getHandler())) {
if(dynamic_cast<String *>(&attr.getHandler())
|| dynamic_cast<UpperCaseString *>(&attr.getHandler())) {
expr = dynamic_cast<SValue<std::string> *>(base)->evaluate();
} else if(SValue<SRefAttr<std::string> > *ref =
dynamic_cast<SValue<SRefAttr<std::string> > *>(base)) {
......@@ -368,8 +371,35 @@ namespace Attributes {
// ----------------------------------------------------------------------
// String array value.
// Upper case string value.
Attribute makeUpperCaseString(const std::string &name, const std::string &help) {
return Attribute(new UpperCaseString(name, help), nullptr);
}
Attribute
makeUpperCaseString(const std::string &name, const std::string &help, const std::string &initial) {
return Attribute(new UpperCaseString(name, help), new SValue<std::string>(Util::toUpper(initial)));
}
void setUpperCaseString(Attribute &attr, const std::string &val) {
if(dynamic_cast<const UpperCaseString *>(&attr.getHandler())) {
attr.set(new SValue<std::string>(Util::toUpper(val)));
} else if(SValue<SRefAttr<std::string> > *ref =
dynamic_cast<SValue<SRefAttr<std::string> >*>(&attr.getBase())) {
const SRefAttr<std::string> &value = ref->evaluate();
value.set(Util::toUpper(val));
} else {
throw OpalException("Attributes::setUpperCaseString()", "Attribute \"" +
attr.getName() + "\" is not an upper case string.");
}
}
// ----------------------------------------------------------------------
// String array value.
Attribute makeStringArray(const std::string &name, const std::string &help) {
return Attribute(new StringArray(name, help), nullptr);
}
......@@ -378,7 +408,8 @@ namespace Attributes {
std::vector<std::string> getStringArray(const Attribute &attr) {
if(attr.isBaseAllocated()) {
AttributeBase *base = &attr.getBase();
if(dynamic_cast<StringArray *>(&attr.getHandler())) {
if(dynamic_cast<StringArray *>(&attr.getHandler())
|| dynamic_cast<UpperCaseStringArray *>(&attr.getHandler())) {
auto opal = OpalData::getInstance();
boost::regex variableRE("\\$\\{(.*?)\\}");
......@@ -430,6 +461,25 @@ namespace Attributes {
}
}
// ----------------------------------------------------------------------
// Upper case string array value.
Attribute makeUpperCaseStringArray(const std::string &name, const std::string &help) {
return Attribute(new UpperCaseStringArray(name, help), nullptr);
}
void setUpperCaseStringArray(Attribute &attr, const std::vector<std::string> &value) {
if(dynamic_cast<const UpperCaseStringArray *>(&attr.getHandler())) {
// Strings are never expressions, so AValue will do here.
std::vector<std::string> uppercase(value.size());
std::transform(value.begin(), value.end(), uppercase.begin(),
[](std::string val) -> std::string { return Util::toUpper(val); });
attr.set(new AValue<std::string>(uppercase));
} else {
throw OpalException("Attributes::setUpperCaseStringArray()", "Attribute \"" +
attr.getName() + "\" is not an upper case string array.");
}
}
// ----------------------------------------------------------------------
// Table row reference value.
......
......@@ -139,17 +139,34 @@ namespace Attributes {
/// Set string value.
extern void setString(Attribute &attr, const std::string &val);
/// Make uppercase string attribute.
// Initial value undefined.
extern Attribute makeUpperCaseString(const std::string &name, const std::string &help);
/// Make uppercase string attribute.
// Initial value is defined.
extern Attribute
makeUpperCaseString(const std::string &name, const std::string &help, const std::string &initial);
/// Set uppercase string value.
extern void setUpperCaseString(Attribute &attr, const std::string &val);
/// Create a string array attribute.
// Initial value is empty array.
extern Attribute makeStringArray(const std::string &name, const std::string &help);
/// Get string array value.
extern std::vector<std::string> getStringArray(const Attribute &);
/// Set string array value.
extern void setStringArray(Attribute &, const std::vector<std::string> &);
/// Make uppercase string array attribute.
// Initial value is empty array.
extern Attribute makeUpperCaseStringArray(const std::string &name, const std::string &help);
/// Set upper case string array value.
extern void setUpperCaseStringArray(Attribute &, const std::vector<std::string> &);
/// Create a table row attribute.
// Initial value is undefined.
......@@ -187,4 +204,4 @@ namespace Attributes {
};
#endif // OPAL_Attributes_HH
#endif // OPAL_Attributes_HH
\ No newline at end of file
......@@ -12,6 +12,8 @@ set (_SRCS
TableRow.cpp
TokenList.cpp
TokenListArray.cpp
UpperCaseString.cpp
UpperCaseStringArray.cpp
)
include_directories (
......@@ -34,6 +36,8 @@ set (HDRS
TableRow.h
TokenListArray.h
TokenList.h
UpperCaseString.h
UpperCaseStringArray.h
)
install (FILES ${HDRS} DESTINATION "${CMAKE_INSTALL_PREFIX}/include/Attributes")
......@@ -45,4 +49,4 @@ install (FILES ${HDRS} DESTINATION "${CMAKE_INSTALL_PREFIX}/include/Attributes")
# cmake-tab-width: 4
# indent-tabs-mode: nil
# require-final-newline: nil
# End:
# End:
\ No newline at end of file
//
// Class UpperCaseString
// This class is used to parse attributes of type string that should
// be upper case, i.e. it returns upper case strings.
//
// Copyright (c) 2020, Christof Metzger-Kraus, Open Sourcerer
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#include "Attributes/Attributes.h"
#include "Attributes/UpperCaseString.h"
#include "AbstractObjects/Expressions.h"
#include "Expressions/SValue.h"
#include "Utilities/OpalException.h"
#include "Utilities/ParseError.h"
#include "Utilities/Util.h"
using namespace Expressions;
namespace Attributes {
UpperCaseString::UpperCaseString(const std::string &name, const std::string &help):
AttributeHandler(name, help, 0)
{}
UpperCaseString::~UpperCaseString()
{}
const std::string &UpperCaseString::getType() const {
static const std::string type("upper case string");
return type;
}
void UpperCaseString::parse(Attribute &attr, Statement &stat, bool) const {
Attributes::setUpperCaseString(attr, parseString(stat, "String value expected."));
}
};
\ No newline at end of file
//
// Class UpperCaseString
// This class is used to parse attributes of type string that should
// be upper case, i.e. it returns upper case strings.
//
// Copyright (c) 2020, Christof Metzger-Kraus, Open Sourcerer
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef OPAL_UpperCaseString_HH
#define OPAL_UpperCaseString_HH
#include "AbstractObjects/Attribute.h"
#include "AbstractObjects/AttributeHandler.h"
namespace Attributes {
/// Parser for an attribute of type string.
class UpperCaseString: public AttributeHandler {
public:
/// Constructor.
// Assign attribute name and help string.
UpperCaseString(const std::string &name, const std::string &help);
virtual ~UpperCaseString();
/// Return attribute type string ``string''.
virtual const std::string &getType() const;
/// Parse the attribute.
virtual void parse(Attribute &, Statement &, bool) const;
private:
// Not implemented.
UpperCaseString();
UpperCaseString(const UpperCaseString &);
void operator=(const UpperCaseString &);
};
};
#endif
\ No newline at end of file
//
// Class UpperCaseStringArray
// This class is used to parse attributes of type array of strings that should
// be all upper case, i.e. it returns an array of upper case strings.
//
// Copyright (c) 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#include "Attributes/UpperCaseStringArray.h"
#include "Attributes/Attributes.h"
#include "AbstractObjects/Expressions.h"
#include "Expressions/AValue.h"
#include "Utilities/OpalException.h"
#include "Utilities/ParseError.h"
#include "Utilities/Util.h"
#include <vector>
using namespace Expressions;
namespace Attributes {
UpperCaseStringArray::UpperCaseStringArray(const std::string &name, const std::string &help):
AttributeHandler(name, help, 0)
{}
UpperCaseStringArray::~UpperCaseStringArray()
{}
const std::string &UpperCaseStringArray::getType() const {
static std::string type = "upper case string array";
return type;
}
void UpperCaseStringArray::parse(Attribute &attr, Statement &stat, bool) const {
std::vector<std::string> array = Expressions::parseStringArray(stat);
Attributes::setUpperCaseStringArray(attr, array);
}
void UpperCaseStringArray::parseComponent
(Attribute &attr, Statement &statement, bool, int index) const {
std::vector<std::string> array = Attributes::getStringArray(attr);
if(AttributeBase *base = &attr.getBase()) {
array = dynamic_cast<AValue<std::string>*>(base)->evaluate();
}
while(int(array.size()) < index) {
array.push_back(std::string());
}
array[index - 1] =
Expressions::parseString(statement, "String value expected.");
Attributes::setUpperCaseStringArray(attr, array);
}
};
//
// Class UpperCaseStringArray
// This class is used to parse attributes of type array of strings that should
// be all upper case, i.e. it returns an array of upper case strings.
//
// Copyright (c) 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef OPAL_UpperCaseStringArray_HH
#define OPAL_UpperCaseStringArray_HH
#include "AbstractObjects/Attribute.h"
#include "AbstractObjects/AttributeHandler.h"
#include "AbstractObjects/Expressions.h"
namespace Attributes {
/// Parser for an attribute of type string array.
class UpperCaseStringArray: public AttributeHandler {
public:
/// Constructor.
// Assign attribute name and help string.
UpperCaseStringArray(const std::string &name, const std::string &help);
virtual ~UpperCaseStringArray();
/// Return attribute type string ``string array''.
virtual const std::string &getType() const;
/// Parse the attribute.
virtual void parse(Attribute &, Statement &, bool) const;
/// Parse a component of the array.
// Identified by its index.
virtual void parseComponent(Attribute &, Statement &, bool, int) const;
private:
// Not implemented.
UpperCaseStringArray();
UpperCaseStringArray(const UpperCaseStringArray &);
void operator=(const UpperCaseStringArray &);
};
};
#endif
......@@ -23,7 +23,6 @@
#include "Utilities/OptionTypes.h"
#include "Utilities/ClassicRandom.h"
#include "Utility/IpplInfo.h"
#include "Utilities/Util.h"
#include "Utilities/OpalException.h"
......@@ -148,7 +147,7 @@ Option::Option():
"to the beam mass is larger than REMOTEPARTDEL times of the beam rms size, "
"its default values is 0 (no delete) ",0.0);
itsAttr[PSDUMPFRAME] = Attributes::makeString
itsAttr[PSDUMPFRAME] = Attributes::makeUpperCaseString
("PSDUMPFRAME", "Controls the frame of phase space dump in "
"stat file and h5 file. If 'GLOBAL' OPAL will dump in the "
"lab (global) Cartesian frame; if 'BUNCH_MEAN' OPAL will "
......@@ -211,7 +210,7 @@ Option::Option():
("CZERO", "If set to true a symmetric distribution is "
"created -> centroid == 0.0 ", cZero);
itsAttr[RNGTYPE] = Attributes::makeString
itsAttr[RNGTYPE] = Attributes::makeUpperCaseString
("RNGTYPE", "RANDOM (default), Quasi-random number "
"generators: HALTON, SOBOL, NIEDERREITER (Gsl ref manual 18.5)", rngtype);
......@@ -290,7 +289,7 @@ Option::Option(const std::string &name, Option *parent):
Attributes::setReal(itsAttr[PSDUMPFREQ], psDumpFreq);
Attributes::setReal(itsAttr[STATDUMPFREQ], statDumpFreq);
Attributes::setBool(itsAttr[PSDUMPEACHTURN], psDumpEachTurn);
Attributes::setString(itsAttr[PSDUMPFRAME], DumpFrameToString(psDumpFrame));
Attributes::setUpperCaseString(itsAttr[PSDUMPFRAME], DumpFrameToString(psDumpFrame));
Attributes::setReal(itsAttr[SPTDUMPFREQ], sptDumpFreq);
Attributes::setReal(itsAttr[SCSOLVEFREQ], scSolveFreq);
Attributes::setReal(itsAttr[MTSSUBSTEPS], mtsSubsteps);
......@@ -305,7 +304,7 @@ Option::Option(const std::string &name, Option *parent):
Attributes::setReal(itsAttr[SURFDUMPFREQ], surfDumpFreq);
Attributes::setBool(itsAttr[CZERO], cZero);
Attributes::setBool(itsAttr[CLOTUNEONLY], cloTuneOnly);
Attributes::setString(itsAttr[RNGTYPE], std::string(rngtype));
Attributes::setUpperCaseString(itsAttr[RNGTYPE], std::string(rngtype));
Attributes::setReal(itsAttr[NUMBLOCKS], numBlocks);
Attributes::setReal(itsAttr[RECYCLEBLOCKS], recycleBlocks);
Attributes::setReal(itsAttr[NLHS], nLHS);
......@@ -384,7 +383,7 @@ void Option::execute() {
IpplInfo::Info->on(info);
IpplInfo::Warn->on(warn);
handlePsDumpFrame(Util::toUpper(Attributes::getString(itsAttr[PSDUMPFRAME])));
handlePsDumpFrame(Attributes::getString(itsAttr[PSDUMPFRAME]));
if(itsAttr[ASCIIDUMP]) {
asciidump = Attributes::getBool(itsAttr[ASCIIDUMP]);
......
......@@ -328,7 +328,6 @@ double RFCavity::getPhi0() const {
}
void RFCavity::setComponentType(std::string name) {
name = Util::toUpper(name);
if(name == "STANDING") {
type_m = SW;
} else if(name == "SINGLEGAP") {
......
......@@ -31,7 +31,6 @@
#include "TrimCoils/TrimCoilPhaseFit.h"
#include "TrimCoils/TrimCoilMirrored.h"
#include "Utilities/OpalException.h"
#include "Utilities/Util.h"
#include "Utility/IpplInfo.h"
extern Inform *gmsg;
......@@ -58,7 +57,7 @@ OpalTrimCoil::OpalTrimCoil():
Definition(SIZE, "TRIMCOIL",
"The \"TRIMCOIL\" statement defines a trim coil."),
trimcoil_m(nullptr) {
itsAttr[TYPE] = Attributes::makeString
itsAttr[TYPE] = Attributes::makeUpperCaseString
("TYPE", "Specifies the type of trim coil: PSI-BFIELD, PSI-PHASE, PSI-BFIELD-MIRRORED");
itsAttr[COEFNUM] = Attributes::makeRealArray
......@@ -151,7 +150,7 @@ void OpalTrimCoil::update() {
void OpalTrimCoil::initOpalTrimCoil() {
if (trimcoil_m != nullptr) return;
std::string type = Util::toUpper(Attributes::getString(itsAttr[TYPE]));
std::string type = Attributes::getString(itsAttr[TYPE]);
double bmax = Attributes::getReal(itsAttr[BMAX]);
double phimin = Attributes::getReal(itsAttr[PHIMIN]);
......@@ -187,7 +186,7 @@ Inform& OpalTrimCoil::print(Inform &os) const {
<< "* TRIMCOIL " << getOpalName() << '\n'
<< "* TYPE " << Attributes::getString(itsAttr[TYPE]) << '\n';
std::string type = Util::toUpper(Attributes::getString(itsAttr[TYPE]));
std::string type = Attributes::getString(itsAttr[TYPE]);
if (type == "PSI-BFIELD" || type == "PSI-PHASE") {
printPolynom(os,itsAttr[COEFNUM]);
printPolynom(os,itsAttr[COEFDENOM]);
......@@ -199,7 +198,7 @@ Inform& OpalTrimCoil::print(Inform &os) const {
<< "* RMIN " << Attributes::getReal(itsAttr[RMIN]) << '\n'
<< "* RMAX " << Attributes::getReal(itsAttr[RMAX]) << '\n';
if (Util::toUpper(Attributes::getString(itsAttr[TYPE])) == "PSI-BFIELD-MIRRORED") {
if (Attributes::getString(itsAttr[TYPE]) == "PSI-BFIELD-MIRRORED") {
os << "* SLPTC " << Attributes::getReal(itsAttr[SLPTC]) << '\n';
}
os << "* *********************************************************************************" << endl;
......
......@@ -36,7 +36,6 @@
#include "AbstractObjects/BeamSequence.h"
#include "Structure/H5PartWrapper.h"
#include "Structure/H5PartWrapperForPC.h"
#include "Utilities/Util.h"
#include "Utilities/EarlyLeaveException.h"
#include <gsl/gsl_histogram.h>
......@@ -878,7 +877,7 @@ void Distribution::chooseInputMomentumUnits(InputMomentumUnitsT::InputMomentumUn
/*
* Toggle what units to use for inputing momentum.
*/
std::string inputUnits = Util::toUpper(Attributes::getString(itsAttr[Attrib::Distribution::INPUTMOUNITS]));
std::string inputUnits = Attributes::getString(itsAttr[Attrib::Distribution::INPUTMOUNITS]);
if (inputUnits == "NONE")
inputMoUnits_m = InputMomentumUnitsT::NONE;
else if (inputUnits == "EV")
......@@ -1355,7 +1354,6 @@ void Distribution::createMatchedGaussDistribution(size_t numberOfParticles, doub
sigmaR_m[1] = std::sqrt(sigma(4, 4));
//p_l^2 = [(delta+1)*beta*gamma]^2 - px^2 - pz^2
double pl2 = (std::sqrt(sigma(5,5)) + 1)*(std::sqrt(sigma(5,5)) + 1)*beta*gamma*beta*gamma -
sigmaP_m[0]*sigmaP_m[0] - sigmaP_m[2]*sigmaP_m[2];
......@@ -3206,15 +3204,15 @@ gsl_qrng* Distribution::selectRandomGenerator(std::string,unsigned int dimension
void Distribution::setAttributes() {
itsAttr[Attrib::Distribution::TYPE]
= Attributes::makeString("TYPE","Distribution type: "
"FROMFILE, "
"GAUSS, "
"BINOMIAL, "
"FLATTOP, "
"MULTIGAUSS, "
"GUNGAUSSFLATTOPTH, "
"ASTRAFLATTOPTH, "
"GAUSSMATCHED");
= Attributes::makeUpperCaseString("TYPE","Distribution type: "
"FROMFILE, "
"GAUSS, "
"BINOMIAL, "
"FLATTOP, "
"MULTIGAUSS, "
"GUNGAUSSFLATTOPTH, "
"ASTRAFLATTOPTH, "
"GAUSSMATCHED");
itsAttr[Attrib::Legacy::Distribution::DISTRIBUTION]
= Attributes::makeString("DISTRIBUTION","This attribute isn't supported any more. Use TYPE instead");
itsAttr[Attrib::Distribution::LINE]
......@@ -3266,8 +3264,8 @@ void Distribution::setAttributes() {
"distribution list.", 1.0);
itsAttr[Attrib::Distribution::INPUTMOUNITS]
= Attributes::makeString("INPUTMOUNITS", "Tell OPAL what input units are for momentum."
" Currently \"NONE\" or \"EV\".", "");
= Attributes::makeUpperCaseString("INPUTMOUNITS", "Tell OPAL what input units are for momentum."
" Currently \"NONE\" or \"EV\".", "");
// Attributes for beam emission.
itsAttr[Attrib::Distribution::EMITTED]
......@@ -3277,8 +3275,8 @@ void Distribution::setAttributes() {
= Attributes::makeReal("EMISSIONSTEPS", "Number of time steps to use during emission.",
1);
itsAttr[Attrib::Distribution::EMISSIONMODEL]
= Attributes::makeString("EMISSIONMODEL", "Model used to emit electrons from a "
"photocathode.", "None");
= Attributes::makeUpperCaseString("EMISSIONMODEL", "Model used to emit electrons from a "
"photocathode.", "None");
itsAttr[Attrib::Distribution::EKIN]
= Attributes::makeReal("EKIN", "Kinetic energy used in ASTRA thermal emittance "