xref: /petsc/src/mat/tests/ex149.c (revision d5b43468fb8780a8feea140ccd6fa3e6a50411cc)
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 {
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;
19 
20   PetscFunctionBeginUser;
21   PetscCall(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
25 
26   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
27   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
28 
29   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rdm));
30   PetscCall(PetscRandomSetFromOptions(rdm));
31 
32   PetscCall(VecCreate(PETSC_COMM_WORLD, &input));
33   PetscCall(VecSetSizes(input, PETSC_DECIDE, N));
34   PetscCall(VecSetFromOptions(input));
35   PetscCall(VecSetRandom(input, rdm));
36   PetscCall(VecDuplicate(input, &output));
37   /*  PetscCall(VecGetSize(input,&vsize)); */
38   /*  printf("Size of the input Vector is %d\n",vsize); */
39 
40   DIM    = 3;
41   dim[0] = N0;
42   dim[1] = N1;
43   dim[2] = N2;
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(y, &vsize));
49   printf("The vector size from the main routine is %d\n", vsize);
50 
51   PetscCall(InputTransformFFT(A, input, x));
52   PetscCall(MatMult(A, x, y));
53   PetscCall(MatMultTranspose(A, y, z));
54   PetscCall(OutputTransformFFT(A, z, output));
55 
56   fac = 1.0 / (PetscReal)N;
57   PetscCall(VecScale(output, fac));
58 
59   PetscCall(VecAssemblyBegin(input));
60   PetscCall(VecAssemblyEnd(input));
61   PetscCall(VecAssemblyBegin(output));
62   PetscCall(VecAssemblyEnd(output));
63 
64   PetscCall(VecView(input, PETSC_VIEWER_STDOUT_WORLD));
65   PetscCall(VecView(output, PETSC_VIEWER_STDOUT_WORLD));
66 
67   PetscCall(VecAXPY(output, -1.0, input));
68   PetscCall(VecNorm(output, NORM_1, &enorm));
69   /*  if (enorm > 1.e-14) { */
70   if (rank == 0) PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Error norm of |x - z| %e\n", enorm));
71   /*      } */
72 
73   /* PetscCall(MatCreateVecs(A,&z,NULL)); */
74   /*  printf("Vector size from ex148 %d\n",vsize); */
75   /*  PetscCall(PetscObjectSetName((PetscObject) x, "Real space vector")); */
76   /*      PetscCall(PetscObjectSetName((PetscObject) y, "Frequency space vector")); */
77   /*      PetscCall(PetscObjectSetName((PetscObject) z, "Reconstructed vector")); */
78 
79   PetscCall(PetscFinalize());
80   return 0;
81 }
82