source: src/molecule.cpp@ 701ad6

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 701ad6 was bf3817, checked in by Frederik Heber <heber@…>, 15 years ago

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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