| 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 |  * XyzParser.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 <vector>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 25 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 26 | #include "XyzParser.hpp"
 | 
|---|
| 27 | #include "World.hpp"
 | 
|---|
| 28 | #include "atom.hpp"
 | 
|---|
| 29 | #include "molecule.hpp"
 | 
|---|
| 30 | #include "element.hpp"
 | 
|---|
| 31 | #include "periodentafel.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | using namespace std;
 | 
|---|
| 34 | 
 | 
|---|
| 35 | // declare specialized static variables
 | 
|---|
| 36 | const std::string FormatParserTrait<xyz>::name = "xyz";
 | 
|---|
| 37 | const std::string FormatParserTrait<xyz>::suffix = "xyz";
 | 
|---|
| 38 | const ParserTypes FormatParserTrait<xyz>::type = xyz;
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /**
 | 
|---|
| 41 |  * Constructor.
 | 
|---|
| 42 |  */
 | 
|---|
| 43 | FormatParser< xyz >::FormatParser() :
 | 
|---|
| 44 |   FormatParser_common(NULL),
 | 
|---|
| 45 |   comment("")
 | 
|---|
| 46 | {}
 | 
|---|
| 47 | 
 | 
|---|
| 48 | /**
 | 
|---|
| 49 |  * Destructor.
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | FormatParser< xyz >::~FormatParser() 
 | 
|---|
| 52 | {}
 | 
|---|
| 53 | 
 | 
|---|
| 54 | /**
 | 
|---|
| 55 |  * Loads an XYZ file into the World.
 | 
|---|
| 56 |  *
 | 
|---|
| 57 |  * \param XYZ file
 | 
|---|
| 58 |  */
 | 
|---|
| 59 | void FormatParser< xyz >::load(istream* file)
 | 
