solve.jl 6.64 KB
Newer Older
1

2
3
4
#remove Base.@propagate_inbounds during tests if you get segfaults or change anything,
# as that might mean there is an indexing bug that is not being caught,
#and therefore accessing the wrong memory is causing background errors
5

6
7
8
function contact_weight(p, contact_time) 
    return 1 - (1-p)^contact_time
end
9

10
11
12
13
function Φ(payoff,β)
    return 1 / (exp(-1*β*payoff))
end

14
15
16
17
Base.@propagate_inbounds @views function update_alert_durations!(t,modelsol) # Base.@propagate_inbounds 
    @unpack notification_parameter,notification_threshold = modelsol.params
    @unpack time_of_last_alert, app_user_index,inf_network,covid_alert_notifications,app_user = modelsol
    for (i,node) in enumerate(modelsol.app_user_index)
18
        for j in 2:14
19
            covid_alert_notifications[j-1,i] = covid_alert_notifications[j,i] #shift them all back  
20
        end
21
22
23
24
25
26
        total_weight_i = 0
        for mixing_graph in inf_network.graph_list[t]
            for j in neighbors(mixing_graph.g,node)
                if app_user[j]
                    total_weight_i+= get_weight(mixing_graph,GraphEdge(node,j))
                end
27
28
            end
        end
29
        coin_flip = 1 - (1 - notification_parameter)^total_weight_i
30
        r = rand(Random.default_rng(Threads.threadid()))
31
32
33
34
35
36
        if r < coin_flip
            covid_alert_notifications[end,i] = 1  #add the notifications for today
        else
            covid_alert_notifications[end,i] = 0
        end
        if sum(covid_alert_notifications[:,i])>=notification_threshold
37
38
39
40
            time_of_last_alert[i] = t
        end
    end
end
41

42
Base.@propagate_inbounds @views function update_infection_state!(t,modelsol)
Peter Jentsch's avatar
Peter Jentsch committed
43
    @unpack base_transmission_probability,immunization_loss_prob,recovery_rate,immunizing,immunization_begin_day = modelsol.params
44
    @unpack u_inf,u_vac,u_next_inf,u_next_vac,demographics,inf_network,status_totals, immunization_countdown = modelsol
45
    
46
47
    modelsol.daily_cases_by_age .= 0
    modelsol.daily_immunized_by_age .= 0
48
    function agent_transition!(node, from::AgentStatus,to::AgentStatus) 
49
        immunization_countdown[node] = -1 
50
51
        status_totals[Int(from)] -= 1
        status_totals[Int(to)] += 1
52
53
54
        u_next_inf[node] = to
    end
    u_next_inf .= u_inf
55
56
57
58
59
    for i in 1:modelsol.nodes
        agent_status = u_inf[i]
        is_vaccinator = u_vac[i]
        agent_demo = demographics[i]
        if agent_status == Susceptible
Peter Jentsch's avatar
Peter Jentsch committed
60
            if is_vaccinator && immunizing && immunization_countdown[i] == -1 && t> immunization_begin_day
61
                immunization_countdown[i] = 14
62
            else
63
                for mixing_graph in inf_network.graph_list[t]
64
                    for j in neighbors(mixing_graph.g,i)    
65
                        if u_inf[j] == Infected && u_next_inf[i] != Infected 
66
                            if rand(Random.default_rng(Threads.threadid())) < contact_weight(base_transmission_probability,get_weight(mixing_graph,GraphEdge(i,j)))
67
                                modelsol.daily_cases_by_age[Int(agent_demo)]+=1
68
69
                                agent_transition!(i, Susceptible,Infected)
                            end
70
71
72
73
74
                        end
                    end
                end
            end
        elseif agent_status == Infected
75
            if rand(Random.default_rng(Threads.threadid())) < recovery_rate
76
               agent_transition!(i, Infected,Recovered)
77
78
            end
        elseif agent_status == Immunized
79
            if rand(Random.default_rng(Threads.threadid())) < immunization_loss_prob
80
               agent_transition!(i, Immunized,Susceptible)
81
82
            end
        end
83
84
        
        if immunization_countdown[i] == 0 
85
            modelsol.daily_immunized_by_age[Int(agent_demo)] += 1
