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);
}
}
};
}
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);
}
Aggregations