Search in sources :

Example 1 with MultivariateVectorFunction

use of org.hipparchus.analysis.MultivariateVectorFunction in project Orekit by CS-SI.

the class JacobianPropagatorConverter method getObjectiveFunction.

/**
 * {@inheritDoc}
 */
protected MultivariateVectorFunction getObjectiveFunction() {
    return new MultivariateVectorFunction() {

        /**
         * {@inheritDoc}
         */
        public double[] value(final double[] arg) throws IllegalArgumentException, OrekitExceptionWrapper {
            try {
                final double[] value = new double[getTargetSize()];
                final NumericalPropagator prop = builder.buildPropagator(arg);
                final int stateSize = isOnlyPosition() ? 3 : 6;
                final List<SpacecraftState> sample = getSample();
                for (int i = 0; i < sample.size(); ++i) {
                    final int row = i * stateSize;
                    if (prop.getInitialState().getDate().equals(sample.get(i).getDate())) {
                        // use initial state
                        fillRows(value, row, prop.getInitialState());
                    } else {
                        // use a date detector to pick up states
                        prop.addEventDetector(new DateDetector(sample.get(i).getDate()).withHandler(new EventHandler<DateDetector>() {

                            /**
                             * {@inheritDoc}
                             */
                            @Override
                            public Action eventOccurred(final SpacecraftState state, final DateDetector detector, final boolean increasing) throws OrekitException {
                                fillRows(value, row, state);
                                return row + stateSize >= getTargetSize() ? Action.STOP : Action.CONTINUE;
                            }
                        }));
                    }
                }
                prop.propagate(sample.get(sample.size() - 1).getDate().shiftedBy(10.0));
                return value;
            } catch (OrekitException ex) {
                throw new OrekitExceptionWrapper(ex);
            }
        }
    };
}
Also used : DateDetector(org.orekit.propagation.events.DateDetector) SpacecraftState(org.orekit.propagation.SpacecraftState) OrekitExceptionWrapper(org.orekit.errors.OrekitExceptionWrapper) NumericalPropagator(org.orekit.propagation.numerical.NumericalPropagator) EventHandler(org.orekit.propagation.events.handlers.EventHandler) OrekitException(org.orekit.errors.OrekitException) MultivariateVectorFunction(org.hipparchus.analysis.MultivariateVectorFunction)

Example 2 with MultivariateVectorFunction

use of org.hipparchus.analysis.MultivariateVectorFunction in project Orekit by CS-SI.

the class JacobianPropagatorConverterTest method doTestDerivatives.

