| [bcf653] | 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 | 
 | 
|---|
| [6bc51d] | 8 | /*
 | 
|---|
 | 9 |  * TremoloParser.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Mar 2, 2010
 | 
|---|
 | 12 |  *      Author: metzler
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 21 | 
 | 
|---|
| [ad011c] | 22 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 23 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [4d4d33] | 24 | #include "CodePatterns/toString.hpp"
 | 
|---|
| [ad011c] | 25 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [9131f3] | 26 | #include "TremoloParser.hpp"
 | 
|---|
 | 27 | #include "World.hpp"
 | 
|---|
| [9d83b6] | 28 | #include "WorldTime.hpp"
 | 
|---|
| [9131f3] | 29 | #include "atom.hpp"
 | 
|---|
| [129204] | 30 | #include "Bond/bond.hpp"
 | 
|---|
| [dc1d9e] | 31 | #include "element.hpp"
 | 
|---|
 | 32 | #include "molecule.hpp"
 | 
|---|
| [9131f3] | 33 | #include "periodentafel.hpp"
 | 
|---|
| [b8d4a3] | 34 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [9131f3] | 35 | #include <map>
 | 
|---|
 | 36 | #include <vector>
 | 
|---|
 | 37 | 
 | 
|---|
| [72d108] | 38 | #include <boost/tokenizer.hpp>
 | 
|---|
| [74a444] | 39 | #include <iostream>
 | 
|---|
 | 40 | #include <iomanip>
 | 
|---|
| [b8d4a3] | 41 | 
 | 
|---|
| [9131f3] | 42 | using namespace std;
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | /**
 | 
|---|
 | 45 |  * Constructor.
 | 
|---|
 | 46 |  */
 | 
|---|
 | 47 | TremoloParser::TremoloParser() {
 | 
|---|
| [b8d4a3] | 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;
 | 
|---|
| [52baf9] | 69 | 
 | 
|---|
| [4d4d33] | 70 |   createKnownTypesByIdentity();
 | 
|---|
 | 71 | 
 | 
|---|
| [52baf9] | 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);
 | 
|---|
| [ff3c40] | 75 | 
 | 
|---|
 | 76 |   // and noKey afterwards(!) such that it is not used in usedFields
 | 
|---|
 | 77 |   knownKeys[" "] = TremoloKey::noKey; // with this we can detect invalid keys
 | 
|---|
| [4d4d33] | 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();
 | 
|---|
| [9131f3] | 84 | }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | /**
 | 
|---|
 | 87 |  * Destructor.
 | 
|---|
 | 88 |  */
 | 
|---|
 | 89 | TremoloParser::~TremoloParser() {
 | 
|---|
| [b8d4a3] | 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 | 
 | 
|---|
| [0bbfa1] | 105 |   // reset atomIdMap, for we now get new serials
 | 
|---|
 | 106 |   atomIdMap.clear();
 | 
|---|
| [b8d4a3] | 107 |   usedFields.clear();
 | 
|---|
| [0bbfa1] | 108 | 
 | 
|---|
| [dc1d9e] | 109 |   molecule *newmol = World::getInstance().createMolecule();
 | 
|---|
| [bd2390] | 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);
 | 
|---|
| [b8d4a3] | 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) != '#') {
 | 
|---|
| [dc1d9e] | 122 |       readAtomDataLine(line, newmol);
 | 
|---|
| [b8d4a3] | 123 |     }
 | 
|---|
 | 124 |   }
 | 
|---|
| [4afa46] | 125 |   // refresh atom::nr and atom::name
 | 
|---|
 | 126 |   newmol->getAtomCount();
 | 
|---|
| [b8d4a3] | 127 | 
 | 
|---|
 | 128 |   processNeighborInformation();
 | 
|---|
 | 129 |   adaptImprData();
 | 
|---|
 | 130 |   adaptTorsion();
 | 
|---|
 | 131 | }
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 | /**
 | 
|---|
| [73916f] | 134 |  * Saves the \a atoms into as a tremolo file.
 | 
|---|
| [b8d4a3] | 135 |  *
 | 
|---|
 | 136 |  * \param file where to save the state
 | 
|---|
| [73916f] | 137 |  * \param atoms atoms to store
 | 
|---|
| [b8d4a3] | 138 |  */
 | 
