001
002 /**
003 * Integrator is an abstract class which can be subclassed to create
004 * any kind of numerical integrator. All calculations are performed
005 * using Points enabling any dimension of problem to be solved without
006 * rewriting either this superclass or any its subclasses.*/
007
008 public abstract class Integrator implements Runnable {
009
010 /**
011 * Initial value of the independent variable that the integration
012 * is begin performed with respect to.*/
013 protected final double x0;
014
015 /**
016 * Final value of the independent variable that the integration
017 * is begin performed with respect to.*/
018 protected double x1;
019
020 /**
021 * Current value of the independent variable that the integration
022 * is begin performed with respect to.*/
023 protected double x;
024
025 /**
026 * Initial state of the system being integrated.*/
027 protected final Point y0;
028
029 /**
030 * Current state of the system being integrated.*/
031 protected Point y;
032
033 /**
034 * The number of steps being used for the integration.*/
035 protected int steps;
036
037
038
039
040
041 /**
042 * Constructor to instantiate Integrator - never called directly.*/
043 public Integrator(double xStart, double xEnd, Point yStart, int steps) {
044 x0 = xStart;
045 x1 = xEnd;
046 y0 = yStart;
047 y = y0;
048 this.steps = steps;
049 }
050
051
052
053
054
055
056 /**
057 * Abstract method which performs the numerical integration,
058 * implemented by any subclass of Integrator.*/
059 public abstract Point integrate();
060
061 /**
062 * Method to calculate the required function which defines the
063 * problem being integrated. This is calculated with respect to
064 * the independent variable x and perhaps wrt the Point y but this
065 * depends on the problem, e.g. an equation of motion integration
066 * would require the derivative of the Point y wrt x. This is
067 * abstract and must be implemented by the instance subclass of
068 * Integrator.*/
069 public abstract Point function(double x,Point y);
070
071 /**
072 * Abstract method to return the problem being integrated in human
073 * readable form.*/
074 public abstract String toString();
075
076
077
078
079
080
081 /**
082 * Method used to return the current state of the system. Used by
083 * the Display class in package compsim3.display.*/
084 public Point getLocation(){return y;}
085
086
087
088
089
090 /**
091 * Method used to return the current value of the independent
092 * variable. Used by the Display class in package
093 * compsim3.display.*/
094 public double getIV() {return x;}
095
096
097 /**
098 * Method used to reset the number of steps used in the numerical
099 * integration.*/
100 public void setSteps(int steps) {
101 this.steps = steps;
102 }
103
104 /**
105 * Method used to reset the final value of the independent
106 * variable used in the numerical integration.*/
107 public void setEnd(double x1) {this.x1 = x1;}
108
109 /**
110 * Method used to start a new thread performing the integration.
111 * used by the display class in package compsim3.display.*/
112 public void run() {integrate();}
113 }