| [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" | 
|---|
| [b8d4a3] | 30 | #include "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 | } | 
|---|
|  | 125 |  | 
|---|
|  | 126 | processNeighborInformation(); | 
|---|
|  | 127 | adaptImprData(); | 
|---|
|  | 128 | adaptTorsion(); | 
|---|
|  | 129 | } | 
|---|
|  | 130 |  | 
|---|
|  | 131 | /** | 
|---|
| [73916f] | 132 | * Saves the \a atoms into as a tremolo file. | 
|---|
| [b8d4a3] | 133 | * | 
|---|
|  | 134 | * \param file where to save the state | 
|---|
| [73916f] | 135 | * \param atoms atoms to store | 
|---|
| [b8d4a3] | 136 | */ | 
|---|
| [73916f] | 137 | void TremoloParser::save(ostream* file, const std::vector<atom *> &AtomList) { | 
|---|
| [e97a44] | 138 | DoLog(0) && (Log() << Verbose(0) << "Saving changes to tremolo." << std::endl); | 
|---|
|  | 139 |  | 
|---|
| [73916f] | 140 | vector<atom*>::const_iterator atomIt; | 
|---|
| [b8d4a3] | 141 | vector<string>::iterator it; | 
|---|
|  | 142 |  | 
|---|
| [acd638] | 143 | DoLog(3) && (Log() << Verbose(3) << "additionalAtomData contains: " << additionalAtomData << std::endl); | 
|---|
|  | 144 |  | 
|---|
| [b8d4a3] | 145 | *file << "# ATOMDATA"; | 
|---|
|  | 146 | for (it=usedFields.begin(); it < usedFields.end(); it++) { | 
|---|
|  | 147 | *file << "\t" << *it; | 
|---|
|  | 148 | } | 
|---|
|  | 149 | *file << endl; | 
|---|
|  | 150 | for (atomIt = AtomList.begin(); atomIt != AtomList.end(); atomIt++) { | 
|---|
|  | 151 | saveLine(file, *atomIt); | 
|---|
|  | 152 | } | 
|---|
|  | 153 | } | 
|---|
|  | 154 |  | 
|---|
|  | 155 | /** | 
|---|
|  | 156 | * Sets the keys for which data should be written to the stream when save is | 
|---|
|  | 157 | * called. | 
|---|
|  | 158 | * | 
|---|
|  | 159 | * \param string of field names with the same syntax as for an ATOMDATA line | 
|---|
|  | 160 | *        but without the prexix "ATOMDATA" | 
|---|
|  | 161 | */ | 
|---|
|  | 162 | void TremoloParser::setFieldsForSave(std::string atomDataLine) { | 
|---|
|  | 163 | parseAtomDataKeysLine(atomDataLine, 0); | 
|---|
|  | 164 | } | 
|---|
|  | 165 |  | 
|---|
|  | 166 |  | 
|---|
|  | 167 | /** | 
|---|
|  | 168 | * Writes one line of tremolo-formatted data to the provided stream. | 
|---|
|  | 169 | * | 
|---|
|  | 170 | * \param stream where to write the line to | 
|---|
|  | 171 | * \param reference to the atom of which information should be written | 
|---|
|  | 172 | */ | 
|---|
|  | 173 | void TremoloParser::saveLine(ostream* file, atom* currentAtom) { | 
|---|
|  | 174 | vector<string>::iterator it; | 
|---|
|  | 175 | TremoloKey::atomDataKey currentField; | 
|---|
|  | 176 |  | 
|---|
| [acd638] | 177 | DoLog(4) && (Log() << Verbose(4) << "INFO: Saving atom " << *currentAtom << ", its father id is " << currentAtom->GetTrueFather()->getId() << std::endl); | 
|---|
| [4d4d33] | 178 |  | 
|---|
| [b8d4a3] | 179 | for (it = usedFields.begin(); it != usedFields.end(); it++) { | 
|---|
|  | 180 | currentField = knownKeys[it->substr(0, it->find("="))]; | 
|---|
|  | 181 | switch (currentField) { | 
|---|
|  | 182 | case TremoloKey::x : | 
|---|
|  | 183 | // for the moment, assume there are always three dimensions | 
|---|
| [4d4d33] | 184 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getPosition() << std::endl); | 
|---|
| [d74077] | 185 | *file << currentAtom->at(0) << "\t"; | 
|---|
|  | 186 | *file << currentAtom->at(1) << "\t"; | 
|---|
|  | 187 | *file << currentAtom->at(2) << "\t"; | 
|---|
| [b8d4a3] | 188 | break; | 
|---|
|  | 189 | case TremoloKey::u : | 
|---|
|  | 190 | // for the moment, assume there are always three dimensions | 
|---|
| [bce72c] | 191 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getAtomicVelocity() << std::endl); | 
|---|
|  | 192 | *file << currentAtom->getAtomicVelocity()[0] << "\t"; | 
|---|
|  | 193 | *file << currentAtom->getAtomicVelocity()[1] << "\t"; | 
|---|
|  | 194 | *file << currentAtom->getAtomicVelocity()[2] << "\t"; | 
|---|
| [b8d4a3] | 195 | break; | 
|---|
|  | 196 | case TremoloKey::Type : | 
|---|
| [acd638] | 197 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
|  | 198 | if (additionalAtomData[currentAtom->getId()].get(currentField) != "-") { | 
|---|
|  | 199 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField) << std::endl); | 
|---|
|  | 200 | *file << additionalAtomData[currentAtom->getId()].get(currentField) << "\t"; | 
|---|
|  | 201 | } else { | 
|---|
|  | 202 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " default value: " << currentAtom->getType()->getSymbol() << std::endl); | 
|---|
|  | 203 | *file << currentAtom->getType()->getSymbol() << "\t"; | 
|---|
|  | 204 | } | 
|---|
|  | 205 | } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) { | 
|---|
|  | 206 | if (additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) != "-") { | 
|---|
|  | 207 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) << std::endl); | 
|---|
|  | 208 | *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) << "\t"; | 
|---|
|  | 209 | } else { | 
|---|
|  | 210 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " default value from father: " << currentAtom->GetTrueFather()->getType()->getSymbol() << std::endl); | 
|---|
|  | 211 | *file << currentAtom->GetTrueFather()->getType()->getSymbol() << "\t"; | 
|---|
|  | 212 | } | 
|---|
| [4d4d33] | 213 | } else { | 
|---|
| [acd638] | 214 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " its default value: " << currentAtom->getType()->getSymbol() << std::endl); | 
|---|
|  | 215 | *file << currentAtom->getType()->getSymbol() << "\t"; | 
|---|
| [4d4d33] | 216 | } | 
|---|
| [b8d4a3] | 217 | break; | 
|---|
|  | 218 | case TremoloKey::Id : | 
|---|
| [4d4d33] | 219 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getId()+1 << std::endl); | 
|---|
| [dc1d9e] | 220 | *file << currentAtom->getId()+1 << "\t"; | 
|---|
| [b8d4a3] | 221 | break; | 
|---|
|  | 222 | case TremoloKey::neighbors : | 
|---|
| [4d4d33] | 223 | DoLog(3) && (Log() << Verbose(3) << "Writing type " << knownKeyNames[currentField] << std::endl); | 
|---|
| [b8d4a3] | 224 | writeNeighbors(file, atoi(it->substr(it->find("=") + 1, 1).c_str()), currentAtom); | 
|---|
|  | 225 | break; | 
|---|
| [74a444] | 226 | case TremoloKey::resSeq : | 
|---|
| [4d4d33] | 227 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
|  | 228 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField) << std::endl); | 
|---|
| [74a444] | 229 | *file << additionalAtomData[currentAtom->getId()].get(currentField); | 
|---|
|  | 230 | } else if (currentAtom->getMolecule() != NULL) { | 
|---|
| [4d4d33] | 231 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " its own id: " << currentAtom->getMolecule()->getId()+1 << std::endl); | 
|---|
| [74a444] | 232 | *file << setw(4) << currentAtom->getMolecule()->getId()+1; | 
|---|
|  | 233 | } else { | 
|---|
| [4d4d33] | 234 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " default value: " << defaultAdditionalData.get(currentField) << std::endl); | 
|---|
| [74a444] | 235 | *file << defaultAdditionalData.get(currentField); | 
|---|
|  | 236 | } | 
|---|
|  | 237 | *file << "\t"; | 
|---|
| [4d4d33] | 238 | break; | 
|---|
| [b8d4a3] | 239 | default : | 
|---|
| [4d4d33] | 240 | if (additionalAtomData.count(currentAtom->getId())) { | 
|---|
|  | 241 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField) << std::endl); | 
|---|
| [74a444] | 242 | *file << additionalAtomData[currentAtom->getId()].get(currentField); | 
|---|
| [4d4d33] | 243 | } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) { | 
|---|
|  | 244 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) << std::endl); | 
|---|
| [74a444] | 245 | *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField); | 
|---|
|  | 246 | } else { | 
|---|
| [4d4d33] | 247 | DoLog(3) && (Log() << Verbose(3) << "Writing for type " << knownKeyNames[currentField] << " the default: " << defaultAdditionalData.get(currentField) << std::endl); | 
|---|
| [74a444] | 248 | *file << defaultAdditionalData.get(currentField); | 
|---|
|  | 249 | } | 
|---|
| [b8d4a3] | 250 | *file << "\t"; | 
|---|
|  | 251 | break; | 
|---|
|  | 252 | } | 
|---|
|  | 253 | } | 
|---|
|  | 254 |  | 
|---|
|  | 255 | *file << endl; | 
|---|
|  | 256 | } | 
|---|
|  | 257 |  | 
|---|
|  | 258 | /** | 
|---|
|  | 259 | * Writes the neighbor information of one atom to the provided stream. | 
|---|
|  | 260 | * | 
|---|
| [9d83b6] | 261 | * Note that ListOfBonds of WorldTime::CurrentTime is used. | 
|---|
|  | 262 | * | 
|---|
| [b8d4a3] | 263 | * \param stream where to write neighbor information to | 
|---|
|  | 264 | * \param number of neighbors | 
|---|
|  | 265 | * \param reference to the atom of which to take the neighbor information | 
|---|
|  | 266 | */ | 
|---|
|  | 267 | void TremoloParser::writeNeighbors(ostream* file, int numberOfNeighbors, atom* currentAtom) { | 
|---|
| [9d83b6] | 268 | const BondList& ListOfBonds = currentAtom->getListOfBonds(); | 
|---|
|  | 269 | BondList::const_iterator currentBond = ListOfBonds.begin(); | 
|---|
| [b8d4a3] | 270 | for (int i = 0; i < numberOfNeighbors; i++) { | 
|---|
| [9d83b6] | 271 | *file << (currentBond != ListOfBonds.end() | 
|---|
| [dc1d9e] | 272 | ? (*currentBond)->GetOtherAtom(currentAtom)->getId()+1 : 0) << "\t"; | 
|---|
| [9d83b6] | 273 | if (currentBond != ListOfBonds.end()) | 
|---|
| [0bbfa1] | 274 | ++currentBond; | 
|---|
| [b8d4a3] | 275 | } | 
|---|
| [9131f3] | 276 | } | 
|---|
|  | 277 |  | 
|---|
|  | 278 | /** | 
|---|
|  | 279 | * Stores keys from the ATOMDATA line. | 
|---|
|  | 280 | * | 
|---|
|  | 281 | * \param line to parse the keys from | 
|---|
|  | 282 | * \param with which offset the keys begin within the line | 
|---|
|  | 283 | */ | 
|---|
|  | 284 | void TremoloParser::parseAtomDataKeysLine(string line, int offset) { | 
|---|
|  | 285 | string keyword; | 
|---|
|  | 286 | stringstream lineStream; | 
|---|
|  | 287 |  | 
|---|
|  | 288 | lineStream << line.substr(offset); | 
|---|
| [52baf9] | 289 | usedFields.clear(); | 
|---|
| [9131f3] | 290 | while (lineStream.good()) { | 
|---|
|  | 291 | lineStream >> keyword; | 
|---|
| [b8d4a3] | 292 | if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey) { | 
|---|
| [ecb799] | 293 | // TODO: throw exception about unknown key | 
|---|
| [4415da] | 294 | cout << "Unknown key: " << keyword << " is not part of the tremolo format specification." << endl; | 
|---|
|  | 295 | break; | 
|---|
|  | 296 | } | 
|---|
| [9131f3] | 297 | usedFields.push_back(keyword); | 
|---|
|  | 298 | } | 
|---|
| [72d108] | 299 | //DoLog(1) && (Log() << Verbose(1) << "INFO: " << usedFields << std::endl); | 
|---|
| [9131f3] | 300 | } | 
|---|
|  | 301 |  | 
|---|
|  | 302 | /** | 
|---|
|  | 303 | * Reads one data line of a tremolo file and interprets it according to the keys | 
|---|
|  | 304 | * obtained from the ATOMDATA line. | 
|---|
|  | 305 | * | 
|---|
|  | 306 | * \param line to parse as an atom | 
|---|
| [dc1d9e] | 307 | * \param *newmol molecule to add atom to | 
|---|
| [9131f3] | 308 | */ | 
|---|
| [dc1d9e] | 309 | void TremoloParser::readAtomDataLine(string line, molecule *newmol = NULL) { | 
|---|
| [9131f3] | 310 | vector<string>::iterator it; | 
|---|
|  | 311 | stringstream lineStream; | 
|---|
| [4415da] | 312 | atom* newAtom = World::getInstance().createAtom(); | 
|---|
| [b8d4a3] | 313 | TremoloAtomInfoContainer *atomInfo = NULL; | 
|---|
| [24f128] | 314 | additionalAtomData[newAtom->getId()] = TremoloAtomInfoContainer(); // fill with default values | 
|---|
| [b8d4a3] | 315 | atomInfo = &additionalAtomData[newAtom->getId()]; | 
|---|
|  | 316 | TremoloKey::atomDataKey currentField; | 
|---|
| [72d108] | 317 | ConvertTo<double> toDouble; | 
|---|
|  | 318 | ConvertTo<int> toInt; | 
|---|
| [056e70] | 319 | Vector tempVector; | 
|---|
| [72d108] | 320 |  | 
|---|
|  | 321 | // setup tokenizer, splitting up white-spaced entries | 
|---|
|  | 322 | typedef boost::tokenizer<boost::char_separator<char> > | 
|---|
|  | 323 | tokenizer; | 
|---|
|  | 324 | boost::char_separator<char> whitespacesep(" \t"); | 
|---|
|  | 325 | tokenizer tokens(line, whitespacesep); | 
|---|
|  | 326 | ASSERT(tokens.begin() != tokens.end(), | 
|---|
|  | 327 | "TremoloParser::readAtomDataLine - empty string, need at least ' '!"); | 
|---|
|  | 328 | tokenizer::iterator tok_iter = tokens.begin(); | 
|---|
|  | 329 | // then associate each token to each file | 
|---|
| [b8d4a3] | 330 | for (it = usedFields.begin(); it < usedFields.end(); it++) { | 
|---|
| [72d108] | 331 | const std::string keyName = it->substr(0, it->find("=")); | 
|---|
|  | 332 | currentField = knownKeys[keyName]; | 
|---|
|  | 333 | const string word = *tok_iter; | 
|---|
| [056e70] | 334 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with remaining data " << word << std::endl); | 
|---|
| [4415da] | 335 | switch (currentField) { | 
|---|
| [b8d4a3] | 336 | case TremoloKey::x : | 
|---|
| [4415da] | 337 | // for the moment, assume there are always three dimensions | 
|---|
| [d74077] | 338 | for (int i=0;i<NDIM;i++) { | 
|---|
| [72d108] | 339 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for x["+toString(i)+"]!"); | 
|---|
| [056e70] | 340 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| [72d108] | 341 | newAtom->set(i, toDouble(*tok_iter)); | 
|---|
|  | 342 | tok_iter++; | 
|---|
| [d74077] | 343 | } | 
|---|
| [4415da] | 344 | break; | 
|---|
| [b8d4a3] | 345 | case TremoloKey::u : | 
|---|
| [4415da] | 346 | // for the moment, assume there are always three dimensions | 
|---|
| [72d108] | 347 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 348 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for u["+toString(i)+"]!"); | 
|---|
| [056e70] | 349 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
|  | 350 | tempVector[i] = toDouble(*tok_iter); | 
|---|
| [72d108] | 351 | tok_iter++; | 
|---|
|  | 352 | } | 
|---|
| [056e70] | 353 | newAtom->setAtomicVelocity(tempVector); | 
|---|
| [4415da] | 354 | break; | 
|---|
| [b8d4a3] | 355 | case TremoloKey::Type : | 
|---|
| [4d4d33] | 356 | { | 
|---|
| [72d108] | 357 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| [056e70] | 358 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| [4d4d33] | 359 | std::string element(knownTypes[(*tok_iter)]); | 
|---|
|  | 360 | // put type name into container for later use | 
|---|
|  | 361 | atomInfo->set(currentField, *tok_iter); | 
|---|
| [056e70] | 362 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing element " << (*tok_iter) << " as " << element << " according to KnownTypes." << std::endl); | 
|---|
| [72d108] | 363 | tok_iter++; | 
|---|
| [4d4d33] | 364 | newAtom->setType(World::getInstance().getPeriode()->FindElement(element)); | 
|---|
| [b8d4a3] | 365 | ASSERT(newAtom->getType(), "Type was not set for this atom"); | 
|---|
| [4415da] | 366 | break; | 
|---|
| [4d4d33] | 367 | } | 
|---|
| [b8d4a3] | 368 | case TremoloKey::Id : | 
|---|
| [72d108] | 369 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| [056e70] | 370 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| [72d108] | 371 | atomIdMap[toInt(*tok_iter)] = newAtom->getId(); | 
|---|
|  | 372 | tok_iter++; | 
|---|
| [4415da] | 373 | break; | 
|---|
| [b8d4a3] | 374 | case TremoloKey::neighbors : | 
|---|
| [72d108] | 375 | for (int i=0;i<atoi(it->substr(it->find("=") + 1, 1).c_str());i++) { | 
|---|
|  | 376 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| [056e70] | 377 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| [72d108] | 378 | lineStream << *tok_iter << "\t"; | 
|---|
|  | 379 | tok_iter++; | 
|---|
|  | 380 | } | 
|---|
| [b8d4a3] | 381 | readNeighbors(&lineStream, | 
|---|
|  | 382 | atoi(it->substr(it->find("=") + 1, 1).c_str()), newAtom->getId()); | 
|---|
| [9131f3] | 383 | break; | 
|---|
|  | 384 | default : | 
|---|
| [72d108] | 385 | ASSERT(tok_iter != tokens.end(), "TremoloParser::readAtomDataLine() - no value for "+keyName+"!"); | 
|---|
| [056e70] | 386 | DoLog(4) && (Log() << Verbose(4) << "INFO: Parsing key " << keyName << " with next token " << *tok_iter << std::endl); | 
|---|
| [72d108] | 387 | atomInfo->set(currentField, *tok_iter); | 
|---|
|  | 388 | tok_iter++; | 
|---|
| [9131f3] | 389 | break; | 
|---|
|  | 390 | } | 
|---|
|  | 391 | } | 
|---|
| [72d108] | 392 | if (newmol != NULL) { | 
|---|
|  | 393 | //DoLog(0) && (Log() << Verbose(0) << "New Atom: " << *newAtom << " with type " << newAtom->getType()->getName() << std::endl); | 
|---|
| [dc1d9e] | 394 | newmol->AddAtom(newAtom); | 
|---|
| [72d108] | 395 | } | 
|---|
| [6bc51d] | 396 | } | 
|---|
| [9131f3] | 397 |  | 
|---|
| [b8d4a3] | 398 | /** | 
|---|
|  | 399 | * Reads neighbor information for one atom from the input. | 
|---|
|  | 400 | * | 
|---|
| [0bbfa1] | 401 | * \param line stream where to read the information from | 
|---|
|  | 402 | * \param numberOfNeighbors number of neighbors to read | 
|---|
|  | 403 | * \param atomid world id of the atom the information belongs to | 
|---|
| [b8d4a3] | 404 | */ | 
|---|
|  | 405 | void TremoloParser::readNeighbors(stringstream* line, int numberOfNeighbors, int atomId) { | 
|---|
|  | 406 | int neighborId = 0; | 
|---|
|  | 407 | for (int i = 0; i < numberOfNeighbors; i++) { | 
|---|
|  | 408 | *line >> neighborId; | 
|---|
|  | 409 | // 0 is used to fill empty neighbor positions in the tremolo file. | 
|---|
|  | 410 | if (neighborId > 0) { | 
|---|
| [056e70] | 411 | DoLog(4) && (Log() << Verbose(4) | 
|---|
|  | 412 | << "Atom with global id " << atomId | 
|---|
|  | 413 | << " has neighbour with serial " << neighborId | 
|---|
|  | 414 | << std::endl); | 
|---|
| [b8d4a3] | 415 | additionalAtomData[atomId].neighbors.push_back(neighborId); | 
|---|
|  | 416 | } | 
|---|
|  | 417 | } | 
|---|
|  | 418 | } | 
|---|
| [9131f3] | 419 |  | 
|---|
|  | 420 | /** | 
|---|
| [b8d4a3] | 421 | * Checks whether the provided name is within the list of used fields. | 
|---|
| [9131f3] | 422 | * | 
|---|
| [b8d4a3] | 423 | * \param field name to check | 
|---|
|  | 424 | * | 
|---|
|  | 425 | * \return true if the field name is used | 
|---|
| [9131f3] | 426 | */ | 
|---|
| [b8d4a3] | 427 | bool TremoloParser::isUsedField(string fieldName) { | 
|---|
|  | 428 | bool fieldNameExists = false; | 
|---|
|  | 429 | for (vector<string>::iterator usedField = usedFields.begin(); usedField != usedFields.end(); usedField++) { | 
|---|
|  | 430 | if (usedField->substr(0, usedField->find("=")) == fieldName) | 
|---|
|  | 431 | fieldNameExists = true; | 
|---|
|  | 432 | } | 
|---|
| [9131f3] | 433 |  | 
|---|
| [b8d4a3] | 434 | return fieldNameExists; | 
|---|
|  | 435 | } | 
|---|
|  | 436 |  | 
|---|
|  | 437 |  | 
|---|
|  | 438 | /** | 
|---|
|  | 439 | * Adds the collected neighbor information to the atoms in the world. The atoms | 
|---|
|  | 440 | * are found by their current ID and mapped to the corresponding atoms with the | 
|---|
|  | 441 | * Id found in the parsed file. | 
|---|
|  | 442 | */ | 
|---|
|  | 443 | void TremoloParser::processNeighborInformation() { | 
|---|
|  | 444 | if (!isUsedField("neighbors")) { | 
|---|
|  | 445 | return; | 
|---|
|  | 446 | } | 
|---|
|  | 447 |  | 
|---|
|  | 448 | for(map<int, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
|  | 449 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
|  | 450 | ) { | 
|---|
| [0bbfa1] | 451 | if (!currentInfo->second.neighbors_processed) { | 
|---|
|  | 452 | for(vector<int>::iterator neighbor = currentInfo->second.neighbors.begin(); | 
|---|
|  | 453 | neighbor != currentInfo->second.neighbors.end(); neighbor++ | 
|---|
|  | 454 | ) { | 
|---|
| [72d108] | 455 | //        DoLog(1) && (Log() << Verbose(1) << "Creating bond between (" | 
|---|
| [0bbfa1] | 456 | //            << currentInfo->first | 
|---|
|  | 457 | //            << ") and (" | 
|---|
| [72d108] | 458 | //            << atomIdMap[*neighbor] << "|" << *neighbor << ")" << std::endl); | 
|---|
| [0bbfa1] | 459 | World::getInstance().getAtom(AtomById(currentInfo->first)) | 
|---|
|  | 460 | ->addBond(World::getInstance().getAtom(AtomById(atomIdMap[*neighbor]))); | 
|---|
|  | 461 | } | 
|---|
|  | 462 | currentInfo->second.neighbors_processed = true; | 
|---|
| [9131f3] | 463 | } | 
|---|
|  | 464 | } | 
|---|
| [6bc51d] | 465 | } | 
|---|
|  | 466 |  | 
|---|
| [9131f3] | 467 | /** | 
|---|
| [b8d4a3] | 468 | * Replaces atom IDs read from the file by the corresponding world IDs. All IDs | 
|---|
|  | 469 | * IDs of the input string will be replaced; expected separating characters are | 
|---|
|  | 470 | * "-" and ",". | 
|---|
| [9131f3] | 471 | * | 
|---|
| [b8d4a3] | 472 | * \param string in which atom IDs should be adapted | 
|---|
|  | 473 | * | 
|---|
|  | 474 | * \return input string with modified atom IDs | 
|---|
| [9131f3] | 475 | */ | 
|---|
| [b8d4a3] | 476 | string TremoloParser::adaptIdDependentDataString(string data) { | 
|---|
|  | 477 | // there might be no IDs | 
|---|
|  | 478 | if (data == "-") { | 
|---|
|  | 479 | return "-"; | 
|---|
|  | 480 | } | 
|---|
|  | 481 |  | 
|---|
|  | 482 | char separator; | 
|---|
|  | 483 | int id; | 
|---|
|  | 484 | stringstream line, result; | 
|---|
|  | 485 | line << data; | 
|---|
|  | 486 |  | 
|---|
|  | 487 | line >> id; | 
|---|
|  | 488 | result << atomIdMap[id]; | 
|---|
|  | 489 | while (line.good()) { | 
|---|
|  | 490 | line >> separator >> id; | 
|---|
|  | 491 | result << separator << atomIdMap[id]; | 
|---|
|  | 492 | } | 
|---|
|  | 493 |  | 
|---|
|  | 494 | return result.str(); | 
|---|
| [6bc51d] | 495 | } | 
|---|
| [b8d4a3] | 496 |  | 
|---|
|  | 497 | /** | 
|---|
|  | 498 | * Corrects the atom IDs in each imprData entry to the corresponding world IDs | 
|---|
|  | 499 | * as they might differ from the originally read IDs. | 
|---|
|  | 500 | */ | 
|---|
|  | 501 | void TremoloParser::adaptImprData() { | 
|---|
|  | 502 | if (!isUsedField("imprData")) { | 
|---|
|  | 503 | return; | 
|---|
|  | 504 | } | 
|---|
|  | 505 |  | 
|---|
|  | 506 | for(map<int, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
|  | 507 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
|  | 508 | ) { | 
|---|
|  | 509 | currentInfo->second.imprData = adaptIdDependentDataString(currentInfo->second.imprData); | 
|---|
|  | 510 | } | 
|---|
| [6bc51d] | 511 | } | 
|---|
| [4415da] | 512 |  | 
|---|
| [b8d4a3] | 513 | /** | 
|---|
|  | 514 | * Corrects the atom IDs in each torsion entry to the corresponding world IDs | 
|---|
|  | 515 | * as they might differ from the originally read IDs. | 
|---|
|  | 516 | */ | 
|---|
|  | 517 | void TremoloParser::adaptTorsion() { | 
|---|
|  | 518 | if (!isUsedField("torsion")) { | 
|---|
|  | 519 | return; | 
|---|
|  | 520 | } | 
|---|
|  | 521 |  | 
|---|
|  | 522 | for(map<int, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin(); | 
|---|
|  | 523 | currentInfo != additionalAtomData.end(); currentInfo++ | 
|---|
|  | 524 | ) { | 
|---|
|  | 525 | currentInfo->second.torsion = adaptIdDependentDataString(currentInfo->second.torsion); | 
|---|
|  | 526 | } | 
|---|
|  | 527 | } | 
|---|
|  | 528 |  | 
|---|
| [4d4d33] | 529 | /** Creates knownTypes as the identity, e.g. H -> H, He -> He, ... . | 
|---|
|  | 530 | * | 
|---|
|  | 531 | */ | 
|---|
|  | 532 | void TremoloParser::createKnownTypesByIdentity() | 
|---|
|  | 533 | { | 
|---|
|  | 534 | // remove old mapping | 
|---|
|  | 535 | knownTypes.clear(); | 
|---|
|  | 536 | // make knownTypes the identity mapping | 
|---|
|  | 537 | const periodentafel *periode = World::getInstance().getPeriode(); | 
|---|
|  | 538 | for (periodentafel::const_iterator iter = periode->begin(); | 
|---|
|  | 539 | iter != periode->end(); | 
|---|
|  | 540 | ++iter) { | 
|---|
|  | 541 | knownTypes.insert( make_pair(iter->second->getSymbol(), iter->second->getSymbol()) ); | 
|---|
|  | 542 | } | 
|---|
|  | 543 | } | 
|---|
|  | 544 |  | 
|---|
|  | 545 | /** Parses a .potentials file and creates from it the knownTypes file. | 
|---|
|  | 546 | * | 
|---|
|  | 547 | * @param file input stream of .potentials file | 
|---|
|  | 548 | */ | 
|---|
|  | 549 | void TremoloParser::parseKnownTypes(std::istream &file) | 
|---|
|  | 550 | { | 
|---|
|  | 551 | const periodentafel *periode = World::getInstance().getPeriode(); | 
|---|
|  | 552 | // remove old mapping | 
|---|
|  | 553 | knownTypes.clear(); | 
|---|
|  | 554 |  | 
|---|
|  | 555 | DoLog(3) && (Log() << Verbose(3) << "additionalAtomData contains: " << additionalAtomData << std::endl); | 
|---|
|  | 556 |  | 
|---|
|  | 557 | // parse in file | 
|---|
|  | 558 | typedef boost::tokenizer<boost::char_separator<char> > | 
|---|
|  | 559 | tokenizer; | 
|---|
|  | 560 | boost::char_separator<char> tokensep(":\t ,;"); | 
|---|
|  | 561 | boost::char_separator<char> equalitysep("\t ="); | 
|---|
|  | 562 | std::string line; | 
|---|
|  | 563 | while (file.good()) { | 
|---|
|  | 564 | std::getline( file, line ); | 
|---|
|  | 565 | DoLog(4) && (Log() << Verbose(4) << "INFO: full line of parameters is '" << line << "'" << std::endl); | 
|---|
|  | 566 | if (line.find("particle:") != string::npos) { | 
|---|
|  | 567 | DoLog(3) && (Log() << Verbose(3) << "INFO: found line '" << line << "' containing keyword 'particle:'." << std::endl); | 
|---|
|  | 568 | tokenizer tokens(line, tokensep); | 
|---|
|  | 569 | ASSERT(tokens.begin() != tokens.end(), | 
|---|
|  | 570 | "TremoloParser::parseKnownTypes() - line with 'particle:' but no particles separated by comma."); | 
|---|
|  | 571 | // look for particle_type | 
|---|
|  | 572 | std::string particle_type("NULL"); | 
|---|
|  | 573 | std::string element_type("NULL"); | 
|---|
|  | 574 | for (tokenizer::iterator tok_iter = tokens.begin(); | 
|---|
|  | 575 | tok_iter != tokens.end(); | 
|---|
|  | 576 | ++tok_iter) { | 
|---|
|  | 577 | if ((*tok_iter).find("particle_type") != string::npos) { | 
|---|
|  | 578 | DoLog(3) && (Log() << Verbose(3) << "INFO: found line '" << line << "' containing keyword 'particle_type'." << std::endl); | 
|---|
|  | 579 | tokenizer token((*tok_iter), equalitysep); | 
|---|
|  | 580 | ASSERT(token.begin() != token.end(), | 
|---|
|  | 581 | "TremoloParser::parseKnownTypes() - could not split particle_type by equality sign"); | 
|---|
|  | 582 | tokenizer::iterator particle_iter = token.begin(); | 
|---|
|  | 583 | particle_iter++; | 
|---|
|  | 584 | particle_type = *particle_iter; | 
|---|
|  | 585 | } | 
|---|
|  | 586 | if ((*tok_iter).find("element_name") != string::npos) { | 
|---|
|  | 587 | DoLog(3) && (Log() << Verbose(3) << "INFO: found line '" << line << "' containing keyword 'element_name'." << std::endl); | 
|---|
|  | 588 | tokenizer token((*tok_iter), equalitysep); | 
|---|
|  | 589 | ASSERT(token.begin() != token.end(), | 
|---|
|  | 590 | "TremoloParser::parseKnownTypes() - could not split particle_type by equality sign"); | 
|---|
|  | 591 | tokenizer::iterator element_iter = token.begin(); | 
|---|
|  | 592 | element_iter++; | 
|---|
|  | 593 | element_type = *element_iter; | 
|---|
|  | 594 | } | 
|---|
|  | 595 | } | 
|---|
|  | 596 | if ((particle_type != "NULL") && (element_type != "NULL")) { | 
|---|
|  | 597 | if (periode->FindElement(element_type) != NULL) { | 
|---|
|  | 598 | DoLog(1) && (Log() << Verbose(1) << "INFO: Added Type " << particle_type << " as reference to element " << element_type << "." << std::endl); | 
|---|
|  | 599 | knownTypes.insert( make_pair (particle_type, element_type) ); | 
|---|
|  | 600 | } else { | 
|---|
|  | 601 | DoeLog(1) && (Log() << Verbose(1) << "INFO: Either Type " << particle_type << " or " << element_type << " could not be recognized." << std::endl); | 
|---|
|  | 602 | } | 
|---|
|  | 603 | } else { | 
|---|
|  | 604 | DoeLog(1) && (Log() << Verbose(1) << "INFO: Desired element " << element_type << " is not known." << std::endl); | 
|---|
|  | 605 | } | 
|---|
|  | 606 | } | 
|---|
|  | 607 | } | 
|---|
|  | 608 |  | 
|---|
|  | 609 | } | 
|---|