1 static char help[]="This program illustrates the use of PETSc-fftw interface for real DFT\n"; 2 #include <petscmat.h> 3 #include <fftw3-mpi.h> 4 5 extern PetscErrorCode InputTransformFFT(Mat,Vec,Vec); 6 extern PetscErrorCode OutputTransformFFT(Mat,Vec,Vec); 7 int main(int argc,char **args) 8 { 9 PetscMPIInt rank,size; 10 PetscInt N0=3,N1=3,N2=3,N=N0*N1*N2; 11 PetscRandom rdm; 12 PetscScalar a; 13 PetscReal enorm; 14 Vec x,y,z,input,output; 15 PetscBool view=PETSC_FALSE,use_interface=PETSC_TRUE; 16 Mat A; 17 PetscInt DIM, dim[3],vsize; 18 PetscReal fac; 19 20 PetscFunctionBeginUser; 21 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 22 #if defined(PETSC_USE_COMPLEX) 23 SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This example requires real numbers"); 24 #endif 25 26 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 27 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 28 29 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rdm)); 30 PetscCall(PetscRandomSetFromOptions(rdm)); 31 32 PetscCall(VecCreate(PETSC_COMM_WORLD,&input)); 33 PetscCall(VecSetSizes(input,PETSC_DECIDE,N)); 34 PetscCall(VecSetFromOptions(input)); 35 PetscCall(VecSetRandom(input,rdm)); 36 PetscCall(VecDuplicate(input,&output)); 37 /* PetscCall(VecGetSize(input,&vsize)); */ 38 /* printf("Size of the input Vector is %d\n",vsize); */ 39 40 DIM = 3; 41 dim[0] = N0; dim[1] = N1; dim[2] = N2; 42 43 PetscCall(MatCreateFFT(PETSC_COMM_WORLD,DIM,dim,MATFFTW,&A)); 44 PetscCall(MatCreateVecs(A,&x,&y)); 45 PetscCall(MatCreateVecs(A,&z,NULL)); 46 PetscCall(VecGetSize(y,&vsize)); 47 printf("The vector size from the main routine is %d\n",vsize); 48 49 PetscCall(InputTransformFFT(A,input,x)); 50 PetscCall(MatMult(A,x,y)); 51 PetscCall(MatMultTranspose(A,y,z)); 52 PetscCall(OutputTransformFFT(A,z,output)); 53 54 fac = 1.0/(PetscReal)N; 55 PetscCall(VecScale(output,fac)); 56 57 PetscCall(VecAssemblyBegin(input)); 58 PetscCall(VecAssemblyEnd(input)); 59 PetscCall(VecAssemblyBegin(output)); 60 PetscCall(VecAssemblyEnd(output)); 61 62 PetscCall(VecView(input,PETSC_VIEWER_STDOUT_WORLD)); 63 PetscCall(VecView(output,PETSC_VIEWER_STDOUT_WORLD)); 64 65 PetscCall(VecAXPY(output,-1.0,input)); 66 PetscCall(VecNorm(output,NORM_1,&enorm)); 67 /* if (enorm > 1.e-14) { */ 68 if (rank == 0) { 69 PetscCall(PetscPrintf(PETSC_COMM_SELF," Error norm of |x - z| %e\n",enorm)); 70 } 71 /* } */ 72 73 /* PetscCall(MatCreateVecs(A,&z,NULL)); */ 74 /* printf("Vector size from ex148 %d\n",vsize); */ 75 /* PetscCall(PetscObjectSetName((PetscObject) x, "Real space vector")); */ 76 /* PetscCall(PetscObjectSetName((PetscObject) y, "Frequency space vector")); */ 77 /* PetscCall(PetscObjectSetName((PetscObject) z, "Reconstructed vector")); */ 78 79 PetscCall(PetscFinalize()); 80 return 0; 81 82 } 83