Actual source code: ex150.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>

  9: PetscInt main(PetscInt argc,char **args)
 10: {
 11:   PetscErrorCode  ierr;
 12:   PetscMPIInt     rank,size;
 13:   PetscInt        N0=3,N1=3,N2=3,N3=3,N4=3,N=N0*N1*N2*N3;
 14:   PetscRandom     rdm;
 15:   PetscReal       enorm;
 16:   Vec             x,y,z,input,output;
 17:   Mat             A;
 18:   PetscInt        DIM, dim[5],vsize;
 19:   PetscReal       fac;
 20:   PetscScalar     one=1;

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

 27:   MPI_Comm_size(PETSC_COMM_WORLD, &size);
 28:   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);

 30:   PetscRandomCreate(PETSC_COMM_WORLD, &rdm);
 31:   PetscRandomSetFromOptions(rdm);
 32:   VecCreate(PETSC_COMM_WORLD,&input);
 33:   VecSetSizes(input,PETSC_DECIDE,N);
 34:   VecSetFromOptions(input);
 35:   VecSetRandom(input,rdm);
 36:   VecAssemblyBegin(input);
 37:   VecAssemblyEnd(input);
 38: //  VecView(input,PETSC_VIEWER_STDOUT_WORLD);
 39:   VecDuplicate(input,&output);

 41:   DIM = 4;
 42:   dim[0] = N0; dim[1] = N1; dim[2] = N2; dim[3] = N3; dim[4] = N4;

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

 50:   InputTransformFFT(A,input,x);

 52:   MatMult(A,x,y);
 53:   VecAssemblyBegin(y);
 54:   VecAssemblyEnd(y);
 55:   VecView(y,PETSC_VIEWER_STDOUT_WORLD);
 56:   MatMultTranspose(A,y,z);
 57:   VecGetSize(z,&vsize);
 58:   printf("The vector size of zfrom the main routine is %d\n",vsize);

 60:   OutputTransformFFT(A,z,output);

 62:   fac = 1.0/(PetscReal)N;
 63:   VecScale(output,fac);

 65:   VecAssemblyBegin(input);
 66:   VecAssemblyEnd(input);
 67:   VecAssemblyBegin(output);
 68:   VecAssemblyEnd(output);

 70:   VecView(input,PETSC_VIEWER_STDOUT_WORLD);
 71:   VecView(output,PETSC_VIEWER_STDOUT_WORLD);

 73:   VecAXPY(output,-1.0,input);
 74:   VecNorm(output,NORM_1,&enorm);
 75: //  if (enorm > 1.e-14){
 76:     if (!rank)
 77:       PetscPrintf(PETSC_COMM_SELF,"  Error norm of |x - z| %e\n",enorm);
 78: //      }


 81: 
 82: 
 83: // MatGetVecs(A,&z,PETSC_NULL);
 84: //  printf("Vector size from ex148 %d\n",vsize);
 85: //  PetscObjectSetName((PetscObject) x, "Real space vector");
 86: //      PetscObjectSetName((PetscObject) y, "Frequency space vector");
 87: //      PetscObjectSetName((PetscObject) z, "Reconstructed vector");

 89:   VecDestroy(&output);
 90:   VecDestroy(&input);
 91:   VecDestroy(&x);
 92:   VecDestroy(&y);
 93:   VecDestroy(&z);
 94:   MatDestroy(&A);
 95:   PetscRandomDestroy(&rdm);
 96:   PetscFinalize();
 97:   return 0;
 98: }