Debian Codegraph @

src/dr/evolution/coalescent/ExponentialSawtooth.java

raw symbols SCIP
1 /*
2 * ExponentialSawtooth.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.evolution.coalescent;
27
28 /**
29 * This class models a sawtooth demographic in which exponential growth is punctuated by frequent periodic crashes.
30 *
31 * @version $Id: ExponentialSawtooth.java,v 1.3 2005/04/11 11:43:03 alexei Exp $
32 *
33 * @author Alexei Drummond
34 */
35 public class ExponentialSawtooth extends ExponentialGrowth {
36
37 /**
38 * Construct demographic model with default settings
39 */
40 public ExponentialSawtooth(Type units) {
41
42 super(units);
43 }
44
45 public final double getWavelength() { return wavelength; }
46
47 public final void setWavelength(double t) {
48 if (t <= 0) throw new IllegalArgumentException();
49 wavelength = t;
50 }
51
52 public final double getOffset() { return offset; }
53
54 public final void setOffset(double offset) {
55 if (offset < 0 || offset >= 1.0) {
56 throw new IllegalArgumentException();
57 }
58 this.offset = offset;
59 }
60
61 // Implementation of abstract methods
62
63 public double getDemographic(double t) {
64
65 t += offset * wavelength;
66
67 // rescale t so that 0 <= t < wavelength
68 int cycle = (int)Math.floor(t / wavelength);
69 t -= (cycle * wavelength);
70
71 return super.getDemographic(t);
72 }
73
74 public double getIntensity(double t) {
75
76 double absOffset = offset*wavelength;
77
78 if (t < wavelength-absOffset) {
79 // calculate intensity of first partial offset
80 return super.getIntensity(t+absOffset) - super.getIntensity(absOffset);
81 }
82
83 // calculate intensity of first epoch:
84 double intensity = super.getIntensity(wavelength) - super.getIntensity(absOffset);
85
86 t -= (wavelength-absOffset);
87
88 // calculate intensity of all full cycles
89 int cycles = (int)Math.floor(t / wavelength);
90 intensity += cycles * super.getIntensity(wavelength);
91 t -= (cycles * wavelength);
92
93 // calculate intensity of last partial cycle
94 intensity += super.getIntensity(t);
95
96 return intensity;
97 }
98
99 public double getInverseIntensity(double x) {
100
101 throw new UnsupportedOperationException();
102 }
103
104 public int getNumArguments() {
105 return 4;
106 }
107
108 public String getArgumentName(int n) {
109
110 switch (n) {
111 case 0: return "N0";
112 case 1: return "r";
113 case 2: return "wavelength";
114 case 3: return "offset";
115 default: throw new IllegalArgumentException();
116 }
117 }
118
119 public double getArgument(int n) {
120 switch (n) {
121 case 0: return getN0();
122 case 1: return getGrowthRate();
123 case 2: return getWavelength();
124 case 3: return getOffset();
125 default: throw new IllegalArgumentException();
126 }
127 }
128
129 public void setArgument(int n, double value) {
130 switch (n) {
131 case 0: setN0(value); break;
132 case 1: setGrowthRate(value); break;
133 case 2: setWavelength(value); break;
134 case 3: setOffset(value); break;
135 default: throw new IllegalArgumentException();
136 }
137 }
138
139 public DemographicFunction getCopy() {
140 ExponentialSawtooth df = new ExponentialSawtooth(getUnits());
141 df.setN0(getN0());
142 df.setGrowthRate(getGrowthRate());
143 df.setWavelength(getWavelength());
144 df.setOffset(getOffset());
145
146 return df;
147 }
148
149 public static void main(String[] args) {
150
151 double N0 = Double.parseDouble(args[0]);
152 double growthRate = Double.parseDouble(args[1]);
153 double wavelength = Double.parseDouble(args[2]);
154 double offset = Double.parseDouble(args[3]);
155
156
157 ExponentialSawtooth est = new ExponentialSawtooth(Type.SUBSTITUTIONS);
158 est.setN0(N0);
159 est.setGrowthRate(growthRate);
160 est.setWavelength(wavelength);
161 est.setOffset(offset);
162
163 for (double time = 0; time < 20; time+=0.1) {
164 System.out.println(time + "\t" + est.getDemographic(time) + "\t" + est.getIntensity(time));
165 }
166 }
167
168
169 //
170 // private stuff
171 //
172
173 private double wavelength;
174 private double offset;
175 }