1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * RandomNumberEngine_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 "RandomNumberEngine_Parameters.hpp"
|
---|
32 |
|
---|
33 | #include "RandomNumberEngine.hpp"
|
---|
34 |
|
---|
35 | RandomNumberEngine_Parameters::ParamNames RandomNumberEngine_Parameters::params;
|
---|
36 | const double RandomNumberEngine_Parameters::noset_value = -1.;
|
---|
37 |
|
---|
38 | RandomNumberEngine_Parameters::RandomNumberEngine_Parameters() :
|
---|
39 | seed(noset_value)
|
---|
40 | {
|
---|
41 | // if param names map not yet filled, do so
|
---|
42 | if (params.begin() == params.end()) {
|
---|
43 | params["seed"] = param_seed;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | RandomNumberEngine_Parameters::RandomNumberEngine_Parameters(
|
---|
48 | const RandomNumberEngine_Parameters &_ref)
|
---|
49 | {
|
---|
50 | // check self assignment
|
---|
51 | if (&_ref != this) {
|
---|
52 | seed = _ref.seed;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | RandomNumberEngine_Parameters::~RandomNumberEngine_Parameters()
|
---|
57 | {}
|
---|
58 |
|
---|
59 | void RandomNumberEngine_Parameters::getParameters(const RandomNumberEngine* const _engine)
|
---|
60 | {
|
---|
61 | seed = _engine->getseed();
|
---|
62 | }
|
---|
63 |
|
---|
64 | void RandomNumberEngine_Parameters::update(const RandomNumberEngine_Parameters &_params)
|
---|
65 | {
|
---|
66 | seed = _params.seed != noset_value ? _params.seed : seed;
|
---|
67 | }
|
---|
68 |
|
---|
69 | bool RandomNumberEngine_Parameters::isDefault() const
|
---|
70 | {
|
---|
71 | bool status = true;
|
---|
72 | status = status && (seed == noset_value);
|
---|
73 | return status;
|
---|
74 | }
|
---|
75 |
|
---|
76 | std::ostream & operator << (std::ostream& ost, const RandomNumberEngine_Parameters ¶ms)
|
---|
77 | {
|
---|
78 | std::ostringstream output;
|
---|
79 | output << "seed=" << params.seed << ";";
|
---|
80 | ost << output.str();
|
---|
81 | return ost;
|
---|
82 | }
|
---|
83 |
|
---|
84 | // TODO: These should throw exceptions such that invalid user entry can be detected.
|
---|
85 | std::istream & operator >> (std::istream& ist, RandomNumberEngine_Parameters ¶ms)
|
---|
86 | {
|
---|
87 | typedef boost::tokenizer<boost::char_separator<char> >
|
---|
88 | tokenizer;
|
---|
89 | boost::char_separator<char> semicolonsep(";");
|
---|
90 | boost::char_separator<char> equalitysep("=");
|
---|
91 | std::string line;
|
---|
92 | double tmp;
|
---|
93 | std::getline( ist, line );
|
---|
94 | //LOG(0, "INFO: full line of parameters is '" << line << "'");
|
---|
95 | tokenizer tokens(line, semicolonsep);
|
---|
96 | ASSERT(tokens.begin() != tokens.end(),
|
---|
97 | "operator<< on RandomNumberEngine_Parameters - empty string, need at least ';'!");
|
---|
98 | for (tokenizer::iterator tok_iter = tokens.begin();
|
---|
99 | tok_iter != tokens.end(); ++tok_iter) {
|
---|
100 | tokenizer paramtokens(*tok_iter, equalitysep);
|
---|
101 | if (paramtokens.begin() != paramtokens.end()) {
|
---|
102 | tokenizer::iterator tok_paramiter = paramtokens.begin();
|
---|
103 | tokenizer::iterator tok_valueiter = tok_paramiter;
|
---|
104 | tokenizer::iterator tok_checkiter = ++tok_valueiter;
|
---|
105 | ASSERT(tok_paramiter != paramtokens.end(),
|
---|
106 | "operator<< on RandomNumberEngine_Parameters - missing value before '=' in token"
|
---|
107 | +*tok_iter+"!");
|
---|
108 | ASSERT(tok_valueiter != paramtokens.end(),
|
---|
109 | "operator<< on RandomNumberEngine_Parameters - missing value after '=' in token"
|
---|
110 | +*tok_iter+"!");
|
---|
111 | ++tok_checkiter;
|
---|
112 | ASSERT(tok_checkiter == paramtokens.end(),
|
---|
113 | "operator<< on RandomNumberEngine_Parameters - still tokens before ';' in token"
|
---|
114 | +*tok_iter+": "+*tok_checkiter+"!");
|
---|
115 | LOG(0, "INFO: Token pair is " << *tok_paramiter << "," << *tok_valueiter);
|
---|
116 | std::stringstream value(*tok_valueiter);
|
---|
117 | value >> tmp;
|
---|
118 | if ((RandomNumberEngine_Parameters::params.count(*tok_paramiter) != 0)
|
---|
119 | && (tmp != RandomNumberEngine_Parameters::noset_value)) {
|
---|
120 | switch (RandomNumberEngine_Parameters::params[*tok_paramiter]) {
|
---|
121 | case (RandomNumberEngine_Parameters::param_seed):
|
---|
122 | params.seed = tmp;
|
---|
123 | break;
|
---|
124 | default:
|
---|
125 | std::cerr << "This is weird, we cannot get here!" << std::endl;
|
---|
126 | }
|
---|
127 | LOG(1, "ACCEPT: Value for token " << *tok_paramiter << " set.");
|
---|
128 | } else {
|
---|
129 | LOG(1, "REJECT: Value for token " << *tok_paramiter << " not specified.");
|
---|
130 | }
|
---|
131 | } else {
|
---|
132 | ist.setstate(std::ios::eofbit);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | return ist;
|
---|
136 | }
|
---|