|---|
| [73916f] | 139 | void TremoloParser::save(ostream* file, const std::vector<atom *> &AtomList) {
 | 
|---|
| [e97a44] | 140 |   DoLog(0) && (Log() << Verbose(0) << "Saving changes to tremolo." << std::endl);
 | 
|---|
 | 141 | 
 | 
|---|
| [73916f] | 142 |   vector<atom*>::const_iterator atomIt;
 | 
|---|
| [b8d4a3] | 143 |   vector<string>::iterator it;
 | 
|---|
 | 144 | 
 | 
|---|
| [acd638] | 145 |   DoLog(3) && (Log() << Verbose(3) << "additionalAtomData contains: " << additionalAtomData << std::endl);
 | 
|---|
 | 146 | 
 | 
|---|
| [b8d4a3] | 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 | 
 | 
|---|
| [acd638] | 179 |   DoLog(4) && (Log() << Verbose(4) << "INFO: Saving atom " << *currentAtom << ", its father id is " << currentAtom->GetTrueFather()->getId() << std::endl);
 | 
|---|
| [4d4d33] | 180 | 
 | 
|---|
| [b8d4a3] | 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
 | 
|---|
| [4d4d33] | 186 |         DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getPosition() << std::endl);
 | 
|---|
| [d74077] | 187 |         *file << currentAtom->at(0) << "\t";
 | 
|---|
 | 188 |         *file << currentAtom->at(1) << "\t";
 | 
|---|
 | 189 |         *file << currentAtom->at(2) << "\t";
 | 
|---|
| [b8d4a3] | 190 |         break;
 | 
|---|
 | 191 |       case TremoloKey::u :
 | 
|---|
 | 192 |         // for the moment, assume there are always three dimensions
 | 
|---|
| [bce72c] | 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";
 | 
|---|
| [b8d4a3] | 197 |         break;
 | 
|---|
 | 198 |       case TremoloKey::Type :
 | 
|---|
| [acd638] | 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 |           }
 | 
|---|
| [4d4d33] | 215 |         } else {
 | 
|---|
| [acd638] | 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";
 | 
|---|
| [4d4d33] | 218 |         }
 | 
|---|
| [b8d4a3] | 219 |         break;
 | 
|---|
 | 220 |       case TremoloKey::Id :
 | 
|---|
| [4d4d33] | 221 |         DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getId()+1 << std::endl);
 | 
|---|
| [dc1d9e] | 222 |         *file << currentAtom->getId()+1 << "\t";
 | 
|---|
| [b8d4a3] | 223 |         break;
 | 
|---|
 | 224 |       case TremoloKey::neighbors :
 | 
|---|
| [4d4d33] | 225 |         DoLog(3) && (Log() << Verbose(3) << "Writing type " << knownKeyNames[currentField] << std::endl);
 | 
|---|
| [b8d4a3] | 226 |         writeNeighbors(file, atoi(it->substr(it->find("=") + 1, 1).c_str()), currentAtom);
 | 
|---|
 | 227 |         break;
 | 
|---|
| [74a444] | 228 |       case TremoloKey::resSeq :
 | 
|---|
| [4d4d33] | 229 |         if (additionalAtomData.count(currentAtom->getId())) {
 | 
|---|
 | 230 |           DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField) << std::endl);
 | 
|---|
| [74a444] | 231 |           *file << additionalAtomData[currentAtom->getId()].get(currentField);
 | 
|---|
 | 232 |         } else if (currentAtom->getMolecule() != NULL) {
 | 
|---|
| [4d4d33] | 233 |           DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " its own id: " << currentAtom->getMolecule()->getId()+1 << std::endl);
 | 
|---|
| [74a444] | 234 |           *file << setw(4) << currentAtom->getMolecule()->getId()+1;
 | 
|---|
 | 235 |         } else {
 | 
|---|
| [4d4d33] | 236 |           DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " default value: " << defaultAdditionalData.get(currentField) << std::endl);
 | 
|---|
| [74a444] | 237 |           *file << defaultAdditionalData.get(currentField);
 | 
|---|
 | 238 |         }
 | 
|---|
 | 239 |         *file << "\t";
 | 
|---|
| [4d4d33] | 240 |         break;
 | 
|---|
| [b8d4a3] | 241 |       default :
 | 
|---|
| [4d4d33] | 242 |         if (additionalAtomData.count(currentAtom->getId())) {
 | 
|---|
 | 243 |           DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField) << std::endl);
 | 
|---|
| [74a444] | 244 |           *file << additionalAtomData[currentAtom->getId()].get(currentField);
 | 
|---|
| [4d4d33] | 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);
 | 
