source: src/Parser/TremoloParser.cpp@ a275b3

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since a275b3 was a275b3, checked in by Frederik Heber <heber@…>, 13 years ago

TremoloParser uses ElementKeys.

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