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