source: src/atom.cpp@ d74077

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 Candidate_v1.7.0 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 d74077 was d74077, checked in by Frederik Heber <heber@…>, 15 years ago

Member variable Vector and element of class atom are now private.

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