001            
002            import java.io.*;
003            /**
004            * SolarIntegrator is a subclass of the abstract RK4 class.
005            * It implements a function method which represents the mathematical  
006            * representation of my simplified solar system. (about 90 coupled
007            * differential equations :)
008            * @author Markus Signitzer
009            */
010            public class SolarIntegrator extends RK4{ 
011                    
012                    // declare variables
013                    /** Instance variable G representing the value of the physical 
014                    * constant G (Newtonian Gravity) in SI units.*/
015                    double G = 6.6742*Math.pow(10,-11);
016                    /** Instance variable numberOfObjects storing the length of the objects[] array,
017                    * (i.e. the total number of objects in my Solar System)*/
018                    int numberOfObjects;
019                    /** Instance array objects[] to hold the objects of type SpaceObject,
020                    * which are present in my Solar System. */
021                    SpaceObject[] objects;
022                    
023                    
024                    // constructor
025                    // startPoint has to be a huge list where the first three entries are the sun coordinates,
026                    // the next three the sun velocities, and then it is repeating this patter with all planets,
027                    // following the order: sun -> outwards until Neptun.
028                    /**
029                    * Constructs a <code>SolarIntegrator</code> for performing numerical integration.
030                    *
031                    * @param o arry of SpaceObjects in the system.
032                    * @param t0 start time of integration (in seconds as I only use SI units).
033                    * @param t1 stop time of integration.
034                    * @param startPoint has to be a huge list where the first three entries are the sun coordinates,
035                    *        the next three the sun velocities, and then it is repeating this patter with all planets,
036                    *        following the order: sun -> outwards until Neptun.
037                    * @param steps number of integration steps between start and stop time
038                    */
039                    public SolarIntegrator (SpaceObject[] o, double t0, double t1, PointN startPoint, int steps){
040                            super(t0, t1, startPoint, steps); 
041                            objects = o;
042                            numberOfObjects = o.length;
043                            
044                            
045                    }
046                    
047                    
048                    /**
049                    * Method for numerical integration.
050                    * @return a <code>Point</code> object which contains the new (after the integration)
051                    *         position and velocities of all space objects. Starting with the sun's positions 
052                    *         and velocities, then mercury's and so on until neptuns positions and velocities.
053                    */
054                    public Point integrate(){
055                            Point result = null;
056                            result = super.integrate();
057                            return result;
058                    }
059                    
060                    /**
061                    * represents the mathematical behaviour of my simplified solar system. 
062                    * ~about 90 coupled differential equations, but which is actually just
063                    * calculating the new velocities and accelerations of each <code>SpaceObject</code>.
064                    * (i.e. differentiating with respect to time)
065                    * @param time a time step.
066                    * @param n a <code>Point</code> object holding current positions and velocities.
067                    * @return a <code>Point</code> object which contains the velocieties and accelerations
068                    *         of all space objects. Starting with the sun's velocities and accelerations, then 
069                    *         mercury's and so on until neptun's.
070                    */
071                    public Point function(double time, Point n) {  
072                            // n holds all planets/objects postion and velocities in such a way that 
073                            // always 6 entries correspond to one object, look below to see what i mean.
074                            /*
075                            double x = n.coordinate(0);
076                            double y = n.coordinate(1);
077                            double z = n.coordinate(2);
078                            double vx = n.coordinate(3);
079                            double vy = n.coordinate(4);
080                            double vz = n.coordinate(5);
081                            */
082                            
083                            // get the length of n and divide by 6 to see
084                            // how many objects there are.
085                            int nr = (n.dimension())/6;
086                            if (nr != numberOfObjects)
087                                    System.out.println("there is something wrong! Line 47 in SolarIntegrator");
088                            
089                            // this will be the resultArray, which will be changed to a point and then returned
090                            double[] resultArray = new double[n.dimension()];
091                            // this is just the sun -> it will always be at center and have zero velocity
092                            for (int k = 0; k < 6; k++){
093                                    resultArray[k] = 0.0;
094                            }
095                            
096                            
097                            // now need to loop throw all Planet objects except sun:
098                            for (int j = 1; j < nr; j++){
099                                    double x = n.coordinate(j*6+0);
100                                    double y = n.coordinate(j*6+1);
101                                    double z = n.coordinate(j*6+2);
102                                    double vx = n.coordinate(j*6+3);
103                                    double vy = n.coordinate(j*6+4);
104                                    double vz = n.coordinate(j*6+5);
105                                    
106                                    double axNew = 0.0;
107                                    double ayNew = 0.0;
108                                    double azNew = 0.0;
109                                    
110                                    for(int i = 0; i < numberOfObjects; i++){
111                                            if (i != j){
112                                                    SpaceObject star = (SpaceObject) objects[i];
113                                                    PointN pos = star.getC();
114                                                    double mass = star.getM();
115                                                    double dist = getDistance(star, x, y, z);
116                                                    
117                                                    // need to use point method, not access individual
118                                                    // coordinates
119                                                    // calc new x-acceleration
120                                                    double X = (pos.coordinate(0)-x); 
121                                                    axNew = axNew + (mass*G*X/(Math.pow(dist, 3)));
122                                                    // calc new y-acceleration
123                                                    double Y = (pos.coordinate(1)-y); 
124                                                    ayNew = ayNew + (mass*G*Y/(Math.pow(dist, 3)));
125                                                    // calc new z-acceleration
126                                                    double Z = (pos.coordinate(2)-z); 
127                                                    azNew = azNew + (mass*G*Z/(Math.pow(dist, 3)));
128                                            }
129                                    }
130                                    
131                                    // create a point which holds all the (vx,vx,vz,ax,ay,az) and return it to the integrator
132                                    
133                                    resultArray[j*6+0] = vx;
134                                    resultArray[j*6+1] = vy;
135                                    resultArray[j*6+2] = vz;
136                                    resultArray[j*6+3] = axNew;
137                                    resultArray[j*6+4] = ayNew;
138                                    resultArray[j*6+5] = azNew;
139                            }
140                            PointN result = new PointN(resultArray);
141                            return result;
142                    }
143                    
144                    
145                    
146                    
147                    
148                    
149                    
150                    // little method to calc the distance between the current object and an other space object
151                    /**
152                    * calculates the distance of a <code>SpaceObject</code> s from a certain position in 3D.
153                    * @param s a <code>SpaceObject</code>.
154                    * @param x cartesian x coordinate of a point in 3D.
155                    * @param y cartesian y coordinate of a point in 3D.
156                    * @param z cartesian z coordinate of a point in 3D.
157                    * @return the distace beetween the <code>SpaceObject</code> s and the point in 3D.
158                    */
159                    public double getDistance(SpaceObject s, double x, double y, double z){
160                            PointN pos = s.getC();
161                            double sx = pos.coordinate(0);
162                            double sy = pos.coordinate(1);
163                            double sz = pos.coordinate(2);
164                            double xx = sx-x;
165                            double yy = sy-y;
166                            double zz = sz-z;
167                            return Math.sqrt((Math.pow(xx, 2) + Math.pow(yy, 2) + Math.pow(zz, 2)));
168                    }
169                    /**
170                    * This method is NOT USED!!!
171                    * only implementet because the abstract super class RK4 has it and there fore 
172                    * it is necessary to implement it -> otherwise compiler error!.
173                    * @return a empty String.
174                    */
175                    public String toString(){
176                            return " ";
177                    }
178            }
179