| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| [94d5ac6] | 6 |  * 
 | 
|---|
 | 7 |  *
 | 
|---|
 | 8 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 9 |  *
 | 
|---|
 | 10 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 11 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 12 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 13 |  *    (at your option) any later version.
 | 
|---|
 | 14 |  *
 | 
|---|
 | 15 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 16 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 17 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 18 |  *    GNU General Public License for more details.
 | 
|---|
 | 19 |  *
 | 
|---|
 | 20 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 21 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [bcf653] | 22 |  */
 | 
|---|
 | 23 | 
 | 
|---|
| [ab4b55] | 24 | /*
 | 
|---|
 | 25 |  * XyzParser.cpp
 | 
|---|
 | 26 |  *
 | 
|---|
 | 27 |  *  Created on: Mar 2, 2010
 | 
|---|
 | 28 |  *      Author: metzler
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
| [bf3817] | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
| [ad011c] | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 37 | 
 | 
|---|
| [c2808e] | 38 | #include <limits>
 | 
|---|
| [637358] | 39 | #include <vector>
 | 
|---|
 | 40 | 
 | 
|---|
| [ad011c] | 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 42 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [42127c] | 43 | 
 | 
|---|
| [ab4b55] | 44 | #include "XyzParser.hpp"
 | 
|---|
| [42127c] | 45 | 
 | 
|---|
| [6f0841] | 46 | #include "Atom/atom.hpp"
 | 
|---|
| [3bdb6d] | 47 | #include "Element/element.hpp"
 | 
|---|
 | 48 | #include "Element/periodentafel.hpp"
 | 
|---|
| [42127c] | 49 | #include "molecule.hpp"
 | 
|---|
 | 50 | #include "MoleculeListClass.hpp"
 | 
|---|
 | 51 | #include "World.hpp"
 | 
|---|
| [ab4b55] | 52 | 
 | 
|---|
 | 53 | using namespace std;
 | 
|---|
 | 54 | 
 | 
|---|
| [765f16] | 55 | // declare specialized static variables
 | 
|---|
 | 56 | const std::string FormatParserTrait<xyz>::name = "xyz";
 | 
|---|
 | 57 | const std::string FormatParserTrait<xyz>::suffix = "xyz";
 | 
|---|
 | 58 | const ParserTypes FormatParserTrait<xyz>::type = xyz;
 | 
|---|
 | 59 | 
 | 
|---|
| [ab4b55] | 60 | /**
 | 
|---|
 | 61 |  * Constructor.
 | 
|---|
 | 62 |  */
 | 
|---|
| [765f16] | 63 | FormatParser< xyz >::FormatParser() :
 | 
|---|
 | 64 |   FormatParser_common(NULL),
 | 
|---|
| [97b825] | 65 |   comment("")
 | 
|---|
 | 66 | {}
 | 
|---|
| [ab4b55] | 67 | 
 | 
|---|
 | 68 | /**
 | 
|---|
 | 69 |  * Destructor.
 | 
|---|
 | 70 |  */
 | 
|---|
| [765f16] | 71 | FormatParser< xyz >::~FormatParser() 
 | 
|---|
 | 72 | {}
 | 
|---|
| [ab4b55] | 73 | 
 | 
|---|
 | 74 | /**
 | 
|---|
 | 75 |  * Loads an XYZ file into the World.
 | 
|---|
 | 76 |  *
 | 
|---|
 | 77 |  * \param XYZ file
 | 
|---|
 | 78 |  */
 | 
|---|
| [765f16] | 79 | void FormatParser< xyz >::load(istream* file)
 | 
