1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | *
|
---|
6 | *
|
---|
7 | * This file is part of MoleCuilder.
|
---|
8 | *
|
---|
9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * RandomNumberEngine_Parameters.cpp
|
---|
25 | *
|
---|
26 | * Created on: Jan 6, 2011
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "CodePatterns/MemDebug.hpp"
|
---|
36 |
|
---|
37 | #include <iostream>
|
---|
38 | #include <boost/tokenizer.hpp>
|
---|
39 | #include <string>
|
---|
40 | #include <map>
|
---|
41 |
|
---|
42 | #include "CodePatterns/Assert.hpp"
|
---|
43 | #include "CodePatterns/Log.hpp"
|
---|
44 | #include "CodePatterns/Verbose.hpp"
|
---|
45 |
|
---|
46 | #include "RandomNumberEngine_Parameters.hpp"
|
---|
47 |
|
---|
48 | #include "RandomNumberEngine.hpp"
|
---|
49 |
|
---|
50 | RandomNumberEngine_Parameters::ParamNames RandomNumberEngine_Parameters::params;
|
---|
51 | const double RandomNumberEngine_Parameters::noset_value = -1.;
|
---|
52 |
|
---|
53 | RandomNumberEngine_Parameters::RandomNumberEngine_Parameters() :
|
---|
54 | seed(noset_value)
|
---|
55 | {
|
---|
56 | // if param names map not yet filled, do so
|
---|
57 | if (params.begin() == params.end()) {
|
---|
58 | params["seed"] = param_seed;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | RandomNumberEngine_Parameters::RandomNumberEngine_Parameters(
|
---|
63 | const RandomNumberEngine_Parameters &_ref)
|
---|
64 | {
|
---|
65 | // check self assignment
|
---|
66 | if (&_ref != this) {
|
---|
67 | seed = _ref.seed;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | RandomNumberEngine_Parameters::~RandomNumberEngine_Parameters()
|
---|
72 | {}
|
---|
73 |
|
---|
74 | void RandomNumberEngine_Parameters::getParameters(const RandomNumberEngine* const _engine)
|
---|
75 | {
|
---|
76 | seed = _engine->getseed();
|
---|
77 | }
|
---|
78 |
|
---|
79 | void RandomNumberEngine_Parameters::update(const RandomNumberEngine_Parameters &_params)
|
---|
80 | {
|
---|
81 | seed = _params.seed != noset_value ? _params.seed : seed;
|
---|
82 | }
|
---|
83 |
|
---|
84 | bool RandomNumberEngine_Parameters::isDefault() const
|
---|
85 | {
|
---|
86 | bool status = true;
|
---|
87 | status = status && (seed == noset_value);
|
---|
88 | return status;
|
---|
89 | }
|
---|
90 |
|
---|
91 | std::ostream & operator << (std::ostream& ost, const RandomNumberEngine_Parameters ¶ms)
|
---|
92 | {
|
---|
93 | std::ostringstream output;
|
---|
94 | output << "seed=" << params.seed << ";";
|
---|
95 | ost << output.str();
|
---|
96 | return ost;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // TODO: These should throw exceptions such that invalid user entry can be detected.
|
---|
100 | std::istream & operator >> (std::istream& ist, RandomNumberEngine_Parameters ¶ms)
|
---|
101 | {
|
---|
102 | typedef boost::tokenizer<boost::char_separator<char> >
|
---|
103 | tokenizer;
|
---|
104 | boost::char_separator<char> semicolonsep(";");
|
---|
105 | boost::char_separator<char> equalitysep("=");
|
---|
106 | std::string line;
|
---|
107 | double tmp;
|
---|
108 | std::getline( ist, line );
|
---|
109 | //LOG(0, "INFO: full line of parameters is '" << line << "'");
|
---|
110 | tokenizer tokens(line, semicolonsep);
|
---|
111 | ASSERT(tokens.begin() != tokens.end(),
|
---|
112 | "operator<< on RandomNumberEngine_Parameters - empty string, need at least ';'!");
|
---|
113 | for (tokenizer::iterator tok_iter = tokens.begin();
|
---|
114 | tok_iter != tokens.end(); ++tok_iter) {
|
---|
115 | tokenizer paramtokens(*tok_iter, equalitysep);
|
---|
116 | if (paramtokens.begin() != paramtokens.end()) {
|
---|
117 | tokenizer::iterator tok_paramiter = paramtokens.begin();
|
---|
118 | tokenizer::iterator tok_valueiter = tok_paramiter;
|
---|
119 | tokenizer::iterator tok_checkiter = ++tok_valueiter;
|
---|
120 | ASSERT(tok_paramiter != paramtokens.end(),
|
---|
121 | "operator<< on RandomNumberEngine_Parameters - missing value before '=' in token"
|
---|
122 | +*tok_iter+"!");
|
---|
123 | ASSERT(tok_valueiter != paramtokens.end(),
|
---|
124 | "operator<< on RandomNumberEngine_Parameters - missing value after '=' in token"
|
---|
125 | +*tok_iter+"!");
|
---|
126 | ++tok_checkiter;
|
---|
127 | ASSERT(tok_checkiter == paramtokens.end(),
|
---|
128 | "operator<< on RandomNumberEngine_Parameters - still tokens before ';' in token"
|
---|
129 | +*tok_iter+": "+*tok_checkiter+"!");
|
---|
130 | LOG(0, "INFO: Token pair is " << *tok_paramiter << "," << *tok_valueiter);
|
---|
131 | std::stringstream value(*tok_valueiter);
|
---|
132 | value >> tmp;
|
---|
133 | if ((RandomNumberEngine_Parameters::params.count(*tok_paramiter) != 0)
|
---|
134 | && (tmp != RandomNumberEngine_Parameters::noset_value)) {
|
---|
135 | switch (RandomNumberEngine_Parameters::params[*tok_paramiter]) {
|
---|
136 | case (RandomNumberEngine_Parameters::param_seed):
|
---|
137 | params.seed = tmp;
|
---|
138 | break;
|
---|
139 | default:
|
---|
140 | std::cerr << "This is weird, we cannot get here!" << std::endl;
|
---|
141 | }
|
---|
142 | LOG(1, "ACCEPT: Value for token " << *tok_paramiter << " set.");
|
---|
143 | } else {
|
---|
144 | LOG(1, "REJECT: Value for token " << *tok_paramiter << " not specified.");
|
---|
145 | }
|
---|
146 | } else {
|
---|
147 | ist.setstate(std::ios::eofbit);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | return ist;
|
---|
151 | }
|
---|