Commit 5ef83dc0 authored by Luc Maisonobe's avatar Luc Maisonobe
Browse files

Reduced code duplication.

parent c712523f
Pipeline #1996 passed with stages
in 31 minutes and 53 seconds
......@@ -24,9 +24,7 @@ import org.orekit.frames.TopocentricFrame;
import org.orekit.models.earth.ionosphere.IonosphericModel;
import org.orekit.propagation.FieldSpacecraftState;
import org.orekit.propagation.SpacecraftState;
import org.orekit.utils.Differentiation;
import org.orekit.utils.ParameterDriver;
import org.orekit.utils.ParameterFunction;
/** Base class modifying theoretical range-rate measurement with ionospheric delay.
* The effect of ionospheric correction on the range-rate is directly computed
......@@ -109,65 +107,6 @@ public abstract class BaseRangeRateIonosphericDelayModifier {
return delay2.subtract(delay1).divide(dt);
}
/** Compute the Jacobian of the delay term wrt state using
* automatic differentiation.
*
* @param derivatives ionospheric delay derivatives
*
* @return Jacobian of the delay wrt state
*/
protected double[][] rangeRateErrorJacobianState(final double[] derivatives) {
final double[][] finiteDifferencesJacobian = new double[1][6];
System.arraycopy(derivatives, 0, finiteDifferencesJacobian[0], 0, 6);
return finiteDifferencesJacobian;
}
/** Compute the derivative of the delay term wrt parameters.
*
* @param station ground station
* @param driver driver for the station offset parameter
* @param state spacecraft state
* @return derivative of the delay wrt station offset parameter
*/
protected double rangeRateErrorParameterDerivative(final GroundStation station,
final ParameterDriver driver,
final SpacecraftState state) {
final ParameterFunction rangeError = new ParameterFunction() {
/** {@inheritDoc} */
@Override
public double value(final ParameterDriver parameterDriver) {
return rangeRateErrorIonosphericModel(station, state);
}
};
final ParameterFunction rangeErrorDerivative =
Differentiation.differentiate(rangeError, 3, 10.0 * driver.getScale());
return rangeErrorDerivative.value(driver);
}
/** Compute the derivative of the delay term wrt parameters using
* automatic differentiation.
*
* @param derivatives ionospheric delay derivatives
* @param freeStateParameters dimension of the state.
* @return derivative of the delay wrt ionospheric model parameters
*/
protected double[] rangeRateErrorParameterDerivative(final double[] derivatives, final int freeStateParameters) {
// 0 ... freeStateParameters - 1 -> derivatives of the delay wrt state
// freeStateParameters ... n -> derivatives of the delay wrt ionospheric parameters
final int dim = derivatives.length - freeStateParameters;
final double[] rangeError = new double[dim];
for (int i = 0; i < dim; i++) {
rangeError[i] = derivatives[freeStateParameters + i];
}
return rangeError;
}
/** Get the drivers for this modifier parameters.
* @return drivers for this modifier parameters
*/
......
......@@ -26,9 +26,7 @@ import org.orekit.estimation.measurements.GroundStation;
import org.orekit.models.earth.troposphere.DiscreteTroposphericModel;
import org.orekit.propagation.FieldSpacecraftState;
import org.orekit.propagation.SpacecraftState;
import org.orekit.utils.Differentiation;
import org.orekit.utils.ParameterDriver;
import org.orekit.utils.ParameterFunction;
/** Baselass modifying theoretical range-rate measurements with tropospheric delay.
* The effect of tropospheric correction on the range-rate is directly computed
......@@ -158,65 +156,6 @@ public abstract class BaseRangeRateTroposphericDelayModifier {
return zero;
}
/** Compute the Jacobian of the delay term wrt state using
* automatic differentiation.
*
* @param derivatives tropospheric delay derivatives
*
* @return Jacobian of the delay wrt state
*/
protected double[][] rangeRateErrorJacobianState(final double[] derivatives) {
final double[][] finiteDifferencesJacobian = new double[1][6];
System.arraycopy(derivatives, 0, finiteDifferencesJacobian[0], 0, 6);
return finiteDifferencesJacobian;
}
/** Compute the derivative of the delay term wrt parameters.
*
* @param station ground station
* @param driver driver for the station offset parameter
* @param state spacecraft state
* @return derivative of the delay wrt station offset parameter
*/
protected double rangeRateErrorParameterDerivative(final GroundStation station,
final ParameterDriver driver,
final SpacecraftState state) {
final ParameterFunction rangeError = new ParameterFunction() {
/** {@inheritDoc} */
@Override
public double value(final ParameterDriver parameterDriver) {
return rangeRateErrorTroposphericModel(station, state);
}
};
final ParameterFunction rangeErrorDerivative =
Differentiation.differentiate(rangeError, 3, 10.0 * driver.getScale());
return rangeErrorDerivative.value(driver);
}
/** Compute the derivative of the delay term wrt parameters using
* automatic differentiation.
*
* @param derivatives tropospheric delay derivatives
* @param freeStateParameters dimension of the state.
* @return derivative of the delay wrt tropospheric model parameters
*/
protected double[] rangeRateErrorParameterDerivative(final double[] derivatives, final int freeStateParameters) {
// 0 ... freeStateParameters - 1 -> derivatives of the delay wrt state
// freeStateParameters ... n -> derivatives of the delay wrt tropospheric parameters
final int dim = derivatives.length - freeStateParameters;
final double[] rangeError = new double[dim];
for (int i = 0; i < dim; i++) {
rangeError[i] = derivatives[freeStateParameters + i];
}
return rangeError;
}
/** Get the drivers for this modifier parameters.
* @return drivers for this modifier parameters
*/
......
......@@ -54,12 +54,11 @@ class BistaticModifierUtil {
final ParametersDriversProvider parametricModel,
final AbstractGradientConverter converter,
final GroundStation emitter, final GroundStation receiver,
final ModelEffect modelEffect,
final ModelEffectGradient modelEffectGradient) {
final ParametricModelEffect modelEffect,
final ParametricModelEffectGradient modelEffectGradient) {
final SpacecraftState state = estimated.getStates()[0];
final double[] oldValue = estimated.getEstimatedValue();
final SpacecraftState state = estimated.getStates()[0];
final double[] oldValue = estimated.getEstimatedValue();
// update estimated derivatives with Jacobian of the measure wrt state
final FieldSpacecraftState<Gradient> gState = converter.getState(parametricModel);
......@@ -84,10 +83,8 @@ class BistaticModifierUtil {
if (driver.isSelected()) {
// update estimated derivatives with derivative of the modification wrt model parameters
double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
final double[] dDelayUpdP = Arrays.copyOfRange(derivativesUp, converter.getFreeStateParameters(), derivativesUp.length);
parameterDerivative += dDelayUpdP[index];
final double[] dDelayDowndP = Arrays.copyOfRange(derivativesDown, converter.getFreeStateParameters(), derivativesDown.length);
parameterDerivative += dDelayDowndP[index];
parameterDerivative += derivativesUp[index + converter.getFreeStateParameters()];
parameterDerivative += derivativesDown[index + converter.getFreeStateParameters()];
estimated.setParameterDerivatives(driver, parameterDerivative);
index++;
}
......@@ -128,31 +125,4 @@ class BistaticModifierUtil {
}
/** Parametric model effect.
* @since 11.2
*/
@FunctionalInterface
public interface ModelEffect {
/** Evaluate the parametric model effect.
* @param station station
* @param state spacecraft state
* @return the measurement error due to parametric model
*/
double evaluate(GroundStation station, SpacecraftState state);
}
/** Parametric model effect.
* @since 11.2
*/
@FunctionalInterface
public interface ModelEffectGradient {
/** Evaluate the parametric model effect.
* @param station station
* @param state spacecraft state
* @param parameters parametric model parameters
* @return the measurement error due to parametric model
*/
Gradient evaluate(GroundStation station, FieldSpacecraftState<Gradient> state, Gradient[] parameters);
}
}
/* Copyright 2002-2022 CS GROUP
* Licensed to CS GROUP (CS) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* CS licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.orekit.estimation.measurements.modifiers;
import org.orekit.estimation.measurements.GroundStation;
import org.orekit.propagation.SpacecraftState;
/** Functional interface for parameteric models.
* @author Luc Maisonobe
* @since 11.2
*/
@FunctionalInterface
interface ParametricModelEffect {
/** Evaluate the parametric model effect.
* @param station station
* @param state spacecraft state
* @return the measurement error due to parametric model
*/
double evaluate(GroundStation station, SpacecraftState state);
}
/* Copyright 2002-2022 CS GROUP
* Licensed to CS GROUP (CS) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* CS licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.orekit.estimation.measurements.modifiers;
import org.hipparchus.analysis.differentiation.Gradient;
import org.orekit.estimation.measurements.GroundStation;
import org.orekit.propagation.FieldSpacecraftState;
/** Functional interface for parameteric models.
* @author Luc Maisonobe
* @since 11.2
*/
@FunctionalInterface
interface ParametricModelEffectGradient {
/** Evaluate the parametric model effect.
* @param station station
* @param state spacecraft state
* @param parameters parametric model parameters
* @return the measurement error due to parametric model
*/
Gradient evaluate(GroundStation station, FieldSpacecraftState<Gradient> state, Gradient[] parameters);
}
......@@ -16,11 +16,9 @@
*/
package org.orekit.estimation.measurements.modifiers;
import java.util.Arrays;
import java.util.List;
import org.hipparchus.CalculusFieldElement;
import org.hipparchus.analysis.differentiation.Gradient;
import org.orekit.attitudes.InertialProvider;
import org.orekit.estimation.measurements.EstimatedMeasurement;
import org.orekit.estimation.measurements.EstimationModifier;
......@@ -30,9 +28,7 @@ import org.orekit.frames.TopocentricFrame;
import org.orekit.models.earth.ionosphere.IonosphericModel;
import org.orekit.propagation.FieldSpacecraftState;
import org.orekit.propagation.SpacecraftState;
import org.orekit.utils.Differentiation;
import org.orekit.utils.ParameterDriver;
import org.orekit.utils.ParameterFunction;
/** Class modifying theoretical range measurement with ionospheric delay.
* The effect of ionospheric correction on the range is directly computed
......@@ -89,8 +85,8 @@ public class RangeIonosphericDelayModifier implements EstimationModifier<Range>
* @return the measurement error due to ionosphere
*/
private <T extends CalculusFieldElement<T>> T rangeErrorIonosphericModel(final GroundStation station,
final FieldSpacecraftState<T> state,
final T[] parameters) {
final FieldSpacecraftState<T> state,
final T[] parameters) {
// Base frame associated with the station
final TopocentricFrame baseFrame = station.getBaseFrame();
// delay in meters
......@@ -98,68 +94,6 @@ public class RangeIonosphericDelayModifier implements EstimationModifier<Range>
return delay;
}
/** Compute the Jacobian of the delay term wrt state using
* automatic differentiation.
*
* @param derivatives ionospheric delay derivatives
*
* @return Jacobian of the delay wrt state
*/
private double[][] rangeErrorJacobianState(final double[] derivatives) {
final double[][] finiteDifferencesJacobian = new double[1][6];
System.arraycopy(derivatives, 0, finiteDifferencesJacobian[0], 0, 6);
return finiteDifferencesJacobian;
}
/** Compute the derivative of the delay term wrt parameters.
*
* @param station ground station
* @param driver driver for the station offset parameter
* @param state spacecraft state
* @param delay current ionospheric delay
* @return derivative of the delay wrt station offset parameter
*/
private double rangeErrorParameterDerivative(final GroundStation station,
final ParameterDriver driver,
final SpacecraftState state,
final double delay) {
final ParameterFunction rangeError = new ParameterFunction() {
/** {@inheritDoc} */
@Override
public double value(final ParameterDriver parameterDriver) {
return rangeErrorIonosphericModel(station, state);
}
};
final ParameterFunction rangeErrorDerivative =
Differentiation.differentiate(rangeError, 3, 10.0 * driver.getScale());
return rangeErrorDerivative.value(driver);
}
/** Compute the derivative of the delay term wrt parameters using
* automatic differentiation.
*
* @param derivatives ionospheric delay derivatives
* @param freeStateParameters dimension of the state.
* @return derivative of the delay wrt ionospheric model parameters
*/
private double[] rangeErrorParameterDerivative(final double[] derivatives, final int freeStateParameters) {
// 0 ... freeStateParameters - 1 -> derivatives of the delay wrt state
// freeStateParameters ... n -> derivatives of the delay wrt ionospheric parameters
final int dim = derivatives.length - freeStateParameters;
final double[] rangeError = new double[dim];
for (int i = 0; i < dim; i++) {
rangeError[i] = derivatives[freeStateParameters + i];
}
return rangeError;
}
/** {@inheritDoc} */
@Override
public List<ParameterDriver> getParametersDrivers() {
......@@ -168,60 +102,16 @@ public class RangeIonosphericDelayModifier implements EstimationModifier<Range>
@Override
public void modify(final EstimatedMeasurement<Range> estimated) {
final Range measurement = estimated.getObservedMeasurement();
final GroundStation station = measurement.getStation();
final SpacecraftState state = estimated.getStates()[0];
final double[] oldValue = estimated.getEstimatedValue();
// update estimated derivatives with Jacobian of the measure wrt state
final ModifierGradientConverter converter =
new ModifierGradientConverter(state, 6, new InertialProvider(state.getFrame()));
final FieldSpacecraftState<Gradient> gState = converter.getState(ionoModel);
final Gradient[] gParameters = converter.getParameters(gState, ionoModel);
final Gradient gDelay = rangeErrorIonosphericModel(station, gState, gParameters);
final double[] derivatives = gDelay.getGradient();
final double[][] djac = rangeErrorJacobianState(derivatives);
final double[][] stateDerivatives = estimated.getStateDerivatives(0);
for (int irow = 0; irow < stateDerivatives.length; ++irow) {
for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
stateDerivatives[irow][jcol] += djac[irow][jcol];
}
}
estimated.setStateDerivatives(0, stateDerivatives);
int index = 0;
for (final ParameterDriver driver : getParametersDrivers()) {
if (driver.isSelected()) {
// update estimated derivatives with derivative of the modification wrt ionospheric parameters
double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
final double[] dDelaydP = rangeErrorParameterDerivative(derivatives, converter.getFreeStateParameters());
parameterDerivative += dDelaydP[index];
estimated.setParameterDerivatives(driver, parameterDerivative);
index = index + 1;
}
}
for (final ParameterDriver driver : Arrays.asList(station.getClockOffsetDriver(),
station.getEastOffsetDriver(),
station.getNorthOffsetDriver(),
station.getZenithOffsetDriver())) {
if (driver.isSelected()) {
// update estimated derivatives with derivative of the modification wrt station parameters
double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
parameterDerivative += rangeErrorParameterDerivative(station, driver, state, gDelay.getValue());
estimated.setParameterDerivatives(driver, parameterDerivative);
}
}
// update estimated value taking into account the ionospheric delay.
// The ionospheric delay is directly added to the range.
final double[] newValue = oldValue.clone();
newValue[0] = newValue[0] + gDelay.getValue();
estimated.setEstimatedValue(newValue);
RangeModifierUtil.modify(estimated, ionoModel,
new ModifierGradientConverter(state, 6, new InertialProvider(state.getFrame())),
station,
this::rangeErrorIonosphericModel,
this::rangeErrorIonosphericModel);
}
......
/* Copyright 2002-2022 CS GROUP
* Licensed to CS GROUP (CS) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* CS licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.orekit.estimation.measurements.modifiers;
import java.util.Arrays;
import org.hipparchus.analysis.differentiation.Gradient;
import org.orekit.estimation.measurements.EstimatedMeasurement;
import org.orekit.estimation.measurements.GroundStation;
import org.orekit.estimation.measurements.ObservedMeasurement;
import org.orekit.propagation.FieldSpacecraftState;
import org.orekit.propagation.SpacecraftState;
import org.orekit.propagation.integration.AbstractGradientConverter;
import org.orekit.utils.Differentiation;
import org.orekit.utils.ParameterDriver;
import org.orekit.utils.ParametersDriversProvider;
/** Utility class modifying theoretical range measurement.
* @author Maxime Journot
* @author Joris Olympio
* @since 11.2
*/
public class RangeModifierUtil {
/** Private constructor for utility class.*/
private RangeModifierUtil() {
// not used
}
/** Apply a modifier to an estimated measurement.
* @param <T> type of the measurement
* @param estimated estimated measurement to modify
* @param station ground station
* @param converter gradient converter
* @param parametricModel parametric modifier model
* @param modelEffect model effect
* @param modelEffectGradient model effect gradient
*/
public static <T extends ObservedMeasurement<T>> void modify(final EstimatedMeasurement<T> estimated,
final ParametersDriversProvider parametricModel,
final AbstractGradientConverter converter,
final GroundStation station,
final ParametricModelEffect modelEffect,
final ParametricModelEffectGradient modelEffectGradient) {
final SpacecraftState state = estimated.getStates()[0];
final double[] oldValue = estimated.getEstimatedValue();
// update estimated derivatives with Jacobian of the measure wrt state
final FieldSpacecraftState<Gradient> gState = converter.getState(parametricModel);
final Gradient[] gParameters = converter.getParameters(gState, parametricModel);
final Gradient gDelay = modelEffectGradient.evaluate(station, gState, gParameters);
final double[] derivatives = gDelay.getGradient();
final double[][] stateDerivatives = estimated.getStateDerivatives(0);
for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
stateDerivatives[0][jcol] += derivatives[jcol];
}
estimated.setStateDerivatives(0, stateDerivatives);
int index = 0;
for (final ParameterDriver driver : parametricModel.getParametersDrivers()) {
if (driver.isSelected()) {
// update estimated derivatives with derivative of the modification wrt ionospheric parameters
double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
parameterDerivative += derivatives[index + converter.getFreeStateParameters()];
estimated.setParameterDerivatives(driver, parameterDerivative);
index = index + 1;
}
}
for (final ParameterDriver driver : Arrays.asList(station.getClockOffsetDriver(),
station.getEastOffsetDriver(),
station.getNorthOffsetDriver(),
station.getZenithOffsetDriver())) {
if (driver.isSelected()) {
// update estimated derivatives with derivative of the modification wrt station parameters
double parameterDerivative = estimated.getParameterDerivatives(driver)[0];
parameterDerivative += Differentiation.differentiate(d -> modelEffect.evaluate(station, state),
3, 10.0 * driver.getScale()).value(driver);
estimated.setParameterDerivatives(driver, parameterDerivative);
}
}
// update estimated value taking into account the ionospheric delay.
// The ionospheric delay is directly added to the range.
final double[] newValue = oldValue.clone();
newValue[0] = newValue[0] + gDelay.getValue();