|---|
| [0180d6] | 80 | {
 | 
|---|
| [dddbfe] | 81 |   atom* newAtom = NULL;
 | 
|---|
 | 82 |   molecule* newmol = NULL;
 | 
|---|
| [ab4b55] | 83 |   int numberOfAtoms;
 | 
|---|
| [58fccca] | 84 |   string elementtype;
 | 
|---|
 | 85 |   string streambuffer;
 | 
|---|
| [637358] | 86 |   std::vector<atom *> AddedAtoms;
 | 
|---|
| [ab4b55] | 87 | 
 | 
|---|
| [0180d6] | 88 |   // create the molecule
 | 
|---|
| [dddbfe] | 89 |   newmol = World::getInstance().createMolecule();
 | 
|---|
 | 90 |   newmol->ActiveFlag = true;
 | 
|---|
| [bb4408] | 91 |   // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
 | 
|---|
| [dddbfe] | 92 |   World::getInstance().getMolecules()->insert(newmol);
 | 
|---|
| [0180d6] | 93 | 
 | 
|---|
 | 94 |   // the first line tells number of atoms,
 | 
|---|
 | 95 |   // where we need this construct due to skipping of empty lines below
 | 
|---|
| [58fccca] | 96 |   getline(*file, streambuffer);
 | 
|---|
| [0180d6] | 97 |   unsigned int step = 0;
 | 
|---|
| [4fdc65] | 98 |   while (file->good()) {
 | 
|---|
| [58fccca] | 99 |     std::stringstream numberstream(streambuffer);
 | 
|---|
| [0180d6] | 100 |     numberstream >> numberOfAtoms;
 | 
|---|
| [637358] | 101 |     if (step == 0)
 | 
|---|
 | 102 |       AddedAtoms.resize(numberOfAtoms);
 | 
|---|
| [0180d6] | 103 |     // the second line is always a comment
 | 
|---|
| [58fccca] | 104 |     getline(*file, streambuffer);
 | 
|---|
 | 105 |     comment = streambuffer;
 | 
|---|
| [259887] | 106 |     LOG(3, "DEBUG: comment is '" << comment << "'.");
 | 
|---|
| [0180d6] | 107 | 
 | 
|---|
 | 108 |     for (int i = 0; i < numberOfAtoms; i++) {
 | 
|---|
 | 109 |       // parse type and position
 | 
|---|
| [58fccca] | 110 |       *file >> elementtype;
 | 
|---|
| [0180d6] | 111 |       Vector tempVector;
 | 
|---|
 | 112 |       for (int j=0;j<NDIM;j++) {
 | 
|---|
 | 113 |         *file >> tempVector[j];
 | 
|---|
 | 114 |       }
 | 
|---|
| [58fccca] | 115 |       LOG(4, "INFO: Parsed type as " << elementtype << " and position at "
 | 
|---|
| [0180d6] | 116 |           << tempVector << " for time step " << step);
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 |       if (step == 0) {
 | 
|---|
 | 119 |         newAtom = World::getInstance().createAtom();
 | 
|---|
| [58fccca] | 120 |         newAtom->setType(World::getInstance().getPeriode()->FindElement(elementtype.c_str()));
 | 
|---|
| [0180d6] | 121 |         newmol->AddAtom(newAtom);
 | 
|---|
| [637358] | 122 |         AddedAtoms[i] = newAtom;
 | 
|---|
 | 123 |         LOG(5, "INFO: Created new atom, setting " << i << " th component of AddedAtoms.");
 | 
|---|
| [0180d6] | 124 |       } else {
 | 
|---|
| [637358] | 125 |         newAtom = AddedAtoms[i];
 | 
|---|
 | 126 |         LOG(5, "INFO: Using present atom " << *newAtom << " from " << i << " th component of AddedAtoms.");
 | 
|---|
| [58fccca] | 127 |         ASSERT(newAtom->getType() == World::getInstance().getPeriode()->FindElement(elementtype.c_str()),
 | 
|---|
 | 128 |             "FormatParser< xyz >::load() - atom has different type "+newAtom->getType()->getSymbol()+" than parsed now "+elementtype+", mixed up order?");
 | 
|---|
| [0180d6] | 129 |       }
 | 
|---|
 | 130 |       newAtom->setPositionAtStep(step, tempVector);
 | 
|---|
| [d74077] | 131 |     }
 | 
|---|
| [58fccca] | 132 |     getline (*file, streambuffer);  // eat away rest of last line
 | 
|---|
| [0180d6] | 133 |     // skip empty lines
 | 
|---|
 | 134 |     unsigned int counter = 0;
 | 
|---|
 | 135 |     while (file->good()) {
 | 
|---|
| [58fccca] | 136 |       getline (*file, streambuffer);
 | 
|---|
 | 137 |       LOG(4, "INFO: Skipped line is '" << streambuffer << "'");
 | 
|---|
| [0180d6] | 138 |       counter++;
 | 
|---|
| [58fccca] | 139 |       if (!streambuffer.empty())
 | 
|---|
| [0180d6] | 140 |         break;
 | 
|---|
 | 141 |     }
 | 
|---|
 | 142 |     LOG(3, "INFO: I skipped "+toString(counter)+" empty lines.");
 | 
|---|
 | 143 |     ++step;
 | 
|---|
| [4fdc65] | 144 |   }
 | 
|---|
| [a58c16] | 145 |   BOOST_FOREACH(const atom *_atom, const_cast<const World &>(World::getInstance()).getAllAtoms())
 | 
|---|
 | 146 |     LOG(3, "INFO: Atom " << _atom->getName() << " " << *dynamic_cast<const AtomInfo *>(_atom) << ".");
 | 
|---|
| [4afa46] | 147 | 
 | 
|---|
 | 148 |   // refresh atom::nr and atom::name
 | 
|---|
 | 149 |   newmol->getAtomCount();
 | 
|---|
| [ab4b55] | 150 | }
 | 
|---|
 | 151 | 
 | 
|---|
| [d5b90b] | 152 | const std::string FormatParser< xyz >::printCoordinate(const double value)
 | 
