org.apache.commons.math.ode.sampling.StepHandler Java Examples

The following examples show how to use org.apache.commons.math.ode.sampling.StepHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ThreeEighthesIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStepSize()
  throws MathUserException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                Assert.assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #2
Source File: ClassicalRungeKuttaIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStepSize()
  throws MathUserException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                Assert.assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #3
Source File: GillIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStepSize()
  throws MathUserException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new GillIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                Assert.assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #4
Source File: MidpointIntegratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStepSize()
  throws MathUserException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new MidpointIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                Assert.assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #5
Source File: MidpointIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new MidpointIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        private static final long serialVersionUID = 0L;
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #6
Source File: ThreeEighthesIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #7
Source File: GillIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new GillIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        private static final long serialVersionUID = 0L;
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #8
Source File: GraggBulirschStoerIntegrator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void addStepHandler (final StepHandler handler) {

  super.addStepHandler(handler);
  denseOutput = requiresDenseOutput() || (! eventsHandlersManager.isEmpty());

  // reinitialize the arrays
  initializeArrays();

}
 
Example #9
Source File: AbstractIntegrator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build an instance.
 * @param name name of the method
 */
public AbstractIntegrator(final String name) {
    this.name = name;
    stepHandlers = new ArrayList<StepHandler>();
    stepStart = Double.NaN;
    stepSize  = Double.NaN;
    eventsHandlersManager = new CombinedEventsManager();
    setMaxEvaluations(-1);
    resetEvaluations();
}
 
Example #10
Source File: ClassicalRungeKuttaIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }          
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        private static final long serialVersionUID = 0L;
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #11
Source File: MidpointIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new MidpointIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }          
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        private static final long serialVersionUID = 0L;
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #12
Source File: ClassicalRungeKuttaIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        private static final long serialVersionUID = 0L;
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #13
Source File: ClassicalRungeKuttaIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        private static final long serialVersionUID = 0L;
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #14
Source File: ClassicalRungeKuttaIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        private static final long serialVersionUID = 0L;
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #15
Source File: EulerIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new EulerIntegrator(step);
    integ.addStepHandler(new StepHandler() {
      public void handleStep(StepInterpolator interpolator, boolean isLast) {
          if (! isLast) {
              assertEquals(step,
                           interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                           1.0e-12);
          }
      }
      public boolean requiresDenseOutput() {
          return false;
      }
      public void reset() {
      }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
                        private static final long serialVersionUID = 0L;
                        public void computeDerivatives(double t, double[] y, double[] dot) {
                            dot[0] = 1.0;
                        }
                        public int getDimension() {
                            return 1;
                        }
                    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #16
Source File: MidpointIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws MathUserException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new MidpointIntegrator(step);
    integ.addStepHandler(new StepHandler() {
        public void handleStep(StepInterpolator interpolator, boolean isLast) {
            if (! isLast) {
                assertEquals(step,
                             interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                             1.0e-12);
            }
        }
        public boolean requiresDenseOutput() {
            return false;
        }
        public void reset() {
        }
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
        private static final long serialVersionUID = 0L;
        public void computeDerivatives(double t, double[] y, double[] dot) {
            dot[0] = 1.0;
        }
        public int getDimension() {
            return 1;
        }
    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #17
Source File: EulerIntegratorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testStepSize()
  throws DerivativeException, IntegratorException {
    final double step = 1.23456;
    FirstOrderIntegrator integ = new EulerIntegrator(step);
    integ.addStepHandler(new StepHandler() {
      public void handleStep(StepInterpolator interpolator, boolean isLast) {
          if (! isLast) {
              assertEquals(step,
                           interpolator.getCurrentTime() - interpolator.getPreviousTime(),
                           1.0e-12);
          }
      }
      public boolean requiresDenseOutput() {
          return false;
      }
      public void reset() {
      }          
    });
    integ.integrate(new FirstOrderDifferentialEquations() {
                        private static final long serialVersionUID = 0L;
                        public void computeDerivatives(double t, double[] y, double[] dot) {
                            dot[0] = 1.0;
                        }
                        public int getDimension() {
                            return 1;
                        }
                    }, 0.0, new double[] { 0.0 }, 5.0, new double[1]);
}
 
Example #18
Source File: AbstractIntegrator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Check if dense output is needed.
 * @return true if there is at least one event handler or if
 * one of the step handlers requires dense output
 */
protected boolean requiresDenseOutput() {
    if (!eventsStates.isEmpty()) {
        return true;
    }
    for (StepHandler handler : stepHandlers) {
        if (handler.requiresDenseOutput()) {
            return true;
        }
    }
    return false;
}
 
Example #19
Source File: HighamHall54StepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void checkClone()
throws MathUserException, IntegratorException {
  TestProblem3 pb = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;
  HighamHall54Integrator integ = new HighamHall54Integrator(minStep, maxStep,
                                                            scalAbsoluteTolerance,
                                                            scalRelativeTolerance);
  integ.addStepHandler(new StepHandler() {
      public void handleStep(StepInterpolator interpolator, boolean isLast)
      throws MathUserException {
          StepInterpolator cloned = interpolator.copy();
          double tA = cloned.getPreviousTime();
          double tB = cloned.getCurrentTime();
          double halfStep = FastMath.abs(tB - tA) / 2;
          assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12);
          assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12);
          for (int i = 0; i < 10; ++i) {
              double t = (i * tB + (9 - i) * tA) / 9;
              interpolator.setInterpolatedTime(t);
              assertTrue(FastMath.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10));
              cloned.setInterpolatedTime(t);
              assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12);
              double[] referenceState = interpolator.getInterpolatedState();
              double[] cloneState     = cloned.getInterpolatedState();
              for (int j = 0; j < referenceState.length; ++j) {
                  assertEquals(referenceState[j], cloneState[j], 1.0e-12);
              }
          }
      }
      public boolean requiresDenseOutput() {
          return true;
      }
      public void reset() {
      }
  });
  integ.integrate(pb,
          pb.getInitialTime(), pb.getInitialState(),
          pb.getFinalTime(), new double[pb.getDimension()]);

}
 
