Search in sources :

Example 21 with DSFactory

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

the class FieldPropagation method main.

/**
 * Program entry point.
 * @param args program arguments (unused here)
 * @throws IOException
 * @throws OrekitException
 */
public static void main(String[] args) throws IOException, OrekitException {
    // the goal of this example is to make a Montecarlo simulation giving an error on the semiaxis,
    // the inclination and the RAAN. The interest of doing it with Orekit based on the
    // DerivativeStructure is that instead of doing a large number of propagation around the initial
    // point we will do a single propagation of the initial state, and thanks to the Taylor expansion
    // we will see the evolution of the std deviation of the position, which is divided in the
    // CrossTrack, the LongTrack and the Radial error.
    // configure Orekit
    File home = new File(System.getProperty("user.home"));
    File orekitData = new File(home, "orekit-data");
    if (!orekitData.exists()) {
        System.err.format(Locale.US, "Failed to find %s folder%n", orekitData.getAbsolutePath());
        System.err.format(Locale.US, "You need to download %s from the %s page and unzip it in %s for this tutorial to work%n", "orekit-data.zip", "https://www.orekit.org/forge/projects/orekit/files", home.getAbsolutePath());
        System.exit(1);
    }
    DataProvidersManager manager = DataProvidersManager.getInstance();
    manager.addProvider(new DirectoryCrawler(orekitData));
    // output file in user's home directory
    File workingDir = new File(System.getProperty("user.home"));
    File errorFile = new File(workingDir, "error.txt");
    System.out.println("Output file is in : " + errorFile.getAbsolutePath());
    PrintWriter PW = new PrintWriter(errorFile, "UTF-8");
    PW.printf("time \t\tCrossTrackErr \tLongTrackErr  \tRadialErr \tTotalErr%n");
    // setting the parameters of the simulation
    // Order of derivation of the DerivativeStructures
    int params = 3;
    int order = 3;
    DSFactory factory = new DSFactory(params, order);
    // number of samples of the montecarlo simulation
    int montecarlo_size = 100;
    // nominal values of the Orbital parameters
    double a_nominal = 7.278E6;
    double e_nominal = 1e-3;
    double i_nominal = FastMath.toRadians(98.3);
    double pa_nominal = FastMath.PI / 2;
    double raan_nominal = 0.0;
    double ni_nominal = 0.0;
    // mean of the gaussian curve for each of the errors around the nominal values
    // {a, i, RAAN}
    double[] mean = { 0, 0, 0 };
    // standard deviation of the gaussian curve for each of the errors around the nominal values
    // {dA, dI, dRaan}
    double[] dAdIdRaan = { 5, FastMath.toRadians(1e-3), FastMath.toRadians(1e-3) };
    // time of integration
    double final_Dt = 1 * 60 * 60;
    // number of steps per orbit
    double num_step_orbit = 10;
    DerivativeStructure a_0 = factory.variable(0, a_nominal);
    DerivativeStructure e_0 = factory.constant(e_nominal);
    DerivativeStructure i_0 = factory.variable(1, i_nominal);
    DerivativeStructure pa_0 = factory.constant(pa_nominal);
    DerivativeStructure raan_0 = factory.variable(2, raan_nominal);
    DerivativeStructure ni_0 = factory.constant(ni_nominal);
    // sometimes we will need the field of the DerivativeStructure to build new instances
    Field<DerivativeStructure> field = a_0.getField();
    // sometimes we will need the zero of the DerivativeStructure to build new instances
    DerivativeStructure zero = field.getZero();
    // initializing the FieldAbsoluteDate with only the field it will generate the day J2000
    FieldAbsoluteDate<DerivativeStructure> date_0 = new FieldAbsoluteDate<>(field);
    // initialize a basic frame
    Frame frame = FramesFactory.getEME2000();
    // initialize the orbit
    double mu = 3.9860047e14;
    FieldKeplerianOrbit<DerivativeStructure> KO = new FieldKeplerianOrbit<>(a_0, e_0, i_0, pa_0, raan_0, ni_0, PositionAngle.ECCENTRIC, frame, date_0, mu);
    // step of integration (how many times per orbit we take the mesures)
    double int_step = KO.getKeplerianPeriod().getReal() / num_step_orbit;
    // random generator to conduct an
    long number = 23091991;
    RandomGenerator RG = new Well19937a(number);
    GaussianRandomGenerator NGG = new GaussianRandomGenerator(RG);
    UncorrelatedRandomVectorGenerator URVG = new UncorrelatedRandomVectorGenerator(mean, dAdIdRaan, NGG);
    double[][] rand_gen = new double[montecarlo_size][3];
    for (int jj = 0; jj < montecarlo_size; jj++) {
        rand_gen[jj] = URVG.nextVector();
    }
    // 
    FieldSpacecraftState<DerivativeStructure> SS_0 = new FieldSpacecraftState<>(KO);
    // adding force models
    ForceModel fModel_Sun = new ThirdBodyAttraction(CelestialBodyFactory.getSun());
    ForceModel fModel_Moon = new ThirdBodyAttraction(CelestialBodyFactory.getMoon());
    ForceModel fModel_HFAM = new HolmesFeatherstoneAttractionModel(FramesFactory.getITRF(IERSConventions.IERS_2010, true), GravityFieldFactory.getNormalizedProvider(18, 18));
    // setting an hipparchus field integrator
    OrbitType type = OrbitType.CARTESIAN;
    double[][] tolerance = NumericalPropagator.tolerances(0.001, KO.toOrbit(), type);
    AdaptiveStepsizeFieldIntegrator<DerivativeStructure> integrator = new DormandPrince853FieldIntegrator<>(field, 0.001, 200, tolerance[0], tolerance[1]);
    integrator.setInitialStepSize(zero.add(60));
    // setting of the field propagator, we used the numerical one in order to add the third body attraction
    // and the holmes featherstone force models
    FieldNumericalPropagator<DerivativeStructure> numProp = new FieldNumericalPropagator<>(field, integrator);
    numProp.setOrbitType(type);
    numProp.setInitialState(SS_0);
    numProp.addForceModel(fModel_Sun);
    numProp.addForceModel(fModel_Moon);
    numProp.addForceModel(fModel_HFAM);
    // with the master mode we will calulcate and print the error on every fixed step on the file error.txt
    // we defined the StepHandler to do that giving him the random number generator,
    // the size of the montecarlo simulation and the initial date
    numProp.setMasterMode(zero.add(int_step), new MyStepHandler<DerivativeStructure>(rand_gen, montecarlo_size, date_0, PW));
    // 
    long START = System.nanoTime();
    FieldSpacecraftState<DerivativeStructure> finalState = numProp.propagate(date_0.shiftedBy(final_Dt));
    long STOP = System.nanoTime();
    System.out.println((STOP - START) / 1E6 + " ms");
    System.out.println(finalState.getDate());
    PW.close();
}
Also used : Frame(org.orekit.frames.Frame) GaussianRandomGenerator(org.hipparchus.random.GaussianRandomGenerator) ForceModel(org.orekit.forces.ForceModel) Well19937a(org.hipparchus.random.Well19937a) RandomGenerator(org.hipparchus.random.RandomGenerator) GaussianRandomGenerator(org.hipparchus.random.GaussianRandomGenerator) FieldKeplerianOrbit(org.orekit.orbits.FieldKeplerianOrbit) DirectoryCrawler(org.orekit.data.DirectoryCrawler) PrintWriter(java.io.PrintWriter) DormandPrince853FieldIntegrator(org.hipparchus.ode.nonstiff.DormandPrince853FieldIntegrator) FieldSpacecraftState(org.orekit.propagation.FieldSpacecraftState) DerivativeStructure(org.hipparchus.analysis.differentiation.DerivativeStructure) DSFactory(org.hipparchus.analysis.differentiation.DSFactory) ThirdBodyAttraction(org.orekit.forces.gravity.ThirdBodyAttraction) FieldNumericalPropagator(org.orekit.propagation.numerical.FieldNumericalPropagator) DataProvidersManager(org.orekit.data.DataProvidersManager) UncorrelatedRandomVectorGenerator(org.hipparchus.random.UncorrelatedRandomVectorGenerator) OrbitType(org.orekit.orbits.OrbitType) HolmesFeatherstoneAttractionModel(org.orekit.forces.gravity.HolmesFeatherstoneAttractionModel) File(java.io.File) FieldAbsoluteDate(org.orekit.time.FieldAbsoluteDate)

