Debian Codegraph @

src/dr/evomodel/epidemiology/DynamicalSystem.java

raw symbols SCIP
1 /*
2 * DynamicalSystem.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.epidemiology;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.HashMap;
31
32 /**
33 * @author Trevor Bedford
34 */
35 public class DynamicalSystem {
36
37 private List<DynamicalVariable> variables = new ArrayList<DynamicalVariable>();
38 private List<DynamicalForce> forces = new ArrayList<DynamicalForce>();
39 private HashMap<String,DynamicalVariable> varMap = new HashMap<String,DynamicalVariable>();
40 private HashMap<String,DynamicalForce> forceMap = new HashMap<String,DynamicalForce>();
41
42 private double currentTime = 0.0;
43 private double startTime = 0.0;
44 private double timeStep = 0.0;
45
46 private double storedCurrentTime = 0.0;
47 private double storedStartTime = 0.0;
48
49 public static void main(String[] args) {
50
51 DynamicalSystem syst = new DynamicalSystem(0, 0.001);
52
53 double transmissionRate = 0.027;
54 double recoveryRate = 0.00054;
55
56 syst.addVariable("susceptibles", 330);
57 syst.addVariable("infecteds", 23325);
58 syst.addVariable("recovereds", 4524);
59 syst.addVariable("total", 330 + 23325 + 4524);
60 syst.addForce("contact", transmissionRate, new String[]{"infecteds","susceptibles"}, new String[]{"total"}, "susceptibles", "infecteds");
61 syst.addForce("recovery", recoveryRate, new String[]{"infecteds"}, new String[]{}, "infecteds", "recovereds");
62
63 // while (syst.getTime() < 400) {
64 // syst.step();
65 // }
66 // syst.print(0,400,1);
67
68 double val = syst.getValue("susceptibles", 500);
69 System.out.println(val);
70 syst.print(0,500,10);
71
72 }
73
74 public DynamicalSystem(double t0, double dt) {
75 startTime = t0;
76 currentTime = startTime;
77 timeStep = dt;
78 }
79
80 public double getTime() {
81 return currentTime;
82 }
83
84 public int size() {
85 return variables.size();
86 }
87
88 public DynamicalVariable getVar(String n) {
89 return varMap.get(n);
90 }
91
92 public DynamicalForce getForce(String n) {
93 return forceMap.get(n);
94 }
95
96 public void resetVar(String n, double v0) {
97 DynamicalVariable var = getVar(n);
98 var.reset(startTime, v0);
99 }
100
101 public void resetForce(String n, double c) {
102 DynamicalForce frc = getForce(n);
103 frc.reset(c);
104 }
105
106 public void resetTime() {
107 currentTime = startTime;
108 }
109
110 // copy values to stored state
111 public void store() {
112 storedCurrentTime = currentTime;
113 storedStartTime = startTime;
114 for (DynamicalVariable var : variables) {
115 var.store();
116 }
117 for (DynamicalForce frc : forces) {
118 frc.store();
119 }
120 }
121
122 // copy values from stored state
123 public void restore() {
124 currentTime = storedCurrentTime;
125 startTime = storedStartTime;
126 for (DynamicalVariable var : variables) {
127 var.restore();
128 }
129 for (DynamicalForce frc : forces) {
130 frc.restore();
131 }
132 }
133
134 // get value of indexed variable at time t
135 // dynamically extend trace
136 public double getValue(int index, double t) {
137 while (currentTime < t) {
138 step();
139 }
140 DynamicalVariable var = variables.get(index);
141 return var.getValue(t);
142 }
143
144 // get value of named variable at time t
145 // dynamically extend trace
146 public double getValue(String n, double t) {
147 while (currentTime < t) {
148 step();
149 }
150 DynamicalVariable var = getVar(n);
151 return var.getValue(t);
152 }
153
154 // get integral of indexed variable between times start and finish
155 // dynamically extend trace
156 public double getIntegral(int index, double start, double finish) {
157 while (currentTime < finish) {
158 step();
159 }
160 DynamicalVariable var = variables.get(index);
161 return var.getIntegral(start, finish);
162 }
163
164 // get integral of named variable between times start and finish
165 // dynamically extend trace
166 public double getIntegral(String n, double start, double finish) {
167 while (currentTime < finish) {
168 step();
169 }
170 DynamicalVariable var = getVar(n);
171 return var.getIntegral(start, finish);
172 }
173
174 // get average of indexed variable between times start and finish
175 // dynamically extend trace
176 public double getAverage(int index, double start, double finish) {
177 while (currentTime < finish) {
178 step();
179 }
180 DynamicalVariable var = variables.get(index);
181 return var.getAverage(start, finish);
182 }
183
184 // get average of named variable between times start and finish
185 // dynamically extend trace
186 public double getAverage(String n, double start, double finish) {
187 while (currentTime < finish) {
188 step();
189 }
190 DynamicalVariable var = getVar(n);
191 return var.getAverage(start, finish);
192 }
193
194 public void addVariable(String n, double v0) {
195 DynamicalVariable var = new DynamicalVariable(n, startTime, v0);
196 varMap.put(n, var);
197 variables.add(var);
198 }
199
200 public void addForce(String n, double coeff, String[] mult, String[] div, String increasing, String decreasing) {
201 DynamicalForce frc = new DynamicalForce(n, coeff, getVar(increasing), getVar(decreasing));
202 for (String s : mult) {
203 frc.addMultiplier(getVar(s));
204 }
205 for (String s : div) {
206 frc.addDivisor(getVar(s));
207 }
208 forceMap.put(n, frc);
209 forces.add(frc);
210 }
211
212 public void step() {
213 for (DynamicalForce frc : forces) {
214 frc.modCurrentValue(currentTime, timeStep);
215 }
216 for (DynamicalVariable var : variables) {
217 var.modCurrentTime(timeStep);
218 var.pushCurrentState();
219 }
220 currentTime += timeStep;
221 }
222
223 public void print(double start, double finish, double step) {
224
225 System.out.print("time");
226 for (DynamicalVariable var : variables) {
227 System.out.print("\t" + var.getName());
228 }
229 System.out.println();
230
231 for (double t=start; t<=finish; t += step) {
232 System.out.printf("%.3f", t);
233 for (DynamicalVariable var : variables) {
234 System.out.printf("\t%.3f", var.getValue(t));
235 }
236 System.out.println();
237 }
238
239 }
240
241 public String printValues(String n, double start, double finish, double step) {
242
243 String out = "";
244 DynamicalVariable var = getVar(n);
245 for (double t=start; t<=finish; t += step) {
246 double v = var.getValue(t);
247 out += "\t";
248 out += Double.toString(v);
249 }
250 return out;
251
252 }
253
254 }