Example #20
Source File: MidpointStepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void serialization()
  throws MathUserException, IntegratorException,
         IOException, ClassNotFoundException {

  TestProblem1 pb = new TestProblem1();
  double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001;
  MidpointIntegrator integ = new MidpointIntegrator(step);
  integ.addStepHandler(new ContinuousOutputModel());
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream    oos = new ObjectOutputStream(bos);
  for (StepHandler handler : integ.getStepHandlers()) {
      oos.writeObject(handler);
  }

  Assert.assertTrue(bos.size () > 114000);
  Assert.assertTrue(bos.size () < 115000);

  ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
  ObjectInputStream     ois = new ObjectInputStream(bis);
  ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();

  Random random = new Random(347588535632l);
  double maxError = 0.0;
  for (int i = 0; i < 1000; ++i) {
    double r = random.nextDouble();
    double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
    cm.setInterpolatedTime(time);
    double[] interpolatedY = cm.getInterpolatedState ();
    double[] theoreticalY  = pb.computeTheoreticalState(time);
    double dx = interpolatedY[0] - theoreticalY[0];
    double dy = interpolatedY[1] - theoreticalY[1];
    double error = dx * dx + dy * dy;
    if (error > maxError) {
      maxError = error;
    }
  }

  Assert.assertTrue(maxError < 1.0e-6);

}
 
Example #21
Source File: GillStepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void serialization()
  throws DerivativeException, IntegratorException,
         IOException, ClassNotFoundException {

  TestProblem3 pb = new TestProblem3(0.9);
  double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003;
  GillIntegrator integ = new GillIntegrator(step);
  integ.addStepHandler(new ContinuousOutputModel());
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream    oos = new ObjectOutputStream(bos);
  for (StepHandler handler : integ.getStepHandlers()) {
      oos.writeObject(handler);
  }

  assertTrue(bos.size () > 700000);
  assertTrue(bos.size () < 701000);

  ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
  ObjectInputStream     ois = new ObjectInputStream(bis);
  ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();

  Random random = new Random(347588535632l);
  double maxError = 0.0;
  for (int i = 0; i < 1000; ++i) {
    double r = random.nextDouble();
    double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
    cm.setInterpolatedTime(time);
    double[] interpolatedY = cm.getInterpolatedState ();
    double[] theoreticalY  = pb.computeTheoreticalState(time);
    double dx = interpolatedY[0] - theoreticalY[0];
    double dy = interpolatedY[1] - theoreticalY[1];
    double error = dx * dx + dy * dy;
    if (error > maxError) {
      maxError = error;
    }
  }

  assertTrue(maxError < 0.003);

}
 