|---|
| 60 | {
 | 
|---|
| 61 |   atom* newAtom = NULL;
 | 
|---|
| 62 |   molecule* newmol = NULL;
 | 
|---|
| 63 |   int numberOfAtoms;
 | 
|---|
| 64 |   char commentBuffer[512], type[3];
 | 
|---|
| 65 |   string number;
 | 
|---|
| 66 |   std::vector<atom *> AddedAtoms;
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   // create the molecule
 | 
|---|
| 69 |   newmol = World::getInstance().createMolecule();
 | 
|---|
| 70 |   newmol->ActiveFlag = true;
 | 
|---|
| 71 |   // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
 | 
|---|
| 72 |   World::getInstance().getMolecules()->insert(newmol);
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   // the first line tells number of atoms,
 | 
|---|
| 75 |   // where we need this construct due to skipping of empty lines below
 | 
|---|
| 76 |   *file >> number >> ws;
 | 
|---|
| 77 |   unsigned int step = 0;
 | 
|---|
| 78 |   while (file->good()) {
 | 
|---|
| 79 |     std::stringstream numberstream(number);
 | 
|---|
| 80 |     numberstream >> numberOfAtoms;
 | 
|---|
| 81 |     if (step == 0)
 | 
|---|
| 82 |       AddedAtoms.resize(numberOfAtoms);
 | 
|---|
| 83 |     // the second line is always a comment
 | 
|---|
| 84 |     file->getline(commentBuffer, 512);
 | 
|---|
| 85 |     comment = commentBuffer;
 | 
|---|
| 86 | 
 | 
|---|
| 87 |     for (int i = 0; i < numberOfAtoms; i++) {
 | 
|---|
| 88 |       // parse type and position
 | 
|---|
| 89 |       *file >> type;
 | 
|---|
| 90 |       Vector tempVector;
 | 
|---|
| 91 |       for (int j=0;j<NDIM;j++) {
 | 
|---|
| 92 |         *file >> tempVector[j];
 | 
|---|
| 93 |       }
 | 
|---|
| 94 |       LOG(4, "INFO: Parsed type as " << type << " and position at "
 | 
|---|
| 95 |           << tempVector << " for time step " << step);
 | 
|---|
| 96 | 
 | 
|---|
| 97 |       if (step == 0) {
 | 
|---|
| 98 |         newAtom = World::getInstance().createAtom();
 | 
|---|
| 99 |         newAtom->setType(World::getInstance().getPeriode()->FindElement(type));
 | 
|---|
| 100 |         newmol->AddAtom(newAtom);
 | 
|---|
| 101 |         AddedAtoms[i] = newAtom;
 | 
|---|
| 102 |         LOG(5, "INFO: Created new atom, setting " << i << " th component of AddedAtoms.");
 | 
|---|
| 103 |       } else {
 | 
|---|
| 104 |         newAtom = AddedAtoms[i];
 | 
|---|
| 105 |         LOG(5, "INFO: Using present atom " << *newAtom << " from " << i << " th component of AddedAtoms.");
 | 
|---|
| 106 |         ASSERT(newAtom->getType() == World::getInstance().getPeriode()->FindElement(type),
 | 
|---|
| 107 |             "FormatParser< xyz >::load() - atom has different type "+newAtom->getType()->getSymbol()+" than parsed now "+type+", mixed up order?");
 | 
|---|
| 108 |       }
 | 
|---|
| 109 |       newAtom->setPositionAtStep(step, tempVector);
 | 
|---|
| 110 |     }
 | 
|---|
| 111 |     getline (*file, number);  // eat away rest of last line
 | 
|---|
| 112 |     // skip empty lines
 | 
|---|
| 113 |     unsigned int counter = 0;
 | 
|---|
| 114 |     while (file->good()) {
 | 
|---|
| 115 |       getline (*file, number);
 | 
|---|
| 116 |       LOG(4, "INFO: Skipped line is '" << number << "'");
 | 
|---|
| 117 |       counter++;
 | 
|---|
| 118 |       if (!number.empty())
 | 
|---|
| 119 |         break;
 | 
|---|
| 120 |     }
 | 
|---|
| 121 |     LOG(3, "INFO: I skipped "+toString(counter)+" empty lines.");
 | 
|---|
| 122 |     ++step;
 | 
|---|
| 123 |   }
 | 
|---|
| 124 |   BOOST_FOREACH(atom *_atom, World::getInstance().getAllAtoms())
 | 
|---|
| 125 |     LOG(3, "INFO: Atom " << _atom->getName() << " " << *dynamic_cast<AtomInfo *>(_atom) << ".");
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   // refresh atom::nr and atom::name
 | 
|---|
| 128 |   newmol->getAtomCount();
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | /**
 | 
|---|
| 132 |  * Saves the \a atoms into as a XYZ file.
 | 
|---|
| 133 |  *
 | 
|---|
| 134 |  * \param file where to save the state
 | 
|---|
| 135 |  * \param atoms atoms to store
 | 
|---|
| 136 |  */
 | 
|---|
| 137 | void FormatParser< xyz >::save(ostream* file, const std::vector<atom *> &atoms) {
 | 
|---|
| 138 |   DoLog(0) && (Log() << Verbose(0) << "Saving changes to xyz." << std::endl);
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   // get max and min trajectories
 | 
|---|
| 141 |   unsigned int min_trajectories = 1;
 | 
|---|
| 142 |   unsigned int max_trajectories = 1;
 | 
|---|
| 143 |   for (std::vector<atom *>::const_iterator iter = atoms.begin();
 | 
|---|
| 144 |       iter != atoms.end();
 | 
|---|
| 145 |       ++iter) {
 | 
|---|
| 146 |     if (max_trajectories < (*iter)->getTrajectorySize())
 | 
|---|
| 147 |       max_trajectories = (*iter)->getTrajectorySize();
 | 
|---|
| 148 |     if ((min_trajectories > (*iter)->getTrajectorySize())
 | 
|---|
| 149 |         && (max_trajectories > (*iter)->getTrajectorySize()))
 | 
|---|
| 150 |       min_trajectories = (*iter)->getTrajectorySize();
 | 
|---|
| 151 |   }
 | 
|---|
| 152 |   ASSERT((min_trajectories == max_trajectories) || (min_trajectories == 1),
 | 
|---|
| 153 |       "FormatParser< xyz >::save() - not all atoms have same number of trajectories.");
 | 
|---|
| 154 |   LOG(2, "INFO: There are " << max_trajectories << " to save.");
 | 
|---|
| 155 | 
 | 
|---|
| 156 |   for (unsigned int step = 0; step < max_trajectories; ++step) {
 | 
|---|
| 157 |     if (step != 0)
 | 
|---|
| 158 |       *file << "\n";
 | 
|---|
| 159 |     //if (comment == "") {
 | 
|---|
| 160 |       time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| 161 |       comment = "Created by molecuilder on ";
 | 
|---|
| 162 |       // ctime ends in \n\0, we have to cut away the newline
 | 
|---|
| 163 |       std::string time(ctime(&now));
 | 
|---|
| 164 |       size_t pos = time.find('\n');
 | 
|---|
| 165 |       if (pos != 0)
 | 
|---|
| 166 |         comment += time.substr(0,pos);
 | 
|---|
| 167 |       else
 | 
|---|
| 168 |         comment += time;
 | 
|---|
| 169 |       comment += ", time step "+toString(step);
 | 
|---|
| 170 |     //}
 | 
|---|
| 171 |     *file << atoms.size() << endl << "\t" << comment << endl;
 | 
|---|
| 172 | 
 | 
|---|
| 173 |     for(vector<atom*>::const_iterator it = atoms.begin(); it != atoms.end(); it++) {
 | 
|---|
| 174 |       *file << noshowpoint << (*it)->getType()->getSymbol();
 | 
|---|
| 175 |       *file << "\t" << (*it)->atStep(0, step);
 | 
|---|
| 176 |       *file << "\t" << (*it)->atStep(1, step);
 | 
|---|
| 177 |       *file << "\t" << (*it)->atStep(2, step);
 | 
|---|
| 178 |       *file << endl;
 | 
|---|
| 179 |     }
 | 
|---|
| 180 |   }
 | 
|---|
| 181 | }
 | 
|---|