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