private void doTestDerivatives(double tolP, double tolV, String... names) throws OrekitException {
    // we use a fixed step integrator on purpose
    // as the test is based on external differentiation using finite differences,
    // an adaptive step size integrator would introduce *lots* of numerical noise
    NumericalPropagatorBuilder builder = new NumericalPropagatorBuilder(OrbitType.CARTESIAN.convertType(orbit), new LutherIntegratorBuilder(10.0), PositionAngle.TRUE, dP);
    builder.setMass(200.0);
    builder.addForceModel(drag);
    builder.addForceModel(gravity);
    // retrieve a state slightly different from the initial state,
    // using normalized values different from 0.0 for the sake of generality
    RandomGenerator random = new Well19937a(0xe67f19c1a678d037l);
    List<ParameterDriver> all = new ArrayList<ParameterDriver>();
    for (final ParameterDriver driver : builder.getOrbitalParametersDrivers().getDrivers()) {
        all.add(driver);
    }
    for (final ParameterDriver driver : builder.getPropagationParametersDrivers().getDrivers()) {
        all.add(driver);
    }
    double[] normalized = new double[names.length];
    List<ParameterDriver> selected = new ArrayList<ParameterDriver>(names.length);
    int index = 0;
    for (final ParameterDriver driver : all) {
        boolean found = false;
        for (final String name : names) {
            if (name.equals(driver.getName())) {
                found = true;
                normalized[index++] = driver.getNormalizedValue() + (2 * random.nextDouble() - 1);
                selected.add(driver);
            }
        }
        driver.setSelected(found);
    }
    // create a one hour sample that starts 10 minutes after initial state
    // the 10 minutes offset implies even the first point is influenced by model parameters
    final List<SpacecraftState> sample = new ArrayList<SpacecraftState>();
    Propagator propagator = builder.buildPropagator(normalized);
    propagator.setMasterMode(60.0, new OrekitFixedStepHandler() {

        @Override
        public void handleStep(SpacecraftState currentState, boolean isLast) {
            sample.add(currentState);
        }
    });
    propagator.propagate(orbit.getDate().shiftedBy(600.0), orbit.getDate().shiftedBy(4200.0));
    JacobianPropagatorConverter fitter = new JacobianPropagatorConverter(builder, 1.0e-3, 5000);
    try {
        Method setSample = AbstractPropagatorConverter.class.getDeclaredMethod("setSample", List.class);
        setSample.setAccessible(true);
        setSample.invoke(fitter, sample);
    } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        Assert.fail(e.getLocalizedMessage());
    }
    MultivariateVectorFunction f = fitter.getObjectiveFunction();
    Pair<RealVector, RealMatrix> p = fitter.getModel().value(new ArrayRealVector(normalized));
    // check derivatives
    // a h offset on normalized parameter represents a physical offset of h * scale
    RealMatrix m = p.getSecond();
    double h = 10.0;
    double[] shifted = normalized.clone();
    double maxErrorP = 0;
    double maxErrorV = 0;
    for (int j = 0; j < selected.size(); ++j) {
        shifted[j] = normalized[j] + 2.0 * h;
        double[] valueP2 = f.value(shifted);
        shifted[j] = normalized[j] + 1.0 * h;
        double[] valueP1 = f.value(shifted);
        shifted[j] = normalized[j] - 1.0 * h;
        double[] valueM1 = f.value(shifted);
        shifted[j] = normalized[j] - 2.0 * h;
        double[] valueM2 = f.value(shifted);
        shifted[j] = normalized[j];
        for (int i = 0; i < valueP2.length; ++i) {
            double d = (8 * (valueP1[i] - valueM1[i]) - (valueP2[i] - valueM2[i])) / (12 * h);
            if (i % 6 < 3) {
                // position
                maxErrorP = FastMath.max(maxErrorP, FastMath.abs(m.getEntry(i, j) - d));
            } else {
                // velocity
                maxErrorV = FastMath.max(maxErrorV, FastMath.abs(m.getEntry(i, j) - d));
            }
        }
    }
    Assert.assertEquals(0.0, maxErrorP, tolP);
    Assert.assertEquals(0.0, maxErrorV, tolV);
}
Also used : ArrayList(java.util.ArrayList) Well19937a(org.hipparchus.random.Well19937a) RandomGenerator(org.hipparchus.random.RandomGenerator) SpacecraftState(org.orekit.propagation.SpacecraftState) ArrayRealVector(org.hipparchus.linear.ArrayRealVector) RealVector(org.hipparchus.linear.RealVector) Propagator(org.orekit.propagation.Propagator) OrekitFixedStepHandler(org.orekit.propagation.sampling.OrekitFixedStepHandler) MathIllegalArgumentException(org.hipparchus.exception.MathIllegalArgumentException) ArrayRealVector(org.hipparchus.linear.ArrayRealVector) Method(java.lang.reflect.Method) ParameterDriver(org.orekit.utils.ParameterDriver) MultivariateVectorFunction(org.hipparchus.analysis.MultivariateVectorFunction) InvocationTargetException(java.lang.reflect.InvocationTargetException) RealMatrix(org.hipparchus.linear.RealMatrix)

Aggregations

MultivariateVectorFunction (org.hipparchus.analysis.MultivariateVectorFunction)2 SpacecraftState (org.orekit.propagation.SpacecraftState)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 MathIllegalArgumentException (org.hipparchus.exception.MathIllegalArgumentException)1 ArrayRealVector (org.hipparchus.linear.ArrayRealVector)1 RealMatrix (org.hipparchus.linear.RealMatrix)1 RealVector (org.hipparchus.linear.RealVector)1 RandomGenerator (org.hipparchus.random.RandomGenerator)1 Well19937a (org.hipparchus.random.Well19937a)1 OrekitException (org.orekit.errors.OrekitException)1 OrekitExceptionWrapper (org.orekit.errors.OrekitExceptionWrapper)1 Propagator (org.orekit.propagation.Propagator)1 DateDetector (org.orekit.propagation.events.DateDetector)1 EventHandler (org.orekit.propagation.events.handlers.EventHandler)1 NumericalPropagator (org.orekit.propagation.numerical.NumericalPropagator)1 OrekitFixedStepHandler (org.orekit.propagation.sampling.OrekitFixedStepHandler)1 ParameterDriver (org.orekit.utils.ParameterDriver)1