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