source: src/molecule.cpp@ 52ed5b

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 52ed5b was 52ed5b, checked in by Frederik Heber <heber@…>, 13 years ago

Ids handed out by molecule now start with 1.

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