|---|
| [74a444] | 247 |           *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField);
 | 
|---|
 | 248 |         } else {
 | 
|---|
| [4d4d33] | 249 |           DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " the default: " << defaultAdditionalData.get(currentField) << std::endl);
 | 
|---|
| [74a444] | 250 |           *file << defaultAdditionalData.get(currentField);
 | 
|---|
 | 251 |         }
 | 
|---|
| [b8d4a3] | 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 |  *
 | 
|---|
| [9d83b6] | 263 |  * Note that ListOfBonds of WorldTime::CurrentTime is used.
 | 
|---|
 | 264 |  *
 | 
|---|
| [b8d4a3] | 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) {
 | 
|---|
| [9d83b6] | 270 |   const BondList& ListOfBonds = currentAtom->getListOfBonds();
 | 
|---|
 | 271 |   BondList::const_iterator currentBond = ListOfBonds.begin();
 | 
|---|
| [b8d4a3] | 272 |   for (int i = 0; i < numberOfNeighbors; i++) {
 | 
|---|
| [9d83b6] | 273 |     *file << (currentBond != ListOfBonds.end()
 | 
|---|
| [dc1d9e] | 274 |         ? (*currentBond)->GetOtherAtom(currentAtom)->getId()+1 : 0) << "\t";
 | 
|---|
| [9d83b6] | 275 |     if (currentBond != ListOfBonds.end())
 | 
|---|
| [0bbfa1] | 276 |       ++currentBond;
 | 
|---|
| [b8d4a3] | 277 |   }
 | 
|---|
| [9131f3] | 278 | }
 | 
|---|
 | 279 | 
 | 
|---|
 | 280 | /**
 | 
|---|
 | 281 |  * Stores keys from the ATOMDATA line.
 | 
|---|
 | 282 |  *
 | 
|---|
 | 283 |  * \param line to parse the keys from
 | 
|---|
 | 284 |  * \param with which offset the keys begin within the line
 | 
|---|
 | 285 |  */
 | 
|---|
 | 286 | void TremoloParser::parseAtomDataKeysLine(string line, int offset) {
 | 
|---|
 | 287 |   string keyword;
 | 
|---|
 | 288 |   stringstream lineStream;
 | 
|---|
 | 289 | 
 | 
|---|
 | 290 |   lineStream << line.substr(offset);
 | 
|---|
| [52baf9] | 291 |   usedFields.clear();
 | 
|---|
| [9131f3] | 292 |   while (lineStream.good()) {
 | 
|---|
 | 293 |     lineStream >> keyword;
 | 
|---|
| [b8d4a3] | 294 |     if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey) {
 | 
|---|
| [ecb799] | 295 |       // TODO: throw exception about unknown key
 | 
|---|
| [4415da] | 296 |       cout << "Unknown key: " << keyword << " is not part of the tremolo format specification." << endl;
 | 
|---|
 | 297 |       break;
 | 
|---|
 | 298 |     }
 | 
|---|
| [9131f3] | 299 |     usedFields.push_back(keyword);
 | 
|---|
 | 300 |   }
 | 
|---|
| [72d108] | 301 |   //DoLog(1) && (Log() << Verbose(1) << "INFO: " << usedFields << std::endl);
 | 
|---|
| [9131f3] | 302 | }
 | 
|---|
 | 303 | 
 | 
|---|
 | 304 | /**
 | 
|---|
 | 305 |  * Reads one data line of a tremolo file and interprets it according to the keys
 | 
|---|
 | 306 |  * obtained from the ATOMDATA line.
 | 
|---|
 | 307 |  *
 | 
|---|
 | 308 |  * \param line to parse as an atom
 | 
|---|
| [dc1d9e] | 309 |  * \param *newmol molecule to add atom to
 | 
|---|
| [9131f3] | 310 |  */
 | 
