1   /* Copyright 2002-2025 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  
19  import org.hipparchus.util.FastMath;
20  import org.orekit.time.AbsoluteDate;
21  
22  /** Simple implementation of {@link RawSphericalHarmonicsProvider} for constant gravity fields.
23   * @author Luc Maisonobe
24   * @since 6.0
25   */
26  class ConstantSphericalHarmonics implements RawSphericalHarmonicsProvider {
27  
28      /** Central body reference radius. */
29      private final double ae;
30  
31      /** Central body attraction coefficient. */
32      private final double mu;
33  
34      /** Tide system. */
35      private final TideSystem tideSystem;
36  
37      /** Converter from triangular to flatten array.
38       * @since 11.1
39       */
40      private final Flattener flattener;
41  
42      /** Raw tesseral-sectorial coefficients matrix. */
43      private final double[] rawC;
44  
45      /** Raw tesseral-sectorial coefficients matrix. */
46      private final double[] rawS;
47  
48      /** Simple constructor.
49       * @param ae central body reference radius
50       * @param mu central body attraction coefficient
51       * @param tideSystem tide system
52       * @param flattener flattener from triangular to flatten array
53       * @param rawC raw tesseral-sectorial coefficients
54       * @param rawS raw tesseral-sectorial coefficients
55       * @since 11.1
56       */
57      ConstantSphericalHarmonics(final double ae, final double mu, final TideSystem tideSystem,
58                                 final Flattener flattener, final double[] rawC, final double[] rawS) {
59          this.ae         = ae;
60          this.mu         = mu;
61          this.tideSystem = tideSystem;
62          this.flattener  = flattener;
63          this.rawC       = rawC;
64          this.rawS       = rawS;
65      }
66  
67      /** Create a constant provider by freezing a regular provider.
68       * @param freezingDate freezing date
69       * @param raw raw provider to freeze
70       * @since 11.1
71       */
72      ConstantSphericalHarmonics(final AbsoluteDate freezingDate, final RawSphericalHarmonicsProvider raw) {
73  
74          this.ae         = raw.getAe();
75          this.mu         = raw.getMu();
76          this.tideSystem = raw.getTideSystem();
77          this.flattener  = new Flattener(raw.getMaxDegree(), raw.getMaxOrder());
78          this.rawC       = new double[flattener.arraySize()];
79          this.rawS       = new double[flattener.arraySize()];
80  
81          // freeze the raw provider
82          final RawSphericalHarmonics frozen = raw.onDate(freezingDate);
83          for (int n = 0; n <= flattener.getDegree(); ++n) {
84              for (int m = 0; m <= FastMath.min(n, flattener.getOrder()); ++m) {
85                  final int index = flattener.index(n, m);
86                  rawC[index] = frozen.getRawCnm(n, m);
87                  rawS[index] = frozen.getRawSnm(n, m);
88              }
89          }
90  
91      }
92  
93      /** {@inheritDoc} */
94      public int getMaxDegree() {
95          return flattener.getDegree();
96      }
97  
98      /** {@inheritDoc} */
99      public int getMaxOrder() {
100         return flattener.getOrder();
101     }
102 
103     /** {@inheritDoc} */
104     public double getMu() {
105         return mu;
106     }
107 
108     /** {@inheritDoc} */
109     public double getAe() {
110         return ae;
111     }
112 
113     /** {@inheritDoc}
114      * <p>
115      * For a constant field, null is always returned.
116      * </p>
117      */
118     public AbsoluteDate getReferenceDate() {
119         return null;
120     }
121 
122     /** {@inheritDoc} */
123     public TideSystem getTideSystem() {
124         return tideSystem;
125     }
126 
127     @Override
128     public RawSphericalHarmonics onDate(final AbsoluteDate date) {
129         return new RawSphericalHarmonics() {
130 
131             @Override
132             public AbsoluteDate getDate() {
133                 return date;
134             }
135 
136             /** {@inheritDoc} */
137             public double getRawCnm(final int n, final int m) {
138                 return rawC[flattener.index(n, m)];
139             }
140 
141             /** {@inheritDoc} */
142             public double getRawSnm(final int n, final int m) {
143                 return rawS[flattener.index(n, m)];
144             }
145 
146         };
147     }
148 
149 }
150