| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evolution.coalescent; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class ExponentialSawtooth extends ExponentialGrowth { |
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 62 |
|
| 63 |
public double getDemographic(double t) { |
| 64 |
|
| 65 |
t += offset * wavelength; |
| 66 |
|
| 67 |
|
| 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 |
|
| 80 |
return super.getIntensity(t+absOffset) - super.getIntensity(absOffset); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
double intensity = super.getIntensity(wavelength) - super.getIntensity(absOffset); |
| 85 |
|
| 86 |
t -= (wavelength-absOffset); |
| 87 |
|
| 88 |
|
| 89 |
int cycles = (int)Math.floor(t / wavelength); |
| 90 |
intensity += cycles * super.getIntensity(wavelength); |
| 91 |
t -= (cycles * wavelength); |
| 92 |
|
| 93 |
|
| 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 |
|
| 171 |
|
| 172 |
|
| 173 |
private double wavelength; |
| 174 |
private double offset; |
| 175 |
} |