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