[c3df23] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2013 University of Bonn. All rights reserved.
|
---|
[5aaa43] | 5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
[c3df23] | 6 | *
|
---|
| 7 | *
|
---|
| 8 | * This file is part of MoleCuilder.
|
---|
| 9 | *
|
---|
| 10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 11 | * it under the terms of the GNU General Public License as published by
|
---|
| 12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 13 | * (at your option) any later version.
|
---|
| 14 | *
|
---|
| 15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | * GNU General Public License for more details.
|
---|
| 19 | *
|
---|
| 20 | * You should have received a copy of the GNU General Public License
|
---|
| 21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | /*
|
---|
| 25 | * HydrogenPool.cpp
|
---|
| 26 | *
|
---|
| 27 | * Created on: Mar 3, 2013
|
---|
| 28 | * Author: heber
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 | // include config.h
|
---|
| 32 | #ifdef HAVE_CONFIG_H
|
---|
| 33 | #include <config.h>
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
| 36 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 37 |
|
---|
| 38 | #include "HydrogenPool.hpp"
|
---|
| 39 |
|
---|
| 40 | #include "CodePatterns/Assert.hpp"
|
---|
| 41 | #include "CodePatterns/Log.hpp"
|
---|
| 42 |
|
---|
| 43 | #include "Atom/atom.hpp"
|
---|
| 44 | #include "Atom/AtomObserver.hpp"
|
---|
| 45 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
| 46 | #include "Element/periodentafel.hpp"
|
---|
| 47 | #include "World.hpp"
|
---|
[11f0fa] | 48 | #include "WorldTime.hpp"
|
---|
[c3df23] | 49 |
|
---|
| 50 | HydrogenPool::HydrogenPool() :
|
---|
| 51 | HydrogenCount(0)
|
---|
| 52 | {}
|
---|
| 53 |
|
---|
| 54 | HydrogenPool::~HydrogenPool()
|
---|
| 55 | {
|
---|
| 56 | cleanup();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | void HydrogenPool::requestHydrogenIntoPool()
|
---|
| 60 | {
|
---|
| 61 | // get new hydrogen from world, but remove its observers
|
---|
| 62 | atom * const Walker = World::getInstance().createAtom();
|
---|
| 63 | Walker->setType(HYDROGEN); // set element
|
---|
| 64 | Walker->setName(std::string("H_")+toString(HydrogenCount));
|
---|
| 65 | Walker->signOff(AtomObserver::getPointer(), AtomObservable::PositionChanged);
|
---|
| 66 | Walker->signOff(AtomObserver::getPointer(), AtomObservable::ElementChanged);
|
---|
| 67 | HydrogenQueue.push_back(Walker);
|
---|
| 68 | ++HydrogenCount;
|
---|
| 69 |
|
---|
[910a5d] | 70 | // give warning if pool has more than threshold
|
---|
[c3df23] | 71 | if (HydrogenCount >= WARNINGTHRESHOLD) {
|
---|
| 72 | ELOG(2, "HydrogenPool contains more hydrogen atoms than limit.");
|
---|
| 73 | ELOG(2, "Either someone requesting too eagerly, or another not returning them.");
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // final check
|
---|
| 77 | ASSERT(!HydrogenQueue.empty(),
|
---|
| 78 | "HydrogenPool::requestHydrogenIntoPool() - failed to request more hydrogens.");
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | atom * HydrogenPool::leaseHydrogen()
|
---|
| 82 | {
|
---|
| 83 | // check the queue, if empty, add more hydrogens
|
---|
| 84 | if (HydrogenQueue.empty())
|
---|
| 85 | requestHydrogenIntoPool();
|
---|
| 86 |
|
---|
| 87 | // pop hydrogen, mark down, and deliver
|
---|
| 88 | atom * const Walker = HydrogenQueue.front();
|
---|
| 89 | ASSERT( HydrogenInUse.count(Walker->getId()) == 0,
|
---|
| 90 | "HydrogenPool::leaseHydrogen() - hydrogen "+toString(*Walker)
|
---|
| 91 | +" from pool is already in use.");
|
---|
| 92 | LOG(3, "DEBUG: Leasing " << *Walker << ".");
|
---|
[11f0fa] | 93 | UpdateSteps(Walker);
|
---|
[c3df23] | 94 | HydrogenInUse.insert( std::make_pair( Walker->getId(), Walker) );
|
---|
| 95 | HydrogenQueue.pop_front();
|
---|
[11f0fa] | 96 |
|
---|
[c3df23] | 97 | return Walker;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[11f0fa] | 100 | void HydrogenPool::UpdateSteps(atom * _atom) const
|
---|
| 101 | {
|
---|
| 102 | // make sure we are up to current time step
|
---|
| 103 | const size_t CurrentTime = WorldTime::getTime();
|
---|
| 104 | for (size_t step = _atom->getTrajectorySize(); step <= CurrentTime; ++step)
|
---|
[8cc22f] | 105 | _atom->UpdateStep(step);
|
---|
[11f0fa] | 106 | }
|
---|
| 107 |
|
---|
[c3df23] | 108 | void HydrogenPool::releaseHydrogen(atom * _atom)
|
---|
| 109 | {
|
---|
| 110 | if (_atom == NULL) {
|
---|
| 111 | ASSERT( 0,
|
---|
| 112 | "HydrogenPool::releaseHydrogen() - got NULL atom.");
|
---|
| 113 | return;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | // check that it is marked down
|
---|
| 117 | {
|
---|
| 118 | HydrogenInUse_t::iterator iter = HydrogenInUse.find(_atom->getId());
|
---|
| 119 | if (iter == HydrogenInUse.end()) {
|
---|
| 120 | ASSERT( 0,
|
---|
| 121 | "HydrogenPool::releaseHydrogen() - got unknown atom "+toString(_atom)+".");
|
---|
| 122 | return;
|
---|
| 123 | }
|
---|
| 124 | LOG(3, "DEBUG: Releasing " << *_atom << ".");
|
---|
| 125 | HydrogenInUse.erase(iter);
|
---|
| 126 | }
|
---|
| 127 | // check into queue
|
---|
| 128 | HydrogenQueue.push_back(_atom);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void HydrogenPool::releaseHydrogen(atomId_t _atom)
|
---|
| 132 | {
|
---|
| 133 | atom * const Walker = World::getInstance().getAtom(AtomById(_atom));
|
---|
| 134 | ASSERT( Walker != NULL,
|
---|
| 135 | "HydrogenPool::releaseHydrogen() - id "
|
---|
| 136 | +toString(_atom)+" unknown to World.");
|
---|
| 137 | releaseHydrogen(Walker);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void HydrogenPool::cleanup()
|
---|
| 141 | {
|
---|
| 142 | ASSERT(HydrogenInUse.empty(),
|
---|
| 143 | "HydrogenPool::cleanup() - cleanup called, but still hydrogens in use.");
|
---|
| 144 | for (HydrogenQueue_t::iterator iter = HydrogenQueue.begin();
|
---|
| 145 | !HydrogenQueue.empty(); iter = HydrogenQueue.begin()) {
|
---|
| 146 | atom * const Walker = *iter;
|
---|
| 147 | HydrogenQueue.erase(iter);
|
---|
| 148 | World::getInstance().destroyAtom(Walker);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|