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