Actual source code: ex150.c
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>
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;
19: 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
24: MPI_Comm_size(PETSC_COMM_WORLD, &size);
25: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
27: PetscRandomCreate(PETSC_COMM_WORLD, &rdm);
28: PetscRandomSetFromOptions(rdm);
29: VecCreate(PETSC_COMM_WORLD,&input);
30: VecSetSizes(input,PETSC_DECIDE,N);
31: VecSetFromOptions(input);
32: VecSetRandom(input,rdm);
33: VecAssemblyBegin(input);
34: VecAssemblyEnd(input);
35: /* VecView(input,PETSC_VIEWER_STDOUT_WORLD); */
36: VecDuplicate(input,&output);
38: DIM = 4;
39: dim[0] = N0; dim[1] = N1; dim[2] = N2; dim[3] = N3; dim[4] = N4;
41: MatCreateFFT(PETSC_COMM_WORLD,DIM,dim,MATFFTW,&A);
42: MatCreateVecs(A,&x,&y);
43: MatCreateVecs(A,&z,NULL);
44: VecGetSize(x,&vsize);
45: printf("The vector size from the main routine is %d\n",vsize);
47: InputTransformFFT(A,input,x);
49: MatMult(A,x,y);
50: VecAssemblyBegin(y);
51: VecAssemblyEnd(y);
52: VecView(y,PETSC_VIEWER_STDOUT_WORLD);
53: MatMultTranspose(A,y,z);
54: VecGetSize(z,&vsize);
55: printf("The vector size of zfrom the main routine is %d\n",vsize);
57: OutputTransformFFT(A,z,output);
59: fac = 1.0/(PetscReal)N;
60: VecScale(output,fac);
62: VecAssemblyBegin(input);
63: VecAssemblyEnd(input);
64: VecAssemblyBegin(output);
65: VecAssemblyEnd(output);
67: VecView(input,PETSC_VIEWER_STDOUT_WORLD);
68: VecView(output,PETSC_VIEWER_STDOUT_WORLD);
70: VecAXPY(output,-1.0,input);
71: VecNorm(output,NORM_1,&enorm);
72: /* if (enorm > 1.e-14) { */
73: if (rank == 0) {
74: PetscPrintf(PETSC_COMM_SELF," Error norm of |x - z| %e\n",enorm);
75: }
76: /* } */
78: /* MatCreateVecs(A,&z,NULL); */
79: /* printf("Vector size from ex148 %d\n",vsize); */
80: /* PetscObjectSetName((PetscObject) x, "Real space vector"); */
81: /* PetscObjectSetName((PetscObject) y, "Frequency space vector"); */
82: /* PetscObjectSetName((PetscObject) z, "Reconstructed vector"); */
84: VecDestroy(&output);
85: VecDestroy(&input);
86: VecDestroy(&x);
87: VecDestroy(&y);
88: VecDestroy(&z);
89: MatDestroy(&A);
90: PetscRandomDestroy(&rdm);
91: PetscFinalize();
92: return 0;
93: }