Actual source code: ex149.c

  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>

  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;

 20:   PetscInitialize(&argc,&args,(char*)0,help);
 21: #if defined(PETSC_USE_COMPLEX)
 22:   SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This example requires real numbers");
 23: #endif

 25:   MPI_Comm_size(PETSC_COMM_WORLD, &size);
 26:   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);

 28:   PetscRandomCreate(PETSC_COMM_WORLD, &rdm);
 29:   PetscRandomSetFromOptions(rdm);

 31:   VecCreate(PETSC_COMM_WORLD,&input);
 32:   VecSetSizes(input,PETSC_DECIDE,N);
 33:   VecSetFromOptions(input);
 34:   VecSetRandom(input,rdm);
 35:   VecDuplicate(input,&output);
 36: /*  VecGetSize(input,&vsize); */
 37: /*  printf("Size of the input Vector is %d\n",vsize); */

 39:   DIM    = 3;
 40:   dim[0] = N0; dim[1] = N1; dim[2] = N2;

 42:   MatCreateFFT(PETSC_COMM_WORLD,DIM,dim,MATFFTW,&A);
 43:   MatCreateVecs(A,&x,&y);
 44:   MatCreateVecs(A,&z,NULL);
 45:   VecGetSize(y,&vsize);
 46:   printf("The vector size from the main routine is %d\n",vsize);

 48:   InputTransformFFT(A,input,x);
 49:   MatMult(A,x,y);
 50:   MatMultTranspose(A,y,z);
 51:   OutputTransformFFT(A,z,output);

 53:   fac  = 1.0/(PetscReal)N;
 54:   VecScale(output,fac);

 56:   VecAssemblyBegin(input);
 57:   VecAssemblyEnd(input);
 58:   VecAssemblyBegin(output);
 59:   VecAssemblyEnd(output);

 61:   VecView(input,PETSC_VIEWER_STDOUT_WORLD);
 62:   VecView(output,PETSC_VIEWER_STDOUT_WORLD);

 64:   VecAXPY(output,-1.0,input);
 65:   VecNorm(output,NORM_1,&enorm);
 66: /*  if (enorm > 1.e-14) { */
 67:   if (rank == 0) {
 68:     PetscPrintf(PETSC_COMM_SELF,"  Error norm of |x - z| %e\n",enorm);
 69:   }
 70: /*      } */

 72: /* MatCreateVecs(A,&z,NULL); */
 73: /*  printf("Vector size from ex148 %d\n",vsize); */
 74: /*  PetscObjectSetName((PetscObject) x, "Real space vector"); */
 75: /*      PetscObjectSetName((PetscObject) y, "Frequency space vector"); */
 76: /*      PetscObjectSetName((PetscObject) z, "Reconstructed vector"); */

 78:   PetscFinalize();
 79:   return 0;

 81: }