Actual source code: ex150.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  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>

  7: extern PetscErrorCode InputTransformFFT(Mat,Vec,Vec);
  8: extern PetscErrorCode OutputTransformFFT(Mat,Vec,Vec);
  9: PetscInt main(PetscInt argc,char **args)
 10: {
 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:   MatCreateVecs(A,&x,&y);
 46:   MatCreateVecs(A,&z,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:   }
 79: /*      } */




 84: /* MatCreateVecs(A,&z,NULL); */
 85: /*  printf("Vector size from ex148 %d\n",vsize); */
 86: /*  PetscObjectSetName((PetscObject) x, "Real space vector"); */
 87: /*      PetscObjectSetName((PetscObject) y, "Frequency space vector"); */
 88: /*      PetscObjectSetName((PetscObject) z, "Reconstructed vector"); */

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