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