| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 22 |  */
 | 
|---|
| 23 | 
 | 
|---|
| 24 | /*
 | 
|---|
| 25 |  * XyzParser.cpp
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  *  Created on: Mar 2, 2010
 | 
|---|
| 28 |  *      Author: metzler
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // include config.h
 | 
|---|
| 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 33 | #include <config.h>
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include <limits>
 | 
|---|
| 39 | #include <vector>
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 42 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include "XyzParser.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "Atom/atom.hpp"
 | 
|---|
| 47 | #include "Element/element.hpp"
 | 
|---|
| 48 | #include "Element/periodentafel.hpp"
 | 
|---|
| 49 | #include "molecule.hpp"
 | 
|---|
| 50 | #include "World.hpp"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | using namespace std;
 | 
|---|
| 53 | 
 | 
|---|
| 54 | // declare specialized static variables
 | 
|---|
| 55 | const std::string FormatParserTrait<xyz>::name = "xyz";
 | 
|---|
| 56 | const std::string FormatParserTrait<xyz>::suffix = "xyz";
 | 
|---|
| 57 | const ParserTypes FormatParserTrait<xyz>::type = xyz;
 | 
|---|
| 58 | 
 | 
|---|
| 59 | /**
 | 
|---|
| 60 |  * Constructor.
 | 
|---|
| 61 |  */
 | 
|---|
| 62 | FormatParser< xyz >::FormatParser() :
 | 
|---|
| 63 |   FormatParser_common(NULL),
 | 
|---|
| 64 |   comment("")
 | 
|---|
| 65 | {}
 | 
|---|
| 66 | 
 | 
|---|
| 67 | /**
 | 
|---|
| 68 |  * Destructor.
 | 
|---|
| 69 |  */
 | 
|---|
| 70 | FormatParser< xyz >::~FormatParser() 
 | 
|---|
| 71 | {}
 | 
|---|
| 72 | 
 | 
|---|
| 73 | /**
 | 
|---|
| 74 |  * Loads an XYZ file into the World.
 | 
|---|
| 75 |  *
 | 
|---|
| 76 |  * \param XYZ file
 | 
|---|
| 77 |  */
 | 
|---|
| 78 | void FormatParser< xyz >::load(istream* file)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   atom* newAtom = NULL;
 | 
|---|
| 81 |   molecule* newmol = NULL;
 | 
|---|
| 82 |   int numberOfAtoms;
 | 
|---|
| 83 |   string elementtype;
 | 
|---|
| 84 |   string streambuffer;
 | 
|---|
| 85 |   std::vector<atom *> AddedAtoms;
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   // create the molecule
 | 
|---|
| 88 |   newmol = World::getInstance().createMolecule();
 | 
|---|
| 89 |   newmol->ActiveFlag = true;
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   // the first line tells number of atoms,
 | 
|---|
| 92 |   // where we need this construct due to skipping of empty lines below
 | 
|---|
| 93 |   getline(*file, streambuffer);
 | 
|---|
| 94 |   unsigned int step = 0;
 | 
|---|
| 95 |   while (file->good()) {
 | 
|---|
| 96 |     std::stringstream numberstream(streambuffer);
 | 
|---|
| 97 |     numberstream >> numberOfAtoms;
 | 
|---|
| 98 |     if (step == 0)
 | 
|---|
| 99 |       AddedAtoms.resize(numberOfAtoms);
 | 
|---|
| 100 |     // the second line is always a comment
 | 
|---|
| 101 |     getline(*file, streambuffer);
 | 
|---|
| 102 |     comment = streambuffer;
 | 
|---|
| 103 |     LOG(3, "DEBUG: comment is '" << comment << "'.");
 | 
|---|
| 104 | 
 | 
|---|
| 105 |     for (int i = 0; i < numberOfAtoms; i++) {
 | 
|---|
| 106 |       // parse type and position
 | 
|---|
| 107 |       *file >> elementtype;
 | 
|---|
| 108 |       Vector tempVector;
 | 
|---|
| 109 |       for (int j=0;j<NDIM;j++) {
 | 
|---|
| 110 |         *file >> tempVector[j];
 | 
|---|
| 111 |       }
 | 
|---|
| 112 |       LOG(4, "INFO: Parsed type as " << elementtype << " and position at "
 | 
|---|
| 113 |           << tempVector << " for time step " << step);
 | 
|---|
| 114 | 
 | 
|---|
| 115 |       if (step == 0) {
 | 
|---|
| 116 |         newAtom = World::getInstance().createAtom();
 | 
|---|
| 117 |         newAtom->setType(World::getInstance().getPeriode()->FindElement(elementtype.c_str()));
 | 
|---|
| 118 |         newmol->AddAtom(newAtom);
 | 
|---|
| 119 |         AddedAtoms[i] = newAtom;
 | 
|---|
| 120 |         LOG(5, "INFO: Created new atom, setting " << i << " th component of AddedAtoms.");
 | 
|---|
| 121 |       } else {
 | 
|---|
| 122 |         newAtom = AddedAtoms[i];
 | 
|---|
| 123 |         LOG(5, "INFO: Using present atom " << *newAtom << " from " << i << " th component of AddedAtoms.");
 | 
|---|
| 124 |         ASSERT(newAtom->getType() == World::getInstance().getPeriode()->FindElement(elementtype.c_str()),
 | 
|---|
| 125 |             "FormatParser< xyz >::load() - atom has different type "+newAtom->getType()->getSymbol()+" than parsed now "+elementtype+", mixed up order?");
 | 
|---|
| 126 |       }
 | 
|---|
| 127 |       newAtom->setPositionAtStep(step, tempVector);
 | 
|---|
| 128 |     }
 | 
|---|
| 129 |     getline (*file, streambuffer);  // eat away rest of last line
 | 
|---|
| 130 |     // skip empty lines
 | 
|---|
| 131 |     unsigned int counter = 0;
 | 
|---|
| 132 |     while (file->good()) {
 | 
|---|
| 133 |       getline (*file, streambuffer);
 | 
|---|
| 134 |       LOG(4, "INFO: Skipped line is '" << streambuffer << "'");
 | 
|---|
| 135 |       counter++;
 | 
|---|
| 136 |       if (!streambuffer.empty())
 | 
|---|
| 137 |         break;
 | 
|---|
| 138 |     }
 | 
|---|
| 139 |     LOG(3, "INFO: I skipped "+toString(counter)+" empty lines.");
 | 
|---|
| 140 |     ++step;
 | 
|---|
| 141 |   }
 | 
