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>

  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, N3 = 3, N4 = 3, N = N0 * N1 * N2 * N3;
 11:   PetscRandom rdm;
 12:   PetscReal   enorm;
 13:   Vec         x, y, z, input, output;
 14:   Mat         A;
 15:   PetscInt    DIM, dim[5], vsize;
 16:   PetscReal   fac;
 17:   PetscScalar one = 1;

 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);
 30:   VecCreate(PETSC_COMM_WORLD, &input);
 31:   VecSetSizes(input, PETSC_DECIDE, N);
 32:   VecSetFromOptions(input);
 33:   VecSetRandom(input, rdm);
 34:   VecAssemblyBegin(input);
 35:   VecAssemblyEnd(input);
 36:   /*  VecView(input,PETSC_VIEWER_STDOUT_WORLD); */
 37:   VecDuplicate(input, &output);

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

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

 52:   InputTransformFFT(A, input, x);

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

 62:   OutputTransformFFT(A, z, output);

 64:   fac = 1.0 / (PetscReal)N;
 65:   VecScale(output, fac);

 67:   VecAssemblyBegin(input);
 68:   VecAssemblyEnd(input);
 69:   VecAssemblyBegin(output);
 70:   VecAssemblyEnd(output);

 72:   VecView(input, PETSC_VIEWER_STDOUT_WORLD);
 73:   VecView(output, PETSC_VIEWER_STDOUT_WORLD);

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

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

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