source: src/atom.cpp@ 60896f

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 60896f was fa7989, checked in by Frederik Heber <heber@…>, 15 years ago

FIX: World::destroyMolecule(mol *) - assert that pointer is not NULL.

  • added docu to some atom member functions.
  • Property mode set to 100644
File size: 11.2 KB
RevLine 
[14de469]1/** \file atom.cpp
[1907a7]2 *
[14de469]3 * Function implementations for the class atom.
[1907a7]4 *
[14de469]5 */
6
[bf3817]7// include config.h
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11
[112b09]12#include "Helpers/MemDebug.hpp"
13
[357fba]14#include "atom.hpp"
[e41951]15#include "bond.hpp"
[4a7776a]16#include "config.hpp"
[f66195]17#include "element.hpp"
[266237]18#include "lists.hpp"
[ccd9f5]19#include "parser.hpp"
[57f243]20#include "LinearAlgebra/Vector.hpp"
[d346b6]21#include "World.hpp"
[6cfa36]22#include "molecule.hpp"
[c550dd]23#include "Shapes/Shape.hpp"
[1907a7]24
[36166d]25#include <iomanip>
[0ba410]26#include <iostream>
[36166d]27
[14de469]28/************************************* Functions for class atom *************************************/
29
[70ff32]30
[14de469]31/** Constructor of class atom.
32 */
[46d958]33atom::atom() :
[97b825]34 father(this),
35 sort(&nr),
36 mol(0)
[d74077]37{};
[14de469]38
[2319ed]39/** Constructor of class atom.
40 */
[46d958]41atom::atom(atom *pointer) :
[97b825]42 ParticleInfo(pointer),
43 father(pointer),
44 sort(&nr)
[2319ed]45{
[d74077]46 setType(pointer->getType()); // copy element of atom
47 setPosition(pointer->getPosition()); // copy coordination
48 AtomicVelocity = pointer->AtomicVelocity; // copy velocity
[2319ed]49 FixedIon = pointer->FixedIon;
[6cfa36]50 mol = 0;
[b453f9]51};
[2319ed]52
[46d958]53atom *atom::clone(){
[68f03d]54 atom *res = new atom(this);
[46d958]55 res->father = this;
[5f612ee]56 res->sort = &res->nr;
[d74077]57 res->setType(getType());
58 res->setPosition(this->getPosition());
59 res->AtomicVelocity = this->AtomicVelocity;
[46d958]60 res->FixedIon = FixedIon;
[6cfa36]61 res->mol = 0;
[23b547]62 World::getInstance().registerAtom(res);
[46d958]63 return res;
64}
65
[2319ed]66
[14de469]67/** Destructor of class atom.
68 */
[1907a7]69atom::~atom()
[14de469]70{
[6cfa36]71 removeFromMolecule();
[a80241]72 for(BondList::iterator iter=ListOfBonds.begin(); iter!=ListOfBonds.end();){
73 // deleting the bond will invalidate the iterator !!!
74 bond *bond =*(iter++);
75 delete(bond);
76 }
[14de469]77};
78
79
80/** Climbs up the father list until NULL, last is returned.
81 * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen)
82 */
83atom *atom::GetTrueFather()
84{
[215df0]85 if(father == this){ // top most father is the one that points on itself
86 return this;
87 }
88 else if(!father) {
89 return 0;
90 }
91 else {
92 return father->GetTrueFather();
93 }
[14de469]94};
95
[e65246]96/** Sets father to itself or its father in case of copying a molecule.
97 */
98void atom::CorrectFather()
99{
100 if (father->father == father) // same atom in copy's father points to itself
101 father = this; // set father to itself (copy of a whole molecule)
102 else
103 father = father->father; // set father to original's father
104
105};
106
107/** Check whether father is equal to given atom.
108 * \param *ptr atom to compare father to
109 * \param **res return value (only set if atom::father is equal to \a *ptr)
110 */
[b453f9]111void atom::EqualsFather ( const atom *ptr, const atom **res ) const
[e65246]112{
113 if ( ptr == father )
114 *res = this;
115};
116
[00abfc]117bool atom::isFather(const atom *ptr){
118 return ptr==father;
119}
120
[e9f8f9]121/** Checks whether atom is within the given box.
122 * \param offset offset to box origin
123 * \param *parallelepiped box matrix
124 * \return true - is inside, false - is not
125 */
[c550dd]126bool atom::IsInShape(const Shape& shape) const
[e9f8f9]127{
[d74077]128 return shape.isInside(getPosition());
[e9f8f9]129};
130
[266237]131/** Counts the number of bonds weighted by bond::BondDegree.
132 * \param bonds times bond::BondDegree
133 */
[4455f4]134int BondedParticle::CountBonds() const
[266237]135{
136 int NoBonds = 0;
137 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
138 NoBonds += (*Runner)->BondDegree;
139 return NoBonds;
140};
141
[b453f9]142/** Output of a single atom with given numbering.
[14de469]143 * \param ElementNo cardinal number of the element
144 * \param AtomNo cardinal number among these atoms of the same element
145 * \param *out stream to output to
[1907a7]146 * \param *comment commentary after '#' sign
[e41951]147 * \return true - \a *out present, false - \a *out is NULL
[14de469]148 */
[e138de]149bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
[14de469]150{
151 if (out != NULL) {
152 *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
[d74077]153 *out << at(0) << "\t" << at(1) << "\t" << at(2);
[943d02]154 *out << "\t" << FixedIon;
[d74077]155 if (AtomicVelocity.Norm() > MYEPSILON)
156 *out << "\t" << scientific << setprecision(6) << AtomicVelocity[0] << "\t" << AtomicVelocity[1] << "\t" << AtomicVelocity[2] << "\t";
[437922]157 if (comment != NULL)
158 *out << " # " << comment << endl;
[e9f8f9]159 else
160 *out << " # molecule nr " << nr << endl;
161 return true;
162 } else
163 return false;
164};
[b453f9]165
166/** Output of a single atom with numbering from array according to atom::type.
167 * \param *ElementNo cardinal number of the element
168 * \param *AtomNo cardinal number among these atoms of the same element
169 * \param *out stream to output to
170 * \param *comment commentary after '#' sign
171 * \return true - \a *out present, false - \a *out is NULL
172 */
[0ba410]173bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
[e9f8f9]174{
[d74077]175 AtomNo[getType()->Z]++; // increment number
[e9f8f9]176 if (out != NULL) {
[8f4df1]177 const element *elemental = getType();
178 cout << "Looking for atom with element " << *elemental << endl;
179 ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
180 *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->Z] << "\t" << fixed << setprecision(9) << showpoint;
[d74077]181 *out << at(0) << "\t" << at(1) << "\t" << at(2);
[e9f8f9]182 *out << "\t" << FixedIon;
[d74077]183 if (AtomicVelocity.Norm() > MYEPSILON)
184 *out << "\t" << scientific << setprecision(6) << AtomicVelocity[0] << "\t" << AtomicVelocity[1] << "\t" << AtomicVelocity[2] << "\t";
[e9f8f9]185 if (comment != NULL)
186 *out << " # " << comment << endl;
[437922]187 else
188 *out << " # molecule nr " << nr << endl;
[14de469]189 return true;
190 } else
191 return false;
192};
193
194/** Output of a single atom as one lin in xyz file.
195 * \param *out stream to output to
[e41951]196 * \return true - \a *out present, false - \a *out is NULL
[14de469]197 */
198bool atom::OutputXYZLine(ofstream *out) const
199{
200 if (out != NULL) {
[b5c53d]201 *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl;
[14de469]202 return true;
203 } else
204 return false;
205};
206
[fcd7b6]207/** Output of a single atom as one lin in xyz file.
208 * \param *out stream to output to
[e41951]209 * \param *ElementNo array with ion type number in the config file this atom's element shall have
210 * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically
211 * \param step Trajectory time step to output
212 * \return true - \a *out present, false - \a *out is NULL
[fcd7b6]213 */
[e138de]214bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const
[fcd7b6]215{
[d74077]216 AtomNo[getType()->Z]++;
[fcd7b6]217 if (out != NULL) {
[d74077]218 *out << "Ion_Type" << ElementNo[getType()->Z] << "_" << AtomNo[getType()->Z] << "\t" << fixed << setprecision(9) << showpoint;
[0a4f7f]219 *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2];
[fcd7b6]220 *out << "\t" << FixedIon;
221 if (Trajectory.U.at(step).Norm() > MYEPSILON)
[0a4f7f]222 *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t";
[fcd7b6]223 if (Trajectory.F.at(step).Norm() > MYEPSILON)
[0a4f7f]224 *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t";
[fcd7b6]225 *out << "\t# Number in molecule " << nr << endl;
226 return true;
227 } else
228 return false;
229};
230
[681a8a]231/** Output of a single atom as one lin in xyz file.
232 * \param *out stream to output to
[e41951]233 * \param step Trajectory time step to output
234 * \return true - \a *out present, false - \a *out is NULL
[681a8a]235 */
[e138de]236bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
[681a8a]237{
238 if (out != NULL) {
[b5c53d]239 *out << getType()->getSymbol() << "\t";
[0a4f7f]240 *out << Trajectory.R.at(step)[0] << "\t";
241 *out << Trajectory.R.at(step)[1] << "\t";
242 *out << Trajectory.R.at(step)[2] << endl;
[681a8a]243 return true;
244 } else
245 return false;
246};
247
[4455f4]248/** Outputs the MPQC configuration line for this atom.
249 * \param *out output stream
250 * \param *center center of molecule subtracted from position
251 * \param *AtomNo pointer to atom counter that is increased by one
252 */
[1b2d30]253void atom::OutputMPQCLine(ostream * const out, const Vector *center, int *AtomNo = NULL) const
[4455f4]254{
[d74077]255 Vector recentered(getPosition());
256 recentered -= *center;
[b5c53d]257 *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl;
[4455f4]258 if (AtomNo != NULL)
259 *AtomNo++;
260};
261
262/** Compares the indices of \a this atom with a given \a ptr.
263 * \param ptr atom to compare index against
264 * \return true - this one's is smaller, false - not
265 */
[b453f9]266bool atom::Compare(const atom &ptr) const
[4455f4]267{
268 if (nr < ptr.nr)
269 return true;
270 else
271 return false;
272};
273
274/** Returns squared distance to a given vector.
275 * \param origin vector to calculate distance to
276 * \return distance squared
277 */
[b453f9]278double atom::DistanceSquaredToVector(const Vector &origin) const
[4455f4]279{
[d74077]280 return DistanceSquared(origin);
[4455f4]281};
282
283/** Returns distance to a given vector.
284 * \param origin vector to calculate distance to
285 * \return distance
286 */
[b453f9]287double atom::DistanceToVector(const Vector &origin) const
[4455f4]288{
[d74077]289 return distance(origin);
[4455f4]290};
291
292/** Initialises the component number array.
293 * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1)
294 */
295void atom::InitComponentNr()
296{
297 if (ComponentNr != NULL)
[920c70]298 delete[](ComponentNr);
299 ComponentNr = new int[ListOfBonds.size()+1];
[4455f4]300 for (int i=ListOfBonds.size()+1;i--;)
301 ComponentNr[i] = -1;
302};
303
[d74077]304std::ostream & atom::operator << (std::ostream &ost) const
305{
306 ParticleInfo::operator<<(ost);
307 ost << "," << getPosition();
308 return ost;
309}
310
311std::ostream & operator << (std::ostream &ost, const atom &a)
312{
313 a.ParticleInfo::operator<<(ost);
314 ost << "," << a.getPosition();
315 return ost;
316}
[4455f4]317
318bool operator < (atom &a, atom &b)
319{
320 return a.Compare(b);
321};
322
[46d958]323World *atom::getWorld(){
324 return world;
325}
326
327void atom::setWorld(World* _world){
328 world = _world;
329}
330
[88d586]331bool atom::changeId(atomId_t newId){
332 // first we move ourselves in the world
333 // the world lets us know if that succeeded
334 if(world->changeAtomId(id,newId,this)){
335 id = newId;
336 return true;
337 }
338 else{
339 return false;
340 }
341}
342
343void atom::setId(atomId_t _id) {
[46d958]344 id=_id;
345}
346
[ad2b411]347atomId_t atom::getId() const {
[46d958]348 return id;
349}
350
[fa7989]351/** Makes the atom be contained in the new molecule \a *_mol.
352 * Uses atom::removeFromMolecule() to delist from old molecule.
353 * \param *_mol pointer to new molecule
354 */
[6cfa36]355void atom::setMolecule(molecule *_mol){
356 // take this atom from the old molecule
357 removeFromMolecule();
358 mol = _mol;
359 if(!mol->containsAtom(this)){
[dddbfe]360 mol->insert(this);
[6cfa36]361 }
362}
363
[fa7989]364/** Returns pointer to the molecule which atom belongs to.
365 * \return containing molecule
366 */
[e41c48]367molecule* atom::getMolecule() const {
[c084cc]368 return mol;
369}
370
[fa7989]371/** Erases the atom in atom::mol's list of atoms and sets it to zero.
372 */
[6cfa36]373void atom::removeFromMolecule(){
374 if(mol){
375 if(mol->containsAtom(this)){
376 mol->erase(this);
377 }
378 mol=0;
379 }
380}
381
382
[88d586]383atom* NewAtom(atomId_t _id){
384 atom * res =new atom();
385 res->setId(_id);
386 return res;
[46d958]387}
388
[88d586]389void DeleteAtom(atom* atom){
[46d958]390 delete atom;
391}
Note: See TracBrowser for help on using the repository browser.