1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2011 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /** \file molecules.cpp
|
---|
9 | *
|
---|
10 | * Functions for the class molecule.
|
---|
11 | *
|
---|
12 | */
|
---|
13 |
|
---|
14 | // include config.h
|
---|
15 | #ifdef HAVE_CONFIG_H
|
---|
16 | #include <config.h>
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | #include "CodePatterns/MemDebug.hpp"
|
---|
20 |
|
---|
21 | #include <cstring>
|
---|
22 | #include <boost/bind.hpp>
|
---|
23 | #include <boost/foreach.hpp>
|
---|
24 |
|
---|
25 | #include <gsl/gsl_inline.h>
|
---|
26 | #include <gsl/gsl_heapsort.h>
|
---|
27 |
|
---|
28 | #include "atom.hpp"
|
---|
29 | #include "Bond/bond.hpp"
|
---|
30 | #include "Box.hpp"
|
---|
31 | #include "CodePatterns/enumeration.hpp"
|
---|
32 | #include "CodePatterns/Log.hpp"
|
---|
33 | #include "config.hpp"
|
---|
34 | #include "Element/element.hpp"
|
---|
35 | #include "Graph/BondGraph.hpp"
|
---|
36 | #include "LinearAlgebra/Exceptions.hpp"
|
---|
37 | #include "LinearAlgebra/leastsquaremin.hpp"
|
---|
38 | #include "LinearAlgebra/Plane.hpp"
|
---|
39 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
40 | #include "LinearAlgebra/Vector.hpp"
|
---|
41 | #include "linkedcell.hpp"
|
---|
42 | #include "molecule.hpp"
|
---|
43 | #include "Element/periodentafel.hpp"
|
---|
44 | #include "Tesselation/tesselation.hpp"
|
---|
45 | #include "World.hpp"
|
---|
46 | #include "WorldTime.hpp"
|
---|
47 |
|
---|
48 |
|
---|
49 | /************************************* Functions for class molecule *********************************/
|
---|
50 |
|
---|
51 | /** Constructor of class molecule.
|
---|
52 | * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
|
---|
53 | */
|
---|
54 | molecule::molecule(const periodentafel * const teil) :
|
---|
55 | Observable("molecule"),
|
---|
56 | elemente(teil),
|
---|
57 | MDSteps(0),
|
---|
58 | NoNonHydrogen(0),
|
---|
59 | NoNonBonds(0),
|
---|
60 | NoCyclicBonds(0),
|
---|
61 | ActiveFlag(false),
|
---|
62 | IndexNr(-1),
|
---|
63 | AtomCount(this,boost::bind(&molecule::doCountAtoms,this),"AtomCount"),
|
---|
64 | BondCount(this,boost::bind(&molecule::doCountBonds,this),"BondCount"),
|
---|
65 | last_atom(0)
|
---|
66 | {
|
---|
67 |
|
---|
68 | strcpy(name,World::getInstance().getDefaultName().c_str());
|
---|
69 | };
|
---|
70 |
|
---|
71 | molecule *NewMolecule(){
|
---|
72 | return new molecule(World::getInstance().getPeriode());
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Destructor of class molecule.
|
---|
76 | * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
|
---|
77 | */
|
---|
78 | molecule::~molecule()
|
---|
79 | {
|
---|
80 | CleanupMolecule();
|
---|
81 | };
|
---|
82 |
|
---|
83 |
|
---|
84 | void DeleteMolecule(molecule *mol){
|
---|
85 | delete mol;
|
---|
86 | }
|
---|
87 |
|
---|
88 | // getter and setter
|
---|
89 | const std::string molecule::getName() const{
|
---|
90 | return std::string(name);
|
---|
91 | }
|
---|
92 |
|
---|
93 | int molecule::getAtomCount() const{
|
---|
94 | return *AtomCount;
|
---|
95 | }
|
---|
96 |
|
---|
97 | int molecule::getBondCount() const{
|
---|
98 | return *BondCount;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void molecule::setName(const std::string _name){
|
---|
102 | OBSERVE;
|
---|
103 | cout << "Set name of molecule " << getId() << " to " << _name << endl;
|
---|
104 | strncpy(name,_name.c_str(),MAXSTRINGSIZE);
|
---|
105 | }
|
---|
106 |
|
---|
107 | bool molecule::changeId(moleculeId_t newId){
|
---|
108 | // first we move ourselves in the world
|
---|
109 | // the world lets us know if that succeeded
|
---|
110 | if(World::getInstance().changeMoleculeId(id,newId,this)){
|
---|
111 | id = newId;
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 | else{
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | moleculeId_t molecule::getId() const {
|
---|
121 | return id;
|
---|
122 | }
|
---|
123 |
|
---|
124 | void molecule::setId(moleculeId_t _id){
|
---|
125 | id =_id;
|
---|
126 | }
|
---|
127 |
|
---|
128 | const Formula &molecule::getFormula() const {
|
---|
129 | return formula;
|
---|
130 | }
|
---|
131 |
|
---|
132 | unsigned int molecule::getElementCount() const{
|
---|
133 | return formula.getElementCount();
|
---|
134 | }
|
---|
135 |
|
---|
136 | bool molecule::hasElement(const element *element) const{
|
---|
137 | return formula.hasElement(element);
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool molecule::hasElement(atomicNumber_t Z) const{
|
---|
141 | return formula.hasElement(Z);
|
---|
142 | }
|
---|
143 |
|
---|
144 | bool molecule::hasElement(const string &shorthand) const{
|
---|
145 | return formula.hasElement(shorthand);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /************************** Access to the List of Atoms ****************/
|
---|
149 |
|
---|
150 |
|
---|
151 | molecule::iterator molecule::begin(){
|
---|
152 | return iterator(atomIds.begin(), FromIdToAtom());
|
---|
153 | }
|
---|
154 |
|
---|
155 | molecule::const_iterator molecule::begin() const{
|
---|
156 | return const_iterator(atomIds.begin(), FromIdToAtom());
|
---|
157 | }
|
---|
158 |
|
---|
159 | molecule::iterator molecule::end(){
|
---|
160 | return iterator(atomIds.end(), FromIdToAtom());
|
---|
161 | }
|
---|
162 |
|
---|
163 | molecule::const_iterator molecule::end() const{
|
---|
164 | return const_iterator(atomIds.end(), FromIdToAtom());
|
---|
165 | }
|
---|
166 |
|
---|
167 | bool molecule::empty() const
|
---|
168 | {
|
---|
169 | return (atomIds.empty());
|
---|
170 | }
|
---|
171 |
|
---|
172 | size_t molecule::size() const
|
---|
173 | {
|
---|
174 | size_t counter = 0;
|
---|
175 | for (molecule::const_iterator iter = begin(); iter != end (); ++iter)
|
---|
176 | counter++;
|
---|
177 | return counter;
|
---|
178 | }
|
---|
179 |
|
---|
180 | molecule::const_iterator molecule::erase( const_iterator loc )
|
---|
181 | {
|
---|
182 | OBSERVE;
|
---|
183 | molecule::const_iterator iter = loc;
|
---|
184 | ++iter;
|
---|
185 | atom* atom = *loc;
|
---|
186 | atomIds.erase( atom->getId() );
|
---|
187 | formula-=atom->getType();
|
---|
188 | atom->removeFromMolecule();
|
---|
189 | return iter;
|
---|
190 | }
|
---|
191 |
|
---|
192 | molecule::const_iterator molecule::erase( atom * key )
|
---|
193 | {
|
---|
194 | OBSERVE;
|
---|
195 | molecule::const_iterator iter = find(key);
|
---|
196 | if (iter != end()){
|
---|
197 | ++iter;
|
---|
198 | atomIds.erase( key->getId() );
|
---|
199 | formula-=key->getType();
|
---|
200 | key->removeFromMolecule();
|
---|
201 | }
|
---|
202 | return iter;
|
---|
203 | }
|
---|
204 |
|
---|
205 | molecule::const_iterator molecule::find ( atom * key ) const
|
---|
206 | {
|
---|
207 | return const_iterator(atomIds.find(key->getId()), FromIdToAtom());
|
---|
208 | }
|
---|
209 |
|
---|
210 | pair<molecule::iterator,bool> molecule::insert ( atom * const key )
|
---|
211 | {
|
---|
212 | OBSERVE;
|
---|
213 | pair<atomIdSet::iterator,bool> res = atomIds.insert(key->getId());
|
---|
214 | if (res.second) { // push atom if went well
|
---|
215 | formula+=key->getType();
|
---|
216 | return pair<iterator,bool>(iterator(res.first, FromIdToAtom()),res.second);
|
---|
217 | } else {
|
---|
218 | return pair<iterator,bool>(end(),res.second);
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | bool molecule::containsAtom(atom* key){
|
---|
223 | return (find(key) != end());
|
---|
224 | }
|
---|
225 |
|
---|
226 | World::AtomComposite molecule::getAtomSet() const
|
---|
227 | {
|
---|
228 | World::AtomComposite vector_of_atoms;
|
---|
229 | // std::copy(MyIter(atomIds.begin(), FromIdToAtom()),
|
---|
230 | // MyIter(atomIds.end(), FromIdToAtom()),
|
---|
231 | // vector_of_atoms.begin());
|
---|
232 | // for (MyIter iter = MyIter(atomIds.begin(), FromIdToAtom());
|
---|
233 | // iter != MyIter(atomIds.end(), FromIdToAtom());
|
---|
234 | // ++iter)
|
---|
235 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter)
|
---|
236 | vector_of_atoms.push_back(*iter);
|
---|
237 | return vector_of_atoms;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /** Adds given atom \a *pointer from molecule list.
|
---|
241 | * Increases molecule::last_atom and gives last number to added atom and names it according to its element::abbrev and molecule::AtomCount
|
---|
242 | * \param *pointer allocated and set atom
|
---|
243 | * \return true - succeeded, false - atom not found in list
|
---|
244 | */
|
---|
245 | bool molecule::AddAtom(atom *pointer)
|
---|
246 | {
|
---|
247 | OBSERVE;
|
---|
248 | if (pointer != NULL) {
|
---|
249 | if (pointer->getType() != NULL) {
|
---|
250 | if (pointer->getType()->getAtomicNumber() != 1)
|
---|
251 | NoNonHydrogen++;
|
---|
252 | if(pointer->getName() == "Unknown"){
|
---|
253 | stringstream sstr;
|
---|
254 | sstr << pointer->getType()->getSymbol() << pointer->getNr()+1;
|
---|
255 | pointer->setName(sstr.str());
|
---|
256 | }
|
---|
257 | }
|
---|
258 | insert(pointer);
|
---|
259 | pointer->setMolecule(this);
|
---|
260 | }
|
---|
261 | return true;
|
---|
262 | };
|
---|
263 |
|
---|
264 | /** Adds a copy of the given atom \a *pointer from molecule list.
|
---|
265 | * Increases molecule::last_atom and gives last number to added atom.
|
---|
266 | * \param *pointer allocated and set atom
|
---|
267 | * \return pointer to the newly added atom
|
---|
268 | */
|
---|
269 | atom * molecule::AddCopyAtom(atom *pointer)
|
---|
270 | {
|
---|
271 | atom *retval = NULL;
|
---|
272 | OBSERVE;
|
---|
273 | if (pointer != NULL) {
|
---|
274 | atom *walker = pointer->clone();
|
---|
275 | walker->setName(pointer->getName());
|
---|
276 | walker->setNr(last_atom++); // increase number within molecule
|
---|
277 | insert(walker);
|
---|
278 | if ((pointer->getType() != NULL) && (pointer->getType()->getAtomicNumber() != 1))
|
---|
279 | NoNonHydrogen++;
|
---|
280 | walker->setMolecule(this);
|
---|
281 | retval=walker;
|
---|
282 | }
|
---|
283 | return retval;
|
---|
284 | };
|
---|
285 |
|
---|
286 | /** Adds a Hydrogen atom in replacement for the given atom \a *partner in bond with a *origin.
|
---|
287 | * Here, we have to distinguish between single, double or triple bonds as stated by \a BondDegree, that each demand
|
---|
288 | * a different scheme when adding \a *replacement atom for the given one.
|
---|
289 | * -# Single Bond: Simply add new atom with bond distance rescaled to typical hydrogen one
|
---|
290 | * -# Double Bond: Here, we need the **BondList of the \a *origin atom, by scanning for the other bonds instead of
|
---|
291 | * *Bond, we use the through these connected atoms to determine the plane they lie in, vector::MakeNormalvector().
|
---|
292 | * The orthonormal vector to this plane along with the vector in *Bond direction determines the plane the two
|
---|
293 | * replacing hydrogens shall lie in. Now, all remains to do is take the usual hydrogen double bond angle for the
|
---|
294 | * element of *origin and form the sin/cos admixture of both plane vectors for the new coordinates of the two
|
---|
295 | * hydrogens forming this angle with *origin.
|
---|
296 | * -# Triple Bond: The idea is to set up a tetraoid (C1-H1-H2-H3) (however the lengths \f$b\f$ of the sides of the base
|
---|
297 | * triangle formed by the to be added hydrogens are not equal to the typical bond distance \f$l\f$ but have to be
|
---|
298 | * determined from the typical angle \f$\alpha\f$ for a hydrogen triple connected to the element of *origin):
|
---|
299 | * We have the height \f$d\f$ as the vector in *Bond direction (from triangle C1-H1-H2).
|
---|
300 | * \f[ h = l \cdot \cos{\left (\frac{\alpha}{2} \right )} \qquad b = 2l \cdot \sin{\left (\frac{\alpha}{2} \right)} \quad \rightarrow \quad d = l \cdot \sqrt{\cos^2{\left (\frac{\alpha}{2} \right)}-\frac{1}{3}\cdot\sin^2{\left (\frac{\alpha}{2}\right )}}
|
---|
301 | * \f]
|
---|
302 | * vector::GetNormalvector() creates one orthonormal vector from this *Bond vector and vector::MakeNormalvector creates
|
---|
303 | * the third one from the former two vectors. The latter ones form the plane of the base triangle mentioned above.
|
---|
304 | * The lengths for these are \f$f\f$ and \f$g\f$ (from triangle H1-H2-(center of H1-H2-H3)) with knowledge that
|
---|
305 | * the median lines in an isosceles triangle meet in the center point with a ratio 2:1.
|
---|
306 | * \f[ f = \frac{b}{\sqrt{3}} \qquad g = \frac{b}{2}
|
---|
307 | * \f]
|
---|
308 | * as the coordination of all three atoms in the coordinate system of these three vectors:
|
---|
309 | * \f$\pmatrix{d & f & 0}\f$, \f$\pmatrix{d & -0.5 \cdot f & g}\f$ and \f$\pmatrix{d & -0.5 \cdot f & -g}\f$.
|
---|
310 | *
|
---|
311 | * \param *out output stream for debugging
|
---|
312 | * \param *Bond pointer to bond between \a *origin and \a *replacement
|
---|
313 | * \param *TopOrigin son of \a *origin of upper level molecule (the atom added to this molecule as a copy of \a *origin)
|
---|
314 | * \param *origin pointer to atom which acts as the origin for scaling the added hydrogen to correct bond length
|
---|
315 | * \param *replacement pointer to the atom which shall be copied as a hydrogen atom in this molecule
|
---|
316 | * \param isAngstroem whether the coordination of the given atoms is in AtomicLength (false) or Angstrom(true)
|
---|
317 | * \return number of atoms added, if < bond::BondDegree then something went wrong
|
---|
318 | * \todo double and triple bonds splitting (always use the tetraeder angle!)
|
---|
319 | */
|
---|
320 | bool molecule::AddHydrogenReplacementAtom(bond *TopBond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem)
|
---|
321 | {
|
---|
322 | // Info info(__func__);
|
---|
323 | bool AllWentWell = true; // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
|
---|
324 | OBSERVE;
|
---|
325 | double bondlength; // bond length of the bond to be replaced/cut
|
---|
326 | double bondangle; // bond angle of the bond to be replaced/cut
|
---|
327 | double BondRescale; // rescale value for the hydrogen bond length
|
---|
328 | bond *FirstBond = NULL, *SecondBond = NULL; // Other bonds in double bond case to determine "other" plane
|
---|
329 | atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added
|
---|
330 | double b,l,d,f,g, alpha, factors[NDIM]; // hold temporary values in triple bond case for coordination determination
|
---|
331 | Vector Orthovector1, Orthovector2; // temporary vectors in coordination construction
|
---|
332 | Vector InBondvector; // vector in direction of *Bond
|
---|
333 | const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
|
---|
334 | bond *Binder = NULL;
|
---|
335 |
|
---|
336 | // create vector in direction of bond
|
---|
337 | InBondvector = TopReplacement->getPosition() - TopOrigin->getPosition();
|
---|
338 | bondlength = InBondvector.Norm();
|
---|
339 |
|
---|
340 | // is greater than typical bond distance? Then we have to correct periodically
|
---|
341 | // the problem is not the H being out of the box, but InBondvector have the wrong direction
|
---|
342 | // due to TopReplacement or Origin being on the wrong side!
|
---|
343 | const BondGraph * const BG = World::getInstance().getBondGraph();
|
---|
344 | const range<double> MinMaxBondDistance(
|
---|
345 | BG->getMinMaxDistance(TopOrigin,TopReplacement));
|
---|
346 | if (!MinMaxBondDistance.isInRange(bondlength)) {
|
---|
347 | // LOG(4, "InBondvector is: " << InBondvector << ".");
|
---|
348 | Orthovector1.Zero();
|
---|
349 | for (int i=NDIM;i--;) {
|
---|
350 | l = TopReplacement->at(i) - TopOrigin->at(i);
|
---|
351 | if (fabs(l) > MinMaxBondDistance.last) { // is component greater than bond distance (check against min not useful here)
|
---|
352 | Orthovector1[i] = (l < 0) ? -1. : +1.;
|
---|
353 | } // (signs are correct, was tested!)
|
---|
354 | }
|
---|
355 | Orthovector1 *= matrix;
|
---|
356 | InBondvector -= Orthovector1; // subtract just the additional translation
|
---|
357 | bondlength = InBondvector.Norm();
|
---|
358 | // LOG(4, "INFO: Corrected InBondvector is now: " << InBondvector << ".");
|
---|
359 | } // periodic correction finished
|
---|
360 |
|
---|
361 | InBondvector.Normalize();
|
---|
362 | // get typical bond length and store as scale factor for later
|
---|
363 | ASSERT(TopOrigin->getType() != NULL, "AddHydrogenReplacementAtom: element of TopOrigin is not given.");
|
---|
364 | BondRescale = TopOrigin->getType()->getHBondDistance(TopBond->BondDegree-1);
|
---|
365 | if (BondRescale == -1) {
|
---|
366 | ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->getName() << "<->" << TopReplacement->getName() << ") of degree " << TopBond->BondDegree << "!");
|
---|
367 | return false;
|
---|
368 | BondRescale = bondlength;
|
---|
369 | } else {
|
---|
370 | if (!IsAngstroem)
|
---|
371 | BondRescale /= (1.*AtomicLengthToAngstroem);
|
---|
372 | }
|
---|
373 |
|
---|
374 | // discern single, double and triple bonds
|
---|
375 | switch(TopBond->BondDegree) {
|
---|
376 | case 1:
|
---|
377 | FirstOtherAtom = World::getInstance().createAtom(); // new atom
|
---|
378 | FirstOtherAtom->setType(1); // element is Hydrogen
|
---|
379 | FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
380 | FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
381 | if (TopReplacement->getType()->getAtomicNumber() == 1) { // neither rescale nor replace if it's already hydrogen
|
---|
382 | FirstOtherAtom->father = TopReplacement;
|
---|
383 | BondRescale = bondlength;
|
---|
384 | } else {
|
---|
385 | FirstOtherAtom->father = NULL; // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father
|
---|
386 | }
|
---|
387 | InBondvector *= BondRescale; // rescale the distance vector to Hydrogen bond length
|
---|
388 | FirstOtherAtom->setPosition(TopOrigin->getPosition() + InBondvector); // set coordination to origin and add distance vector to replacement atom
|
---|
389 | AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
|
---|
390 | // LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
|
---|
391 | Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
|
---|
392 | Binder->Cyclic = false;
|
---|
393 | Binder->Type = GraphEdge::TreeEdge;
|
---|
394 | break;
|
---|
395 | case 2:
|
---|
396 | {
|
---|
397 | // determine two other bonds (warning if there are more than two other) plus valence sanity check
|
---|
398 | const BondList& ListOfBonds = TopOrigin->getListOfBonds();
|
---|
399 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
400 | Runner != ListOfBonds.end();
|
---|
401 | ++Runner) {
|
---|
402 | if ((*Runner) != TopBond) {
|
---|
403 | if (FirstBond == NULL) {
|
---|
404 | FirstBond = (*Runner);
|
---|
405 | FirstOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
|
---|
406 | } else if (SecondBond == NULL) {
|
---|
407 | SecondBond = (*Runner);
|
---|
408 | SecondOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
|
---|
409 | } else {
|
---|
410 | ELOG(2, "Detected more than four bonds for atom " << TopOrigin->getName());
|
---|
411 | }
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }
|
---|
415 | if (SecondOtherAtom == NULL) { // then we have an atom with valence four, but only 3 bonds: one to replace and one which is TopBond (third is FirstBond)
|
---|
416 | SecondBond = TopBond;
|
---|
417 | SecondOtherAtom = TopReplacement;
|
---|
418 | }
|
---|
419 | if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all
|
---|
420 | // LOG(3, "Regarding the double bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") to be constructed: Taking " << FirstOtherAtom->Name << " and " << SecondOtherAtom->Name << " along with " << TopOrigin->Name << " to determine orthogonal plane.");
|
---|
421 |
|
---|
422 | // determine the plane of these two with the *origin
|
---|
423 | try {
|
---|
424 | Orthovector1 = Plane(TopOrigin->getPosition(), FirstOtherAtom->getPosition(), SecondOtherAtom->getPosition()).getNormal();
|
---|
425 | }
|
---|
426 | catch(LinearDependenceException &excp){
|
---|
427 | LOG(0, boost::diagnostic_information(excp));
|
---|
428 | // TODO: figure out what to do with the Orthovector in this case
|
---|
429 | AllWentWell = false;
|
---|
430 | }
|
---|
431 | } else {
|
---|
432 | Orthovector1.GetOneNormalVector(InBondvector);
|
---|
433 | }
|
---|
434 | //LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
|
---|
435 | // orthogonal vector and bond vector between origin and replacement form the new plane
|
---|
436 | Orthovector1.MakeNormalTo(InBondvector);
|
---|
437 | Orthovector1.Normalize();
|
---|
438 | //LOG(3, "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << ".");
|
---|
439 |
|
---|
440 | // create the two Hydrogens ...
|
---|
441 | FirstOtherAtom = World::getInstance().createAtom();
|
---|
442 | SecondOtherAtom = World::getInstance().createAtom();
|
---|
443 | FirstOtherAtom->setType(1);
|
---|
444 | SecondOtherAtom->setType(1);
|
---|
445 | FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
446 | FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
447 | SecondOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
448 | SecondOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
449 | FirstOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
450 | SecondOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
451 | bondangle = TopOrigin->getType()->getHBondAngle(1);
|
---|
452 | if (bondangle == -1) {
|
---|
453 | ELOG(1, "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->getName() << "<->" << TopReplacement->getName() << ") of degree " << TopBond->BondDegree << "!");
|
---|
454 | return false;
|
---|
455 | bondangle = 0;
|
---|
456 | }
|
---|
457 | bondangle *= M_PI/180./2.;
|
---|
458 | // LOG(3, "INFO: ReScaleCheck: InBondvector " << InBondvector << ", " << Orthovector1 << ".");
|
---|
459 | // LOG(3, "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle));
|
---|
460 | FirstOtherAtom->Zero();
|
---|
461 | SecondOtherAtom->Zero();
|
---|
462 | for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction)
|
---|
463 | FirstOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (sin(bondangle)));
|
---|
464 | SecondOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (-sin(bondangle)));
|
---|
465 | }
|
---|
466 | FirstOtherAtom->Scale(BondRescale); // rescale by correct BondDistance
|
---|
467 | SecondOtherAtom->Scale(BondRescale);
|
---|
468 | //LOG(3, "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << ".");
|
---|
469 | *FirstOtherAtom += TopOrigin->getPosition();
|
---|
470 | *SecondOtherAtom += TopOrigin->getPosition();
|
---|
471 | // ... and add to molecule
|
---|
472 | AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
|
---|
473 | AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
|
---|
474 | // LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
|
---|
475 | // LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
|
---|
476 | Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
|
---|
477 | Binder->Cyclic = false;
|
---|
478 | Binder->Type = GraphEdge::TreeEdge;
|
---|
479 | Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
|
---|
480 | Binder->Cyclic = false;
|
---|
481 | Binder->Type = GraphEdge::TreeEdge;
|
---|
482 | break;
|
---|
483 | case 3:
|
---|
484 | // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid)
|
---|
485 | FirstOtherAtom = World::getInstance().createAtom();
|
---|
486 | SecondOtherAtom = World::getInstance().createAtom();
|
---|
487 | ThirdOtherAtom = World::getInstance().createAtom();
|
---|
488 | FirstOtherAtom->setType(1);
|
---|
489 | SecondOtherAtom->setType(1);
|
---|
490 | ThirdOtherAtom->setType(1);
|
---|
491 | FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
492 | FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
493 | SecondOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
494 | SecondOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
495 | ThirdOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
496 | ThirdOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
497 | FirstOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
498 | SecondOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
499 | ThirdOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
500 |
|
---|
501 | // we need to vectors orthonormal the InBondvector
|
---|
502 | AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(InBondvector);
|
---|
503 | // LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
|
---|
504 | try{
|
---|
505 | Orthovector2 = Plane(InBondvector, Orthovector1,0).getNormal();
|
---|
506 | }
|
---|
507 | catch(LinearDependenceException &excp) {
|
---|
508 | LOG(0, boost::diagnostic_information(excp));
|
---|
509 | AllWentWell = false;
|
---|
510 | }
|
---|
511 | // LOG(3, "INFO: Orthovector2: " << Orthovector2 << ".")
|
---|
512 |
|
---|
513 | // create correct coordination for the three atoms
|
---|
514 | alpha = (TopOrigin->getType()->getHBondAngle(2))/180.*M_PI/2.; // retrieve triple bond angle from database
|
---|
515 | l = BondRescale; // desired bond length
|
---|
516 | b = 2.*l*sin(alpha); // base length of isosceles triangle
|
---|
517 | d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.); // length for InBondvector
|
---|
518 | f = b/sqrt(3.); // length for Orthvector1
|
---|
519 | g = b/2.; // length for Orthvector2
|
---|
520 | // LOG(3, "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", ");
|
---|
521 | // LOG(3, "The three Bond lengths: " << sqrt(d*d+f*f) << ", " << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g) << ", " << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g));
|
---|
522 | factors[0] = d;
|
---|
523 | factors[1] = f;
|
---|
524 | factors[2] = 0.;
|
---|
525 | FirstOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
|
---|
526 | factors[1] = -0.5*f;
|
---|
527 | factors[2] = g;
|
---|
528 | SecondOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
|
---|
529 | factors[2] = -g;
|
---|
530 | ThirdOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
|
---|
531 |
|
---|
532 | // rescale each to correct BondDistance
|
---|
533 | // FirstOtherAtom->x.Scale(&BondRescale);
|
---|
534 | // SecondOtherAtom->x.Scale(&BondRescale);
|
---|
535 | // ThirdOtherAtom->x.Scale(&BondRescale);
|
---|
536 |
|
---|
537 | // and relative to *origin atom
|
---|
538 | *FirstOtherAtom += TopOrigin->getPosition();
|
---|
539 | *SecondOtherAtom += TopOrigin->getPosition();
|
---|
540 | *ThirdOtherAtom += TopOrigin->getPosition();
|
---|
541 |
|
---|
542 | // ... and add to molecule
|
---|
543 | AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
|
---|
544 | AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
|
---|
545 | AllWentWell = AllWentWell && AddAtom(ThirdOtherAtom);
|
---|
546 | // LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
|
---|
547 | // LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
|
---|
548 | // LOG(4, "INFO: Added " << *ThirdOtherAtom << " at: " << ThirdOtherAtom->x << ".");
|
---|
549 | Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
|
---|
550 | Binder->Cyclic = false;
|
---|
551 | Binder->Type = GraphEdge::TreeEdge;
|
---|
552 | Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
|
---|
553 | Binder->Cyclic = false;
|
---|
554 | Binder->Type = GraphEdge::TreeEdge;
|
---|
555 | Binder = AddBond(BottomOrigin, ThirdOtherAtom, 1);
|
---|
556 | Binder->Cyclic = false;
|
---|
557 | Binder->Type = GraphEdge::TreeEdge;
|
---|
558 | break;
|
---|
559 | default:
|
---|
560 | ELOG(1, "BondDegree does not state single, double or triple bond!");
|
---|
561 | AllWentWell = false;
|
---|
562 | break;
|
---|
563 | }
|
---|
564 |
|
---|
565 | return AllWentWell;
|
---|
566 | };
|
---|
567 |
|
---|
568 | /** Adds given atom \a *pointer from molecule list.
|
---|
569 | * Increases molecule::last_atom and gives last number to added atom.
|
---|
570 | * \param filename name and path of xyz file
|
---|
571 | * \return true - succeeded, false - file not found
|
---|
572 | */
|
---|
573 | bool molecule::AddXYZFile(string filename)
|
---|
574 | {
|
---|
575 |
|
---|
576 | istringstream *input = NULL;
|
---|
577 | int NumberOfAtoms = 0; // atom number in xyz read
|
---|
578 | int i; // loop variables
|
---|
579 | atom *Walker = NULL; // pointer to added atom
|
---|
580 | char shorthand[3]; // shorthand for atom name
|
---|
581 | ifstream xyzfile; // xyz file
|
---|
582 | string line; // currently parsed line
|
---|
583 | double x[3]; // atom coordinates
|
---|
584 |
|
---|
585 | xyzfile.open(filename.c_str());
|
---|
586 | if (!xyzfile)
|
---|
587 | return false;
|
---|
588 |
|
---|
589 | OBSERVE;
|
---|
590 | getline(xyzfile,line,'\n'); // Read numer of atoms in file
|
---|
591 | input = new istringstream(line);
|
---|
592 | *input >> NumberOfAtoms;
|
---|
593 | LOG(0, "Parsing " << NumberOfAtoms << " atoms in file.");
|
---|
594 | getline(xyzfile,line,'\n'); // Read comment
|
---|
595 | LOG(1, "Comment: " << line);
|
---|
596 |
|
---|
597 | if (MDSteps == 0) // no atoms yet present
|
---|
598 | MDSteps++;
|
---|
599 | for(i=0;i<NumberOfAtoms;i++){
|
---|
600 | Walker = World::getInstance().createAtom();
|
---|
601 | getline(xyzfile,line,'\n');
|
---|
602 | istringstream *item = new istringstream(line);
|
---|
603 | //istringstream input(line);
|
---|
604 | //LOG(1, "Reading: " << line);
|
---|
605 | *item >> shorthand;
|
---|
606 | *item >> x[0];
|
---|
607 | *item >> x[1];
|
---|
608 | *item >> x[2];
|
---|
609 | Walker->setType(elemente->FindElement(shorthand));
|
---|
610 | if (Walker->getType() == NULL) {
|
---|
611 | ELOG(1, "Could not parse the element at line: '" << line << "', setting to H.");
|
---|
612 | Walker->setType(1);
|
---|
613 | }
|
---|
614 |
|
---|
615 | Walker->setPosition(Vector(x));
|
---|
616 | Walker->setPositionAtStep(MDSteps-1, Vector(x));
|
---|
617 | Walker->setAtomicVelocityAtStep(MDSteps-1, zeroVec);
|
---|
618 | Walker->setAtomicForceAtStep(MDSteps-1, zeroVec);
|
---|
619 | AddAtom(Walker); // add to molecule
|
---|
620 | delete(item);
|
---|
621 | }
|
---|
622 | xyzfile.close();
|
---|
623 | delete(input);
|
---|
624 | return true;
|
---|
625 | };
|
---|
626 |
|
---|
627 | /** Creates a copy of this molecule.
|
---|
628 | * \return copy of molecule
|
---|
629 | */
|
---|
630 | molecule *molecule::CopyMolecule() const
|
---|
631 | {
|
---|
632 | molecule *copy = World::getInstance().createMolecule();
|
---|
633 |
|
---|
634 | // copy all atoms
|
---|
635 | std::map< const atom *, atom *> FatherFinder;
|
---|
636 | for (const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
637 | atom *const copy_atom = copy->AddCopyAtom(*iter);
|
---|
638 | FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
|
---|
639 | }
|
---|
640 |
|
---|
641 | // copy all bonds
|
---|
642 | for(const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
|
---|
643 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
644 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
645 | BondRunner != ListOfBonds.end();
|
---|
646 | ++BondRunner)
|
---|
647 | if ((*BondRunner)->leftatom == *AtomRunner) {
|
---|
648 | bond *Binder = (*BondRunner);
|
---|
649 | // get the pendant atoms of current bond in the copy molecule
|
---|
650 | ASSERT(FatherFinder.count(Binder->leftatom),
|
---|
651 | "molecule::CopyMolecule() - No copy of original left atom for bond copy found");
|
---|
652 | ASSERT(FatherFinder.count(Binder->rightatom),
|
---|
653 | "molecule::CopyMolecule() - No copy of original right atom for bond copy found");
|
---|
654 | atom * const LeftAtom = FatherFinder[Binder->leftatom];
|
---|
655 | atom * const RightAtom = FatherFinder[Binder->rightatom];
|
---|
656 |
|
---|
657 | bond * const NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->BondDegree);
|
---|
658 | NewBond->Cyclic = Binder->Cyclic;
|
---|
659 | if (Binder->Cyclic)
|
---|
660 | copy->NoCyclicBonds++;
|
---|
661 | NewBond->Type = Binder->Type;
|
---|
662 | }
|
---|
663 | }
|
---|
664 | // correct fathers
|
---|
665 | //for_each(begin(),end(),mem_fun(&atom::CorrectFather));
|
---|
666 |
|
---|
667 | return copy;
|
---|
668 | };
|
---|
669 |
|
---|
670 |
|
---|
671 | /** Destroys all atoms inside this molecule.
|
---|
672 | */
|
---|
673 | void molecule::removeAtomsinMolecule()
|
---|
674 | {
|
---|
675 | // remove each atom from world
|
---|
676 | for(const_iterator AtomRunner = begin(); !empty(); AtomRunner = begin())
|
---|
677 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
678 | };
|
---|
679 |
|
---|
680 |
|
---|
681 | /**
|
---|
682 | * Copies all atoms of a molecule which are within the defined parallelepiped.
|
---|
683 | *
|
---|
684 | * @param offest for the origin of the parallelepiped
|
---|
685 | * @param three vectors forming the matrix that defines the shape of the parallelpiped
|
---|
686 | */
|
---|
687 | molecule* molecule::CopyMoleculeFromSubRegion(const Shape ®ion) const {
|
---|
688 | molecule *copy = World::getInstance().createMolecule();
|
---|
689 |
|
---|
690 | // copy all atoms
|
---|
691 | std::map< const atom *, atom *> FatherFinder;
|
---|
692 | for (const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
693 | if((*iter)->IsInShape(region)){
|
---|
694 | atom *const copy_atom = copy->AddCopyAtom(*iter);
|
---|
695 | FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
|
---|
696 | }
|
---|
697 | }
|
---|
698 |
|
---|
699 | // copy all bonds
|
---|
700 | for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
|
---|
701 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
702 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
703 | BondRunner != ListOfBonds.end();
|
---|
704 | ++BondRunner)
|
---|
705 | if ((*BondRunner)->leftatom == *AtomRunner) {
|
---|
706 | bond *Binder = (*BondRunner);
|
---|
707 | if ((FatherFinder.count(Binder->leftatom))
|
---|
708 | && (FatherFinder.count(Binder->rightatom))) {
|
---|
709 | // if copy present, then it must be from subregion
|
---|
710 | atom * const LeftAtom = FatherFinder[Binder->leftatom];
|
---|
711 | atom * const RightAtom = FatherFinder[Binder->rightatom];
|
---|
712 |
|
---|
713 | bond * const NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->BondDegree);
|
---|
714 | NewBond->Cyclic = Binder->Cyclic;
|
---|
715 | if (Binder->Cyclic)
|
---|
716 | copy->NoCyclicBonds++;
|
---|
717 | NewBond->Type = Binder->Type;
|
---|
718 | }
|
---|
719 | }
|
---|
720 | }
|
---|
721 | // correct fathers
|
---|
722 | //for_each(begin(),end(),mem_fun(&atom::CorrectFather));
|
---|
723 |
|
---|
724 | //TODO: copy->BuildInducedSubgraph(this);
|
---|
725 |
|
---|
726 | return copy;
|
---|
727 | }
|
---|
728 |
|
---|
729 | /** Adds a bond to a the molecule specified by two atoms, \a *first and \a *second.
|
---|
730 | * Also updates molecule::BondCount and molecule::NoNonBonds.
|
---|
731 | * \param *first first atom in bond
|
---|
732 | * \param *second atom in bond
|
---|
733 | * \return pointer to bond or NULL on failure
|
---|
734 | */
|
---|
735 | bond * molecule::AddBond(atom *atom1, atom *atom2, int degree)
|
---|
736 | {
|
---|
737 | OBSERVE;
|
---|
738 | bond *Binder = NULL;
|
---|
739 |
|
---|
740 | // some checks to make sure we are able to create the bond
|
---|
741 | ASSERT(atom1, "First atom in bond-creation was an invalid pointer");
|
---|
742 | ASSERT(atom2, "Second atom in bond-creation was an invalid pointer");
|
---|
743 | ASSERT(FindAtom(atom1->getNr()),"First atom in bond-creation was not part of molecule");
|
---|
744 | ASSERT(FindAtom(atom2->getNr()),"Second atom in bond-creation was not part of molecule");
|
---|
745 |
|
---|
746 | Binder = new bond(atom1, atom2, degree);
|
---|
747 | atom1->RegisterBond(WorldTime::getTime(), Binder);
|
---|
748 | atom2->RegisterBond(WorldTime::getTime(), Binder);
|
---|
749 | if ((atom1->getType() != NULL) && (atom1->getType()->getAtomicNumber() != 1) && (atom2->getType() != NULL) && (atom2->getType()->getAtomicNumber() != 1))
|
---|
750 | NoNonBonds++;
|
---|
751 |
|
---|
752 | return Binder;
|
---|
753 | };
|
---|
754 |
|
---|
755 | /** Remove bond from bond chain list and from the both atom::ListOfBonds.
|
---|
756 | * Bond::~Bond takes care of bond removal
|
---|
757 | * \param *pointer bond pointer
|
---|
758 | * \return true - bound found and removed, false - bond not found/removed
|
---|
759 | */
|
---|
760 | bool molecule::RemoveBond(bond *pointer)
|
---|
761 | {
|
---|
762 | //ELOG(1, "molecule::RemoveBond: Function not implemented yet.");
|
---|
763 | delete(pointer);
|
---|
764 | return true;
|
---|
765 | };
|
---|
766 |
|
---|
767 | /** Remove every bond from bond chain list that atom \a *BondPartner is a constituent of.
|
---|
768 | * \todo Function not implemented yet
|
---|
769 | * \param *BondPartner atom to be removed
|
---|
770 | * \return true - bounds found and removed, false - bonds not found/removed
|
---|
771 | */
|
---|
772 | bool molecule::RemoveBonds(atom *BondPartner)
|
---|
773 | {
|
---|
774 | //ELOG(1, "molecule::RemoveBond: Function not implemented yet.");
|
---|
775 | BondPartner->removeAllBonds();
|
---|
776 | return false;
|
---|
777 | };
|
---|
778 |
|
---|
779 | /** Set molecule::name from the basename without suffix in the given \a *filename.
|
---|
780 | * \param *filename filename
|
---|
781 | */
|
---|
782 | void molecule::SetNameFromFilename(const char *filename)
|
---|
783 | {
|
---|
784 | int length = 0;
|
---|
785 | const char *molname = strrchr(filename, '/');
|
---|
786 | if (molname != NULL)
|
---|
787 | molname += sizeof(char); // search for filename without dirs
|
---|
788 | else
|
---|
789 | molname = filename; // contains no slashes
|
---|
790 | const char *endname = strchr(molname, '.');
|
---|
791 | if ((endname == NULL) || (endname < molname))
|
---|
792 | length = strlen(molname);
|
---|
793 | else
|
---|
794 | length = strlen(molname) - strlen(endname);
|
---|
795 | cout << "Set name of molecule " << getId() << " to " << molname << endl;
|
---|
796 | strncpy(name, molname, length);
|
---|
797 | name[length]='\0';
|
---|
798 | };
|
---|
799 |
|
---|
800 | /** Sets the molecule::cell_size to the components of \a *dim (rectangular box)
|
---|
801 | * \param *dim vector class
|
---|
802 | */
|
---|
803 | void molecule::SetBoxDimension(Vector *dim)
|
---|
804 | {
|
---|
805 | RealSpaceMatrix domain;
|
---|
806 | for(int i =0; i<NDIM;++i)
|
---|
807 | domain.at(i,i) = dim->at(i);
|
---|
808 | World::getInstance().setDomain(domain);
|
---|
809 | };
|
---|
810 |
|
---|
811 | /** Removes atom from molecule list and removes all of its bonds.
|
---|
812 | * \param *pointer atom to be removed
|
---|
813 | * \return true - succeeded, false - atom not found in list
|
---|
814 | */
|
---|
815 | bool molecule::RemoveAtom(atom *pointer)
|
---|
816 | {
|
---|
817 | ASSERT(pointer, "Null pointer passed to molecule::RemoveAtom().");
|
---|
818 | OBSERVE;
|
---|
819 | RemoveBonds(pointer);
|
---|
820 | pointer->removeFromMolecule();
|
---|
821 | return true;
|
---|
822 | };
|
---|
823 |
|
---|
824 | /** Removes atom from molecule list, but does not delete it.
|
---|
825 | * \param *pointer atom to be removed
|
---|
826 | * \return true - succeeded, false - atom not found in list
|
---|
827 | */
|
---|
828 | bool molecule::UnlinkAtom(atom *pointer)
|
---|
829 | {
|
---|
830 | if (pointer == NULL)
|
---|
831 | return false;
|
---|
832 | pointer->removeFromMolecule();
|
---|
833 | return true;
|
---|
834 | };
|
---|
835 |
|
---|
836 | /** Removes every atom from molecule list.
|
---|
837 | * \return true - succeeded, false - atom not found in list
|
---|
838 | */
|
---|
839 | bool molecule::CleanupMolecule()
|
---|
840 | {
|
---|
841 | for (molecule::iterator iter = begin(); !empty(); iter = begin())
|
---|
842 | (*iter)->removeFromMolecule();
|
---|
843 | return empty();
|
---|
844 | };
|
---|
845 |
|
---|
846 | /** Finds an atom specified by its continuous number.
|
---|
847 | * \param Nr number of atom withim molecule
|
---|
848 | * \return pointer to atom or NULL
|
---|
849 | */
|
---|
850 | atom * molecule::FindAtom(int Nr) const
|
---|
851 | {
|
---|
852 | molecule::const_iterator iter = begin();
|
---|
853 | for (; iter != end(); ++iter)
|
---|
854 | if ((*iter)->getNr() == Nr)
|
---|
855 | break;
|
---|
856 | if (iter != end()) {
|
---|
857 | //LOG(0, "Found Atom Nr. " << walker->getNr());
|
---|
858 | return (*iter);
|
---|
859 | } else {
|
---|
860 | LOG(0, "Atom not found in list.");
|
---|
861 | return NULL;
|
---|
862 | }
|
---|
863 | };
|
---|
864 |
|
---|
865 | /** Asks for atom number, and checks whether in list.
|
---|
866 | * \param *text question before entering
|
---|
867 | */
|
---|
868 | atom * molecule::AskAtom(string text)
|
---|
869 | {
|
---|
870 | int No;
|
---|
871 | atom *ion = NULL;
|
---|
872 | do {
|
---|
873 | //std::cout << "============Atom list==========================" << std::endl;
|
---|
874 | //mol->Output((ofstream *)&cout);
|
---|
875 | //std::cout << "===============================================" << std::endl;
|
---|
876 | std::cout << text;
|
---|
877 | cin >> No;
|
---|
878 | ion = this->FindAtom(No);
|
---|
879 | } while (ion == NULL);
|
---|
880 | return ion;
|
---|
881 | };
|
---|
882 |
|
---|
883 | /** Checks if given coordinates are within cell volume.
|
---|
884 | * \param *x array of coordinates
|
---|
885 | * \return true - is within, false - out of cell
|
---|
886 | */
|
---|
887 | bool molecule::CheckBounds(const Vector *x) const
|
---|
888 | {
|
---|
889 | const RealSpaceMatrix &domain = World::getInstance().getDomain().getM();
|
---|
890 | bool result = true;
|
---|
891 | for (int i=0;i<NDIM;i++) {
|
---|
892 | result = result && ((x->at(i) >= 0) && (x->at(i) < domain.at(i,i)));
|
---|
893 | }
|
---|
894 | //return result;
|
---|
895 | return true; /// probably not gonna use the check no more
|
---|
896 | };
|
---|
897 |
|
---|
898 | /** Prints molecule to *out.
|
---|
899 | * \param *out output stream
|
---|
900 | */
|
---|
901 | bool molecule::Output(ostream * const output) const
|
---|
902 | {
|
---|
903 | if (output == NULL) {
|
---|
904 | return false;
|
---|
905 | } else {
|
---|
906 | int AtomNo[MAX_ELEMENTS];
|
---|
907 | memset(AtomNo,0,(MAX_ELEMENTS-1)*sizeof(*AtomNo));
|
---|
908 | enumeration<const element*> elementLookup = formula.enumerateElements();
|
---|
909 | *output << "#Ion_TypeNr._Nr.R[0] R[1] R[2] MoveType (0 MoveIon, 1 FixedIon)" << endl;
|
---|
910 | for_each(begin(),end(),boost::bind(&atom::OutputArrayIndexed,_1,output,elementLookup,AtomNo,(const char*)0));
|
---|
911 | return true;
|
---|
912 | }
|
---|
913 | };
|
---|
914 |
|
---|
915 | /** Prints molecule with all atomic trajectory positions to *out.
|
---|
916 | * \param *out output stream
|
---|
917 | */
|
---|
918 | bool molecule::OutputTrajectories(ofstream * const output) const
|
---|
919 | {
|
---|
920 | if (output == NULL) {
|
---|
921 | return false;
|
---|
922 | } else {
|
---|
923 | for (int step = 0; step < MDSteps; step++) {
|
---|
924 | if (step == 0) {
|
---|
925 | *output << "#Ion_TypeNr._Nr.R[0] R[1] R[2] MoveType (0 MoveIon, 1 FixedIon)" << endl;
|
---|
926 | } else {
|
---|
927 | *output << "# ====== MD step " << step << " =========" << endl;
|
---|
928 | }
|
---|
929 | int AtomNo[MAX_ELEMENTS];
|
---|
930 | memset(AtomNo,0,(MAX_ELEMENTS-1)*sizeof(*AtomNo));
|
---|
931 | enumeration<const element*> elementLookup = formula.enumerateElements();
|
---|
932 | for_each(begin(),end(),boost::bind(&atom::OutputTrajectory,_1,output,elementLookup, AtomNo, (const int)step));
|
---|
933 | }
|
---|
934 | return true;
|
---|
935 | }
|
---|
936 | };
|
---|
937 |
|
---|
938 | /** Outputs contents of each atom::ListOfBonds.
|
---|
939 | * \param *out output stream
|
---|
940 | */
|
---|
941 | void molecule::OutputListOfBonds() const
|
---|
942 | {
|
---|
943 | std::stringstream output;
|
---|
944 | LOG(2, "From Contents of ListOfBonds, all atoms:");
|
---|
945 | for (molecule::const_iterator iter = begin();
|
---|
946 | iter != end();
|
---|
947 | ++iter) {
|
---|
948 | (*iter)->OutputBondOfAtom(output);
|
---|
949 | output << std::endl << "\t\t";
|
---|
950 | }
|
---|
951 | LOG(2, output.str());
|
---|
952 | }
|
---|
953 |
|
---|
954 | /** Output of element before the actual coordination list.
|
---|
955 | * \param *out stream pointer
|
---|
956 | */
|
---|
957 | bool molecule::Checkout(ofstream * const output) const
|
---|
958 | {
|
---|
959 | return formula.checkOut(output);
|
---|
960 | };
|
---|
961 |
|
---|
962 | /** Prints molecule with all its trajectories to *out as xyz file.
|
---|
963 | * \param *out output stream
|
---|
964 | */
|
---|
965 | bool molecule::OutputTrajectoriesXYZ(ofstream * const output)
|
---|
966 | {
|
---|
967 | time_t now;
|
---|
968 |
|
---|
969 | if (output != NULL) {
|
---|
970 | now = time((time_t *)NULL); // Get the system time and put it into 'now' as 'calender time'
|
---|
971 | for (int step=0;step<MDSteps;step++) {
|
---|
972 | *output << getAtomCount() << "\n\tCreated by molecuilder, step " << step << ", on " << ctime(&now);
|
---|
973 | for_each(begin(),end(),boost::bind(&atom::OutputTrajectoryXYZ,_1,output,step));
|
---|
974 | }
|
---|
975 | return true;
|
---|
976 | } else
|
---|
977 | return false;
|
---|
978 | };
|
---|
979 |
|
---|
980 | /** Prints molecule to *out as xyz file.
|
---|
981 | * \param *out output stream
|
---|
982 | */
|
---|
983 | bool molecule::OutputXYZ(ofstream * const output) const
|
---|
984 | {
|
---|
985 | time_t now;
|
---|
986 |
|
---|
987 | if (output != NULL) {
|
---|
988 | now = time((time_t *)NULL); // Get the system time and put it into 'now' as 'calender time'
|
---|
989 | *output << getAtomCount() << "\n\tCreated by molecuilder on " << ctime(&now);
|
---|
990 | for_each(begin(),end(),bind2nd(mem_fun(&atom::OutputXYZLine),output));
|
---|
991 | return true;
|
---|
992 | } else
|
---|
993 | return false;
|
---|
994 | };
|
---|
995 |
|
---|
996 | /** Brings molecule::AtomCount and atom::*Name up-to-date.
|
---|
997 | * \param *out output stream for debugging
|
---|
998 | */
|
---|
999 | int molecule::doCountAtoms()
|
---|
1000 | {
|
---|
1001 | int res = size();
|
---|
1002 | int i = 0;
|
---|
1003 | NoNonHydrogen = 0;
|
---|
1004 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
1005 | (*iter)->setNr(i); // update number in molecule (for easier referencing in FragmentMolecule lateron)
|
---|
1006 | if ((*iter)->getType()->getAtomicNumber() != 1) // count non-hydrogen atoms whilst at it
|
---|
1007 | NoNonHydrogen++;
|
---|
1008 | stringstream sstr;
|
---|
1009 | sstr << (*iter)->getType()->getSymbol() << (*iter)->getNr()+1;
|
---|
1010 | (*iter)->setName(sstr.str());
|
---|
1011 | LOG(3, "Naming atom nr. " << (*iter)->getNr() << " " << (*iter)->getName() << ".");
|
---|
1012 | i++;
|
---|
1013 | }
|
---|
1014 | return res;
|
---|
1015 | };
|
---|
1016 |
|
---|
1017 | /** Counts the number of present bonds.
|
---|
1018 | * \return number of bonds
|
---|
1019 | */
|
---|
1020 | int molecule::doCountBonds() const
|
---|
1021 | {
|
---|
1022 | unsigned int counter = 0;
|
---|
1023 | for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
|
---|
1024 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
1025 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
1026 | BondRunner != ListOfBonds.end();
|
---|
1027 | ++BondRunner)
|
---|
1028 | if ((*BondRunner)->leftatom == *AtomRunner)
|
---|
1029 | counter++;
|
---|
1030 | }
|
---|
1031 | return counter;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 |
|
---|
1035 | /** Returns an index map for two father-son-molecules.
|
---|
1036 | * The map tells which atom in this molecule corresponds to which one in the other molecul with their fathers.
|
---|
1037 | * \param *out output stream for debugging
|
---|
1038 | * \param *OtherMolecule corresponding molecule with fathers
|
---|
1039 | * \return allocated map of size molecule::AtomCount with map
|
---|
1040 | * \todo make this with a good sort O(n), not O(n^2)
|
---|
1041 | */
|
---|
1042 | int * molecule::GetFatherSonAtomicMap(molecule *OtherMolecule)
|
---|
1043 | {
|
---|
1044 | LOG(3, "Begin of GetFatherAtomicMap.");
|
---|
1045 | int *AtomicMap = new int[getAtomCount()];
|
---|
1046 | for (int i=getAtomCount();i--;)
|
---|
1047 | AtomicMap[i] = -1;
|
---|
1048 | if (OtherMolecule == this) { // same molecule
|
---|
1049 | for (int i=getAtomCount();i--;) // no need as -1 means already that there is trivial correspondence
|
---|
1050 | AtomicMap[i] = i;
|
---|
1051 | LOG(4, "Map is trivial.");
|
---|
1052 | } else {
|
---|
1053 | std::stringstream output;
|
---|
1054 | output << "Map is ";
|
---|
1055 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
1056 | if ((*iter)->father == NULL) {
|
---|
1057 | AtomicMap[(*iter)->getNr()] = -2;
|
---|
1058 | } else {
|
---|
1059 | for (molecule::const_iterator runner = OtherMolecule->begin(); runner != OtherMolecule->end(); ++runner) {
|
---|
1060 | //for (int i=0;i<AtomCount;i++) { // search atom
|
---|
1061 | //for (int j=0;j<OtherMolecule->getAtomCount();j++) {
|
---|
1062 | //LOG(4, "Comparing father " << (*iter)->father << " with the other one " << (*runner)->father << ".");
|
---|
1063 | if ((*iter)->father == (*runner))
|
---|
1064 | AtomicMap[(*iter)->getNr()] = (*runner)->getNr();
|
---|
1065 | }
|
---|
1066 | }
|
---|
1067 | output << AtomicMap[(*iter)->getNr()] << "\t";
|
---|
1068 | }
|
---|
1069 | LOG(4, output.str());
|
---|
1070 | }
|
---|
1071 | LOG(3, "End of GetFatherAtomicMap.");
|
---|
1072 | return AtomicMap;
|
---|
1073 | };
|
---|
1074 |
|
---|
1075 |
|
---|
1076 | void molecule::flipActiveFlag(){
|
---|
1077 | ActiveFlag = !ActiveFlag;
|
---|
1078 | }
|
---|