| [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"
 | 
|---|
| [ccb487] | 31 | #include "Box.hpp"
 | 
|---|
| [42127c] | 32 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [3bdb6d] | 33 | #include "Element/element.hpp"
 | 
|---|
 | 34 | #include "Element/periodentafel.hpp"
 | 
|---|
| [ccb487] | 35 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| [42127c] | 36 | #include "molecule.hpp"
 | 
|---|
 | 37 | #include "MoleculeListClass.hpp"
 | 
|---|
 | 38 | #include "World.hpp"
 | 
|---|
 | 39 | #include "WorldTime.hpp"
 | 
|---|
 | 40 | 
 | 
|---|
| [9131f3] | 41 | 
 | 
|---|
| [9f8b01] | 42 | #include <algorithm>
 | 
|---|
| [ca331c] | 43 | #include <boost/lambda/lambda.hpp>
 | 
|---|
| [2034f3] | 44 | #include <boost/lexical_cast.hpp>
 | 
|---|
| [72d108] | 45 | #include <boost/tokenizer.hpp>
 | 
|---|
| [74a444] | 46 | #include <iostream>
 | 
|---|
 | 47 | #include <iomanip>
 | 
|---|
| [8bf9c6] | 48 | #include <map>
 | 
|---|
 | 49 | #include <sstream>
 | 
|---|
| [ca331c] | 50 | #include <string>
 | 
|---|
| [8bf9c6] | 51 | #include <vector>
 | 
|---|
| [9131f3] | 52 | 
 | 
|---|
| [5a667d] | 53 | #include <boost/assign/list_of.hpp> // for 'map_list_of()'
 | 
|---|
 | 54 | #include <boost/assert.hpp>
 | 
|---|
 | 55 | 
 | 
|---|
| [765f16] | 56 | // declare specialized static variables
 | 
|---|
 | 57 | const std::string FormatParserTrait<tremolo>::name = "tremolo";
 | 
|---|
 | 58 | const std::string FormatParserTrait<tremolo>::suffix = "data";
 | 
|---|
 | 59 | const ParserTypes FormatParserTrait<tremolo>::type = tremolo;
 | 
|---|
 | 60 | 
 | 
|---|
| [5a667d] | 61 | // static instances
 | 
|---|
 | 62 | std::map<std::string, TremoloKey::atomDataKey> FormatParser<tremolo>::knownKeys =
 | 
|---|
 | 63 |     boost::assign::map_list_of("x",TremoloKey::x)
 | 
|---|
 | 64 |     ("u",TremoloKey::u)
 | 
|---|
 | 65 |     ("F",TremoloKey::F)
 | 
|---|
 | 66 |     ("stress",TremoloKey::stress)
 | 
|---|
 | 67 |     ("Id",TremoloKey::Id)
 | 
|---|
 | 68 |     ("neighbors",TremoloKey::neighbors)
 | 
|---|
 | 69 |     ("imprData",TremoloKey::imprData)
 | 
|---|
 | 70 |     ("GroupMeasureTypeNo",TremoloKey::GroupMeasureTypeNo)
 | 
|---|
 | 71 |     ("type",TremoloKey::type)
 | 
|---|
 | 72 |     ("extType",TremoloKey::extType)
 | 
|---|
 | 73 |     ("name",TremoloKey::name)
 | 
|---|
 | 74 |     ("resName",TremoloKey::resName)
 | 
|---|
 | 75 |     ("chainID",TremoloKey::chainID)
 | 
|---|
 | 76 |     ("resSeq",TremoloKey::resSeq)
 | 
|---|
 | 77 |     ("occupancy",TremoloKey::occupancy)
 | 
|---|
 | 78 |     ("tempFactor",TremoloKey::tempFactor)
 | 
|---|
 | 79 |     ("segID",TremoloKey::segID)
 | 
|---|
 | 80 |     ("Charge",TremoloKey::Charge)
 | 
|---|
 | 81 |     ("charge",TremoloKey::charge)
 | 
|---|
 | 82 |     ("GrpTypeNo",TremoloKey::GrpTypeNo)
 | 
|---|
 | 83 |     ("torsion",TremoloKey::torsion)
 | 
|---|
 | 84 |     (" ",TremoloKey::noKey); // with this we can detect invalid keys
 | 
|---|
 | 85 | 
 | 
|---|
| [9131f3] | 86 | /**
 | 
|---|
 | 87 |  * Constructor.
 | 
|---|
 | 88 |  */
 | 
|---|
| [765f16] | 89 | FormatParser< tremolo >::FormatParser()  :
 | 
|---|
 | 90 |   FormatParser_common(NULL)
 | 
|---|
 | 91 | {
 | 
|---|
| [4d4d33] | 92 |   createKnownTypesByIdentity();
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 |   // invert knownKeys for debug output
 | 
|---|
 | 95 |   for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin(); iter != knownKeys.end(); ++iter)
 | 
|---|
 | 96 |     knownKeyNames.insert( make_pair( iter->second, iter->first) );
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |   additionalAtomData.clear();
 | 
|---|
| [9131f3] | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
| [5a667d] | 101 | 
 | 
|---|
| [9131f3] | 102 | /**
 | 
|---|
 | 103 |  * Destructor.
 | 
|---|
 | 104 |  */
 | 
|---|
| [765f16] | 105 | FormatParser< tremolo >::~FormatParser() 
 | 
|---|
 | 106 | {
 | 
|---|
| [23fd43] | 107 |   usedFields_save.clear();
 | 
|---|
| [b8d4a3] | 108 |   additionalAtomData.clear();
 | 
|---|
 | 109 | }
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 | /**
 | 
|---|
 | 112 |  * Loads atoms from a tremolo-formatted file.
 | 
|---|
 | 113 |  *
 | 
|---|
 | 114 |  * \param tremolo file
 | 
|---|
 | 115 |  */
 | 
|---|
| [765f16] | 116 | void FormatParser< tremolo >::load(istream* file) {
 | 
|---|
| [8bf9c6] | 117 |   std::string line;
 | 
|---|
 | 118 |   std::string::size_type location;
 | 
|---|
| [b8d4a3] | 119 | 
 | 
|---|
| [c0e28c] | 120 |   // reset the id maps
 | 
|---|
 | 121 |   resetIdAssociations();
 | 
|---|
 | 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');
 | 
|---|
| [23fd43] | 129 |     // we only parse in the first ATOMDATA line
 | 
|---|
 | 130 |     if (usedFields_load.empty()) {
 | 
|---|
| [b8d4a3] | 131 |       location = line.find("ATOMDATA", 0);
 | 
|---|
 | 132 |       if (location != string::npos) {
 | 
|---|
| [23fd43] | 133 |        parseAtomDataKeysLine(line, location + 8, usedFields_load);
 | 
|---|
| [b8d4a3] | 134 |       }
 | 
|---|
 | 135 |     }
 | 
|---|
 | 136 |     if (line.length() > 0 && line.at(0) != '#') {
 | 
|---|
| [dc1d9e] | 137 |       readAtomDataLine(line, newmol);
 | 
|---|
| [b8d4a3] | 138 |     }
 | 
|---|
 | 139 |   }
 | 
|---|
| [23fd43] | 140 |   LOG(3, "DEBUG: Local usedFields is: " << usedFields_load);
 | 
|---|
| [2e352f] | 141 | 
 | 
|---|
| [9f8b01] | 142 |   // refresh atom::nr and atom::name
 | 
|---|
 | 143 |   std::vector<atomId_t> atoms(newmol->getAtomCount());
 | 
|---|
 | 144 |   std::transform(newmol->begin(), newmol->end(), atoms.begin(),
 | 
|---|
 | 145 |       boost::bind(&atom::getId, _1));
 | 
|---|
 | 146 |   processNeighborInformation(atoms);
 | 
|---|
| [b8d4a3] | 147 |   adaptImprData();
 | 
|---|
 | 148 |   adaptTorsion();
 | 
|---|
| [23fd43] | 149 | 
 | 
|---|
 | 150 |   // append usedFields to global usedFields, is made unique on save, clear after use
 | 
|---|
 | 151 |   usedFields_save.insert(usedFields_save.end(), usedFields_load.begin(), usedFields_load.end());
 | 
|---|
 | 152 |   usedFields_load.clear();
 | 
|---|
| [b8d4a3] | 153 | }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 | /**
 | 
|---|
| [73916f] | 156 |  * Saves the \a atoms into as a tremolo file.
 | 
|---|
| [b8d4a3] | 157 |  *
 | 
|---|
 | 158 |  * \param file where to save the state
 | 
|---|
| [73916f] | 159 |  * \param atoms atoms to store
 | 
|---|
| [b8d4a3] | 160 |  */
 | 
