source: src/Atom/atom_bondedparticle.cpp@ 8ce730

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 8ce730 was 3aa8a5, checked in by Frederik Heber <heber@…>, 13 years ago

REFACTOR: AdjacencyList now just contains a single internal map.

  • Property mode set to 100644
File size: 10.8 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[bcf653]21 */
22
[6b919f8]23/*
24 * atom_bondedparticle.cpp
25 *
26 * Created on: Oct 19, 2009
27 * Author: heber
28 */
29
[bf3817]30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
[ad011c]35#include "CodePatterns/MemDebug.hpp"
[112b09]36
[6b919f8]37#include "atom.hpp"
38#include "atom_bondedparticle.hpp"
[129204]39#include "Bond/bond.hpp"
[d557374]40#include "CodePatterns/Assert.hpp"
[ad011c]41#include "CodePatterns/Log.hpp"
42#include "CodePatterns/Verbose.hpp"
[3bdb6d]43#include "Element/element.hpp"
[db7e6d]44#include "WorldTime.hpp"
[6b919f8]45
46/** Constructor of class BondedParticle.
47 */
[70ff32]48BondedParticle::BondedParticle()
49{
[9d83b6]50 ListOfBonds.push_back(BondList());
[70ff32]51};
[6b919f8]52
53/** Destructor of class BondedParticle.
54 */
55BondedParticle::~BondedParticle()
56{
[5e2f80]57 removeAllBonds();
[6b919f8]58};
59
60/** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
61 * \param *file output stream
62 */
[b453f9]63void BondedParticle::OutputOrder(ofstream *file) const
[6b919f8]64{
[735b1c]65 *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
[47d041]66 //LOG(2, "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << ".");
[6b919f8]67};
68
69/** Prints all bonds of this atom with total degree.
70 */
[4b5cf8]71void BondedParticle::OutputBondOfAtom(std::ostream &ost) const
[6b919f8]72{
[9d83b6]73 const BondList& ListOfBonds = getListOfBonds();
[4b5cf8]74 ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: ";
[e138de]75 int TotalDegree = 0;
76 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
[4b5cf8]77 ost << **Runner << "\t";
[e138de]78 TotalDegree += (*Runner)->BondDegree;
79 }
[4b5cf8]80 ost << " -- TotalDegree: " << TotalDegree;
[6b919f8]81};
82
[5309ba]83/** Output of atom::Nr along each bond partner per line.
84 * Only bonds are printed where atom::Nr is smaller than the one of the bond partner.
[1f1b23]85 * \param *AdjacencyFile output stream
86 */
87void BondedParticle::OutputBonds(ofstream * const BondFile) const
88{
[9d83b6]89 const BondList& ListOfBonds = getListOfBonds();
[1f1b23]90 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
[735b1c]91 if (getNr() < (*Runner)->GetOtherAtom(this)->getNr())
[52ed5b]92 *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n";
[1f1b23]93};
94
[b8d4a3]95/**
[db7e6d]96 * Adds a bond between this bonded particle and another. Returns present instance if this
[b8d4a3]97 * bond already exists.
98 *
[073a9e4]99 * @param _step time step to access
[db7e6d]100 * @param bonding partner
[05a885]101 * @return const pointer to created bond or to already present bonds
[b8d4a3]102 */
[05a885]103bond * const BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner)
[db7e6d]104{
105 const BondList &bondlist = getListOfBondsAtStep(_step);
106 for (BondList::const_iterator runner = bondlist.begin();
107 runner != bondlist.end();
108 runner++) {
109 if ((*runner)->Contains(Partner))
110 return *runner;
[b8d4a3]111 }
112
[efe516]113 bond* newBond = new bond((atom*) this, (atom*) Partner, 1);
[073a9e4]114 RegisterBond(_step, newBond);
115 Partner->RegisterBond(_step, newBond);
[db7e6d]116
117 return newBond;
118}
119
120/** Removes a bond for this atom.
121 *
122 * @param Binder bond to remove
123 */
124void BondedParticle::removeBond(bond * binder)
125{
126 UnregisterBond(binder);
[b8d4a3]127}
128
[cc9119]129/** Removes all bonds in all timesteps and their instances, too.
[5e2f80]130 *
131 */
132void BondedParticle::removeAllBonds()
133{
134 for (size_t index = 0; index < ListOfBonds.size(); ++index)
[cc9119]135 removeAllBonds(index);
136}
137
138/** Removes all bonds for a given \a _step and their instances, too.
139 *
140 * @param _step time step to access
141 */
142void BondedParticle::removeAllBonds(const unsigned int _step)
143{
144 for (BondList::iterator iter = ListOfBonds[_step].begin();
145 !ListOfBonds[_step].empty();
146 iter = ListOfBonds[_step].begin()) {
147 delete (*iter);
148 // unregister/NOTIFY is done by bond::~bond()
[5e2f80]149 }
150}
151
[6b919f8]152/** Puts a given bond into atom::ListOfBonds.
[073a9e4]153 * @param _step time step to access
[6b919f8]154 * \param *Binder bond to insert
155 */
[05a885]156bool BondedParticle::RegisterBond(const unsigned int _step, bond * const Binder)
[6b919f8]157{
[db7e6d]158 OBSERVE;
[6b919f8]159 bool status = false;
160 if (Binder != NULL) {
161 if (Binder->Contains(this)) {
[d557374]162 //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
[5e2f80]163 if (ListOfBonds.size() <= _step)
164 ListOfBonds.resize(_step+1);
165 ListOfBonds[_step].push_back(Binder);
[74ec1f]166 if (WorldTime::getTime() == _step)
[2ad1ec]167 NOTIFY(AtomObservable::BondsAdded);
[6b919f8]168 status = true;
169 } else {
[47d041]170 ELOG(1, *Binder << " does not contain " << *this << ".");
[6b919f8]171 }
172 } else {
[47d041]173 ELOG(1, "Binder is " << Binder << ".");
[6b919f8]174 }
175 return status;
176};
177
178/** Removes a given bond from atom::ListOfBonds.
[9d83b6]179 * @param _step time step to access
[6b919f8]180 * \param *Binder bond to remove
181 */
[05a885]182bool BondedParticle::UnregisterBond(bond * const Binder)
[6b919f8]183{
[db7e6d]184 OBSERVE;
[6b919f8]185 bool status = false;
[d557374]186 ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL.");
187 const int step = ContainsBondAtStep(Binder);
188 if (step != -1) {
[5e2f80]189 //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
[d557374]190 ListOfBonds[step].remove(Binder);
[760c4c]191 if (WorldTime::getTime() == step)
192 NOTIFY(AtomObservable::BondsRemoved);
[d557374]193 status = true;
[6b919f8]194 } else {
[47d041]195 ELOG(1, *Binder << " does not contain " << *this << ".");
[6b919f8]196 }
197 return status;
198};
199
200/** Removes all bonds from atom::ListOfBonds.
201 * \note Does not do any memory de-allocation.
202 */
[a2bdbe]203void BondedParticle::UnregisterAllBond(const unsigned int _step)
[6b919f8]204{
[74ec1f]205 OBSERVE;
[760c4c]206 NOTIFY(AtomObservable::BondsRemoved);
[af897f]207 ListOfBonds[_step].clear();
208}
[6b919f8]209
[583081]210/** Removes all bonds of given \a _step with freeing memory.
211 *
212 * @param _step time step whose bonds to free
213 */
214void BondedParticle::ClearBondsAtStep(const unsigned int _step)
215{
[db7e6d]216 OBSERVE;
[760c4c]217 NOTIFY(AtomObservable::BondsRemoved);
[583081]218 //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]);
219 for (BondList::iterator iter = (ListOfBonds[_step]).begin();
220 !(ListOfBonds[_step]).empty();
221 iter = (ListOfBonds[_step]).begin()) {
222 //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds);
223 delete((*iter)); // will also unregister with us and remove from list
224 }
225}
226
[93c6e9]227/** Searches for the time step where the given bond \a *Binder is a bond of this particle.
228 *
229 * @param Binder bond to check
230 * @return >=0 - first time step where bond appears, -1 - bond not present in lists
231 */
[db7e6d]232int BondedParticle::ContainsBondAtStep(bond *Binder) const
[93c6e9]233{
234 int step = -1;
235 int tempstep = 0;
236 for(std::vector<BondList>::const_iterator iter = ListOfBonds.begin();
237 iter != ListOfBonds.end();
238 ++iter,++tempstep) {
239 for (BondList::const_iterator bonditer = iter->begin();
240 bonditer != iter->end();
241 ++bonditer) {
242 if ((*bonditer) == Binder) {
243 step = tempstep;
244 break;
245 }
246 }
247 if (step != -1)
248 break;
249 }
250
251 return step;
252}
253
[6b919f8]254/** Corrects the bond degree by one at most if necessary.
[93c6e9]255 * \return number of corrections done
[6b919f8]256 */
[e138de]257int BondedParticle::CorrectBondDegree()
[6b919f8]258{
[db7e6d]259 OBSERVE;
[74ec1f]260 NOTIFY(AtomObservable::BondDegreeChanged);
[6b919f8]261 int NoBonds = 0;
262 int OtherNoBonds = 0;
263 int FalseBondDegree = 0;
264 atom *OtherWalker = NULL;
265 bond *CandidateBond = NULL;
266
267 NoBonds = CountBonds();
[47d041]268 //LOG(3, "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?");
[83f176]269 if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
[9d83b6]270 const BondList& ListOfBonds = getListOfBonds();
[6b919f8]271 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
272 OtherWalker = (*Runner)->GetOtherAtom(this);
273 OtherNoBonds = OtherWalker->CountBonds();
[47d041]274 //LOG(3, "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?");
[83f176]275 if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate
[9d83b6]276 const BondList& OtherListOfBonds = OtherWalker->getListOfBonds();
277 if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first
[6b919f8]278 CandidateBond = (*Runner);
[47d041]279 //LOG(3, "New candidate is " << *CandidateBond << ".");
[6b919f8]280 }
281 }
282 }
283 if ((CandidateBond != NULL)) {
284 CandidateBond->BondDegree++;
[47d041]285 //LOG(2, "Increased bond degree for bond " << *CandidateBond << ".");
[6b919f8]286 } else {
[47d041]287 ELOG(2, "Could not find correct degree for atom " << *this << ".");
[6b919f8]288 FalseBondDegree++;
289 }
290 }
291 return FalseBondDegree;
292};
293
[5e2f80]294/** Sets the weight of all connected bonds to one.
295 */
296void BondedParticle::resetBondDegree()
297{
298 OBSERVE;
[74ec1f]299 NOTIFY(BondedParticle::BondDegreeChanged);
[5e2f80]300 for (std::vector<BondList>::iterator Runner = ListOfBonds.begin();
301 Runner != ListOfBonds.end();
302 ++Runner)
303 for (BondList::iterator BondRunner = (*Runner).begin();
304 BondRunner != (*Runner).end();
305 ++BondRunner)
306 (*BondRunner)->BondDegree = 1;
307};
308
[c0d9eb]309/** Counts the number of bonds weighted by bond::BondDegree.
310 * @param _step time step to access
311 * \param bonds times bond::BondDegree
312 */
313int BondedParticle::CountBonds() const
314{
315 int NoBonds = 0;
[9d83b6]316 const BondList& ListOfBonds = getListOfBonds();
[c0d9eb]317 for (BondList::const_iterator Runner = ListOfBonds.begin();
318 Runner != ListOfBonds.end();
319 (++Runner))
320 NoBonds += (*Runner)->BondDegree;
321 return NoBonds;
322};
323
[b70721]324/** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
[073a9e4]325 * @param _step time step to access
[b70721]326 * \param *BondPartner atom to check for
327 * \return true - bond exists, false - bond does not exist
328 */
[073a9e4]329bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
[b70721]330{
331 bool status = false;
332
[073a9e4]333 const BondList& ListOfBonds = getListOfBondsAtStep(_step);
334 for (BondList::const_iterator runner = ListOfBonds.begin();
335 runner != ListOfBonds.end();
336 runner++) {
[b70721]337 status = status || ((*runner)->Contains(BondPartner));
338 }
339 return status;
340};
341
[d74077]342std::ostream & BondedParticle::operator << (std::ostream &ost) const
343{
344 ParticleInfo::operator<<(ost);
345 ost << "," << getPosition();
346 return ost;
347}
348
349std::ostream & operator << (std::ostream &ost, const BondedParticle &a)
350{
351 a.ParticleInfo::operator<<(ost);
352 ost << "," << a.getPosition();
353 return ost;
354}
355
Note: See TracBrowser for help on using the repository browser.