-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
direct.jl
141 lines (120 loc) · 4.45 KB
/
direct.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
mutable struct DirectJumpAggregation{T, S, F1, F2, RNG} <:
AbstractSSAJumpAggregator{T, S, F1, F2, RNG}
next_jump::Int
prev_jump::Int
next_jump_time::T
end_time::T
cur_rates::Vector{T}
sum_rate::T
ma_jumps::S
rates::F1
affects!::F2
save_positions::Tuple{Bool, Bool}
rng::RNG
end
function DirectJumpAggregation(nj::Int, njt::T, et::T, crs::Vector{T}, sr::T, maj::S,
rs::F1, affs!::F2, sps::Tuple{Bool, Bool}, rng::RNG;
kwargs...) where {T, S, F1, F2, RNG}
affecttype = F2 <: Tuple ? F2 : Any
DirectJumpAggregation{T, S, F1, affecttype, RNG}(nj, nj, njt, et, crs, sr, maj, rs,
affs!, sps, rng)
end
############################# Required Functions #############################
# creating the JumpAggregation structure (tuple-based constant jumps)
function aggregate(aggregator::Direct, u, p, t, end_time, constant_jumps,
ma_jumps, save_positions, rng; kwargs...)
# handle constant jumps using tuples
rates, affects! = get_jump_info_tuples(constant_jumps)
build_jump_aggregation(DirectJumpAggregation, u, p, t, end_time, ma_jumps,
rates, affects!, save_positions, rng; kwargs...)
end
# creating the JumpAggregation structure (function wrapper-based constant jumps)
function aggregate(aggregator::DirectFW, u, p, t, end_time, constant_jumps,
ma_jumps, save_positions, rng; kwargs...)
# handle constant jumps using function wrappers
rates, affects! = get_jump_info_fwrappers(u, p, t, constant_jumps)
build_jump_aggregation(DirectJumpAggregation, u, p, t, end_time, ma_jumps,
rates, affects!, save_positions, rng; kwargs...)
end
# set up a new simulation and calculate the first jump / jump time
function initialize!(p::DirectJumpAggregation, integrator, u, params, t)
p.end_time = integrator.sol.prob.tspan[2]
generate_jumps!(p, integrator, u, params, t)
nothing
end
# execute one jump, changing the system state
@inline function execute_jumps!(p::DirectJumpAggregation, integrator, u, params, t,
affects!)
update_state!(p, integrator, u, affects!)
nothing
end
# calculate the next jump / jump time
function generate_jumps!(p::DirectJumpAggregation, integrator, u, params, t)
p.sum_rate, ttnj = time_to_next_jump(p, u, params, t)
p.next_jump_time = add_fast(t, ttnj)
@inbounds p.next_jump = searchsortedfirst(p.cur_rates, rand(p.rng) * p.sum_rate)
nothing
end
######################## SSA specific helper routines ########################
# tuple-based constant jumps
function time_to_next_jump(p::DirectJumpAggregation{T, S, F1}, u, params,
t) where {T, S, F1 <: Tuple}
prev_rate = zero(t)
new_rate = zero(t)
cur_rates = p.cur_rates
# mass action rates
majumps = p.ma_jumps
idx = get_num_majumps(majumps)
@inbounds for i in 1:idx
new_rate = evalrxrate(u, i, majumps)
cur_rates[i] = add_fast(new_rate, prev_rate)
prev_rate = cur_rates[i]
end
# constant jump rates
rates = p.rates
if !isempty(rates)
idx += 1
fill_cur_rates(u, params, t, cur_rates, idx, rates...)
@inbounds for i in idx:length(cur_rates)
cur_rates[i] = add_fast(cur_rates[i], prev_rate)
prev_rate = cur_rates[i]
end
end
@inbounds sum_rate = cur_rates[end]
sum_rate, randexp(p.rng) / sum_rate
end
@inline function fill_cur_rates(u, p, t, cur_rates, idx, rate, rates...)
@inbounds cur_rates[idx] = rate(u, p, t)
idx += 1
fill_cur_rates(u, p, t, cur_rates, idx, rates...)
end
@inline function fill_cur_rates(u, p, t, cur_rates, idx, rate)
@inbounds cur_rates[idx] = rate(u, p, t)
nothing
end
# function wrapper-based constant jumps
function time_to_next_jump(p::DirectJumpAggregation{T, S, F1}, u, params,
t) where {T, S, F1 <: AbstractArray}
prev_rate = zero(t)
new_rate = zero(t)
cur_rates = p.cur_rates
# mass action rates
majumps = p.ma_jumps
idx = get_num_majumps(majumps)
@inbounds for i in 1:idx
new_rate = evalrxrate(u, i, majumps)
cur_rates[i] = add_fast(new_rate, prev_rate)
prev_rate = cur_rates[i]
end
# constant jump rates
idx += 1
rates = p.rates
@inbounds for i in 1:length(p.rates)
new_rate = rates[i](u, params, t)
cur_rates[idx] = add_fast(new_rate, prev_rate)
prev_rate = cur_rates[idx]
idx += 1
end
@inbounds sum_rate = cur_rates[end]
sum_rate, randexp(p.rng) / sum_rate
end