Commit d65cf8e1 authored by Luc Maisonobe's avatar Luc Maisonobe
Browse files

Use the new STM and Jacobian API at several places.

parent 2f0286fe
Pipeline #1530 passed with stages
in 20 minutes and 40 seconds
......@@ -140,17 +140,17 @@ public abstract class AbstractPropagator implements Propagator {
/** {@inheritDoc} */
@Override
public void addClosedFormGenerator(final StackableGenerator updater) {
public void addClosedFormGenerator(final StackableGenerator generator) {
// check if the name is already used
if (isAdditionalStateManaged(updater.getName())) {
if (isAdditionalStateManaged(generator.getName())) {
// this additional state is already registered, complain
throw new OrekitException(OrekitMessages.ADDITIONAL_STATE_NAME_ALREADY_IN_USE,
updater.getName());
generator.getName());
}
// this is really a new name, add it
closedFormGenerators.add(updater);
closedFormGenerators.add(generator);
}
......@@ -215,7 +215,7 @@ public abstract class AbstractPropagator implements Propagator {
// start with original state and unmanaged states
SpacecraftState updated = updateUnmanagedStates(original);
// set up queue for updaters
// set up queue for generators
final Queue<StackableGenerator> pending = new LinkedList<>(getAllGenerators());
// update the additional states managed by generators, taking care of dependencies
......@@ -223,17 +223,17 @@ public abstract class AbstractPropagator implements Propagator {
while (!pending.isEmpty()) {
final StackableGenerator generator = pending.remove();
if (generator.yield(updated)) {
// this updater has to wait for another one,
// this generator has to wait for another one,
// we put it again in the pending queue
pending.add(generator);
if (++yieldCount >= pending.size()) {
// all pending updaters yielded!, they probably need data not yet initialized
// all pending generators yielded!, they probably need data not yet initialized
// we let the propagation proceed, if these data are really needed right now
// an appropriate exception will be triggered when caller tries to access them
break;
}
} else {
// we can use this updater right now
// we can use this generator right now
if (generator.isClosedForm()) {
updated = updated.addAdditionalState(generator.getName(), generator.generate(updated));
} else {
......@@ -249,8 +249,8 @@ public abstract class AbstractPropagator implements Propagator {
/** {@inheritDoc} */
public boolean isAdditionalStateManaged(final String name) {
for (final StackableGenerator updater : closedFormGenerators) {
if (updater.getName().equals(name)) {
for (final StackableGenerator generator : closedFormGenerators) {
if (generator.getName().equals(name)) {
return true;
}
}
......
......@@ -213,29 +213,29 @@ public abstract class FieldAbstractPropagator<T extends CalculusFieldElement<T>>
// start with original state and unmanaged states
FieldSpacecraftState<T> updated = updateUnmanagedStates(original);
// set up queue for updaters
// set up queue for generators
final Queue<FieldStackableGenerator<T>> pending = new LinkedList<>(getAllGenerators());
// update the additional states managed by updaters, taking care of dependencies
// update the additional states managed by generators, taking care of dependencies
int yieldCount = 0;
while (!pending.isEmpty()) {
final FieldStackableGenerator<T> updater = pending.remove();
if (updater.yield(updated)) {
// this updater has to wait for another one,
final FieldStackableGenerator<T> generator = pending.remove();
if (generator.yield(updated)) {
// this generator has to wait for another one,
// we put it again in the pending queue
pending.add(updater);
pending.add(generator);
if (++yieldCount >= pending.size()) {
// all pending updaters yielded!, they probably need data not yet initialized
// all pending generators yielded!, they probably need data not yet initialized
// we let the propagation proceed, if these data are really needed right now
// an appropriate exception will be triggered when caller tries to access them
break;
}
} else {
// we can use this updater right now
if (updater.isClosedForm()) {
updated = updated.addAdditionalState(updater.getName(), updater.generate(updated));
// we can use this generator right now
if (generator.isClosedForm()) {
updated = updated.addAdditionalState(generator.getName(), generator.generate(updated));
} else {
updated = updated.addAdditionalStateDerivative(updater.getName(), updater.generate(updated));
updated = updated.addAdditionalStateDerivative(generator.getName(), generator.generate(updated));
}
yieldCount = 0;
}
......
......@@ -25,7 +25,7 @@ import org.hipparchus.CalculusFieldElement;
*/
public class FieldClosedFormAdapter<T extends CalculusFieldElement<T>> implements FieldAdditionalStateProvider<T> {
/** Underlying updater. */
/** Underlying generator. */
private final FieldStackableGenerator<T> generator;
/** Simple constructor.
......
......@@ -80,12 +80,12 @@ public abstract class AbstractPropagatorBuilder implements PropagatorBuilder {
/** Attitude provider for the propagator. */
private AttitudeProvider attitudeProvider;
/** Closed-form updaters.
/** Closed-form generators.
* @since 11.1
*/
private List<StackableGenerator> closedFormGenerators;
/** Integrable updaters.
/** Integrable generators.
* @since 11.1
*/
private List<IntegrableGenerator> integrableGenerators;
......@@ -385,11 +385,11 @@ public abstract class AbstractPropagatorBuilder implements PropagatorBuilder {
closedFormGenerators.add(generator);
}
/** Get an unmodifiable list of updaters for additional state.
* @return updaters for the additional states
/** Get an unmodifiable list of generators for additional state.
* @return generators for the additional states
* @since 11.1
*/
public List<StackableGenerator> getClosedFormUpdaters() {
public List<StackableGenerator> getClosedFormGenerators() {
return Collections.unmodifiableList(closedFormGenerators);
}
......
......@@ -23,18 +23,17 @@ import org.hipparchus.linear.ArrayRealVector;
import org.hipparchus.linear.MatrixUtils;
import org.hipparchus.linear.RealMatrix;
import org.hipparchus.linear.RealVector;
import org.hipparchus.ode.events.Action;
import org.hipparchus.optim.nonlinear.vector.leastsquares.MultivariateJacobianFunction;
import org.hipparchus.util.Pair;
import org.orekit.errors.OrekitException;
import org.orekit.errors.OrekitMessages;
import org.orekit.orbits.OrbitType;
import org.orekit.propagation.MatricesHarvester;
import org.orekit.propagation.SpacecraftState;
import org.orekit.propagation.events.DateDetector;
import org.orekit.propagation.events.handlers.EventHandler;
import org.orekit.propagation.numerical.JacobiansMapper;
import org.orekit.propagation.numerical.NumericalPropagator;
import org.orekit.propagation.numerical.PartialDerivatives;
import org.orekit.propagation.sampling.OrekitStepHandler;
import org.orekit.propagation.sampling.OrekitStepInterpolator;
import org.orekit.time.AbsoluteDate;
import org.orekit.utils.PVCoordinates;
import org.orekit.utils.ParameterDriver;
import org.orekit.utils.ParameterDriversList;
......@@ -67,151 +66,205 @@ public class JacobianPropagatorConverter extends AbstractPropagatorConverter {
/** {@inheritDoc} */
protected MultivariateVectorFunction getObjectiveFunction() {
return new MultivariateVectorFunction() {
/** {@inheritDoc} */
public double[] value(final double[] arg)
throws IllegalArgumentException, OrekitException {
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) {
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;
}
return point -> {
final NumericalPropagator propagator = builder.buildPropagator(point);
final ValuesHandler handler = new ValuesHandler();
propagator.getMultiplexer().add(handler);
final List<SpacecraftState> sample = getSample();
propagator.propagate(sample.get(sample.size() - 1).getDate().shiftedBy(10.0));
return handler.value;
};
}
/** {@inheritDoc} */
protected MultivariateJacobianFunction getModel() {
return new MultivariateJacobianFunction() {
/** {@inheritDoc} */
public Pair<RealVector, RealMatrix> value(final RealVector point)
throws IllegalArgumentException, OrekitException {
final RealVector value = new ArrayRealVector(getTargetSize());
final RealMatrix jacobian = MatrixUtils.createRealMatrix(getTargetSize(), point.getDimension());
final NumericalPropagator prop = builder.buildPropagator(point.toArray());
final int stateSize = isOnlyPosition() ? 3 : 6;
final ParameterDriversList orbitalParameters = builder.getOrbitalParametersDrivers();
final PartialDerivatives pde = new PartialDerivatives("pde", prop);
final ParameterDriversList propagationParameters = pde.getSelectedParameters();
prop.setInitialState(pde.setInitialJacobians(prop.getInitialState()));
final JacobiansMapper mapper = pde.getMapper();
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 and Jacobians
fillRows(value, jacobian, row, prop.getInitialState(), stateSize,
orbitalParameters, propagationParameters, mapper);
} else {
// use a date detector to pick up state and Jacobians
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) {
fillRows(value, jacobian, row, state, stateSize,
orbitalParameters, propagationParameters, mapper);
return row + stateSize >= getTargetSize() ? Action.STOP : Action.CONTINUE;
}
}));
}
return point -> {
final NumericalPropagator propagator = builder.buildPropagator(point.toArray());
final JacobianHandler handler = new JacobianHandler(propagator, point.getDimension());
propagator.getMultiplexer().add(handler);
final List<SpacecraftState> sample = getSample();
propagator.propagate(sample.get(sample.size() - 1).getDate().shiftedBy(10.0));
return new Pair<>(handler.value, handler.jacobian);
};
}
/** Handler for picking up values at sample dates.
* <p>
* This class is heavily based on org.orekit.estimation.leastsquares.MeasurementHandler.
* </p>
* @since 11.1
*/
private class ValuesHandler implements OrekitStepHandler {
/** Values vector. */
private final double[] value;
/** Number of the next measurement. */
private int number;
/** Index of the next component in the model. */
private int index;
/** Simple constructor.
*/
ValuesHandler() {
this.value = new double[getTargetSize()];
}
/** {@inheritDoc} */
@Override
public void init(final SpacecraftState initialState, final AbsoluteDate target) {
number = 0;
index = 0;
}
/** {@inheritDoc} */
@Override
public void handleStep(final OrekitStepInterpolator interpolator) {
while (number < getSample().size()) {
// Consider the next sample to handle
final SpacecraftState next = getSample().get(number);
// Current state date
final AbsoluteDate currentDate = interpolator.getCurrentState().getDate();
if (next.getDate().compareTo(currentDate) > 0) {
return;
}
final PVCoordinates pv = interpolator.getInterpolatedState(next.getDate()).getPVCoordinates(getFrame());
value[index++] = pv.getPosition().getX();
value[index++] = pv.getPosition().getY();
value[index++] = pv.getPosition().getZ();
if (!isOnlyPosition()) {
value[index++] = pv.getVelocity().getX();
value[index++] = pv.getVelocity().getY();
value[index++] = pv.getVelocity().getZ();
}
prop.propagate(sample.get(sample.size() - 1).getDate().shiftedBy(10.0));
// prepare handling of next measurement
++number;
return new Pair<RealVector, RealMatrix>(value, jacobian);
}
};
}
/** Fill up a few value rows (either 6 or 3 depending on velocities used or not).
* @param value values array
* @param row first row index
* @param state spacecraft state
*/
private void fillRows(final double[] value, final int row, final SpacecraftState state) {
final PVCoordinates pv = state.getPVCoordinates(getFrame());
value[row ] = pv.getPosition().getX();
value[row + 1] = pv.getPosition().getY();
value[row + 2] = pv.getPosition().getZ();
if (!isOnlyPosition()) {
value[row + 3] = pv.getVelocity().getX();
value[row + 4] = pv.getVelocity().getY();
value[row + 5] = pv.getVelocity().getZ();
}
}
/** Fill up a few Jacobian rows (either 6 or 3 depending on velocities used or not).
* @param value values vector
* @param jacobian Jacobian matrix
* @param row first row index
* @param state spacecraft state
* @param stateSize state size
* @param orbitalParameters drivers for the orbital parameters
* @param propagationParameters drivers for the propagation model parameters
* @param mapper state mapper
/** Handler for picking up Jacobians at sample dates.
* <p>
* This class is heavily based on org.orekit.estimation.leastsquares.MeasurementHandler.
* </p>
* @since 11.1
*/
private void fillRows(final RealVector value, final RealMatrix jacobian, final int row,
final SpacecraftState state, final int stateSize,
final ParameterDriversList orbitalParameters,
final ParameterDriversList propagationParameters,
final JacobiansMapper mapper) {
// value part
final PVCoordinates pv = state.getPVCoordinates(getFrame());
value.setEntry(row, pv.getPosition().getX());
value.setEntry(row + 1, pv.getPosition().getY());
value.setEntry(row + 2, pv.getPosition().getZ());
if (!isOnlyPosition()) {
value.setEntry(row + 3, pv.getVelocity().getX());
value.setEntry(row + 4, pv.getVelocity().getY());
value.setEntry(row + 5, pv.getVelocity().getZ());
private class JacobianHandler implements OrekitStepHandler {
/** Values vector. */
private final RealVector value;
/** Jacobian matrix. */
private final RealMatrix jacobian;
/** State size (3 or 6). */
private final int stateSize;
/** Matrices harvester. */
private final MatricesHarvester harvester;
/** Number of the next measurement. */
private int number;
/** Index of the next Jacobian component in the model. */
private int index;
/** Simple constructor.
* @param propagator propagator
* @param columns number of columns of the Jacobian matrix
*/
JacobianHandler(final NumericalPropagator propagator, final int columns) {
this.value = new ArrayRealVector(getTargetSize());
this.jacobian = MatrixUtils.createRealMatrix(getTargetSize(), columns);
this.stateSize = isOnlyPosition() ? 3 : 6;
this.harvester = propagator.setupMatricesComputation("converter-partials", null, null);
}
/** {@inheritDoc} */
@Override
public void init(final SpacecraftState initialState, final AbsoluteDate target) {
number = 0;
index = 0;
}
// Jacobian part
final RealMatrix dYdY0 = mapper.getStateTransitionMatrix(state);
final RealMatrix dYdP = mapper.getParametersJacobian(state);
for (int k = 0; k < stateSize; k++) {
int index = 0;
for (int j = 0; j < orbitalParameters.getNbParams(); ++j) {
final ParameterDriver driver = orbitalParameters.getDrivers().get(j);
if (driver.isSelected()) {
jacobian.setEntry(row + k, index++, dYdY0.getEntry(k, j) * driver.getScale());
/** {@inheritDoc} */
@Override
public void handleStep(final OrekitStepInterpolator interpolator) {
while (number < getSample().size()) {
// Consider the next sample to handle
final SpacecraftState next = getSample().get(number);
// Current state date
final AbsoluteDate currentDate = interpolator.getCurrentState().getDate();
if (next.getDate().compareTo(currentDate) > 0) {
return;
}
fillRows(index, interpolator.getInterpolatedState(next.getDate()),
builder.getOrbitalParametersDrivers());
// prepare handling of next measurement
++number;
index += stateSize;
}
}
/** Fill up a few Jacobian rows (either 6 or 3 depending on velocities used or not).
* @param row first row index
* @param state spacecraft state
* @param orbitalParameters drivers for the orbital parameters
*/
private void fillRows(final int row,
final SpacecraftState state,
final ParameterDriversList orbitalParameters) {
// value part
final PVCoordinates pv = state.getPVCoordinates(getFrame());
value.setEntry(row, pv.getPosition().getX());
value.setEntry(row + 1, pv.getPosition().getY());
value.setEntry(row + 2, pv.getPosition().getZ());
if (!isOnlyPosition()) {
value.setEntry(row + 3, pv.getVelocity().getX());
value.setEntry(row + 4, pv.getVelocity().getY());
value.setEntry(row + 5, pv.getVelocity().getZ());
}
for (int j = 0; j < propagationParameters.getNbParams(); ++j) {
final ParameterDriver driver = propagationParameters.getDrivers().get(j);
jacobian.setEntry(row + k, index++, dYdP.getEntry(k, j) * driver.getScale());
// Jacobian part
final RealMatrix dYdY0 = harvester.getStateTransitionMatrix(state);
final RealMatrix dYdP = harvester.getParametersJacobian(state);
for (int k = 0; k < stateSize; k++) {
int column = 0;
for (int j = 0; j < orbitalParameters.getNbParams(); ++j) {
final ParameterDriver driver = orbitalParameters.getDrivers().get(j);
if (driver.isSelected()) {
jacobian.setEntry(row + k, column++, dYdY0.getEntry(k, j) * driver.getScale());
}
}
if (dYdP != null) {
for (int j = 0; j < dYdP.getColumnDimension(); ++j) {
final String name = harvester.getJacobiansColumnsNames().get(j);
for (final ParameterDriver driver : builder.getPropagationParametersDrivers().getDrivers()) {
if (name.equals(driver.getName())) {
jacobian.setEntry(row + k, column++, dYdP.getEntry(k, j) * driver.getScale());
}
}
}
}
}
}
}
......
......@@ -219,12 +219,12 @@ public class NumericalPropagatorBuilder extends AbstractPropagatorBuilder implem
propagator.resetInitialState(state);
// Add updaters to the propagator
for (StackableGenerator updater: getClosedFormUpdaters()) {
propagator.addClosedFormGenerator(updater);
// Add generators to the propagator
for (StackableGenerator generator: getClosedFormGenerators()) {
propagator.addClosedFormGenerator(generator);
}
for (IntegrableGenerator updater: getIntegrableGenerators()) {
propagator.addIntegrableGenerator(updater);
for (IntegrableGenerator generator: getIntegrableGenerators()) {
propagator.addIntegrableGenerator(generator);
}
return propagator;
......
......@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.hipparchus.exception.MathRuntimeException;
......@@ -77,6 +78,11 @@ public abstract class AbstractIntegratedPropagator extends AbstractPropagator {
*/
private final List<IntegrableGenerator> integrableGenerators;
/** Dimension of the secondary states (gathering all integrable generators).
* @since 11.1
*/
private int secondaryDimension;
/** Counter for differential equations calls. */
private int calls;
......@@ -99,12 +105,13 @@ public abstract class AbstractIntegratedPropagator extends AbstractPropagator {
* @param propagationType type of orbit to output (mean or osculating).
*/
protected AbstractIntegratedPropagator(final ODEIntegrator integrator, final PropagationType propagationType) {
detectors = new ArrayList<>();
ephemerisGenerators = new ArrayList<>();
integrableGenerators = new ArrayList<>();
this.integrator = integrator;
this.propagationType = propagationType;
this.resetAtEnd = true;
detectors = new ArrayList<>();
ephemerisGenerators = new ArrayList<>();
integrableGenerators = new ArrayList<>();
this.secondaryDimension = 0;
this.integrator = integrator;
this.propagationType = propagationType;
this.resetAtEnd = true;
}
/** Allow/disallow resetting the initial state at end of propagation.
......@@ -245,7 +252,7 @@ public abstract class AbstractIntegratedPropagator extends AbstractPropagator {
/** Add a set of user-specified equations to be integrated along with the orbit propagation.
* @param additional additional equations