|---|
| [dc1d9e] | 311 | void TremoloParser::readAtomDataLine(string line, molecule *newmol = NULL) {
 | 
|---|
| [9131f3] | 312 |   vector<string>::iterator it;
 | 
|---|
 | 313 |   stringstream lineStream;
 | 
|---|
| [4415da] | 314 |   atom* newAtom = World::getInstance().createAtom();
 | 
|---|
| [b8d4a3] | 315 |   TremoloAtomInfoContainer *atomInfo = NULL;
 | 
|---|
| [24f128] | 316 |   additionalAtomData[newAtom->getId()] = TremoloAtomInfoContainer(); // fill with default values
 | 
|---|
| [b8d4a3] | 317 |   atomInfo = &additionalAtomData[newAtom->getId()];
 | 
|---|
 | 318 |   TremoloKey::atomDataKey currentField;
 | 
|---|
| [72d108] | 319 |   ConvertTo<double> toDouble;
 | 
|---|
 | 320 |   ConvertTo<int> toInt;
 | 
|---|
| [056e70] | 321 |   Vector tempVector;
 | 
|---|
| [72d108] | 322 | 
 | 
|---|
 | 323 |   // setup tokenizer, splitting up white-spaced entries
 | 
|---|
 | 324 |   typedef boost::tokenizer<boost::char_separator<char> >
 | 
|---|
 | 325 |       tokenizer;
 | 
|---|
 | 326 |   boost::char_separator<char> whitespacesep(" \t");
 | 
|---|
 | 327 |   tokenizer tokens(line, whitespacesep);
 | 
|---|
 | 328 |   ASSERT(tokens.begin() != tokens.end(),
 | 
|---|
 | 329 |       "TremoloParser::readAtomDataLine - empty string, need at least ' '!");
 | 
|---|
 | 330 |   tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
 | 331 |   // then associate each token to each file
 | 
|---|
| [b8d4a3] | 332 |   for (it = usedFields.begin(); it < usedFields.end(); it++) {
 | 
|---|
| [72d108] | 333 |     const std::string keyName = it->substr(0, it->find("="));
 | 
|---|
 | 334 |     currentField = knownKeys[keyName];
 | 
|---|
 | 335 |     const string word = *tok_iter;
 | 
|---|
| [056e70] | 336 |     DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with remaining data " << word << std::endl);
 | 
|---|
| [4415da] | 337 |     switch (currentField) {
 | 
|---|
| [b8d4a3] | 338 |       case TremoloKey::x :
 | 
|---|
| [4415da] | 339 |         // for the moment, assume there are always three dimensions
 | 
|---|
| [d74077] | 340 |         for (int i=0;i<NDIM;i++) {
 | 
|---|
| [72d108] | 341 |           ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for x["+toString(i)+"]!");
 | 
|---|
| [056e70] | 342 |           DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl);
 | 
|---|
| [72d108] | 343 |           newAtom->set(i, toDouble(*tok_iter));
 | 
|---|
 | 344 |           tok_iter++;
 | 
|---|
| [d74077] | 345 |         }
 | 
|---|
| [4415da] | 346 |         break;
 | 
|---|
| [b8d4a3] | 347 |       case TremoloKey::u :
 | 
|---|
| [4415da] | 348 |         // for the moment, assume there are always three dimensions
 | 
|---|
| [72d108] | 349 |         for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 350 |           ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for u["+toString(i)+"]!");
 | 
|---|
| [056e70] | 351 |           DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl);
 | 
|---|
 | 352 |           tempVector[i] = toDouble(*tok_iter);
 | 
|---|
| [72d108] | 353 |           tok_iter++;
 | 
|---|
 | 354 |         }
 | 
|---|
| [056e70] | 355 |         newAtom->setAtomicVelocity(tempVector);
 | 
|---|
| [4415da] | 356 |         break;
 | 
|---|
| [b8d4a3] | 357 |       case TremoloKey::Type :
 | 
|---|
| [4d4d33] | 358 |       {
 | 
|---|
| [72d108] | 359 |         ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!");
 | 
|---|
| [056e70] | 360 |         DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl);
 | 
|---|
| [4d4d33] | 361 |         std::string element(knownTypes[(*tok_iter)]);
 | 
|---|
 | 362 |         // put type name into container for later use
 | 
|---|
 | 363 |         atomInfo->set(currentField, *tok_iter);
 | 
|---|
| [056e70] | 364 |         DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing element " << (*tok_iter) << " as " << element << " according to KnownTypes." << std::endl);
 | 
|---|
| [72d108] | 365 |         tok_iter++;
 | 
|---|
| [4d4d33] | 366 |         newAtom->setType(World::getInstance().getPeriode()->FindElement(element));
 | 
|---|
| [b8d4a3] | 367 |         ASSERT(newAtom->getType(), "Type was not set for this atom");
 | 
|---|
| [4415da] | 368 |         break;
 | 
|---|
| [4d4d33] | 369 |       }
 | 
|---|
| [b8d4a3] | 370 |       case TremoloKey::Id :
 | 