|---|
| [23fd43] | 161 | void FormatParser< tremolo >::save(std::ostream* file, const std::vector<atom *> &AtomList) {
 | 
|---|
| [47d041] | 162 |   LOG(0, "Saving changes to tremolo.");
 | 
|---|
| [e97a44] | 163 | 
 | 
|---|
| [23fd43] | 164 |   // install default usedFields if empty so far
 | 
|---|
 | 165 |   if (usedFields_save.empty()) {
 | 
|---|
 | 166 |     // default behavior: use all possible keys on output
 | 
|---|
 | 167 |     for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin();
 | 
|---|
 | 168 |         iter != knownKeys.end(); ++iter)
 | 
|---|
 | 169 |       if (iter->second != TremoloKey::noKey) // don't add noKey
 | 
|---|
 | 170 |         usedFields_save.push_back(iter->first);
 | 
|---|
 | 171 |   }
 | 
|---|
 | 172 |   // make present usedFields_save unique
 | 
|---|
 | 173 |   makeUsedFieldsUnique(usedFields_save);
 | 
|---|
 | 174 |   LOG(1, "DEBUG: Global (with unique entries) usedFields_save is: " << usedFields_save);
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 |   // distribute ids continuously
 | 
|---|
 | 177 |   distributeContinuousIds(AtomList);
 | 
|---|
 | 178 | 
 | 
|---|
 | 179 |   // store atomdata
 | 
|---|
 | 180 |   save_AtomDataLine(file);
 | 
|---|
 | 181 | 
 | 
|---|
 | 182 |   // store box
 | 
|---|
 | 183 |   save_BoxLine(file);
 | 
|---|
| [b8d4a3] | 184 | 
 | 
|---|
| [23fd43] | 185 |   // store particles
 | 
|---|
 | 186 |   for (std::vector<atom*>::const_iterator  atomIt = AtomList.begin();
 | 
|---|
 | 187 |       atomIt != AtomList.end(); ++atomIt)
 | 
|---|
 | 188 |     saveLine(file, *atomIt);
 | 
|---|
 | 189 | }
 | 
|---|
| [acd638] | 190 | 
 | 
|---|
| [5b0581] | 191 | struct usedFieldsWeakComparator
 | 
|---|
 | 192 | {
 | 
|---|
 | 193 |   /** Special comparator regards "neighbors=4" and "neighbors=2" as equal
 | 
|---|
 | 194 |    *
 | 
|---|
 | 195 |    * \note This one is used for making usedFields unique, i.e. throwing out the "smaller"
 | 
|---|
 | 196 |    * neighbors.
 | 
|---|
 | 197 |    */
 | 
|---|
 | 198 |   bool operator()(const std::string &a, const std::string &b) const
 | 
|---|
 | 199 |   {
 | 
|---|
 | 200 |     // only compare up to first equality sign
 | 
|---|
 | 201 |     return (a.substr(0, a.find_first_of('=')) == b.substr(0, b.find_first_of('=')));
 | 
|---|
 | 202 |   }
 | 
|---|
 | 203 | };
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 | struct usedFieldsSpecialOrderer
 | 
|---|
 | 206 | {
 | 
|---|
 | 207 |   /** Special string comparator that regards "neighbors=4" < "neighbors=2" as true and
 | 
|---|
 | 208 |    * the other way round as false.
 | 
|---|
 | 209 |    *
 | 
|---|
 | 210 |    * Here, we implement the operator "\a < \b" in a special way to allow the
 | 
|---|
 | 211 |    * above.
 | 
|---|
 | 212 |    *
 | 
|---|
 | 213 |    * \note This one is used for sorting usedFields in preparation for making it unique.
 | 
|---|
 | 214 |    */
 | 
|---|
 | 215 |   bool operator()(const std::string &a, const std::string &b) const
 | 
|---|
 | 216 |   {
 | 
|---|
 | 217 |     // only compare up to first equality sign
 | 
|---|
 | 218 |     size_t a_equality = a.find_first_of('=');
 | 
|---|
 | 219 |     size_t b_equality = b.find_first_of('=');
 | 
|---|
 | 220 |     // if key before equality is not equal, return whether it is smaller or not
 | 
|---|
 | 221 |     if (a.substr(0, a_equality) != b.substr(0, b_equality)) {
 | 
|---|
 | 222 |       return a.substr(0, a_equality) < b.substr(0, b_equality);
 | 
|---|
 | 223 |     } else { // now we know that the key before equality is the same in either string
 | 
|---|
 | 224 |       // if one of them has no equality, the one with equality must go before
 | 
|---|
 | 225 |       if ((a_equality != std::string::npos) && (b_equality == std::string::npos))
 | 
|---|
 | 226 |         return true;
 | 
|---|
 | 227 |       if ((a_equality == std::string::npos) && (b_equality != std::string::npos))
 | 
|---|
 | 228 |         return false;
 | 
|---|
 | 229 |       // if both don't have equality (and the token before is equal), it is not "<" but "=="
 | 
|---|
 | 230 |       if ((a_equality == std::string::npos) && (b_equality == std::string::npos))
 | 
|---|
 | 231 |         return false;
 | 
|---|
 | 232 |       // if now both have equality sign, the larger value after it, must come first
 | 
|---|
 | 233 |       return a.substr(a_equality, std::string::npos) > b.substr(b_equality, std::string::npos);
 | 
|---|
 | 234 |     }
 | 
|---|
 | 235 |   }
 | 
|---|
 | 236 | };
 | 
|---|
 | 237 | 
 | 
|---|
| [23fd43] | 238 | /** Helper function to make \given fields unique while preserving the order of first appearance.
 | 
|---|
 | 239 |  *
 | 
|---|
 | 240 |  * As std::unique only removes element if equal to predecessor, a vector is only
 | 
|---|
 | 241 |  * made unique if sorted beforehand. But sorting would destroy order of first
 | 
|---|
 | 242 |  * appearance, hence we do the sorting on a temporary field and add the unique
 | 
|---|
 | 243 |  * elements in the order as in \a fields.
 | 
|---|
 | 244 |  *
 | 
|---|
 | 245 |  * @param fields usedFields to make unique while preserving order of appearance
 | 
|---|
 | 246 |  */
 | 
|---|
| [27cfde] | 247 | void FormatParser< tremolo >::makeUsedFieldsUnique(usedFields_t &fields) const
 | 
