Devashri commited on
Commit
6ccfa45
·
verified ·
1 Parent(s): 8f215a8

Upload SIR_model.py

Browse files
Files changed (1) hide show
  1. SIR_model.py +236 -0
SIR_model.py ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Example of ModECI MDF - SIR model
3
+
4
+ An SIR model is an epidemiological model that computes the theoretical number of people infected with a contagious illness in a closed population over time. The name of this class of models derives from the fact that they involve coupled equations relating the number of susceptible people S(t), number of people infected I(t), and number of people who have recovered R(t).
5
+ """
6
+
7
+
8
+ from modeci_mdf.mdf import*
9
+ import matplotlib.pyplot as plt
10
+ import os
11
+ import sys
12
+
13
+ def main(mode=None):
14
+ # Initialize the Model
15
+ sir_model = Model(id="SIR_Model")
16
+
17
+ # Create a Graph within the Model
18
+ sir_graph = Graph(id="SIR_Graph")
19
+ sir_model.graphs.append(sir_graph)
20
+
21
+ # Parameters for the model
22
+ total_population = 1000
23
+ initial_infected = 1
24
+ initial_recovered = 0
25
+ beta = 0.3 # Infection rate
26
+ gamma = 0.1 # Recovery rate
27
+ initial_susceptible = total_population - initial_infected - initial_recovered
28
+
29
+
30
+ # SIR equation Node
31
+ sir_node = Node(id="id")
32
+
33
+
34
+ total_population = Parameter(id="total_population", value=total_population)
35
+ gamma = Parameter(id="gamma", value=gamma)
36
+ beta = Parameter(id="beta", value=beta)
37
+
38
+ susceptible_population = Parameter(id="susceptible_population",
39
+ default_initial_value=initial_susceptible,
40
+ time_derivative="-beta*susceptible_population*infected_population/total_population"
41
+ )
42
+ infected_population = Parameter(id="infected_population",
43
+ default_initial_value=initial_infected,
44
+ time_derivative="beta*susceptible_population*infected_population/total_population - gamma*infected_population"
45
+ )
46
+ recovered_population = Parameter(id="recovered_population",
47
+ default_initial_value=initial_recovered,
48
+ time_derivative="gamma*infected_population"
49
+ )
50
+ infected_output1 = OutputPort(id="out_port1",value=susceptible_population.id)
51
+ infected_output2 = OutputPort(id="out_port2",value=infected_population.id)
52
+ infected_output3 = OutputPort(id="out_port3",value=recovered_population.id)
53
+
54
+
55
+ sir_node.parameters.append(gamma)
56
+ sir_node.parameters.append(beta)
57
+ sir_node.parameters.append(total_population)
58
+ sir_node.parameters.append(susceptible_population)
59
+ sir_node.parameters.append(infected_population)
60
+ sir_node.parameters.append(recovered_population)
61
+ sir_node.output_ports.append(infected_output1)
62
+ sir_node.output_ports.append(infected_output2)
63
+ sir_node.output_ports.append(infected_output3)
64
+
65
+
66
+ # Recovered Node
67
+ recovered_node = Node(id="Recovered")
68
+ recovered_input = InputPort(id="input_port")
69
+ recovered_output = OutputPort(id="out_port",value=recovered_input.id)
70
+ recovered_node.input_ports.append(recovered_input)
71
+ recovered_node.output_ports.append(recovered_output)
72
+
73
+ #Infected Node
74
+ infected_node = Node(id="Infected")
75
+ infected_input = InputPort(id="input_port")
76
+ infected_output = OutputPort(id="out_port",value=infected_input.id)
77
+ infected_node.input_ports.append(infected_input)
78
+ infected_node.output_ports.append(infected_output)
79
+
80
+
81
+ #Infected Node
82
+ susceptible_node = Node(id="Susceptible")
83
+ susceptible_input = InputPort(id="input_port")
84
+ susceptible_output = OutputPort(id="out_port",value=susceptible_input.id)
85
+ susceptible_node.input_ports.append(susceptible_input)
86
+ susceptible_node.output_ports.append(susceptible_output)
87
+
88
+ # Add nodes to the graph
89
+ sir_graph.nodes.append(sir_node)
90
+ sir_graph.nodes.append(recovered_node)
91
+ sir_graph.nodes.append(infected_node)
92
+ sir_graph.nodes.append(susceptible_node)
93
+
94
+
95
+ # Infected to Recovered transition
96
+ sir_to_rec_edge = Edge(
97
+ id="sir_to_rec",
98
+ sender=sir_node.id,
99
+ sender_port="out_port3",
100
+ receiver=recovered_node.id,
101
+ receiver_port="input_port",
102
+
103
+ )
104
+
105
+ sir_to_inf_edge = Edge(
106
+ id="sir_to_inf",
107
+ sender=sir_node.id,
108
+ sender_port="out_port2",
109
+ receiver=infected_node.id,
110
+ receiver_port="input_port",
111
+ )
112
+
113
+ sir_to_sus_edge = Edge(
114
+ id="sir_to_sus",
115
+ sender=sir_node.id,
116
+ sender_port="out_port1",
117
+ receiver=susceptible_node.id,
118
+ receiver_port="input_port",
119
+ )
120
+
121
+
122
+ # Add edges to the graph
123
+ sir_graph.edges.append(sir_to_rec_edge)
124
+ sir_graph.edges.append(sir_to_inf_edge)
125
+ sir_graph.edges.append(sir_to_sus_edge)
126
+
127
+ if mode=="run":
128
+
129
+ from modeci_mdf.execution_engine import EvaluableGraph
130
+ eg = EvaluableGraph(sir_graph, verbose=False)
131
+ eg.evaluate()
132
+
133
+ dt = 1
134
+
135
+ duration = 100
136
+ t = 0
137
+ times = []
138
+ s = []
139
+ i = []
140
+ r = []
141
+ while t <= duration:
142
+ times.append(t)
143
+ print("====== Evaluating at t = %s ======" % (t))
144
+ if t == 0:
145
+ eg.evaluate()
146
+ else:
147
+ eg.evaluate(time_increment=dt)
148
+
149
+ s.append(eg.enodes["id"].evaluable_outputs["out_port1"].curr_value)
150
+ i.append(eg.enodes["id"].evaluable_outputs["out_port2"].curr_value)
151
+ r.append(eg.enodes["id"].evaluable_outputs["out_port3"].curr_value)
152
+
153
+ t += dt
154
+ print('Susceptible polution: %s'%eg.enodes["id"].evaluable_outputs["out_port1"].curr_value)
155
+ print('Infected polution: %s'%eg.enodes["id"].evaluable_outputs["out_port2"].curr_value)
156
+ print('Recovered polution: %s'%eg.enodes["id"].evaluable_outputs["out_port3"].curr_value)
157
+
158
+ # Create subplots
159
+ fig1, axs = plt.subplots(1, 3, figsize=(15, 5),sharey=True)
160
+
161
+ # Plotting Susceptible population
162
+ axs[0].plot(times, s, label='Susceptible', color='blue')
163
+ axs[0].set_xlabel('Time')
164
+ axs[0].set_ylabel('Susceptible Population')
165
+ axs[0].set_title('Susceptible Population over Time')
166
+ axs[0].legend()
167
+ axs[0].grid(True)
168
+
169
+ # Plotting Infected population
170
+ axs[1].plot(times, i, label='Infected', color='orange')
171
+ axs[1].set_xlabel('Time')
172
+ axs[1].set_ylabel('Infected Population')
173
+ axs[1].set_title('Infected Population over Time')
174
+ axs[1].legend()
175
+ axs[1].grid(True)
176
+
177
+ # Plotting Recovered population
178
+ axs[2].plot(times, r, label='Recovered', color='green')
179
+ axs[2].set_xlabel('Time')
180
+ axs[2].set_ylabel('Recovered Population')
181
+ axs[2].set_title('Recovered Population over Time')
182
+ axs[2].legend()
183
+ axs[2].grid(True)
184
+
185
+ plt.tight_layout() # Adjust layout to prevent overlap
186
+ plt.show()
187
+
188
+
189
+ # Plotting
190
+ fig2 = plt.figure(figsize=(10, 5))
191
+ plt.plot(times, s, label='Susceptible')
192
+ plt.plot(times, i, label='Infected')
193
+ plt.plot(times, r, label='Recovered')
194
+ plt.xlabel('Time')
195
+ plt.ylabel('Population')
196
+ plt.title('Population over time')
197
+ plt.legend()
198
+ plt.grid(True)
199
+ plt.show()
200
+
201
+ return [fig1, fig2]
202
+
203
+ elif mode=="graph":
204
+
205
+ sir_model.to_graph_image(
206
+ engine="dot",
207
+ output_format="png",
208
+ view_on_render=False,
209
+ level=3,
210
+ filename_root="sir_model",
211
+ is_horizontal=True
212
+ )
213
+
214
+ from IPython.display import Image
215
+ Image(filename="sir_model.png")
216
+ image_path = "sir_model.png"
217
+ return image_path
218
+
219
+
220
+
221
+ return sir_graph
222
+
223
+ if __name__ == "__main__":
224
+ # Check if there are any command line arguments
225
+ if len(sys.argv) > 1:
226
+ # Assuming the second argument is the mode (e.g., '-run' or '-graph')
227
+ mode_arg = sys.argv[1]
228
+ if mode_arg == "-run":
229
+ main(mode="run")
230
+ elif mode_arg == "-graph":
231
+ main(mode="graph")
232
+ else:
233
+ print("Invalid argument. Please use '-run' or '-graph'.")
234
+ else:
235
+ print("No arguments provided. Please specify '-run' or '-graph'.")
236
+