Example 22 with DSFactory

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

the class DSConverter method getState.

/**
 * Get the state with the number of parameters consistent with force model.
 * @param forceModel force model
 * @return state with the number of parameters consistent with force model
 */
public FieldSpacecraftState<DerivativeStructure> getState(final ForceModel forceModel) {
    // count the required number of parameters
    int nbParams = 0;
    for (final ParameterDriver driver : forceModel.getParametersDrivers()) {
        if (driver.isSelected()) {
            ++nbParams;
        }
    }
    // fill in intermediate slots
    while (dsStates.size() < nbParams + 1) {
        dsStates.add(null);
    }
    if (dsStates.get(nbParams) == null) {
        // it is the first time we need this number of parameters
        // we need to create the state
        final DSFactory factory = new DSFactory(freeStateParameters + nbParams, 1);
        final FieldSpacecraftState<DerivativeStructure> s0 = dsStates.get(0);
        // orbit
        final FieldPVCoordinates<DerivativeStructure> pv0 = s0.getPVCoordinates();
        final FieldOrbit<DerivativeStructure> dsOrbit = new FieldCartesianOrbit<>(new TimeStampedFieldPVCoordinates<>(s0.getDate().toAbsoluteDate(), extend(pv0.getPosition(), factory), extend(pv0.getVelocity(), factory), extend(pv0.getAcceleration(), factory)), s0.getFrame(), s0.getMu());
        // attitude
        final FieldAngularCoordinates<DerivativeStructure> ac0 = s0.getAttitude().getOrientation();
        final FieldAttitude<DerivativeStructure> dsAttitude = new FieldAttitude<>(s0.getAttitude().getReferenceFrame(), new TimeStampedFieldAngularCoordinates<>(dsOrbit.getDate(), extend(ac0.getRotation(), factory), extend(ac0.getRotationRate(), factory), extend(ac0.getRotationAcceleration(), factory)));
        // mass
        final DerivativeStructure dsM = extend(s0.getMass(), factory);
        dsStates.set(nbParams, new FieldSpacecraftState<>(dsOrbit, dsAttitude, dsM));
    }
    return dsStates.get(nbParams);
}
Also used : DerivativeStructure(org.hipparchus.analysis.differentiation.DerivativeStructure) DSFactory(org.hipparchus.analysis.differentiation.DSFactory) ParameterDriver(org.orekit.utils.ParameterDriver) FieldCartesianOrbit(org.orekit.orbits.FieldCartesianOrbit) FieldAttitude(org.orekit.attitudes.FieldAttitude)

