| 1 | /*
 | 
|---|
| 2 |  * XyzParser.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Mar 2, 2010
 | 
|---|
| 5 |  *      Author: metzler
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | // include config.h
 | 
|---|
| 9 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 10 | #include <config.h>
 | 
|---|
| 11 | #endif
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include "Helpers/Log.hpp"
 | 
|---|
| 16 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 17 | #include "XyzParser.hpp"
 | 
|---|
| 18 | #include "World.hpp"
 | 
|---|
| 19 | #include "atom.hpp"
 | 
|---|
| 20 | #include "molecule.hpp"
 | 
|---|
| 21 | #include "element.hpp"
 | 
|---|
| 22 | #include "periodentafel.hpp"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | using namespace std;
 | 
|---|
| 25 | 
 | 
|---|
| 26 | /**
 | 
|---|
| 27 |  * Constructor.
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | XyzParser::XyzParser() :
 | 
|---|
| 30 |   comment("")
 | 
|---|
| 31 | {}
 | 
|---|
| 32 | 
 | 
|---|
| 33 | /**
 | 
|---|
| 34 |  * Destructor.
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | XyzParser::~XyzParser() {
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | /**
 | 
|---|
| 40 |  * Loads an XYZ file into the World.
 | 
|---|
| 41 |  *
 | 
|---|
| 42 |  * \param XYZ file
 | 
|---|
| 43 |  */
 | 
|---|
| 44 | void XyzParser::load(istream* file) {
 | 
|---|
| 45 |   atom* newAtom = NULL;
 | 
|---|
| 46 |   molecule* newmol = NULL;
 | 
|---|
| 47 |   int numberOfAtoms;
 | 
|---|
| 48 |   char commentBuffer[512], type[3];
 | 
|---|
| 49 |   double tmp;
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   // the first line tells number of atoms, the second line is always a comment
 | 
|---|
| 52 |   *file >> numberOfAtoms >> ws;
 | 
|---|
| 53 |   file->getline(commentBuffer, 512);
 | 
|---|
| 54 |   comment = commentBuffer;
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   newmol = World::getInstance().createMolecule();
 | 
|---|
| 57 |   newmol->ActiveFlag = true;
 | 
|---|
| 58 |   // TODO: Remove the insertion into molecule when saving does not depend on them anymore. Also, remove molecule.hpp include
 | 
|---|
| 59 |   World::getInstance().getMolecules()->insert(newmol);
 | 
|---|
| 60 |   for (int i = 0; i < numberOfAtoms; i++) {
 | 
|---|
| 61 |     newAtom = World::getInstance().createAtom();
 | 
|---|
| 62 |     *file >> type;
 | 
|---|
| 63 |     for (int j=0;j<NDIM;j++) {
 | 
|---|
| 64 |       *file >> tmp;
 | 
|---|
| 65 |       newAtom->set(j, tmp);
 | 
|---|
| 66 |     }
 | 
|---|
| 67 |     newAtom->setType(World::getInstance().getPeriode()->FindElement(type));
 | 
|---|
| 68 |     newmol->AddAtom(newAtom);
 | 
|---|
| 69 |   }
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | /**
 | 
|---|
| 73 |  * Saves the current state of the World into the given XYZ file.
 | 
|---|
| 74 |  *
 | 
|---|
| 75 |  * \param XYZ file
 | 
|---|
| 76 |  */
 | 
|---|
| 77 | void XyzParser::save(ostream* file) {
 | 
|---|
| 78 |   DoLog(0) && (Log() << Verbose(0) << "Saving changes to xyz." << std::endl);
 | 
|---|
| 79 |   if (comment == "") {
 | 
|---|
| 80 |     time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| 81 |     comment = "Created by molecuilder on ";
 | 
|---|
| 82 |     // ctime ends in \n\0, we have to cut away the newline
 | 
|---|
| 83 |     std::string time(ctime(&now));
 | 
|---|
| 84 |     size_t pos = time.find('\n');
 | 
|---|
| 85 |     if (pos != 0)
 | 
|---|
| 86 |       comment += time.substr(0,pos);
 | 
|---|
| 87 |     else
 | 
|---|
| 88 |       comment += time;
 | 
|---|
| 89 |   }
 | 
|---|
| 90 |   *file << World::getInstance().numAtoms() << endl << "\t" << comment << endl;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   vector<atom*> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 93 |   for(vector<atom*>::iterator it = atoms.begin(); it != atoms.end(); it++) {
 | 
|---|
| 94 |     *file << noshowpoint << (*it)->getType()->getSymbol() << "\t" << (*it)->at(0) << "\t" << (*it)->at(1) << "\t" << (*it)->at(2) << endl;
 | 
|---|
| 95 |   }
 | 
|---|
| 96 | }
 | 
|---|