| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2013 Frederik Heber. 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 | * Particle.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: May 13, 2013 | 
|---|
| 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 "Particle.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include <boost/assign.hpp> | 
|---|
| 40 | #include <boost/lexical_cast.hpp> | 
|---|
| 41 | #include <boost/tokenizer.hpp> | 
|---|
| 42 | #include <iostream> | 
|---|
| 43 | #include <iterator> | 
|---|
| 44 | #include <string> | 
|---|
| 45 |  | 
|---|
| 46 | #include "CodePatterns/Assert.hpp" | 
|---|
| 47 |  | 
|---|
| 48 | #include "Element/element.hpp" | 
|---|
| 49 | #include "Element/periodentafel.hpp" | 
|---|
| 50 | #include "Potentials/Exceptions.hpp" | 
|---|
| 51 | #include "Potentials/Particles/ParticleRegistry.hpp" | 
|---|
| 52 |  | 
|---|
| 53 | using namespace boost::assign; | 
|---|
| 54 |  | 
|---|
| 55 | std::vector<std::string> getParameterNames() | 
|---|
| 56 | { | 
|---|
| 57 | std::vector<std::string> tempnames; | 
|---|
| 58 | tempnames += "particle_type", "element_name", "sigma", "epsilon", "sigma14", "epsilon14", "mass", "free", "charge"; | 
|---|
| 59 | return tempnames; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | const std::vector<std::string> Particle::ParameterNames = getParameterNames(); | 
|---|
| 63 |  | 
|---|
| 64 | Particle::Particle( | 
|---|
| 65 | const periodentafel &_periode, | 
|---|
| 66 | const std::string &_token, | 
|---|
| 67 | const atomicNumber_t &_number) : | 
|---|
| 68 | name(_token), | 
|---|
| 69 | periode(_periode), | 
|---|
| 70 | charge(0.), | 
|---|
| 71 | mass(0.), | 
|---|
| 72 | dof(3), | 
|---|
| 73 | atomic_number(_number), | 
|---|
| 74 | sigma(0.), | 
|---|
| 75 | epsilon(0.), | 
|---|
| 76 | sigma_14(0.), | 
|---|
| 77 | epsilon_14(0.) | 
|---|
| 78 | { | 
|---|
| 79 | ParticleRegistry::getInstance().registerInstance(this); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | std::string Particle::findFreeName( | 
|---|
| 83 | const periodentafel &_periode, | 
|---|
| 84 | const atomicNumber_t &_number) | 
|---|
| 85 | { | 
|---|
| 86 | const element *e = _periode.FindElement(_number); | 
|---|
| 87 | ASSERT (e != NULL, | 
|---|
| 88 | "Particle::findFreeName() - cannot find element with number " | 
|---|
| 89 | +toString(_number)+"."); | 
|---|
| 90 | bool FoundFlag = false; | 
|---|
| 91 | size_t index = 1; | 
|---|
| 92 | std::string returnname; | 
|---|
| 93 | while(!FoundFlag) { | 
|---|
| 94 | returnname = e->getSymbol()+toString(index++); | 
|---|
| 95 | FoundFlag = !ParticleRegistry::getInstance().isPresentByName(returnname); | 
|---|
| 96 | } | 
|---|
| 97 | return returnname; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | Particle::Particle( | 
|---|
| 101 | const periodentafel &_periode, | 
|---|
| 102 | const atomicNumber_t &_number) : | 
|---|
| 103 | name(findFreeName(_periode, _number)), | 
|---|
| 104 | periode(_periode), | 
|---|
| 105 | charge(0.), | 
|---|
| 106 | mass(0.), | 
|---|
| 107 | dof(3), | 
|---|
| 108 | atomic_number(_number), | 
|---|
| 109 | sigma(0.), | 
|---|
| 110 | epsilon(0.), | 
|---|
| 111 | sigma_14(0.), | 
|---|
| 112 | epsilon_14(0.) | 
|---|
| 113 | { | 
|---|
| 114 | ParticleRegistry::getInstance().registerInstance(this); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | Particle::Particle(const periodentafel &_periode) : | 
|---|
| 118 | periode(_periode), | 
|---|
| 119 | charge(0.), | 
|---|
| 120 | mass(0.), | 
|---|
| 121 | dof(3), | 
|---|
| 122 | atomic_number(-1), | 
|---|
| 123 | sigma(0.), | 
|---|
| 124 | epsilon(0.), | 
|---|
| 125 | sigma_14(0.), | 
|---|
| 126 | epsilon_14(0.) | 
|---|
| 127 | { | 
|---|
| 128 | // not registered as it does not have a name yet | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | void Particle::stream_to(std::ostream &ost) const | 
|---|
| 132 | { | 
|---|
| 133 | // check stream | 
|---|
| 134 | if (ost.bad()) | 
|---|
| 135 | throw SerializerStreamException(); | 
|---|
| 136 |  | 
|---|
| 137 | /// print parameter key | 
|---|
| 138 | ost << "\tparticle:"; | 
|---|
| 139 | /// print name and values | 
|---|
| 140 | ost << "\tparticle_type=" << getName(); | 
|---|
| 141 | ost << ",\telement_name=" << getElement(); | 
|---|
| 142 | ost << ",\tsigma=" << sigma; | 
|---|
| 143 | ost << ",\tepsilon=" << epsilon; | 
|---|
| 144 | ost << ",\tsigma14=" << sigma_14; | 
|---|
| 145 | ost << ",\tepsilon14=" << epsilon_14; | 
|---|
| 146 | ost << ",\tmass=" << mass; | 
|---|
| 147 | ost << ",\tfree=" << dof; | 
|---|
| 148 | ost << ",\tcharge=" << std::setprecision(10) << charge; | 
|---|
| 149 | ost << ";"; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | size_t Particle::lookupParameterName(const std::string &name) const | 
|---|
| 153 | { | 
|---|
| 154 | std::vector<std::string>::const_iterator iter = | 
|---|
| 155 | std::find(ParameterNames.begin(), ParameterNames.end(), name); | 
|---|
| 156 | if (iter == ParameterNames.end()) | 
|---|
| 157 | return -1; | 
|---|
| 158 | else | 
|---|
| 159 | return std::distance(ParameterNames.begin(), iter); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void Particle::stream_from(std::istream &ist) | 
|---|
| 163 | { | 
|---|
| 164 | // check stream | 
|---|
| 165 | if (ist.bad()) | 
|---|
| 166 | throw SerializerStreamException(); | 
|---|
| 167 |  | 
|---|
| 168 | // read in full line | 
|---|
| 169 | std::string linestring; | 
|---|
| 170 | getline(ist, linestring); | 
|---|
| 171 | const std::string whitespace(" \t"); | 
|---|
| 172 | const size_t strBegin = linestring.find_first_not_of(whitespace); | 
|---|
| 173 | const size_t colonpos = linestring.find(":"); | 
|---|
| 174 | if ((strBegin == std::string::npos) || (colonpos == std::string::npos) || | 
|---|
| 175 | (linestring.substr(strBegin, colonpos-strBegin) != std::string("particle"))) | 
|---|
| 176 | throw SerializerMissingValueException() | 
|---|
| 177 | << SerializerKey(std::string("particle")); | 
|---|
| 178 |  | 
|---|
| 179 | // tokenize by "," | 
|---|
| 180 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer; | 
|---|
| 181 | boost::char_separator<char> pairsep(",\t ;"); | 
|---|
| 182 | std::string remainderstring(linestring.substr(colonpos+1)); | 
|---|
| 183 | tokenizer tokens(remainderstring, pairsep); //skip colon | 
|---|
| 184 |  | 
|---|
| 185 | // step through each token | 
|---|
| 186 | for (tokenizer::iterator tok_iter = tokens.begin(); | 
|---|
| 187 | tok_iter != tokens.end(); ++tok_iter) { | 
|---|
| 188 | const std::string &keyvalue = *tok_iter; | 
|---|
| 189 | const size_t equalitypos = keyvalue.find("="); | 
|---|
| 190 | const std::string key = keyvalue.substr(0,equalitypos); | 
|---|
| 191 | std::string value = keyvalue.substr(equalitypos+1); | 
|---|
| 192 |  | 
|---|
| 193 | const size_t index = lookupParameterName(key); | 
|---|
| 194 |  | 
|---|
| 195 | if (equalitypos == std::string::npos) | 
|---|
| 196 | throw SerializerMissingValueException() << SerializerKey(key); | 
|---|
| 197 |  | 
|---|
| 198 | switch((parameters_t)index) { | 
|---|
| 199 | case e_particle_type: | 
|---|
| 200 | const_cast<std::string &>(name) = value; | 
|---|
| 201 | break; | 
|---|
| 202 | case e_element_name: | 
|---|
| 203 | std::transform(value.begin()+1, value.end(), value.begin()+1, ::tolower); | 
|---|
| 204 | setElement(value); | 
|---|
| 205 | break; | 
|---|
| 206 | case e_sigma: | 
|---|
| 207 | sigma = boost::lexical_cast<double>(value); | 
|---|
| 208 | break; | 
|---|
| 209 | case e_epsilon: | 
|---|
| 210 | epsilon = boost::lexical_cast<double>(value); | 
|---|
| 211 | break; | 
|---|
| 212 | case e_sigma_14: | 
|---|
| 213 | sigma_14 = boost::lexical_cast<double>(value); | 
|---|
| 214 | break; | 
|---|
| 215 | case e_epsilon_14: | 
|---|
| 216 | epsilon_14 = boost::lexical_cast<double>(value); | 
|---|
| 217 | break; | 
|---|
| 218 | case e_mass: | 
|---|
| 219 | mass = boost::lexical_cast<double>(value); | 
|---|
| 220 | break; | 
|---|
| 221 | case e_free: | 
|---|
| 222 | dof = boost::lexical_cast<unsigned int>(value); | 
|---|
| 223 | break; | 
|---|
| 224 | case e_charge: | 
|---|
| 225 | charge = boost::lexical_cast<double>(value); | 
|---|
| 226 | break; | 
|---|
| 227 | default: | 
|---|
| 228 | throw SerializerMissingValueException() << SerializerKey(key); | 
|---|
| 229 | break; | 
|---|
| 230 | } | 
|---|
| 231 | } | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | std::ostream& operator<<(std::ostream &ost, const Particle &p) | 
|---|
| 235 | { | 
|---|
| 236 | p.stream_to(ost); | 
|---|
| 237 | return ost; | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | std::string Particle::getElement() const | 
|---|
| 241 | { | 
|---|
| 242 | std::string returnstring; | 
|---|
| 243 | const element *e = periode.FindElement(atomic_number); | 
|---|
| 244 | if (e != NULL) | 
|---|
| 245 | returnstring = e->getSymbol(); | 
|---|
| 246 |  | 
|---|
| 247 | return returnstring; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | void Particle::setElement(const std::string &element_name) | 
|---|
| 251 | { | 
|---|
| 252 | const element *e = periode.FindElement(element_name); | 
|---|
| 253 | if (e != NULL) | 
|---|
| 254 | atomic_number = e->getAtomicNumber(); | 
|---|
| 255 | } | 
|---|