source: src/Atom/atom.cpp@ b97a60

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

AtomicInfo now stores Father by id not ref.

  • this migh have caused a crash before with Remove- and UndoActins by:
    • additionalAtomData pointing nowhere because the id might have changed.
    • father is stored as ref and not as id. If it's its own father, the ref is wrong.
    • molecule is given as ref and not as id.
  • atom::setMolecule() - NULL ref is allowed.
  • AtomicInfo: stores ids and not refs, first calls changeId then assigns father and mol (who both need updated id).
  • Property mode set to 100644
File size: 9.2 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/** \file atom.cpp
9 *
10 * Function implementations for the class atom.
11 *
12 */
13
14// include config.h
15#ifdef HAVE_CONFIG_H
16#include <config.h>
17#endif
18
19#include "CodePatterns/MemDebug.hpp"
20
21#include "atom.hpp"
22#include "Bond/bond.hpp"
23#include "CodePatterns/Log.hpp"
24#include "config.hpp"
25#include "Element/element.hpp"
26#include "LinearAlgebra/Vector.hpp"
27#include "World.hpp"
28#include "molecule.hpp"
29#include "Shapes/Shape.hpp"
30
31#include <iomanip>
32#include <iostream>
33
34/************************************* Functions for class atom *************************************/
35
36
37atom::atom() :
38 father(this),
39 sort(&Nr),
40 mol(0)
41{};
42
43atom::atom(atom *pointer) :
44 ParticleInfo(pointer),
45 father(pointer),
46 sort(&Nr),
47 mol(0)
48{
49 setType(pointer->getType()); // copy element of atom
50 AtomicPosition = pointer->AtomicPosition; // copy trajectory of coordination
51 AtomicVelocity = pointer->AtomicVelocity; // copy trajectory of velocity
52 AtomicForce = pointer->AtomicForce;
53 setFixedIon(pointer->getFixedIon());
54};
55
56atom *atom::clone(){
57 atom *res = new atom(this);
58 World::getInstance().registerAtom(res);
59 return res;
60}
61
62
63/** Destructor of class atom.
64 */
65atom::~atom()
66{
67 removeFromMolecule();
68};
69
70
71void atom::UpdateSteps()
72{
73 LOG(4,"atom::UpdateSteps() called.");
74 // append to position, velocity and force vector
75 AtomInfo::AppendTrajectoryStep();
76 // append to ListOfBonds vector
77 BondedParticleInfo::AppendTrajectoryStep();
78}
79
80atom *atom::GetTrueFather()
81{
82 const atom *father = const_cast<const atom *>(this)->GetTrueFather();
83 return const_cast<atom *>(father);
84}
85
86const atom *atom::GetTrueFather() const
87{
88 if(father == this){ // top most father is the one that points on itself
89 return this;
90 }
91 else if(!father) {
92 return 0;
93 }
94 else {
95 return father->GetTrueFather();
96 }
97};
98
99/** Sets father to itself or its father in case of copying a molecule.
100 */
101void atom::CorrectFather()
102{
103 if (father->father != father) // same atom in copy's father points to itself
104// father = this; // set father to itself (copy of a whole molecule)
105// else
106 father = father->father; // set father to original's father
107
108};
109
110void atom::EqualsFather ( const atom *ptr, const atom **res ) const
111{
112 if ( ptr == father )
113 *res = this;
114};
115
116bool atom::isFather(const atom *ptr){
117 return ptr==father;
118}
119
120bool atom::IsInShape(const Shape& shape) const
121{
122 return shape.isInside(getPosition());
123};
124
125bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
126{
127 if (out != NULL) {
128 *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
129 *out << at(0) << "\t" << at(1) << "\t" << at(2);
130 *out << "\t" << (int)(getFixedIon());
131 if (getAtomicVelocity().Norm() > MYEPSILON)
132 *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
133 if (comment != NULL)
134 *out << " # " << comment << endl;
135 else
136 *out << " # molecule nr " << getNr() << endl;
137 return true;
138 } else
139 return false;
140};
141
142bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
143{
144 AtomNo[getType()->getAtomicNumber()]++; // increment number
145 if (out != NULL) {
146 const element *elemental = getType();
147 ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
148 *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
149 *out << at(0) << "\t" << at(1) << "\t" << at(2);
150 *out << "\t" << getFixedIon();
151 if (getAtomicVelocity().Norm() > MYEPSILON)
152 *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
153 if (comment != NULL)
154 *out << " # " << comment << endl;
155 else
156 *out << " # molecule nr " << getNr() << endl;
157 return true;
158 } else
159 return false;
160};
161
162bool atom::OutputXYZLine(ofstream *out) const
163{
164 if (out != NULL) {
165 *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl;
166 return true;
167 } else
168 return false;
169};
170
171bool atom::OutputTrajectory(ofstream * const out, const enumeration<const element*> &elementLookup, int *AtomNo, const int step) const
172{
173 AtomNo[getType()->getAtomicNumber()]++;
174 if (out != NULL) {
175 const element *elemental = getType();
176 ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
177 *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[getType()->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
178 *out << getPositionAtStep(step)[0] << "\t" << getPositionAtStep(step)[1] << "\t" << getPositionAtStep(step)[2];
179 *out << "\t" << (int)(getFixedIon());
180 if (getAtomicVelocityAtStep(step).Norm() > MYEPSILON)
181 *out << "\t" << scientific << setprecision(6) << getAtomicVelocityAtStep(step)[0] << "\t" << getAtomicVelocityAtStep(step)[1] << "\t" << getAtomicVelocityAtStep(step)[2] << "\t";
182 if (getAtomicForceAtStep(step).Norm() > MYEPSILON)
183 *out << "\t" << scientific << setprecision(6) << getAtomicForceAtStep(step)[0] << "\t" << getAtomicForceAtStep(step)[1] << "\t" << getAtomicForceAtStep(step)[2] << "\t";
184 *out << "\t# Number in molecule " << getNr() << endl;
185 return true;
186 } else
187 return false;
188};
189
190bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
191{
192 if (out != NULL) {
193 *out << getType()->getSymbol() << "\t";
194 *out << getPositionAtStep(step)[0] << "\t";
195 *out << getPositionAtStep(step)[1] << "\t";
196 *out << getPositionAtStep(step)[2] << endl;
197 return true;
198 } else
199 return false;
200};
201
202void atom::OutputMPQCLine(ostream * const out, const Vector *center) const
203{
204 Vector recentered(getPosition());
205 recentered -= *center;
206 *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl;
207};
208
209void atom::OutputPsi3Line(ostream * const out, const Vector *center) const
210{
211 Vector recentered(getPosition());
212 recentered -= *center;
213 *out << "\t( " << getType()->getSymbol() << "\t" << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " )" << endl;
214};
215
216bool atom::Compare(const atom &ptr) const
217{
218 if (getNr() < ptr.getNr())
219 return true;
220 else
221 return false;
222};
223
224double atom::DistanceSquaredToVector(const Vector &origin) const
225{
226 return DistanceSquared(origin);
227};
228
229double atom::DistanceToVector(const Vector &origin) const
230{
231 return distance(origin);
232};
233
234void atom::InitComponentNr()
235{
236 if (ComponentNr != NULL)
237 delete[](ComponentNr);
238 const BondList& ListOfBonds = getListOfBonds();
239 ComponentNr = new int[ListOfBonds.size()+1];
240 for (int i=ListOfBonds.size()+1;i--;)
241 ComponentNr[i] = -1;
242};
243
244void atom::resetGraphNr(){
245 GraphNr=-1;
246}
247
248std::ostream & atom::operator << (std::ostream &ost) const
249{
250 ParticleInfo::operator<<(ost);
251 ost << "," << getPosition();
252 return ost;
253}
254
255std::ostream & operator << (std::ostream &ost, const atom &a)
256{
257 a.ParticleInfo::operator<<(ost);
258 ost << "," << a.getPosition();
259 return ost;
260}
261
262bool operator < (atom &a, atom &b)
263{
264 return a.Compare(b);
265};
266
267World *atom::getWorld(){
268 return world;
269}
270
271void atom::setWorld(World* _world){
272 world = _world;
273}
274
275bool atom::changeId(atomId_t newId){
276 // first we move ourselves in the world
277 // the world lets us know if that succeeded
278 if(world->changeAtomId(id,newId,this)){
279 id = newId;
280 return true;
281 }
282 else{
283 return false;
284 }
285}
286
287void atom::setId(atomId_t _id) {
288 id=_id;
289}
290
291atomId_t atom::getId() const {
292 return id;
293}
294
295void atom::setMolecule(molecule *_mol){
296 // take this atom from the old molecule
297 removeFromMolecule();
298 mol = _mol;
299 if ((mol) && (!mol->containsAtom(this)))
300 mol->insert(this);
301}
302
303void atom::unsetMolecule()
304{
305 // take this atom from the old molecule
306 ASSERT(!mol->containsAtom(this),
307 "atom::unsetMolecule() - old molecule "+toString(mol)+" still contains us!");
308 mol = NULL;
309}
310
311molecule* atom::getMolecule() const {
312 return mol;
313}
314
315void atom::removeFromMolecule(){
316 if(mol){
317 if(mol->containsAtom(this)){
318 mol->erase(this);
319 }
320 mol=0;
321 }
322}
323
324int atom::getNr() const{
325 return ParticleInfo::getNr();
326}
327
328atom* NewAtom(atomId_t _id){
329 atom * res =new atom();
330 res->setId(_id);
331 return res;
332}
333
334void DeleteAtom(atom* atom){
335 delete atom;
336}
337
338bool compareAtomElements(atom* atom1,atom* atom2){
339 return atom1->getType()->getNumber() < atom2->getType()->getNumber();
340}
Note: See TracBrowser for help on using the repository browser.