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.analysis.UnivariateFunction;
26  import org.hipparchus.analysis.solvers.BracketedUnivariateSolver;
27  import org.hipparchus.analysis.solvers.BracketingNthOrderBrentSolver;
28  import org.hipparchus.ode.ODEStateAndDerivative;
29  import org.hipparchus.ode.OrdinaryDifferentialEquation;
30  import org.hipparchus.ode.events.AbstractODEDetector;
31  import org.hipparchus.ode.events.Action;
32  import org.hipparchus.ode.events.AdaptableInterval;
33  import org.hipparchus.ode.events.ODEEventDetector;
34  import org.hipparchus.ode.events.ODEEventHandler;
35  
36  
37  public class StepProblem extends AbstractODEDetector<StepProblem> implements OrdinaryDifferentialEquation {
38  
39      private double rateBefore;
40      private double rateAfter;
41      private double rate;
42      private double switchTime;
43  
44      public StepProblem(final AdaptableInterval maxCheck, final double threshold, final int maxIter,
45                         double rateBefore, double rateAfter, double switchTime) {
46          this(maxCheck, maxIter, new BracketingNthOrderBrentSolver(0, threshold, 0, 5),
47               new LocalHandler(), rateBefore, rateAfter, switchTime);
48      }
49  
50      private StepProblem(final AdaptableInterval maxCheck, final int maxIter,
51                          final BracketedUnivariateSolver<UnivariateFunction> solver,
52                          final ODEEventHandler handler,
53                          final double rateBefore, final double rateAfter,
54                          final double switchTime) {
55          super(maxCheck, maxIter, solver, handler);
56          this.rateBefore = rateBefore;
57          this.rateAfter  = rateAfter;
58          this.switchTime = switchTime;
59          setRate(rateBefore);
60      }
61  
62      protected StepProblem create(AdaptableInterval newMaxCheck, int newMaxIter,
63                                   BracketedUnivariateSolver<UnivariateFunction> newSolver,
64                                   ODEEventHandler newHandler) {
65          return new StepProblem(newMaxCheck, newMaxIter, newSolver, newHandler,
66                                 rateBefore, rateAfter, switchTime);
67      }
68  
69      public double[] computeDerivatives(double t, double[] y) {
70          return new double[] {
71              rate
72          };
73      }
74  
75      public int getDimension() {
76          return 1;
77      }
78  
79      public void setRate(double rate) {
80          this.rate = rate;
81      }
82  
83      public void init(double t0, double[] y0, double t) {
84      }
85  
86      private static class LocalHandler implements ODEEventHandler {
87          public Action eventOccurred(ODEStateAndDerivative state, ODEEventDetector detector, boolean increasing) {
88              final StepProblem sp = (StepProblem) detector;
89              sp.setRate(sp.rateAfter);
90              return Action.RESET_DERIVATIVES;
91          }
92      }
93  
94      public double g(ODEStateAndDerivative s) {
95          return s.getTime() - switchTime;
96      }
97  
98  }