1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 }