| [d9005c1] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 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/>.
 | 
|---|
| [d9005c1] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
 | 24 |  * TremoloParser_ElementKeys.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Mar 12, 2012
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | #include "TremoloParser_ElementKeys.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | #include <boost/tokenizer.hpp>
 | 
|---|
 | 41 | #include <iostream>
 | 
|---|
| [045228] | 42 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [d9005c1] | 43 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | #include "Element/element.hpp"
 | 
|---|
 | 46 | #include "Element/periodentafel.hpp"
 | 
|---|
 | 47 | #include "Parser/Exceptions.hpp"
 | 
|---|
| [045228] | 48 | #include "Potentials/Particles/ParticleFactory.hpp"
 | 
|---|
| [d9005c1] | 49 | #include "World.hpp"
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | /** Parses a .potentials file and creates from it the knownTypes file.
 | 
|---|
 | 52 |  *
 | 
|---|
 | 53 |  * @param file input stream of .potentials file
 | 
|---|
 | 54 |  */
 | 
|---|
 | 55 | void ElementKeys::parseKnownTypes(std::istream &file)
 | 
|---|
 | 56 | {
 | 
|---|
 | 57 |   const periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
 | 58 |   // remove old mapping
 | 
|---|
 | 59 |   clear();
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | //  LOG(3, "INFO: additionalAtomData contains: " << additionalAtomData);
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 |   // parse in file
 | 
|---|
 | 64 |   typedef boost::tokenizer<boost::char_separator<char> >
 | 
|---|
 | 65 |       tokenizer;
 | 
|---|
 | 66 |   boost::char_separator<char> tokensep(":\t ,;");
 | 
|---|
 | 67 |   boost::char_separator<char> equalitysep("\t =");
 | 
|---|
 | 68 |   std::string line;
 | 
|---|
 | 69 |   while (file.good()) {
 | 
|---|
 | 70 |     std::getline( file, line );
 | 
|---|
 | 71 |     LOG(4, "INFO: full line of parameters is '" << line << "'");
 | 
|---|
 | 72 |     if (line.find("particle:") != string::npos) {
 | 
|---|
 | 73 |       LOG(3, "INFO: found line '" << line << "' containing keyword 'particle:'.");
 | 
|---|
 | 74 |       tokenizer tokens(line, tokensep);
 | 
|---|
 | 75 |       ASSERT(tokens.begin() != tokens.end(),
 | 
|---|
 | 76 |           "FormatParser< tremolo >::parseKnownTypes() - line with 'particle:' but no particles separated by comma.");
 | 
|---|
 | 77 |       // look for particle_type
 | 
|---|
 | 78 |       std::string particle_type("NULL");
 | 
|---|
 | 79 |       std::string element_type("NULL");
 | 
|---|
 | 80 |       for (tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
 | 81 |           tok_iter != tokens.end();
 | 
|---|
 | 82 |           ++tok_iter) {
 | 
|---|
 | 83 |         if ((*tok_iter).find("particle_type") != string::npos) {
 | 
|---|
 | 84 |           LOG(3, "INFO: found token '" << *tok_iter << "' containing keyword 'particle_type'.");
 | 
|---|
 | 85 |           tokenizer token((*tok_iter), equalitysep);
 | 
|---|
 | 86 |           ASSERT(token.begin() != token.end(),
 | 
|---|
 | 87 |                     "FormatParser< tremolo >::parseKnownTypes() - could not split particle_type by equality sign");
 | 
|---|
 | 88 |           tokenizer::iterator particle_iter = token.begin();
 | 
|---|
 | 89 |           particle_iter++;
 | 
|---|
 | 90 |           particle_type = *particle_iter;
 | 
|---|
 | 91 |         }
 | 
|---|
 | 92 |         if ((*tok_iter).find("element_name") != string::npos) {
 | 
|---|
 | 93 |           LOG(3, "INFO: found token '" << *tok_iter << "' containing keyword 'element_name'.");
 | 
|---|
 | 94 |           tokenizer token((*tok_iter), equalitysep);
 | 
|---|
 | 95 |           ASSERT(token.begin() != token.end(),
 | 
|---|
 | 96 |                     "FormatParser< tremolo >::parseKnownTypes() - could not split particle_type by equality sign");
 | 
|---|
 | 97 |           tokenizer::iterator element_iter = token.begin();
 | 
|---|
 | 98 |           element_iter++;
 | 
|---|
 | 99 |           element_type = *element_iter;
 | 
|---|
 | 100 |         }
 | 
|---|
 | 101 |       }
 | 
|---|
 | 102 |       if ((particle_type != "NULL") && (element_type != "NULL")) {
 | 
|---|
 | 103 |         if (periode->FindElement(element_type) != NULL) {
 | 
|---|
 | 104 |           LOG(1, "INFO: Added Type " << particle_type << " as reference to element " << element_type << ".");
 | 
|---|
 | 105 |           setType(particle_type, element_type);
 | 
|---|
 | 106 |         } else {
 | 
|---|
 | 107 |           ELOG(1, "INFO: Either Type " << particle_type << " or " << element_type << " could not be recognized." );
 | 
|---|
 | 108 |         }
 | 
|---|
 | 109 |       } else {
 | 
|---|
 | 110 |         ELOG(3, "Line does not contain both 'particle_type' and 'element_name' as keys." );
 | 
|---|
 | 111 |       }
 | 
|---|
 | 112 |     }
 | 
|---|
 | 113 |   }
 | 
|---|
 | 114 | }
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 | /** Creates knownTypes as the identity, e.g. H -> H, He -> He, ... .
 | 
|---|
 | 117 |  *
 | 
|---|
 | 118 |  */
 | 