|---|
| [23fd43] | 248 | {
 | 
|---|
 | 249 |   // std::unique only removes if predecessor is equal, not over whole range, hence do it manually
 | 
|---|
| [27cfde] | 250 |   usedFields_t temp_fields(fields);
 | 
|---|
| [5b0581] | 251 |   usedFieldsSpecialOrderer SpecialOrderer;
 | 
|---|
 | 252 |   usedFieldsWeakComparator WeakComparator;
 | 
|---|
 | 253 |   std::sort(temp_fields.begin(), temp_fields.end(), SpecialOrderer);
 | 
|---|
| [23fd43] | 254 |   usedFields_t::iterator it =
 | 
|---|
| [5b0581] | 255 |       std::unique(temp_fields.begin(), temp_fields.end(), WeakComparator);
 | 
|---|
| [23fd43] | 256 |   temp_fields.erase(it, temp_fields.end());
 | 
|---|
| [27cfde] | 257 |   usedFields_t usedfields(fields);
 | 
|---|
 | 258 |   fields.clear();
 | 
|---|
 | 259 |   fields.reserve(temp_fields.size());
 | 
|---|
| [23fd43] | 260 |   // now go through each usedFields entry, check if in temp_fields and remove there on first occurence
 | 
|---|
 | 261 |   for (usedFields_t::const_iterator iter = usedfields.begin();
 | 
|---|
 | 262 |       iter != usedfields.end(); ++iter) {
 | 
|---|
 | 263 |     usedFields_t::iterator uniqueiter =
 | 
|---|
 | 264 |         std::find(temp_fields.begin(), temp_fields.end(), *iter);
 | 
|---|
 | 265 |     if (uniqueiter != temp_fields.end()) {
 | 
|---|
| [27cfde] | 266 |       fields.push_back(*iter);
 | 
|---|
| [23fd43] | 267 |       // add only once to ATOMDATA
 | 
|---|
 | 268 |       temp_fields.erase(uniqueiter);
 | 
|---|
 | 269 |     }
 | 
|---|
 | 270 |   }
 | 
|---|
 | 271 |   ASSERT( temp_fields.empty(),
 | 
|---|
 | 272 |       "FormatParser< tremolo >::save() - still unique entries left in temp_fields after unique?");
 | 
|---|
 | 273 | }
 | 
|---|
 | 274 | 
 | 
|---|
 | 275 | 
 | 
|---|
 | 276 | /** Resets and distributes the indices continuously.
 | 
|---|
 | 277 |  *
 | 
|---|
 | 278 |  * \param atoms atoms to store
 | 
|---|
 | 279 |  */
 | 
|---|
 | 280 | void FormatParser< tremolo >::distributeContinuousIds(const std::vector<atom *> &AtomList)
 | 
|---|
 | 281 | {
 | 
|---|
| [812155] | 282 |   resetIdAssociations();
 | 
|---|
 | 283 |   atomId_t lastid = 0;
 | 
|---|
| [23fd43] | 284 |   for (std::vector<atom*>::const_iterator atomIt = AtomList.begin();
 | 
|---|
 | 285 |       atomIt != AtomList.end(); ++atomIt)
 | 
|---|
| [812155] | 286 |     associateLocaltoGlobalId(++lastid, (*atomIt)->getId());
 | 
|---|
| [23fd43] | 287 | }
 | 
|---|
| [812155] | 288 | 
 | 
|---|
| [23fd43] | 289 | /** Store Atomdata line to \a file.
 | 
|---|
 | 290 |  *
 | 
|---|
 | 291 |  * @param file output stream
 | 
|---|
 | 292 |  */
 | 
|---|
 | 293 | void FormatParser< tremolo >::save_AtomDataLine(std::ostream* file) const
 | 
|---|
 | 294 | {
 | 
|---|
| [b8d4a3] | 295 |   *file << "# ATOMDATA";
 | 
|---|
| [23fd43] | 296 |   for (usedFields_t::const_iterator it=usedFields_save.begin();
 | 
|---|
 | 297 |       it != usedFields_save.end(); ++it)
 | 
|---|
| [b8d4a3] | 298 |     *file << "\t" << *it;
 | 
|---|
| [23fd43] | 299 |   *file << std::endl;
 | 
|---|
 | 300 | }
 | 
|---|
| [ccb487] | 301 | 
 | 
|---|
| [23fd43] | 302 | /** Store Box info to \a file
 | 
|---|
 | 303 |  *
 | 
|---|
 | 304 |  * @param file output stream
 | 
|---|
 | 305 |  */
 | 
|---|
 | 306 | void FormatParser< tremolo >::save_BoxLine(std::ostream* file) const
 | 
|---|
 | 307 | {
 | 
|---|
| [ccb487] | 308 |   *file << "# Box";
 | 
|---|
 | 309 |   const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
 | 
|---|
 | 310 |   for (size_t i=0; i<NDIM;++i)
 | 
|---|
 | 311 |     for (size_t j=0; j<NDIM;++j)
 | 
|---|
 | 312 |       *file << "\t" << M.at(i,j);
 | 
|---|
 | 313 |   *file << std::endl;
 | 
|---|
| [b8d4a3] | 314 | }
 | 
|---|
 | 315 | 
 | 
|---|
| [6bc86c] | 316 | /** Add default info, when new atom is added to World.
 | 
|---|
 | 317 |  *
 | 
|---|
 | 318 |  * @param id of atom
 | 
|---|
 | 319 |  */
 | 
|---|
| [765f16] | 320 | void FormatParser< tremolo >::AtomInserted(atomId_t id)
 | 
|---|
| [6bc86c] | 321 | {
 | 
|---|
| [8bf9c6] | 322 |   std::map<const atomId_t, TremoloAtomInfoContainer>::iterator iter = additionalAtomData.find(id);
 | 
|---|
| [6bc86c] | 323 |   ASSERT(iter == additionalAtomData.end(),
 | 
|---|
| [765f16] | 324 |       "FormatParser< tremolo >::AtomInserted() - additionalAtomData already present for newly added atom "
 | 
|---|
| [6bc86c] | 325 |       +toString(id)+".");
 | 
|---|
 | 326 |   // don't add entry, as this gives a default resSeq of 0 not the molecule id
 | 
|---|
 | 327 |   // additionalAtomData.insert( std::make_pair(id, TremoloAtomInfoContainer()) );
 | 
|---|
 | 328 | }
 | 
|---|
 | 329 | 
 | 
|---|
 | 330 | /** Remove additional AtomData info, when atom has been removed from World.
 | 
|---|
 | 331 |  *
 | 
|---|
 | 332 |  * @param id of atom
 | 
|---|
 | 333 |  */
 | 
|---|
| [765f16] | 334 | void FormatParser< tremolo >::AtomRemoved(atomId_t id)
 | 
|---|
| [6bc86c] | 335 | {
 | 
|---|
| [8bf9c6] | 336 |   std::map<const atomId_t, TremoloAtomInfoContainer>::iterator iter = additionalAtomData.find(id);
 | 
|---|
| [6bc86c] | 337 |   // as we do not insert AtomData on AtomInserted, we cannot be assured of its presence
 | 
|---|
 | 338 | //  ASSERT(iter != additionalAtomData.end(),
 | 
|---|
| [765f16] | 339 | //      "FormatParser< tremolo >::AtomRemoved() - additionalAtomData is not present for atom "
 | 
|---|
| [6bc86c] | 340 | //      +toString(id)+" to remove.");
 | 
|---|
 | 341 |   if (iter != additionalAtomData.end())
 | 
|---|
 | 342 |     additionalAtomData.erase(iter);
 | 
|---|
 | 343 | }
 | 
|---|
 | 344 | 
 | 
|---|
| [b8d4a3] | 345 | /**
 | 
|---|
 | 346 |  * Writes one line of tremolo-formatted data to the provided stream.
 | 
|---|
 | 347 |  *
 | 
|---|
 | 348 |  * \param stream where to write the line to
 | 
|---|
 | 349 |  * \param reference to the atom of which information should be written
 | 
|---|
 | 350 |  */
 | 
|---|
| [23fd43] | 351 | void FormatParser< tremolo >::saveLine(std::ostream* file, const atom* currentAtom)
 | 
