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(size_t index, const BoundaryConditions::BoundaryCondition_t _condition)
|
---|
110 | {
|
---|
111 | #ifndef NDEBUG
|
---|
112 | range<size_t> AllowedRange(0, this->size());
|
---|
113 | ASSERT(AllowedRange.isInRange(index),
|
---|
114 | "BCContainer::set() - index "+toString(index)+" out of bounds "
|
---|
115 | +toString(AllowedRange)+".");
|
---|
116 | #endif
|
---|
117 | (*this)[index] = _condition;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** Converter from enum to string.
|
---|
121 | *
|
---|
122 | * @param condition enum
|
---|
123 | * @return name of the num
|
---|
124 | */
|
---|
125 | const std::string & BoundaryConditions::BCContainer::getName(const BoundaryConditions::BoundaryCondition_t &condition) const
|
---|
126 | {
|
---|
127 | ASSERT( ((condition >= (BoundaryConditions::BoundaryCondition_t)0) && (condition < BoundaryConditions::MAX_BoundaryCondition_t)),
|
---|
128 | "BCContainer::getName() - enum "+toString(condition)+" is not a valid enum.");
|
---|
129 | EnumToStringMap::const_iterator iter = ConverterBiMap.find(condition);
|
---|
130 | ASSERT(iter != ConverterBiMap.end(),
|
---|
131 | "BCContainer::getName() - enum "+toString(condition)+" is unknown for any enum name.");
|
---|
132 | return iter->second;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Converter from string to enum.
|
---|
136 | *
|
---|
137 | * @param condition name of the condition
|
---|
138 | * @return enum index
|
---|
139 | */
|
---|
140 | const BoundaryConditions::BoundaryCondition_t & BoundaryConditions::BCContainer::getEnum(const std::string &condition) const
|
---|
141 | {
|
---|
142 | StringToEnumMap::const_iterator iter = ReConverterBiMap.find(condition);
|
---|
143 | ASSERT( iter != ReConverterBiMap.end(),
|
---|
144 | "BCContainer::getEnum() - name "+condition+" is unknown for any enum.");
|
---|
145 | return iter->second;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Output routine for class BCContainer to stream.
|
---|
149 | *
|
---|
150 | * @param out stream to print to
|
---|
151 | * @param t instance of BCContainer.
|
---|
152 | * @return stream for concatenation
|
---|
153 | */
|
---|
154 | std::ostream &operator<<(std::ostream &out, const BoundaryConditions::BCContainer &t)
|
---|
155 | {
|
---|
156 | for (BoundaryConditions::BCContainer::const_iterator iter = t.begin(); iter != t.end(); ++iter) {
|
---|
157 | if (iter != t.begin())
|
---|
158 | out << ", ";
|
---|
159 | out << t.getName(*iter);
|
---|
160 | }
|
---|
161 | return out;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** Input routine for class BCContainer from stream.
|
---|
165 | *
|
---|
166 | * We expect input as comma-separated list with NDIM values.
|
---|
167 | *
|
---|
168 | * @param in input stream
|
---|
169 | * @param t instance of BCContainer to set
|
---|
170 | * @return input stream for concatenation
|
---|
171 | */
|
---|
172 | std::istream &operator>>(std::istream &in, BoundaryConditions::BCContainer &t)
|
---|
173 | {
|
---|
174 | // read from stream till semicolon or end
|
---|
175 | std::stringbuf sbuf;
|
---|
176 | in.get(sbuf, ';');
|
---|
177 |
|
---|
178 | // tokenize
|
---|
179 | typedef boost::tokenizer< boost::char_separator<char> > tokens;
|
---|
180 | BoundaryConditions::Conditions_t conditions;
|
---|
181 | boost::char_separator<char> commasep(",");
|
---|
182 | // it is imperative to copy the content of the stringbuffer, otherwise we'll
|
---|
183 | // get strange bug where 'Wrap' != 'Wrap' and so on.
|
---|
184 | std::string line(sbuf.str());
|
---|
185 | tokens tok(line, commasep);
|
---|
186 | for(tokens::iterator iter=tok.begin(); iter!=tok.end();++iter) {
|
---|
187 | // remove leading and trailing white space
|
---|
188 | std::string condition(*iter);
|
---|
189 | boost::trim(condition);
|
---|
190 | conditions.push_back(t.getEnum(condition));
|
---|
191 | }
|
---|
192 |
|
---|
193 | // set
|
---|
194 | t.set(conditions);
|
---|
195 |
|
---|
196 | return in;
|
---|
197 | }
|
---|