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