|---|
 | 352 | {
 | 
|---|
| [b8d4a3] | 353 |   TremoloKey::atomDataKey currentField;
 | 
|---|
 | 354 | 
 | 
|---|
| [47d041] | 355 |   LOG(4, "INFO: Saving atom " << *currentAtom << ", its father id is " << currentAtom->GetTrueFather()->getId());
 | 
|---|
| [4d4d33] | 356 | 
 | 
|---|
| [23fd43] | 357 |   for (usedFields_t::iterator it = usedFields_save.begin(); it != usedFields_save.end(); it++) {
 | 
|---|
| [b8d4a3] | 358 |     currentField = knownKeys[it->substr(0, it->find("="))];
 | 
|---|
 | 359 |     switch (currentField) {
 | 
|---|
 | 360 |       case TremoloKey::x :
 | 
|---|
 | 361 |         // for the moment, assume there are always three dimensions
 | 
|---|
| [47d041] | 362 |         LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getPosition());
 | 
|---|
| [d74077] | 363 |         *file << currentAtom->at(0) << "\t";
 | 
|---|
 | 364 |         *file << currentAtom->at(1) << "\t";
 | 
|---|
 | 365 |         *file << currentAtom->at(2) << "\t";
 | 
|---|
| [b8d4a3] | 366 |         break;
 | 
|---|
 | 367 |       case TremoloKey::u :
 | 
|---|
 | 368 |         // for the moment, assume there are always three dimensions
 | 
|---|
| [47d041] | 369 |         LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getAtomicVelocity());
 | 
|---|
| [bce72c] | 370 |         *file << currentAtom->getAtomicVelocity()[0] << "\t";
 | 
|---|
 | 371 |         *file << currentAtom->getAtomicVelocity()[1] << "\t";
 | 
|---|
 | 372 |         *file << currentAtom->getAtomicVelocity()[2] << "\t";
 | 
|---|
| [b8d4a3] | 373 |         break;
 | 
|---|
| [e6e4a0] | 374 |       case TremoloKey::F :
 | 
|---|
 | 375 |         // for the moment, assume there are always three dimensions
 | 
|---|
 | 376 |         LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getAtomicForce());
 | 
|---|
 | 377 |         *file << currentAtom->getAtomicForce()[0] << "\t";
 | 
|---|
 | 378 |         *file << currentAtom->getAtomicForce()[1] << "\t";
 | 
|---|
 | 379 |         *file << currentAtom->getAtomicForce()[2] << "\t";
 | 
|---|
 | 380 |         break;
 | 
|---|
| [305e7e] | 381 |       case TremoloKey::type :
 | 
|---|
| [acd638] | 382 |         if (additionalAtomData.count(currentAtom->getId())) {
 | 
|---|
 | 383 |           if (additionalAtomData[currentAtom->getId()].get(currentField) != "-") {
 | 
|---|
| [47d041] | 384 |             LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField));
 | 
|---|
| [acd638] | 385 |             *file << additionalAtomData[currentAtom->getId()].get(currentField) << "\t";
 | 
|---|
 | 386 |           } else {
 | 
|---|
| [47d041] | 387 |             LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value: " << currentAtom->getType()->getSymbol());
 | 
|---|
| [acd638] | 388 |             *file << currentAtom->getType()->getSymbol() << "\t";
 | 
|---|
 | 389 |           }
 | 
|---|
 | 390 |         } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) {
 | 
|---|
 | 391 |           if (additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) != "-") {
 | 
|---|
| [47d041] | 392 |             LOG(3, "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField));
 | 
|---|
| [acd638] | 393 |             *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField) << "\t";
 | 
|---|
 | 394 |           } else {
 | 
|---|
| [47d041] | 395 |             LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value from father: " << currentAtom->GetTrueFather()->getType()->getSymbol());
 | 
|---|
| [acd638] | 396 |             *file << currentAtom->GetTrueFather()->getType()->getSymbol() << "\t";
 | 
|---|
 | 397 |           }
 | 
|---|
| [4d4d33] | 398 |         } else {
 | 
|---|
| [47d041] | 399 |           LOG(3, "Writing for type " << knownKeyNames[currentField] << " its default value: " << currentAtom->getType()->getSymbol());
 | 
|---|
| [acd638] | 400 |           *file << currentAtom->getType()->getSymbol() << "\t";
 | 
|---|
| [4d4d33] | 401 |         }
 | 
|---|
| [b8d4a3] | 402 |         break;
 | 
|---|
 | 403 |       case TremoloKey::Id :
 | 
|---|
| [47d041] | 404 |         LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getId()+1);
 | 
|---|
| [812155] | 405 |         *file << getLocalId(currentAtom->getId()) << "\t";
 | 
|---|
| [b8d4a3] | 406 |         break;
 | 
|---|
 | 407 |       case TremoloKey::neighbors :
 | 
|---|
| [47d041] | 408 |         LOG(3, "Writing type " << knownKeyNames[currentField]);
 | 
|---|
| [b8d4a3] | 409 |         writeNeighbors(file, atoi(it->substr(it->find("=") + 1, 1).c_str()), currentAtom);
 | 
|---|
 | 410 |         break;
 | 
|---|
| [74a444] | 411 |       case TremoloKey::resSeq :
 | 
|---|
| [4d4d33] | 412 |         if (additionalAtomData.count(currentAtom->getId())) {
 | 
|---|
| [47d041] | 413 |           LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField));
 | 
|---|
| [74a444] | 414 |           *file << additionalAtomData[currentAtom->getId()].get(currentField);
 | 
|---|
 | 415 |         } else if (currentAtom->getMolecule() != NULL) {
 | 
|---|
| [47d041] | 416 |           LOG(3, "Writing for type " << knownKeyNames[currentField] << " its own id: " << currentAtom->getMolecule()->getId()+1);
 | 
|---|
| [74a444] | 417 |           *file << setw(4) << currentAtom->getMolecule()->getId()+1;
 | 
|---|
 | 418 |         } else {
 | 
|---|
| [47d041] | 419 |           LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value: " << defaultAdditionalData.get(currentField));
 | 
|---|
| [74a444] | 420 |           *file << defaultAdditionalData.get(currentField);
 | 
|---|
 | 421 |         }
 | 
|---|
 | 422 |         *file << "\t";
 | 
|---|
| [4d4d33] | 423 |         break;
 | 
|---|
| [2034f3] | 424 |       case TremoloKey::charge :
 | 
|---|
 | 425 |         if (currentAtom->getCharge() == 0.) {
 | 
|---|
 | 426 |           if (additionalAtomData.count(currentAtom->getId())) {
 | 
|---|
 | 427 |             LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField));
 | 
|---|
 | 428 |             *file << additionalAtomData[currentAtom->getId()].get(currentField);
 | 
|---|
 | 429 |           } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) {
 | 
|---|
 | 430 |             LOG(3, "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField));
 | 
|---|
 | 431 |             *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField);
 | 
|---|
 | 432 |           } else {
 | 
|---|
 | 433 |             LOG(3, "Writing for type " << knownKeyNames[currentField] << " AtomInfo::charge : " << currentAtom->getCharge());
 | 
|---|
 | 434 |             *file << currentAtom->getCharge();
 | 
|---|
 | 435 |           }
 | 
|---|
 | 436 |         } else {
 | 
|---|
 | 437 |           LOG(3, "Writing for type " << knownKeyNames[currentField] << " AtomInfo::charge : " << currentAtom->getCharge());
 | 
|---|
 | 438 |           *file << currentAtom->getCharge();
 | 
|---|
 | 439 |         }
 | 
|---|
 | 440 |         *file << "\t";
 | 
|---|
 | 441 |         break;
 | 
|---|
| [b8d4a3] | 442 |       default :
 | 
|---|
| [4d4d33] | 443 |         if (additionalAtomData.count(currentAtom->getId())) {
 | 
|---|
| [47d041] | 444 |           LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField));
 | 
|---|
| [74a444] | 445 |           *file << additionalAtomData[currentAtom->getId()].get(currentField);
 | 
