/* * ODEDemographicFunction.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ /* * Base class for demographic models based on numerically-integrated ODEs * */ package dr.evomodel.epidemiology; import dr.evolution.coalescent.*; /** * This interface provides methods that describe a demographic function. * @author Daniel Wilson */ /*public interface ODEDemographicFunction extends DemographicFunction { public abstract class Abstract implements ODEDemographicFunction { public Abstract(Type units) { setUnits(units); } public double getLogDemographic(double t) { return Math.log(getDemographic(t)); } public double getIntegral(double start, double finish) { return getIntensity(finish) - getIntensity(start); } } }*/ public abstract class ODEDemographicFunction extends DemographicFunction.Abstract { public ODEDemographicFunction(Type units) { super(units); } // Implement abstract types from base class /** * Default implementation * @param t * @return log(demographic at t) */ /*public double getLogDemographic(double t) { return Math.log(getDemographic(t)); }*/ /** * Calculates the integral 1/N(x) dx between start and finish. */ /*public double getIntegral(double start, double finish) { return getIntensity(finish) - getIntensity(start); }*/ /** * Returns the integral of 1/N(x) between start and finish, calling either the getAnalyticalIntegral or * getNumericalIntegral function as appropriate. */ public double getNumericalIntegral(double start, double finish) { throw new RuntimeException("not implemented"); } public double getDemographic(double t) { Evaluate(t); if(RKfail) return 0.0; return getDemographicFromPrevalence(Ynow,t); } public double getIntensity(double t) { Evaluate(t); if(RKfail) return Math.log(0.0); return Ynow[0]; } // Implement the following abstract functions: /** * Calculate the derivatives and store in dydt */ abstract void derivs(double t, double[] y, double[] dydt); /** * Set initial values of y * for(i=0;iT[klast]) { if(klast+1>=kmax) { // 3rd December 2011: Previous behaviour was stupid, now increase storage capacity // Assume linear, but flag warning //RKwarning = true; //for(i=0;iklast-1) k1 = klast-1; if(k1<0) k1 = 0; int k2 = k1+1; if(k2>klast || T[k1]>t) { while(k2>klast || T[k1]>t) { --k1; --k2; } } else if(k1<0 || T[k2]<=t) { while(k1<0 || T[k2]<=t) { ++k1; ++k2; } } if(T[k1]==t) { for(i=0;iMAXSTP/2) { tmp = tmp+3; } // Calculate the derivatives at the present time derivs(t,y,dydt); // Calculate appropriate scalings for the error tolerance for(i=0;i Math.abs(dtsav)) { ++klast; for (i=0;i 0.0) h=t2-t; // Perform the adaptive integration and update time t = rkqs(y,dydt,t,h,yscal); // Was the predicted step size used? if(hdid == h) ++nok; else ++nbad; // If the target t2 has been reached if((t-t2)*(t2-t1) >= 0.0) { // Store state at t2 if (klast < kmax-1) { ++klast; for (i=0;i= 0.0 ? Math.max(htemp,0.1*h) : Math.min(htemp,0.1*h)); } tnew=t+h; if(tnew == t) { throw new RuntimeException("stepsize underflow in rkqs"); } } if(errmax > ERRCON) hnext=SAFETY*h*Math.pow(errmax,PGROW); else hnext=5.0*h; t += (hdid=h); for(i=0;i