Example #22
Source File: DormandPrince54StepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void serialization()
  throws DerivativeException, IntegratorException,
         IOException, ClassNotFoundException {

  TestProblem3 pb = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;
  DormandPrince54Integrator integ = new DormandPrince54Integrator(minStep, maxStep,
                                                                  scalAbsoluteTolerance,
                                                                  scalRelativeTolerance);
  integ.addStepHandler(new ContinuousOutputModel());
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream    oos = new ObjectOutputStream(bos);
  for (StepHandler handler : integ.getStepHandlers()) {
      oos.writeObject(handler);
  }

  assertTrue(bos.size () > 119500);
  assertTrue(bos.size () < 120500);

  ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
  ObjectInputStream     ois = new ObjectInputStream(bis);
  ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();

  Random random = new Random(347588535632l);
  double maxError = 0.0;
  for (int i = 0; i < 1000; ++i) {
    double r = random.nextDouble();
    double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
    cm.setInterpolatedTime(time);
    double[] interpolatedY = cm.getInterpolatedState ();
    double[] theoreticalY  = pb.computeTheoreticalState(time);
    double dx = interpolatedY[0] - theoreticalY[0];
    double dy = interpolatedY[1] - theoreticalY[1];
    double error = dx * dx + dy * dy;
    if (error > maxError) {
      maxError = error;
    }
  }

  assertTrue(maxError < 7.0e-10);

}
 
Example #23
Source File: HighamHall54StepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void serialization()
  throws DerivativeException, IntegratorException,
         IOException, ClassNotFoundException {

  TestProblem3 pb = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;
  HighamHall54Integrator integ = new HighamHall54Integrator(minStep, maxStep,
                                                            scalAbsoluteTolerance,
                                                            scalRelativeTolerance);
  integ.addStepHandler(new ContinuousOutputModel());
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream    oos = new ObjectOutputStream(bos);
  for (StepHandler handler : integ.getStepHandlers()) {
      oos.writeObject(handler);
  }

  assertTrue(bos.size () > 158000);
  assertTrue(bos.size () < 159000);

  ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
  ObjectInputStream     ois = new ObjectInputStream(bis);
  ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();

  Random random = new Random(347588535632l);
  double maxError = 0.0;
  for (int i = 0; i < 1000; ++i) {
    double r = random.nextDouble();
    double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
    cm.setInterpolatedTime(time);
    double[] interpolatedY = cm.getInterpolatedState ();
    double[] theoreticalY  = pb.computeTheoreticalState(time);
    double dx = interpolatedY[0] - theoreticalY[0];
    double dy = interpolatedY[1] - theoreticalY[1];
    double error = dx * dx + dy * dy;
    if (error > maxError) {
      maxError = error;
    }
  }

  assertTrue(maxError < 1.6e-10);

}
 
Example #24
Source File: HighamHall54StepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void checkClone()
throws DerivativeException, IntegratorException {
  TestProblem3 pb = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;
  HighamHall54Integrator integ = new HighamHall54Integrator(minStep, maxStep,
                                                            scalAbsoluteTolerance,
                                                            scalRelativeTolerance);
  integ.addStepHandler(new StepHandler() {
      public void handleStep(StepInterpolator interpolator, boolean isLast)
      throws DerivativeException {
          StepInterpolator cloned = interpolator.copy();
          double tA = cloned.getPreviousTime();
          double tB = cloned.getCurrentTime();
          double halfStep = Math.abs(tB - tA) / 2;
          assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12);
          assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12);
          for (int i = 0; i < 10; ++i) {
              double t = (i * tB + (9 - i) * tA) / 9;
              interpolator.setInterpolatedTime(t);
              assertTrue(Math.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10));
              cloned.setInterpolatedTime(t);
              assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12);
              double[] referenceState = interpolator.getInterpolatedState();
              double[] cloneState     = cloned.getInterpolatedState();
              for (int j = 0; j < referenceState.length; ++j) {
                  assertEquals(referenceState[j], cloneState[j], 1.0e-12);
              }
          }
      }
      public boolean requiresDenseOutput() {
          return true;
      }
      public void reset() {
      }
  });
  integ.integrate(pb,
          pb.getInitialTime(), pb.getInitialState(),
          pb.getFinalTime(), new double[pb.getDimension()]);

}
 
