| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * TremoloParser.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Mar 2, 2010 | 
|---|
| 12 | *      Author: metzler | 
|---|
| 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 "CodePatterns/Assert.hpp" | 
|---|
| 23 | #include "CodePatterns/Log.hpp" | 
|---|
| 24 | #include "CodePatterns/toString.hpp" | 
|---|
| 25 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 26 | #include "TremoloParser.hpp" | 
|---|
| 27 | #include "World.hpp" | 
|---|
| 28 | #include "WorldTime.hpp" | 
|---|
| 29 | #include "atom.hpp" | 
|---|
| 30 | #include "Bond/bond.hpp" | 
|---|
| 31 | #include "element.hpp" | 
|---|
| 32 | #include "molecule.hpp" | 
|---|
| 33 | #include "periodentafel.hpp" | 
|---|
| 34 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| 35 | #include <map> | 
|---|
| 36 | #include <vector> | 
|---|
| 37 |  | 
|---|
| 38 | #include <boost/tokenizer.hpp> | 
|---|
| 39 | #include <iostream> | 
|---|
| 40 | #include <iomanip> | 
|---|
| 41 |  | 
|---|
| 42 | using namespace std; | 
|---|
| 43 |  | 
|---|
| 44 | /** | 
|---|
| 45 | * Constructor. | 
|---|
| 46 | */ | 
|---|
| 47 | TremoloParser::TremoloParser() { | 
|---|
| 48 | knownKeys["x"] = TremoloKey::x; | 
|---|
| 49 | knownKeys["u"] = TremoloKey::u; | 
|---|
| 50 | knownKeys["F"] = TremoloKey::F; | 
|---|
| 51 | knownKeys["stress"] = TremoloKey::stress; | 
|---|
| 52 | knownKeys["Id"] = TremoloKey::Id; | 
|---|
| 53 | knownKeys["neighbors"] = TremoloKey::neighbors; | 
|---|
| 54 | knownKeys["imprData"] = TremoloKey::imprData; | 
|---|
| 55 | knownKeys["GroupMeasureTypeNo"] = TremoloKey::GroupMeasureTypeNo; | 
|---|
| 56 | knownKeys["type"] = TremoloKey::type; | 
|---|
| 57 | knownKeys["extType"] = TremoloKey::extType; | 
|---|
| 58 | knownKeys["name"] = TremoloKey::name; | 
|---|
| 59 | knownKeys["resName"] = TremoloKey::resName; | 
|---|
| 60 | knownKeys["chainID"] = TremoloKey::chainID; | 
|---|
| 61 | knownKeys["resSeq"] = TremoloKey::resSeq; | 
|---|
| 62 | knownKeys["occupancy"] = TremoloKey::occupancy; | 
|---|
| 63 | knownKeys["tempFactor"] = TremoloKey::tempFactor; | 
|---|
| 64 | knownKeys["segID"] = TremoloKey::segID; | 
|---|
| 65 | knownKeys["Charge"] = TremoloKey::Charge; | 
|---|
| 66 | knownKeys["charge"] = TremoloKey::charge; | 
|---|
| 67 | knownKeys["GrpTypeNo"] = TremoloKey::GrpTypeNo; | 
|---|
| 68 | knownKeys["torsion"] = TremoloKey::torsion; | 
|---|
| 69 |  | 
|---|
| 70 | createKnownTypesByIdentity(); | 
|---|
| 71 |  | 
|---|
| 72 | // default behavior: use all possible keys on output | 
|---|
| 73 | for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin(); iter != knownKeys.end(); ++iter) | 
|---|
| 74 | usedFields.push_back(iter->first); | 
|---|
| 75 |  | 
|---|
| 76 | // and noKey afterwards(!) such that it is not used in usedFields | 
|---|
| 77 | knownKeys[" "] = TremoloKey::noKey; // with this we can detect invalid keys | 
|---|
| 78 |  | 
|---|
| 79 | // invert knownKeys for debug output | 
|---|
| 80 | for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin(); iter != knownKeys.end(); ++iter) | 
|---|
| 81 | knownKeyNames.insert( make_pair( iter->second, iter->first) ); | 
|---|
| 82 |  | 
|---|
| 83 | additionalAtomData.clear(); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | /** | 
|---|
| 87 | * Destructor. | 
|---|
| 88 | */ | 
|---|
| 89 | TremoloParser::~TremoloParser() { | 
|---|
| 90 | usedFields.clear(); | 
|---|
| 91 | additionalAtomData.clear(); | 
|---|
| 92 | atomIdMap.clear(); | 
|---|
| 93 | knownKeys.clear(); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | /** | 
|---|
| 97 | * Loads atoms from a tremolo-formatted file. | 
|---|
| 98 | * | 
|---|
| 99 | * \param tremolo file | 
|---|
| 100 | */ | 
|---|
| 101 | void TremoloParser::load(istream* file) { | 
|---|
| 102 | string line; | 
|---|
| 103 | string::size_type location; | 
|---|
| 104 |  | 
|---|
| 105 | // reset atomIdMap, for we now get new serials | 
|---|
| 106 | atomIdMap.clear(); | 
|---|
| 107 | usedFields.clear(); | 
|---|
| 108 |  | 
|---|
| 109 | molecule *newmol = World::getInstance().createMolecule(); | 
|---|
| 110 | newmol->ActiveFlag = true; | 
|---|
| 111 | // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include | 
|---|
| 112 | World::getInstance().getMolecules()->insert(newmol); | 
|---|
| 113 | while (file->good()) { | 
|---|
| 114 | std::getline(*file, line, '\n'); | 
|---|
| 115 | if (usedFields.empty()) { | 
|---|
| 116 | location = line.find("ATOMDATA", 0); | 
|---|
| 117 | if (location != string::npos) { | 
|---|
| 118 | parseAtomDataKeysLine(line, location + 8); | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|
| 121 | if (line.length() > 0 && line.at(0) != '#') { | 
|---|
| 122 | readAtomDataLine(line, newmol); | 
|---|
| 123 | } | 
|---|
| 124 | } | 
|---|
| 125 | // refresh atom::nr and atom::name | 
|---|
| 126 | newmol->getAtomCount(); | 
|---|
| 127 |  | 
|---|
| 128 | processNeighborInformation(); | 
|---|
| 129 | adaptImprData(); | 
|---|
| 130 | adaptTorsion(); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | /** | 
|---|
| 134 | * Saves the \a atoms into as a tremolo file. | 
|---|
| 135 | * | 
|---|
| 136 | * \param file where to save the state | 
|---|
| 137 | * \param atoms atoms to store | 
|---|
| 138 | */ | 
|---|
| 139 | void TremoloParser::save(ostream* file, const std::vector<atom *> &AtomList) { | 
|---|
| 140 | DoLog(0) && (Log() << Verbose(0) << "Saving changes to tremolo." << std::endl); | 
|---|
| 141 |  | 
|---|
| 142 | vector<atom*>::const_iterator atomIt; | 
|---|
| 143 | vector<string>::iterator it; | 
|---|
| 144 |  | 
|---|
| 145 | DoLog(3) && (Log() << Verbose(3) << "additionalAtomData contains: " << additionalAtomData << std::endl); | 
|---|
| 146 |  | 
|---|
| 147 | *file << "# ATOMDATA"; | 
|---|
| 148 | for (it=usedFields.begin(); it < usedFields.end(); it++) { | 
|---|
| 149 | *file << "\t" << *it; | 
|---|
| 150 | } | 
|---|
| 151 | *file << endl; | 
|---|
| 152 | for (atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) { | 
|---|
| 153 | saveLine(file, *atomIt); | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | /** | 
|---|
| 158 | * Sets the keys for which data should be written to the stream when save is | 
|---|
| 159 | * called. | 
|---|
| 160 | * | 
|---|
| 161 | * \param string of field names with the same syntax as for an ATOMDATA line | 
|---|
| 162 | *        but without the prexix "ATOMDATA" | 
|---|
| 163 | */ | 
|---|
| 164 | void TremoloParser::setFieldsForSave(std::string atomDataLine) { | 
|---|
| 165 | parseAtomDataKeysLine(atomDataLine, 0); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | /** | 
|---|
| 170 | * Writes one line of tremolo-formatted data to the provided stream. | 
|---|
| 171 | * | 
|---|
| 172 | * \param stream where to write the line to | 
|---|
| 173 | * \param reference to the atom of which information should be written | 
|---|
| 174 | */ | 
|---|
| 175 | void TremoloParser::saveLine(ostream* file, atom* currentAtom) { | 
|---|
| 176 | vector<string>::iterator it; | 
|---|
| 177 | TremoloKey::atomDataKey currentField; | 
|---|
| 178 |  | 
|---|
| 179 | DoLog(4) && (Log() << Verbose(4) << "INFO: Saving atom " << *currentAtom << ", its father id is " << currentAtom->GetTrueFather()->getId() << std::endl); | 
|---|
| 180 |  | 
|---|
| 181 | for (it = usedFields.begin(); it != usedFields.end(); it++) { | 
|---|
| 182 | currentField = knownKeys[it->substr(0, it->find("="))]; | 
|---|
| 183 | switch (currentField) { | 
|---|
| 184 | case TremoloKey::x : | 
|---|
| 185 | // for the moment, assume there are always three dimensions | 
|---|
| 186 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getPosition() << std::endl); | 
|---|
| 187 | *file << currentAtom->at(0) << "\t"; | 
|---|
| 188 | *file << currentAtom->at(1) << "\t"; | 
|---|
| 189 | *file << currentAtom->at(2) << "\t"; | 
|---|
| 190 | break; | 
|---|
| 191 | case TremoloKey::u : | 
|---|
| 192 | // for the moment, assume there are always three dimensions | 
|---|
| 193 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getAtomicVelocity() << std::endl); | 
|---|
| 194 | *file << currentAtom->getAtomicVelocity()[0] << "\t"; | 
|---|
| 195 | *file << currentAtom->getAtomicVelocity()[1] << "\t"; | 
|---|
| 196 | *file << currentAtom->getAtomicVelocity()[2] << "\t"; | 
|---|
| 197 | break; | 
|---|
| 198 | case TremoloKey::type : | 
|---|
| 199 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
| 200 | if (additionalAtomData[currentAtom->getId()].get(currentField) != "-") { | 
|---|
| 201 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField) << std::endl); | 
|---|
| 202 | *file << additionalAtomData[currentAtom->getId()].get(currentField) << "\t"; | 
|---|
| 203 | } else { | 
|---|
| 204 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " default value: " << currentAtom->getType()->getSymbol() << std::endl); | 
|---|
| 205 | *file << currentAtom->getType()->getSymbol() << "\t"; | 
|---|
| 206 | } | 
|---|
| 207 | } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) { | 
|---|
| 208 | if (additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) != "-") { | 
|---|
| 209 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) << std::endl); | 
|---|
| 210 | *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) << "\t"; | 
|---|
| 211 | } else { | 
|---|
| 212 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " default value from father: " << currentAtom->GetTrueFather()->getType()->getSymbol() << std::endl); | 
|---|
| 213 | *file << currentAtom->GetTrueFather()->getType()->getSymbol() << "\t"; | 
|---|
| 214 | } | 
|---|
| 215 | } else { | 
|---|
| 216 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " its default value: " << currentAtom->getType()->getSymbol() << std::endl); | 
|---|
| 217 | *file << currentAtom->getType()->getSymbol() << "\t"; | 
|---|
| 218 | } | 
|---|
| 219 | break; | 
|---|
| 220 | case TremoloKey::Id : | 
|---|
| 221 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getId()+1 << std::endl); | 
|---|
| 222 | *file << currentAtom->getId()+1 << "\t"; | 
|---|
| 223 | break; | 
|---|
| 224 | case TremoloKey::neighbors : | 
|---|
| 225 | DoLog(3) && (Log() << Verbose(3) << "Writing type " << knownKeyNames[currentField] << std::endl); | 
|---|
| 226 | writeNeighbors(file, atoi(it->substr(it->find("=") + 1, 1).c_str()), currentAtom); | 
|---|
| 227 | break; | 
|---|
| 228 | case TremoloKey::resSeq : | 
|---|
| 229 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
| 230 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField) << std::endl); | 
|---|
| 231 | *file << additionalAtomData[currentAtom->getId()].get(currentField); | 
|---|
| 232 | } else if (currentAtom->getMolecule() != NULL) { | 
|---|
| 233 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " its own id: " << currentAtom->getMolecule()->getId()+1 << std::endl); | 
|---|
| 234 | *file << setw(4) << currentAtom->getMolecule()->getId()+1; | 
|---|
| 235 | } else { | 
|---|
| 236 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " default value: " << defaultAdditionalData.get(currentField) << std::endl); | 
|---|
| 237 | *file << defaultAdditionalData.get(currentField); | 
|---|
| 238 | } | 
|---|
| 239 | *file << "\t"; | 
|---|
| 240 | break; | 
|---|
| 241 | default : | 
|---|
| 242 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
| 243 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField) << std::endl); | 
|---|
| 244 | *file << additionalAtomData[currentAtom->getId()].get(currentField); | 
|---|
| 245 | } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) { | 
|---|
| 246 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) << std::endl); | 
|---|
| 247 | *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField); | 
|---|
| 248 | } else { | 
|---|
| 249 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " the default: " << defaultAdditionalData.get(currentField) << std::endl); | 
|---|
| 250 | *file << defaultAdditionalData.get(currentField); | 
|---|
| 251 | } | 
|---|
| 252 | *file << "\t"; | 
|---|
| 253 | break; | 
|---|
| 254 | } | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | *file << endl; | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | /** | 
|---|
| 261 | * Writes the neighbor information of one atom to the provided stream. | 
|---|
| 262 | * | 
|---|
| 263 | * Note that ListOfBonds of WorldTime::CurrentTime is used. | 
|---|
| 264 | * | 
|---|
| 265 | * \param stream where to write neighbor information to | 
|---|
| 266 | * \param number of neighbors | 
|---|
| 267 | * \param reference to the atom of which to take the neighbor information | 
|---|
| 268 | */ | 
|---|
| 269 | void TremoloParser::writeNeighbors(ostream* file, int numberOfNeighbors, atom* currentAtom) { | 
|---|
| 270 | const BondList& ListOfBonds = currentAtom->getListOfBonds(); | 
|---|
| 271 | // sort bonded indices | 
|---|
| 272 | typedef std::set<atomId_t> sortedIndices; | 
|---|
| 273 | sortedIndices sortedBonds; | 
|---|
| 274 | for (BondList::const_iterator iter = ListOfBonds.begin(); | 
|---|
| 275 | iter != ListOfBonds.end(); ++iter) | 
|---|
| 276 | sortedBonds.insert((*iter)->GetOtherAtom(currentAtom)->getId()); | 
|---|
| 277 | // print indices | 
|---|
| 278 | sortedIndices::const_iterator currentBond = sortedBonds.begin(); | 
|---|
| 279 | for (int i = 0; i < numberOfNeighbors; i++) { | 
|---|
| 280 | *file << (currentBond != sortedBonds.end() ? (*currentBond)+1 : 0) << "\t"; | 
|---|
| 281 | if (currentBond != sortedBonds.end()) | 
|---|
| 282 | ++currentBond; | 
|---|
| 283 | } | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | /** | 
|---|
| 287 | * Stores keys from the ATOMDATA line. | 
|---|
| 288 | * | 
|---|
| 289 | * \param line to parse the keys from | 
|---|
| 290 | * \param with which offset the keys begin within the line | 
|---|
| 291 | */ | 
|---|
| 292 | void TremoloParser::parseAtomDataKeysLine(string line, int offset) { | 
|---|
| 293 | string keyword; | 
|---|
| 294 | stringstream lineStream; | 
|---|
| 295 |  | 
|---|
| 296 | lineStream << line.substr(offset); | 
|---|
| 297 | usedFields.clear(); | 
|---|
| 298 | while (lineStream.good()) { | 
|---|
| 299 | lineStream >> keyword; | 
|---|
| 300 | if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey) { | 
|---|
| 301 | // TODO: throw exception about unknown key | 
|---|
| 302 | cout << "Unknown key: " << keyword << " is not part of the tremolo format specification." << endl; | 
|---|
| 303 | break; | 
|---|
| 304 | } | 
|---|
| 305 | usedFields.push_back(keyword); | 
|---|
| 306 | } | 
|---|
| 307 | //DoLog(1) && (Log() << Verbose(1) << "INFO: " << usedFields << std::endl); | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | /** | 
|---|
| 311 | * Reads one data line of a tremolo file and interprets it according to the keys | 
|---|
| 312 | * obtained from the ATOMDATA line. | 
|---|
| 313 | * | 
|---|
| 314 | * \param line to parse as an atom | 
|---|
| 315 | * \param *newmol molecule to add atom to | 
|---|
| 316 | */ | 
|---|
| 317 | void TremoloParser::readAtomDataLine(string line, molecule *newmol = NULL) { | 
|---|
| 318 | vector<string>::iterator it; | 
|---|
| 319 | stringstream lineStream; | 
|---|
| 320 | atom* newAtom = World::getInstance().createAtom(); | 
|---|
| 321 | TremoloAtomInfoContainer *atomInfo = NULL; | 
|---|
| 322 | additionalAtomData[newAtom->getId()] = TremoloAtomInfoContainer(); // fill with default values | 
|---|
| 323 | atomInfo = &additionalAtomData[newAtom->getId()]; | 
|---|
| 324 | TremoloKey::atomDataKey currentField; | 
|---|
| 325 | ConvertTo<double> toDouble; | 
|---|
| 326 | ConvertTo<int> toInt; | 
|---|
| 327 | Vector tempVector; | 
|---|
| 328 |  | 
|---|
| 329 | // setup tokenizer, splitting up white-spaced entries | 
|---|
| 330 | typedef boost::tokenizer<boost::char_separator<char> > | 
|---|
| 331 | tokenizer; | 
|---|
| 332 | boost::char_separator<char> whitespacesep(" \t"); | 
|---|
| 333 | tokenizer tokens(line, whitespacesep); | 
|---|
| 334 | ASSERT(tokens.begin() != tokens.end(), | 
|---|
| 335 | "TremoloParser::readAtomDataLine - empty string, need at least ' '!"); | 
|---|
| 336 | tokenizer::iterator tok_iter = tokens.begin(); | 
|---|
| 337 | // then associate each token to each file | 
|---|
| 338 | for (it = usedFields.begin(); it < usedFields.end(); it++) { | 
|---|
| 339 | const std::string keyName = it->substr(0, it->find("=")); | 
|---|
| 340 | currentField = knownKeys[keyName]; | 
|---|
| 341 | const string word = *tok_iter; | 
|---|
| 342 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with remaining data " << word << std::endl); | 
|---|
| 343 | switch (currentField) { | 
|---|
| 344 | case TremoloKey::x : | 
|---|
| 345 | // for the moment, assume there are always three dimensions | 
|---|
| 346 | for (int i=0;i<NDIM;i++) { | 
|---|
| 347 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for x["+toString(i)+"]!"); | 
|---|
| 348 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| 349 | newAtom->set(i, toDouble(*tok_iter)); | 
|---|
| 350 | tok_iter++; | 
|---|
| 351 | } | 
|---|
| 352 | break; | 
|---|
| 353 | case TremoloKey::u : | 
|---|
| 354 | // for the moment, assume there are always three dimensions | 
|---|
| 355 | for (int i=0;i<NDIM;i++) { | 
|---|
| 356 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for u["+toString(i)+"]!"); | 
|---|
| 357 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| 358 | tempVector[i] = toDouble(*tok_iter); | 
|---|
| 359 | tok_iter++; | 
|---|
| 360 | } | 
|---|
| 361 | newAtom->setAtomicVelocity(tempVector); | 
|---|
| 362 | break; | 
|---|
| 363 | case TremoloKey::type : | 
|---|
| 364 | { | 
|---|
| 365 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| 366 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| 367 | std::string element(knownTypes[(*tok_iter)]); | 
|---|
| 368 | // put type name into container for later use | 
|---|
| 369 | atomInfo->set(currentField, *tok_iter); | 
|---|
| 370 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing element " << (*tok_iter) << " as " << element << " according to KnownTypes." << std::endl); | 
|---|
| 371 | tok_iter++; | 
|---|
| 372 | newAtom->setType(World::getInstance().getPeriode()->FindElement(element)); | 
|---|
| 373 | ASSERT(newAtom->getType(), "Type was not set for this atom"); | 
|---|
| 374 | break; | 
|---|
| 375 | } | 
|---|
| 376 | case TremoloKey::Id : | 
|---|
| 377 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| 378 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| 379 | atomIdMap[toInt(*tok_iter)] = newAtom->getId(); | 
|---|
| 380 | tok_iter++; | 
|---|
| 381 | break; | 
|---|
| 382 | case TremoloKey::neighbors : | 
|---|
| 383 | for (int i=0;i<atoi(it->substr(it->find("=") + 1, 1).c_str());i++) { | 
|---|
| 384 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| 385 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| 386 | lineStream << *tok_iter << "\t"; | 
|---|
| 387 | tok_iter++; | 
|---|
| 388 | } | 
|---|
| 389 | readNeighbors(&lineStream, | 
|---|
| 390 | atoi(it->substr(it->find("=") + 1, 1).c_str()), newAtom->getId()); | 
|---|
| 391 | break; | 
|---|
| 392 | default : | 
|---|
| 393 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| 394 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| 395 | atomInfo->set(currentField, *tok_iter); | 
|---|
| 396 | tok_iter++; | 
|---|
| 397 | break; | 
|---|
| 398 | } | 
|---|
| 399 | } | 
|---|
| 400 | if (newmol != NULL) { | 
|---|
| 401 | //DoLog(0) && (Log() << Verbose(0) << "New Atom: " << *newAtom << " with type " << newAtom->getType()->getName() << std::endl); | 
|---|
| 402 | newmol->AddAtom(newAtom); | 
|---|
| 403 | } | 
|---|
| 404 | } | 
|---|
| 405 |  | 
|---|
| 406 | /** | 
|---|
| 407 | * Reads neighbor information for one atom from the input. | 
|---|
| 408 | * | 
|---|
| 409 | * \param line stream where to read the information from | 
|---|
| 410 | * \param numberOfNeighbors number of neighbors to read | 
|---|
| 411 | * \param atomid world id of the atom the information belongs to | 
|---|
| 412 | */ | 
|---|
| 413 | void TremoloParser::readNeighbors(stringstream* line, int numberOfNeighbors, int atomId) { | 
|---|
| 414 | int neighborId = 0; | 
|---|
| 415 | for (int i = 0; i < numberOfNeighbors; i++) { | 
|---|
| 416 | *line >> neighborId; | 
|---|
| 417 | // 0 is used to fill empty neighbor positions in the tremolo file. | 
|---|
| 418 | if (neighborId > 0) { | 
|---|
| 419 | DoLog(4) && (Log() << Verbose(4) | 
|---|
| 420 | << "Atom with global id " << atomId | 
|---|
| 421 | << " has neighbour with serial " << neighborId | 
|---|
| 422 | << std::endl); | 
|---|
| 423 | additionalAtomData[atomId].neighbors.push_back(neighborId); | 
|---|
| 424 | } | 
|---|
| 425 | } | 
|---|
| 426 | } | 
|---|
| 427 |  | 
|---|
| 428 | /** | 
|---|
| 429 | * Checks whether the provided name is within the list of used fields. | 
|---|
| 430 | * | 
|---|
| 431 | * \param field name to check | 
|---|
| 432 | * | 
|---|
| 433 | * \return true if the field name is used | 
|---|
| 434 | */ | 
|---|
| 435 | bool TremoloParser::isUsedField(string fieldName) { | 
|---|
| 436 | bool fieldNameExists = false; | 
|---|
| 437 | for (vector<string>::iterator usedField = usedFields.begin(); usedField != usedFields.end(); usedField++) { | 
|---|
| 438 | if (usedField->substr(0, usedField->find("=")) == fieldName) | 
|---|
| 439 | fieldNameExists = true; | 
|---|
| 440 | } | 
|---|
| 441 |  | 
|---|
| 442 | return fieldNameExists; | 
|---|
| 443 | } | 
|---|
| 444 |  | 
|---|
| 445 |  | 
|---|
| 446 | /** | 
|---|
| 447 | * Adds the collected neighbor information to the atoms in the world. The atoms | 
|---|
| 448 | * are found by their current ID and mapped to the corresponding atoms with the | 
|---|
| 449 | * Id found in the parsed file. | 
|---|
| 450 | */ | 
|---|
| 451 | void TremoloParser::processNeighborInformation() { | 
|---|
| 452 | if (!isUsedField("neighbors")) { | 
|---|
| 453 | return; | 
|---|
| 454 | } | 
|---|
| 455 |  | 
|---|
| 456 | for(map<int, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
| 457 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
| 458 | ) { | 
|---|
| 459 | if (!currentInfo->second.neighbors_processed) { | 
|---|
| 460 | for(vector<int>::iterator neighbor = currentInfo->second.neighbors.begin(); | 
|---|
| 461 | neighbor != currentInfo->second.neighbors.end(); neighbor++ | 
|---|
| 462 | ) { | 
|---|
| 463 | //        DoLog(1) && (Log() << Verbose(1) << "Creating bond between (" | 
|---|
| 464 | //            << currentInfo->first | 
|---|
| 465 | //            << ") and (" | 
|---|
| 466 | //            << atomIdMap[*neighbor] << "|" << *neighbor << ")" << std::endl); | 
|---|
| 467 | World::getInstance().getAtom(AtomById(currentInfo->first)) | 
|---|
| 468 | ->addBond(WorldTime::getTime(), World::getInstance().getAtom(AtomById(atomIdMap[*neighbor]))); | 
|---|
| 469 | } | 
|---|
| 470 | currentInfo->second.neighbors_processed = true; | 
|---|
| 471 | } | 
|---|
| 472 | } | 
|---|
| 473 | } | 
|---|
| 474 |  | 
|---|
| 475 | /** | 
|---|
| 476 | * Replaces atom IDs read from the file by the corresponding world IDs. All IDs | 
|---|
| 477 | * IDs of the input string will be replaced; expected separating characters are | 
|---|
| 478 | * "-" and ",". | 
|---|
| 479 | * | 
|---|
| 480 | * \param string in which atom IDs should be adapted | 
|---|
| 481 | * | 
|---|
| 482 | * \return input string with modified atom IDs | 
|---|
| 483 | */ | 
|---|
| 484 | string TremoloParser::adaptIdDependentDataString(string data) { | 
|---|
| 485 | // there might be no IDs | 
|---|
| 486 | if (data == "-") { | 
|---|
| 487 | return "-"; | 
|---|
| 488 | } | 
|---|
| 489 |  | 
|---|
| 490 | char separator; | 
|---|
| 491 | int id; | 
|---|
| 492 | stringstream line, result; | 
|---|
| 493 | line << data; | 
|---|
| 494 |  | 
|---|
| 495 | line >> id; | 
|---|
| 496 | result << atomIdMap[id]; | 
|---|
| 497 | while (line.good()) { | 
|---|
| 498 | line >> separator >> id; | 
|---|
| 499 | result << separator << atomIdMap[id]; | 
|---|
| 500 | } | 
|---|
| 501 |  | 
|---|
| 502 | return result.str(); | 
|---|
| 503 | } | 
|---|
| 504 |  | 
|---|
| 505 | /** | 
|---|
| 506 | * Corrects the atom IDs in each imprData entry to the corresponding world IDs | 
|---|
| 507 | * as they might differ from the originally read IDs. | 
|---|
| 508 | */ | 
|---|
| 509 | void TremoloParser::adaptImprData() { | 
|---|
| 510 | if (!isUsedField("imprData")) { | 
|---|
| 511 | return; | 
|---|
| 512 | } | 
|---|
| 513 |  | 
|---|
| 514 | for(map<int, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
| 515 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
| 516 | ) { | 
|---|
| 517 | currentInfo->second.imprData = adaptIdDependentDataString(currentInfo->second.imprData); | 
|---|
| 518 | } | 
|---|
| 519 | } | 
|---|
| 520 |  | 
|---|
| 521 | /** | 
|---|
| 522 | * Corrects the atom IDs in each torsion entry to the corresponding world IDs | 
|---|
| 523 | * as they might differ from the originally read IDs. | 
|---|
| 524 | */ | 
|---|
| 525 | void TremoloParser::adaptTorsion() { | 
|---|
| 526 | if (!isUsedField("torsion")) { | 
|---|
| 527 | return; | 
|---|
| 528 | } | 
|---|
| 529 |  | 
|---|
| 530 | for(map<int, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
| 531 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
| 532 | ) { | 
|---|
| 533 | currentInfo->second.torsion = adaptIdDependentDataString(currentInfo->second.torsion); | 
|---|
| 534 | } | 
|---|
| 535 | } | 
|---|
| 536 |  | 
|---|
| 537 | /** Creates knownTypes as the identity, e.g. H -> H, He -> He, ... . | 
|---|
| 538 | * | 
|---|
| 539 | */ | 
|---|
| 540 | void TremoloParser::createKnownTypesByIdentity() | 
|---|
| 541 | { | 
|---|
| 542 | // remove old mapping | 
|---|
| 543 | knownTypes.clear(); | 
|---|
| 544 | // make knownTypes the identity mapping | 
|---|
| 545 | const periodentafel *periode = World::getInstance().getPeriode(); | 
|---|
| 546 | for (periodentafel::const_iterator iter = periode->begin(); | 
|---|
| 547 | iter != periode->end(); | 
|---|
| 548 | ++iter) { | 
|---|
| 549 | knownTypes.insert( make_pair(iter->second->getSymbol(), iter->second->getSymbol()) ); | 
|---|
| 550 | } | 
|---|
| 551 | } | 
|---|
| 552 |  | 
|---|
| 553 | /** Parses a .potentials file and creates from it the knownTypes file. | 
|---|
| 554 | * | 
|---|
| 555 | * @param file input stream of .potentials file | 
|---|
| 556 | */ | 
|---|
| 557 | void TremoloParser::parseKnownTypes(std::istream &file) | 
|---|
| 558 | { | 
|---|
| 559 | const periodentafel *periode = World::getInstance().getPeriode(); | 
|---|
| 560 | // remove old mapping | 
|---|
| 561 | knownTypes.clear(); | 
|---|
| 562 |  | 
|---|
| 563 | DoLog(3) && (Log() << Verbose(3) << "additionalAtomData contains: " << additionalAtomData << std::endl); | 
|---|
| 564 |  | 
|---|
| 565 | // parse in file | 
|---|
| 566 | typedef boost::tokenizer<boost::char_separator<char> > | 
|---|
| 567 | tokenizer; | 
|---|
| 568 | boost::char_separator<char> tokensep(":\t ,;"); | 
|---|
| 569 | boost::char_separator<char> equalitysep("\t ="); | 
|---|
| 570 | std::string line; | 
|---|
| 571 | while (file.good()) { | 
|---|
| 572 | std::getline( file, line ); | 
|---|
| 573 | DoLog(4) && (Log() << Verbose(4) << "INFO: full line of parameters is '" << line << "'" << std::endl); | 
|---|
| 574 | if (line.find("particle:") != string::npos) { | 
|---|
| 575 | DoLog(3) && (Log() << Verbose(3) << "INFO: found line '" << line << "' containing keyword 'particle:'." << std::endl); | 
|---|
| 576 | tokenizer tokens(line, tokensep); | 
|---|
| 577 | ASSERT(tokens.begin() != tokens.end(), | 
|---|
| 578 | "TremoloParser::parseKnownTypes() - line with 'particle:' but no particles separated by comma."); | 
|---|
| 579 | // look for particle_type | 
|---|
| 580 | std::string particle_type("NULL"); | 
|---|
| 581 | std::string element_type("NULL"); | 
|---|
| 582 | for (tokenizer::iterator tok_iter = tokens.begin(); | 
|---|
| 583 | tok_iter != tokens.end(); | 
|---|
| 584 | ++tok_iter) { | 
|---|
| 585 | if ((*tok_iter).find("particle_type") != string::npos) { | 
|---|
| 586 | DoLog(3) && (Log() << Verbose(3) << "INFO: found line '" << line << "' containing keyword 'particle_type'." << std::endl); | 
|---|
| 587 | tokenizer token((*tok_iter), equalitysep); | 
|---|
| 588 | ASSERT(token.begin() != token.end(), | 
|---|
| 589 | "TremoloParser::parseKnownTypes() - could not split particle_type by equality sign"); | 
|---|
| 590 | tokenizer::iterator particle_iter = token.begin(); | 
|---|
| 591 | particle_iter++; | 
|---|
| 592 | particle_type = *particle_iter; | 
|---|
| 593 | } | 
|---|
| 594 | if ((*tok_iter).find("element_name") != string::npos) { | 
|---|
| 595 | DoLog(3) && (Log() << Verbose(3) << "INFO: found line '" << line << "' containing keyword 'element_name'." << std::endl); | 
|---|
| 596 | tokenizer token((*tok_iter), equalitysep); | 
|---|
| 597 | ASSERT(token.begin() != token.end(), | 
|---|
| 598 | "TremoloParser::parseKnownTypes() - could not split particle_type by equality sign"); | 
|---|
| 599 | tokenizer::iterator element_iter = token.begin(); | 
|---|
| 600 | element_iter++; | 
|---|
| 601 | element_type = *element_iter; | 
|---|
| 602 | } | 
|---|
| 603 | } | 
|---|
| 604 | if ((particle_type != "NULL") && (element_type != "NULL")) { | 
|---|
| 605 | if (periode->FindElement(element_type) != NULL) { | 
|---|
| 606 | DoLog(1) && (Log() << Verbose(1) << "INFO: Added Type " << particle_type << " as reference to element " << element_type << "." << std::endl); | 
|---|
| 607 | knownTypes.insert( make_pair (particle_type, element_type) ); | 
|---|
| 608 | } else { | 
|---|
| 609 | DoeLog(1) && (Log() << Verbose(1) << "INFO: Either Type " << particle_type << " or " << element_type << " could not be recognized." << std::endl); | 
|---|
| 610 | } | 
|---|
| 611 | } else { | 
|---|
| 612 | DoeLog(1) && (Log() << Verbose(1) << "INFO: Desired element " << element_type << " is not known." << std::endl); | 
|---|
| 613 | } | 
|---|
| 614 | } | 
|---|
| 615 | } | 
|---|
| 616 |  | 
|---|
| 617 | } | 
|---|