Actual source code: ex153.c
petsc-3.14.6 2021-03-30
1: static char help[]="This program illustrates the use of PETSc-fftw interface for sequential real DFT\n";
2: #include <petscmat.h>
3: #include <fftw3-mpi.h>
4: PetscInt main(PetscInt argc,char **args)
5: {
7: PetscMPIInt rank,size;
8: PetscInt N0=10,N1=10,N2=10,N3=10,N4=10,N=N0*N1*N2*N3*N4;
9: PetscRandom rdm;
10: PetscReal enorm;
11: Vec x,y,z,input,output;
12: Mat A;
13: PetscInt DIM, dim[5],vsize;
14: PetscReal fac;
16: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
17: #if defined(PETSC_USE_COMPLEX)
18: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This example requires real numbers");
19: #endif
20: MPI_Comm_size(PETSC_COMM_WORLD, &size);
21: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
23: if (size!=1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This is a uni-processor example only");
24: PetscRandomCreate(PETSC_COMM_SELF, &rdm);
25: PetscRandomSetFromOptions(rdm);
26: VecCreate(PETSC_COMM_SELF,&input);
27: VecSetSizes(input,N,N);
28: VecSetFromOptions(input);
29: VecSetRandom(input,rdm);
30: VecDuplicate(input,&output);
32: DIM = 5; dim[0] = N0; dim[1] = N1; dim[2] = N2; dim[3] = N3; dim[4] = N4;
33: MatCreateFFT(PETSC_COMM_SELF,DIM,dim,MATFFTW,&A);
34: MatCreateVecs(A,&x,&y);
35: MatCreateVecs(A,&z,NULL);
37: VecGetSize(x,&vsize);
38: printf("The vector size of input from the main routine is %d\n",vsize);
40: VecGetSize(z,&vsize);
41: printf("The vector size of output from the main routine is %d\n",vsize);
43: InputTransformFFT(A,input,x);
45: MatMult(A,x,y);
46: MatMultTranspose(A,y,z);
48: OutputTransformFFT(A,z,output);
49: fac = 1.0/(PetscReal)N;
50: VecScale(output,fac);
51: /*
52: VecAssemblyBegin(input);
53: VecAssemblyEnd(input);
54: VecAssemblyBegin(output);
55: VecAssemblyEnd(output);
57: VecView(input,PETSC_VIEWER_STDOUT_WORLD);
58: VecView(output,PETSC_VIEWER_STDOUT_WORLD);
59: */
60: VecAXPY(output,-1.0,input);
61: VecNorm(output,NORM_1,&enorm);
62: /* if (enorm > 1.e-14) { */
63: PetscPrintf(PETSC_COMM_SELF," Error norm of |x - z| %e\n",enorm);
64: /* } */
66: VecDestroy(&output);
67: VecDestroy(&input);
68: VecDestroy(&x);
69: VecDestroy(&y);
70: VecDestroy(&z);
71: MatDestroy(&A);
72: PetscRandomDestroy(&rdm);
73: PetscFinalize();
74: return ierr;
76: }