Example #25
Source File: GraggBulirschStoerStepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void checklone()
throws DerivativeException, IntegratorException {
  TestProblem3 pb = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;
  GraggBulirschStoerIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep,
                                                                        scalAbsoluteTolerance,
                                                                        scalRelativeTolerance);
  integ.addStepHandler(new StepHandler() {
      public void handleStep(StepInterpolator interpolator, boolean isLast)
      throws DerivativeException {
          StepInterpolator cloned = interpolator.copy();
          double tA = cloned.getPreviousTime();
          double tB = cloned.getCurrentTime();
          double halfStep = Math.abs(tB - tA) / 2;
          assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12);
          assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12);
          for (int i = 0; i < 10; ++i) {
              double t = (i * tB + (9 - i) * tA) / 9;
              interpolator.setInterpolatedTime(t);
              assertTrue(Math.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10));
              cloned.setInterpolatedTime(t);
              assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12);
              double[] referenceState = interpolator.getInterpolatedState();
              double[] cloneState     = cloned.getInterpolatedState();
              for (int j = 0; j < referenceState.length; ++j) {
                  assertEquals(referenceState[j], cloneState[j], 1.0e-12);
              }
          }
      }
      public boolean requiresDenseOutput() {
          return true;
      }
      public void reset() {
      }
  });
  integ.integrate(pb,
          pb.getInitialTime(), pb.getInitialState(),
          pb.getFinalTime(), new double[pb.getDimension()]);

}
 
Example #26
Source File: GraggBulirschStoerStepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void serialization()
  throws DerivativeException, IntegratorException,
         IOException, ClassNotFoundException {

  TestProblem3 pb  = new TestProblem3(0.9);
  double minStep   = 0;
  double maxStep   = pb.getFinalTime() - pb.getInitialTime();
  double absTolerance = 1.0e-8;
  double relTolerance = 1.0e-8;

  GraggBulirschStoerIntegrator integ =
    new GraggBulirschStoerIntegrator(minStep, maxStep,
                                     absTolerance, relTolerance);
  integ.addStepHandler(new ContinuousOutputModel());
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream    oos = new ObjectOutputStream(bos);
  for (StepHandler handler : integ.getStepHandlers()) {
      oos.writeObject(handler);
  }

  assertTrue(bos.size () > 33000);
  assertTrue(bos.size () < 34000);

  ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
  ObjectInputStream     ois = new ObjectInputStream(bis);
  ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();

  Random random = new Random(347588535632l);
  double maxError = 0.0;
  for (int i = 0; i < 1000; ++i) {
    double r = random.nextDouble();
    double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
    cm.setInterpolatedTime(time);
    double[] interpolatedY = cm.getInterpolatedState ();
    double[] theoreticalY  = pb.computeTheoreticalState(time);
    double dx = interpolatedY[0] - theoreticalY[0];
    double dy = interpolatedY[1] - theoreticalY[1];
    double error = dx * dx + dy * dy;
    if (error > maxError) {
      maxError = error;
    }
  }

  assertTrue(maxError < 5.0e-10);

}
 
Example #27
Source File: DormandPrince853StepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void checklone()
throws MathUserException, IntegratorException {
  TestProblem3 pb = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;
  DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep,
                                                                    scalAbsoluteTolerance,
                                                                    scalRelativeTolerance);
  integ.addStepHandler(new StepHandler() {
      public void handleStep(StepInterpolator interpolator, boolean isLast)
      throws MathUserException {
          StepInterpolator cloned = interpolator.copy();
          double tA = cloned.getPreviousTime();
          double tB = cloned.getCurrentTime();
          double halfStep = FastMath.abs(tB - tA) / 2;
          Assert.assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12);
          Assert.assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12);
          for (int i = 0; i < 10; ++i) {
              double t = (i * tB + (9 - i) * tA) / 9;
              interpolator.setInterpolatedTime(t);
              Assert.assertTrue(FastMath.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10));
              cloned.setInterpolatedTime(t);
              Assert.assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12);
              double[] referenceState = interpolator.getInterpolatedState();
              double[] cloneState     = cloned.getInterpolatedState();
              for (int j = 0; j < referenceState.length; ++j) {
                  Assert.assertEquals(referenceState[j], cloneState[j], 1.0e-12);
              }
          }
      }
      public void reset() {
      }
  });
  integ.integrate(pb,
          pb.getInitialTime(), pb.getInitialState(),
          pb.getFinalTime(), new double[pb.getDimension()]);

}
 