|---|
| 142 |   BOOST_FOREACH(const atom *_atom, const_cast<const World &>(World::getInstance()).getAllAtoms())
 | 
|---|
| 143 |     LOG(3, "INFO: Atom " << _atom->getName() << " " << *dynamic_cast<const AtomInfo *>(_atom) << ".");
 | 
|---|
| 144 | 
 | 
|---|
| 145 |   // refresh atom::nr and atom::name
 | 
|---|
| 146 |   newmol->getAtomCount();
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | const std::string FormatParser< xyz >::printCoordinate(const double value)
 | 
|---|
| 150 | {
 | 
|---|
| 151 |   std::stringstream position;
 | 
|---|
| 152 |   if (fabs(value) > 1000) // enforce precision for large components
 | 
|---|
| 153 |     position << std::fixed << std::setprecision(3) << value; 
 | 
|---|
| 154 |   else
 | 
|---|
| 155 |     position << value; 
 | 
|---|
| 156 |   return position.str();
 | 
|---|
| 157 | }
 | 
|---|
| 158 | 
 | 
|---|
| 159 | /**
 | 
|---|
| 160 |  * Saves the \a atoms into as a XYZ file.
 | 
|---|
| 161 |  *
 | 
|---|
| 162 |  * \param file where to save the state
 | 
|---|
| 163 |  * \param atoms atoms to store
 | 
|---|
| 164 |  */
 | 
|---|
| 165 | void FormatParser< xyz >::save(
 | 
|---|
| 166 |     ostream* file,
 | 
|---|
| 167 |     const std::vector<const atom *> &atoms) {
 | 
|---|
| 168 |   LOG(2, "DEBUG: Saving changes to xyz.");
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   // get max and min trajectories
 | 
|---|
| 171 |   std::pair<size_t, size_t> minmax_trajectories =
 | 
|---|
| 172 |       getMinMaxTrajectories(atoms);
 | 
|---|
| 173 |   LOG(2, "INFO: There are " << minmax_trajectories.second << " steps to save.");
 | 
|---|
| 174 | 
 | 
|---|
| 175 |   // always store at least one step
 | 
|---|
| 176 |   for (size_t step = 0; (step < minmax_trajectories.second) || (step == 0); ++step) {
 | 
|---|
| 177 |     if (step != 0)
 | 
|---|
| 178 |       *file << "\n";
 | 
|---|
| 179 |     //if (comment == "") {
 | 
|---|
| 180 |       time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| 181 |       comment = "Created by molecuilder on ";
 | 
|---|
| 182 |       // ctime ends in \n\0, we have to cut away the newline
 | 
|---|
| 183 |       std::string time(ctime(&now));
 | 
|---|
| 184 |       size_t pos = time.find('\n');
 | 
|---|
| 185 |       if (pos != 0)
 | 
|---|
| 186 |         comment += time.substr(0,pos);
 | 
|---|
| 187 |       else
 | 
|---|
| 188 |         comment += time;
 | 
|---|
| 189 |       comment += ", time step "+toString(step);
 | 
|---|
| 190 |     //}
 | 
|---|
| 191 |     *file << atoms.size() << endl << "\t" << comment << endl;
 | 
|---|
| 192 | 
 | 
|---|
| 193 |     for(vector<const atom*>::const_iterator it = atoms.begin(); it != atoms.end(); it++) {
 | 
|---|
| 194 |       *file << (*it)->getType()->getSymbol();
 | 
|---|
| 195 |       *file << "\t" << printCoordinate((*it)->atStep(0, step));
 | 
|---|
| 196 |       *file << "\t" << printCoordinate((*it)->atStep(1, step));
 | 
|---|
| 197 |       *file << "\t" << printCoordinate((*it)->atStep(2, step));
 | 
|---|
| 198 |       *file << endl;
 | 
|---|
| 199 |     }
 | 
|---|
| 200 |   }
 | 
|---|
| 201 | }
 | 
|---|