View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF 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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.ode.nonstiff;
24  
25  import org.hipparchus.CalculusFieldElement;
26  import org.hipparchus.Field;
27  import org.hipparchus.analysis.solvers.BracketedRealFieldUnivariateSolver;
28  import org.hipparchus.analysis.solvers.FieldBracketingNthOrderBrentSolver;
29  import org.hipparchus.ode.FieldODEStateAndDerivative;
30  import org.hipparchus.ode.FieldOrdinaryDifferentialEquation;
31  import org.hipparchus.ode.events.AbstractFieldODEDetector;
32  import org.hipparchus.ode.events.Action;
33  import org.hipparchus.ode.events.FieldAdaptableInterval;
34  import org.hipparchus.ode.events.FieldODEEventDetector;
35  import org.hipparchus.ode.events.FieldODEEventHandler;
36  import org.hipparchus.util.MathArrays;
37  
38  
39  public class StepFieldProblem<T extends CalculusFieldElement<T>>
40      extends AbstractFieldODEDetector<StepFieldProblem<T>, T>
41      implements FieldOrdinaryDifferentialEquation<T> {
42  
43      private Field<T> field;
44      private T        rateBefore;
45      private T        rateAfter;
46      private T        rate;
47      private T        switchTime;
48  
49      public StepFieldProblem(Field<T> field,
50                              final FieldAdaptableInterval<T> maxCheck, final T threshold, final int maxIter,
51                              T rateBefore, T rateAfter, T switchTime) {
52          this(field, maxCheck, maxIter,
53               new FieldBracketingNthOrderBrentSolver<>(field.getZero(), threshold, field.getZero(), 5),
54               new LocalHandler<>(),
55               rateBefore, rateAfter, switchTime);
56      }
57  
58      private StepFieldProblem(Field<T> field,
59                               final FieldAdaptableInterval<T> maxCheck, final int maxIter,
60                               final BracketedRealFieldUnivariateSolver<T> solver,
61                               final FieldODEEventHandler<T> handler,
62                               final T rateBefore, final T rateAfter,
63                               final T switchTime) {
64          super(maxCheck, maxIter, solver, handler);
65          this.field      = field;
66          this.rateBefore = rateBefore;
67          this.rateAfter  = rateAfter;
68          this.switchTime = switchTime;
69          setRate(rateBefore);
70      }
71  
72      protected StepFieldProblem<T> create(FieldAdaptableInterval<T> newMaxCheck, int newMaxIter,
73                                           BracketedRealFieldUnivariateSolver<T> newSolver,
74                                           FieldODEEventHandler<T> newHandler) {
75          return new StepFieldProblem<>(field, newMaxCheck, newMaxIter, newSolver, newHandler,
76                                        rateBefore, rateAfter, switchTime);
77      }
78  
79      public T[] computeDerivatives(T t, T[] y) {
80          T[] yDot = MathArrays.buildArray(field, 1);
81          yDot[0] = rate;
82          return yDot;
83      }
84  
85      public int getDimension() {
86          return 1;
87      }
88  
89      public void setRate(T rate) {
90          this.rate = rate;
91      }
92  
93      public void init(T t0, T[] y0, T t) {
94      }
95  
96      public void init(FieldODEStateAndDerivative<T> state0, T t) {
97      }
98  
99      private static class LocalHandler<T extends CalculusFieldElement<T>> implements FieldODEEventHandler<T> {
100         public Action eventOccurred(FieldODEStateAndDerivative<T> state, FieldODEEventDetector<T> detector, boolean increasing) {
101             final StepFieldProblem<T> sp = (StepFieldProblem<T>) detector;
102             sp.setRate(sp.rateAfter);
103             return Action.RESET_DERIVATIVES;
104         }
105 
106     }
107 
108     public T g(FieldODEStateAndDerivative<T> state) {
109         return state.getTime().subtract(switchTime);
110     }
111 
112 }