Actual source code: ex150.c
petsc-3.13.6 2020-09-29
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: PetscInt main(PetscInt argc,char **args)
8: {
10: PetscMPIInt rank,size;
11: PetscInt N0=3,N1=3,N2=3,N3=3,N4=3,N=N0*N1*N2*N3;
12: PetscRandom rdm;
13: PetscReal enorm;
14: Vec x,y,z,input,output;
15: Mat A;
16: PetscInt DIM, dim[5],vsize;
17: PetscReal fac;
18: PetscScalar one=1;
20: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
21: #if defined(PETSC_USE_COMPLEX)
22: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This example requires real numbers");
23: #endif
25: MPI_Comm_size(PETSC_COMM_WORLD, &size);
26: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
28: PetscRandomCreate(PETSC_COMM_WORLD, &rdm);
29: PetscRandomSetFromOptions(rdm);
30: VecCreate(PETSC_COMM_WORLD,&input);
31: VecSetSizes(input,PETSC_DECIDE,N);
32: VecSetFromOptions(input);
33: VecSetRandom(input,rdm);
34: VecAssemblyBegin(input);
35: VecAssemblyEnd(input);
36: /* VecView(input,PETSC_VIEWER_STDOUT_WORLD); */
37: VecDuplicate(input,&output);
39: DIM = 4;
40: dim[0] = N0; dim[1] = N1; dim[2] = N2; dim[3] = N3; dim[4] = N4;
42: MatCreateFFT(PETSC_COMM_WORLD,DIM,dim,MATFFTW,&A);
43: MatCreateVecs(A,&x,&y);
44: MatCreateVecs(A,&z,NULL);
45: VecGetSize(x,&vsize);
46: printf("The vector size from the main routine is %d\n",vsize);
48: InputTransformFFT(A,input,x);
50: MatMult(A,x,y);
51: VecAssemblyBegin(y);
52: VecAssemblyEnd(y);
53: VecView(y,PETSC_VIEWER_STDOUT_WORLD);
54: MatMultTranspose(A,y,z);
55: VecGetSize(z,&vsize);
56: printf("The vector size of zfrom the main routine is %d\n",vsize);
58: OutputTransformFFT(A,z,output);
60: fac = 1.0/(PetscReal)N;
61: VecScale(output,fac);
63: VecAssemblyBegin(input);
64: VecAssemblyEnd(input);
65: VecAssemblyBegin(output);
66: VecAssemblyEnd(output);
68: VecView(input,PETSC_VIEWER_STDOUT_WORLD);
69: VecView(output,PETSC_VIEWER_STDOUT_WORLD);
71: VecAXPY(output,-1.0,input);
72: VecNorm(output,NORM_1,&enorm);
73: /* if (enorm > 1.e-14) { */
74: if (!rank) {
75: PetscPrintf(PETSC_COMM_SELF," Error norm of |x - z| %e\n",enorm);
76: }
77: /* } */
82: /* MatCreateVecs(A,&z,NULL); */
83: /* printf("Vector size from ex148 %d\n",vsize); */
84: /* PetscObjectSetName((PetscObject) x, "Real space vector"); */
85: /* PetscObjectSetName((PetscObject) y, "Frequency space vector"); */
86: /* PetscObjectSetName((PetscObject) z, "Reconstructed vector"); */
88: VecDestroy(&output);
89: VecDestroy(&input);
90: VecDestroy(&x);
91: VecDestroy(&y);
92: VecDestroy(&z);
93: MatDestroy(&A);
94: PetscRandomDestroy(&rdm);
95: PetscFinalize();
96: return ierr;
97: }