source: src/molecule.cpp@ 79b59b

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 79b59b was 575343, checked in by Frederik Heber <heber@…>, 13 years ago

added OBSERVE to molecule::SetNameFromFilename

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