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>
4
5 extern PetscErrorCode InputTransformFFT(Mat, Vec, Vec);
6 extern PetscErrorCode OutputTransformFFT(Mat, Vec, Vec);
main(int argc,char ** args)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;
18
19 PetscFunctionBeginUser;
20 PetscCall(PetscInitialize(&argc, &args, NULL, help));
21 #if defined(PETSC_USE_COMPLEX)
22 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "This example requires real numbers");
23 #endif
24
25 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
26 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
27
28 PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rdm));
29 PetscCall(PetscRandomSetFromOptions(rdm));
30 PetscCall(VecCreate(PETSC_COMM_WORLD, &input));
31 PetscCall(VecSetSizes(input, PETSC_DECIDE, N));
32 PetscCall(VecSetFromOptions(input));
33 PetscCall(VecSetRandom(input, rdm));
34 PetscCall(VecAssemblyBegin(input));
35 PetscCall(VecAssemblyEnd(input));
36 /* PetscCall(VecView(input,PETSC_VIEWER_STDOUT_WORLD)); */
37 PetscCall(VecDuplicate(input, &output));
38
39 DIM = 4;
40 dim[0] = N0;
41 dim[1] = N1;
42 dim[2] = N2;
43 dim[3] = N3;
44 dim[4] = N4;
45
46 PetscCall(MatCreateFFT(PETSC_COMM_WORLD, DIM, dim, MATFFTW, &A));
47 PetscCall(MatCreateVecs(A, &x, &y));
48 PetscCall(MatCreateVecs(A, &z, NULL));
49 PetscCall(VecGetSize(x, &vsize));
50 printf("The vector size from the main routine is %d\n", vsize);
51
52 PetscCall(InputTransformFFT(A, input, x));
53
54 PetscCall(MatMult(A, x, y));
55 PetscCall(VecAssemblyBegin(y));
56 PetscCall(VecAssemblyEnd(y));
57 PetscCall(VecView(y, PETSC_VIEWER_STDOUT_WORLD));
58 PetscCall(MatMultTranspose(A, y, z));
59 PetscCall(VecGetSize(z, &vsize));
60 printf("The vector size of zfrom the main routine is %d\n", vsize);
61
62 PetscCall(OutputTransformFFT(A, z, output));
63
64 fac = 1.0 / (PetscReal)N;
65 PetscCall(VecScale(output, fac));
66
67 PetscCall(VecAssemblyBegin(input));
68 PetscCall(VecAssemblyEnd(input));
69 PetscCall(VecAssemblyBegin(output));
70 PetscCall(VecAssemblyEnd(output));
71
72 PetscCall(VecView(input, PETSC_VIEWER_STDOUT_WORLD));
73 PetscCall(VecView(output, PETSC_VIEWER_STDOUT_WORLD));
74
75 PetscCall(VecAXPY(output, -1.0, input));
76 PetscCall(VecNorm(output, NORM_1, &enorm));
77 /* if (enorm > 1.e-14) { */
78 if (rank == 0) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Error norm of |x - z| %e\n", enorm));
79 /* } */
80
81 /* PetscCall(MatCreateVecs(A,&z,NULL)); */
82 /* printf("Vector size from ex148 %d\n",vsize); */
83 /* PetscCall(PetscObjectSetName((PetscObject) x, "Real space vector")); */
84 /* PetscCall(PetscObjectSetName((PetscObject) y, "Frequency space vector")); */
85 /* PetscCall(PetscObjectSetName((PetscObject) z, "Reconstructed vector")); */
86
87 PetscCall(VecDestroy(&output));
88 PetscCall(VecDestroy(&input));
89 PetscCall(VecDestroy(&x));
90 PetscCall(VecDestroy(&y));
91 PetscCall(VecDestroy(&z));
92 PetscCall(MatDestroy(&A));
93 PetscCall(PetscRandomDestroy(&rdm));
94 PetscCall(PetscFinalize());
95 return 0;
96 }
97