|---|
 | 119 | void ElementKeys::createKnownTypesByIdentity()
 | 
|---|
 | 120 | {
 | 
|---|
 | 121 |   // remove old mapping
 | 
|---|
 | 122 |   clear();
 | 
|---|
 | 123 |   // make knownTypes the identity mapping
 | 
|---|
 | 124 |   const periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
 | 125 |   for (periodentafel::const_iterator iter = periode->begin();
 | 
|---|
 | 126 |       iter != periode->end();
 | 
|---|
 | 127 |       ++iter) {
 | 
|---|
 | 128 |     setType( iter->second->getSymbol(), iter->second->getSymbol());
 | 
|---|
 | 129 |   }
 | 
|---|
 | 130 | }
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | /** Getter for value indexed by \a type.
 | 
|---|
 | 133 |  *
 | 
|---|
 | 134 |  * @param type key for map
 | 
|---|
 | 135 |  * @return value stored under \a type
 | 
|---|
 | 136 |  */
 | 
|---|
 | 137 | const std::string &ElementKeys::getType(const std::string &type) const throw (IllegalParserKeyException)
 | 
|---|
 | 138 | {
 | 
|---|
 | 139 |   const_iterator iter = find(type);
 | 
|---|
 | 140 |   if (iter == end()) {
 | 
|---|
 | 141 |     throw IllegalParserKeyException() << ParserMapLookup(type);
 | 
|---|
 | 142 |   } else
 | 
|---|
 | 143 |     return iter->second;
 | 
|---|
 | 144 | }
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | /** Getter for value indexed by \a type.
 | 
|---|
 | 147 |  *
 | 
|---|
 | 148 |  * @param type key for map
 | 
|---|
 | 149 |  * @return value stored under \a type
 | 
|---|
 | 150 |  */
 | 
|---|
 | 151 | void ElementKeys::setType(const std::string &type, const std::string &value) throw (IllegalParserKeyException)
 | 
|---|
 | 152 | {
 | 
|---|
 | 153 |   const_iterator iter = find(type);
 | 
|---|
 | 154 |   if (iter != end()) {
 | 
|---|
 | 155 |     throw IllegalParserKeyException() << ParserMapLookup(type);
 | 
|---|
 | 156 |   } else {
 | 
|---|
 | 157 |     insert( make_pair(type,value) );
 | 
|---|
| [045228] | 158 |     if (value != type) {
 | 
|---|
 | 159 |       const periodentafel * periode = World::getInstance().getPeriode();
 | 
|---|
 | 160 |       const element * elem = periode->FindElement(value);
 | 
|---|
 | 161 |       ASSERT( elem != NULL,
 | 
|---|
 | 162 |           "ElementKeys::setType() - could not find element "+value);
 | 
|---|
 | 163 |       ParticleFactory::getInstance().createInstance(
 | 
|---|
 | 164 |           type, elem->getAtomicNumber(), elem->getCharge()); 
 | 
|---|
 | 165 |     }
 | 
|---|
| [d9005c1] | 166 |     LOG(3, "DEBUG: Key " << type << " refers to element " << at(type) << ".");
 | 
|---|
 | 167 |   }
 | 
|---|
 | 168 | }
 | 
|---|