1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.ode;
19
20 import org.apache.commons.math.ode.events.EventHandler;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class TestProblem4
38 extends TestProblemAbstract {
39
40
41 private static final long serialVersionUID = -5910438521889015745L;
42
43
44 private double a;
45
46
47 private double[] y;
48
49
50 public TestProblem4() {
51 super();
52 a = 1.2;
53 double[] y0 = { Math.sin(a), Math.cos(a) };
54 setInitialConditions(0.0, y0);
55 setFinalConditions(15);
56 double[] errorScale = { 1.0, 0.0 };
57 setErrorScale(errorScale);
58 y = new double[y0.length];
59 }
60
61
62
63
64
65 public TestProblem4(TestProblem4 problem) {
66 super(problem);
67 a = problem.a;
68 y = problem.y.clone();
69 }
70
71
72 public TestProblem4 copy() {
73 return new TestProblem4(this);
74 }
75
76 @Override
77 public EventHandler[] getEventsHandlers() {
78 return new EventHandler[] { new Bounce(), new Stop() };
79 }
80
81 @Override
82 public void doComputeDerivatives(double t, double[] y, double[] yDot) {
83 yDot[0] = y[1];
84 yDot[1] = -y[0];
85 }
86
87 @Override
88 public double[] computeTheoreticalState(double t) {
89 double sin = Math.sin(t + a);
90 double cos = Math.cos(t + a);
91 y[0] = Math.abs(sin);
92 y[1] = (sin >= 0) ? cos : -cos;
93 return y;
94 }
95
96 private static class Bounce implements EventHandler {
97
98 private static final long serialVersionUID = 1356097180027801200L;
99 private int sign;
100
101 public Bounce() {
102 sign = +1;
103 }
104
105 public double g(double t, double[] y) {
106 return sign * y[0];
107 }
108
109 public int eventOccurred(double t, double[] y, boolean increasing) {
110
111 sign = -sign;
112 return EventHandler.RESET_STATE;
113 }
114
115 public void resetState(double t, double[] y) {
116 y[0] = -y[0];
117 y[1] = -y[1];
118 }
119
120 }
121
122 private static class Stop implements EventHandler {
123
124 private static final long serialVersionUID = 6975050568227951931L;
125
126 public Stop() {
127 }
128
129 public double g(double t, double[] y) {
130 return t - 12.0;
131 }
132
133 public int eventOccurred(double t, double[] y, boolean increasing) {
134 return EventHandler.STOP;
135 }
136
137 public void resetState(double t, double[] y) {
138 }
139
140 }
141
142 }