Program: 57408
Project: 57408
Buggy source:
/** 
 * Integrate the differential equations and the variational equations up to the given time. <p>This method solves an Initial Value Problem (IVP) and also computes the derivatives of the solution with respect to initial state and parameters. This can be used as a basis to solve Boundary Value Problems (BVP).</p> <p>Since this method stores some internal state variables made available in its public interface during integration (  {@link #getCurrentSignedStepsize()}), it is <em>not</em> thread-safe.</p>
 * @param equations differential equations to integrate
 * @param t0 initial time
 * @param y0 initial value of the state vector at t0
 * @param dY0dP initial value of the state vector derivative with respect to theparameters at t0
 * @param t target time for the integration(can be set to a value smaller than <code>t0</code> for backward integration)
 * @param y placeholder where to put the state vector at each successfulstep (and hence at the end of integration), can be the same object as y0
 * @param dYdY0 placeholder where to put the state vector derivative with respectto the initial state (dy[i]/dy0[j] is in element array dYdY0[i][j]) at each successful step (and hence at the end of integration)
 * @param dYdP placeholder where to put the state vector derivative with respectto the parameters (dy[i]/dp[j] is in element array dYdP[i][j]) at each successful step (and hence at the end of integration)
 * @return stop time, will be the same as target time if integration reached itstarget, but may be different if some event handler stops it at some point.
 * @throws IntegratorException if the integrator cannot perform integration
 * @throws DerivativeException this exception is propagated to the caller ifthe underlying user function triggers one
 */
public double integrate(final double t0,final double[] y0,final double[][] dY0dP,final double t,final double[] y,final double[][] dYdY0,final double[][] dYdP) throws DerivativeException, IntegratorException {
  final int n=ode.getDimension();
  final int k=ode.getParametersDimension();
  checkDimension(n,y0);
  checkDimension(n,y);
  checkDimension(n,dYdY0);
  checkDimension(n,dYdY0[0]);
  if (k != 0) {
    checkDimension(n,dY0dP);
    checkDimension(k,dY0dP[0]);
    checkDimension(n,dYdP);
    checkDimension(k,dYdP[0]);
  }
  final int q=n * (1 + n + k);
  final double[] z=new double[q];
  System.arraycopy(y0,0,z,0,n);
  for (int i=0; i < n; ++i) {
    z[i * (1 + n) + n]=1.0;
    System.arraycopy(dY0dP[i],0,z,n * (n + 1) + i * k,k);
  }
  final double stopTime=integrator.integrate(new FirstOrderDifferentialEquations(){
    /** 
 * Current state. 
 */
    private final double[] y=new double[n];
    /** 
 * Time derivative of the current state. 
 */
    private final double[] yDot=new double[n];
    /** 
 * Derivatives of yDot with respect to state. 
 */
    private final double[][] dFdY=new double[n][n];
    /** 
 * Derivatives of yDot with respect to parameters. 
 */
    private final double[][] dFdP=new double[n][k];
    /** 
 * {@inheritDoc} 
 */
    public int getDimension(){
      return q;
    }
    /** 
 * {@inheritDoc} 
 */
    public void computeDerivatives(    final double t,    final double[] z,    final double[] zDot) throws DerivativeException {
      System.arraycopy(z,0,y,0,n);
      ode.computeDerivatives(t,y,yDot);
      ode.computeJacobians(t,y,yDot,dFdY,dFdP);
      System.arraycopy(yDot,0,zDot,0,n);
      for (int i=0; i < n; ++i) {
        final double[] dFdYi=dFdY[i];
        for (int j=0; j < n; ++j) {
          double s=0;
          int zIndex=n + j;
          for (int l=0; l < n; ++l) {
            s+=dFdYi[l] * z[zIndex];
            zIndex+=l;
          }
          zDot[n + i * n + j]=s;
        }
      }
      for (int i=0; i < n; ++i) {
        final double[] dFdYi=dFdY[i];
        final double[] dFdPi=dFdP[i];
        for (int j=0; j < k; ++j) {
          double s=dFdPi[j];
          int zIndex=n * (n + 1) + j;
          for (int l=0; l < n; ++l) {
            s+=dFdYi[l] * z[zIndex];
            zIndex+=k;
          }
          zDot[n * (n + 1) + i * k + j]=s;
        }
      }
    }
  }
,t0,z,t,z);
  System.arraycopy(z,0,y,0,n);
  for (int i=0; i < n; ++i) {
    System.arraycopy(z,n * (i + 1),dYdY0[i],0,n);
  }
  for (int i=0; i < n; ++i) {
    System.arraycopy(z,n * (n + 1) + i * k,dYdP[i],0,k);
  }
  return stopTime;
}

