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