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.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 }