| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * XyzParser.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 2, 2010
 | 
|---|
| 27 |  *      Author: metzler
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <limits>
 | 
|---|
| 38 | #include <vector>
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 41 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "XyzParser.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "Atom/atom.hpp"
 | 
|---|
| 46 | #include "Element/element.hpp"
 | 
|---|
| 47 | #include "Element/periodentafel.hpp"
 | 
|---|
| 48 | #include "molecule.hpp"
 | 
|---|
| 49 | #include "MoleculeListClass.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 |   char commentBuffer[512], type[3];
 | 
|---|
| 84 |   string number;
 | 
|---|
| 85 |   std::vector<atom *> AddedAtoms;
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   // create the molecule
 | 
|---|
| 88 |   newmol = World::getInstance().createMolecule();
 | 
|---|
| 89 |   newmol->ActiveFlag = true;
 | 
|---|
| 90 |   // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
 | 
|---|
| 91 |   World::getInstance().getMolecules()->insert(newmol);
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   // the first line tells number of atoms,
 | 
|---|
| 94 |   // where we need this construct due to skipping of empty lines below
 | 
|---|
| 95 |   file->getline(commentBuffer, 512);
 | 
|---|
| 96 |   unsigned int step = 0;
 | 
|---|
| 97 |   while (file->good()) {
 | 
|---|
| 98 |     std::stringstream numberstream(commentBuffer);
 | 
|---|
| 99 |     numberstream >> numberOfAtoms;
 | 
|---|
| 100 |     if (step == 0)
 | 
|---|
| 101 |       AddedAtoms.resize(numberOfAtoms);
 | 
|---|
| 102 |     // the second line is always a comment
 | 
|---|
| 103 |     file->getline(commentBuffer, 512);
 | 
|---|
| 104 |     comment = commentBuffer;
 | 
|---|
| 105 |     LOG(3, "DEBUG: comment is '" << comment << "'.");
 | 
|---|
| 106 | 
 | 
|---|
| 107 |     for (int i = 0; i < numberOfAtoms; i++) {
 | 
|---|
| 108 |       // parse type and position
 | 
|---|
| 109 |       *file >> type;
 | 
|---|
| 110 |       Vector tempVector;
 | 
|---|
| 111 |       for (int j=0;j<NDIM;j++) {
 | 
|---|
| 112 |         *file >> tempVector[j];
 | 
|---|
| 113 |       }
 | 
|---|
| 114 |       LOG(4, "INFO: Parsed type as " << type << " and position at "
 | 
|---|
| 115 |           << tempVector << " for time step " << step);
 | 
|---|
| 116 | 
 | 
|---|
| 117 |       if (step == 0) {
 | 
|---|
| 118 |         newAtom = World::getInstance().createAtom();
 | 
|---|
| 119 |         newAtom->setType(World::getInstance().getPeriode()->FindElement(type));
 | 
|---|
| 120 |         newmol->AddAtom(newAtom);
 | 
|---|
| 121 |         AddedAtoms[i] = newAtom;
 | 
|---|
| 122 |         LOG(5, "INFO: Created new atom, setting " << i << " th component of AddedAtoms.");
 | 
|---|
| 123 |       } else {
 | 
|---|
| 124 |         newAtom = AddedAtoms[i];
 | 
|---|
| 125 |         LOG(5, "INFO: Using present atom " << *newAtom << " from " << i << " th component of AddedAtoms.");
 | 
|---|
| 126 |         ASSERT(newAtom->getType() == World::getInstance().getPeriode()->FindElement(type),
 | 
|---|
| 127 |             "FormatParser< xyz >::load() - atom has different type "+newAtom->getType()->getSymbol()+" than parsed now "+type+", mixed up order?");
 | 
|---|
| 128 |       }
 | 
|---|
| 129 |       newAtom->setPositionAtStep(step, tempVector);
 | 
|---|
| 130 |     }
 | 
|---|
| 131 |     getline (*file, number);  // eat away rest of last line
 | 
|---|
| 132 |     // skip empty lines
 | 
|---|
| 133 |     unsigned int counter = 0;
 | 
|---|
| 134 |     while (file->good()) {
 | 
|---|
| 135 |       getline (*file, number);
 | 
|---|
| 136 |       LOG(4, "INFO: Skipped line is '" << number << "'");
 | 
|---|
| 137 |       counter++;
 | 
|---|
| 138 |       if (!number.empty())
 | 
|---|
| 139 |         break;
 | 
|---|
| 140 |     }
 | 
|---|
| 141 |     LOG(3, "INFO: I skipped "+toString(counter)+" empty lines.");
 | 
|---|
| 142 |     ++step;
 | 
|---|
| 143 |   }
 | 
