source: src/Fragmentation/Exporters/SaturatedFragment.cpp@ f5fa48

Last change on this file since f5fa48 was f5fa48, checked in by Frederik Heber <heber@…>, 11 years ago

SaturatedFragment can deal with a global saturation position map.

  • so far, we create an empty one in FragmentationAction such that nothing's changed for the moment.
  • similarly in StoreSaturatedFragmentAction. However, there this is intended as only local information is required (it's only a single fragment).
  • Property mode set to 100644
File size: 27.1 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2013 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/*
25 * SaturatedFragment.cpp
26 *
27 * Created on: Mar 3, 2013
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include "CodePatterns/MemDebug.hpp"
37
38#include "SaturatedFragment.hpp"
39
40#include <algorithm>
41#include <cmath>
42#include <iostream>
43
44#include "CodePatterns/Assert.hpp"
45#include "CodePatterns/Log.hpp"
46
47#include "LinearAlgebra/Exceptions.hpp"
48#include "LinearAlgebra/Plane.hpp"
49#include "LinearAlgebra/RealSpaceMatrix.hpp"
50#include "LinearAlgebra/Vector.hpp"
51
52#include "Atom/atom.hpp"
53#include "Bond/bond.hpp"
54#include "config.hpp"
55#include "Descriptors/AtomIdDescriptor.hpp"
56#include "Fragmentation/Exporters/HydrogenPool.hpp"
57#include "Fragmentation/Exporters/SphericalPointDistribution.hpp"
58#include "Fragmentation/HydrogenSaturation_enum.hpp"
59#include "Graph/BondGraph.hpp"
60#include "World.hpp"
61
62SaturatedFragment::SaturatedFragment(
63 const KeySet &_set,
64 KeySetsInUse_t &_container,
65 HydrogenPool &_hydrogens,
66 const enum HydrogenTreatment _treatment,
67 const enum HydrogenSaturation _saturation,
68 const GlobalSaturationPositions_t &_globalsaturationpositions) :
69 container(_container),
70 set(_set),
71 hydrogens(_hydrogens),
72 FullMolecule(set),
73 treatment(_treatment),
74 saturation(_saturation)
75{
76 // add to in-use container
77 ASSERT( container.find(set) == container.end(),
78 "SaturatedFragment::SaturatedFragment() - the set "
79 +toString(set)+" is already marked as in use.");
80 container.insert(set);
81
82 // prepare saturation hydrogens, either using global information
83 // or if not given, local information (created in the function)
84 if (_globalsaturationpositions.empty())
85 saturate();
86 else
87 saturate(_globalsaturationpositions);
88}
89
90SaturatedFragment::~SaturatedFragment()
91{
92 // release all saturation hydrogens if present
93 for (KeySet::iterator iter = SaturationHydrogens.begin();
94 !SaturationHydrogens.empty();
95 iter = SaturationHydrogens.begin()) {
96 hydrogens.releaseHydrogen(*iter);
97 SaturationHydrogens.erase(iter);
98 }
99
100 // remove ourselves from in-use container
101 KeySetsInUse_t::iterator iter = container.find(set);
102 ASSERT( container.find(set) != container.end(),
103 "SaturatedFragment::SaturatedFragment() - the set "
104 +toString(set)+" is not marked as in use.");
105 container.erase(iter);
106}
107
108typedef std::vector<atom *> atoms_t;
109
110atoms_t gatherAllAtoms(const KeySet &_FullMolecule)
111{
112 atoms_t atoms;
113 for (KeySet::const_iterator iter = _FullMolecule.begin();
114 iter != _FullMolecule.end();
115 ++iter) {
116 atom * const Walker = World::getInstance().getAtom(AtomById(*iter));
117 ASSERT( Walker != NULL,
118 "gatherAllAtoms() - id "
119 +toString(*iter)+" is unknown to World.");
120 atoms.push_back(Walker);
121 }
122 LOG(4, "DEBUG: We have gathered the following atoms: " << atoms);
123
124 return atoms;
125}
126
127typedef std::map<atom *, BondList > CutBonds_t;
128
129CutBonds_t gatherCutBonds(
130 const atoms_t &_atoms,
131 const KeySet &_set,
132 const enum HydrogenTreatment _treatment,
133 KeySet &_FullMolecule)
134{
135 // bool LonelyFlag = false;
136 CutBonds_t CutBonds;
137 for (atoms_t::const_iterator iter = _atoms.begin();
138 iter != _atoms.end();
139 ++iter) {
140 atom * const Walker = *iter;
141 // start with an empty list
142 CutBonds.insert( std::make_pair(Walker, BondList() ));
143
144 // go through all bonds
145 const BondList& ListOfBonds = Walker->getListOfBonds();
146 for (BondList::const_iterator BondRunner = ListOfBonds.begin();
147 BondRunner != ListOfBonds.end();
148 ++BondRunner) {
149 atom * const OtherWalker = (*BondRunner)->GetOtherAtom(Walker);
150 // if other atom is in key set
151 if (_set.find(OtherWalker->getId()) != _set.end()) {
152 LOG(4, "DEBUG: Walker " << *Walker << " is bound to " << *OtherWalker << ".");
153// if (OtherWalker->getId() > Walker->getId()) { // add bond (Nr check is for adding only one of both variants: ab, ba)
154//// std::stringstream output;
155//// output << "ACCEPT: Adding Bond: "
156// output << Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->getDegree());
157//// LOG(3, output.str());
158// //NumBonds[(*iter)->getNr()]++;
159// } else {
160//// LOG(3, "REJECY: Not adding bond, labels in wrong order.");
161// }
162// LonelyFlag = false;
163 } else {
164 LOG(4, "DEBUG: Walker " << *Walker << " is bound to "
165 << *OtherWalker << ", who is not in this fragment molecule.");
166 if ((_treatment == ExcludeHydrogen) && (OtherWalker->getElementNo() == (atomicNumber_t)1)) {
167 LOG(4, "REJECT: " << *OtherWalker << " is a hydrogen, that are excluded from the set.");
168 _FullMolecule.insert(OtherWalker->getId());
169 } else {
170 LOG(3, "ACCEPT: Adding " << **BondRunner << " as a cut bond.");
171 // there is always at least an empty list
172 CutBonds[Walker].push_back(*BondRunner);
173 }
174 }
175 }
176 }
177 LOG(4, "DEBUG: We have gathered the following CutBonds: " << CutBonds);
178
179 return CutBonds;
180}
181
182void SaturatedFragment::saturate()
183{
184 // so far, we just have a set of keys. Hence, convert to atom refs
185 // and gather all atoms in a vector
186 std::vector<atom *> atoms = gatherAllAtoms(FullMolecule);
187
188 // go through each atom of the fragment and gather all cut bonds in list
189 CutBonds_t CutBonds = gatherCutBonds(atoms, set, treatment, FullMolecule);
190
191 // go through all cut bonds and replace with a hydrogen
192 if (saturation == DoSaturate) {
193 for (CutBonds_t::const_iterator atomiter = CutBonds.begin();
194 atomiter != CutBonds.end(); ++atomiter) {
195 atom * const Walker = atomiter->first;
196 LOG(4, "DEBUG: We are now saturating " << *Walker);
197
198 if (!saturateAtom(Walker, atomiter->second))
199 exit(1);
200 }
201 } else
202 LOG(3, "INFO: We are not saturating cut bonds.");
203}
204
205void SaturatedFragment::saturate(
206 const GlobalSaturationPositions_t &_globalsaturationpositions)
207{
208 // so far, we just have a set of keys. Hence, convert to atom refs
209 // and gather all atoms in a vector
210 std::vector<atom *> atoms = gatherAllAtoms(FullMolecule);
211
212 // go through each atom of the fragment and gather all cut bonds in list
213 CutBonds_t CutBonds = gatherCutBonds(atoms, set, treatment, FullMolecule);
214
215 // go through all cut bonds and replace with a hydrogen
216 if (saturation == DoSaturate) {
217 for (CutBonds_t::const_iterator atomiter = CutBonds.begin();
218 atomiter != CutBonds.end(); ++atomiter) {
219 atom * const Walker = atomiter->first;
220 LOG(4, "DEBUG: We are now saturating dangling bonds of " << *Walker);
221
222 // gather set of positions for this atom from global map
223 GlobalSaturationPositions_t::const_iterator mapiter =
224 _globalsaturationpositions.find(Walker->getId());
225 ASSERT( mapiter != _globalsaturationpositions.end(),
226 "SaturatedFragment::saturate() - no global information for "
227 +toString(*Walker));
228 const SaturationsPositionsPerNeighbor_t &saturationpositions =
229 mapiter->second;
230
231 // go through all cut bonds for this atom
232 for (BondList::const_iterator bonditer = atomiter->second.begin();
233 bonditer != atomiter->second.end(); ++bonditer) {
234 atom * const OtherWalker = (*bonditer)->GetOtherAtom(Walker);
235
236 // get positions from global map
237 SaturationsPositionsPerNeighbor_t::const_iterator positionsiter =
238 saturationpositions.find(OtherWalker->getId());
239 ASSERT(positionsiter != saturationpositions.end(),
240 "SaturatedFragment::saturate() - no information on bond neighbor "
241 +toString(*OtherWalker)+" to atom "+toString(*Walker));
242 ASSERT(!positionsiter->second.empty(),
243 "SaturatedFragment::saturate() - no positions for saturating bond to"
244 +toString(*OtherWalker)+" to atom "+toString(*Walker));
245
246 // get typical bond distance from elements database
247 double BondDistance = Walker->getType()->getHBondDistance(positionsiter->second.size()-1);
248 if (BondDistance < 0.) {
249 ELOG(2, "saturateAtoms() - no typical hydrogen bond distance of degree "
250 +toString(positionsiter->second.size())+" for element "
251 +toString(Walker->getType()->getName()));
252 // try bond degree 1 distance
253 BondDistance = Walker->getType()->getHBondDistance(1-1);
254 if (BondDistance < 0.) {
255 ELOG(1, "saturateAtoms() - no typical hydrogen bond distance for element "
256 +toString(Walker->getType()->getName()));
257 BondDistance = 1.;
258 }
259 }
260 ASSERT( BondDistance > 0.,
261 "SaturatedFragment::saturate() - negative bond distance");
262
263 // place hydrogen at each point
264 LOG(4, "DEBUG: Places to saturate for atom " << *OtherWalker
265 << " are " << positionsiter->second);
266 atom * const father = Walker;
267 for (SaturationsPositions_t::const_iterator positer = positionsiter->second.begin();
268 positer != positionsiter->second.end(); ++positer) {
269 const atom& hydrogen =
270 setHydrogenReplacement(Walker, *positer, BondDistance, father);
271 FullMolecule.insert(hydrogen.getId());
272 }
273 }
274 }
275 } else
276 LOG(3, "INFO: We are not saturating cut bonds.");
277}
278
279bool SaturatedFragment::saturateAtom(
280 atom * const _atom,
281 const BondList &_cutbonds)
282{
283 // OLD WAY: use AddHydrogenReplacementAtom() on each cut bond
284// // go through each bond and replace
285// for (BondList::const_iterator bonditer = _cutbonds.begin();
286// bonditer != _cutbonds.end(); ++bonditer) {
287// atom * const OtherWalker = (*bonditer)->GetOtherAtom(_atom);
288// if (!AddHydrogenReplacementAtom(
289// (*bonditer),
290// _atom,
291// OtherWalker,
292// World::getInstance().getConfig()->IsAngstroem == 1))
293// return false;
294// }
295
296 SphericalPointDistribution::WeightedPolygon_t Polygon;
297 {
298 // prepare a list of "uncut" bonds via set_difference. For this both lists
299 // have to be sorted.
300 typedef std::vector<bond::ptr> BondVector_t;
301 BondVector_t ListOfBonds(_atom->getListOfBonds().begin(),_atom->getListOfBonds().end());
302 std::sort(ListOfBonds.begin(), ListOfBonds.end());
303 BondVector_t CutBonds(_cutbonds.begin(), _cutbonds.end());
304 std::sort(CutBonds.begin(), CutBonds.end());
305 const BondVector_t::iterator eraseiter = std::set_difference(
306 ListOfBonds.begin(), ListOfBonds.end(),
307 CutBonds.begin(), CutBonds.end(),
308 ListOfBonds.begin());
309 ListOfBonds.erase(eraseiter, ListOfBonds.end());
310
311 // gather the nodes of the shape defined by the current set of bonded atoms
312 for (BondVector_t::const_iterator bonditer = ListOfBonds.begin();
313 bonditer != ListOfBonds.end();
314 ++bonditer) {
315 Vector DistanceVector;
316 if ((*bonditer)->leftatom == _atom)
317 DistanceVector = (*bonditer)->rightatom->getPosition() - (*bonditer)->leftatom->getPosition();
318 else
319 DistanceVector = (*bonditer)->leftatom->getPosition() - (*bonditer)->rightatom->getPosition();
320 // always use unit distances
321 DistanceVector.Normalize();
322 Polygon.push_back( std::make_pair(DistanceVector, (*bonditer)->getDegree()) );
323 }
324 LOG(3, "DEBUG: Polygon of atom " << *_atom << " to saturate is " << Polygon);
325 }
326
327 unsigned int NumberOfPoints = _atom->getElement().getNoValenceOrbitals();
328 LOG(3, "DEBUG: There are " << NumberOfPoints
329 << " places to fill in in total for this atom " << *_atom << ".");
330
331 // get perfect node distribution for the given remaining atoms with respect
332 // to valence of the atoms (for a saturated fragment, resembles number of bonds)
333 // then get the number of vacant spots
334 SphericalPointDistribution polygonizer;
335 SphericalPointDistribution::Polygon_t RemainingPoints =
336 polygonizer.getRemainingPoints(Polygon, NumberOfPoints);
337
338 LOG(3, "INFO: Points identified to fill are " << RemainingPoints);
339
340 // and place hydrogen atoms at each vacant spot in the distance given by the table
341 for(SphericalPointDistribution::Polygon_t::const_iterator iter = RemainingPoints.begin();
342 iter != RemainingPoints.end(); ++iter) {
343 // find nearest atom as father to this point
344 atom * const _father = _atom;
345 LOG(4, "DEBUG: Filling saturation hydrogen for atom " << _atom << " at " << *iter);
346 double BondRescale = _atom->getType()->getHBondDistance(1.);
347 if (BondRescale == -1) {
348 ELOG(1, "There is no typical hydrogen bond distance in replacing bond from atom "
349 << _atom->getName() << " of degree 1!");
350 BondRescale = 1.;
351 }
352 const atom& hydrogen = setHydrogenReplacement(
353 _atom,
354 *iter,
355 BondRescale,
356 _father);
357 FullMolecule.insert(hydrogen.getId());
358 }
359
360 return true;
361}
362
363
364bool SaturatedFragment::OutputConfig(
365 std::ostream &out,
366 const ParserTypes _type) const
367{
368 // gather all atoms in a vector
369 std::vector<atom *> atoms;
370 for (KeySet::const_iterator iter = FullMolecule.begin();
371 iter != FullMolecule.end();
372 ++iter) {
373 atom * const Walker = World::getInstance().getAtom(AtomById(*iter));
374 ASSERT( Walker != NULL,
375 "SaturatedFragment::OutputConfig() - id "
376 +toString(*iter)+" is unknown to World.");
377 atoms.push_back(Walker);
378 }
379
380 // TODO: ScanForPeriodicCorrection() is missing so far!
381 // note however that this is not straight-forward for the following reasons:
382 // - we do not copy all atoms anymore, hence we are forced to shift the real
383 // atoms hither and back again
384 // - we use a long-range potential that supports periodic boundary conditions.
385 // Hence, there we would like the original configuration (split through the
386 // the periodic boundaries). Otherwise, we would have to shift (and probably
387 // interpolate) the potential with OBCs applying.
388
389 // list atoms in fragment for debugging
390 {
391 std::stringstream output;
392 output << "INFO: Contained atoms: ";
393 for (std::vector<atom *>::const_iterator iter = atoms.begin();
394 iter != atoms.end(); ++iter) {
395 output << (*iter)->getName() << " ";
396 }
397 LOG(3, output.str());
398 }
399
400 // store to stream via FragmentParser
401 const bool intermediateResult =
402 FormatParserStorage::getInstance().save(
403 out,
404 FormatParserStorage::getInstance().getSuffixFromType(_type),
405 atoms);
406
407 return intermediateResult;
408}
409
410atom * const SaturatedFragment::getHydrogenReplacement(atom * const replacement)
411{
412 atom * const _atom = hydrogens.leaseHydrogen(); // new atom
413 _atom->setAtomicVelocity(replacement->getAtomicVelocity()); // copy velocity
414 _atom->setFixedIon(replacement->getFixedIon());
415 // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father
416 _atom->father = replacement;
417 SaturationHydrogens.insert(_atom->getId());
418 return _atom;
419}
420
421const atom& SaturatedFragment::setHydrogenReplacement(
422 const atom * const _OwnerAtom,
423 const Vector &_position,
424 const double _distance,
425 atom * const _father)
426{
427 atom * const _atom = hydrogens.leaseHydrogen(); // new atom
428 _atom->setPosition( _OwnerAtom->getPosition() + _distance * _position );
429 // always set as fixed ion (not moving during molecular dynamics simulation)
430 _atom->setFixedIon(true);
431 // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father
432 _atom->father = _father;
433 SaturationHydrogens.insert(_atom->getId());
434 return *_atom;
435}
436
437bool SaturatedFragment::AddHydrogenReplacementAtom(
438 bond::ptr TopBond,
439 atom *Origin,
440 atom *Replacement,
441 bool IsAngstroem)
442{
443// Info info(__func__);
444 bool AllWentWell = true; // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
445 double bondlength; // bond length of the bond to be replaced/cut
446 double bondangle; // bond angle of the bond to be replaced/cut
447 double BondRescale; // rescale value for the hydrogen bond length
448 bond::ptr FirstBond;
449 bond::ptr SecondBond; // Other bonds in double bond case to determine "other" plane
450 atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added
451 double b,l,d,f,g, alpha, factors[NDIM]; // hold temporary values in triple bond case for coordination determination
452 Vector Orthovector1, Orthovector2; // temporary vectors in coordination construction
453 Vector InBondvector; // vector in direction of *Bond
454 const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
455 bond::ptr Binder;
456
457 // create vector in direction of bond
458 InBondvector = Replacement->getPosition() - Origin->getPosition();
459 bondlength = InBondvector.Norm();
460
461 // is greater than typical bond distance? Then we have to correct periodically
462 // the problem is not the H being out of the box, but InBondvector have the wrong direction
463 // due to Replacement or Origin being on the wrong side!
464 const BondGraph * const BG = World::getInstance().getBondGraph();
465 const range<double> MinMaxBondDistance(
466 BG->getMinMaxDistance(Origin,Replacement));
467 if (!MinMaxBondDistance.isInRange(bondlength)) {
468// LOG(4, "InBondvector is: " << InBondvector << ".");
469 Orthovector1.Zero();
470 for (int i=NDIM;i--;) {
471 l = Replacement->at(i) - Origin->at(i);
472 if (fabs(l) > MinMaxBondDistance.last) { // is component greater than bond distance (check against min not useful here)
473 Orthovector1[i] = (l < 0) ? -1. : +1.;
474 } // (signs are correct, was tested!)
475 }
476 Orthovector1 *= matrix;
477 InBondvector -= Orthovector1; // subtract just the additional translation
478 bondlength = InBondvector.Norm();
479// LOG(4, "INFO: Corrected InBondvector is now: " << InBondvector << ".");
480 } // periodic correction finished
481
482 InBondvector.Normalize();
483 // get typical bond length and store as scale factor for later
484 ASSERT(Origin->getType() != NULL,
485 "SaturatedFragment::AddHydrogenReplacementAtom() - element of Origin is not given.");
486 BondRescale = Origin->getType()->getHBondDistance(TopBond->getDegree()-1);
487 if (BondRescale == -1) {
488 ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!");
489 BondRescale = Origin->getType()->getHBondDistance(TopBond->getDegree());
490 if (BondRescale == -1) {
491 ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of any degree!");
492 return false;
493 BondRescale = bondlength;
494 }
495 } else {
496 if (!IsAngstroem)
497 BondRescale /= (1.*AtomicLengthToAngstroem);
498 }
499
500 // discern single, double and triple bonds
501 switch(TopBond->getDegree()) {
502 case 1:
503 // check whether replacement has been an excluded hydrogen
504 if (Replacement->getType()->getAtomicNumber() == HydrogenPool::HYDROGEN) { // neither rescale nor replace if it's already hydrogen
505 FirstOtherAtom = Replacement;
506 BondRescale = bondlength;
507 } else {
508 FirstOtherAtom = getHydrogenReplacement(Replacement);
509 InBondvector *= BondRescale; // rescale the distance vector to Hydrogen bond length
510 FirstOtherAtom->setPosition(Origin->getPosition() + InBondvector); // set coordination to origin and add distance vector to replacement atom
511 }
512 FullMolecule.insert(FirstOtherAtom->getId());
513// LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
514 break;
515 case 2:
516 {
517 // determine two other bonds (warning if there are more than two other) plus valence sanity check
518 const BondList& ListOfBonds = Origin->getListOfBonds();
519 for (BondList::const_iterator Runner = ListOfBonds.begin();
520 Runner != ListOfBonds.end();
521 ++Runner) {
522 if ((*Runner) != TopBond) {
523 if (FirstBond == NULL) {
524 FirstBond = (*Runner);
525 FirstOtherAtom = (*Runner)->GetOtherAtom(Origin);
526 } else if (SecondBond == NULL) {
527 SecondBond = (*Runner);
528 SecondOtherAtom = (*Runner)->GetOtherAtom(Origin);
529 } else {
530 ELOG(2, "Detected more than four bonds for atom " << Origin->getName());
531 }
532 }
533 }
534 }
535 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)
536 SecondBond = TopBond;
537 SecondOtherAtom = Replacement;
538 }
539 if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all
540// LOG(3, "Regarding the double bond (" << Origin->Name << "<->" << Replacement->Name << ") to be constructed: Taking " << FirstOtherAtom->Name << " and " << SecondOtherAtom->Name << " along with " << Origin->Name << " to determine orthogonal plane.");
541
542 // determine the plane of these two with the *origin
543 try {
544 Orthovector1 = Plane(Origin->getPosition(), FirstOtherAtom->getPosition(), SecondOtherAtom->getPosition()).getNormal();
545 }
546 catch(LinearDependenceException &excp){
547 LOG(0, boost::diagnostic_information(excp));
548 // TODO: figure out what to do with the Orthovector in this case
549 AllWentWell = false;
550 }
551 } else {
552 Orthovector1.GetOneNormalVector(InBondvector);
553 }
554 //LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
555 // orthogonal vector and bond vector between origin and replacement form the new plane
556 Orthovector1.MakeNormalTo(InBondvector);
557 Orthovector1.Normalize();
558 //LOG(3, "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << ".");
559
560 // create the two Hydrogens ...
561 FirstOtherAtom = getHydrogenReplacement(Replacement);
562 SecondOtherAtom = getHydrogenReplacement(Replacement);
563 FullMolecule.insert(FirstOtherAtom->getId());
564 FullMolecule.insert(SecondOtherAtom->getId());
565 bondangle = Origin->getType()->getHBondAngle(1);
566 if (bondangle == -1) {
567 ELOG(1, "There is no typical hydrogen bond angle in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!");
568 return false;
569 bondangle = 0;
570 }
571 bondangle *= M_PI/180./2.;
572// LOG(3, "INFO: ReScaleCheck: InBondvector " << InBondvector << ", " << Orthovector1 << ".");
573// LOG(3, "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle));
574 FirstOtherAtom->Zero();
575 SecondOtherAtom->Zero();
576 for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction)
577 FirstOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (sin(bondangle)));
578 SecondOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (-sin(bondangle)));
579 }
580 FirstOtherAtom->Scale(BondRescale); // rescale by correct BondDistance
581 SecondOtherAtom->Scale(BondRescale);
582 //LOG(3, "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << ".");
583 *FirstOtherAtom += Origin->getPosition();
584 *SecondOtherAtom += Origin->getPosition();
585 // ... and add to molecule
586// LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
587// LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
588 break;
589 case 3:
590 // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid)
591 FirstOtherAtom = getHydrogenReplacement(Replacement);
592 SecondOtherAtom = getHydrogenReplacement(Replacement);
593 ThirdOtherAtom = getHydrogenReplacement(Replacement);
594 FullMolecule.insert(FirstOtherAtom->getId());
595 FullMolecule.insert(SecondOtherAtom->getId());
596 FullMolecule.insert(ThirdOtherAtom->getId());
597
598 // we need to vectors orthonormal the InBondvector
599 AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(InBondvector);
600// LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
601 try{
602 Orthovector2 = Plane(InBondvector, Orthovector1,0).getNormal();
603 }
604 catch(LinearDependenceException &excp) {
605 LOG(0, boost::diagnostic_information(excp));
606 AllWentWell = false;
607 }
608// LOG(3, "INFO: Orthovector2: " << Orthovector2 << ".")
609
610 // create correct coordination for the three atoms
611 alpha = (Origin->getType()->getHBondAngle(2))/180.*M_PI/2.; // retrieve triple bond angle from database
612 l = BondRescale; // desired bond length
613 b = 2.*l*sin(alpha); // base length of isosceles triangle
614 d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.); // length for InBondvector
615 f = b/sqrt(3.); // length for Orthvector1
616 g = b/2.; // length for Orthvector2
617// LOG(3, "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", ");
618// 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));
619 factors[0] = d;
620 factors[1] = f;
621 factors[2] = 0.;
622 FirstOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
623 factors[1] = -0.5*f;
624 factors[2] = g;
625 SecondOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
626 factors[2] = -g;
627 ThirdOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
628
629 // rescale each to correct BondDistance
630// FirstOtherAtom->x.Scale(&BondRescale);
631// SecondOtherAtom->x.Scale(&BondRescale);
632// ThirdOtherAtom->x.Scale(&BondRescale);
633
634 // and relative to *origin atom
635 *FirstOtherAtom += Origin->getPosition();
636 *SecondOtherAtom += Origin->getPosition();
637 *ThirdOtherAtom += Origin->getPosition();
638
639 // ... and add to molecule
640// LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
641// LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
642// LOG(4, "INFO: Added " << *ThirdOtherAtom << " at: " << ThirdOtherAtom->x << ".");
643 break;
644 default:
645 ELOG(1, "BondDegree does not state single, double or triple bond!");
646 AllWentWell = false;
647 break;
648 }
649
650 return AllWentWell;
651};
Note: See TracBrowser for help on using the repository browser.