Example 23 with DSFactory

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

the class DSConverter method getParameters.

/**
 * Get the force model parameters.
 * @param state state as returned by {@link #getState(ForceModel)}
 * @param forceModel force model associated with the parameters
 * @return force model parameters
 * @since 9.0
 */
public DerivativeStructure[] getParameters(final FieldSpacecraftState<DerivativeStructure> state, final ForceModel forceModel) {
    final DSFactory factory = state.getMass().getFactory();
    final ParameterDriver[] drivers = forceModel.getParametersDrivers();
    final DerivativeStructure[] parameters = new DerivativeStructure[drivers.length];
    int index = freeStateParameters;
    for (int i = 0; i < drivers.length; ++i) {
        parameters[i] = drivers[i].isSelected() ? factory.variable(index++, drivers[i].getValue()) : factory.constant(drivers[i].getValue());
    }
    return parameters;
}
Also used : DerivativeStructure(org.hipparchus.analysis.differentiation.DerivativeStructure) DSFactory(org.hipparchus.analysis.differentiation.DSFactory) ParameterDriver(org.orekit.utils.ParameterDriver)

Example 24 with DSFactory

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

the class RelativityTest method RealFieldExpectErrorTest.

/**
 *Same test as the previous one but not adding the ForceModel to the NumericalPropagator
 *        it is a test to validate the previous test.
 *        (to test if the ForceModel it's actually
 *        doing something in the Propagator and the FieldPropagator)
 */
