/usr/include/liggghts/settings.h is in libliggghts-dev 3.0.3+repack-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | /* ----------------------------------------------------------------------
LIGGGHTS - LAMMPS Improved for General Granular and Granular Heat
Transfer Simulations
LIGGGHTS is part of the CFDEMproject
www.liggghts.com | www.cfdem.com
Christoph Kloss, christoph.kloss@cfdem.com
Copyright 2009-2012 JKU Linz
Copyright 2012- DCS Computing GmbH, Linz
LIGGGHTS is based on LAMMPS
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
This software is distributed under the GNU General Public License.
See the README file in the top-level directory.
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
Contributing authors:
Christoph Kloss (JKU Linz, DCS Computing GmbH, Linz)
Richard Berger (JKU Linz)
------------------------------------------------------------------------- */
#ifndef SETTINGS_H_
#define SETTINGS_H_
#include "pointers.h"
#include <string>
#include <set>
#include <map>
#include <sstream>
using namespace LAMMPS_NS;
using namespace std;
template<typename T>
class ValuePropagator {
public:
void registerTarget(T & target)
{
targets.insert(&target);
target = currentValue;
}
void setValue(T value)
{
currentValue = value;
for(typename set<T*>::iterator it = targets.begin(); it != targets.end(); ++it) {
*(*it) = value;
}
}
T getValue() const {
return currentValue;
}
private:
T currentValue;
set<T*> targets;
};
class Setting {
public:
Setting(string name, int num_params) : name(name), num_params(num_params)
{
}
virtual ~Setting(){}
// subclasses implement this method and return the amount of arguments have
// been consumed.
virtual int parseArguments(char ** args) = 0;
virtual void print_value(FILE * out) = 0;
string name;
int num_params;
string error_message;
};
template<typename T>
class EnumSetting : public Setting
{
public:
typedef T value_type;
EnumSetting(string name) : Setting(name, 1)
{
}
virtual ~EnumSetting(){}
void addOption(string option, T value)
{
options[option] = value;
}
void setDefault(string option){
current.setValue(options[option]);
}
void registerTarget(T & target) {
current.registerTarget(target);
}
int parseArguments(char ** args) {
if(name != args[0]) return 0; // argument not consumed
string selected(args[1]);
if(options.find(selected) != options.end()){
current.setValue(options[selected]);
return 2; // argument consumed
} else {
stringstream ss;
ss << "while parsing '" << name << "' argument: ";
ss << "unknown option or wrong keyword order: '" << args[1] << "'";
error_message = ss.str();
}
return -1; // error while parsing argument
}
virtual void print_value(FILE* out) {
T value = current.getValue();
for(typename map<string, T>::iterator it = options.begin(); it != options.end(); ++it) {
if(it->second == value) {
fprintf(out, "%s", it->first.c_str());
return;
}
}
fprintf(out, "BAD_VALUE");
}
private:
ValuePropagator<T> current;
map<string, T> options;
};
class DoubleSetting : public Setting
{
public:
typedef double value_type;
DoubleSetting(string name, double default_value) : Setting(name, 1)
{
setDefault(default_value);
}
virtual ~DoubleSetting(){}
void setDefault(double d){
current.setValue(d);
}
void registerTarget(double & target) {
current.registerTarget(target);
}
int parseArguments(char ** args) {
if(name != args[0]) return 0;
current.setValue(atof(args[1]));
return 2;
}
void print_value(FILE* out) {
fprintf(out, "%g", current.getValue());
}
private:
ValuePropagator<double> current;
};
class OnOffSetting : public EnumSetting<bool> {
public:
OnOffSetting(string name, bool default_value) : EnumSetting<bool>(name)
{
addOption("off", false);
addOption("on", true);
if(default_value) setDefault("on");
else setDefault("off");
}
virtual ~OnOffSetting(){}
};
class YesNoSetting : public EnumSetting<bool> {
public:
YesNoSetting(string name, bool default_value) : EnumSetting<bool>(name)
{
addOption("no", false);
addOption("yes", true);
if(default_value) setDefault("yes");
else setDefault("no");
}
virtual ~YesNoSetting(){}
};
class Settings : protected Pointers
{
typedef map<string, Setting*> SettingMap;
SettingMap settings;
template<typename SettingType>
void registerSetting(string name, typename SettingType::value_type & variable, typename SettingType::value_type default_value) {
if(settings.find(name) == settings.end()) {
settings[name] = new SettingType(name, default_value);
}
SettingType * setting = dynamic_cast<SettingType*>(settings[name]);
if(setting) setting->registerTarget(variable);
}
public:
std::string error_message;
Settings(LAMMPS * lmp) : Pointers(lmp) {}
~Settings() {
for(SettingMap::iterator it = settings.begin(); it != settings.end(); ++it) {
delete it->second;
}
}
void registerOnOff(string name, bool & variable, bool default_value = false)
{
registerSetting<OnOffSetting>(name, variable, default_value);
}
void registerYesNo(string name, bool & variable, bool default_value = false) {
registerSetting<YesNoSetting>(name, variable, default_value);
}
void registerDoubleSetting(string name, double & variable, double default_value = 0.0)
{
registerSetting<DoubleSetting>(name, variable, default_value);
}
bool parseArguments(int nargs, char ** args) {
bool found = false;
int remaining = nargs;
char ** remaining_args = args;
int consumed = 0;
while(remaining > 0) {
found = false;
for(SettingMap::iterator it = settings.begin(); it != settings.end(); ++it) {
consumed = it->second->parseArguments(remaining_args);
if(consumed > 0) {
found = true;
break;
} else if(consumed < 0) {
error_message = it->second->error_message;
return false;
}
}
if(found) {
remaining -= consumed;
remaining_args = &remaining_args[consumed];
} else {
stringstream ss;
ss << "Unknown argument or wrong keyword order: '" << remaining_args[0] << "'";
error_message = ss.str();
return false;
}
}
return true;
}
void print_all(FILE * out)
{
for(SettingMap::iterator it = settings.begin(); it != settings.end(); ++it) {
fprintf(out, " %s = ", it->first.c_str());
it->second->print_value(out);
fprintf(out, "\n");
}
}
};
#endif /* SETTINGS_H_ */
|