source: src/molecule.cpp@ ba94c5

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

Removed modules graph.[ch]pp.

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