CachedNormalizedSphericalHarmonicsProvider.java

  1. /* Copyright 2002-2022 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.forces.gravity.potential;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import java.util.stream.Stream;

  23. import org.hipparchus.analysis.interpolation.HermiteInterpolator;
  24. import org.orekit.errors.OrekitException;
  25. import org.orekit.errors.TimeStampedCacheException;
  26. import org.orekit.time.AbsoluteDate;
  27. import org.orekit.time.TimeStamped;
  28. import org.orekit.utils.GenericTimeStampedCache;
  29. import org.orekit.utils.TimeStampedCache;
  30. import org.orekit.utils.TimeStampedGenerator;

  31. /** Caching wrapper for {@link NormalizedSphericalHarmonicsProvider}.
  32.  * <p>
  33.  * This wrapper improves efficiency of {@link NormalizedSphericalHarmonicsProvider}
  34.  * by sampling the values at a user defined rate and using interpolation
  35.  * between samples. This is important with providers that have sub-daily
  36.  * frequencies and are computing intensive, such as tides fields.
  37.  * </p>
  38.  * @see NormalizedSphericalHarmonicsProvider
  39.  * @see org.orekit.forces.gravity.SolidTides
  40.  * @see TimeStampedCache
  41.  * @author Luc Maisonobe
  42.  * @since 6.1
  43.  */
  44. public class CachedNormalizedSphericalHarmonicsProvider implements NormalizedSphericalHarmonicsProvider {

  45.     /** Underlying raw provider. */
  46.     private final NormalizedSphericalHarmonicsProvider rawProvider;

  47.     /** Number of coefficients in C<sub>n, m</sub> and S<sub>n, m</sub> arrays (counted separately). */
  48.     private final int size;

  49.     /** Cache. */
  50.     private final TimeStampedCache<TimeStampedSphericalHarmonics> cache;

  51.     /** Simple constructor.
  52.      * @param rawProvider underlying raw provider
  53.      * @param step time step between sample points for interpolation
  54.      * @param nbPoints number of points to use for interpolation, must be at least 2
  55.      * @param maxSlots maximum number of independent cached time slots
  56.      * @param maxSpan maximum duration span in seconds of one slot
  57.      * (can be set to {@code Double.POSITIVE_INFINITY} if desired)
  58.      * @param newSlotInterval time interval above which a new slot is created
  59.      * instead of extending an existing one
  60.      */
  61.     public CachedNormalizedSphericalHarmonicsProvider(final NormalizedSphericalHarmonicsProvider rawProvider,
  62.                                                       final double step, final int nbPoints,
  63.                                                       final int maxSlots, final double maxSpan,
  64.                                                       final double newSlotInterval) {

  65.         this.rawProvider  = rawProvider;
  66.         final int k       = rawProvider.getMaxDegree() + 1;
  67.         this.size         = (k * (k + 1)) / 2;

  68.         cache = new GenericTimeStampedCache<TimeStampedSphericalHarmonics>(nbPoints, maxSlots, maxSpan,
  69.                                                                            newSlotInterval, new Generator(step));
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public int getMaxDegree() {
  74.         return rawProvider.getMaxDegree();
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public int getMaxOrder() {
  79.         return rawProvider.getMaxOrder();
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public double getMu() {
  84.         return rawProvider.getMu();
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public double getAe() {
  89.         return rawProvider.getAe();
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public AbsoluteDate getReferenceDate() {
  94.         return rawProvider.getReferenceDate();
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Deprecated
  98.     @Override
  99.     public double getOffset(final AbsoluteDate date) {
  100.         return rawProvider.getOffset(date);
  101.     }

  102.     /** {@inheritDoc} */
  103.     @Override
  104.     public TideSystem getTideSystem() {
  105.         return rawProvider.getTideSystem();
  106.     }

  107.     /** {@inheritDoc} */
  108.     @Override
  109.     public NormalizedSphericalHarmonics onDate(final AbsoluteDate date) {
  110.         return TimeStampedSphericalHarmonics.interpolate(date, cache.getNeighbors(date));
  111.     }

  112.     /** Generator for time-stamped spherical harmonics. */
  113.     private class Generator implements TimeStampedGenerator<TimeStampedSphericalHarmonics> {

  114.         /** Time step between generated sets. */
  115.         private final double step;

  116.         /** Simple constructor.
  117.          * @param step time step between generated sets
  118.          */
  119.         Generator(final double step) {
  120.             this.step = step;
  121.         }

  122.         /** {@inheritDoc} */
  123.         @Override
  124.         public List<TimeStampedSphericalHarmonics> generate(final AbsoluteDate existingDate,
  125.                                                             final AbsoluteDate date) {
  126.             try {

  127.                 final List<TimeStampedSphericalHarmonics> generated =
  128.                         new ArrayList<TimeStampedSphericalHarmonics>();
  129.                 final double[] cnmsnm = new double[2 * size];

  130.                 if (existingDate == null) {

  131.                     // no prior existing transforms, just generate a first set
  132.                     for (int i = 0; i < cache.getNeighborsSize(); ++i) {
  133.                         final AbsoluteDate t = date.shiftedBy((i - cache.getNeighborsSize() / 2) * step);
  134.                         fillArray(rawProvider.onDate(t), cnmsnm);
  135.                         generated.add(new TimeStampedSphericalHarmonics(t, cnmsnm));
  136.                     }

  137.                 } else {

  138.                     // some coefficients have already been generated
  139.                     // add the missing ones up to specified date

  140.                     AbsoluteDate t = existingDate;
  141.                     if (date.compareTo(t) > 0) {
  142.                         // forward generation
  143.                         do {
  144.                             t = t.shiftedBy(step);
  145.                             fillArray(rawProvider.onDate(t), cnmsnm);
  146.                             generated.add(new TimeStampedSphericalHarmonics(t, cnmsnm));
  147.                         } while (t.compareTo(date) <= 0);
  148.                     } else {
  149.                         // backward generation
  150.                         do {
  151.                             t = t.shiftedBy(-step);
  152.                             fillArray(rawProvider.onDate(t), cnmsnm);
  153.                             generated.add(new TimeStampedSphericalHarmonics(t, cnmsnm));
  154.                         } while (t.compareTo(date) >= 0);
  155.                         // ensure forward chronological order
  156.                         Collections.reverse(generated);
  157.                     }

  158.                 }

  159.                 // return the generated sample
  160.                 return generated;

  161.             } catch (OrekitException oe) {
  162.                 throw new TimeStampedCacheException(oe);
  163.             }
  164.         }

  165.         /** Fill coefficients array for one entry.
  166.          * @param raw the un-interpolated spherical harmonics
  167.          * @param cnmsnm arrays to fill in
  168.          */
  169.         private void fillArray(final NormalizedSphericalHarmonics raw,
  170.                                final double[] cnmsnm) {
  171.             int index = 0;
  172.             for (int n = 0; n <= rawProvider.getMaxDegree(); ++n) {
  173.                 for (int m = 0; m <= n; ++m) {
  174.                     cnmsnm[index++] = raw.getNormalizedCnm(n, m);
  175.                 }
  176.             }
  177.             for (int n = 0; n <= rawProvider.getMaxDegree(); ++n) {
  178.                 for (int m = 0; m <= n; ++m) {
  179.                     cnmsnm[index++] = raw.getNormalizedSnm(n, m);
  180.                 }
  181.             }
  182.         }

  183.     }

  184.     /**
  185.      * Internal class for time-stamped spherical harmonics. Instances are created using
  186.      * {@link #interpolate(AbsoluteDate, Collection)}
  187.      */
  188.     private static class TimeStampedSphericalHarmonics
  189.             implements TimeStamped, NormalizedSphericalHarmonics {

  190.         /** Current date. */
  191.         private final AbsoluteDate date;

  192.         /** number of C or S coefficients. */
  193.         private final int size;

  194.         /** Flattened array for C<sub>n,m</sub> and S<sub>n,m</sub> coefficients. */
  195.         private final double[] cnmsnm;

  196.         /** Simple constructor.
  197.          * @param date current date
  198.          * @param cnmsnm flattened array for C<sub>n,m</sub> and S<sub>n,m</sub>
  199.          *               coefficients. It is copied.
  200.          */
  201.         private TimeStampedSphericalHarmonics(final AbsoluteDate date,
  202.                                               final double[] cnmsnm) {
  203.             this.date   = date;
  204.             this.cnmsnm = cnmsnm.clone();
  205.             this.size   = cnmsnm.length / 2;
  206.         }

  207.         /** {@inheritDoc} */
  208.         @Override
  209.         public AbsoluteDate getDate() {
  210.             return date;
  211.         }

  212.         /** {@inheritDoc} */
  213.         @Override
  214.         public double getNormalizedCnm(final int n, final int m) {
  215.             return cnmsnm[(n * (n + 1)) / 2 + m];
  216.         }

  217.         /** {@inheritDoc} */
  218.         @Override
  219.         public double getNormalizedSnm(final int n, final int m) {
  220.             return cnmsnm[(n * (n + 1)) / 2 + m + size];
  221.         }

  222.         /** Interpolate spherical harmonics.
  223.          * <p>
  224.          * The interpolated instance is created by polynomial Hermite interpolation.
  225.          * </p>
  226.          * @param date interpolation date
  227.          * @param sample sample points on which interpolation should be done
  228.          * @return a new time-stamped spherical harmonics, interpolated at specified date
  229.          */
  230.         public static TimeStampedSphericalHarmonics interpolate(final AbsoluteDate date,
  231.                                                                 final Stream<TimeStampedSphericalHarmonics> sample) {

  232.             // set up an interpolator taking derivatives into account
  233.             final HermiteInterpolator interpolator = new HermiteInterpolator();

  234.             // add sample points
  235.             sample.forEach(tssh -> interpolator.addSamplePoint(tssh.date.durationFrom(date), tssh.cnmsnm));

  236.             // build a new interpolated instance
  237.             return new TimeStampedSphericalHarmonics(date, interpolator.value(0.0));

  238.         }

  239.     }

  240. }