source: src/Parser/TremoloParser.cpp@ dc8827

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 dc8827 was 2e352f, checked in by Frederik Heber <heber@…>, 13 years ago

Because of problems with columns in data-files, some changes were done.

  • atom::CorrectFactor() does not set fathet to itself anymore in father's father case.
  • molecule::CopyMolecule() does not correct fathers anymore

Changed due to rebase to v1.1.3:

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