Fixed version (ground truth):
/** 
 * Integrate the differential equations and the variational equations up to the given time. <p>This method solves an Initial Value Problem (IVP) and also computes the derivatives of the solution with respect to initial state and parameters. This can be used as a basis to solve Boundary Value Problems (BVP).</p> <p>Since this method stores some internal state variables made available in its public interface during integration (  {@link #getCurrentSignedStepsize()}), it is <em>not</em> thread-safe.</p>
 * @param equations differential equations to integrate
 * @param t0 initial time
 * @param y0 initial value of the state vector at t0
 * @param dY0dP initial value of the state vector derivative with respect to theparameters at t0
 * @param t target time for the integration(can be set to a value smaller than <code>t0</code> for backward integration)
 * @param y placeholder where to put the state vector at each successfulstep (and hence at the end of integration), can be the same object as y0
 * @param dYdY0 placeholder where to put the state vector derivative with respectto the initial state (dy[i]/dy0[j] is in element array dYdY0[i][j]) at each successful step (and hence at the end of integration)
 * @param dYdP placeholder where to put the state vector derivative with respectto the parameters (dy[i]/dp[j] is in element array dYdP[i][j]) at each successful step (and hence at the end of integration)
 * @return stop time, will be the same as target time if integration reached itstarget, but may be different if some event handler stops it at some point.
 * @throws IntegratorException if the integrator cannot perform integration
 * @throws DerivativeException this exception is propagated to the caller ifthe underlying user function triggers one
 */
public double integrate(final double t0,final double[] y0,final double[][] dY0dP,final double t,final double[] y,final double[][] dYdY0,final double[][] dYdP) throws DerivativeException, IntegratorException {
  final int n=ode.getDimension();
  final int k=ode.getParametersDimension();
  checkDimension(n,y0);
  checkDimension(n,y);
  checkDimension(n,dYdY0);
  checkDimension(n,dYdY0[0]);
  if (k != 0) {
    checkDimension(n,dY0dP);
    checkDimension(k,dY0dP[0]);
    checkDimension(n,dYdP);
    checkDimension(k,dYdP[0]);
  }
  final int q=n * (1 + n + k);
  final double[] z=new double[q];
  System.arraycopy(y0,0,z,0,n);
  for (int i=0; i < n; ++i) {
    z[i * (1 + n) + n]=1.0;
    System.arraycopy(dY0dP[i],0,z,n * (n + 1) + i * k,k);
  }
  final double stopTime=integrator.integrate(new FirstOrderDifferentialEquations(){
    /** 
 * Current state. 
 */
    private final double[] y=new double[n];
    /** 
 * Time derivative of the current state. 
 */
    private final double[] yDot=new double[n];
    /** 
 * Derivatives of yDot with respect to state. 
 */
    private final double[][] dFdY=new double[n][n];
    /** 
 * Derivatives of yDot with respect to parameters. 
 */
    private final double[][] dFdP=new double[n][k];
    /** 
 * {@inheritDoc} 
 */
    public int getDimension(){
      return q;
    }
    /** 
 * {@inheritDoc} 
 */
    public void computeDerivatives(    final double t,    final double[] z,    final double[] zDot) throws DerivativeException {
      System.arraycopy(z,0,y,0,n);
      ode.computeDerivatives(t,y,yDot);
      ode.computeJacobians(t,y,yDot,dFdY,dFdP);
      System.arraycopy(yDot,0,zDot,0,n);
      for (int i=0; i < n; ++i) {
        final double[] dFdYi=dFdY[i];
        for (int j=0; j < n; ++j) {
          double s=0;
          final int startIndex=n + j;
          int zIndex=startIndex;
          for (int l=0; l < n; ++l) {
            s+=dFdYi[l] * z[zIndex];
            zIndex+=n;
          }
          zDot[startIndex + i * n]=s;
        }
      }
      for (int i=0; i < n; ++i) {
        final double[] dFdYi=dFdY[i];
        final double[] dFdPi=dFdP[i];
        for (int j=0; j < k; ++j) {
          double s=dFdPi[j];
          final int startIndex=n * (n + 1) + j;
          int zIndex=startIndex;
          for (int l=0; l < n; ++l) {
            s+=dFdYi[l] * z[zIndex];
            zIndex+=k;
          }
          zDot[startIndex + i * k]=s;
        }
      }
    }
  }
,t0,z,t,z);
  System.arraycopy(z,0,y,0,n);
  for (int i=0; i < n; ++i) {
    System.arraycopy(z,n * (i + 1),dYdY0[i],0,n);
  }
  for (int i=0; i < n; ++i) {
    System.arraycopy(z,n * (n + 1) + i * k,dYdP[i],0,k);
  }
  return stopTime;
}