int FFT2D(COMPLEX c[][32],int nx,int ny,int dir) { int i,j; int m,twopm; /* Transform the rows */ if (realx == 0) { realx = (double *)malloc(nx * sizeof(double)); imagx = (double *)malloc(nx * sizeof(double)); realy = (double *)malloc(ny * sizeof(double)); imagy = (double *)malloc(ny * sizeof(double)); } //if (real == NULL || imag == NULL) // return(FALSE); if (!Powerof2(nx,&m,&twopm) || twopm != nx) return(FALSE); for (j=0;j<ny;j++) { for (i=0;i<nx;i++) { realx[i] = c[i][j].real; imagx[i] = c[i][j].imag; } FFT(dir,m,realx,imagx); for (i=0;i<nx;i++) { c[i][j].real = realx[i]; c[i][j].imag = imagx[i]; } } //free(real); //free(imag); /* Transform the columns */ //real = (double *)malloc(ny * sizeof(double)); //imag = (double *)malloc(ny * sizeof(double)); //if (real == NULL || imag == NULL) // return(FALSE); if (!Powerof2(ny,&m,&twopm) || twopm != ny) return(FALSE); for (i=0;i<nx;i++) { for (j=0;j<ny;j++) { realy[j] = c[i][j].real; imagy[j] = c[i][j].imag; } FFT(dir,m,realy,imagy); for (j=0;j<ny;j++) { c[i][j].real = realy[j]; c[i][j].imag = imagy[j]; } } //free(real); //free(imag); return(TRUE); }
int FFT2D(COMPLEX *c,int nx,int ny,int dir) { int i,j; int m,twopm; //double* real = new double[nx]; double* real = malloc(nx*sizeof(double)); //double* imag = new double[nx]; double* imag = malloc(nx*sizeof(double)); if (!Powerof2(nx,&m,&twopm) || twopm != nx) return(0); for (j=0;j<ny;j++) { for (i=0;i<nx;i++) { real[i] = c[j*nx+i].real; imag[i] = c[j*nx+i].imag; } FFT(real,imag,m,dir); for (i=0;i<nx;i++) { c[j*nx+i].real = real[i]; c[j*nx+i].imag = imag[i]; } } //delete[] real; free(real); //delete[] imag; free(imag); //real = new double[ny]; real = malloc(ny*sizeof(double)); //imag = new double[ny]; imag = malloc(ny*sizeof(double)); if (!Powerof2(ny,&m,&twopm) || twopm != ny) return(0); for (i=0;i<nx;i++) { for (j=0;j<ny;j++) { real[j] = c[j*nx+i].real; imag[j] = c[j*nx+i].imag; } FFT(real,imag,m,dir); for (j=0;j<ny;j++) { c[j*nx+i].real = real[j]; c[j*nx+i].imag = imag[j]; } } //delete[] real; free(real); //delete[] imag; free(imag); return(1); }
//int FFT2D(COMPLEX **c,int nx,int ny,int dir) int FFT2D(Complex c[WAVE_SIZE][WAVE_SIZE], int nx, int ny, int dir) { int i, j; int m, twopm; double *real, *imag; /* Transform the rows */ real = (double *)malloc(nx * sizeof(double)); imag = (double *)malloc(nx * sizeof(double)); if (real == NULL || imag == NULL) return 0; if (!Powerof2(nx, &m, &twopm) || twopm != nx) return 0; for (j = 0; j<ny; j++) { for (i = 0; i<nx; i++) { real[i] = c[i][j].real; imag[i] = c[i][j].imag; } FFT(dir, m, real, imag); for (i = 0; i<nx; i++) { c[i][j].real = real[i]; c[i][j].imag = imag[i]; } } free(real); free(imag); /* Transform the columns */ real = (double *)malloc(ny * sizeof(double)); imag = (double *)malloc(ny * sizeof(double)); if (real == NULL || imag == NULL) return 0; if (!Powerof2(ny, &m, &twopm) || twopm != ny) return 0; for (i = 0; i<nx; i++) { for (j = 0; j<ny; j++) { real[j] = c[i][j].real; imag[j] = c[i][j].imag; } FFT(dir, m, real, imag); for (j = 0; j<ny; j++) { c[i][j].real = real[j]; c[i][j].imag = imag[j]; } } free(real); free(imag); return 1; }
/*------------------------------------------------------------------------- Perform a 2D FFT inplace given a complex 2D array The direction dir, 1 for forward, -1 for reverse The size of the array (nx,ny) Return false if there are memory problems or the dimensions are not powers of 2 */ procStatus CFFTMachine::FFT2D( COMPLEX **c,int nx,int ny,int dir ) { int i,j; int m,twopm; double *real,*imag; /* Transform the rows */ real = (double *)malloc(nx * sizeof(double)); imag = (double *)malloc(nx * sizeof(double)); if (real == NULL || imag == NULL) return(eMemAllocErr); if (!Powerof2(nx,&m,&twopm) || twopm != nx) return(eInvalideArg); for (j=0;j<ny;j++) { for (i=0;i<nx;i++) { real[i] = c[i][j].real; imag[i] = c[i][j].imag; } FFT(dir,m,real,imag); for (i=0;i<nx;i++) { c[i][j].real = real[i]; c[i][j].imag = imag[i]; } } free(real); free(imag); /* Transform the columns */ real = (double *)malloc(ny * sizeof(double)); imag = (double *)malloc(ny * sizeof(double)); if (real == NULL || imag == NULL) return(eMemAllocErr); if (!Powerof2(ny,&m,&twopm) || twopm != ny) return(eInvalideArg); for (i=0;i<nx;i++) { for (j=0;j<ny;j++) { real[j] = c[i][j].real; imag[j] = c[i][j].imag; } FFT(dir,m,real,imag); for (j=0;j<ny;j++) { c[i][j].real = real[j]; c[i][j].imag = imag[j]; } } free(real); free(imag); return(eNormal); }
//int FFT2D(Complex **c,int nx,int ny,int dir) int FFT2D(Complex c[][NY],int nx,int ny,int dir) { int i,j; int m,twopm; /* Transform the rows */ float real[NY], imag[NY]; if (!Powerof2(nx,&m,&twopm) || twopm != nx) return(FALSE); for (j=0;j<ny;j++) { for (i=0;i<nx;i++) { real[i] = c[i][j].real; imag[i] = c[i][j].imag; } FFT(dir,m,real,imag); for (i=0;i<nx;i++) { c[i][j].real = real[i]; c[i][j].imag = imag[i]; } } /* Transform the columns */ if (!Powerof2(ny,&m,&twopm) || twopm != ny) return(FALSE); for (i=0;i<nx;i++) { for (j=0;j<ny;j++) { real[j] = c[i][j].real; imag[j] = c[i][j].imag; } FFT(dir,m,real,imag); for (j=0;j<ny;j++) { c[i][j].real = real[j]; c[i][j].imag = imag[j]; } } return(TRUE); }
EXPORT bool output_spectral_in_time( char *basename, Wave *wave, Front *front) { COMPONENT comp; Locstate state; float *coords; float *L = wave->rect_grid->L; float *U = wave->rect_grid->U; float *h = wave->rect_grid->h; float kk,kx,ky,dk,Ek,Vk; int icoords[MAXD]; int dim = wave->rect_grid->dim; int status; char eng_name[100],vor_name[100]; static FILE *eng_file,*vor_file; static int first = YES; int i, ix, iy; int xmax, ymax, kmax, mx, my, dummy; COMPLEX **mesh_eng,**mesh_vor; Locstate lstate,rstate,bstate,tstate; debug_print("fft","Entered output_spectral_in_time()\n"); if (first) { first = NO; (void) sprintf(eng_name,"%s.energy-time.dat",basename); (void) sprintf(vor_name,"%s.vorticity-time.dat",basename); eng_file = fopen(eng_name,"w"); vor_file = fopen(vor_name,"w"); fprintf(eng_file,"VARIABLES=k,E(k)\n",xmax,ymax); fprintf(vor_file,"VARIABLES=k,V(k)\n",xmax,ymax); } xmax = wave->rect_grid->gmax[0]; ymax = wave->rect_grid->gmax[1]; if (!Powerof2(xmax,&mx,&dummy) || !Powerof2(ymax,&my,&dummy)) { screen("output_spectral_in_time() cannot analyze " "mesh not power of 2\n"); screen("xmax = %d ymax = %d\n",xmax,ymax); return FUNCTION_FAILED; } bi_array(&mesh_eng,xmax,ymax,sizeof(COMPLEX)); bi_array(&mesh_vor,xmax,ymax,sizeof(COMPLEX)); for (iy = 0; iy < ymax; ++iy) { for (ix = 0; ix < xmax; ++ix) { icoords[0] = ix; icoords[1] = iy; coords = Rect_coords(icoords,wave); comp = Rect_comp(icoords,wave); state = Rect_state(icoords,wave); if (ix != 0) icoords[0] = ix - 1; else icoords[0] = ix; lstate = Rect_state(icoords,wave); if (ix != xmax-1) icoords[0] = ix + 1; else icoords[0] = ix; rstate = Rect_state(icoords,wave); icoords[0] = ix; if (iy != 0) icoords[1] = iy - 1; else icoords[1] = iy; bstate = Rect_state(icoords,wave); if (iy != ymax-1) icoords[1] = iy + 1; else icoords[1] = iy; tstate = Rect_state(icoords,wave); mesh_eng[ix][iy].real = kinetic_energy(state); mesh_eng[ix][iy].imag = 0.0; mesh_vor[ix][iy].real = (Mom(rstate)[1]/Dens(rstate) - Mom(lstate)[1]/Dens(lstate))/h[0] - (Mom(tstate)[0]/Dens(tstate) - Mom(bstate)[0]/Dens(bstate))/h[1]; mesh_vor[ix][iy].imag = 0.0; } } fft2d(mesh_eng,xmax,ymax,1); fft2d(mesh_vor,xmax,ymax,1); kmax = xmax/2; dk = 2.0*PI/(U[0] - L[0]); fprintf(eng_file,"ZONE\n",kk,Ek); fprintf(vor_file,"ZONE\n",kk,Vk); for (i = 0; i < kmax; ++i) { Ek = 0.0; Vk = 0.0; kk = 2.0*i*PI/(U[0] - L[0]); for (iy = 0; iy < ymax/2; ++iy) { for (ix = 0; ix < xmax/2; ++ix) { kx = 2.0*ix*PI/(U[0] - L[0]); ky = 2.0*iy*PI/(U[1] - L[1]); if (sqrt(sqr(kx)+sqr(ky)) >= kk-0.5*dk && sqrt(sqr(kx)+sqr(ky)) < kk+0.5*dk) { Ek += 2.0*sqrt(sqr(mesh_eng[ix][iy].real) + sqr(mesh_eng[ix][iy].imag)); Vk += 2.0*sqrt(sqr(mesh_vor[ix][iy].real) + sqr(mesh_vor[ix][iy].imag)); } } } fprintf(eng_file,"%lf\t%lf\n",kk,Ek); fprintf(vor_file,"%lf\t%lf\n",kk,Vk); } fflush(eng_file); fflush(vor_file); free_these(2,mesh_eng,mesh_vor); debug_print("fft","Left output_spectral_in_time()\n"); return FUNCTION_SUCCEEDED; } /*end output_spectral_in_time*/
EXPORT bool output_spectral_analysis( char *basename, Wave *wave, Front *front) { COMPONENT comp; Locstate state; float *coords; float *L = wave->rect_grid->L; float *U = wave->rect_grid->U; float *h = wave->rect_grid->h; int icoords[MAXD]; int dim = wave->rect_grid->dim; int status; int step = front->step; char energy_name[100],vorticity_name[100], enstrophy_name[100],dens_name[100],pres_name[100]; FILE *energy_file,*vorticity_file, *enstrophy_file,*dens_file,*pres_file; debug_print("fft","Entered fft_energy_spectral()\n"); (void) sprintf(energy_name,"%s.energy%s.dat",basename, right_flush(step,TSTEP_FIELD_WIDTH)); (void) sprintf(vorticity_name,"%s.vorticity%s.dat",basename, right_flush(step,TSTEP_FIELD_WIDTH)); (void) sprintf(enstrophy_name,"%s.enstrophy%s.dat",basename, right_flush(step,TSTEP_FIELD_WIDTH)); (void) sprintf(dens_name,"%s.density%s.dat",basename, right_flush(step,TSTEP_FIELD_WIDTH)); (void) sprintf(pres_name,"%s.pressure%s.dat",basename, right_flush(step,TSTEP_FIELD_WIDTH)); energy_file = fopen(energy_name,"w"); vorticity_file = fopen(vorticity_name,"w"); enstrophy_file = fopen(enstrophy_name,"w"); dens_file = fopen(dens_name,"w"); pres_file = fopen(pres_name,"w"); if (wave->sizest == 0) { debug_print("fft","Left fft_energy_spectral()\n"); return FUNCTION_FAILED; } switch (dim) { #if defined(ONED) case 1: { int ix; int xmax; COMPLEX *mesh_energy; xmax = wave->rect_grid->gmax[0]; uni_array(&mesh_energy,xmax,sizeof(COMPLEX)); for (ix = 0; ix < xmax; ++ix) { icoords[0] = ix; coords = Rect_coords(icoords,wave); comp = Rect_comp(icoords,wave); state = Rect_state(icoords,wave); mesh_energy[ix].real = Energy(state); mesh_energy[ix].imag = 0.0; } break; } #endif /* defined(ONED) */ #if defined(TWOD) case 2: { int ix, iy; int xmax, ymax, mx, my, dummy; COMPLEX **mesh_energy,**mesh_vorticity,**mesh_enstrophy; Locstate lstate,rstate,bstate,tstate; float kk,kx,ky,dk; xmax = wave->rect_grid->gmax[0]; ymax = wave->rect_grid->gmax[1]; if (!Powerof2(xmax,&mx,&dummy) || !Powerof2(ymax,&my,&dummy)) { screen("fft_energy_spectral() cannot analyze " "mesh not power of 2\n"); screen("xmax = %d ymax = %d\n",xmax,ymax); return FUNCTION_FAILED; } bi_array(&mesh_energy,xmax,ymax,sizeof(COMPLEX)); bi_array(&mesh_vorticity,xmax,ymax,sizeof(COMPLEX)); fprintf(energy_file,"zone i=%d, j=%d\n",xmax,ymax); fprintf(vorticity_file,"zone i=%d, j=%d\n",xmax,ymax); fprintf(dens_file,"zone i=%d, j=%d\n",xmax,ymax); fprintf(pres_file,"zone i=%d, j=%d\n",xmax,ymax); fprintf(enstrophy_file,"zone i=%d, j=%d\n",xmax,ymax); for (iy = 0; iy < ymax; ++iy) { for (ix = 0; ix < xmax; ++ix) { icoords[0] = ix; icoords[1] = iy; coords = Rect_coords(icoords,wave); comp = Rect_comp(icoords,wave); state = Rect_state(icoords,wave); mesh_energy[ix][iy].real = kinetic_energy(state); mesh_energy[ix][iy].imag = 0.0; if (ix != 0) icoords[0] = ix - 1; else icoords[0] = ix; lstate = Rect_state(icoords,wave); if (ix != xmax-1) icoords[0] = ix + 1; else icoords[0] = ix; rstate = Rect_state(icoords,wave); icoords[0] = ix; if (iy != 0) icoords[1] = iy - 1; else icoords[1] = iy; bstate = Rect_state(icoords,wave); if (iy != ymax-1) icoords[1] = iy + 1; else icoords[1] = iy; tstate = Rect_state(icoords,wave); mesh_vorticity[ix][iy].real = (Mom(rstate)[1]/Dens(rstate) - Mom(lstate)[1]/Dens(lstate))/h[0] - (Mom(tstate)[0]/Dens(tstate) - Mom(bstate)[0]/Dens(bstate))/h[1]; mesh_vorticity[ix][iy].imag = 0.0; fprintf(energy_file,"%lf\n",kinetic_energy(state)); fprintf(vorticity_file,"%lf\n",mesh_vorticity[ix][iy].real); fprintf(enstrophy_file,"%lf\n", sqr(mesh_vorticity[ix][iy].real)); fprintf(dens_file,"%lf\n",Dens(state)); fprintf(pres_file,"%lf\n",pressure(state)); } } fft_output2d(basename,"energy",step,wave->rect_grid, mesh_energy); fft_output2d(basename,"vorticity",step,wave->rect_grid, mesh_vorticity); free_these(2,mesh_energy,mesh_vorticity); break; } #endif /* defined(TWOD) */ #if defined(THREED) case 3: { int ix, iy, iz; int xmax, ymax, zmax; COMPLEX ***mesh_energy; xmax = wave->rect_grid->gmax[0]; ymax = wave->rect_grid->gmax[1]; zmax = wave->rect_grid->gmax[2]; tri_array(&mesh_energy,xmax,ymax,zmax,sizeof(COMPLEX)); for (iz = 0; iz < zmax; ++iz) { icoords[2] = iz; for (iy = 0; iy < ymax; ++iy) { icoords[1] = iy; for (ix = 0; ix < xmax; ++ix) { icoords[0] = ix; coords = Rect_coords(icoords,wave); comp = Rect_comp(icoords,wave); state = Rect_state(icoords,wave); mesh_energy[ix][iy][iz].real = Energy(state); mesh_energy[ix][iy][iz].imag = 0.0; } } } break; } #endif /* defined(THREED) */ } fclose(energy_file); fclose(vorticity_file); fclose(enstrophy_file); fclose(dens_file); fclose(pres_file); debug_print("fft","Left fft_energy_spectral()\n"); return FUNCTION_SUCCEEDED; } /*end fft_energy_spectral*/