86
87
88
89
            agent_transition!(i, Susceptible,Immunized)
        elseif immunization_countdown[i]>0
            immunization_countdown[i] -= 1
        end
90
91
    end
end
92
93


Peter Jentsch's avatar
Peter Jentsch committed
94
Base.@propagate_inbounds @views function update_vaccination_opinion_state!(t,modelsol,total_infections)
95
    @unpack π_base_y,π_base_m,π_base_o, η,γ, κ, ω, ω_en,γ,β = modelsol.params
96
97
98
    @unpack demographics,time_of_last_alert, nodes, soc_network,u_vac,u_next_vac,app_user,app_user_list = modelsol
    app_user_pointer = 0
    for i in 1:nodes
Peter Jentsch's avatar
Peter Jentsch committed
99
        π_base = @SVector [π_base_y,π_base_m,π_base_o]
100
101
        vac_payoff = 0
        num_soc_nbrs = 0
Peter Jentsch's avatar
Peter Jentsch committed
102
        random_soc_network = sample(Random.default_rng(Threads.threadid()), soc_network.graph_list[t])
103

Peter Jentsch's avatar
Peter Jentsch committed
104
105
106
        if !isempty(neighbors(random_soc_network.g,i))
            random_neighbour = sample(Random.default_rng(Threads.threadid()), neighbors(random_soc_network.g,i))
            if u_vac[random_neighbour] == u_vac[i]
107
                vac_payoff += π_base[Int(demographics[i])] + total_infections*ω             
Peter Jentsch's avatar
Peter Jentsch committed
108
                if app_user[i] && time_of_last_alert[app_user_list[i]]>=0
109
                    vac_payoff += γ^(-1*(t - time_of_last_alert[app_user_list[i]])) * (η + total_infections*ω_en) 
Peter Jentsch's avatar
Peter Jentsch committed
110
                end
111
        
Peter Jentsch's avatar
Peter Jentsch committed
112
113
114
115
116
117
118
119
120
                if u_vac[i]
                    if rand(Random.default_rng(Threads.threadid())) < 1 - Φ(vac_payoff,β)
                        u_next_vac[i] = false
                    end
                else
                    if rand(Random.default_rng(Threads.threadid())) < Φ(vac_payoff,β)
                        u_next_vac[i] = true
                    end
                end
121
122
123
            end
        end
    end
124

Peter Jentsch's avatar
Peter Jentsch committed
125
    modelsol.daily_vaccinators = count(==(true),u_vac) #could maybe make this more efficient
126

127
128
129
130
end


function agents_step!(t,modelsol)
131
132

    remake!(modelsol.inf_network,modelsol.index_vectors,modelsol.ws_matrix_tuple.daily)
133
    remake!(modelsol.soc_network,modelsol.index_vectors,modelsol.rest_matrix_tuple.daily)
134
    for network in modelsol.inf_network.graph_list[t]
135
        sample_mixing_graph!(network) #get new contact weights
136
    end
Peter Jentsch's avatar
Peter Jentsch committed
137
    if t == modelsol.params.infection_introduction_day
138
        init_indices = rand(Random.default_rng(Threads.threadid()), 1:modelsol.nodes, round(Int,modelsol.nodes*modelsol.params.I_0_fraction))
139
140
141
        modelsol.u_inf[init_indices] .= Infected
        modelsol.status_totals[Int(Infected)] += length(init_indices)
    end
Peter Jentsch's avatar
Peter Jentsch committed
142
    if t>modelsol.params.infection_introduction_day
143
144
        update_alert_durations!(t,modelsol)
    end
145
    update_vaccination_opinion_state!(t,modelsol,modelsol.status_totals[Int(Infected)])
146
    update_infection_state!(t,modelsol)
147

148
149
150
151
152
153
    modelsol.u_vac .= modelsol.u_next_vac
    modelsol.u_inf .= modelsol.u_next_inf
end



154
155
function solve!(modelsol,recordings...)

156
    for t in 1:modelsol.sim_length
Peter Jentsch's avatar
Peter Jentsch committed
157
        agents_step!(t,modelsol) 
158
        #advance agent states based on the new network
159
160
161
        for recording in recordings
            record!(t,modelsol,recording)
        end
162
163
164
165
    end
end