[0275ad] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[d103d3] | 4 | * Copyright (C) 2010-2011 University of Bonn. All rights reserved.
|
---|
[0275ad] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * RandomNumberDistribution_Parameters.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Jan 6, 2011
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 21 |
|
---|
| 22 | #include <iostream>
|
---|
| 23 | #include <boost/tokenizer.hpp>
|
---|
| 24 | #include <string>
|
---|
| 25 | #include <map>
|
---|
| 26 |
|
---|
| 27 | #include "CodePatterns/Assert.hpp"
|
---|
| 28 | #include "CodePatterns/Log.hpp"
|
---|
| 29 | #include "CodePatterns/Verbose.hpp"
|
---|
| 30 |
|
---|
| 31 | #include "RandomNumberDistribution_Parameters.hpp"
|
---|
| 32 |
|
---|
| 33 | #include "RandomNumberDistribution.hpp"
|
---|
| 34 |
|
---|
| 35 | RandomNumberDistribution_Parameters::ParamNames RandomNumberDistribution_Parameters::params;
|
---|
| 36 | const double RandomNumberDistribution_Parameters::noset_value = -1.;
|
---|
| 37 |
|
---|
| 38 | RandomNumberDistribution_Parameters::RandomNumberDistribution_Parameters() :
|
---|
| 39 | min(noset_value),
|
---|
| 40 | max(noset_value),
|
---|
| 41 | p(noset_value),
|
---|
| 42 | t(noset_value),
|
---|
| 43 | median(noset_value),
|
---|
| 44 | sigma(noset_value),
|
---|
| 45 | alpha(noset_value),
|
---|
| 46 | mean(noset_value),
|
---|
| 47 | a(noset_value),
|
---|
| 48 | b(noset_value),
|
---|
| 49 | c(noset_value),
|
---|
| 50 | lambda(noset_value)
|
---|
| 51 | {
|
---|
| 52 | // if param names map not yet filled, do so
|
---|
| 53 | if (params.begin() == params.end()) {
|
---|
| 54 | params["min"] = param_min;
|
---|
| 55 | params["max"] = param_max;
|
---|
| 56 | params["p"] = param_p;
|
---|
| 57 | params["t"] = param_t;
|
---|
| 58 | params["median"] = param_median;
|
---|
| 59 | params["sigma"] = param_sigma;
|
---|
| 60 | params["alpha"] = param_alpha;
|
---|
| 61 | params["mean"] = param_mean;
|
---|
| 62 | params["a"] = param_a;
|
---|
| 63 | params["b"] = param_b;
|
---|
| 64 | params["c"] = param_c;
|
---|
| 65 | params["lambda"] = param_lambda;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | RandomNumberDistribution_Parameters::RandomNumberDistribution_Parameters(
|
---|
| 70 | const RandomNumberDistribution_Parameters &_ref)
|
---|
| 71 | {
|
---|
| 72 | // check self assignment
|
---|
| 73 | if (&_ref != this) {
|
---|
| 74 | min = _ref.min;
|
---|
| 75 | max = _ref.max;
|
---|
| 76 | p = _ref.p;
|
---|
| 77 | p = _ref.t;
|
---|
| 78 | median = _ref.median;
|
---|
| 79 | sigma = _ref.sigma;
|
---|
| 80 | alpha = _ref.alpha;
|
---|
| 81 | mean = _ref.mean;
|
---|
| 82 | a = _ref.a;
|
---|
| 83 | b = _ref.b;
|
---|
| 84 | c = _ref.c;
|
---|
| 85 | lambda = _ref.lambda;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | RandomNumberDistribution_Parameters::~RandomNumberDistribution_Parameters()
|
---|
| 90 | {}
|
---|
| 91 |
|
---|
| 92 | void RandomNumberDistribution_Parameters::getParameters(const RandomNumberDistribution* const _distribution)
|
---|
| 93 | {
|
---|
| 94 | min = _distribution->min();
|
---|
| 95 | max = _distribution->max();
|
---|
| 96 | p = _distribution->p();
|
---|
| 97 | t = _distribution->t();
|
---|
| 98 | median = _distribution->median();
|
---|
| 99 | sigma = _distribution->sigma();
|
---|
| 100 | alpha = _distribution->alpha();
|
---|
| 101 | mean = _distribution->mean();
|
---|
| 102 | a = _distribution->a();
|
---|
| 103 | b = _distribution->b();
|
---|
| 104 | c = _distribution->c();
|
---|
| 105 | lambda = _distribution->lambda();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void RandomNumberDistribution_Parameters::update(const RandomNumberDistribution_Parameters &_params)
|
---|
| 109 | {
|
---|
| 110 | min = _params.min != noset_value ? _params.min : min;
|
---|
| 111 | max = _params.max != noset_value ? _params.max : max;
|
---|
| 112 | p = _params.p != noset_value ? _params.p : p;
|
---|
| 113 | p = _params.t != noset_value ? _params.t : t;
|
---|
| 114 | median = _params.median != noset_value ? _params.median : median;
|
---|
| 115 | sigma = _params.sigma != noset_value ? _params.sigma : sigma;
|
---|
| 116 | alpha = _params.alpha != noset_value ? _params.alpha : alpha;
|
---|
| 117 | mean = _params.mean != noset_value ? _params.mean : mean;
|
---|
| 118 | a = _params.a != noset_value ? _params.a : a;
|
---|
| 119 | b = _params.b != noset_value ? _params.b : b;
|
---|
| 120 | c = _params.c != noset_value ? _params.c : c;
|
---|
| 121 | lambda = _params.lambda != noset_value ? _params.lambda : lambda;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | bool RandomNumberDistribution_Parameters::isDefault() const
|
---|
| 125 | {
|
---|
| 126 | bool status = true;
|
---|
| 127 | status = status && (min == noset_value);
|
---|
| 128 | status = status && (max == noset_value);
|
---|
| 129 | status = status && (p == noset_value);
|
---|
| 130 | status = status && (t == noset_value);
|
---|
| 131 | status = status && (median == noset_value);
|
---|
| 132 | status = status && (sigma == noset_value);
|
---|
| 133 | status = status && (alpha == noset_value);
|
---|
| 134 | status = status && (mean == noset_value);
|
---|
| 135 | status = status && (a == noset_value);
|
---|
| 136 | status = status && (b == noset_value);
|
---|
| 137 | status = status && (c == noset_value);
|
---|
| 138 | status = status && (lambda == noset_value);
|
---|
| 139 | return status;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | std::ostream & operator << (std::ostream& ost, const RandomNumberDistribution_Parameters ¶ms)
|
---|
| 143 | {
|
---|
| 144 | std::ostringstream output;
|
---|
| 145 | output << "min=" << params.min << ";";
|
---|
| 146 | output << "max=" << params.max << ";";
|
---|
| 147 | output << "p=" << params.p << ";";
|
---|
| 148 | output << "t=" << params.t << ";";
|
---|
| 149 | output << "median=" << params.median << ";";
|
---|
| 150 | output << "sigma=" << params.sigma << ";";
|
---|
| 151 | output << "alpha=" << params.alpha << ";";
|
---|
| 152 | output << "mean=" << params.mean << ";";
|
---|
| 153 | output << "a=" << params.a << ";";
|
---|
| 154 | output << "b=" << params.b << ";";
|
---|
| 155 | output << "c=" << params.c << ";";
|
---|
| 156 | output << "lambda=" << params.lambda << ";" << std::endl;
|
---|
| 157 | ost << output.str();
|
---|
| 158 | return ost;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | // TODO: These should throw exceptions such that invalid user entry can be detected.
|
---|
| 162 | std::istream & operator >> (std::istream& ist, RandomNumberDistribution_Parameters ¶ms)
|
---|
| 163 | {
|
---|
| 164 | typedef boost::tokenizer<boost::char_separator<char> >
|
---|
| 165 | tokenizer;
|
---|
| 166 | boost::char_separator<char> semicolonsep(";");
|
---|
| 167 | boost::char_separator<char> equalitysep("=");
|
---|
| 168 | std::string line;
|
---|
| 169 | double tmp;
|
---|
| 170 | std::getline( ist, line );
|
---|
[47d041] | 171 | //LOG(0, "INFO: full line of parameters is '" << line << "'");
|
---|
[0275ad] | 172 | tokenizer tokens(line, semicolonsep);
|
---|
| 173 | ASSERT(tokens.begin() != tokens.end(),
|
---|
| 174 | "operator<< on RandomNumberDistribution_Parameters - empty string, need at least ';'!");
|
---|
| 175 | for (tokenizer::iterator tok_iter = tokens.begin();
|
---|
| 176 | tok_iter != tokens.end(); ++tok_iter) {
|
---|
| 177 | tokenizer paramtokens(*tok_iter, equalitysep);
|
---|
| 178 | if (paramtokens.begin() != paramtokens.end()) {
|
---|
| 179 | tokenizer::iterator tok_paramiter = paramtokens.begin();
|
---|
| 180 | tokenizer::iterator tok_valueiter = tok_paramiter;
|
---|
| 181 | tokenizer::iterator tok_checkiter = ++tok_valueiter;
|
---|
[c14c78] | 182 | ASSERT(tok_paramiter != paramtokens.end(),
|
---|
| 183 | "operator<< on RandomNumberDistribution_Parameters - missing value before '='!");
|
---|
| 184 | ASSERT(tok_valueiter != paramtokens.end(),
|
---|
| 185 | "operator<< on RandomNumberDistribution_Parameters - missing value after '='!");
|
---|
[0275ad] | 186 | ++tok_checkiter;
|
---|
[c14c78] | 187 | ASSERT(tok_checkiter == paramtokens.end(),
|
---|
[0275ad] | 188 | "operator<< on RandomNumberDistribution_Parameters - still tokens before ';'!");
|
---|
[47d041] | 189 | LOG(0, "INFO: Token pair is " << *tok_paramiter << "," << *tok_valueiter);
|
---|
[0275ad] | 190 | std::stringstream value(*tok_valueiter);
|
---|
| 191 | value >> tmp;
|
---|
| 192 | if ((RandomNumberDistribution_Parameters::params.count(*tok_paramiter) != 0)
|
---|
| 193 | && (tmp != RandomNumberDistribution_Parameters::noset_value)) {
|
---|
| 194 | switch (RandomNumberDistribution_Parameters::params[*tok_paramiter]) {
|
---|
| 195 | case (RandomNumberDistribution_Parameters::param_min):
|
---|
| 196 | params.min = tmp;
|
---|
| 197 | break;
|
---|
| 198 | case (RandomNumberDistribution_Parameters::param_max):
|
---|
| 199 | params.max = tmp;
|
---|
| 200 | break;
|
---|
| 201 | case (RandomNumberDistribution_Parameters::param_p):
|
---|
| 202 | params.p = tmp;
|
---|
| 203 | break;
|
---|
| 204 | case (RandomNumberDistribution_Parameters::param_t):
|
---|
| 205 | params.t = tmp;
|
---|
| 206 | break;
|
---|
| 207 | case (RandomNumberDistribution_Parameters::param_median):
|
---|
| 208 | params.median = tmp;
|
---|
| 209 | break;
|
---|
| 210 | case (RandomNumberDistribution_Parameters::param_sigma):
|
---|
| 211 | params.sigma = tmp;
|
---|
| 212 | break;
|
---|
| 213 | case (RandomNumberDistribution_Parameters::param_alpha):
|
---|
| 214 | params.alpha = tmp;
|
---|
| 215 | break;
|
---|
| 216 | case (RandomNumberDistribution_Parameters::param_mean):
|
---|
| 217 | params.mean = tmp;
|
---|
| 218 | break;
|
---|
| 219 | case (RandomNumberDistribution_Parameters::param_a):
|
---|
| 220 | params.a = tmp;
|
---|
| 221 | break;
|
---|
| 222 | case (RandomNumberDistribution_Parameters::param_b):
|
---|
| 223 | params.b = tmp;
|
---|
| 224 | break;
|
---|
| 225 | case (RandomNumberDistribution_Parameters::param_c):
|
---|
| 226 | params.c = tmp;
|
---|
| 227 | break;
|
---|
| 228 | case (RandomNumberDistribution_Parameters::param_lambda):
|
---|
| 229 | params.lambda = tmp;
|
---|
| 230 | break;
|
---|
| 231 | default:
|
---|
| 232 | std::cerr << "This is weird, we cannot get here!" << std::endl;
|
---|
| 233 | }
|
---|
[47d041] | 234 | LOG(1, "ACCEPT: Value for token " << *tok_paramiter << " set.");
|
---|
[0275ad] | 235 | } else {
|
---|
[47d041] | 236 | LOG(1, "REJECT: Value for token " << *tok_paramiter << " not specified.");
|
---|
[0275ad] | 237 | }
|
---|
| 238 | } else {
|
---|
| 239 | ist.setstate(std::ios::eofbit);
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | return ist;
|
---|
| 243 | }
|
---|