|---|
| [72d108] | 371 |         ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!");
 | 
|---|
| [056e70] | 372 |         DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl);
 | 
|---|
| [72d108] | 373 |         atomIdMap[toInt(*tok_iter)] = newAtom->getId();
 | 
|---|
 | 374 |         tok_iter++;
 | 
|---|
| [4415da] | 375 |         break;
 | 
|---|
| [b8d4a3] | 376 |       case TremoloKey::neighbors :
 | 
|---|
| [72d108] | 377 |         for (int i=0;i<atoi(it->substr(it->find("=") + 1, 1).c_str());i++) {
 | 
|---|
 | 378 |           ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!");
 | 
|---|
| [056e70] | 379 |           DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl);
 | 
|---|
| [72d108] | 380 |           lineStream << *tok_iter << "\t";
 | 
|---|
 | 381 |           tok_iter++;
 | 
|---|
 | 382 |         }
 | 
|---|
| [b8d4a3] | 383 |         readNeighbors(&lineStream,
 | 
|---|
 | 384 |             atoi(it->substr(it->find("=") + 1, 1).c_str()), newAtom->getId());
 | 
|---|
| [9131f3] | 385 |         break;
 | 
|---|
 | 386 |       default :
 | 
|---|
| [72d108] | 387 |         ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!");
 | 
|---|
| [056e70] | 388 |         DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl);
 | 
|---|
| [72d108] | 389 |         atomInfo->set(currentField, *tok_iter);
 | 
|---|
 | 390 |         tok_iter++;
 | 
|---|
| [9131f3] | 391 |         break;
 | 
|---|
 | 392 |     }
 | 
|---|
 | 393 |   }
 | 
|---|
| [72d108] | 394 |   if (newmol != NULL) {
 | 
|---|
 | 395 |     //DoLog(0) && (Log() << Verbose(0) << "New Atom: " << *newAtom << " with type " << newAtom->getType()->getName() << std::endl);
 | 
|---|
| [dc1d9e] | 396 |     newmol->AddAtom(newAtom);
 | 
|---|
| [72d108] | 397 |   }
 | 
|---|
| [6bc51d] | 398 | }
 | 
|---|
| [9131f3] | 399 | 
 | 
|---|
| [b8d4a3] | 400 | /**
 | 
|---|
 | 401 |  * Reads neighbor information for one atom from the input.
 | 
|---|
 | 402 |  *
 | 
|---|
| [0bbfa1] | 403 |  * \param line stream where to read the information from
 | 
|---|
 | 404 |  * \param numberOfNeighbors number of neighbors to read
 | 
|---|
 | 405 |  * \param atomid world id of the atom the information belongs to
 | 
|---|
| [b8d4a3] | 406 |  */
 | 
|---|
 | 407 | void TremoloParser::readNeighbors(stringstream* line, int numberOfNeighbors, int atomId) {
 | 
|---|
 | 408 |   int neighborId = 0;
 | 
|---|
 | 409 |   for (int i = 0; i < numberOfNeighbors; i++) {
 | 
|---|
 | 410 |     *line >> neighborId;
 | 
|---|
 | 411 |     // 0 is used to fill empty neighbor positions in the tremolo file.
 | 
|---|
 | 412 |     if (neighborId > 0) {
 | 
|---|
| [056e70] | 413 |       DoLog(4) && (Log() << Verbose(4)
 | 
|---|
 | 414 |           << "Atom with global id " << atomId
 | 
|---|
 | 415 |           << " has neighbour with serial " << neighborId
 | 
|---|
 | 416 |           << std::endl);
 | 
|---|
| [b8d4a3] | 417 |       additionalAtomData[atomId].neighbors.push_back(neighborId);
 | 
|---|
 | 418 |     }
 | 
|---|
 | 419 |   }
 | 
|---|
 | 420 | }
 | 
|---|
| [9131f3] | 421 | 
 | 
|---|
 | 422 | /**
 | 
|---|
| [b8d4a3] | 423 |  * Checks whether the provided name is within the list of used fields.
 | 
|---|
| [9131f3] | 424 |  *
 | 
|---|
| [b8d4a3] | 425 |  * \param field name to check
 | 
|---|
 | 426 |  *
 | 
|---|
 | 427 |  * \return true if the field name is used
 | 
|---|
| [9131f3] | 428 |  */
 | 
