[ff09f3] | 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) const
|
---|
| 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 | void Particle::stream_to(std::ostream &ost) const
|
---|
| 118 | {
|
---|
| 119 | // check stream
|
---|
| 120 | if (ost.bad())
|
---|
| 121 | throw SerializablePotentialException();
|
---|
| 122 |
|
---|
| 123 | /// print parameter key
|
---|
| 124 | ost << "\tparticle:";
|
---|
| 125 | /// print name and values
|
---|
| 126 | ost << "\tparticle_type=" << getName();
|
---|
| 127 | ost << ",\telement_name=" << getElement();
|
---|
| 128 | ost << ",\tsigma=" << sigma;
|
---|
| 129 | ost << ",\tepsilon=" << epsilon;
|
---|
| 130 | ost << ",\tsigma14=" << sigma_14;
|
---|
| 131 | ost << ",\tepsilon14=" << epsilon_14;
|
---|
| 132 | ost << ",\tmass=" << mass;
|
---|
| 133 | ost << ",\tfree=" << dof;
|
---|
| 134 | ost << ",\tcharge=" << charge;
|
---|
| 135 | ost << std::endl;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | size_t Particle::lookupParameterName(const std::string &name) const
|
---|
| 139 | {
|
---|
| 140 | std::vector<std::string>::const_iterator iter =
|
---|
| 141 | std::find(ParameterNames.begin(), ParameterNames.end(), name);
|
---|
| 142 | if (iter == ParameterNames.end())
|
---|
| 143 | return -1;
|
---|
| 144 | else
|
---|
| 145 | return std::distance(ParameterNames.begin(), iter);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | void Particle::stream_from(std::istream &ist)
|
---|
| 149 | {
|
---|
| 150 | // check stream
|
---|
| 151 | if (ist.bad())
|
---|
| 152 | throw SerializablePotentialException();
|
---|
| 153 |
|
---|
| 154 | // read in full line
|
---|
| 155 | std::string linestring;
|
---|
| 156 | getline(ist, linestring);
|
---|
| 157 | const std::string whitespace(" \t");
|
---|
| 158 | const size_t strBegin = linestring.find_first_not_of(whitespace);
|
---|
| 159 | const size_t colonpos = linestring.find(":");
|
---|
| 160 | if ((strBegin == std::string::npos) || (colonpos == std::string::npos) ||
|
---|
| 161 | (linestring.substr(strBegin, colonpos-1) != std::string("particle")))
|
---|
| 162 | throw SerializablePotentialMissingValueException()
|
---|
| 163 | << SerializablePotentialKey(std::string("particle"));
|
---|
| 164 |
|
---|
| 165 | // tokenize by ","
|
---|
| 166 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
|
---|
| 167 | boost::char_separator<char> pairsep(",\t ;");
|
---|
| 168 | boost::char_separator<char> keyvaluesep("=");
|
---|
| 169 | std::string remainderstring(linestring.substr(colonpos+1));
|
---|
| 170 | tokenizer tokens(remainderstring, pairsep); //skip colon
|
---|
| 171 |
|
---|
| 172 | // step through each token
|
---|
| 173 | for (tokenizer::iterator tok_iter = tokens.begin();
|
---|
| 174 | tok_iter != tokens.end(); ++tok_iter) {
|
---|
| 175 | const std::string &keyvalue = *tok_iter;
|
---|
| 176 | tokenizer keyvaluetoken(keyvalue, keyvaluesep);
|
---|
| 177 | tokenizer::iterator keyvalue_iter = keyvaluetoken.begin();
|
---|
| 178 | const std::string &key = *keyvalue_iter;
|
---|
| 179 | const std::string &value = *keyvalue_iter;
|
---|
| 180 |
|
---|
| 181 | const size_t index = lookupParameterName(key);
|
---|
| 182 |
|
---|
| 183 | switch((parameters_t)index) {
|
---|
| 184 | case e_particle_type:
|
---|
| 185 | const_cast<std::string &>(name) = value;
|
---|
| 186 | break;
|
---|
| 187 | case e_element_name:
|
---|
| 188 | setElement(value);
|
---|
| 189 | break;
|
---|
| 190 | case e_sigma:
|
---|
| 191 | sigma = boost::lexical_cast<double>(value);
|
---|
| 192 | break;
|
---|
| 193 | case e_epsilon:
|
---|
| 194 | epsilon = boost::lexical_cast<double>(value);
|
---|
| 195 | break;
|
---|
| 196 | case e_sigma_14:
|
---|
| 197 | sigma_14 = boost::lexical_cast<double>(value);
|
---|
| 198 | break;
|
---|
| 199 | case e_epsilon_14:
|
---|
| 200 | epsilon_14 = boost::lexical_cast<double>(value);
|
---|
| 201 | break;
|
---|
| 202 | case e_mass:
|
---|
| 203 | mass = boost::lexical_cast<double>(value);
|
---|
| 204 | break;
|
---|
| 205 | case e_free:
|
---|
| 206 | dof = boost::lexical_cast<unsigned int>(value);
|
---|
| 207 | break;
|
---|
| 208 | case e_charge:
|
---|
| 209 | charge = boost::lexical_cast<double>(value);
|
---|
| 210 | break;
|
---|
| 211 | default:
|
---|
| 212 | ASSERT(0, "Particle::stream_from() - unknown index "
|
---|
| 213 | +toString(index)+".");
|
---|
| 214 | break;
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | std::ostream& operator<<(std::ostream &ost, const Particle &p)
|
---|
| 220 | {
|
---|
| 221 | p.stream_to(ost);
|
---|
| 222 | return ost;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | std::string Particle::getElement() const
|
---|
| 226 | {
|
---|
| 227 | std::string returnstring;
|
---|
| 228 | const element *e = periode.FindElement(atomic_number);
|
---|
| 229 | if (e != NULL)
|
---|
| 230 | returnstring = e->getSymbol();
|
---|
| 231 |
|
---|
| 232 | return returnstring;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | void Particle::setElement(const std::string &element_name)
|
---|
| 236 | {
|
---|
| 237 | const element *e = periode.FindElement(element_name);
|
---|
| 238 | if (e != NULL)
|
---|
| 239 | atomic_number = e->getAtomicNumber();
|
---|
| 240 | }
|
---|