| 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 "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "Helpers/Log.hpp"
 | 
|---|
| 23 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 24 | #include "XyzParser.hpp"
 | 
|---|
| 25 | #include "World.hpp"
 | 
|---|
| 26 | #include "atom.hpp"
 | 
|---|
| 27 | #include "molecule.hpp"
 | 
|---|
| 28 | #include "element.hpp"
 | 
|---|
| 29 | #include "periodentafel.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | using namespace std;
 | 
|---|
| 32 | 
 | 
|---|
| 33 | /**
 | 
|---|
| 34 |  * Constructor.
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | XyzParser::XyzParser() :
 | 
|---|
| 37 |   comment("")
 | 
|---|
| 38 | {}
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /**
 | 
|---|
| 41 |  * Destructor.
 | 
|---|
| 42 |  */
 | 
|---|
| 43 | XyzParser::~XyzParser() {
 | 
|---|
| 44 | }
 | 
|---|
| 45 | 
 | 
|---|
| 46 | /**
 | 
|---|
| 47 |  * Loads an XYZ file into the World.
 | 
|---|
| 48 |  *
 | 
|---|
| 49 |  * \param XYZ file
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | void XyzParser::load(istream* file) {
 | 
|---|
| 52 |   atom* newAtom = NULL;
 | 
|---|
| 53 |   molecule* newmol = NULL;
 | 
|---|
| 54 |   int numberOfAtoms;
 | 
|---|
| 55 |   char commentBuffer[512], type[3];
 | 
|---|
| 56 |   double tmp;
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   // the first line tells number of atoms, the second line is always a comment
 | 
|---|
| 59 |   *file >> numberOfAtoms >> ws;
 | 
|---|
| 60 |   file->getline(commentBuffer, 512);
 | 
|---|
| 61 |   comment = commentBuffer;
 | 
|---|
| 62 | 
 | 
|---|
| 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 |   for (int i = 0; i < numberOfAtoms; i++) {
 | 
|---|
| 68 |     newAtom = World::getInstance().createAtom();
 | 
|---|
| 69 |     *file >> type;
 | 
|---|
| 70 |     for (int j=0;j<NDIM;j++) {
 | 
|---|
| 71 |       *file >> tmp;
 | 
|---|
| 72 |       newAtom->set(j, tmp);
 | 
|---|
| 73 |     }
 | 
|---|
| 74 |     newAtom->setType(World::getInstance().getPeriode()->FindElement(type));
 | 
|---|
| 75 |     newmol->AddAtom(newAtom);
 | 
|---|
| 76 |   }
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | /**
 | 
|---|
| 80 |  * Saves the current state of the World into the given XYZ file.
 | 
|---|
| 81 |  *
 | 
|---|
| 82 |  * \param XYZ file
 | 
|---|
| 83 |  */
 | 
|---|
| 84 | void XyzParser::save(ostream* file) {
 | 
|---|
| 85 |   DoLog(0) && (Log() << Verbose(0) << "Saving changes to xyz." << std::endl);
 | 
|---|
| 86 |   if (comment == "") {
 | 
|---|
| 87 |     time_t now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
 | 
|---|
| 88 |     comment = "Created by molecuilder on ";
 | 
|---|
| 89 |     // ctime ends in \n\0, we have to cut away the newline
 | 
|---|
| 90 |     std::string time(ctime(&now));
 | 
|---|
| 91 |     size_t pos = time.find('\n');
 | 
|---|
| 92 |     if (pos != 0)
 | 
|---|
| 93 |       comment += time.substr(0,pos);
 | 
|---|
| 94 |     else
 | 
|---|
| 95 |       comment += time;
 | 
|---|
| 96 |   }
 | 
|---|
| 97 |   *file << World::getInstance().numAtoms() << endl << "\t" << comment << endl;
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   vector<atom*> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 100 |   for(vector<atom*>::iterator it = atoms.begin(); it != atoms.end(); it++) {
 | 
|---|
| 101 |     *file << noshowpoint << (*it)->getType()->getSymbol() << "\t" << (*it)->at(0) << "\t" << (*it)->at(1) << "\t" << (*it)->at(2) << endl;
 | 
|---|
| 102 |   }
 | 
|---|
| 103 | }
 | 
|---|