Example #28
Source File: GraggBulirschStoerStepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void checklone()
throws MathUserException, IntegratorException {
  TestProblem3 pb = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;
  GraggBulirschStoerIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep,
                                                                        scalAbsoluteTolerance,
                                                                        scalRelativeTolerance);
  integ.addStepHandler(new StepHandler() {
      public void handleStep(StepInterpolator interpolator, boolean isLast)
      throws MathUserException {
          StepInterpolator cloned = interpolator.copy();
          double tA = cloned.getPreviousTime();
          double tB = cloned.getCurrentTime();
          double halfStep = FastMath.abs(tB - tA) / 2;
          assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12);
          assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12);
          for (int i = 0; i < 10; ++i) {
              double t = (i * tB + (9 - i) * tA) / 9;
              interpolator.setInterpolatedTime(t);
              assertTrue(FastMath.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10));
              cloned.setInterpolatedTime(t);
              assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12);
              double[] referenceState = interpolator.getInterpolatedState();
              double[] cloneState     = cloned.getInterpolatedState();
              for (int j = 0; j < referenceState.length; ++j) {
                  assertEquals(referenceState[j], cloneState[j], 1.0e-12);
              }
          }
      }
      public boolean requiresDenseOutput() {
          return true;
      }
      public void reset() {
      }
  });
  integ.integrate(pb,
          pb.getInitialTime(), pb.getInitialState(),
          pb.getFinalTime(), new double[pb.getDimension()]);

}
 
Example #29
Source File: HighamHall54StepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void serialization()
  throws MathUserException, IntegratorException,
         IOException, ClassNotFoundException {

  TestProblem3 pb = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;
  HighamHall54Integrator integ = new HighamHall54Integrator(minStep, maxStep,
                                                            scalAbsoluteTolerance,
                                                            scalRelativeTolerance);
  integ.addStepHandler(new ContinuousOutputModel());
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  ObjectOutputStream    oos = new ObjectOutputStream(bos);
  for (StepHandler handler : integ.getStepHandlers()) {
      oos.writeObject(handler);
  }

  Assert.assertTrue(bos.size () > 167000);
  Assert.assertTrue(bos.size () < 168000);

  ByteArrayInputStream  bis = new ByteArrayInputStream(bos.toByteArray());
  ObjectInputStream     ois = new ObjectInputStream(bis);
  ContinuousOutputModel cm  = (ContinuousOutputModel) ois.readObject();

  Random random = new Random(347588535632l);
  double maxError = 0.0;
  for (int i = 0; i < 1000; ++i) {
    double r = random.nextDouble();
    double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime();
    cm.setInterpolatedTime(time);
    double[] interpolatedY = cm.getInterpolatedState ();
    double[] theoreticalY  = pb.computeTheoreticalState(time);
    double dx = interpolatedY[0] - theoreticalY[0];
    double dy = interpolatedY[1] - theoreticalY[1];
    double error = dx * dx + dy * dy;
    if (error > maxError) {
      maxError = error;
    }
  }

  Assert.assertTrue(maxError < 1.6e-10);

}
 
Example #30
Source File: HighamHall54StepInterpolatorTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void checkClone()
throws DerivativeException, IntegratorException {
  TestProblem3 pb = new TestProblem3(0.9);
  double minStep = 0;
  double maxStep = pb.getFinalTime() - pb.getInitialTime();
  double scalAbsoluteTolerance = 1.0e-8;
  double scalRelativeTolerance = scalAbsoluteTolerance;
  HighamHall54Integrator integ = new HighamHall54Integrator(minStep, maxStep,
                                                            scalAbsoluteTolerance,
                                                            scalRelativeTolerance);
  integ.addStepHandler(new StepHandler() {
      public void handleStep(StepInterpolator interpolator, boolean isLast)
      throws DerivativeException {
          StepInterpolator cloned = interpolator.copy();
          double tA = cloned.getPreviousTime();
          double tB = cloned.getCurrentTime();
          double halfStep = Math.abs(tB - tA) / 2;
          assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12);
          assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12);
          for (int i = 0; i < 10; ++i) {
              double t = (i * tB + (9 - i) * tA) / 9;
              interpolator.setInterpolatedTime(t);
              assertTrue(Math.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10));
              cloned.setInterpolatedTime(t);
              assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12);
              double[] referenceState = interpolator.getInterpolatedState();
              double[] cloneState     = cloned.getInterpolatedState();
              for (int j = 0; j < referenceState.length; ++j) {
                  assertEquals(referenceState[j], cloneState[j], 1.0e-12);
              }
          }
      }
      public boolean requiresDenseOutput() {
          return true;
      }
      public void reset() {
      }
  });
  integ.integrate(pb,
          pb.getInitialTime(), pb.getInitialState(),
          pb.getFinalTime(), new double[pb.getDimension()]);

}