|---|
| [4d4d33] | 446 |         } else if (additionalAtomData.count(currentAtom->GetTrueFather()->getId())) {
 | 
|---|
| [47d041] | 447 |           LOG(3, "Writing for type " << knownKeyNames[currentField] << " stuff from father: " << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField));
 | 
|---|
| [74a444] | 448 |           *file << additionalAtomData[currentAtom->GetTrueFather()->getId()].get(currentField);
 | 
|---|
 | 449 |         } else {
 | 
|---|
| [47d041] | 450 |           LOG(3, "Writing for type " << knownKeyNames[currentField] << " the default: " << defaultAdditionalData.get(currentField));
 | 
|---|
| [74a444] | 451 |           *file << defaultAdditionalData.get(currentField);
 | 
|---|
 | 452 |         }
 | 
|---|
| [b8d4a3] | 453 |         *file << "\t";
 | 
|---|
 | 454 |         break;
 | 
|---|
 | 455 |     }
 | 
|---|
 | 456 |   }
 | 
|---|
 | 457 | 
 | 
|---|
| [23fd43] | 458 |   *file << std::endl;
 | 
|---|
| [b8d4a3] | 459 | }
 | 
|---|
 | 460 | 
 | 
|---|
 | 461 | /**
 | 
|---|
 | 462 |  * Writes the neighbor information of one atom to the provided stream.
 | 
|---|
 | 463 |  *
 | 
|---|
| [9d83b6] | 464 |  * Note that ListOfBonds of WorldTime::CurrentTime is used.
 | 
|---|
 | 465 |  *
 | 
|---|
| [b8d4a3] | 466 |  * \param stream where to write neighbor information to
 | 
|---|
 | 467 |  * \param number of neighbors
 | 
|---|
 | 468 |  * \param reference to the atom of which to take the neighbor information
 | 
|---|
 | 469 |  */
 | 
|---|
| [23fd43] | 470 | void FormatParser< tremolo >::writeNeighbors(std::ostream* file, const int numberOfNeighbors, const atom* currentAtom) {
 | 
|---|
| [9d83b6] | 471 |   const BondList& ListOfBonds = currentAtom->getListOfBonds();
 | 
|---|
| [ca2cfa] | 472 |   // sort bonded indices
 | 
|---|
 | 473 |   typedef std::set<atomId_t> sortedIndices;
 | 
|---|
 | 474 |   sortedIndices sortedBonds;
 | 
|---|
 | 475 |   for (BondList::const_iterator iter = ListOfBonds.begin();
 | 
|---|
 | 476 |       iter != ListOfBonds.end(); ++iter)
 | 
|---|
| [812155] | 477 |     sortedBonds.insert(getLocalId((*iter)->GetOtherAtom(currentAtom)->getId()));
 | 
|---|
| [ca2cfa] | 478 |   // print indices
 | 
|---|
 | 479 |   sortedIndices::const_iterator currentBond = sortedBonds.begin();
 | 
|---|
| [b8d4a3] | 480 |   for (int i = 0; i < numberOfNeighbors; i++) {
 | 
|---|
| [812155] | 481 |     *file << (currentBond != sortedBonds.end() ? (*currentBond) : 0) << "\t";
 | 
|---|
| [ca2cfa] | 482 |     if (currentBond != sortedBonds.end())
 | 
|---|
| [0bbfa1] | 483 |       ++currentBond;
 | 
|---|
| [b8d4a3] | 484 |   }
 | 
|---|
| [9131f3] | 485 | }
 | 
|---|
 | 486 | 
 | 
|---|
 | 487 | /**
 | 
|---|
| [23fd43] | 488 |  * Stores keys from the ATOMDATA line in \a fields.
 | 
|---|
| [9131f3] | 489 |  *
 | 
|---|
 | 490 |  * \param line to parse the keys from
 | 
|---|
| [23fd43] | 491 |  * \param offset with which offset the keys begin within the line
 | 
|---|
 | 492 |  * \param fields which usedFields to use
 | 
|---|
| [9131f3] | 493 |  */
 | 
|---|
| [23fd43] | 494 | void FormatParser< tremolo >::parseAtomDataKeysLine(
 | 
|---|
 | 495 |     const std::string &line,
 | 
|---|
 | 496 |     const int offset,
 | 
|---|
 | 497 |     usedFields_t &fields) {
 | 
|---|
| [8bf9c6] | 498 |   std::string keyword;
 | 
|---|
 | 499 |   std::stringstream lineStream;
 | 
|---|
| [9131f3] | 500 | 
 | 
|---|
 | 501 |   lineStream << line.substr(offset);
 | 
|---|
 | 502 |   while (lineStream.good()) {
 | 
|---|
 | 503 |     lineStream >> keyword;
 | 
|---|
| [b8d4a3] | 504 |     if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey) {
 | 
|---|
| [ecb799] | 505 |       // TODO: throw exception about unknown key
 | 
|---|
| [5a667d] | 506 |       cout << "Unknown key: " << keyword.substr(0, keyword.find("=")) << " is not part of the tremolo format specification." << endl;
 | 
|---|
 | 507 |       throw IllegalParserKeyException();
 | 
|---|
| [4415da] | 508 |       break;
 | 
|---|
 | 509 |     }
 | 
|---|
| [23fd43] | 510 |     fields.push_back(keyword);
 | 
|---|
| [9131f3] | 511 |   }
 | 
|---|
| [23fd43] | 512 |   //LOG(1, "INFO: " << fields);
 | 
|---|
| [9131f3] | 513 | }
 | 
|---|
 | 514 | 
 | 
|---|
| [5a667d] | 515 | /**
 | 
|---|
 | 516 |  * Tests whether the keys from the ATOMDATA line can be read correctly.
 | 
|---|
 | 517 |  *
 | 
|---|
 | 518 |  * \param line to parse the keys from
 | 
|---|
 | 519 |  */
 | 
|---|
 | 520 | bool FormatParser< tremolo >::testParseAtomDataKeysLine(
 | 
|---|
 | 521 |     const std::string &line) {
 | 
|---|
 | 522 |   std::string keyword;
 | 
|---|
 | 523 |   std::stringstream lineStream;
 | 
|---|
 | 524 | 
 | 
|---|
 | 525 |   // check string after ATOMDATA
 | 
|---|
 | 526 |   const std::string AtomData("ATOMDATA");
 | 
|---|
 | 527 |   const size_t AtomDataOffset = line.find(AtomData, 0);
 | 
|---|
 | 528 |   if (AtomDataOffset == std::string::npos)
 | 
|---|
 | 529 |     lineStream << line;
 | 
|---|
 | 530 |   else
 | 
|---|
 | 531 |     lineStream << line.substr(AtomDataOffset + AtomData.length());
 | 
|---|
 | 532 |   while (lineStream.good()) {
 | 
|---|
 | 533 |     lineStream >> keyword;
 | 
|---|
 | 534 |     //LOG(2, "DEBUG: Checking key " << keyword.substr(0, keyword.find("=")) << ".");
 | 
|---|
 | 535 |     if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey)
 | 
|---|
 | 536 |       return false;
 | 
|---|
 | 537 |   }
 | 
|---|
 | 538 |   //LOG(1, "INFO: " << fields);
 | 
|---|
 | 539 |   return true;
 | 
|---|
 | 540 | }
 | 
|---|
 | 541 | 
 | 
|---|
| [ca331c] | 542 | std::string FormatParser< tremolo >::getAtomData() const
 | 
|---|
 | 543 | {
 | 
|---|
 | 544 |   std::stringstream output;
 | 
|---|
 | 545 |   std::for_each(usedFields_save.begin(), usedFields_save.end(),
 | 
|---|
 | 546 |       output << boost::lambda::_1 << " ");
 | 
|---|
 | 547 |   const std::string returnstring(output.str());
 | 
|---|
 | 548 |   return returnstring.substr(0, returnstring.find_last_of(" "));
 | 
|---|
 | 549 | }
 | 
