Lattice Microbes 2.5
This is for whole cell modeling
Loading...
Searching...
No Matches
ReactionQueue.h
Go to the documentation of this file.
1/*
2 * University of Illinois Open Source License
3 * Copyright 2011-2018 Luthey-Schulten Group,
4 * All rights reserved.
5 *
6 * Developed by: Luthey-Schulten Group
7 * University of Illinois at Urbana-Champaign
8 * http://www.scs.uiuc.edu/~schulten
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy of
11 * this software and associated documentation files (the Software), to deal with
12 * the Software without restriction, including without limitation the rights to
13 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 * of the Software, and to permit persons to whom the Software is furnished to
15 * do so, subject to the following conditions:
16 *
17 * - Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimers.
19 *
20 * - Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimers in the documentation
22 * and/or other materials provided with the distribution.
23 *
24 * - Neither the names of the Luthey-Schulten Group, University of Illinois at
25 * Urbana-Champaign, nor the names of its contributors may be used to endorse or
26 * promote products derived from this Software without specific prior written
27 * permission.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
33 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 * OTHER DEALINGS WITH THE SOFTWARE.
36 *
37 * Author(s): Elijah Roberts
38 */
39
40#ifndef LM_REACTION_REACTIONQUEUE_H_
41#define LM_REACTION_REACTIONQUEUE_H_
42
43#include <cmath>
44
45namespace lm {
46namespace reaction {
47
51{
52public:
56 {
57 double time;
58 double propensity;
59 };
60
61public:
78
80 {
81 if (reactionEvents != NULL) {delete[] reactionEvents; reactionEvents = NULL;}
82 if (reactionQueue != NULL) {delete[] reactionQueue; reactionQueue = NULL;}
83 if (reactionPositions != NULL) {delete[] reactionPositions; reactionPositions = NULL;}
84 }
85
89 {
90 return reactionQueue[1];
91 }
92
97 {
98 return reactionEvents[reactionIndex];
99 }
100
105 void updateReactionEvent(uint reactionIndex, double newTime, double newPropensity)
106 {
107 // Update the reaction entry.
108 double oldTime = reactionEvents[reactionIndex].time;
109 reactionEvents[reactionIndex].time = newTime;
110 reactionEvents[reactionIndex].propensity = newPropensity;
111
112 // Get the reaction's current position in the queue.
113 uint currentPosition = reactionPositions[reactionIndex];
114
115 // If the new time is before the old time we need to move up in the tree.
116 if (newTime <= oldTime)
117 {
118 // While the new time is less that its parent's time, move it up.
119 uint parentPosition = currentPosition>>1;
120 uint parentReactionIndex = reactionQueue[parentPosition];
121 while (parentPosition >= 1 && newTime < reactionEvents[parentReactionIndex].time)
122 {
123 // Swap the reaction with its parent.
124 reactionQueue[parentPosition] = reactionIndex;
125 reactionQueue[currentPosition] = parentReactionIndex;
126
127 // Update the position list.
128 reactionPositions[reactionIndex] = parentPosition;
129 reactionPositions[parentReactionIndex] = currentPosition;
130
131 // Set our new current position and loop again.
132 currentPosition = parentPosition;
133 parentPosition = currentPosition>>1;
134 parentReactionIndex = reactionQueue[parentPosition];
135 }
136 }
137
138 // The new time must be after the old time, so we need to move down in the tree.
139 else
140 {
141 // While the new time is greater than one of its children, move it down.
142 uint child1Position = currentPosition<<1;
143 uint child2Position = child1Position+1;
144 while ((child1Position <= numberReactions && newTime > reactionEvents[reactionQueue[child1Position]].time) ||
145 (child2Position <= numberReactions && newTime > reactionEvents[reactionQueue[child2Position]].time))
146 {
147 // If only the first child is valid, use it, otherwise use the child with the min time.
148 uint minChildPosition = (child2Position > numberReactions)?(child1Position)
149 :((reactionEvents[reactionQueue[child1Position]].time <= reactionEvents[reactionQueue[child2Position]].time)?(child1Position):(child2Position));
150 uint minChildReactionIndex = reactionQueue[minChildPosition];
151
152 // Swap the reaction with the child.
153 reactionQueue[minChildPosition] = reactionIndex;
154 reactionQueue[currentPosition] = minChildReactionIndex;
155
156 // Update the position list.
157 reactionPositions[reactionIndex] = minChildPosition;
158 reactionPositions[minChildReactionIndex] = currentPosition;
159
160 // Set our new current position and loop again.
161 currentPosition = minChildPosition;
162 child1Position = currentPosition<<1;
163 child2Position = child1Position+1;
164 }
165 }
166 }
167
168protected:
169 uint numberReactions; // The number of total reactions
170 ReactionEvent * reactionEvents; // The reaction events
171 uint * reactionQueue; // Queue of reactions
172 uint * reactionPositions; // Position of the reaction in the queue
173};
174
175}
176}
177
178#endif
unsigned int uint
Definition Types.h:52
~ReactionQueue()
Destroy the reaction queue.
Definition ReactionQueue.h:79
void updateReactionEvent(uint reactionIndex, double newTime, double newPropensity)
Update the reaction event queue.
Definition ReactionQueue.h:105
uint numberReactions
Definition ReactionQueue.h:169
uint getNextReaction()
get the next reaction
Definition ReactionQueue.h:88
uint * reactionPositions
Definition ReactionQueue.h:172
ReactionEvent getReactionEvent(uint reactionIndex)
Get the next reaction event.
Definition ReactionQueue.h:96
uint * reactionQueue
Definition ReactionQueue.h:171
ReactionQueue(uint numberReactions)
Initialize the reaction queue.
Definition ReactionQueue.h:64
ReactionEvent * reactionEvents
Definition ReactionQueue.h:170
Definition ReactionQueue.h:46
Definition Capsule.cpp:46
Definition of a reaction event with the time to reaction and the propensity for the reaction to occur...
Definition ReactionQueue.h:56
double propensity
Definition ReactionQueue.h:58
double time
Definition ReactionQueue.h:57