|---|
 | 153 | {
 | 
|---|
 | 154 |   std::stringstream position;
 | 
|---|
 | 155 |   if (fabs(value) > 1000) // enforce precision for large components
 | 
|---|
 | 156 |     position << std::fixed << std::setprecision(3) << value; 
 | 
|---|
 | 157 |   else
 | 
|---|
 | 158 |     position << value; 
 | 
|---|
 | 159 |   return position.str();
 | 
|---|
 | 160 | }
 | 
|---|
 | 161 | 
 | 
|---|
| [ab4b55] | 162 | /**
 | 
|---|
| [73916f] | 163 |  * Saves the \a atoms into as a XYZ file.
 | 
|---|
| [ab4b55] | 164 |  *
 | 
|---|
| [73916f] | 165 |  * \param file where to save the state
 | 
|---|
 | 166 |  * \param atoms atoms to store
 | 
|---|
| [ab4b55] | 167 |  */
 | 
|---|
| [fac58f] | 168 | void FormatParser< xyz >::save(
 | 
|---|
 | 169 |     ostream* file,
 | 
|---|
 | 170 |     const std::vector<const atom *> &atoms) {
 | 
|---|
| [830b3e] | 171 |   LOG(2, "DEBUG: Saving changes to xyz.");
 | 
|---|
| [0180d6] | 172 | 
 | 
|---|
 | 173 |   // get max and min trajectories
 | 
|---|
| [c2808e] | 174 |   size_t min_trajectories = std::numeric_limits<size_t>::max();
 | 
|---|
 | 175 |   size_t max_trajectories = std::numeric_limits<size_t>::min();
 | 
|---|
| [fac58f] | 176 |   for (std::vector<const atom *>::const_iterator iter = atoms.begin();
 | 
|---|
| [0180d6] | 177 |       iter != atoms.end();
 | 
|---|
 | 178 |       ++iter) {
 | 
|---|
 | 179 |     if (max_trajectories < (*iter)->getTrajectorySize())
 | 
|---|
 | 180 |       max_trajectories = (*iter)->getTrajectorySize();
 | 
|---|
| [c2808e] | 181 |     if (min_trajectories > (*iter)->getTrajectorySize())
 | 
|---|
| [0180d6] | 182 |       min_trajectories = (*iter)->getTrajectorySize();
 | 
|---|
 | 183 |   }
 | 
|---|
| [c2808e] | 184 |   // no atoms? Then, they all have same amount
 | 
|---|
 | 185 |   if (atoms.size() == 0)
 | 
|---|
 | 186 |     min_trajectories = max_trajectories = 1;
 | 
|---|
 | 187 |   ASSERT(min_trajectories == max_trajectories,
 | 
|---|
 | 188 |       "FormatParser< xyz >::save() - not all atoms have same number of trajectories: "
 | 
|---|
 | 189 |       +toString(min_trajectories)+" != "+toString(max_trajectories)+".");
 | 
|---|
 | 190 |   LOG(2, "INFO: There are " << max_trajectories << " steps to save.");
 | 
|---|
 | 191 | 
 | 
|---|
 | 192 |   // always store at least one step
 | 
|---|
 | 193 |   for (size_t step = 0; (step < max_trajectories) || (step == 0); ++step) {
 | 
|---|
| [0180d6] | 194 |     if (step != 0)
 | 
|---|
 | 195 |       *file << "\n";
 | 
|---|
 | 196 |     //if (comment == "") {
 | 
|---|
 | 197 |       time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
 | 198 |       comment = "Created by molecuilder on ";
 | 
|---|
 | 199 |       // ctime ends in \n\0, we have to cut away the newline
 | 
|---|
 | 200 |       std::string time(ctime(&now));
 | 
|---|
 | 201 |       size_t pos = time.find('\n');
 | 
|---|
 | 202 |       if (pos != 0)
 | 
|---|
 | 203 |         comment += time.substr(0,pos);
 | 
|---|
 | 204 |       else
 | 
|---|
 | 205 |         comment += time;
 | 
|---|
 | 206 |       comment += ", time step "+toString(step);
 | 
|---|
 | 207 |     //}
 | 
|---|
 | 208 |     *file << atoms.size() << endl << "\t" << comment << endl;
 | 
|---|
 | 209 | 
 | 
|---|
| [fac58f] | 210 |     for(vector<const atom*>::const_iterator it = atoms.begin(); it != atoms.end(); it++) {
 | 
|---|
| [d5b90b] | 211 |       *file << (*it)->getType()->getSymbol();
 | 
|---|
 | 212 |       *file << "\t" << printCoordinate((*it)->atStep(0, step));
 | 
|---|
 | 213 |       *file << "\t" << printCoordinate((*it)->atStep(1, step));
 | 
|---|
 | 214 |       *file << "\t" << printCoordinate((*it)->atStep(2, step));
 | 
|---|
| [0180d6] | 215 |       *file << endl;
 | 
|---|
 | 216 |     }
 | 
|---|
| [ab4b55] | 217 |   }
 | 
|---|
 | 218 | }
 | 
|---|