|---|
 | 550 | 
 | 
|---|
 | 551 | /** Appends the properties per atom to print to .data file by parsing line from
 | 
|---|
 | 552 |  *  \a atomdata_string.
 | 
|---|
 | 553 |  *
 | 
|---|
 | 554 |  *  We just call \sa  FormatParser< tremolo >::parseAtomDataKeysLine().
 | 
|---|
 | 555 |  *
 | 
|---|
 | 556 |  * @param atomdata_string line to parse with space-separated values
 | 
|---|
 | 557 |  */
 | 
|---|
 | 558 | void FormatParser< tremolo >::setAtomData(const std::string &atomdata_string)
 | 
|---|
 | 559 | {
 | 
|---|
 | 560 |   parseAtomDataKeysLine(atomdata_string, 0, usedFields_save);
 | 
|---|
 | 561 | }
 | 
|---|
 | 562 | 
 | 
|---|
| [81c980b] | 563 | /** Sets the properties per atom to print to .data file by parsing line from
 | 
|---|
 | 564 |  *  \a atomdata_string.
 | 
|---|
 | 565 |  *
 | 
|---|
| [23fd43] | 566 |  *  We just call \sa  FormatParser< tremolo >::parseAtomDataKeysLine(), however
 | 
|---|
 | 567 |  *  we clear FormatParser< tremolo >::usedFields_save.
 | 
|---|
| [81c980b] | 568 |  *
 | 
|---|
 | 569 |  * @param atomdata_string line to parse with space-separated values
 | 
|---|
 | 570 |  */
 | 
|---|
| [ca331c] | 571 | void FormatParser< tremolo >::resetAtomData(const std::string &atomdata_string)
 | 
|---|
| [81c980b] | 572 | {
 | 
|---|
| [23fd43] | 573 |   usedFields_save.clear();
 | 
|---|
 | 574 |   parseAtomDataKeysLine(atomdata_string, 0, usedFields_save);
 | 
|---|
| [81c980b] | 575 | }
 | 
|---|
 | 576 | 
 | 
|---|
 | 577 | 
 | 
|---|
| [9131f3] | 578 | /**
 | 
|---|
 | 579 |  * Reads one data line of a tremolo file and interprets it according to the keys
 | 
|---|
 | 580 |  * obtained from the ATOMDATA line.
 | 
|---|
 | 581 |  *
 | 
|---|
 | 582 |  * \param line to parse as an atom
 | 
|---|
| [dc1d9e] | 583 |  * \param *newmol molecule to add atom to
 | 
|---|
| [9131f3] | 584 |  */
 | 
|---|
| [23fd43] | 585 | void FormatParser< tremolo >::readAtomDataLine(const std::string &line, molecule *newmol = NULL) {
 | 
|---|
| [8bf9c6] | 586 |   std::stringstream lineStream;
 | 
|---|
| [4415da] | 587 |   atom* newAtom = World::getInstance().createAtom();
 | 
|---|
| [89a31d] | 588 |   const atomId_t atomid = newAtom->getId();
 | 
|---|
 | 589 |   additionalAtomData[atomid] = TremoloAtomInfoContainer(); // fill with default values
 | 
|---|
 | 590 |   TremoloAtomInfoContainer *atomInfo = &additionalAtomData[atomid];
 | 
|---|
| [b8d4a3] | 591 |   TremoloKey::atomDataKey currentField;
 | 
|---|
| [72d108] | 592 |   ConvertTo<double> toDouble;
 | 
|---|
 | 593 |   ConvertTo<int> toInt;
 | 
|---|
| [056e70] | 594 |   Vector tempVector;
 | 
|---|
| [72d108] | 595 | 
 | 
|---|
 | 596 |   // setup tokenizer, splitting up white-spaced entries
 | 
|---|
 | 597 |   typedef boost::tokenizer<boost::char_separator<char> >
 | 
|---|
 | 598 |       tokenizer;
 | 
|---|
 | 599 |   boost::char_separator<char> whitespacesep(" \t");
 | 
|---|
 | 600 |   tokenizer tokens(line, whitespacesep);
 | 
|---|
 | 601 |   ASSERT(tokens.begin() != tokens.end(),
 | 
|---|
| [765f16] | 602 |       "FormatParser< tremolo >::readAtomDataLine - empty string, need at least ' '!");
 | 
|---|
| [72d108] | 603 |   tokenizer::iterator tok_iter = tokens.begin();
 | 
|---|
 | 604 |   // then associate each token to each file
 | 
|---|
| [23fd43] | 605 |   for (usedFields_t::const_iterator it = usedFields_load.begin(); it < usedFields_load.end(); it++) {
 | 
|---|
| [72d108] | 606 |     const std::string keyName = it->substr(0, it->find("="));
 | 
|---|
 | 607 |     currentField = knownKeys[keyName];
 | 
|---|
| [8bf9c6] | 608 |     const std::string word = *tok_iter;
 | 
|---|
| [47d041] | 609 |     LOG(4, "INFO: Parsing key " << keyName << " with remaining data " << word);
 | 
|---|
| [4415da] | 610 |     switch (currentField) {
 | 
|---|
| [b8d4a3] | 611 |       case TremoloKey::x :
 | 
|---|
| [4415da] | 612 |         // for the moment, assume there are always three dimensions
 | 
|---|
| [d74077] | 613 |         for (int i=0;i<NDIM;i++) {
 | 
|---|
| [765f16] | 614 |           ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for x["+toString(i)+"]!");
 | 
|---|
| [47d041] | 615 |           LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter);
 | 
|---|
| [72d108] | 616 |           newAtom->set(i, toDouble(*tok_iter));
 | 
|---|
 | 617 |           tok_iter++;
 | 
|---|
| [d74077] | 618 |         }
 | 
|---|
| [4415da] | 619 |         break;
 | 
|---|
| [b8d4a3] | 620 |       case TremoloKey::u :
 | 
|---|
| [4415da] | 621 |         // for the moment, assume there are always three dimensions
 | 
|---|
| [72d108] | 622 |         for (int i=0;i<NDIM;i++) {
 | 
|---|
| [765f16] | 623 |           ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for u["+toString(i)+"]!");
 | 
|---|
| [47d041] | 624 |           LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter);
 | 
|---|
| [056e70] | 625 |           tempVector[i] = toDouble(*tok_iter);
 | 
|---|
| [72d108] | 626 |           tok_iter++;
 | 
|---|
 | 627 |         }
 | 
|---|
| [056e70] | 628 |         newAtom->setAtomicVelocity(tempVector);
 | 
|---|
| [4415da] | 629 |         break;
 | 
|---|
| [e6e4a0] | 630 |       case TremoloKey::F :
 | 
|---|
 | 631 |         // for the moment, assume there are always three dimensions
 | 
|---|
 | 632 |         for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 633 |           ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for F["+toString(i)+"]!");
 | 
|---|
 | 634 |           LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter);
 | 
|---|
 | 635 |           tempVector[i] = toDouble(*tok_iter);
 | 
|---|
 | 636 |           tok_iter++;
 | 
|---|
 | 637 |         }
 | 
|---|
 | 638 |         newAtom->setAtomicForce(tempVector);
 | 
|---|
 | 639 |         break;
 | 
|---|
| [305e7e] | 640 |       case TremoloKey::type :
 | 
|---|
| [4d4d33] | 641 |       {
 | 
|---|
| [765f16] | 642 |         ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
 | 
|---|
| [47d041] | 643 |         LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter);
 | 
|---|
| [a275b3] | 644 |         std::string element;
 | 
