9 #include "LinkMaster.h" 10 #include <util/misc/Observer.h> 11 #include <util/random/Random.h> 31 read<int>(in,
"linkCapacity", linkCapacity_);
32 read<int>(in,
"atomCapacity", atomCapacity_);
42 int atom0Id = atom0.
id();
43 int atom1Id = atom1.
id();
46 if (atom0Id < 0 || atom0Id >= atomCapacity_) {
47 Log::file() <<
"Atom0Id = " << atom0Id << std::endl;
48 Log::file() <<
"atomCapacity = " << atomCapacity_ << std::endl;
51 if (atom1Id < 0 || atom1Id >= atomCapacity_) {
52 Log::file() <<
"Atom1Id = " << atom1Id << std::endl;
53 Log::file() <<
"atomCapacity = " << atomCapacity_ << std::endl;
58 linkPtr = &reservoir_.pop();
59 linkSet_.append(*linkPtr);
62 linkPtr->setAtoms(atom0,atom1);
63 linkPtr->setTypeId(typeId);
65 atomLinkSets_[atom0Id].append(*linkPtr);
66 atomLinkSets_[atom1Id].append(*linkPtr);
84 UTIL_THROW(
"Attempt to remove a nonactive link");
86 atom0Id = linkPtr->
atom0().
id();
87 atom1Id = linkPtr->
atom1().
id();
89 if (!atomLinkSets_[atom0Id].isElement(*linkPtr)) {
90 UTIL_THROW(
"Link is not in atomLinkSets of atom0");
92 if (!atomLinkSets_[atom1Id].isElement(*linkPtr)) {
93 UTIL_THROW(
"Link is not in atomLinkSets of atom1");
95 atomLinkSets_[atom0Id].remove(*linkPtr);
96 atomLinkSets_[atom1Id].remove(*linkPtr);
107 linkSet_.remove(*linkPtr);
108 reservoir_.push(*linkPtr);
117 int atom0Id = atom0.
id();
118 int atom1Id = atom1.
id();
119 int oldAtom0Id = link.
atom0().
id();
120 int oldAtom1Id = link.
atom1().
id();
124 UTIL_THROW(
"Attempt to reset atoms for a nonactive link");
126 if (atom0Id < 0 || atom0Id >= atomCapacity_) {
127 Log::file() <<
"Atom0Id = " << atom0Id << std::endl;
128 Log::file() <<
"atomCapacity = " << atomCapacity_ << std::endl;
131 if (atom1Id < 0 || atom1Id >= atomCapacity_) {
132 Log::file() <<
"Atom1Id = " << atom1Id << std::endl;
133 Log::file() <<
"atomCapacity = " << atomCapacity_ << std::endl;
138 if (!atomLinkSets_[oldAtom0Id].isElement(link)) {
139 UTIL_THROW(
"Link is not in atomLinkSets of atom0");
141 if (!atomLinkSets_[oldAtom1Id].isElement(link)) {
142 UTIL_THROW(
"Link is not in atomLinkSets of atom1");
144 atomLinkSets_[oldAtom0Id].remove(link);
145 atomLinkSets_[oldAtom1Id].remove(link);
148 link.setAtoms(atom0,atom1);
149 atomLinkSets_[atom0Id].append(link);
150 atomLinkSets_[atom1Id].append(link);
162 int atomId = atom.
id();
174 UTIL_THROW(
"Attempt to reset an atom for a nonactive link");
176 if (atomId < 0 || atomId >= atomCapacity_) {
177 Log::file() <<
"AtomId = " << atomId << std::endl;
178 Log::file() <<
"atomCapacity = " << atomCapacity_ << std::endl;
183 if (!atomLinkSets_[oldAtomId].isElement(link)) {
184 UTIL_THROW(
"Link is not in atomLinkSets of atom");
186 atomLinkSets_[oldAtomId].remove(link);
190 link.setAtoms(atom,link.
atom1());
193 link.setAtoms(link.
atom0(),atom);
195 atomLinkSets_[atomId].append(link);
209 UTIL_THROW(
"Number of links in species <= 0");
219 void LinkMaster::allocate()
222 links_.allocate(linkCapacity_);
223 reservoir_.allocate(linkCapacity_);
224 linkSet_.allocate(links_);
227 for (
int i = 0; i < linkCapacity_; ++i) {
234 for (i = linkCapacity_ - 1; i >= 0; --i) {
235 reservoir_.push(links_[i]);
239 atomLinkSets_.allocate(atomCapacity_);
248 if (links_.isAllocated()) {
250 if (links_.capacity() != reservoir_.capacity()) {
251 UTIL_THROW(
"Unequal capacities for links_ and reservoir_");
253 if (links_.capacity() != reservoir_.size() + linkSet_.size()) {
254 UTIL_THROW(
"links_ capacity != reservoir_ + linkSet_ sizes");
259 const Atom* atom0Ptr;
260 const Atom* atom1Ptr;
262 for (i = 0; i < linkSet_.size(); ++i) {
264 linkPtr = &(linkSet_[i]);
265 atom0Ptr = &(linkPtr->
atom0());
266 atom1Ptr = &(linkPtr->
atom1());
271 if (atom0Ptr == 0 || atom1Ptr == 0) {
272 UTIL_THROW(
"Link with one or both atoms missing");
276 if (atom0Ptr == atom1Ptr) {
277 UTIL_THROW(
"Link connects an atom to itself");
286 if (!atomLinkSets_[atom0Ptr->
id()].isElement(*linkPtr)) {
287 UTIL_THROW(
"Link is not in atomLinkSets of atom0");
289 if (!atomLinkSets_[atom1Ptr->
id()].isElement(*linkPtr)) {
290 UTIL_THROW(
"Link is not in atomLinkSets of atom1");
297 for (i = 0; i < linkCapacity_; ++i) {
298 linkPtr = &links_[i];
303 if (nCount != linkSet_.size()) {
304 UTIL_THROW(
"Number of Links with atoms != linkSet.size()");
309 for (i = 0; i < atomCapacity_; ++i) {
310 nCount += atomLinkSets_[i].size();
312 if (nCount != 2*linkSet_.size()) {
313 UTIL_THROW(
"Number links in atom sets != 2*linkSet.size()");
326 for (
int id =
nLink()-1;
id >= 0;
id--) {
int nLink() const
Get the total number of active Links.
void notifyObservers(const Event &event)
Notify the list of observers about an Event.
void removeLink(int id)
Remove a Link.
Event signalling the reset of an atom from the LinkMaster.
void reSetAtoms(Link &link, Atom &atom0, Atom &atom1)
Modify the atoms attached to a link.
A Link represents a crosslink between two Atoms.
Event signalling removal of a Link from the LinkMaster.
bool isActive() const
Is this parameter active?
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
A point particle within a Molecule.
Utility classes for scientific computation.
void clear()
Clear LinkMaster.
int id() const
Get global index for this Atom within the Simulation.
long uniformInt(long range1, long range2)
Return random long int x uniformly distributed in range1 <= x < range2.
void reSetAtom(Link &link, Atom &atom, int endId)
Modify one atom attached to a link.
Event signalling addition of Link to the LinkMaster.
static std::ostream & file()
Get log ostream by reference.
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
const Atom & atom1() const
Get Atom1 connected to a Link.
void setClassName(const char *className)
Set class name string.
bool isValid() const
Return true if this LinkMaster is valid, or throw an Exception.
Link & link(int id) const
Return an active link by an internal set index.
Link & randomLink(Random &random)
Get a randomly chosen link by reference.
Event signalling reset of Link to the LinkMaster.
void addLink(Atom &atom0, Atom &atom1, int typeId)
Add a link betwen two specific Atoms.
const Atom & atom0() const
Get Atom0 connected to a Link.
bool isActive() const
Is this Link active?
void readParameters(std::istream &in)
Read linkCapacity and allocate.
void setIsActive(bool isActive)
Activate or deactivate the Link.