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