[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[5aaa43] | 5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
[94d5ac6] | 6 | *
|
---|
| 7 | *
|
---|
| 8 | * This file is part of MoleCuilder.
|
---|
| 9 | *
|
---|
| 10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
[bcf653] | 22 | */
|
---|
| 23 |
|
---|
[6bc51d] | 24 | /*
|
---|
| 25 | * TremoloParser.cpp
|
---|
| 26 | *
|
---|
| 27 | * Created on: Mar 2, 2010
|
---|
| 28 | * Author: metzler
|
---|
| 29 | */
|
---|
| 30 |
|
---|
[bf3817] | 31 | // include config.h
|
---|
| 32 | #ifdef HAVE_CONFIG_H
|
---|
| 33 | #include <config.h>
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
[9eb71b3] | 36 | //#include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 37 |
|
---|
[ad011c] | 38 | #include "CodePatterns/Assert.hpp"
|
---|
| 39 | #include "CodePatterns/Log.hpp"
|
---|
[4d4d33] | 40 | #include "CodePatterns/toString.hpp"
|
---|
[ad011c] | 41 | #include "CodePatterns/Verbose.hpp"
|
---|
[42127c] | 42 |
|
---|
[9131f3] | 43 | #include "TremoloParser.hpp"
|
---|
[42127c] | 44 |
|
---|
[6f0841] | 45 | #include "Atom/atom.hpp"
|
---|
[129204] | 46 | #include "Bond/bond.hpp"
|
---|
[ccb487] | 47 | #include "Box.hpp"
|
---|
[42127c] | 48 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[3bdb6d] | 49 | #include "Element/element.hpp"
|
---|
| 50 | #include "Element/periodentafel.hpp"
|
---|
[ed8a68] | 51 | #include "Parser/Exceptions.hpp"
|
---|
| 52 | #include "Potentials/Particles/ParticleRegistry.hpp"
|
---|
[ccb487] | 53 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[740f9f] | 54 | #include "Potentials/Particles/ParticleRegistry.hpp"
|
---|
[42127c] | 55 | #include "molecule.hpp"
|
---|
| 56 | #include "World.hpp"
|
---|
| 57 | #include "WorldTime.hpp"
|
---|
| 58 |
|
---|
[9131f3] | 59 |
|
---|
[9f8b01] | 60 | #include <algorithm>
|
---|
[05e2ed] | 61 | #include <boost/bind.hpp>
|
---|
[4795cd] | 62 | #include <boost/function.hpp>
|
---|
[ca331c] | 63 | #include <boost/lambda/lambda.hpp>
|
---|
[2034f3] | 64 | #include <boost/lexical_cast.hpp>
|
---|
[72d108] | 65 | #include <boost/tokenizer.hpp>
|
---|
[74a444] | 66 | #include <iostream>
|
---|
| 67 | #include <iomanip>
|
---|
[8bf9c6] | 68 | #include <map>
|
---|
| 69 | #include <sstream>
|
---|
[ca331c] | 70 | #include <string>
|
---|
[8bf9c6] | 71 | #include <vector>
|
---|
[9131f3] | 72 |
|
---|
[5a667d] | 73 | #include <boost/assign/list_of.hpp> // for 'map_list_of()'
|
---|
| 74 | #include <boost/assert.hpp>
|
---|
| 75 |
|
---|
[765f16] | 76 | // declare specialized static variables
|
---|
| 77 | const std::string FormatParserTrait<tremolo>::name = "tremolo";
|
---|
| 78 | const std::string FormatParserTrait<tremolo>::suffix = "data";
|
---|
| 79 | const ParserTypes FormatParserTrait<tremolo>::type = tremolo;
|
---|
| 80 |
|
---|
[5a667d] | 81 | // static instances
|
---|
| 82 | std::map<std::string, TremoloKey::atomDataKey> FormatParser<tremolo>::knownKeys =
|
---|
| 83 | boost::assign::map_list_of("x",TremoloKey::x)
|
---|
| 84 | ("u",TremoloKey::u)
|
---|
| 85 | ("F",TremoloKey::F)
|
---|
| 86 | ("stress",TremoloKey::stress)
|
---|
| 87 | ("Id",TremoloKey::Id)
|
---|
| 88 | ("neighbors",TremoloKey::neighbors)
|
---|
| 89 | ("imprData",TremoloKey::imprData)
|
---|
| 90 | ("GroupMeasureTypeNo",TremoloKey::GroupMeasureTypeNo)
|
---|
| 91 | ("type",TremoloKey::type)
|
---|
| 92 | ("extType",TremoloKey::extType)
|
---|
| 93 | ("name",TremoloKey::name)
|
---|
| 94 | ("resName",TremoloKey::resName)
|
---|
| 95 | ("chainID",TremoloKey::chainID)
|
---|
| 96 | ("resSeq",TremoloKey::resSeq)
|
---|
| 97 | ("occupancy",TremoloKey::occupancy)
|
---|
| 98 | ("tempFactor",TremoloKey::tempFactor)
|
---|
| 99 | ("segID",TremoloKey::segID)
|
---|
| 100 | ("Charge",TremoloKey::Charge)
|
---|
| 101 | ("charge",TremoloKey::charge)
|
---|
| 102 | ("GrpTypeNo",TremoloKey::GrpTypeNo)
|
---|
| 103 | ("torsion",TremoloKey::torsion)
|
---|
| 104 | (" ",TremoloKey::noKey); // with this we can detect invalid keys
|
---|
| 105 |
|
---|
[9131f3] | 106 | /**
|
---|
| 107 | * Constructor.
|
---|
| 108 | */
|
---|
[765f16] | 109 | FormatParser< tremolo >::FormatParser() :
|
---|
[05e2ed] | 110 | FormatParser_common(NULL),
|
---|
| 111 | idglobalizer(boost::bind(&FormatParser< tremolo >::getGlobalId, this, _1)),
|
---|
| 112 | idlocalizer(boost::bind(&FormatParser< tremolo >::getLocalId, this, _1))
|
---|
[765f16] | 113 | {
|
---|
[4d4d33] | 114 | // invert knownKeys for debug output
|
---|
| 115 | for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin(); iter != knownKeys.end(); ++iter)
|
---|
| 116 | knownKeyNames.insert( make_pair( iter->second, iter->first) );
|
---|
| 117 |
|
---|
| 118 | additionalAtomData.clear();
|
---|
[9131f3] | 119 | }
|
---|
| 120 |
|
---|
[5a667d] | 121 |
|
---|
[9131f3] | 122 | /**
|
---|
| 123 | * Destructor.
|
---|
| 124 | */
|
---|
[765f16] | 125 | FormatParser< tremolo >::~FormatParser()
|
---|
| 126 | {
|
---|
[23fd43] | 127 | usedFields_save.clear();
|
---|
[b8d4a3] | 128 | additionalAtomData.clear();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * Loads atoms from a tremolo-formatted file.
|
---|
| 133 | *
|
---|
| 134 | * \param tremolo file
|
---|
| 135 | */
|
---|
[765f16] | 136 | void FormatParser< tremolo >::load(istream* file) {
|
---|
[8bf9c6] | 137 | std::string line;
|
---|
| 138 | std::string::size_type location;
|
---|
[b8d4a3] | 139 |
|
---|
[c0e28c] | 140 | // reset the id maps
|
---|
| 141 | resetIdAssociations();
|
---|
| 142 |
|
---|
[dc1d9e] | 143 | molecule *newmol = World::getInstance().createMolecule();
|
---|
[bd2390] | 144 | newmol->ActiveFlag = true;
|
---|
[efc80e] | 145 | usedFields_t usedFields_temp;
|
---|
| 146 | size_t timestep = 0;
|
---|
| 147 | atom *addedatom = NULL;
|
---|
[67e885] | 148 | std::list<atom *> addedatoms;
|
---|
| 149 | std::list<atom *>::iterator atomiter = addedatoms.begin();
|
---|
[b8d4a3] | 150 | while (file->good()) {
|
---|
| 151 | std::getline(*file, line, '\n');
|
---|
[23fd43] | 152 | // we only parse in the first ATOMDATA line
|
---|
[efc80e] | 153 | location = line.find("ATOMDATA", 0);
|
---|
| 154 | if (location != string::npos) {
|
---|
| 155 | parseAtomDataKeysLine(line, location + 8, usedFields_temp);
|
---|
| 156 | if (usedFields_load.empty()) {
|
---|
| 157 | // first ATOMDATA: use this line
|
---|
| 158 | usedFields_load.insert(usedFields_load.end(), usedFields_temp.begin(), usedFields_temp.end());
|
---|
| 159 | usedFields_temp.clear();
|
---|
| 160 | LOG(3, "DEBUG: Local usedFields is: " << usedFields_load);
|
---|
| 161 | } else {
|
---|
| 162 | // following ATOMDATA: check against present
|
---|
| 163 | LOG(3, "DEBUG: Parsed check usedFields is: " << usedFields_temp);
|
---|
| 164 | if (usedFields_load != usedFields_temp) {
|
---|
| 165 | ELOG(1, "File contains multiple time steps with differing ATOMDATA line, parsing only first time step.");
|
---|
| 166 | break;
|
---|
| 167 | } else {
|
---|
| 168 | // update time step
|
---|
| 169 | ++timestep;
|
---|
| 170 | atomiter = addedatoms.begin();
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 | } else {
|
---|
| 174 | if (line.length() > 0 && line.at(0) != '#') {
|
---|
| 175 | if (timestep != 0) {
|
---|
| 176 | ASSERT( atomiter != addedatoms.end(),
|
---|
| 177 | "FormatParser< tremolo >::load() - timesteps contains more atoms than first one.");
|
---|
| 178 | addedatom = *atomiter++;
|
---|
| 179 | }
|
---|
| 180 | readAtomDataLine(line, newmol, timestep, addedatom);
|
---|
| 181 | if (timestep == 0)
|
---|
| 182 | addedatoms.push_back(addedatom);
|
---|
[b8d4a3] | 183 | }
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
[efc80e] | 186 | ASSERT( addedatoms.size() == newmol->size(),
|
---|
| 187 | "FormatParser< tremolo >::load() - number of atoms in mol and addedatoms differ.");
|
---|
[2e352f] | 188 |
|
---|
[9f8b01] | 189 | // refresh atom::nr and atom::name
|
---|
| 190 | std::vector<atomId_t> atoms(newmol->getAtomCount());
|
---|
| 191 | std::transform(newmol->begin(), newmol->end(), atoms.begin(),
|
---|
| 192 | boost::bind(&atom::getId, _1));
|
---|
| 193 | processNeighborInformation(atoms);
|
---|
[05e2ed] | 194 | adaptImprData(atoms);
|
---|
| 195 | adaptTorsion(atoms);
|
---|
[23fd43] | 196 |
|
---|
| 197 | // append usedFields to global usedFields, is made unique on save, clear after use
|
---|
| 198 | usedFields_save.insert(usedFields_save.end(), usedFields_load.begin(), usedFields_load.end());
|
---|
| 199 | usedFields_load.clear();
|
---|
[b8d4a3] | 200 | }
|
---|
| 201 |
|
---|
| 202 | /**
|
---|
[73916f] | 203 | * Saves the \a atoms into as a tremolo file.
|
---|
[b8d4a3] | 204 | *
|
---|
| 205 | * \param file where to save the state
|
---|
[73916f] | 206 | * \param atoms atoms to store
|
---|
[b8d4a3] | 207 | */
|
---|
[fac58f] | 208 | void FormatParser< tremolo >::save(
|
---|
| 209 | std::ostream* file,
|
---|
| 210 | const std::vector<const atom *> &AtomList) {
|
---|
[830b3e] | 211 | LOG(2, "DEBUG: Saving changes to tremolo.");
|
---|
[e97a44] | 212 |
|
---|
[23fd43] | 213 | // install default usedFields if empty so far
|
---|
| 214 | if (usedFields_save.empty()) {
|
---|
| 215 | // default behavior: use all possible keys on output
|
---|
| 216 | for (std::map<std::string, TremoloKey::atomDataKey>::iterator iter = knownKeys.begin();
|
---|
| 217 | iter != knownKeys.end(); ++iter)
|
---|
| 218 | if (iter->second != TremoloKey::noKey) // don't add noKey
|
---|
| 219 | usedFields_save.push_back(iter->first);
|
---|
| 220 | }
|
---|
| 221 | // make present usedFields_save unique
|
---|
| 222 | makeUsedFieldsUnique(usedFields_save);
|
---|
[a86cda] | 223 | LOG(1, "INFO: Global (with unique entries) usedFields_save is: " << usedFields_save);
|
---|
[23fd43] | 224 |
|
---|
| 225 | // distribute ids continuously
|
---|
| 226 | distributeContinuousIds(AtomList);
|
---|
| 227 |
|
---|
[67e885] | 228 | std::pair<size_t, size_t> minmax_trajectories =
|
---|
| 229 | getMinMaxTrajectories(AtomList);
|
---|
| 230 | LOG(2, "INFO: There are " << minmax_trajectories.second << " steps to save.");
|
---|
[23fd43] | 231 |
|
---|
[67e885] | 232 | for (size_t step = 0; (step < minmax_trajectories.second) || (step == 0); ++step) {
|
---|
| 233 | // store atomdata
|
---|
| 234 | save_AtomDataLine(file);
|
---|
[b8d4a3] | 235 |
|
---|
[67e885] | 236 | // store box only on first step
|
---|
| 237 | if (step == 0)
|
---|
| 238 | save_BoxLine(file);
|
---|
| 239 |
|
---|
| 240 | // store particles
|
---|
| 241 | for (std::vector<const atom*>::const_iterator atomIt = AtomList.begin();
|
---|
| 242 | atomIt != AtomList.end(); ++atomIt)
|
---|
| 243 | saveLine(file, *atomIt, step);
|
---|
| 244 | }
|
---|
[23fd43] | 245 | }
|
---|
[acd638] | 246 |
|
---|
[5b0581] | 247 | struct usedFieldsWeakComparator
|
---|
| 248 | {
|
---|
| 249 | /** Special comparator regards "neighbors=4" and "neighbors=2" as equal
|
---|
| 250 | *
|
---|
| 251 | * \note This one is used for making usedFields unique, i.e. throwing out the "smaller"
|
---|
| 252 | * neighbors.
|
---|
| 253 | */
|
---|
| 254 | bool operator()(const std::string &a, const std::string &b) const
|
---|
| 255 | {
|
---|
| 256 | // only compare up to first equality sign
|
---|
| 257 | return (a.substr(0, a.find_first_of('=')) == b.substr(0, b.find_first_of('=')));
|
---|
| 258 | }
|
---|
| 259 | };
|
---|
| 260 |
|
---|
| 261 | struct usedFieldsSpecialOrderer
|
---|
| 262 | {
|
---|
| 263 | /** Special string comparator that regards "neighbors=4" < "neighbors=2" as true and
|
---|
| 264 | * the other way round as false.
|
---|
| 265 | *
|
---|
| 266 | * Here, we implement the operator "\a < \b" in a special way to allow the
|
---|
| 267 | * above.
|
---|
| 268 | *
|
---|
| 269 | * \note This one is used for sorting usedFields in preparation for making it unique.
|
---|
| 270 | */
|
---|
| 271 | bool operator()(const std::string &a, const std::string &b) const
|
---|
| 272 | {
|
---|
| 273 | // only compare up to first equality sign
|
---|
| 274 | size_t a_equality = a.find_first_of('=');
|
---|
| 275 | size_t b_equality = b.find_first_of('=');
|
---|
| 276 | // if key before equality is not equal, return whether it is smaller or not
|
---|
| 277 | if (a.substr(0, a_equality) != b.substr(0, b_equality)) {
|
---|
| 278 | return a.substr(0, a_equality) < b.substr(0, b_equality);
|
---|
| 279 | } else { // now we know that the key before equality is the same in either string
|
---|
| 280 | // if one of them has no equality, the one with equality must go before
|
---|
| 281 | if ((a_equality != std::string::npos) && (b_equality == std::string::npos))
|
---|
| 282 | return true;
|
---|
| 283 | if ((a_equality == std::string::npos) && (b_equality != std::string::npos))
|
---|
| 284 | return false;
|
---|
| 285 | // if both don't have equality (and the token before is equal), it is not "<" but "=="
|
---|
| 286 | if ((a_equality == std::string::npos) && (b_equality == std::string::npos))
|
---|
| 287 | return false;
|
---|
| 288 | // if now both have equality sign, the larger value after it, must come first
|
---|
| 289 | return a.substr(a_equality, std::string::npos) > b.substr(b_equality, std::string::npos);
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 | };
|
---|
| 293 |
|
---|
[23fd43] | 294 | /** Helper function to make \given fields unique while preserving the order of first appearance.
|
---|
| 295 | *
|
---|
| 296 | * As std::unique only removes element if equal to predecessor, a vector is only
|
---|
| 297 | * made unique if sorted beforehand. But sorting would destroy order of first
|
---|
| 298 | * appearance, hence we do the sorting on a temporary field and add the unique
|
---|
| 299 | * elements in the order as in \a fields.
|
---|
| 300 | *
|
---|
| 301 | * @param fields usedFields to make unique while preserving order of appearance
|
---|
| 302 | */
|
---|
[27cfde] | 303 | void FormatParser< tremolo >::makeUsedFieldsUnique(usedFields_t &fields) const
|
---|
[23fd43] | 304 | {
|
---|
| 305 | // std::unique only removes if predecessor is equal, not over whole range, hence do it manually
|
---|
[27cfde] | 306 | usedFields_t temp_fields(fields);
|
---|
[5b0581] | 307 | usedFieldsSpecialOrderer SpecialOrderer;
|
---|
| 308 | usedFieldsWeakComparator WeakComparator;
|
---|
| 309 | std::sort(temp_fields.begin(), temp_fields.end(), SpecialOrderer);
|
---|
[23fd43] | 310 | usedFields_t::iterator it =
|
---|
[5b0581] | 311 | std::unique(temp_fields.begin(), temp_fields.end(), WeakComparator);
|
---|
[23fd43] | 312 | temp_fields.erase(it, temp_fields.end());
|
---|
[27cfde] | 313 | usedFields_t usedfields(fields);
|
---|
| 314 | fields.clear();
|
---|
| 315 | fields.reserve(temp_fields.size());
|
---|
[23fd43] | 316 | // now go through each usedFields entry, check if in temp_fields and remove there on first occurence
|
---|
| 317 | for (usedFields_t::const_iterator iter = usedfields.begin();
|
---|
| 318 | iter != usedfields.end(); ++iter) {
|
---|
| 319 | usedFields_t::iterator uniqueiter =
|
---|
| 320 | std::find(temp_fields.begin(), temp_fields.end(), *iter);
|
---|
| 321 | if (uniqueiter != temp_fields.end()) {
|
---|
[27cfde] | 322 | fields.push_back(*iter);
|
---|
[23fd43] | 323 | // add only once to ATOMDATA
|
---|
| 324 | temp_fields.erase(uniqueiter);
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 | ASSERT( temp_fields.empty(),
|
---|
| 328 | "FormatParser< tremolo >::save() - still unique entries left in temp_fields after unique?");
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 |
|
---|
| 332 | /** Resets and distributes the indices continuously.
|
---|
| 333 | *
|
---|
| 334 | * \param atoms atoms to store
|
---|
| 335 | */
|
---|
[fac58f] | 336 | void FormatParser< tremolo >::distributeContinuousIds(
|
---|
| 337 | const std::vector<const atom *> &AtomList)
|
---|
[23fd43] | 338 | {
|
---|
[812155] | 339 | resetIdAssociations();
|
---|
| 340 | atomId_t lastid = 0;
|
---|
[fac58f] | 341 | for (std::vector<const atom*>::const_iterator atomIt = AtomList.begin();
|
---|
[23fd43] | 342 | atomIt != AtomList.end(); ++atomIt)
|
---|
[812155] | 343 | associateLocaltoGlobalId(++lastid, (*atomIt)->getId());
|
---|
[23fd43] | 344 | }
|
---|
[812155] | 345 |
|
---|
[23fd43] | 346 | /** Store Atomdata line to \a file.
|
---|
| 347 | *
|
---|
| 348 | * @param file output stream
|
---|
| 349 | */
|
---|
| 350 | void FormatParser< tremolo >::save_AtomDataLine(std::ostream* file) const
|
---|
| 351 | {
|
---|
[b8d4a3] | 352 | *file << "# ATOMDATA";
|
---|
[23fd43] | 353 | for (usedFields_t::const_iterator it=usedFields_save.begin();
|
---|
| 354 | it != usedFields_save.end(); ++it)
|
---|
[b8d4a3] | 355 | *file << "\t" << *it;
|
---|
[23fd43] | 356 | *file << std::endl;
|
---|
| 357 | }
|
---|
[ccb487] | 358 |
|
---|
[23fd43] | 359 | /** Store Box info to \a file
|
---|
| 360 | *
|
---|
| 361 | * @param file output stream
|
---|
| 362 | */
|
---|
| 363 | void FormatParser< tremolo >::save_BoxLine(std::ostream* file) const
|
---|
| 364 | {
|
---|
[ccb487] | 365 | *file << "# Box";
|
---|
| 366 | const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
|
---|
| 367 | for (size_t i=0; i<NDIM;++i)
|
---|
| 368 | for (size_t j=0; j<NDIM;++j)
|
---|
| 369 | *file << "\t" << M.at(i,j);
|
---|
| 370 | *file << std::endl;
|
---|
[b8d4a3] | 371 | }
|
---|
| 372 |
|
---|
[6bc86c] | 373 | /** Add default info, when new atom is added to World.
|
---|
| 374 | *
|
---|
| 375 | * @param id of atom
|
---|
| 376 | */
|
---|
[765f16] | 377 | void FormatParser< tremolo >::AtomInserted(atomId_t id)
|
---|
[6bc86c] | 378 | {
|
---|
[8bf9c6] | 379 | std::map<const atomId_t, TremoloAtomInfoContainer>::iterator iter = additionalAtomData.find(id);
|
---|
[6bc86c] | 380 | ASSERT(iter == additionalAtomData.end(),
|
---|
[765f16] | 381 | "FormatParser< tremolo >::AtomInserted() - additionalAtomData already present for newly added atom "
|
---|
[6bc86c] | 382 | +toString(id)+".");
|
---|
| 383 | // don't add entry, as this gives a default resSeq of 0 not the molecule id
|
---|
| 384 | // additionalAtomData.insert( std::make_pair(id, TremoloAtomInfoContainer()) );
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | /** Remove additional AtomData info, when atom has been removed from World.
|
---|
| 388 | *
|
---|
| 389 | * @param id of atom
|
---|
| 390 | */
|
---|
[765f16] | 391 | void FormatParser< tremolo >::AtomRemoved(atomId_t id)
|
---|
[6bc86c] | 392 | {
|
---|
[8bf9c6] | 393 | std::map<const atomId_t, TremoloAtomInfoContainer>::iterator iter = additionalAtomData.find(id);
|
---|
[6bc86c] | 394 | // as we do not insert AtomData on AtomInserted, we cannot be assured of its presence
|
---|
| 395 | // ASSERT(iter != additionalAtomData.end(),
|
---|
[765f16] | 396 | // "FormatParser< tremolo >::AtomRemoved() - additionalAtomData is not present for atom "
|
---|
[6bc86c] | 397 | // +toString(id)+" to remove.");
|
---|
| 398 | if (iter != additionalAtomData.end())
|
---|
| 399 | additionalAtomData.erase(iter);
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[4795cd] | 402 | template <class T>
|
---|
| 403 | T NoOp(const atom * const)
|
---|
| 404 | {
|
---|
| 405 | return T();
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | template <class T>
|
---|
| 409 | void writeEntryFromAdditionalAtomData_ifpresent(
|
---|
| 410 | std::map<const atomId_t, TremoloAtomInfoContainer> &_additionalAtomData,
|
---|
| 411 | std::map<TremoloKey::atomDataKey, std::string> &_knownKeyNames,
|
---|
| 412 | std::ostream* _file,
|
---|
| 413 | const atom * const _currentAtom,
|
---|
| 414 | const TremoloKey::atomDataKey _currentField,
|
---|
| 415 | const typename boost::function<T (const atom * const)> _getter)
|
---|
| 416 | {
|
---|
| 417 | if (_additionalAtomData.count(_currentAtom->getId())) {
|
---|
| 418 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << ": "
|
---|
| 419 | << _additionalAtomData[_currentAtom->getId()].get(_currentField));
|
---|
| 420 | *_file << _additionalAtomData[_currentAtom->getId()].get(_currentField);
|
---|
| 421 | } else if (_additionalAtomData.count(_currentAtom->GetTrueFather()->getId())) {
|
---|
| 422 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << " stuff from father: "
|
---|
| 423 | << _additionalAtomData[_currentAtom->GetTrueFather()->getId()].get(_currentField));
|
---|
| 424 | *_file << _additionalAtomData[_currentAtom->GetTrueFather()->getId()].get(_currentField);
|
---|
| 425 | } else {
|
---|
| 426 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << " its default value: "
|
---|
| 427 | << _getter(_currentAtom));
|
---|
| 428 | *_file << _getter(_currentAtom);
|
---|
| 429 | }
|
---|
| 430 | *_file << "\t";
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | template <class T>
|
---|
| 434 | void writeAdditionalAtomDataEntry(
|
---|
| 435 | std::map<const atomId_t, TremoloAtomInfoContainer> &_additionalAtomData,
|
---|
| 436 | std::map<TremoloKey::atomDataKey, std::string> &_knownKeyNames,
|
---|
| 437 | TremoloAtomInfoContainer &_defaultAdditionalData,
|
---|
| 438 | std::ostream* _file,
|
---|
| 439 | const atom * const _currentAtom,
|
---|
| 440 | const TremoloKey::atomDataKey _currentField)
|
---|
| 441 | {
|
---|
| 442 | if (_additionalAtomData.count(_currentAtom->getId())) {
|
---|
| 443 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << ": "
|
---|
| 444 | << _additionalAtomData[_currentAtom->getId()].get(_currentField));
|
---|
| 445 | *_file << _additionalAtomData[_currentAtom->getId()].get(_currentField);
|
---|
| 446 | } else if (_additionalAtomData.count(_currentAtom->GetTrueFather()->getId())) {
|
---|
| 447 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << " stuff from father: "
|
---|
| 448 | << _additionalAtomData[_currentAtom->GetTrueFather()->getId()].get(_currentField));
|
---|
| 449 | *_file << _additionalAtomData[_currentAtom->GetTrueFather()->getId()].get(_currentField);
|
---|
| 450 | } else {
|
---|
| 451 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << " its default value: "
|
---|
| 452 | << _defaultAdditionalData.get(_currentField));
|
---|
| 453 | *_file << _defaultAdditionalData.get(_currentField);
|
---|
| 454 | }
|
---|
| 455 | *_file << "\t";
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | template <class T>
|
---|
| 459 | void writeEntryFromAdditionalAtomData_ifnotempty(
|
---|
| 460 | std::map<const atomId_t, TremoloAtomInfoContainer> &_additionalAtomData,
|
---|
| 461 | std::map<TremoloKey::atomDataKey, std::string> &_knownKeyNames,
|
---|
| 462 | TremoloAtomInfoContainer &_defaultAdditionalData,
|
---|
| 463 | std::ostream* _file,
|
---|
| 464 | const atom * const _currentAtom,
|
---|
| 465 | const TremoloKey::atomDataKey _currentField,
|
---|
| 466 | const typename boost::function<T (const atom * const)> _getter)
|
---|
| 467 | {
|
---|
| 468 | if (_additionalAtomData.count(_currentAtom->getId())) {
|
---|
| 469 | if (_additionalAtomData[_currentAtom->getId()].get(_currentField) != "-") {
|
---|
| 470 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << ": "
|
---|
| 471 | << _additionalAtomData[_currentAtom->getId()].get(_currentField));
|
---|
| 472 | *_file << _additionalAtomData[_currentAtom->getId()].get(_currentField);
|
---|
| 473 | } else {
|
---|
| 474 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << " default value: "
|
---|
| 475 | << _getter(_currentAtom));
|
---|
| 476 | *_file << _getter(_currentAtom);
|
---|
| 477 | }
|
---|
| 478 | } else if (_additionalAtomData.count(_currentAtom->GetTrueFather()->getId())) {
|
---|
| 479 | if (_additionalAtomData[_currentAtom->GetTrueFather()->getId()].get(_currentField) != "-") {
|
---|
| 480 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << " stuff from father: "
|
---|
| 481 | << _additionalAtomData[_currentAtom->GetTrueFather()->getId()].get(_currentField));
|
---|
| 482 | *_file << _additionalAtomData[_currentAtom->GetTrueFather()->getId()].get(_currentField);
|
---|
| 483 | } else {
|
---|
| 484 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << " default value from father: "
|
---|
| 485 | << _getter(_currentAtom->GetTrueFather()));
|
---|
| 486 | *_file << _getter(_currentAtom->GetTrueFather());
|
---|
| 487 | }
|
---|
| 488 | } else {
|
---|
| 489 | LOG(3, "Writing for type " << _knownKeyNames[_currentField] << " its default value: "
|
---|
| 490 | << _getter(_currentAtom));
|
---|
| 491 | *_file << _getter(_currentAtom);
|
---|
| 492 | }
|
---|
| 493 | *_file << "\t";
|
---|
| 494 | }
|
---|
| 495 |
|
---|
[b8d4a3] | 496 | /**
|
---|
| 497 | * Writes one line of tremolo-formatted data to the provided stream.
|
---|
| 498 | *
|
---|
| 499 | * \param stream where to write the line to
|
---|
| 500 | * \param reference to the atom of which information should be written
|
---|
| 501 | */
|
---|
[fac58f] | 502 | void FormatParser< tremolo >::saveLine(
|
---|
| 503 | std::ostream* file,
|
---|
[67e885] | 504 | const atom * const currentAtom,
|
---|
| 505 | const size_t _timestep)
|
---|
[23fd43] | 506 | {
|
---|
[b8d4a3] | 507 | TremoloKey::atomDataKey currentField;
|
---|
| 508 |
|
---|
[67e885] | 509 | LOG(4, "INFO: Saving atom " << *currentAtom << ", its father id is "
|
---|
| 510 | << currentAtom->GetTrueFather()->getId() << " at time step " << _timestep);
|
---|
[4d4d33] | 511 |
|
---|
[23fd43] | 512 | for (usedFields_t::iterator it = usedFields_save.begin(); it != usedFields_save.end(); it++) {
|
---|
[b8d4a3] | 513 | currentField = knownKeys[it->substr(0, it->find("="))];
|
---|
| 514 | switch (currentField) {
|
---|
| 515 | case TremoloKey::x :
|
---|
| 516 | // for the moment, assume there are always three dimensions
|
---|
[67e885] | 517 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getPositionAtStep(_timestep));
|
---|
| 518 | *file << currentAtom->atStep(0, _timestep) << "\t";
|
---|
| 519 | *file << currentAtom->atStep(1, _timestep) << "\t";
|
---|
| 520 | *file << currentAtom->atStep(2, _timestep) << "\t";
|
---|
[b8d4a3] | 521 | break;
|
---|
| 522 | case TremoloKey::u :
|
---|
| 523 | // for the moment, assume there are always three dimensions
|
---|
[67e885] | 524 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getAtomicVelocityAtStep(_timestep));
|
---|
| 525 | *file << currentAtom->getAtomicVelocityAtStep(_timestep)[0] << "\t";
|
---|
| 526 | *file << currentAtom->getAtomicVelocityAtStep(_timestep)[1] << "\t";
|
---|
| 527 | *file << currentAtom->getAtomicVelocityAtStep(_timestep)[2] << "\t";
|
---|
[b8d4a3] | 528 | break;
|
---|
[e6e4a0] | 529 | case TremoloKey::F :
|
---|
| 530 | // for the moment, assume there are always three dimensions
|
---|
[67e885] | 531 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getAtomicForceAtStep(_timestep));
|
---|
| 532 | *file << currentAtom->getAtomicForceAtStep(_timestep)[0] << "\t";
|
---|
| 533 | *file << currentAtom->getAtomicForceAtStep(_timestep)[1] << "\t";
|
---|
| 534 | *file << currentAtom->getAtomicForceAtStep(_timestep)[2] << "\t";
|
---|
[e6e4a0] | 535 | break;
|
---|
[305e7e] | 536 | case TremoloKey::type :
|
---|
[4795cd] | 537 | writeEntryFromAdditionalAtomData_ifnotempty<std::string>(
|
---|
| 538 | additionalAtomData,
|
---|
| 539 | knownKeyNames,
|
---|
| 540 | defaultAdditionalData,
|
---|
| 541 | file,
|
---|
| 542 | currentAtom,
|
---|
| 543 | currentField,
|
---|
| 544 | boost::bind(&element::getSymbol, boost::bind(&AtomInfo::getType, _1)));
|
---|
[b8d4a3] | 545 | break;
|
---|
| 546 | case TremoloKey::Id :
|
---|
[47d041] | 547 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << currentAtom->getId()+1);
|
---|
[812155] | 548 | *file << getLocalId(currentAtom->getId()) << "\t";
|
---|
[b8d4a3] | 549 | break;
|
---|
| 550 | case TremoloKey::neighbors :
|
---|
[47d041] | 551 | LOG(3, "Writing type " << knownKeyNames[currentField]);
|
---|
[b8d4a3] | 552 | writeNeighbors(file, atoi(it->substr(it->find("=") + 1, 1).c_str()), currentAtom);
|
---|
| 553 | break;
|
---|
[05e2ed] | 554 | case TremoloKey::imprData :
|
---|
| 555 | case TremoloKey::torsion :
|
---|
| 556 | LOG(3, "Writing type " << knownKeyNames[currentField]);
|
---|
| 557 | *file << adaptIdDependentDataString(
|
---|
| 558 | additionalAtomData[currentAtom->getId()].get(currentField),
|
---|
| 559 | idlocalizer)
|
---|
| 560 | << "\t";
|
---|
| 561 | break;
|
---|
[74a444] | 562 | case TremoloKey::resSeq :
|
---|
[4d4d33] | 563 | if (additionalAtomData.count(currentAtom->getId())) {
|
---|
[47d041] | 564 | LOG(3, "Writing for type " << knownKeyNames[currentField] << ": " << additionalAtomData[currentAtom->getId()].get(currentField));
|
---|
[74a444] | 565 | *file << additionalAtomData[currentAtom->getId()].get(currentField);
|
---|
| 566 | } else if (currentAtom->getMolecule() != NULL) {
|
---|
[47d041] | 567 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " its own id: " << currentAtom->getMolecule()->getId()+1);
|
---|
[67ab71] | 568 | *file << setw(4) << currentAtom->getMolecule()->getId();
|
---|
[74a444] | 569 | } else {
|
---|
[47d041] | 570 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " default value: " << defaultAdditionalData.get(currentField));
|
---|
[74a444] | 571 | *file << defaultAdditionalData.get(currentField);
|
---|
| 572 | }
|
---|
| 573 | *file << "\t";
|
---|
[4d4d33] | 574 | break;
|
---|
[2034f3] | 575 | case TremoloKey::charge :
|
---|
| 576 | if (currentAtom->getCharge() == 0.) {
|
---|
[4795cd] | 577 | writeEntryFromAdditionalAtomData_ifpresent<double>(
|
---|
| 578 | additionalAtomData,
|
---|
| 579 | knownKeyNames,
|
---|
| 580 | file,
|
---|
| 581 | currentAtom,
|
---|
| 582 | currentField,
|
---|
| 583 | boost::bind(&AtomInfo::getCharge, _1));
|
---|
[2034f3] | 584 | } else {
|
---|
| 585 | LOG(3, "Writing for type " << knownKeyNames[currentField] << " AtomInfo::charge : " << currentAtom->getCharge());
|
---|
[4795cd] | 586 | *file << currentAtom->getCharge() << "\t";
|
---|
[2034f3] | 587 | }
|
---|
| 588 | break;
|
---|
[b8d4a3] | 589 | default :
|
---|
[4795cd] | 590 | writeAdditionalAtomDataEntry<std::string>(
|
---|
| 591 | additionalAtomData,
|
---|
| 592 | knownKeyNames,
|
---|
| 593 | defaultAdditionalData,
|
---|
| 594 | file,
|
---|
| 595 | currentAtom,
|
---|
| 596 | currentField);
|
---|
[b8d4a3] | 597 | break;
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 |
|
---|
[23fd43] | 601 | *file << std::endl;
|
---|
[b8d4a3] | 602 | }
|
---|
| 603 |
|
---|
| 604 | /**
|
---|
| 605 | * Writes the neighbor information of one atom to the provided stream.
|
---|
| 606 | *
|
---|
[9d83b6] | 607 | * Note that ListOfBonds of WorldTime::CurrentTime is used.
|
---|
| 608 | *
|
---|
[b8d4a3] | 609 | * \param stream where to write neighbor information to
|
---|
| 610 | * \param number of neighbors
|
---|
| 611 | * \param reference to the atom of which to take the neighbor information
|
---|
| 612 | */
|
---|
[fac58f] | 613 | void FormatParser< tremolo >::writeNeighbors(
|
---|
| 614 | std::ostream* file,
|
---|
| 615 | const int numberOfNeighbors,
|
---|
| 616 | const atom * const currentAtom) {
|
---|
[9d83b6] | 617 | const BondList& ListOfBonds = currentAtom->getListOfBonds();
|
---|
[ca2cfa] | 618 | // sort bonded indices
|
---|
| 619 | typedef std::set<atomId_t> sortedIndices;
|
---|
| 620 | sortedIndices sortedBonds;
|
---|
| 621 | for (BondList::const_iterator iter = ListOfBonds.begin();
|
---|
| 622 | iter != ListOfBonds.end(); ++iter)
|
---|
[812155] | 623 | sortedBonds.insert(getLocalId((*iter)->GetOtherAtom(currentAtom)->getId()));
|
---|
[ca2cfa] | 624 | // print indices
|
---|
| 625 | sortedIndices::const_iterator currentBond = sortedBonds.begin();
|
---|
[b8d4a3] | 626 | for (int i = 0; i < numberOfNeighbors; i++) {
|
---|
[812155] | 627 | *file << (currentBond != sortedBonds.end() ? (*currentBond) : 0) << "\t";
|
---|
[ca2cfa] | 628 | if (currentBond != sortedBonds.end())
|
---|
[0bbfa1] | 629 | ++currentBond;
|
---|
[b8d4a3] | 630 | }
|
---|
[9131f3] | 631 | }
|
---|
| 632 |
|
---|
| 633 | /**
|
---|
[23fd43] | 634 | * Stores keys from the ATOMDATA line in \a fields.
|
---|
[9131f3] | 635 | *
|
---|
| 636 | * \param line to parse the keys from
|
---|
[23fd43] | 637 | * \param offset with which offset the keys begin within the line
|
---|
| 638 | * \param fields which usedFields to use
|
---|
[9131f3] | 639 | */
|
---|
[23fd43] | 640 | void FormatParser< tremolo >::parseAtomDataKeysLine(
|
---|
| 641 | const std::string &line,
|
---|
| 642 | const int offset,
|
---|
| 643 | usedFields_t &fields) {
|
---|
[8bf9c6] | 644 | std::string keyword;
|
---|
| 645 | std::stringstream lineStream;
|
---|
[9131f3] | 646 |
|
---|
| 647 | lineStream << line.substr(offset);
|
---|
[a86cda] | 648 | lineStream >> ws;
|
---|
[9131f3] | 649 | while (lineStream.good()) {
|
---|
| 650 | lineStream >> keyword;
|
---|
[b8d4a3] | 651 | if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey) {
|
---|
[ecb799] | 652 | // TODO: throw exception about unknown key
|
---|
[5a667d] | 653 | cout << "Unknown key: " << keyword.substr(0, keyword.find("=")) << " is not part of the tremolo format specification." << endl;
|
---|
| 654 | throw IllegalParserKeyException();
|
---|
[4415da] | 655 | break;
|
---|
| 656 | }
|
---|
[23fd43] | 657 | fields.push_back(keyword);
|
---|
[a86cda] | 658 | lineStream >> ws;
|
---|
[9131f3] | 659 | }
|
---|
[a86cda] | 660 | LOG(2, "INFO: " << fields);
|
---|
[9131f3] | 661 | }
|
---|
| 662 |
|
---|
[5a667d] | 663 | /**
|
---|
| 664 | * Tests whether the keys from the ATOMDATA line can be read correctly.
|
---|
| 665 | *
|
---|
| 666 | * \param line to parse the keys from
|
---|
| 667 | */
|
---|
| 668 | bool FormatParser< tremolo >::testParseAtomDataKeysLine(
|
---|
| 669 | const std::string &line) {
|
---|
| 670 | std::string keyword;
|
---|
| 671 | std::stringstream lineStream;
|
---|
| 672 |
|
---|
| 673 | // check string after ATOMDATA
|
---|
| 674 | const std::string AtomData("ATOMDATA");
|
---|
| 675 | const size_t AtomDataOffset = line.find(AtomData, 0);
|
---|
| 676 | if (AtomDataOffset == std::string::npos)
|
---|
| 677 | lineStream << line;
|
---|
| 678 | else
|
---|
| 679 | lineStream << line.substr(AtomDataOffset + AtomData.length());
|
---|
| 680 | while (lineStream.good()) {
|
---|
| 681 | lineStream >> keyword;
|
---|
| 682 | //LOG(2, "DEBUG: Checking key " << keyword.substr(0, keyword.find("=")) << ".");
|
---|
| 683 | if (knownKeys[keyword.substr(0, keyword.find("="))] == TremoloKey::noKey)
|
---|
| 684 | return false;
|
---|
| 685 | }
|
---|
| 686 | //LOG(1, "INFO: " << fields);
|
---|
| 687 | return true;
|
---|
| 688 | }
|
---|
| 689 |
|
---|
[ca331c] | 690 | std::string FormatParser< tremolo >::getAtomData() const
|
---|
| 691 | {
|
---|
| 692 | std::stringstream output;
|
---|
| 693 | std::for_each(usedFields_save.begin(), usedFields_save.end(),
|
---|
| 694 | output << boost::lambda::_1 << " ");
|
---|
| 695 | const std::string returnstring(output.str());
|
---|
| 696 | return returnstring.substr(0, returnstring.find_last_of(" "));
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | /** Appends the properties per atom to print to .data file by parsing line from
|
---|
| 700 | * \a atomdata_string.
|
---|
| 701 | *
|
---|
| 702 | * We just call \sa FormatParser< tremolo >::parseAtomDataKeysLine().
|
---|
| 703 | *
|
---|
| 704 | * @param atomdata_string line to parse with space-separated values
|
---|
| 705 | */
|
---|
| 706 | void FormatParser< tremolo >::setAtomData(const std::string &atomdata_string)
|
---|
| 707 | {
|
---|
| 708 | parseAtomDataKeysLine(atomdata_string, 0, usedFields_save);
|
---|
| 709 | }
|
---|
| 710 |
|
---|
[81c980b] | 711 | /** Sets the properties per atom to print to .data file by parsing line from
|
---|
| 712 | * \a atomdata_string.
|
---|
| 713 | *
|
---|
[23fd43] | 714 | * We just call \sa FormatParser< tremolo >::parseAtomDataKeysLine(), however
|
---|
| 715 | * we clear FormatParser< tremolo >::usedFields_save.
|
---|
[81c980b] | 716 | *
|
---|
| 717 | * @param atomdata_string line to parse with space-separated values
|
---|
| 718 | */
|
---|
[ca331c] | 719 | void FormatParser< tremolo >::resetAtomData(const std::string &atomdata_string)
|
---|
[81c980b] | 720 | {
|
---|
[23fd43] | 721 | usedFields_save.clear();
|
---|
| 722 | parseAtomDataKeysLine(atomdata_string, 0, usedFields_save);
|
---|
[81c980b] | 723 | }
|
---|
| 724 |
|
---|
| 725 |
|
---|
[9131f3] | 726 | /**
|
---|
| 727 | * Reads one data line of a tremolo file and interprets it according to the keys
|
---|
| 728 | * obtained from the ATOMDATA line.
|
---|
| 729 | *
|
---|
| 730 | * \param line to parse as an atom
|
---|
[dc1d9e] | 731 | * \param *newmol molecule to add atom to
|
---|
[efc80e] | 732 | * \param _timestep time step to parse to
|
---|
| 733 | * \param _addedatom ref to added atom
|
---|
[9131f3] | 734 | */
|
---|
[efc80e] | 735 | void FormatParser< tremolo >::readAtomDataLine(
|
---|
| 736 | const std::string &line,
|
---|
| 737 | molecule *newmol,
|
---|
| 738 | const size_t _timestep,
|
---|
| 739 | atom *&_addedatoms)
|
---|
| 740 | {
|
---|
[8bf9c6] | 741 | std::stringstream lineStream;
|
---|
[efc80e] | 742 | atom* newAtom = NULL;
|
---|
| 743 | atomId_t atomid = -1;
|
---|
| 744 | if (_timestep == 0) {
|
---|
| 745 | newAtom = World::getInstance().createAtom();
|
---|
| 746 | _addedatoms = newAtom;
|
---|
| 747 | atomid = newAtom->getId();
|
---|
| 748 | additionalAtomData[atomid] = TremoloAtomInfoContainer(); // fill with default values
|
---|
| 749 | } else {
|
---|
| 750 | newAtom = _addedatoms;
|
---|
| 751 | atomid = newAtom->getId();
|
---|
| 752 | }
|
---|
[89a31d] | 753 | TremoloAtomInfoContainer *atomInfo = &additionalAtomData[atomid];
|
---|
[b8d4a3] | 754 | TremoloKey::atomDataKey currentField;
|
---|
[72d108] | 755 | ConvertTo<double> toDouble;
|
---|
| 756 | ConvertTo<int> toInt;
|
---|
[056e70] | 757 | Vector tempVector;
|
---|
[740f9f] | 758 | const ParticleRegistry& registry = ParticleRegistry::getConstInstance();
|
---|
| 759 | const periodentafel& periode = *World::getInstance().getPeriode();
|
---|
[72d108] | 760 |
|
---|
| 761 | // setup tokenizer, splitting up white-spaced entries
|
---|
| 762 | typedef boost::tokenizer<boost::char_separator<char> >
|
---|
| 763 | tokenizer;
|
---|
| 764 | boost::char_separator<char> whitespacesep(" \t");
|
---|
| 765 | tokenizer tokens(line, whitespacesep);
|
---|
| 766 | ASSERT(tokens.begin() != tokens.end(),
|
---|
[765f16] | 767 | "FormatParser< tremolo >::readAtomDataLine - empty string, need at least ' '!");
|
---|
[fc41df] | 768 | tokenizer::const_iterator tok_iter = tokens.begin();
|
---|
[72d108] | 769 | // then associate each token to each file
|
---|
[fc41df] | 770 | for (usedFields_t::const_iterator it = usedFields_load.begin(); it != usedFields_load.end(); it++) {
|
---|
[72d108] | 771 | const std::string keyName = it->substr(0, it->find("="));
|
---|
| 772 | currentField = knownKeys[keyName];
|
---|
[fc41df] | 773 | ASSERT(tok_iter != tokens.end(),
|
---|
| 774 | "FormatParser< tremolo >::readAtomDataLine - too few entries in line '"+line+"'!");
|
---|
| 775 | const std::string &word = *tok_iter;
|
---|
[47d041] | 776 | LOG(4, "INFO: Parsing key " << keyName << " with remaining data " << word);
|
---|
[4415da] | 777 | switch (currentField) {
|
---|
[b8d4a3] | 778 | case TremoloKey::x :
|
---|
[4415da] | 779 | // for the moment, assume there are always three dimensions
|
---|
[d74077] | 780 | for (int i=0;i<NDIM;i++) {
|
---|
[765f16] | 781 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for x["+toString(i)+"]!");
|
---|
[efc80e] | 782 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << word << " on step " << _timestep);
|
---|
| 783 | newAtom->setAtStep(i, _timestep, toDouble(word));
|
---|
[72d108] | 784 | tok_iter++;
|
---|
[d74077] | 785 | }
|
---|
[4415da] | 786 | break;
|
---|
[b8d4a3] | 787 | case TremoloKey::u :
|
---|
[4415da] | 788 | // for the moment, assume there are always three dimensions
|
---|
[72d108] | 789 | for (int i=0;i<NDIM;i++) {
|
---|
[765f16] | 790 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for u["+toString(i)+"]!");
|
---|
[efc80e] | 791 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << word << " on step " << _timestep);
|
---|
[fc41df] | 792 | tempVector[i] = toDouble(word);
|
---|
[72d108] | 793 | tok_iter++;
|
---|
| 794 | }
|
---|
[efc80e] | 795 | newAtom->setAtomicVelocityAtStep(_timestep, tempVector);
|
---|
[4415da] | 796 | break;
|
---|
[e6e4a0] | 797 | case TremoloKey::F :
|
---|
| 798 | // for the moment, assume there are always three dimensions
|
---|
| 799 | for (int i=0;i<NDIM;i++) {
|
---|
| 800 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for F["+toString(i)+"]!");
|
---|
[efc80e] | 801 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << word << " on step " << _timestep);
|
---|
[fc41df] | 802 | tempVector[i] = toDouble(word);
|
---|
[e6e4a0] | 803 | tok_iter++;
|
---|
| 804 | }
|
---|
[efc80e] | 805 | newAtom->setAtomicForceAtStep(_timestep, tempVector);
|
---|
[e6e4a0] | 806 | break;
|
---|
[305e7e] | 807 | case TremoloKey::type :
|
---|
[4d4d33] | 808 | {
|
---|
[765f16] | 809 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
|
---|
[efc80e] | 810 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << word << " on step " << _timestep);
|
---|
[740f9f] | 811 | std::string elementname;
|
---|
| 812 | const element * elem = NULL;
|
---|
| 813 | if (!registry.isPresentByName(word)) {
|
---|
| 814 | std::string lowercase_word(word);
|
---|
| 815 | std::transform(word.begin()+1, word.end(), lowercase_word.begin()+1, ::tolower);
|
---|
| 816 | elem = periode.FindElement(lowercase_word);
|
---|
| 817 | if (elem == NULL) {
|
---|
| 818 | // clean up
|
---|
| 819 | World::getInstance().destroyAtom(newAtom);
|
---|
| 820 | // give an error
|
---|
| 821 | ELOG(0, "TremoloParser: tokens " << word << "/" << lowercase_word
|
---|
| 822 | << " is unknown to neither ParticleRegistry nor Periodentafel.");
|
---|
| 823 | return;
|
---|
| 824 | } else {
|
---|
| 825 | elementname = elem->getSymbol();
|
---|
| 826 | }
|
---|
| 827 | } else {
|
---|
| 828 | const Particle * const p = registry.getByName(word);
|
---|
| 829 | elementname = p->getElement();
|
---|
| 830 | elem = periode.FindElement(elementname);
|
---|
[a275b3] | 831 | }
|
---|
[4d4d33] | 832 | // put type name into container for later use
|
---|
[fc41df] | 833 | atomInfo->set(currentField, word);
|
---|
[740f9f] | 834 | LOG(4, "INFO: Parsing element " << (word) << " as " << elementname << " according to KnownTypes.");
|
---|
[72d108] | 835 | tok_iter++;
|
---|
[740f9f] | 836 | newAtom->setType(elem);
|
---|
[b8d4a3] | 837 | ASSERT(newAtom->getType(), "Type was not set for this atom");
|
---|
[4415da] | 838 | break;
|
---|
[4d4d33] | 839 | }
|
---|
[b8d4a3] | 840 | case TremoloKey::Id :
|
---|
[765f16] | 841 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
|
---|
[efc80e] | 842 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << word << " on step " << _timestep);
|
---|
| 843 | if (_timestep == 0) {
|
---|
| 844 | associateLocaltoGlobalId(toInt(word), atomid);
|
---|
| 845 | } else {
|
---|
| 846 | // check association is the same
|
---|
| 847 | ASSERT( (atomId_t)getGlobalId(toInt(word)) == atomid,
|
---|
| 848 | "FormatParser< tremolo >::readAtomDataLine() - differing global id "+toString(atomid)
|
---|
| 849 | +" on timestep "+toString(_timestep));
|
---|
| 850 | ASSERT( getLocalId(atomid) == toInt(word),
|
---|
| 851 | "FormatParser< tremolo >::readAtomDataLine() - differing local id "+toString(toInt(word))
|
---|
| 852 | +" on timestep "+toString(_timestep));
|
---|
| 853 | }
|
---|
[72d108] | 854 | tok_iter++;
|
---|
[4415da] | 855 | break;
|
---|
[b8d4a3] | 856 | case TremoloKey::neighbors :
|
---|
[72d108] | 857 | for (int i=0;i<atoi(it->substr(it->find("=") + 1, 1).c_str());i++) {
|
---|
[efc80e] | 858 | ASSERT(tok_iter != tokens.end(),
|
---|
| 859 | "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
|
---|
| 860 | if (_timestep == 0)
|
---|
| 861 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << word << " on step " << _timestep);
|
---|
[fc41df] | 862 | lineStream << word << "\t";
|
---|
[72d108] | 863 | tok_iter++;
|
---|
| 864 | }
|
---|
[efc80e] | 865 | if (_timestep == 0) {
|
---|
| 866 | readNeighbors(&lineStream,
|
---|
| 867 | atoi(it->substr(it->find("=") + 1, 1).c_str()), atomid);
|
---|
| 868 | }
|
---|
[9131f3] | 869 | break;
|
---|
[2034f3] | 870 | case TremoloKey::charge :
|
---|
| 871 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
|
---|
[efc80e] | 872 | if (_timestep == 0) {
|
---|
| 873 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << word << " on step " << _timestep);
|
---|
| 874 | atomInfo->set(currentField, word);
|
---|
| 875 | newAtom->setCharge(boost::lexical_cast<double>(word));
|
---|
| 876 | }
|
---|
[2034f3] | 877 | tok_iter++;
|
---|
| 878 | break;
|
---|
[9131f3] | 879 | default :
|
---|
[765f16] | 880 | ASSERT(tok_iter != tokens.end(), "FormatParser< tremolo >::readAtomDataLine() - no value for "+keyName+"!");
|
---|
[efc80e] | 881 | if (_timestep == 0) {
|
---|
| 882 | LOG(4, "INFO: Parsing key " << keyName << " with next token " << word << " on step " << _timestep);
|
---|
| 883 | atomInfo->set(currentField, word);
|
---|
| 884 | }
|
---|
[72d108] | 885 | tok_iter++;
|
---|
[9131f3] | 886 | break;
|
---|
| 887 | }
|
---|
| 888 | }
|
---|
[89a31d] | 889 | LOG(3, "INFO: Parsed atom " << atomid << ".");
|
---|
| 890 | if (newmol != NULL)
|
---|
[dc1d9e] | 891 | newmol->AddAtom(newAtom);
|
---|
[6bc51d] | 892 | }
|
---|
[9131f3] | 893 |
|
---|
[99db9b] | 894 | bool FormatParser< tremolo >::saveAtomsInExttypes(
|
---|
| 895 | std::ostream &output,
|
---|
| 896 | const std::vector<const atom*> &atoms,
|
---|
| 897 | const int id) const
|
---|
[531f27] | 898 | {
|
---|
| 899 | bool status = true;
|
---|
| 900 | // parse the file
|
---|
[99db9b] | 901 | for (std::vector<const atom *>::const_iterator iter = atoms.begin();
|
---|
[531f27] | 902 | iter != atoms.end(); ++iter) {
|
---|
| 903 | const int atomicid = getLocalId((*iter)->getId());
|
---|
| 904 | if (atomicid == -1)
|
---|
| 905 | status = false;
|
---|
| 906 | output << atomicid << "\t" << id << std::endl;
|
---|
| 907 | }
|
---|
| 908 |
|
---|
| 909 | return status;
|
---|
| 910 | }
|
---|
| 911 |
|
---|
[b8d4a3] | 912 | /**
|
---|
| 913 | * Reads neighbor information for one atom from the input.
|
---|
| 914 | *
|
---|
[0bbfa1] | 915 | * \param line stream where to read the information from
|
---|
| 916 | * \param numberOfNeighbors number of neighbors to read
|
---|
| 917 | * \param atomid world id of the atom the information belongs to
|
---|
[b8d4a3] | 918 | */
|
---|
[fac58f] | 919 | void FormatParser< tremolo >::readNeighbors(
|
---|
| 920 | std::stringstream* line,
|
---|
| 921 | const int numberOfNeighbors,
|
---|
| 922 | const int atomId) {
|
---|
[b8d4a3] | 923 | int neighborId = 0;
|
---|
| 924 | for (int i = 0; i < numberOfNeighbors; i++) {
|
---|
| 925 | *line >> neighborId;
|
---|
| 926 | // 0 is used to fill empty neighbor positions in the tremolo file.
|
---|
| 927 | if (neighborId > 0) {
|
---|
[47d041] | 928 | LOG(4, "INFO: Atom with global id " << atomId
|
---|
| 929 | << " has neighbour with serial " << neighborId);
|
---|
[b8d4a3] | 930 | additionalAtomData[atomId].neighbors.push_back(neighborId);
|
---|
| 931 | }
|
---|
| 932 | }
|
---|
| 933 | }
|
---|
[9131f3] | 934 |
|
---|
| 935 | /**
|
---|
[23fd43] | 936 | * Checks whether the provided name is within \a fields.
|
---|
[b8d4a3] | 937 | *
|
---|
[23fd43] | 938 | * \param fields which usedFields to use
|
---|
| 939 | * \param fieldName name to check
|
---|
[b8d4a3] | 940 | * \return true if the field name is used
|
---|
[9131f3] | 941 | */
|
---|
[fac58f] | 942 | bool FormatParser< tremolo >::isUsedField(
|
---|
| 943 | const usedFields_t &fields,
|
---|
| 944 | const std::string &fieldName) const
|
---|
[23fd43] | 945 | {
|
---|
[b8d4a3] | 946 | bool fieldNameExists = false;
|
---|
[23fd43] | 947 | for (usedFields_t::const_iterator usedField = fields.begin();
|
---|
| 948 | usedField != fields.end(); usedField++) {
|
---|
[b8d4a3] | 949 | if (usedField->substr(0, usedField->find("=")) == fieldName)
|
---|
| 950 | fieldNameExists = true;
|
---|
| 951 | }
|
---|
[9131f3] | 952 |
|
---|
[b8d4a3] | 953 | return fieldNameExists;
|
---|
| 954 | }
|
---|
| 955 |
|
---|
| 956 |
|
---|
| 957 | /**
|
---|
| 958 | * Adds the collected neighbor information to the atoms in the world. The atoms
|
---|
| 959 | * are found by their current ID and mapped to the corresponding atoms with the
|
---|
| 960 | * Id found in the parsed file.
|
---|
[9f8b01] | 961 | *
|
---|
| 962 | * @param atoms vector with all newly added (global) atomic ids
|
---|
[b8d4a3] | 963 | */
|
---|
[fac58f] | 964 | void FormatParser< tremolo >::processNeighborInformation(
|
---|
| 965 | const std::vector<atomId_t> &atoms) {
|
---|
[23fd43] | 966 | if (!isUsedField(usedFields_load, "neighbors")) {
|
---|
[b8d4a3] | 967 | return;
|
---|
| 968 | }
|
---|
| 969 |
|
---|
[9f8b01] | 970 | for (std::vector<atomId_t>::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 971 | ASSERT(additionalAtomData.count(*iter) != 0,
|
---|
| 972 | "FormatParser< tremolo >::processNeighborInformation() - global id "
|
---|
| 973 | +toString(*iter)+" unknown in additionalAtomData.");
|
---|
| 974 | TremoloAtomInfoContainer ¤tInfo = additionalAtomData[*iter];
|
---|
| 975 | ASSERT (!currentInfo.neighbors_processed,
|
---|
| 976 | "FormatParser< tremolo >::processNeighborInformation() - neighbors of new atom "
|
---|
| 977 | +toString(*iter)+" are already processed.");
|
---|
| 978 | for(std::vector<int>::const_iterator neighbor = currentInfo.neighbors.begin();
|
---|
| 979 | neighbor != currentInfo.neighbors.end(); neighbor++
|
---|
| 980 | ) {
|
---|
| 981 | LOG(3, "INFO: Creating bond between ("
|
---|
| 982 | << *iter
|
---|
| 983 | << ") and ("
|
---|
| 984 | << getGlobalId(*neighbor) << "|" << *neighbor << ")");
|
---|
| 985 | ASSERT(getGlobalId(*neighbor) != -1,
|
---|
| 986 | "FormatParser< tremolo >::processNeighborInformation() - global id to local id "
|
---|
| 987 | +toString(*neighbor)+" is unknown.");
|
---|
| 988 | World::getInstance().getAtom(AtomById(*iter))
|
---|
| 989 | ->addBond(WorldTime::getTime(), World::getInstance().getAtom(AtomById(getGlobalId(*neighbor))));
|
---|
[9131f3] | 990 | }
|
---|
[9f8b01] | 991 | currentInfo.neighbors_processed = true;
|
---|
[9131f3] | 992 | }
|
---|
[6bc51d] | 993 | }
|
---|
| 994 |
|
---|
[9131f3] | 995 | /**
|
---|
[b8d4a3] | 996 | * Replaces atom IDs read from the file by the corresponding world IDs. All IDs
|
---|
| 997 | * IDs of the input string will be replaced; expected separating characters are
|
---|
| 998 | * "-" and ",".
|
---|
[9131f3] | 999 | *
|
---|
[b8d4a3] | 1000 | * \param string in which atom IDs should be adapted
|
---|
[05e2ed] | 1001 | * \param idgetter function pointer to change the id
|
---|
[b8d4a3] | 1002 | *
|
---|
| 1003 | * \return input string with modified atom IDs
|
---|
[9131f3] | 1004 | */
|
---|
[05e2ed] | 1005 | std::string FormatParser< tremolo >::adaptIdDependentDataString(
|
---|
| 1006 | const std::string &data,
|
---|
| 1007 | const boost::function<int (const int)> &idgetter
|
---|
| 1008 | ) {
|
---|
[b8d4a3] | 1009 | // there might be no IDs
|
---|
| 1010 | if (data == "-") {
|
---|
| 1011 | return "-";
|
---|
| 1012 | }
|
---|
| 1013 |
|
---|
| 1014 | char separator;
|
---|
| 1015 | int id;
|
---|
[8bf9c6] | 1016 | std::stringstream line, result;
|
---|
[b8d4a3] | 1017 | line << data;
|
---|
| 1018 |
|
---|
| 1019 | line >> id;
|
---|
[05e2ed] | 1020 | result << idgetter(id);
|
---|
[b8d4a3] | 1021 | while (line.good()) {
|
---|
| 1022 | line >> separator >> id;
|
---|
[05e2ed] | 1023 | result << separator << idgetter(id);
|
---|
[b8d4a3] | 1024 | }
|
---|
| 1025 |
|
---|
| 1026 | return result.str();
|
---|
[6bc51d] | 1027 | }
|
---|
[b8d4a3] | 1028 |
|
---|
[05e2ed] | 1029 | /** Corrects the atom IDs in each imprData entry to the corresponding world IDs
|
---|
[b8d4a3] | 1030 | * as they might differ from the originally read IDs.
|
---|
[05e2ed] | 1031 | *
|
---|
| 1032 | * \param atoms currently parsed in atoms
|
---|
[b8d4a3] | 1033 | */
|
---|
[05e2ed] | 1034 | void FormatParser< tremolo >::adaptImprData(const std::vector<atomId_t> &atoms) {
|
---|
[23fd43] | 1035 | if (!isUsedField(usedFields_load, "imprData")) {
|
---|
[b8d4a3] | 1036 | return;
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
[05e2ed] | 1039 | for (std::vector<atomId_t>::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 1040 | ASSERT(additionalAtomData.count(*iter) != 0,
|
---|
| 1041 | "FormatParser< tremolo >::processNeighborInformation() - global id "
|
---|
| 1042 | +toString(*iter)+" unknown in additionalAtomData.");
|
---|
| 1043 | TremoloAtomInfoContainer ¤tInfo = additionalAtomData[*iter];
|
---|
| 1044 | currentInfo.imprData = adaptIdDependentDataString(currentInfo.imprData, idglobalizer);
|
---|
[b8d4a3] | 1045 | }
|
---|
[6bc51d] | 1046 | }
|
---|
[4415da] | 1047 |
|
---|
[05e2ed] | 1048 | /** Corrects the atom IDs in each torsion entry to the corresponding world IDs
|
---|
[b8d4a3] | 1049 | * as they might differ from the originally read IDs.
|
---|
[05e2ed] | 1050 | *
|
---|
| 1051 | * \param atoms currently parsed in atoms
|
---|
[b8d4a3] | 1052 | */
|
---|
[05e2ed] | 1053 | void FormatParser< tremolo >::adaptTorsion(const std::vector<atomId_t> &atoms) {
|
---|
[23fd43] | 1054 | if (!isUsedField(usedFields_load, "torsion")) {
|
---|
[b8d4a3] | 1055 | return;
|
---|
| 1056 | }
|
---|
| 1057 |
|
---|
[05e2ed] | 1058 | for (std::vector<atomId_t>::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
|
---|
| 1059 | ASSERT(additionalAtomData.count(*iter) != 0,
|
---|
| 1060 | "FormatParser< tremolo >::processNeighborInformation() - global id "
|
---|
| 1061 | +toString(*iter)+" unknown in additionalAtomData.");
|
---|
| 1062 | TremoloAtomInfoContainer ¤tInfo = additionalAtomData[*iter];
|
---|
| 1063 | currentInfo.torsion = adaptIdDependentDataString(currentInfo.torsion, idglobalizer);
|
---|
[b8d4a3] | 1064 | }
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|