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

Reduced code duplication.

parent 89ea7d12
Pipeline #1992 failed with stages
in 14 minutes and 51 seconds
/* 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.List;
import org.hipparchus.CalculusFieldElement;
import org.orekit.estimation.measurements.GroundStation;
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
* through the computation of the ionospheric delay difference with respect to
* time.
*
* The ionospheric delay depends on the frequency of the signal (GNSS, VLBI, ...).
* For optical measurements (e.g. SLR), the ray is not affected by ionosphere charged particles.
* <p>
* Since 10.0, state derivatives and ionospheric parameters derivates are computed
* using automatic differentiation.
* </p>
* @author Joris Olympio
* @since 11.2
*/
public abstract class BaseRangeRateIonosphericDelayModifier {
/** Ionospheric delay model. */
private final IonosphericModel ionoModel;
/** Frequency [Hz]. */
private final double frequency;
/** Constructor.
*
* @param model Ionospheric delay model appropriate for the current range-rate measurement method.
* @param freq frequency of the signal in Hz
*/
protected BaseRangeRateIonosphericDelayModifier(final IonosphericModel model, final double freq) {
this.ionoModel = model;
this.frequency = freq;
}
/** Get the ionospheric delay model.
* @return ionospheric delay model
*/
protected IonosphericModel getIonoModel() {
return ionoModel;
}
/** Compute the measurement error due to Ionosphere.
* @param station station
* @param state spacecraft state
* @return the measurement error due to Ionosphere
*/
protected double rangeRateErrorIonosphericModel(final GroundStation station, final SpacecraftState state) {
final double dt = 10; // s
// Base frame associated with the station
final TopocentricFrame baseFrame = station.getBaseFrame();
// delay in meters
final double delay1 = ionoModel.pathDelay(state, baseFrame, frequency, ionoModel.getParameters());
// propagate spacecraft state forward by dt
final SpacecraftState state2 = state.shiftedBy(dt);
// ionospheric delay dt after in meters
final double delay2 = ionoModel.pathDelay(state2, baseFrame, frequency, ionoModel.getParameters());
// delay in meters
return (delay2 - delay1) / dt;
}
/** Compute the measurement error due to Ionosphere.
* @param <T> type of the elements
* @param station station
* @param state spacecraft state
* @param parameters ionospheric model parameters
* @return the measurement error due to Ionosphere
*/
protected <T extends CalculusFieldElement<T>> T rangeRateErrorIonosphericModel(final GroundStation station,
final FieldSpacecraftState<T> state,
final T[] parameters) {
final double dt = 10; // s
// Base frame associated with the station
final TopocentricFrame baseFrame = station.getBaseFrame();
// delay in meters
final T delay1 = ionoModel.pathDelay(state, baseFrame, frequency, parameters);
// propagate spacecraft state forward by dt
final FieldSpacecraftState<T> state2 = state.shiftedBy(dt);
// ionospheric delay dt after in meters
final T delay2 = ionoModel.pathDelay(state2, baseFrame, frequency, parameters);
// delay in meters
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
*/
public List<ParameterDriver> getParametersDrivers() {
return ionoModel.getParametersDrivers();
}
}
/* 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.List;
import org.hipparchus.CalculusFieldElement;
import org.hipparchus.Field;
import org.hipparchus.geometry.euclidean.threed.FieldVector3D;
import org.hipparchus.geometry.euclidean.threed.Vector3D;
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
* through the computation of the tropospheric delay difference with respect to
* time.
*
* In general, for GNSS, VLBI, ... there is hardly any frequency dependence in the delay.
* For SLR techniques however, the frequency dependence is sensitive.
*
* @author Joris Olympio
* @since 11.2
*/
public abstract class BaseRangeRateTroposphericDelayModifier {
/** Tropospheric delay model. */
private final DiscreteTroposphericModel tropoModel;
/** Constructor.
*
* @param model Tropospheric delay model appropriate for the current range-rate measurement method.
*/
protected BaseRangeRateTroposphericDelayModifier(final DiscreteTroposphericModel model) {
tropoModel = model;
}
/** Get the tropospheric delay model.
* @return tropospheric delay model
*/
protected DiscreteTroposphericModel getTropoModel() {
return tropoModel;
}
/** Compute the measurement error due to Troposphere.
* @param station station
* @param state spacecraft state
* @return the measurement error due to Troposphere
*/
public double rangeRateErrorTroposphericModel(final GroundStation station,
final SpacecraftState state) {
// The effect of tropospheric correction on the range rate is
// computed using finite differences.
final double dt = 10; // s
// spacecraft position and elevation as seen from the ground station
final Vector3D position = state.getPVCoordinates().getPosition();
// elevation
final double elevation1 = station.getBaseFrame().getElevation(position,
state.getFrame(),
state.getDate());
// only consider measures above the horizon
if (elevation1 > 0) {
// tropospheric delay in meters
final double d1 = tropoModel.pathDelay(elevation1, station.getBaseFrame().getPoint(), tropoModel.getParameters(), state.getDate());
// propagate spacecraft state forward by dt
final SpacecraftState state2 = state.shiftedBy(dt);
// spacecraft position and elevation as seen from the ground station
final Vector3D position2 = state2.getPVCoordinates().getPosition();
// elevation
final double elevation2 = station.getBaseFrame().getElevation(position2,
state2.getFrame(),
state2.getDate());
// tropospheric delay dt after
final double d2 = tropoModel.pathDelay(elevation2, station.getBaseFrame().getPoint(), tropoModel.getParameters(), state2.getDate());
return (d2 - d1) / dt;
}
return 0;
}
/** Compute the measurement error due to Troposphere.
* @param <T> type of the element
* @param station station
* @param state spacecraft state
* @param parameters tropospheric model parameters
* @return the measurement error due to Troposphere
*/
public <T extends CalculusFieldElement<T>> T rangeRateErrorTroposphericModel(final GroundStation station,
final FieldSpacecraftState<T> state,
final T[] parameters) {
// Field
final Field<T> field = state.getDate().getField();
final T zero = field.getZero();
// The effect of tropospheric correction on the range rate is
// computed using finite differences.
final double dt = 10; // s
// spacecraft position and elevation as seen from the ground station
final FieldVector3D<T> position = state.getPVCoordinates().getPosition();
final T elevation1 = station.getBaseFrame().getElevation(position,
state.getFrame(),
state.getDate());
// only consider measures above the horizon
if (elevation1.getReal() > 0) {
// tropospheric delay in meters
final T d1 = tropoModel.pathDelay(elevation1, station.getBaseFrame().getPoint(field), parameters, state.getDate());
// propagate spacecraft state forward by dt
final FieldSpacecraftState<T> state2 = state.shiftedBy(dt);
// spacecraft position and elevation as seen from the ground station
final FieldVector3D<T> position2 = state2.getPVCoordinates().getPosition();
// elevation
final T elevation2 = station.getBaseFrame().getElevation(position2,
state2.getFrame(),
state2.getDate());
// tropospheric delay dt after
final T d2 = tropoModel.pathDelay(elevation2, station.getBaseFrame().getPoint(field), parameters, state2.getDate());
return d2.subtract(d1).divide(dt);
}
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
*/
public List<ParameterDriver> getParametersDrivers() {
return tropoModel.getParametersDrivers();
}
}
/* 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 for bistatic measurements.
* @author Pascal Parraud
* @since 11.2
*/
class BistaticModifierUtil {
/** Private constructor for utility class.*/
private BistaticModifierUtil() {
// not used
}
/** Apply a modifier to an estimated measurement.
* @param <T> type of the measurement
* @param estimated estimated measurement to modify
* @param emitter emitter station
* @param receiver receiver 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 emitter, final GroundStation receiver,
final ModelEffect modelEffect,
final ModelEffectGradient 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 delayUp = modelEffectGradient.evaluate(emitter, gState, gParameters);
final double[] derivativesUp = delayUp.getGradient();
final Gradient delayDown = modelEffectGradient.evaluate(receiver, gState, gParameters);
final double[] derivativesDown = delayDown.getGradient();
// update estimated derivatives with Jacobian of the measure wrt state
final double[][] stateDerivatives = estimated.getStateDerivatives(0);
for (int jcol = 0; jcol < stateDerivatives[0].length; ++jcol) {
stateDerivatives[0][jcol] += derivativesUp[jcol];
stateDerivatives[0][jcol] += derivativesDown[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 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];
estimated.setParameterDerivatives(driver, parameterDerivative);
index++;
}
}
for (final ParameterDriver driver : Arrays.asList(emitter.getEastOffsetDriver(),
emitter.getNorthOffsetDriver(),
emitter.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(emitter, state),
3, 10.0 * driver.getScale()).value(driver);
estimated.setParameterDerivatives(driver, parameterDerivative);
}
}
for (final ParameterDriver driver : Arrays.asList(receiver.getClockOffsetDriver(),
receiver.getEastOffsetDriver(),
receiver.getNorthOffsetDriver(),
receiver.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(receiver, state),
3, 10.0 * driver.getScale()).value(driver);
estimated.setParameterDerivatives(driver, parameterDerivative);
}
}
// update estimated value taking into account the model effect.
// The model effect delay is directly added to the measurement.
final double[] newValue = oldValue.clone();
newValue[0] += delayUp.getValue();
newValue[0] += delayDown.getValue();
estimated.setEstimatedValue(newValue);
}
/** 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