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;

 21:   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

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

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

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

 40:   DIM    = 3;
 41:   dim[0] = N0;
 42:   dim[1] = N1;
 43:   dim[2] = N2;

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

 51:   InputTransformFFT(A, input, x);
 52:   MatMult(A, x, y);
 53:   MatMultTranspose(A, y, z);
 54:   OutputTransformFFT(A, z, output);

 56:   fac = 1.0 / (PetscReal)N;
 57:   VecScale(output, fac);

 59:   VecAssemblyBegin(input);
 60:   VecAssemblyEnd(input);
 61:   VecAssemblyBegin(output);
 62:   VecAssemblyEnd(output);

 64:   VecView(input, PETSC_VIEWER_STDOUT_WORLD);
 65:   VecView(output, PETSC_VIEWER_STDOUT_WORLD);

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

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

 79:   PetscFinalize();
 80:   return 0;
 81: }