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