|---|
| 144 |   BOOST_FOREACH(atom *_atom, World::getInstance().getAllAtoms())
 | 
|---|
| 145 |     LOG(3, "INFO: Atom " << _atom->getName() << " " << *dynamic_cast<AtomInfo *>(_atom) << ".");
 | 
|---|
| 146 | 
 | 
|---|
| 147 |   // refresh atom::nr and atom::name
 | 
|---|
| 148 |   newmol->getAtomCount();
 | 
|---|
| 149 | }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | /**
 | 
|---|
| 152 |  * Saves the \a atoms into as a XYZ file.
 | 
|---|
| 153 |  *
 | 
|---|
| 154 |  * \param file where to save the state
 | 
|---|
| 155 |  * \param atoms atoms to store
 | 
|---|
| 156 |  */
 | 
|---|
| 157 | void FormatParser< xyz >::save(ostream* file, const std::vector<atom *> &atoms) {
 | 
|---|
| 158 |   LOG(0, "Saving changes to xyz.");
 | 
|---|
| 159 | 
 | 
|---|
| 160 |   // get max and min trajectories
 | 
|---|
| 161 |   size_t min_trajectories = std::numeric_limits<size_t>::max();
 | 
|---|
| 162 |   size_t max_trajectories = std::numeric_limits<size_t>::min();
 | 
|---|
| 163 |   for (std::vector<atom *>::const_iterator iter = atoms.begin();
 | 
|---|
| 164 |       iter != atoms.end();
 | 
|---|
| 165 |       ++iter) {
 | 
|---|
| 166 |     if (max_trajectories < (*iter)->getTrajectorySize())
 | 
|---|
| 167 |       max_trajectories = (*iter)->getTrajectorySize();
 | 
|---|
| 168 |     if (min_trajectories > (*iter)->getTrajectorySize())
 | 
|---|
| 169 |       min_trajectories = (*iter)->getTrajectorySize();
 | 
|---|
| 170 |   }
 | 
|---|
| 171 |   // no atoms? Then, they all have same amount
 | 
|---|
| 172 |   if (atoms.size() == 0)
 | 
|---|
| 173 |     min_trajectories = max_trajectories = 1;
 | 
|---|
| 174 |   ASSERT(min_trajectories == max_trajectories,
 | 
|---|
| 175 |       "FormatParser< xyz >::save() - not all atoms have same number of trajectories: "
 | 
|---|
| 176 |       +toString(min_trajectories)+" != "+toString(max_trajectories)+".");
 | 
|---|
| 177 |   LOG(2, "INFO: There are " << max_trajectories << " steps to save.");
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   // always store at least one step
 | 
|---|
| 180 |   for (size_t step = 0; (step < max_trajectories) || (step == 0); ++step) {
 | 
|---|
| 181 |     if (step != 0)
 | 
|---|
| 182 |       *file << "\n";
 | 
|---|
| 183 |     //if (comment == "") {
 | 
|---|
| 184 |       time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| 185 |       comment = "Created by molecuilder on ";
 | 
|---|
| 186 |       // ctime ends in \n\0, we have to cut away the newline
 | 
|---|
| 187 |       std::string time(ctime(&now));
 | 
|---|
| 188 |       size_t pos = time.find('\n');
 | 
|---|
| 189 |       if (pos != 0)
 | 
|---|
| 190 |         comment += time.substr(0,pos);
 | 
|---|
| 191 |       else
 | 
|---|
| 192 |         comment += time;
 | 
|---|
| 193 |       comment += ", time step "+toString(step);
 | 
|---|
| 194 |     //}
 | 
|---|
| 195 |     *file << atoms.size() << endl << "\t" << comment << endl;
 | 
|---|
| 196 | 
 | 
|---|
| 197 |     for(vector<atom*>::const_iterator it = atoms.begin(); it != atoms.end(); it++) {
 | 
|---|
| 198 |       *file << noshowpoint << (*it)->getType()->getSymbol();
 | 
|---|
| 199 |       *file << "\t" << (*it)->atStep(0, step);
 | 
|---|
| 200 |       *file << "\t" << (*it)->atStep(1, step);
 | 
|---|
| 201 |       *file << "\t" << (*it)->atStep(2, step);
 | 
|---|
| 202 |       *file << endl;
 | 
|---|
| 203 |     }
 | 
|---|
| 204 |   }
 | 
|---|
| 205 | }
 | 
|---|