|---|
| [b8d4a3] | 429 | bool TremoloParser::isUsedField(string fieldName) {
 | 
|---|
 | 430 |   bool fieldNameExists = false;
 | 
|---|
 | 431 |   for (vector<string>::iterator usedField = usedFields.begin(); usedField != usedFields.end(); usedField++) {
 | 
|---|
 | 432 |     if (usedField->substr(0, usedField->find("=")) == fieldName)
 | 
|---|
 | 433 |       fieldNameExists = true;
 | 
|---|
 | 434 |   }
 | 
|---|
| [9131f3] | 435 | 
 | 
|---|
| [b8d4a3] | 436 |   return fieldNameExists;
 | 
|---|
 | 437 | }
 | 
|---|
 | 438 | 
 | 
|---|
 | 439 | 
 | 
|---|
 | 440 | /**
 | 
|---|
 | 441 |  * Adds the collected neighbor information to the atoms in the world. The atoms
 | 
|---|
 | 442 |  * are found by their current ID and mapped to the corresponding atoms with the
 | 
|---|
 | 443 |  * Id found in the parsed file.
 | 
|---|
 | 444 |  */
 | 
|---|
 | 445 | void TremoloParser::processNeighborInformation() {
 | 
|---|
 | 446 |   if (!isUsedField("neighbors")) {
 | 
|---|
 | 447 |     return;
 | 
|---|
 | 448 |   }
 | 
|---|
 | 449 | 
 | 
|---|
 | 450 |   for(map<int, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin();
 | 
|---|
 | 451 |     currentInfo != additionalAtomData.end(); currentInfo++
 | 
|---|
 | 452 |   ) {
 | 
|---|
| [0bbfa1] | 453 |     if (!currentInfo->second.neighbors_processed) {
 | 
|---|
 | 454 |       for(vector<int>::iterator neighbor = currentInfo->second.neighbors.begin();
 | 
|---|
 | 455 |         neighbor != currentInfo->second.neighbors.end(); neighbor++
 | 
|---|
 | 456 |       ) {
 | 
|---|
| [72d108] | 457 | //        DoLog(1) && (Log() << Verbose(1) << "Creating bond between ("
 | 
|---|
| [0bbfa1] | 458 | //            << currentInfo->first
 | 
|---|
 | 459 | //            << ") and ("
 | 
|---|
| [72d108] | 460 | //            << atomIdMap[*neighbor] << "|" << *neighbor << ")" << std::endl);
 | 
|---|
| [0bbfa1] | 461 |         World::getInstance().getAtom(AtomById(currentInfo->first))
 | 
|---|
| [073a9e4] | 462 |             ->addBond(WorldTime::getTime(), World::getInstance().getAtom(AtomById(atomIdMap[*neighbor])));
 | 
|---|
| [0bbfa1] | 463 |       }
 | 
|---|
 | 464 |       currentInfo->second.neighbors_processed = true;
 | 
|---|
| [9131f3] | 465 |     }
 | 
|---|
 | 466 |   }
 | 
|---|
| [6bc51d] | 467 | }
 | 
|---|
 | 468 | 
 | 
|---|
| [9131f3] | 469 | /**
 | 
|---|
| [b8d4a3] | 470 |  * Replaces atom IDs read from the file by the corresponding world IDs. All IDs
 | 
|---|
 | 471 |  * IDs of the input string will be replaced; expected separating characters are
 | 
|---|
 | 472 |  * "-" and ",".
 | 
|---|
| [9131f3] | 473 |  *
 | 
|---|
| [b8d4a3] | 474 |  * \param string in which atom IDs should be adapted
 | 
|---|
 | 475 |  *
 | 
|---|
 | 476 |  * \return input string with modified atom IDs
 | 
|---|
| [9131f3] | 477 |  */
 | 
|---|
| [b8d4a3] | 478 | string TremoloParser::adaptIdDependentDataString(string data) {
 | 
|---|
 | 479 |   // there might be no IDs
 | 
|---|
 | 480 |   if (data == "-") {
 | 
|---|
 | 481 |     return "-";
 | 
|---|
 | 482 |   }
 | 
|---|
 | 483 | 
 | 
|---|
 | 484 |   char separator;
 | 
|---|
 | 485 |   int id;
 | 
|---|
 | 486 |   stringstream line, result;
 | 
|---|
 | 487 |   line << data;
 | 
|---|
 | 488 | 
 | 
|---|
 | 489 |   line >> id;
 | 
|---|
 | 490 |   result << atomIdMap[id];
 | 
|---|
 | 491 |   while (line.good()) {
 | 
|---|
 | 492 |     line >> separator >> id;
 | 
|---|
 | 493 |     result << separator << atomIdMap[id];
 | 
|---|
 | 494 |   }
 | 
|---|
 | 495 | 
 | 
|---|
 | 496 |   return result.str();
 | 
|---|
| [6bc51d] | 497 | }
 | 
