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    *      http://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  package org.apache.commons.math4.legacy.ode.nonstiff;
19  
20  import org.apache.commons.math4.legacy.core.Field;
21  import org.apache.commons.math4.legacy.core.RealFieldElement;
22  import org.apache.commons.math4.legacy.ode.FirstOrderFieldDifferentialEquations;
23  import org.apache.commons.math4.legacy.ode.FieldODEState;
24  import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
25  import org.apache.commons.math4.legacy.ode.events.Action;
26  import org.apache.commons.math4.legacy.ode.events.FieldEventHandler;
27  import org.apache.commons.math4.legacy.core.MathArrays;
28  
29  
30  public class StepFieldProblem<T extends RealFieldElement<T>>
31      implements FirstOrderFieldDifferentialEquations<T>, FieldEventHandler<T> {
32  
33      public StepFieldProblem(Field<T> field, T rateBefore, T rateAfter, T switchTime) {
34          this.field      = field;
35          this.rateAfter  = rateAfter;
36          this.switchTime = switchTime;
37          setRate(rateBefore);
38      }
39  
40      @Override
41      public T[] computeDerivatives(T t, T[] y) {
42          T[] yDot = MathArrays.buildArray(field, 1);
43          yDot[0] = rate;
44          return yDot;
45      }
46  
47      @Override
48      public int getDimension() {
49          return 1;
50      }
51  
52      public void setRate(T rate) {
53          this.rate = rate;
54      }
55  
56      @Override
57      public void init(T t0, T[] y0, T t) {
58      }
59  
60      @Override
61      public void init(FieldODEStateAndDerivative<T> state0, T t) {
62      }
63  
64      @Override
65      public Action eventOccurred(FieldODEStateAndDerivative<T> state, boolean increasing) {
66          setRate(rateAfter);
67          return Action.RESET_DERIVATIVES;
68      }
69  
70      @Override
71      public T g(FieldODEStateAndDerivative<T> state) {
72          return state.getTime().subtract(switchTime);
73      }
74  
75      @Override
76      public FieldODEState<T> resetState(FieldODEStateAndDerivative<T> state) {
77          return state;
78      }
79  
80      private Field<T> field;
81      private T        rate;
82      private T        rateAfter;
83      private T        switchTime;
84  }