Debian Codegraph @

src/dr/evomodel/epidemiology/DynamicalVariable.java

raw symbols SCIP
1 /*
2 * DynamicalVariable.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.Collections;
31
32 /**
33 * @author Trevor Bedford
34 */
35 public class DynamicalVariable {
36
37 private String name = "variable";
38 private List<Double> times = new ArrayList<Double>();
39 private List<Double> values = new ArrayList<Double>();
40 private double currentTime = 0.0;
41 private double currentValue = 0.0;
42
43 private List<Double> storedTimes = new ArrayList<Double>();
44 private List<Double> storedValues = new ArrayList<Double>();
45 private double storedTime = 0.0;
46 private double storedValue = 0.0;
47
48 // initialize variable
49 public DynamicalVariable(String n, double t0, double v0) {
50 name = n;
51 currentTime = t0;
52 currentValue = v0;
53 add(currentTime, currentValue);
54 }
55
56 // add a time / value pair
57 public void add(double t, double v) {
58 Double tD = new Double(t);
59 Double vD = new Double(v);
60 times.add(tD);
61 values.add(vD);
62 }
63
64 public void pushCurrentState() {
65 add(currentTime,currentValue);
66 }
67
68 public void modCurrentTime(double dt) {
69 currentTime += dt;
70 }
71
72 public void modCurrentValue(double dv) {
73 currentValue += dv;
74 }
75
76 // reset arrays
77 public void reset(double t0, double v0) {
78 currentTime = t0;
79 currentValue = v0;
80 times.clear();
81 values.clear();
82 add(currentTime, currentValue);
83 }
84
85 // copy values to stored state
86 public void store() {
87
88 storedTimes.clear();
89 for (Double t : times) {
90 Double tD = new Double(t.doubleValue());
91 storedTimes.add(tD);
92 }
93 storedValues.clear();
94 for (Double v : values) {
95 Double vD = new Double(v.doubleValue());
96 storedValues.add(vD);
97 }
98 storedTime = currentTime;
99 storedValue = currentValue;
100
101 }
102
103 // copy values from stored state
104 public void restore() {
105
106 times.clear();
107 for (Double t : storedTimes) {
108 Double tD = new Double(t.doubleValue());
109 times.add(tD);
110 }
111 values.clear();
112 for (Double v : storedValues) {
113 Double vD = new Double(v.doubleValue());
114 values.add(vD);
115 }
116 currentTime = storedTime;
117 currentValue = storedValue;
118
119 }
120
121 public int size() {
122 return times.size();
123 }
124
125 public String getName() {
126 return name;
127 }
128
129 public double getTimeAtIndex(int i) {
130 return times.get(i).doubleValue();
131 }
132
133 public double getValueAtIndex(int i) {
134 return values.get(i).doubleValue();
135 }
136
137 // pull value closest to given point in time
138 // uses linear interpolation when the exact time point isn't present
139 public double getValue(double t) {
140
141 double rval = 0.0;
142 Double tD = new Double(t);
143 int index = Collections.binarySearch(times, tD);
144
145 // found exact match
146 if (index >= 0) {
147 Double vD = values.get(index);
148 rval = vD.doubleValue();
149 }
150
151 // need to interpolate between two adjacent values
152 else {
153 int a = Math.abs(index) - 2;
154 int b = Math.abs(index) - 1;
155 double t0 = getTimeAtIndex(a);
156 double v0 = getValueAtIndex(a);
157 double t1 = getTimeAtIndex(b);
158 double v1 = getValueAtIndex(b);
159 rval = v0 + (t-t0) * ( (v1-v0) / (t1-t0) );
160 }
161
162 return rval;
163 }
164
165 // return integral between two points in time
166 // approximation via trapezoidal rule
167 public double getIntegral(double start, double finish) {
168
169 // first index after start and last index before finish
170 int a = 0;
171 int b = 0;
172 double sum = 0.0;
173
174 // if not exact match, need first index after than start
175 int index = Collections.binarySearch(times, new Double(start));
176 if (index >= 0)
177 a = index;
178 else
179 a = Math.abs(index) - 1;
180
181 // if not exact match, need last index before than finish
182 index = Collections.binarySearch(times, new Double(finish));
183 if (index >= 0)
184 b = index;
185 else
186 b = Math.abs(index) - 2;
187
188 // from start to a
189 sum += 0.5 * (getTimeAtIndex(a) - start) * (getValueAtIndex(a) + getValue(start));
190
191 // between a and b
192 for (index = a; index < b; index++) {
193 sum += 0.5 * (getTimeAtIndex(index+1) - getTimeAtIndex(index)) * (getValueAtIndex(index+1) + getValueAtIndex(index));
194 }
195
196 // b to finish
197 sum += 0.5 * (finish - getTimeAtIndex(b)) * (getValue(finish) + getValueAtIndex(b));
198
199 return sum;
200
201 }
202
203 public double getAverage(double start, double finish) {
204 return 0.5*(getValue(start) + getValue(finish));
205 }
206
207 public void print() {
208 System.out.println(name);
209 for (int i=0; i<size(); i++) {
210 System.out.println(times.get(i) + "\t" + values.get(i));
211 }
212 }
213
214 }