|---|
| [b8d4a3] | 498 | 
 | 
|---|
 | 499 | /**
 | 
|---|
 | 500 |  * Corrects the atom IDs in each imprData entry to the corresponding world IDs
 | 
|---|
 | 501 |  * as they might differ from the originally read IDs.
 | 
|---|
 | 502 |  */
 | 
|---|
 | 503 | void TremoloParser::adaptImprData() {
 | 
|---|
 | 504 |   if (!isUsedField("imprData")) {
 | 
|---|
 | 505 |     return;
 | 
|---|
 | 506 |   }
 | 
|---|
 | 507 | 
 | 
|---|
 | 508 |   for(map<int, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin();
 | 
|---|
 | 509 |     currentInfo != additionalAtomData.end(); currentInfo++
 | 
|---|
 | 510 |   ) {
 | 
|---|
 | 511 |     currentInfo->second.imprData = adaptIdDependentDataString(currentInfo->second.imprData);
 | 
|---|
 | 512 |   }
 | 
|---|
| [6bc51d] | 513 | }
 | 
|---|
| [4415da] | 514 | 
 | 
|---|
| [b8d4a3] | 515 | /**
 | 
|---|
 | 516 |  * Corrects the atom IDs in each torsion entry to the corresponding world IDs
 | 
|---|
 | 517 |  * as they might differ from the originally read IDs.
 | 
|---|
 | 518 |  */
 | 
|---|
 | 519 | void TremoloParser::adaptTorsion() {
 | 
|---|
 | 520 |   if (!isUsedField("torsion")) {
 | 
|---|
 | 521 |     return;
 | 
|---|
 | 522 |   }
 | 
|---|
 | 523 | 
 | 
|---|
 | 524 |   for(map<int, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin();
 | 
|---|
 | 525 |     currentInfo != additionalAtomData.end(); currentInfo++
 | 
|---|
 | 526 |   ) {
 | 
|---|
 | 527 |     currentInfo->second.torsion = adaptIdDependentDataString(currentInfo->second.torsion);
 | 
|---|
 | 528 |   }
 | 
|---|
 | 529 | }
 | 
|---|
 | 530 | 
 | 
|---|
| [4d4d33] | 531 | /** Creates knownTypes as the identity, e.g. H -> H, He -> He, ... .
 | 
|---|
 | 532 |  *
 | 
|---|
 | 533 |  */
 | 
|---|
 | 534 | void TremoloParser::createKnownTypesByIdentity()
 | 
|---|
 | 535 | {
 | 
|---|
 | 536 |   // remove old mapping
 | 
|---|
 | 537 |   knownTypes.clear();
 | 
|---|
 | 538 |   // make knownTypes the identity mapping
 | 
|---|
 | 539 |   const periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
 | 540 |   for (periodentafel::const_iterator iter = periode->begin();
 | 
|---|
 | 541 |       iter != periode->end();
 | 
|---|
 | 542 |       ++iter) {
 | 
|---|
 | 543 |     knownTypes.insert( make_pair(iter->second->getSymbol(), iter->second->getSymbol()) );
 | 
|---|
 | 544 |   }
 | 
|---|
 | 545 | }
 | 
|---|
 | 546 | 
 | 
|---|
 | 547 | /** Parses a .potentials file and creates from it the knownTypes file.
 | 
|---|
 | 548 |  *
 | 
|---|
 | 549 |  * @param file input stream of .potentials file
 | 
|---|
 | 550 |  */
 | 
|---|
 | 551 | void TremoloParser::parseKnownTypes(std::istream &file)
 | 
