L1Frame gives a wrong L1 position when evaluated with derivative field types (sign error in L1TransformProvider)

Versions:
The below issue (bug) was found with Orekit 13.1.7 (bundled with orekit_jpype 13.1.7.1). It has also been reproduced with plain Orekit 13.1.7 and Hipparchus 4.0.3 JARs, without JPype, and it gives the same result. The code is unchanged on develop.

Problem:
L1Frame places L1 at the wrong position when the transform is evaluated with a FieldAbsoluteDate that has a non-zero derivative part, for example UnivariateDerivative1/UnivariateDerivative2 as used in automatic differentiation. Two examples are FrameAdapter(new L1Frame(…)).getPosition(fieldDate, frame) and the deprecated toExtendedPVCoordinatesProvider(). In our tests the error was about 15,000 km for Earth–Moon L1 and about 640,000 km for Sun / Earth–Moon-barycenter L1. The non-field code, including getPVCoordinates(AbsoluteDate, Frame), is correct, and so is L2Frame.

Field dates whose field part is zero (e.g. any Binary64 date, or derivative types with zero derivatives) don’t show the bug, because Frame.getTransformTo then falls back to the double code (date.hasZeroField()). This is probably why existing field tests didn’t catch it.

Cause:
In L1TransformProvider, the field version of getL1 has the wrong sign on the last term of the equilibrium equation:

// double version (correct)
return lhs - (rhs1 + rhs2 - rhs3);

// field version (wrong)
return lhs.subtract(rhs1.add(rhs2).add(rhs3));

The suggested fix is return lhs.subtract(rhs1.add(rhs2).subtract(rhs3));. Solving both equations for the case below gives exactly the two values Orekit returns (311,825 km and 296,959 km).

There is also a smaller inconsistency: the field version brackets the root on [0, 2R], the double version on [0, R].

Reproduction with Orekit Jpype (pip install orekit_jpype orekitdata):

import orekit_jpype
orekit_jpype.initVM()
from orekit_jpype.pyhelpers import setup_orekit_data
setup_orekit_data()

from org.hipparchus.analysis.differentiation import UnivariateDerivative1
from org.orekit.bodies import CelestialBodyFactory
from org.orekit.frames import FramesFactory, L1Frame, L2Frame
from org.orekit.time import AbsoluteDate, FieldAbsoluteDate, TimeScalesFactory
from org.orekit.utils import FrameAdapter

gcrf = FramesFactory.getGCRF()
date = AbsoluteDate(2023, 10, 1, 0, 0, 0.0, TimeScalesFactory.getUTC())

# Same date, but as a field date with derivative d(date)/dt = 1
dt = UnivariateDerivative1(0.0, 1.0)
field_date = FieldAbsoluteDate(dt.getField(), date).shiftedBy(dt)

earth, moon = CelestialBodyFactory.getEarth(), CelestialBodyFactory.getMoon()
for frame in (L1Frame(earth, moon), L2Frame(earth, moon)):
    point = FrameAdapter(frame)
    p = point.getPosition(date, gcrf)
    p_field = point.getPosition(field_date, gcrf).toVector3D()
    print(f"{frame.getName()}: distance from Earth {p.getNorm() / 1e3:,.0f} km (double), "
          f"{p_field.getNorm() / 1e3:,.0f} km (field), difference {p.subtract(p_field).getNorm() / 1e3:,.0f} km")

Output:

Earth-Moon-L1: distance from Earth 311,825 km (double), 296,959 km (field), difference 14,865 km
Earth-Moon-L2: distance from Earth 428,894 km (double), 428,894 km (field), difference 0 km

Reproduction with plain Orekit 13.1.7 and Hipparchus 4.0.3 JARs:

import java.io.File;

import org.hipparchus.analysis.differentiation.UnivariateDerivative1;
import org.hipparchus.geometry.euclidean.threed.Vector3D;
import org.orekit.bodies.CelestialBody;
import org.orekit.bodies.CelestialBodyFactory;
import org.orekit.data.DataContext;
import org.orekit.data.DirectoryCrawler;
import org.orekit.frames.Frame;
import org.orekit.frames.FramesFactory;
import org.orekit.frames.L1Frame;
import org.orekit.frames.L2Frame;
import org.orekit.time.AbsoluteDate;
import org.orekit.time.FieldAbsoluteDate;
import org.orekit.time.TimeScalesFactory;
import org.orekit.utils.FrameAdapter;

public class L1FrameFieldBug {

    public static void main(final String[] args) {
        DataContext.getDefault().getDataProvidersManager().addProvider(new DirectoryCrawler(new File(args[0])));

        final Frame gcrf = FramesFactory.getGCRF();
        final AbsoluteDate date = new AbsoluteDate(2023, 10, 1, 0, 0, 0.0, TimeScalesFactory.getUTC());

        // Same date, but as a field date with derivative d(date)/dt = 1
        final UnivariateDerivative1 dt = new UnivariateDerivative1(0.0, 1.0);
        final FieldAbsoluteDate<UnivariateDerivative1> fieldDate = new FieldAbsoluteDate<>(dt.getField(), date).shiftedBy(dt);

        final CelestialBody earth = CelestialBodyFactory.getEarth();
        final CelestialBody moon  = CelestialBodyFactory.getMoon();
        for (final Frame frame : new Frame[] {new L1Frame(earth, moon), new L2Frame(earth, moon)}) {
            final FrameAdapter point = new FrameAdapter(frame);
            final Vector3D p      = point.getPosition(date, gcrf);
            final Vector3D pField = point.getPosition(fieldDate, gcrf).toVector3D();
            System.out.printf("%s: distance from Earth %,.0f km (double), %,.0f km (field), difference %,.0f km%n",
                              frame.getName(), p.getNorm() / 1e3, pField.getNorm() / 1e3,
                              p.subtract(pField).getNorm() / 1e3);
        }
    }
}

Run it with Java 11 or newer, with the Orekit 13.1.x and Hipparchus 4.0.x JARs on the classpath: java -cp “[orekit and hipparchus jars]” L1FrameFieldBug.java /path/to/orekit-data

Output:

Earth-Moon-L1: distance from Earth 311,825 km (double), 296,959 km (field), difference 14,865 km
Earth-Moon-L2: distance from Earth 428,894 km (double), 428,894 km (field), difference 0 km

Hi,

Looks legit. Do you mind opening an issue on gitlab and maybe also an MR with your proposed fix?
You can base it on release-13.1 as this is eligible for a patch

Cheers,
Romain.

Hello @sepoma,

It looks like you found out an ugly bug!
Thank you for this.

Done :slightly_smiling_face:. Here it is: