source: src/Parser/TremoloParser.cpp@ c0d9eb

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 c0d9eb was 056e70, checked in by Frederik Heber <heber@…>, 14 years ago

Suffixed getters and setters for AtomInfo trajecories with AtStep.

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