|---|
 | 645 |         try {
 | 
|---|
 | 646 |           element = knownTypes.getType(*tok_iter);
 | 
|---|
 | 647 |         } catch(IllegalParserKeyException) {
 | 
|---|
 | 648 |           // clean up
 | 
|---|
 | 649 |           World::getInstance().destroyAtom(newAtom);
 | 
|---|
 | 650 |           // give an error
 | 
|---|
 | 651 |           ELOG(0, "TremoloParser: I do not understand the element token " << *tok_iter << ".");
 | 
|---|
 | 652 |         }
 | 
|---|
| [4d4d33] | 653 |         // put type name into container for later use
 | 
|---|
 | 654 |         atomInfo->set(currentField, *tok_iter);
 | 
|---|
| [47d041] | 655 |         LOG(4, "INFO: Parsing element " << (*tok_iter) << " as " << element << " according to KnownTypes.");
 | 
|---|
| [72d108] | 656 |         tok_iter++;
 | 
|---|
| [4d4d33] | 657 |         newAtom->setType(World::getInstance().getPeriode()->FindElement(element));
 | 
|---|
| [b8d4a3] | 658 |         ASSERT(newAtom->getType(), "Type was not set for this atom");
 | 
|---|
| [4415da] | 659 |         break;
 | 
|---|
| [4d4d33] | 660 |       }
 | 
|---|
| [b8d4a3] | 661 |       case TremoloKey::Id :
 | 
|---|
| [765f16] | 662 |         ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
 | 
|---|
| [47d041] | 663 |         LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter);
 | 
|---|
| [c0e28c] | 664 |         associateLocaltoGlobalId(toInt(*tok_iter), atomid);
 | 
|---|
| [72d108] | 665 |         tok_iter++;
 | 
|---|
| [4415da] | 666 |         break;
 | 
|---|
| [b8d4a3] | 667 |       case TremoloKey::neighbors :
 | 
|---|
| [72d108] | 668 |         for (int i=0;i<atoi(it->substr(it->find("=") + 1, 1).c_str());i++) {
 | 
|---|
| [765f16] | 669 |           ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
 | 
|---|
| [47d041] | 670 |           LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter);
 | 
|---|
| [72d108] | 671 |           lineStream << *tok_iter << "\t";
 | 
|---|
 | 672 |           tok_iter++;
 | 
|---|
 | 673 |         }
 | 
|---|
| [b8d4a3] | 674 |         readNeighbors(&lineStream,
 | 
|---|
| [89a31d] | 675 |             atoi(it->substr(it->find("=") + 1, 1).c_str()), atomid);
 | 
|---|
| [9131f3] | 676 |         break;
 | 
|---|
| [2034f3] | 677 |       case TremoloKey::charge :
 | 
|---|
 | 678 |         ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
 | 
|---|
 | 679 |         LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter);
 | 
|---|
 | 680 |         atomInfo->set(currentField, *tok_iter);
 | 
|---|
 | 681 |         newAtom->setCharge(boost::lexical_cast<double>(*tok_iter));
 | 
|---|
 | 682 |         tok_iter++;
 | 
|---|
 | 683 |         break;
 | 
|---|
| [9131f3] | 684 |       default :
 | 
|---|
| [765f16] | 685 |         ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
 | 
|---|
| [47d041] | 686 |         LOG(4, "INFO: Parsing key " << keyName << " with next token " << *tok_iter);
 | 
|---|
| [72d108] | 687 |         atomInfo->set(currentField, *tok_iter);
 | 
|---|
 | 688 |         tok_iter++;
 | 
|---|
| [9131f3] | 689 |         break;
 | 
|---|
 | 690 |     }
 | 
|---|
 | 691 |   }
 | 
|---|
| [89a31d] | 692 |   LOG(3, "INFO: Parsed atom " << atomid << ".");
 | 
|---|
 | 693 |   if (newmol != NULL)
 | 
|---|
| [dc1d9e] | 694 |     newmol->AddAtom(newAtom);
 | 
|---|
| [6bc51d] | 695 | }
 | 
|---|
| [9131f3] | 696 | 
 | 
|---|
| [531f27] | 697 | bool FormatParser< tremolo >::saveAtomsInExttypes(std::ostream &output, const std::vector<atom*> &atoms, const int id) const
 | 
|---|
 | 698 | {
 | 
|---|
 | 699 |   bool status = true;
 | 
|---|
 | 700 |   // parse the file
 | 
|---|
 | 701 |   for (std::vector<atom *>::const_iterator iter = atoms.begin();
 | 
|---|
 | 702 |       iter != atoms.end(); ++iter) {
 | 
|---|
 | 703 |     const int atomicid = getLocalId((*iter)->getId());
 | 
|---|
 | 704 |     if (atomicid == -1)
 | 
|---|
 | 705 |       status = false;
 | 
|---|
 | 706 |     output << atomicid << "\t" << id << std::endl;
 | 
|---|
 | 707 |   }
 | 
|---|
 | 708 | 
 | 
|---|
 | 709 |   return status;
 | 
|---|
 | 710 | }
 | 
|---|
 | 711 | 
 | 
|---|
| [b8d4a3] | 712 | /**
 | 
|---|
 | 713 |  * Reads neighbor information for one atom from the input.
 | 
|---|
 | 714 |  *
 | 
|---|
| [0bbfa1] | 715 |  * \param line stream where to read the information from
 | 
|---|
 | 716 |  * \param numberOfNeighbors number of neighbors to read
 | 
|---|
 | 717 |  * \param atomid world id of the atom the information belongs to
 | 
|---|
| [b8d4a3] | 718 |  */
 | 
|---|
| [23fd43] | 719 | void FormatParser< tremolo >::readNeighbors(std::stringstream* line, const int numberOfNeighbors, const int atomId) {
 | 
|---|
| [b8d4a3] | 720 |   int neighborId = 0;
 | 
|---|
 | 721 |   for (int i = 0; i < numberOfNeighbors; i++) {
 | 
|---|
 | 722 |     *line >> neighborId;
 | 
|---|
 | 723 |     // 0 is used to fill empty neighbor positions in the tremolo file.
 | 
|---|
 | 724 |     if (neighborId > 0) {
 | 
|---|
| [47d041] | 725 |       LOG(4, "INFO: Atom with global id " << atomId
 | 
|---|
 | 726 |           << " has neighbour with serial " << neighborId);
 | 
|---|
| [b8d4a3] | 727 |       additionalAtomData[atomId].neighbors.push_back(neighborId);
 | 
|---|
 | 728 |     }
 | 
|---|
 | 729 |   }
 | 
|---|
 | 730 | }
 | 
|---|
| [9131f3] | 731 | 
 | 
|---|
 | 732 | /**
 | 
|---|
| [23fd43] | 733 |  * Checks whether the provided name is within \a fields.
 | 
|---|
| [b8d4a3] | 734 |  *
 | 
|---|
| [23fd43] | 735 |  * \param fields which usedFields to use
 | 
|---|
 | 736 |  * \param fieldName name to check
 | 
|---|
| [b8d4a3] | 737 |  * \return true if the field name is used
 | 
|---|
| [9131f3] | 738 |  */
 | 
|---|
| [23fd43] | 739 | bool FormatParser< tremolo >::isUsedField(const usedFields_t &fields, const std::string &fieldName) const
 | 
|---|
 | 740 | {
 | 
|---|
| [b8d4a3] | 741 |   bool fieldNameExists = false;
 | 
|---|
| [23fd43] | 742 |   for (usedFields_t::const_iterator usedField = fields.begin();
 | 
|---|
 | 743 |       usedField != fields.end(); usedField++) {
 | 
|---|
| [b8d4a3] | 744 |     if (usedField->substr(0, usedField->find("=")) == fieldName)
 | 
|---|
 | 745 |       fieldNameExists = true;
 | 
|---|
 | 746 |   }
 | 
|---|
| [9131f3] | 747 | 
 | 
|---|
| [b8d4a3] | 748 |   return fieldNameExists;
 | 
|---|
 | 749 | }
 | 
