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;

 19:   PetscFunctionBeginUser;
 20:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 21:   PetscCheck(!PetscDefined(USE_COMPLEX), PETSC_COMM_WORLD, PETSC_ERR_SUP, "This example requires real numbers");

 23:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 24:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));

 26:   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rdm));
 27:   PetscCall(PetscRandomSetFromOptions(rdm));
 28:   PetscCall(VecCreate(PETSC_COMM_WORLD, &input));
 29:   PetscCall(VecSetSizes(input, PETSC_DECIDE, N));
 30:   PetscCall(VecSetFromOptions(input));
 31:   PetscCall(VecSetRandom(input, rdm));
 32:   PetscCall(VecAssemblyBegin(input));
 33:   PetscCall(VecAssemblyEnd(input));
 34:   /*  PetscCall(VecView(input,PETSC_VIEWER_STDOUT_WORLD)); */
 35:   PetscCall(VecDuplicate(input, &output));

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

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

 50:   PetscCall(InputTransformFFT(A, input, x));

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

 60:   PetscCall(OutputTransformFFT(A, z, output));

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

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

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

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

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

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