@@ -133,23 +133,36 @@ def rf_kmc(
133
133
reaction_probability = []
134
134
135
135
# 1. Calculate the probability for each reaction
136
+ # get overall sampling time
137
+ # TODO: Can be replaced if we parse mdps for the simulation time
138
+ t_min = recipe_collection .recipes [0 ].timespans [0 ][0 ]
139
+ t_max = recipe_collection .recipes [0 ].timespans [0 ][1 ]
140
+ for recipe in recipe_collection .recipes :
141
+ for ts in recipe .timespans :
142
+ t1 = ts [0 ]
143
+ t2 = ts [1 ]
144
+ t_min = t1 if t1 < t_min else t_min
145
+ t_max = t2 if t2 > t_max else t_max
146
+ sampling_time = t_max - t_min
147
+ logger .debug (f"Sampling time: { sampling_time } ps" )
148
+
136
149
for recipe in recipe_collection .recipes :
137
150
dt = [x [1 ] - x [0 ] for x in recipe .timespans ]
138
- reaction_probability .append (sum (np .multiply (dt , recipe .rates )))
151
+ reaction_probability .append (sum (np .multiply (dt , recipe .rates )) / sampling_time )
139
152
140
153
# 2. Set the total rate to the sum of individual rates
141
- probability_cumulative = np .cumsum (reaction_probability )
142
- probability_sum = probability_cumulative [- 1 ]
154
+ rate_cumulative = np .cumsum (reaction_probability )
155
+ total_rate = rate_cumulative [- 1 ]
143
156
144
157
# 3. Generate two independent uniform (0,1) random numbers u1,u2
145
158
u = rng .random (2 )
146
159
logger .debug (
147
- f"\t Random values u: { u } , number cumulative probabilities "
148
- f"{ len (probability_cumulative )} , probability sum { probability_sum } "
160
+ f"\t Random values u: { u } , number cumulative rates "
161
+ f"{ len (rate_cumulative )} , total rate { total_rate } "
149
162
)
150
163
151
164
# 4. Find the even to carry out, mu, using binary search (np.searchsorted)
152
- pos = np .searchsorted (probability_cumulative , u [0 ] * probability_sum )
165
+ pos = np .searchsorted (rate_cumulative , u [0 ] * total_rate )
153
166
recipe : Recipe = recipe_collection .recipes [pos ]
154
167
time_index = np .argmax (recipe .rates )
155
168
reaction_time = recipe .timespans [time_index ][1 ]
@@ -158,10 +171,8 @@ def rf_kmc(
158
171
)
159
172
160
173
# 5. Calculate the time step associated with uu
161
- time_delta = np .log (1 / u [1 ]) / probability_sum
162
- logger .info (
163
- f"Time delta: { time_delta } ps with cumulative probability: { probability_sum } "
164
- )
174
+ time_delta = np .log (1 / u [1 ]) / total_rate
175
+ logger .info (f"Time delta: { time_delta } ps with total rate: { total_rate } " )
165
176
166
177
return KMCAccept (
167
178
recipe = recipe ,
@@ -213,11 +224,27 @@ def frm(
213
224
# 1. Generate M independent uniform (0,1) random numbers
214
225
u = rng .random (len (recipe_collection .recipes ))
215
226
# 2. Calculate the cumulative probabilities for each event
227
+ # get overall sampling time
228
+ # TODO: Can be replaced if we parse mdps for the simulation time
229
+ t_min = recipe_collection .recipes [0 ].timespans [0 ][0 ]
230
+ t_max = recipe_collection .recipes [0 ].timespans [0 ][1 ]
231
+ for recipe in recipe_collection .recipes :
232
+ for ts in recipe .timespans :
233
+ t1 = ts [0 ]
234
+ t2 = ts [1 ]
235
+ t_min = t1 if t1 < t_min else t_min
236
+ t_max = t2 if t2 > t_max else t_max
237
+ sampling_time = t_max - t_min
238
+ logger .debug (f"Sampling time: { sampling_time } ps" )
239
+
216
240
for i , recipe in enumerate (recipe_collection .recipes ):
217
241
dt = [x [1 ] - x [0 ] for x in recipe .timespans ]
218
242
cumulative_probability = np .cumsum (
219
243
list (map (lambda x , y : x * y , dt , recipe .rates ))
220
244
)
245
+ cumulative_probability = (
246
+ cumulative_probability / sampling_time
247
+ ) # calculate mean instead of sum
221
248
reaction_probability .append (cumulative_probability [- 1 ])
222
249
# 3. For each event k, find the time until the this reaction takes place tau_k,
223
250
# if it is during the time when the propensities are defined (simulation time)
@@ -534,9 +561,24 @@ def multi_rfkmc(
534
561
reaction_probability = []
535
562
536
563
# 1. Calculate the probability for each reaction
537
- for recipe in recipes :
564
+ # get overall sampling time
565
+ # TODO: Can be replaced if we parse mdps for the simulation time
566
+ t_min = recipe_collection .recipes [0 ].timespans [0 ][0 ]
567
+ t_max = recipe_collection .recipes [0 ].timespans [0 ][1 ]
568
+ for recipe in recipe_collection .recipes :
569
+ for ts in recipe .timespans :
570
+ t1 = ts [0 ]
571
+ t2 = ts [1 ]
572
+ t_min = t1 if t1 < t_min else t_min
573
+ t_max = t2 if t2 > t_max else t_max
574
+ sampling_time = t_max - t_min
575
+ logger .debug (f"Sampling time: { sampling_time } ps" )
576
+
577
+ for recipe in recipe_collection .recipes :
538
578
dt = [x [1 ] - x [0 ] for x in recipe .timespans ]
539
- reaction_probability .append (sum (np .multiply (dt , recipe .rates )))
579
+ reaction_probability .append (
580
+ sum (np .multiply (dt , recipe .rates )) / sampling_time
581
+ )
540
582
541
583
# 2. Set the total rate to the sum of individual rates
542
584
probability_cumulative = np .cumsum (reaction_probability )
0 commit comments