|---|
 | 750 | 
 | 
|---|
 | 751 | 
 | 
|---|
 | 752 | /**
 | 
|---|
 | 753 |  * Adds the collected neighbor information to the atoms in the world. The atoms
 | 
|---|
 | 754 |  * are found by their current ID and mapped to the corresponding atoms with the
 | 
|---|
 | 755 |  * Id found in the parsed file.
 | 
|---|
| [9f8b01] | 756 |  *
 | 
|---|
 | 757 |  * @param atoms vector with all newly added (global) atomic ids
 | 
|---|
| [b8d4a3] | 758 |  */
 | 
|---|
| [9f8b01] | 759 | void FormatParser< tremolo >::processNeighborInformation(const std::vector<atomId_t> &atoms) {
 | 
|---|
| [23fd43] | 760 |   if (!isUsedField(usedFields_load, "neighbors")) {
 | 
|---|
| [b8d4a3] | 761 |     return;
 | 
|---|
 | 762 |   }
 | 
|---|
 | 763 | 
 | 
|---|
| [9f8b01] | 764 |   for (std::vector<atomId_t>::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
 | 
|---|
 | 765 |     ASSERT(additionalAtomData.count(*iter) != 0,
 | 
|---|
 | 766 |         "FormatParser< tremolo >::processNeighborInformation() - global id "
 | 
|---|
 | 767 |         +toString(*iter)+" unknown in additionalAtomData.");
 | 
|---|
 | 768 |     TremoloAtomInfoContainer ¤tInfo = additionalAtomData[*iter];
 | 
|---|
 | 769 |     ASSERT (!currentInfo.neighbors_processed,
 | 
|---|
 | 770 |         "FormatParser< tremolo >::processNeighborInformation() - neighbors of new atom "
 | 
|---|
 | 771 |         +toString(*iter)+" are already processed.");
 | 
|---|
 | 772 |     for(std::vector<int>::const_iterator neighbor = currentInfo.neighbors.begin();
 | 
|---|
 | 773 |       neighbor != currentInfo.neighbors.end(); neighbor++
 | 
|---|
 | 774 |     ) {
 | 
|---|
 | 775 |       LOG(3, "INFO: Creating bond between ("
 | 
|---|
 | 776 |           << *iter
 | 
|---|
 | 777 |           << ") and ("
 | 
|---|
 | 778 |           << getGlobalId(*neighbor) << "|" << *neighbor << ")");
 | 
|---|
 | 779 |       ASSERT(getGlobalId(*neighbor) != -1,
 | 
|---|
 | 780 |           "FormatParser< tremolo >::processNeighborInformation() - global id to local id "
 | 
|---|
 | 781 |           +toString(*neighbor)+" is unknown.");
 | 
|---|
 | 782 |       World::getInstance().getAtom(AtomById(*iter))
 | 
|---|
 | 783 |           ->addBond(WorldTime::getTime(), World::getInstance().getAtom(AtomById(getGlobalId(*neighbor))));
 | 
|---|
| [9131f3] | 784 |     }
 | 
|---|
| [9f8b01] | 785 |     currentInfo.neighbors_processed = true;
 | 
|---|
| [9131f3] | 786 |   }
 | 
|---|
| [6bc51d] | 787 | }
 | 
|---|
 | 788 | 
 | 
|---|
| [9131f3] | 789 | /**
 | 
|---|
| [b8d4a3] | 790 |  * Replaces atom IDs read from the file by the corresponding world IDs. All IDs
 | 
|---|
 | 791 |  * IDs of the input string will be replaced; expected separating characters are
 | 
|---|
 | 792 |  * "-" and ",".
 | 
|---|
| [9131f3] | 793 |  *
 | 
|---|
| [b8d4a3] | 794 |  * \param string in which atom IDs should be adapted
 | 
|---|
 | 795 |  *
 | 
|---|
 | 796 |  * \return input string with modified atom IDs
 | 
|---|
| [9131f3] | 797 |  */
 | 
|---|
| [955b91] | 798 | std::string FormatParser< tremolo >::adaptIdDependentDataString(std::string data) {
 | 
|---|
| [b8d4a3] | 799 |   // there might be no IDs
 | 
|---|
 | 800 |   if (data == "-") {
 | 
|---|
 | 801 |     return "-";
 | 
|---|
 | 802 |   }
 | 
|---|
 | 803 | 
 | 
|---|
 | 804 |   char separator;
 | 
|---|
 | 805 |   int id;
 | 
|---|
| [8bf9c6] | 806 |   std::stringstream line, result;
 | 
|---|
| [b8d4a3] | 807 |   line << data;
 | 
|---|
 | 808 | 
 | 
|---|
 | 809 |   line >> id;
 | 
|---|
| [c0e28c] | 810 |   result << getGlobalId(id);
 | 
|---|
| [b8d4a3] | 811 |   while (line.good()) {
 | 
|---|
 | 812 |     line >> separator >> id;
 | 
|---|
| [c0e28c] | 813 |     result << separator << getGlobalId(id);
 | 
|---|
| [b8d4a3] | 814 |   }
 | 
|---|
 | 815 | 
 | 
|---|
 | 816 |   return result.str();
 | 
|---|
| [6bc51d] | 817 | }
 | 
|---|
| [b8d4a3] | 818 | 
 | 
|---|
 | 819 | /**
 | 
|---|
 | 820 |  * Corrects the atom IDs in each imprData entry to the corresponding world IDs
 | 
|---|
 | 821 |  * as they might differ from the originally read IDs.
 | 
|---|
 | 822 |  */
 | 
|---|
| [765f16] | 823 | void FormatParser< tremolo >::adaptImprData() {
 | 
|---|
| [23fd43] | 824 |   if (!isUsedField(usedFields_load, "imprData")) {
 | 
|---|
| [b8d4a3] | 825 |     return;
 | 
|---|
 | 826 |   }
 | 
|---|
 | 827 | 
 | 
|---|
| [8bf9c6] | 828 |   for(std::map<const atomId_t, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin();
 | 
|---|
| [b8d4a3] | 829 |     currentInfo != additionalAtomData.end(); currentInfo++
 | 
|---|
 | 830 |   ) {
 | 
|---|
 | 831 |     currentInfo->second.imprData = adaptIdDependentDataString(currentInfo->second.imprData);
 | 
|---|
 | 832 |   }
 | 
|---|
| [6bc51d] | 833 | }
 | 
|---|
| [4415da] | 834 | 
 | 
|---|
| [b8d4a3] | 835 | /**
 | 
|---|
 | 836 |  * Corrects the atom IDs in each torsion entry to the corresponding world IDs
 | 
|---|
 | 837 |  * as they might differ from the originally read IDs.
 | 
|---|
 | 838 |  */
 | 
|---|
| [765f16] | 839 | void FormatParser< tremolo >::adaptTorsion() {
 | 
|---|
| [23fd43] | 840 |   if (!isUsedField(usedFields_load, "torsion")) {
 | 
|---|
| [b8d4a3] | 841 |     return;
 | 
|---|
 | 842 |   }
 | 
|---|
 | 843 | 
 | 
|---|
| [8bf9c6] | 844 |   for(std::map<const atomId_t, TremoloAtomInfoContainer>::iterator currentInfo = additionalAtomData.begin();
 | 
|---|
| [b8d4a3] | 845 |     currentInfo != additionalAtomData.end(); currentInfo++
 | 
|---|
 | 846 |   ) {
 | 
|---|
 | 847 |     currentInfo->second.torsion = adaptIdDependentDataString(currentInfo->second.torsion);
 | 
|---|
 | 848 |   }
 | 
|---|
 | 849 | }
 | 
|---|
 | 850 | 
 | 
|---|