int main() { long d1 = 3, d2 = 4; class foo { public: foo() {std::cout << "foo created" << std::endl; } ~foo() {std::cout << "foo deleted" << std::endl; } }; foo **f2; allocate2d(d1, d2, f2);// { dg-error "" "" { target { ! c++11 } } } ffree(d1, f2);// { dg-error "" "" { target { ! c++11 } } } }
int main(){ int ieq,imeas,iskp; int i,j,readval,parity,val; double norm; extern void neighbours(); extern void neighchk(); extern double ran(); extern void clusteven(); extern void clustodd(); extern void chkconf(); extern void measureMAB(); extern void measureflux(int); extern void constflux(); extern void initconf(); extern double **allocatedouble2d(int,int); extern void deallocatedouble2d(double**,int,int); extern int **allocate2d(int,int); extern void deallocate2d(int**,int,int); FILE *fptr; char st[20]; /* read file */ fptr=fopen("QUEUE","r"); if(fptr == NULL) {printf("QUEUE error.\n"); exit(1);} readval = fscanf(fptr,"%s %d\n",st,&LX); if(readval == -1) printf("Error\n"); readval = fscanf(fptr,"%s %d\n",st,&LY); if(readval == -1) printf("Error\n"); readval = fscanf(fptr,"%s %d\n",st,<2); if(readval == -1) printf("Error\n"); readval = fscanf(fptr,"%s %d\n",st,&SEED); if(readval == -1) printf("Error\n"); readval = fscanf(fptr,"%s %d\n",st,&ieq); if(readval == -1) printf("Error\n"); readval = fscanf(fptr,"%s %d\n",st,&imeas); if(readval == -1) printf("Error\n"); readval = fscanf(fptr,"%s %lf\n",st,&beta); if(readval == -1) printf("Error\n"); readval = fscanf(fptr,"%s %lf\n",st,&J); if(readval == -1) printf("Error\n"); readval = fscanf(fptr,"%s %lf\n",st,&lam); if(readval == -1) printf("Error\n"); fclose(fptr); printf("Multi-Cluster Algorithm for the U(1) quantum link model\n"); printf("Nx=%d, Ny=%d, Nt=%d\n",LX,LY,LT2); printf("beta=%2.4f; J=%2.3f; lam=%2.3f\n",beta,J,lam); printf("Starting seed=%d\n",SEED); LT = 2*LT2; /* the dof are spread over twice actual length */ VOL = LX*LY*LT; SVOL= LX*LY/2; SPV = 2*SVOL; SIZE= 2*SVOL+1; VOL2= VOL/2; VOL4= VOL/4; minMA = minMB = -LX*LY/2; maxMA = maxMB = LX*LY/2; /* allocate memory */ ixc = (int *)malloc(VOL*sizeof(int)); iyc = (int *)malloc(VOL*sizeof(int)); itc = (int *)malloc(VOL*sizeof(int)); ising = (int *)malloc(VOL*sizeof(int)); list = (int *)malloc(VOL*sizeof(int)); avflx1= (double *)malloc(SPV*sizeof(double)); avfly1= (double *)malloc(SPV*sizeof(double)); avflx2= (double *)malloc(SPV*sizeof(double)); avfly2= (double *)malloc(SPV*sizeof(double)); chptr = (int *)malloc(VOL2*sizeof(double)); backt = (int *)malloc(LT*sizeof(double)); /* MA and MB will be measured for each of the LT/2 timeslices and avg-d */ MA = (int *)malloc((LT2)*sizeof(int)); MB = (int *)malloc((LT2)*sizeof(int)); pMAB = allocatedouble2d(SIZE,SIZE); refC = allocate2d(LX,LY); fx = allocate2d(LT,SPV); fy = allocate2d(LT,SPV); for(i=0;i<NNBR;i++) neigh[i] = (int *)malloc(VOL*sizeof(int)); for(i=0;i<2*DIM+1;i++) next[i] = (int *)malloc(VOL*sizeof(int)); /* Set parameters */ eps=1.0*beta/((double)LT); iskp=1; /* Initialize ranlux */ rlxd_init(1,SEED); /* initialize neighbours */ neighbours(); //neighchk(); /* Define the probabilities */ double x,coshx,sinhx; x = eps*J; coshx = (exp(x)+exp(-x))/2.0; sinhx = (exp(x)-exp(-x))/2.0; p1 = exp(-x)/coshx; p2 = 1.0 - exp(eps*lam)/coshx; printf("eps = %f\n",eps); printf("Prob p1: %f; Prob p2: %f\n",p1,p2); initconf(); chkconf(); /* initialize back-t pointers */ for(i=0;i<LT;i++) backt[i]=(i-1+LT)%LT; /* initialize the reference configuration */ for(i=0;i<LX;i++) for(j=0;j<LY;j++){ parity=(i+j)%2; val=(i-j)%4; if(parity==0){ /* for the even-time slices */ if(val==0) refC[i][j]=1; else refC[i][j]=-1; } else{ /* for the odd-time slices */ if((val==-1)||(val==3)) refC[i][j]=-1; else refC[i][j]=1; } } /* initialize average flux variable */ for(i=0;i<SPV;i++) avflx1[i]=avfly1[i]=avflx2[i]=avflx2[i]=0.0; /* update */ thermflag=1; for(i=0;i<ieq;i++){ nclusevn = 0; nclusevsq=0; mA=0; nclusodd = 0; nclusodsq=0; mB=0; clusteven(); clustodd(); chkconf(); } thermflag=0; /* measure */ flxcnt1=flxcnt2=0; for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++) pMAB[i][j]=0.0; fptr=fopen("multi.dat","w"); for(i=0;i<imeas;i++){ nclusevn = 0; nclusevsq=0; mA=0; nclusodd = 0; nclusodsq=0; mB=0; clusteven(); nclusevsq = nclusevsq/VOL4; clustodd(); nclusodsq = nclusodsq/VOL4; nclus = nclusevn + nclusodd; measureMAB(); //measureflux(); constflux(); chkconf(); fprintf(fptr,"%d %d %d %lf %lf %d %d\n",nclusevn,nclusodd,nclus,nclusevsq,nclusodsq,mA,mB); //fprintf(fptr,"%e %e %e %e %d %d\n",(double)mA,(double)mB,flx,fly,flt1,flt2); } fclose(fptr); /* normalize and print histogram */ norm=0.0; for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++) norm += pMAB[i][j]; fptr=fopen("magdist.dat","w"); for(i=0;i<SIZE;i++){ for(j=0;j<SIZE;j++){ pMAB[i][j] /= norm; fprintf(fptr,"%d %d %le\n",i,j,pMAB[i][j]);} fprintf(fptr,"\n");} fclose(fptr); /* average and normalize the flux profile and print it */ fptr=fopen("fprof.dat","w"); printf("# meas: %d + %d = %d\n",flxcnt1,flxcnt2,flxcnt1+flxcnt2); for(i=0;i<SPV;i++){ avflx1[i] /= (2.0*LT2*flxcnt1); avfly1[i] /= (2.0*LT2*flxcnt1); avflx2[i] /= (2.0*LT2*flxcnt2); avfly2[i] /= (2.0*LT2*flxcnt2); fprintf(fptr,"%d %.5f %.5f %.5f %.5f\n",i,avflx1[i],avfly1[i],avflx2[i],avfly2[i]); } fclose(fptr); /* free memory */ free(ixc); free(iyc); free(itc); free(MA); free(MB); free(avflx1); free(avfly1); free(avflx2); free(avfly2); free(chptr); free(backt); for(i=0;i<NNBR;i++) free(neigh[i]); for(i=0;i<2*DIM+1;i++) free(next[i]); deallocatedouble2d(pMAB,SIZE,SIZE); deallocate2d(refC,LX,LY); deallocate2d(fx,LT,SPV); deallocate2d(fy,LT,SPV); return 0; }
void rungeKutta4( FLOAT **rm, FLOAT **vm, FLOAT **rM, FLOAT **vM, FLOAT *M, FLOAT dt, int n_masses, int n_Masses ){ // Crea los vectores de aceleracion, posicion y velodidad temporales FLOAT **newRm; FLOAT **newRM; // Crea el vector k de constantes intermedias del método RungeKutta FLOAT ***krm; FLOAT ***krM; FLOAT ***kvm; FLOAT ***kvM; // Aparta memoria newRm = allocate2d( n_masses, n_dim ); newRM = allocate2d( n_Masses, n_dim ); krm = allocate3d( n_masses, n_dim, 4 ); krM = allocate3d( n_Masses, n_dim, 4 ); kvm = allocate3d( n_masses, n_dim, 4 ); kvM = allocate3d( n_Masses, n_dim, 4 ); //******************************************************************** //-------------------------------------------------------------------- // Cuerpo del RungeKutta //-------------------------------------------------------------------- //******************************************************************** int i; /* * Rungekutta 1st step */ for( i = 0; i < n_Masses; i++ ){ // RungeKutta 1st step for n_Masses krM[0][0][i] = vM[0][i]; krM[0][1][i] = vM[1][i]; } for( i = 0; i < n_masses; i++ ){ // RungeKutta 1st step for n_masses krm[0][0][i] = vm[0][i]; krm[0][1][i] = vm[1][i]; } // Actualiza las aceleraciones para el primer paso de RungeKutta updateAcc( rm, rM, kvm[0], kvM[0], M, n_masses, n_Masses ); /* * Rungekutta 2nd step */ for( i = 0; i < n_Masses; i++ ){ // RungeKutta 2nd step for n_Masses newRM[0][i] = rM[0][i] + krM[0][0][i]*dt/2 ; newRM[1][i] = rM[1][i] + krM[0][1][i]*dt/2 ; krM[1][0][i] = vM[0][i] + kvM[0][0][i]*dt/2 ; krM[1][1][i] = vM[1][i] + kvM[0][1][i]*dt/2 ; } for( i = 0; i < n_masses; i++ ){ // RungeKutta 2nd step for n_masses newRm[0][i] = rm[0][i] + krm[0][0][i]*dt/2 ; newRm[1][i] = rm[1][i] + krm[0][1][i]*dt/2 ; krm[1][0][i] = vm[0][i] + kvm[0][0][i]*dt/2 ; krm[1][1][i] = vm[1][i] + kvm[0][1][i]*dt/2 ; } // Actualiza las aceleraciones para el segundo paso de RungeKutta updateAcc( newRm, newRM, kvm[1], kvM[1], M, n_masses, n_Masses ); /* * Rungekutta 3rd step */ for( i = 0; i < n_Masses; i++ ){ // RungeKutta 3rd step for n_Masses newRM[0][i] = rM[0][i] + krM[1][0][i]*dt/2 ; newRM[1][i] = rM[1][i] + krM[1][1][i]*dt/2 ; krM[2][0][i] = vM[0][i] + kvM[1][0][i]*dt/2 ; krM[2][1][i] = vM[1][i] + kvM[1][1][i]*dt/2 ; } for( i = 0; i < n_masses; i++ ){ // RungeKutta 3rd step for n_masses newRm[0][i] = rm[0][i] + krm[1][0][i]*dt/2 ; newRm[1][i] = rm[1][i] + krm[1][1][i]*dt/2 ; krm[2][0][i] = vm[0][i] + kvm[1][0][i]*dt/2 ; krm[2][1][i] = vm[1][i] + kvm[1][1][i]*dt/2 ; } // Actualiza las aceleraciones para el segundo paso de RungeKutta updateAcc( newRm, newRM, kvm[2], kvM[2], M, n_masses, n_Masses ); /* * Rungekutta 4th step */ for( i = 0; i < n_Masses; i++ ){ // RungeKutta 4th step for n_Masses newRM[0][i] = rM[0][i] + krM[2][0][i]*dt ; newRM[1][i] = rM[1][i] + krM[2][1][i]*dt ; krM[3][0][i] = vM[0][i] + kvM[2][0][i]*dt ; krM[3][1][i] = vM[1][i] + kvM[2][1][i]*dt ; } for( i = 0; i < n_masses; i++ ){ // RungeKutta 4th step for n_masses newRm[0][i] = rm[0][i] + krm[2][0][i]*dt ; newRm[1][i] = rm[1][i] + krm[2][1][i]*dt ; krm[3][0][i] = vm[0][i] + kvm[2][0][i]*dt ; krm[3][1][i] = vm[1][i] + kvm[2][1][i]*dt ; } // Actualiza las aceleraciones para el segundo paso de RungeKutta updateAcc( newRm, newRM, kvm[3], kvM[3], M, n_masses, n_Masses ); //----------------------------------------------------------------- //***************************************************************** // Promedia los k y actualiza los vectores de entrada en i+1 for( i = 0; i < n_Masses; i++ ){ // Promedia los ks de n_Masses // Actualiza los vectores de entrada FLOAT kmean = (1/6.0)*(krM[0][0][i] + 2*krM[1][0][i] + 2*krM[2][0][i] + krM[3][0][i]); rM[0][i] = rM[0][i] + kmean*dt; kmean = (1/6.0)*(krM[0][1][i] + 2*krM[1][1][i] + 2*krM[2][1][i] + krM[3][1][i]); rM[1][i] = rM[1][i] + kmean*dt; kmean = (1/6.0)*(kvM[0][0][i] + 2*kvM[1][0][i] + 2*kvM[2][0][i] + kvM[3][0][i]); vM[0][i] = vM[0][i] + kmean*dt; kmean = (1/6.0)*(kvM[0][1][i] + 2*kvM[1][1][i] + 2*kvM[2][1][i] + kvM[3][1][i]); vM[1][i] = vM[1][i] + kmean*dt; } for( i = 0; i < n_masses; i++ ){ // Promedia los ks de n_masses // Actualiza los vectores de entrada FLOAT kmean = (1/6.0)*(krm[0][0][i] + 2*krm[1][0][i] + 2*krm[2][0][i] + krm[3][0][i]); rm[0][i] = rm[0][i] + kmean*dt; kmean = (1/6.0)*(krm[0][1][i] + 2*krm[1][1][i] + 2*krm[2][1][i] + krm[3][1][i]); rm[1][i] = rm[1][i] + kmean*dt; kmean = (1/6.0)*(kvm[0][0][i] + 2*kvm[1][0][i] + 2*kvm[2][0][i] + kvm[3][0][i]); vm[0][i] = vm[0][i] + kmean*dt; kmean = (1/6.0)*(kvm[0][1][i] + 2*kvm[1][1][i] + 2*kvm[2][1][i] + kvm[3][1][i]); vm[1][i] = vm[1][i] + kmean*dt; } // Libera memoria int q,w; for(q = 0; q < 4; q++){ for( w = 0; w < 2; w++){ free(krm[q][w]); free(kvm[q][w]); } free(krm[q]); free(kvm[q]); } for( w = 0; w < 2; w++){ free(newRM[w]); } for( w = 0; w < 2; w++){ free(newRm[w]); } for(q = 0; q < 4; q++){ for( w = 0; w < 2; w++){ free(krM[q][w]); free(kvM[q][w]); } free(krM[q]); free(kvM[q]); } free(krm); free(krM); free(kvm); free(kvM); }
int main( int argc, char **argv){ /* * Define las variables */ // El puntero que contiene los punteros de posicion para cada masa pequeña en cada instante de tiempo FLOAT **rm; // El puntero que contiene los punteros de velocidad para cada masa pequeña en cada instante de tiempo FLOAT **vm; // El puntero que contiene los punteros de posicion para cada masa Grande en cada instante de tiempo FLOAT **rM; // El puntero que contiene los punteros de velocidad para cada masa Grande en cada instante de tiempo FLOAT **vM; // EL archivo donde se guardaron las condiciones iniciales FILE *data; // Los archivos donde se guardara la evolucion FILE *data_evolve; FILE *data_vel; // El archivo donde se guardaran los ids FILE *data_id; // El numero de masas pequeñas int n_masses = 0; // El numero de masas grandes int n_Masses = 0; // El vector de masas grandes FLOAT *M; // El tiempo total de integracion (en años) FLOAT T; // El numero de iteraciones que se quiere sobre ese tiempo int N; // El nombre del archivo char *name; // Variable de iteracion int i; /* * Inicializa las variables que pueden ser inicializadas */ N = atoi( argv[1] ); T = atof( argv[2] ); // El intervalo de tiempo sobre integracion FLOAT dt = T/N; name = argv[3]; // El archivo de texto de condiciones iniciales y de evolucion en el tiempo data = fopen( name, "r" ); data_evolve = fopen( "evolve_c.dat", "w" ); data_id = fopen( "id.dat", "w" ); data_vel = fopen( "evolve_vel.dat", "w" ); //------------------------------------------------------------------------------------------------------------------- //******************************************************************************************************************* /* * Lee el archivo y determina el numero de masas pequeñas y el numero de masas grandes (para poder apartar memoria) */ FLOAT tmp1; int id; int test = fscanf(data, "%d %le %le %le %le %le\n", &id, &tmp1, &tmp1, &tmp1, &tmp1, &tmp1); if( id < 0 ){ n_Masses++; }else{ n_masses++; } do{ test = fscanf(data, "%d %le %le %le %le %le\n", &id, &tmp1, &tmp1, &tmp1, &tmp1, &tmp1); if( id < 0 ){ n_Masses++; }else{ n_masses++; } }while( test!=EOF ); // Cierra el archivo y lo vuelve a abrir fclose(data); fopen(name,"r"); //******************************************************************************************************************* //------------------------------------------------------------------------------------------------------------------- /* * Aparta el espacio en memoria de los punteros */ M = malloc( n_Masses*sizeof(FLOAT) ); rm = allocate2d( n_masses, n_dim ); rM = allocate2d( n_Masses, n_dim ); vm = allocate2d( n_masses, n_dim ); vM = allocate2d( n_Masses, n_dim ); //------------------------------------------------------------------------------------------------------------------- //******************************************************************************************************************* /* * Lee el archivo de nuevo y asigna los valores iniciales de posicion y velocidad, ademas del vector de masas */ FLOAT *Temp = malloc( 5*sizeof(FLOAT)); int index = 0; int Index = 0; test = fscanf(data, "%d %le %le %le %le %le\n", &id, &Temp[0], &Temp[1], &Temp[2], &Temp[3], &Temp[4]); if( id < 0 ){ fprintf( data_id, "%d\n", id ); rM[0][Index] = Temp[0]; rM[1][Index] = Temp[1]; vM[0][Index] = Temp[2]; vM[1][Index] = Temp[3]; M[Index] = Temp[4]; Index++; }else{ fprintf( data_id, "%d\n", id ); rm[0][index] = Temp[0]; rm[1][index] = Temp[1]; vm[0][index] = Temp[2]; vm[1][index] = Temp[3]; index++; } do{ test = fscanf(data, "%d %le %le %le %le %le\n", &id, &Temp[0], &Temp[1], &Temp[2], &Temp[3], &Temp[4]); if( id < 0 ){ fprintf( data_id, "%d\n", id ); rM[0][Index] = Temp[0]; rM[1][Index] = Temp[1]; vM[0][Index] = Temp[2]; vM[1][Index] = Temp[3]; M[Index] = Temp[4]; Index++; }else{ fprintf( data_id, "%d\n", id ); rm[0][index] = Temp[0]; rm[1][index] = Temp[1]; vm[0][index] = Temp[2]; vm[1][index] = Temp[3]; index++; } }while( test!=EOF ); // Cierra el archivo free(Temp); fclose(data); //******************************************************************************************************************* //------------------------------------------------------------------------------------------------------------------- /* * Imprime la evolucion de los parametros en los archivos * Formato: x y z vx vy vz */ //printf("M: %d\nm: %d\nM: %le\n", n_Masses, n_masses, M[0]); for( i = 0; i < n_Masses; i++ ){ // Escribe las posiciones y velocidades de n_masses fprintf( data_evolve, "%le %le ", rM[0][i], rM[1][i] ); fprintf( data_vel, "%le %le ", vM[0][i], vM[1][i] ); } for( i = 0; i < n_masses - 1; i++ ){ // Escribe las posiciones y velocidades de n_Mmasses fprintf( data_evolve, "%le %le ", rm[0][i], rm[1][i] ); fprintf( data_vel, "%le %le ", vm[0][i], vm[1][i] ); } i = n_masses-1; fprintf( data_evolve, "%le %le\n", rm[0][i], rm[1][i] ); fprintf( data_vel, "%le %le\n", vm[0][i], vm[1][i] ); /* * Método Runge-Kutta 4to orden sobre el equiespaciado temporal */ int j; for( j = 0; j < N; j++ ){ rungeKutta4( rm, vm, rM, vM, M, dt, n_masses, n_Masses ); /* * Imprime la evolucion de los parametros en los archivos * Formato: x y z vx vy vz */ for( i = 0; i < n_Masses; i++ ){ // Escribe las posiciones y velocidades de n_masses fprintf( data_evolve, "%le %le ", rM[0][i], rM[1][i] ); fprintf( data_vel, "%le %le ", vM[0][i], vM[1][i] ); } for( i = 0; i < n_masses - 1; i++ ){ // Escribe las posiciones y velocidades de n_Mmasses fprintf( data_evolve, "%le %le ", rm[0][i], rm[1][i] ); fprintf( data_vel, "%le %le ", vm[0][i], vm[1][i] ); } i = n_masses-1; fprintf( data_evolve, "%le %le\n", rm[0][i], rm[1][i] ); fprintf( data_vel, "%le %le\n", vm[0][i], vm[1][i] ); } fclose(data_evolve); fclose(data_vel); fclose(data_id); free(M); free(rm); free(rM); free(vm); free(vM); return 0; }