xref: /petsc/src/mat/tests/ex112.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
1 static char help[] = "Test sequential FFTW interface \n\n";
2 
3 /*
4   Compiling the code:
5       This code uses the complex numbers version of PETSc, so configure
6       must be run to enable this
7 
8 */
9 
10 #include <petscmat.h>
11 int main(int argc,char **args)
12 {
13   typedef enum {RANDOM, CONSTANT, TANH, NUM_FUNCS} FuncType;
14   const char     *funcNames[NUM_FUNCS] = {"random", "constant", "tanh"};
15   Mat            A;
16   PetscMPIInt    size;
17   PetscInt       n = 10,N,ndim=4,dim[4],DIM,i;
18   Vec            x,y,z;
19   PetscScalar    s;
20   PetscRandom    rdm;
21   PetscReal      enorm, tol = PETSC_SMALL;
22   PetscInt       func;
23   FuncType       function = RANDOM;
24   PetscBool      view     = PETSC_FALSE;
25 
26   PetscFunctionBeginUser;
27   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
28   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
29   PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");
30   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "FFTW Options", "ex112");
31   PetscCall(PetscOptionsEList("-function", "Function type", "ex112", funcNames, NUM_FUNCS, funcNames[function], &func, NULL));
32   PetscCall(PetscOptionsBool("-vec_view_draw", "View the functions", "ex112", view, &view, NULL));
33   function = (FuncType) func;
34   PetscOptionsEnd();
35 
36   for (DIM = 0; DIM < ndim; DIM++) {
37     dim[DIM] = n;  /* size of transformation in DIM-dimension */
38   }
39   PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &rdm));
40   PetscCall(PetscRandomSetFromOptions(rdm));
41 
42   for (DIM = 1; DIM < 5; DIM++) {
43     for (i = 0, N = 1; i < DIM; i++) N *= dim[i];
44     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n %d-D: FFTW on vector of size %d \n",DIM,N));
45 
46     /* create FFTW object */
47     PetscCall(MatCreateFFT(PETSC_COMM_SELF,DIM,dim,MATFFTW,&A));
48 
49     /* create vectors of length N=n^DIM */
50     PetscCall(MatCreateVecs(A,&x,&y));
51     PetscCall(MatCreateVecs(A,&z,NULL));
52     PetscCall(PetscObjectSetName((PetscObject) x, "Real space vector"));
53     PetscCall(PetscObjectSetName((PetscObject) y, "Frequency space vector"));
54     PetscCall(PetscObjectSetName((PetscObject) z, "Reconstructed vector"));
55 
56     /* set values of space vector x */
57     if (function == RANDOM) {
58       PetscCall(VecSetRandom(x, rdm));
59     } else if (function == CONSTANT) {
60       PetscCall(VecSet(x, 1.0));
61     } else if (function == TANH) {
62       PetscScalar *a;
63       PetscCall(VecGetArray(x, &a));
64       for (i = 0; i < N; ++i) {
65         a[i] = tanh((i - N/2.0)*(10.0/N));
66       }
67       PetscCall(VecRestoreArray(x, &a));
68     }
69     if (view) PetscCall(VecView(x, PETSC_VIEWER_DRAW_WORLD));
70 
71     /* apply FFTW_FORWARD and FFTW_BACKWARD several times on same x, y, and z */
72     for (i=0; i<3; i++) {
73       PetscCall(MatMult(A,x,y));
74       if (view && i == 0) PetscCall(VecView(y, PETSC_VIEWER_DRAW_WORLD));
75       PetscCall(MatMultTranspose(A,y,z));
76 
77       /* compare x and z. FFTW computes an unnormalized DFT, thus z = N*x */
78       s    = 1.0/(PetscReal)N;
79       PetscCall(VecScale(z,s));
80       if (view && i == 0) PetscCall(VecView(z, PETSC_VIEWER_DRAW_WORLD));
81       PetscCall(VecAXPY(z,-1.0,x));
82       PetscCall(VecNorm(z,NORM_1,&enorm));
83       if (enorm > tol) {
84         PetscCall(PetscPrintf(PETSC_COMM_SELF,"  Error norm of |x - z| %g\n",(double)enorm));
85       }
86     }
87 
88     /* apply FFTW_FORWARD and FFTW_BACKWARD several times on different x */
89     for (i=0; i<3; i++) {
90       PetscCall(VecDestroy(&x));
91       PetscCall(VecCreateSeq(PETSC_COMM_SELF,N,&x));
92       PetscCall(VecSetRandom(x, rdm));
93 
94       PetscCall(MatMult(A,x,y));
95       PetscCall(MatMultTranspose(A,y,z));
96 
97       /* compare x and z. FFTW computes an unnormalized DFT, thus z = N*x */
98       s    = 1.0/(PetscReal)N;
99       PetscCall(VecScale(z,s));
100       if (view && i == 0) PetscCall(VecView(z, PETSC_VIEWER_DRAW_WORLD));
101       PetscCall(VecAXPY(z,-1.0,x));
102       PetscCall(VecNorm(z,NORM_1,&enorm));
103       if (enorm > tol) {
104         PetscCall(PetscPrintf(PETSC_COMM_SELF,"  Error norm of new |x - z| %g\n",(double)enorm));
105       }
106     }
107 
108     /* free spaces */
109     PetscCall(VecDestroy(&x));
110     PetscCall(VecDestroy(&y));
111     PetscCall(VecDestroy(&z));
112     PetscCall(MatDestroy(&A));
113   }
114   PetscCall(PetscRandomDestroy(&rdm));
115   PetscCall(PetscFinalize());
116   return 0;
117 }
118 
119 /*TEST
120 
121    build:
122       requires:  fftw complex
123 
124    test:
125       args: -mat_fftw_plannerflags FFTW_ESTIMATE
126       output_file: output/ex112.out
127 
128    test:
129       suffix: 2
130       args: -mat_fftw_plannerflags FFTW_MEASURE
131       output_file: output/ex112.out
132       requires: !defined(PETSC_USE_CXXCOMPLEX)
133 
134    test:
135       suffix: 3
136       args: -mat_fftw_plannerflags FFTW_PATIENT
137       output_file: output/ex112.out
138 
139    test:
140       suffix: 4
141       args: -mat_fftw_plannerflags FFTW_EXHAUSTIVE
142       output_file: output/ex112.out
143 
144 TEST*/
145