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