|---|
 | 552 | {
 | 
|---|
 | 553 |   const periodentafel *periode = World::getInstance().getPeriode();
 | 
|---|
 | 554 |   // remove old mapping
 | 
|---|
 | 555 |   knownTypes.clear();
 | 
|---|
 | 556 | 
 | 
|---|
 | 557 |   DoLog(3) && (Log() << Verbose(3) << "additionalAtomData contains: " << additionalAtomData << std::endl);
 | 
|---|
 | 558 | 
 | 
|---|
 | 559 |   // parse in file
 | 
|---|
 | 560 |   typedef boost::tokenizer<boost::char_separator<char> >
 | 
|---|
 | 561 |       tokenizer;
 | 
|---|
 | 562 |   boost::char_separator<char> tokensep(":\t ,;");
 | 
|---|
 | 563 |   boost::char_separator<char> equalitysep("\t =");
 | 
|---|
 | 564 |   std::string line;
 | 
|---|
 | 565 |   while (file.good()) {
 | 
|---|
 | 566 |     std::getline( file, line );
 | 
|---|
 | 567 |     DoLog(4) && (Log() << Verbose(4) << "INFO: full line of parameters is '" << line << "'" << std::endl);
 | 
|---|
 | 568 |     if (line.find("particle:") != string::npos) {
 | 
|---|
 | 569 |       DoLog(3) && (Log() << Verbose(3) << "INFO: found line '" << line << "' containing keyword 'particle:'." << std::endl);
 | 
|---|
 | 570 |       tokenizer tokens(line, tokensep);
 | 
|---|
 | 571 |       ASSERT(tokens.begin() != tokens.end(),
 | 
|---|
 | 572 |           "TremoloParser::parseKnownTypes() - line with 'particle:' but no particles separated by comma.");
 | 
|---|
 | 573 |       // look for particle_type
 | 
|---|
 | 574 |       std::string particle_type("NULL");
 | 
|---|
 | 575 |       std::string element_type("NULL");
 | 
|---|
 | 576 |       for (tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
 | 577 |           tok_iter != tokens.end();
 | 
|---|
 | 578 |           ++tok_iter) {
 | 
|---|
 | 579 |         if ((*tok_iter).find("particle_type") != string::npos) {
 | 
|---|
 | 580 |           DoLog(3) && (Log() << Verbose(3) << "INFO: found line '" << line << "' containing keyword 'particle_type'." << std::endl);
 | 
|---|
 | 581 |           tokenizer token((*tok_iter), equalitysep);
 | 
|---|
 | 582 |           ASSERT(token.begin() != token.end(),
 | 
|---|
 | 583 |                     "TremoloParser::parseKnownTypes() - could not split particle_type by equality sign");
 | 
|---|
 | 584 |           tokenizer::iterator particle_iter = token.begin();
 | 
|---|
 | 585 |           particle_iter++;
 | 
|---|
 | 586 |           particle_type = *particle_iter;
 | 
|---|
 | 587 |         }
 | 
|---|
 | 588 |         if ((*tok_iter).find("element_name") != string::npos) {
 | 
|---|
 | 589 |           DoLog(3) && (Log() << Verbose(3) << "INFO: found line '" << line << "' containing keyword 'element_name'." << std::endl);
 | 
|---|
 | 590 |           tokenizer token((*tok_iter), equalitysep);
 | 
|---|
 | 591 |           ASSERT(token.begin() != token.end(),
 | 
|---|
 | 592 |                     "TremoloParser::parseKnownTypes() - could not split particle_type by equality sign");
 | 
|---|
 | 593 |           tokenizer::iterator element_iter = token.begin();
 | 
|---|
 | 594 |           element_iter++;
 | 
|---|
 | 595 |           element_type = *element_iter;
 | 
|---|
 | 596 |         }
 | 
|---|
 | 597 |       }
 | 
|---|
 | 598 |       if ((particle_type != "NULL") && (element_type != "NULL")) {
 | 
|---|
 | 599 |         if (periode->FindElement(element_type) != NULL) {
 | 
|---|
 | 600 |           DoLog(1) && (Log() << Verbose(1) << "INFO: Added Type " << particle_type << " as reference to element " << element_type << "." << std::endl);
 | 
|---|
 | 601 |           knownTypes.insert( make_pair (particle_type, element_type) );
 | 
|---|
 | 602 |         } else {
 | 
|---|
 | 603 |           DoeLog(1) && (Log() << Verbose(1) << "INFO: Either Type " << particle_type << " or " << element_type << " could not be recognized." << std::endl);
 | 
|---|
 | 604 |         }
 | 
|---|
 | 605 |       } else {
 | 
|---|
 | 606 |         DoeLog(1) && (Log() << Verbose(1) << "INFO: Desired element " << element_type << " is not known." << std::endl);
 | 
|---|
 | 607 |       }
 | 
|---|
 | 608 |     }
 | 
|---|
 | 609 |   }
 | 
|---|
 | 610 | 
 | 
|---|
 | 611 | }
 | 
|---|