@Test
public void RealFieldExpectErrorTest() throws OrekitException {
    DSFactory factory = new DSFactory(6, 0);
    DerivativeStructure a_0 = factory.variable(0, 7e7);
    DerivativeStructure e_0 = factory.variable(1, 0.4);
    DerivativeStructure i_0 = factory.variable(2, 85 * FastMath.PI / 180);
    DerivativeStructure R_0 = factory.variable(3, 0.7);
    DerivativeStructure O_0 = factory.variable(4, 0.5);
    DerivativeStructure n_0 = factory.variable(5, 0.1);
    Field<DerivativeStructure> field = a_0.getField();
    DerivativeStructure zero = field.getZero();
    FieldAbsoluteDate<DerivativeStructure> J2000 = new FieldAbsoluteDate<>(field);
    Frame EME = FramesFactory.getEME2000();
    FieldKeplerianOrbit<DerivativeStructure> FKO = new FieldKeplerianOrbit<>(a_0, e_0, i_0, R_0, O_0, n_0, PositionAngle.MEAN, EME, J2000, Constants.EIGEN5C_EARTH_MU);
    FieldSpacecraftState<DerivativeStructure> initialState = new FieldSpacecraftState<>(FKO);
    SpacecraftState iSR = initialState.toSpacecraftState();
    OrbitType type = OrbitType.KEPLERIAN;
    double[][] tolerance = NumericalPropagator.tolerances(0.001, FKO.toOrbit(), type);
    AdaptiveStepsizeFieldIntegrator<DerivativeStructure> integrator = new DormandPrince853FieldIntegrator<>(field, 0.001, 200, tolerance[0], tolerance[1]);
    integrator.setInitialStepSize(zero.add(60));
    AdaptiveStepsizeIntegrator RIntegrator = new DormandPrince853Integrator(0.001, 200, tolerance[0], tolerance[1]);
    RIntegrator.setInitialStepSize(60);
    FieldNumericalPropagator<DerivativeStructure> FNP = new FieldNumericalPropagator<>(field, integrator);
    FNP.setOrbitType(type);
    FNP.setInitialState(initialState);
    NumericalPropagator NP = new NumericalPropagator(RIntegrator);
    NP.setOrbitType(type);
    NP.setInitialState(iSR);
    final Relativity forceModel = new Relativity(Constants.EIGEN5C_EARTH_MU);
    FNP.addForceModel(forceModel);
    // NOT ADDING THE FORCE MODEL TO THE NUMERICAL PROPAGATOR   NP.addForceModel(forceModel);
    FieldAbsoluteDate<DerivativeStructure> target = J2000.shiftedBy(1000.);
    FieldSpacecraftState<DerivativeStructure> finalState_DS = FNP.propagate(target);
    SpacecraftState finalState_R = NP.propagate(target.toAbsoluteDate());
    FieldPVCoordinates<DerivativeStructure> finPVC_DS = finalState_DS.getPVCoordinates();
    PVCoordinates finPVC_R = finalState_R.getPVCoordinates();
    Assert.assertEquals(0, Vector3D.distance(finPVC_DS.toPVCoordinates().getPosition(), finPVC_R.getPosition()), 8.0e-13 * finPVC_R.getPosition().getNorm());
}
Also used : DormandPrince853FieldIntegrator(org.hipparchus.ode.nonstiff.DormandPrince853FieldIntegrator) Frame(org.orekit.frames.Frame) FieldSpacecraftState(org.orekit.propagation.FieldSpacecraftState) AdaptiveStepsizeIntegrator(org.hipparchus.ode.nonstiff.AdaptiveStepsizeIntegrator) DerivativeStructure(org.hipparchus.analysis.differentiation.DerivativeStructure) DSFactory(org.hipparchus.analysis.differentiation.DSFactory) FieldPVCoordinates(org.orekit.utils.FieldPVCoordinates) PVCoordinates(org.orekit.utils.PVCoordinates) FieldKeplerianOrbit(org.orekit.orbits.FieldKeplerianOrbit) FieldSpacecraftState(org.orekit.propagation.FieldSpacecraftState) SpacecraftState(org.orekit.propagation.SpacecraftState) FieldNumericalPropagator(org.orekit.propagation.numerical.FieldNumericalPropagator) FieldNumericalPropagator(org.orekit.propagation.numerical.FieldNumericalPropagator) NumericalPropagator(org.orekit.propagation.numerical.NumericalPropagator) OrbitType(org.orekit.orbits.OrbitType) DormandPrince853Integrator(org.hipparchus.ode.nonstiff.DormandPrince853Integrator) FieldAbsoluteDate(org.orekit.time.FieldAbsoluteDate) AbstractLegacyForceModelTest(org.orekit.forces.AbstractLegacyForceModelTest) Test(org.junit.Test)

