xref: /petsc/src/mat/tests/ex150.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283)
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,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   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; dim[1] = N1; dim[2] = N2; dim[3] = N3; dim[4] = N4;
40 
41   PetscCall(MatCreateFFT(PETSC_COMM_WORLD,DIM,dim,MATFFTW,&A));
42   PetscCall(MatCreateVecs(A,&x,&y));
43   PetscCall(MatCreateVecs(A,&z,NULL));
44   PetscCall(VecGetSize(x,&vsize));
45   printf("The vector size from the main routine is %d\n",vsize);
46 
47   PetscCall(InputTransformFFT(A,input,x));
48 
49   PetscCall(MatMult(A,x,y));
50   PetscCall(VecAssemblyBegin(y));
51   PetscCall(VecAssemblyEnd(y));
52   PetscCall(VecView(y,PETSC_VIEWER_STDOUT_WORLD));
53   PetscCall(MatMultTranspose(A,y,z));
54   PetscCall(VecGetSize(z,&vsize));
55   printf("The vector size of zfrom the main routine is %d\n",vsize);
56 
57   PetscCall(OutputTransformFFT(A,z,output));
58 
59   fac  = 1.0/(PetscReal)N;
60   PetscCall(VecScale(output,fac));
61 
62   PetscCall(VecAssemblyBegin(input));
63   PetscCall(VecAssemblyEnd(input));
64   PetscCall(VecAssemblyBegin(output));
65   PetscCall(VecAssemblyEnd(output));
66 
67   PetscCall(VecView(input,PETSC_VIEWER_STDOUT_WORLD));
68   PetscCall(VecView(output,PETSC_VIEWER_STDOUT_WORLD));
69 
70   PetscCall(VecAXPY(output,-1.0,input));
71   PetscCall(VecNorm(output,NORM_1,&enorm));
72 /*  if (enorm > 1.e-14) { */
73   if (rank == 0) {
74     PetscCall(PetscPrintf(PETSC_COMM_SELF,"  Error norm of |x - z| %e\n",enorm));
75   }
76 /*      } */
77 
78 /* PetscCall(MatCreateVecs(A,&z,NULL)); */
79 /*  printf("Vector size from ex148 %d\n",vsize); */
80 /*  PetscCall(PetscObjectSetName((PetscObject) x, "Real space vector")); */
81 /*      PetscCall(PetscObjectSetName((PetscObject) y, "Frequency space vector")); */
82 /*      PetscCall(PetscObjectSetName((PetscObject) z, "Reconstructed vector")); */
83 
84   PetscCall(VecDestroy(&output));
85   PetscCall(VecDestroy(&input));
86   PetscCall(VecDestroy(&x));
87   PetscCall(VecDestroy(&y));
88   PetscCall(VecDestroy(&z));
89   PetscCall(MatDestroy(&A));
90   PetscCall(PetscRandomDestroy(&rdm));
91   PetscCall(PetscFinalize());
92   return 0;
93 }
94