1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Box_BoundaryConditions.cpp
|
---|
10 | *
|
---|
11 | * Created on: Jan 2, 2012
|
---|
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 <boost/tokenizer.hpp>
|
---|
23 | #include <boost/algorithm/string/trim.hpp>
|
---|
24 | #include <iostream>
|
---|
25 | #include <string>
|
---|
26 |
|
---|
27 | #include "Box_BoundaryConditions.hpp"
|
---|
28 |
|
---|
29 | #include "CodePatterns/Assert.hpp"
|
---|
30 | #include "CodePatterns/Log.hpp"
|
---|
31 | #include "CodePatterns/Range.hpp"
|
---|
32 | #include "LinearAlgebra/defs.hpp"
|
---|
33 |
|
---|
34 | /** Constructor of class BCContainer.
|
---|
35 | *
|
---|
36 | * Fills member BCContainer::ConverterBiMap and
|
---|
37 | * BCContainer::ReConverterBiMap.
|
---|
38 | */
|
---|
39 | BoundaryConditions::BCContainer::BCContainer()
|
---|
40 | {
|
---|
41 | // fill static conversion maps when empty
|
---|
42 | ASSERT(ConverterBiMap.size() == ReConverterBiMap.size(),
|
---|
43 | "BCContainer::BoundaryConditions() - internal string<->enum maps differ in size.");
|
---|
44 | if (ConverterBiMap.empty()) {
|
---|
45 | // hither
|
---|
46 | ConverterBiMap.insert( std::make_pair(BoundaryConditions::Wrap, std::string("Wrap")) );
|
---|
47 | ConverterBiMap.insert( std::make_pair(BoundaryConditions::Bounce, std::string("Bounce")) );
|
---|
48 | ConverterBiMap.insert( std::make_pair(BoundaryConditions::Ignore, std::string("Ignore")) );
|
---|
49 | // thither
|
---|
50 | ReConverterBiMap.insert( std::make_pair(std::string("Wrap"), BoundaryConditions::Wrap) );
|
---|
51 | ReConverterBiMap.insert( std::make_pair(std::string("Bounce"), BoundaryConditions::Bounce) );
|
---|
52 | ReConverterBiMap.insert( std::make_pair(std::string("Ignore"), BoundaryConditions::Ignore) );
|
---|
53 | }
|
---|
54 | ASSERT(ConverterBiMap.size() == (size_t)(BoundaryConditions::MAX_BoundaryCondition_t),
|
---|
55 | "BCContainer::BoundaryConditions() - internal string<->enum map has not the same number of elements "
|
---|
56 | +toString(ConverterBiMap.size())+" as the enum "
|
---|
57 | +toString((size_t)(MAX_BoundaryCondition_t))+".");
|
---|
58 |
|
---|
59 | // fill us with default values
|
---|
60 | resize(NDIM, BoundaryConditions::Wrap);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** Destructor of class BCContainer.
|
---|
64 | *
|
---|
65 | */
|
---|
66 | BoundaryConditions::BCContainer::~BCContainer()
|
---|
67 | {
|
---|
68 | ConverterBiMap.clear();
|
---|
69 | ReConverterBiMap.clear();
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Getter for a all boundary conditions.
|
---|
73 | *
|
---|
74 | * @return vector with the boundary conditions
|
---|
75 | */
|
---|
76 | const BoundaryConditions::Conditions_t & BoundaryConditions::BCContainer::get() const
|
---|
77 | {
|
---|
78 | return (*this);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Getter for the boundary condition of a specific axis.
|
---|
82 | *
|
---|
83 | * @param index axis
|
---|
84 | * @return boundary condition of this axis.
|
---|
85 | */
|
---|
86 | const BoundaryConditions::BoundaryCondition_t BoundaryConditions::BCContainer::get(size_t index) const
|
---|
87 | {
|
---|
88 | #ifndef NDEBUG
|
---|
89 | range<size_t> AllowedRange(0, this->size());
|
---|
90 | ASSERT(AllowedRange.isInRange(index),
|
---|
91 | "BCContainer::get() - index "+toString(index)+" out of bounds "
|
---|
92 | +toString(AllowedRange)+".");
|
---|
93 | #endif
|
---|
94 | return operator[](index);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Setter for all boundary conditions.
|
---|
98 | *
|
---|
99 | * @param _conditions vector with all conditions to set.
|
---|
100 | */
|
---|
101 | void BoundaryConditions::BCContainer::set(const BoundaryConditions::Conditions_t &_conditions)
|
---|
102 | {
|
---|
103 | ASSERT(_conditions.size() == this->size(),
|
---|
104 | "BCContainer::set() - given vector of conditions and this have unequal size.");
|
---|
105 | for (size_t i = 0; i < _conditions.size(); ++i)
|
---|
106 | (*this)[i] = _conditions[i];
|
---|
107 | }
|
---|
108 |
|
---|
109 | void BoundaryConditions::BCContainer::set(const std::vector< std::string > &_conditions)
|
---|
110 | {
|
---|
111 | BoundaryConditions::Conditions_t newconditions;
|
---|
112 | for (std::vector< std::string >::const_iterator iter = _conditions.begin();
|
---|
113 | iter != _conditions.end(); ++iter)
|
---|
114 | newconditions.push_back(getEnum(*iter));
|
---|
115 | set(newconditions);
|
---|
116 | }
|
---|
117 |
|
---|
118 | void BoundaryConditions::BCContainer::set(size_t index, const BoundaryConditions::BoundaryCondition_t _condition)
|
---|
119 | {
|
---|
120 | #ifndef NDEBUG
|
---|
121 | range<size_t> AllowedRange(0, this->size());
|
---|
122 | ASSERT(AllowedRange.isInRange(index),
|
---|
123 | "BCContainer::set() - index "+toString(index)+" out of bounds "
|
---|
124 | +toString(AllowedRange)+".");
|
---|
125 | #endif
|
---|
126 | (*this)[index] = _condition;
|
---|
127 | }
|
---|
128 |
|
---|
129 | void BoundaryConditions::BCContainer::set(size_t index, const std::string &_condition)
|
---|
130 | {
|
---|
131 | set(index, getEnum(_condition));
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Converter from enum to string.
|
---|
135 | *
|
---|
136 | * @param condition enum
|
---|
137 | * @return name of the num
|
---|
138 | */
|
---|
139 | const std::string & BoundaryConditions::BCContainer::getName(const BoundaryConditions::BoundaryCondition_t &condition) const
|
---|
140 | {
|
---|
141 | ASSERT( ((condition >= (BoundaryConditions::BoundaryCondition_t)0) && (condition < BoundaryConditions::MAX_BoundaryCondition_t)),
|
---|
142 | "BCContainer::getName() - enum "+toString(condition)+" is not a valid enum.");
|
---|
143 | EnumToStringMap::const_iterator iter = ConverterBiMap.find(condition);
|
---|
144 | ASSERT(iter != ConverterBiMap.end(),
|
---|
145 | "BCContainer::getName() - enum "+toString(condition)+" is unknown for any enum name.");
|
---|
146 | return iter->second;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /** Converter from string to enum.
|
---|
150 | *
|
---|
151 | * @param condition name of the condition
|
---|
152 | * @return enum index
|
---|
153 | */
|
---|
154 | const BoundaryConditions::BoundaryCondition_t & BoundaryConditions::BCContainer::getEnum(const std::string &condition) const
|
---|
155 | {
|
---|
156 | StringToEnumMap::const_iterator iter = ReConverterBiMap.find(condition);
|
---|
157 | ASSERT( iter != ReConverterBiMap.end(),
|
---|
158 | "BCContainer::getEnum() - name "+condition+" is unknown for any enum.");
|
---|
159 | return iter->second;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Output routine for class BCContainer to stream.
|
---|
163 | *
|
---|
164 | * @param out stream to print to
|
---|
165 | * @param t instance of BCContainer.
|
---|
166 | * @return stream for concatenation
|
---|
167 | */
|
---|
168 | std::ostream &operator<<(std::ostream &out, const BoundaryConditions::BCContainer &t)
|
---|
169 | {
|
---|
170 | for (BoundaryConditions::BCContainer::const_iterator iter = t.begin(); iter != t.end(); ++iter) {
|
---|
171 | if (iter != t.begin())
|
---|
172 | out << " ";
|
---|
173 | out << t.getName(*iter);
|
---|
174 | }
|
---|
175 | return out;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Input routine for class BCContainer from stream.
|
---|
179 | *
|
---|
180 | * We expect input as comma-separated list with NDIM values.
|
---|
181 | *
|
---|
182 | * @param in input stream
|
---|
183 | * @param t instance of BCContainer to set
|
---|
184 | * @return input stream for concatenation
|
---|
185 | */
|
---|
186 | std::istream &operator>>(std::istream &in, BoundaryConditions::BCContainer &t)
|
---|
187 | {
|
---|
188 | // read from stream till semicolon or end
|
---|
189 | std::stringbuf sbuf;
|
---|
190 | in.get(sbuf, ';');
|
---|
191 |
|
---|
192 | // tokenize
|
---|
193 | typedef boost::tokenizer< boost::char_separator<char> > tokens;
|
---|
194 | BoundaryConditions::Conditions_t conditions;
|
---|
195 | boost::char_separator<char> commasep(", ");
|
---|
196 | // it is imperative to copy the content of the stringbuffer, otherwise we'll
|
---|
197 | // get strange bug where 'Wrap' != 'Wrap' and so on.
|
---|
198 | std::string line(sbuf.str());
|
---|
199 | tokens tok(line, commasep);
|
---|
200 | for(tokens::iterator iter=tok.begin(); iter!=tok.end();++iter) {
|
---|
201 | // remove leading and trailing white space
|
---|
202 | std::string condition(*iter);
|
---|
203 | boost::trim(condition);
|
---|
204 | conditions.push_back(t.getEnum(condition));
|
---|
205 | }
|
---|
206 |
|
---|
207 | // set
|
---|
208 | t.set(conditions);
|
---|
209 |
|
---|
210 | return in;
|
---|
211 | }
|
---|