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