/* Copyright Emiliano Cristiani 2011 I thank Simone Cacace for the function "SaveBitmap" This code computes the basins of attraction of some attractors 1,2,...,num_attr. Attractors attract particles following the Newton law (ma=F=Cm/r^2 + friction) The final solution is a matrix. Each entry is associated to a point of the plane and represents the number of the attractor the point is attracted to (-1 means that trajectory did not end on any attractor after N iterations). The matrix is converted in a bitmap image (colors should be defined by the user in the function "SaveBitmap") It is also possible to compute a single trajectory of the particle defining by hand the initial point and the initial velocity. In this case the solution is saved in the file "one_trajectory.txt" Set the parameters you prefer below! */ #include #include #include using namespace std; struct vettR2{ double x; double y; }; /*------------------FIRST SET OF PARAMETERS (see also below) ---------------*/ const int num_attr=4; //number of attractors const int pixel_x=200; //number of pixels in the horizontal direction const int pixel_y=200; //number of pixels in the vertical direction /*--------------------------------------------------------------------------*/ double C[num_attr], Dt, b, epsilon; vettR2 M[num_attr]; //prototypes void FIELD(double[], double[]); int SaveBitmap(const char*,int,int,short int**); ///////// MAIN ///////// int main (void){ short int **sol; bool go; int i,j,k,n,steps,close[num_attr]; long int N; double left,right,bottom,top,Dx,Dy,Rsquare,dsquare[num_attr]; double xold[5],xnew[5],vx,vy; double uapp[5],app1[5],app2[5],app3[5],app4[5]; FILE *pf; sol=new short int *[pixel_x]; for(i=0;isteps) {end=k; go=false;}} for (k=1;k<5;k++) {xold[k]=xnew[k];} fprintf(pf,"%lf %lf\n",xnew[1],xnew[2]); n++; } cout << "number of steps for the ODE solver= " << n << endl << "final attractor= #" << end << endl; fclose(pf); cout << "Trajectory saved in 'one_trajectory.txt'" << endl; } if (choice_main==1){ //regular computation //space steps Dx=(right-left)/(pixel_x-1.0); //cout << "Dx= " << Dx << endl; Dy=(top-bottom)/(pixel_y-1.0); //cout << "Dy= " << Dy << endl; //initialization of solution for (i=0;isteps) {sol[i][j]=k; go=false;}} for (k=1;k<5;k++) {xold[k]=xnew[k];} n++; } //cout << "n= " << n << endl; } { cout << "line " << i << "/" << pixel_x-1 << " complete" << endl; } } //write final solution on file "solution.txt" pf=fopen("solution.txt","w"); for (i=0;i=0; j--) // Bitmap format writes the image with a vertical flip { for(int i=0; i