Example 25 with DSFactory

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

the class CartesianOrbitTest method differentiate.

private <S extends Function<CartesianOrbit, Double>> double differentiate(TimeStampedPVCoordinates pv, Frame frame, double mu, S picker) {
    final DSFactory factory = new DSFactory(1, 1);
    FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(8, 0.1);
    UnivariateDifferentiableFunction diff = differentiator.differentiate(new UnivariateFunction() {

        public double value(double dt) {
            return picker.apply(new CartesianOrbit(pv.shiftedBy(dt), frame, mu));
        }
    });
    return diff.value(factory.variable(0, 0.0)).getPartialDerivative(1);
}
Also used : UnivariateFunction(org.hipparchus.analysis.UnivariateFunction) DSFactory(org.hipparchus.analysis.differentiation.DSFactory) UnivariateDifferentiableFunction(org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction) FiniteDifferencesDifferentiator(org.hipparchus.analysis.differentiation.FiniteDifferencesDifferentiator)

Aggregations

DSFactory (org.hipparchus.analysis.differentiation.DSFactory)76 DerivativeStructure (org.hipparchus.analysis.differentiation.DerivativeStructure)64 Test (org.junit.Test)41 FieldAbsoluteDate (org.orekit.time.FieldAbsoluteDate)36 FiniteDifferencesDifferentiator (org.hipparchus.analysis.differentiation.FiniteDifferencesDifferentiator)25 SpacecraftState (org.orekit.propagation.SpacecraftState)24 Frame (org.orekit.frames.Frame)23 AbsoluteDate (org.orekit.time.AbsoluteDate)20 UnivariateFunction (org.hipparchus.analysis.UnivariateFunction)18 UnivariateDifferentiableFunction (org.hipparchus.analysis.differentiation.UnivariateDifferentiableFunction)17 FieldSpacecraftState (org.orekit.propagation.FieldSpacecraftState)17 PVCoordinates (org.orekit.utils.PVCoordinates)17 FieldVector3D (org.hipparchus.geometry.euclidean.threed.FieldVector3D)16 Vector3D (org.hipparchus.geometry.euclidean.threed.Vector3D)15 OrbitType (org.orekit.orbits.OrbitType)15 RandomGenerator (org.hipparchus.random.RandomGenerator)14 FieldKeplerianOrbit (org.orekit.orbits.FieldKeplerianOrbit)14 FieldNumericalPropagator (org.orekit.propagation.numerical.FieldNumericalPropagator)14 NumericalPropagator (org.orekit.propagation.numerical.NumericalPropagator)14 FieldPVCoordinates (org.orekit.utils.FieldPVCoordinates)14