1 /* Copyright 2002-2026 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.utils.drivers;
18
19 import java.util.Map;
20
21 import org.hipparchus.analysis.differentiation.Gradient;
22 import org.hipparchus.util.FastMath;
23 import org.orekit.time.AbsoluteDate;
24 import org.orekit.time.TimeInterval;
25
26 /** Class allowing to drive the value of a parameter.
27 * <p>
28 * This class is typically used as a bridge between an estimation algorithm
29 * (typically orbit determination or optimizer) and an internal parameter in
30 * a physical model that needs to be tuned. The physical model will expose to
31 * the algorithm a set of instances of this class so the algorithm can call the
32 * {@link #setValue(double)} method to update the parameter value.
33 * </p>
34 * <p>
35 * Any object can be notified when any of value, name, selection status… are changed.
36 * This is done by {@link BaseParameterDriver#addObserver(BaseParameterObserver)
37 * registering} a {@link ParameterObserver parameter observer} to the parameter driver.
38 * <p>
39 * This design has two major goals. First, it allows an external algorithm to drive
40 * internal parameters blindly, as it only needs to get a list of instances of this
41 * class, without knowing what they really drive. Second, it allows the physical
42 * model to not expose directly setters methods for its parameters. In order to be
43 * able to modify the parameter value, the algorithm <em>must</em> retrieve a
44 * parameter driver.
45 * </p>
46 * <p>
47 * As of versions 12.X and 13.X, it was possible to set up time-dependent values
48 * within a single {@code ParameterDriver}. This feature has been replaced by
49 * {@link ParameterDriversSequence} as of 14.0, which is simpler and also allows
50 * finer selection, making it possible to select only a subset of the parameters
51 * along a timeline. Starting with version 14.0, {@code ParameterDriver} instances
52 * only hold one value, which can have a restricted validity range.
53 * </p>
54 * @see ParameterObserver
55 * @author Luc Maisonobe
56 * @author Melina Vanel
57 * @since 8.0
58 */
59 public class ParameterDriver extends BaseParameterDriver<ParameterDriver, ParameterObserver> {
60
61 /** Reference date.
62 * @since 9.0
63 */
64 private AbsoluteDate referenceDate;
65
66 /** Reference value. */
67 private double referenceValue;
68
69 /** Current value.
70 * @since 14.0
71 */
72 private double value;
73
74 /**
75 * Simple constructor.
76 * <p>
77 * At construction, the parameter is configured as <em>not</em> selected, the reference date is set to {@code null},
78 * the value is set to the {@code referenceValue}.
79 * </p>
80 * @param name name of the parameter
81 * @param referenceValue reference value of the parameter
82 * @param scale scaling factor to convert the parameters value to non-dimensional (typically set to the
83 * expected standard deviation of the parameter), it must be non-zero
84 * @param minValue minimum value allowed
85 * @param maxValue maximum value allowed
86 * @param validity validity interval
87 */
88 public ParameterDriver(final String name,
89 final double referenceValue, final double scale,
90 final double minValue, final double maxValue,
91 final TimeInterval validity) {
92 super(name, scale, minValue, maxValue, validity);
93 this.referenceValue = referenceValue;
94 this.value = referenceValue;
95 }
96
97 /** Get current reference date.
98 * @return current reference date (null if it was never set)
99 * @since 9.0
100 */
101 public AbsoluteDate getReferenceDate() {
102 return referenceDate;
103 }
104
105 /** Set reference date.
106 * @param newReferenceDate new reference date
107 * @since 9.0
108 */
109 public void setReferenceDate(final AbsoluteDate newReferenceDate) {
110 final AbsoluteDate previousReferenceDate = getReferenceDate();
111 referenceDate = newReferenceDate;
112 for (final ParameterObserver observer : getObservers()) {
113 observer.referenceDateChanged(previousReferenceDate, this);
114 }
115 }
116
117 /** Get reference parameter value.
118 * @return reference parameter value
119 */
120 public double getReferenceValue() {
121 return referenceValue;
122 }
123
124 /** Set reference parameter value.
125 * @since 9.3
126 * @param referenceValue the reference value to set.
127 */
128 public void setReferenceValue(final double referenceValue) {
129 final double previousReferenceValue = this.referenceValue;
130 this.referenceValue = referenceValue;
131 for (final ParameterObserver observer : getObservers()) {
132 observer.referenceValueChanged(previousReferenceValue, this);
133 }
134 }
135
136 /** Get current parameter value.
137 * @return current parameter value
138 */
139 public double getValue() {
140 return value;
141 }
142
143 /** Get the value as a gradient.
144 * @param freeParameters total number of free parameters in the gradient
145 * @param indices indices of the differentiation parameters in derivatives computations
146 * @return value with derivatives
147 * @since 10.2
148 */
149 public Gradient getValue(final int freeParameters, final Map<String, Integer> indices) {
150 final Integer index = indices.get(getName());
151 return (index == null) ?
152 Gradient.constant(freeParameters, getValue()) :
153 Gradient.variable(freeParameters, index, getValue());
154 }
155
156 /** Set parameter value.
157 * <p>
158 * If {@code newValue} is below {@link #getMinValue()}, it will
159 * be silently set to {@link #getMinValue()}. If {@code newValue} is
160 * above {@link #getMaxValue()}, it will be silently set to {@link
161 * #getMaxValue()}.
162 * </p>
163 * @param newValue new value to set
164 */
165 public void setValue(final double newValue) {
166 final double previousValue = value;
167 value = FastMath.max(FastMath.min(newValue, getMaxValue()), getMinValue());
168 for (final ParameterObserver observer : getObservers()) {
169 observer.valueChanged(previousValue, this);
170 }
171 }
172
173 /** Get normalized value.
174 * <p>
175 * The normalized value is a non-dimensional value
176 * suitable for use as part of a vector in an optimization
177 * process. It is computed as {@code (current - reference)/scale}.
178 * </p>
179 * @return normalized value
180 */
181 public double getNormalizedValue() {
182 return (value - referenceValue) / getScale();
183 }
184
185 /** Set normalized value.
186 * <p>
187 * The normalized value is a non-dimensional value
188 * suitable for use as part of a vector in an optimization
189 * process. It is computed as {@code (current - reference)/scale}.
190 * </p>
191 * @param normalized value
192 */
193 public void setNormalizedValue(final double normalized) {
194 setValue(referenceValue + getScale() * normalized);
195 }
196
197 /** Set minimum parameter value.
198 * @since 9.3
199 * @param minValue the minimum value to set.
200 */
201 public void setMinValue(final double minValue) {
202
203 // base handling of the new minimum value
204 super.setMinValue(minValue);
205
206 if (value < minValue) {
207 // clip value to minimum
208 setValue(minValue);
209 }
210
211 }
212
213 /** Set maximum parameter value.
214 * @since 9.3
215 * @param maxValue the maximum value to set.
216 */
217 public void setMaxValue(final double maxValue) {
218
219 // base handling of the new maximum value
220 super.setMaxValue(maxValue);
221
222 if (value > maxValue) {
223 // clip value to maximum
224 setValue(maxValue);
225 }
226
227 }
228
229 /** Get a text representation of the parameter.
230 * @return text representation of the parameter, in the form name = value.
231 */
232 public String toString() {
233 return getName() + " = " + value;
234 }
235
236 }