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