Actual source code: ex153.c
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: int main(int argc, char **args)
5: {
6: PetscMPIInt rank, size;
7: PetscInt N0 = 10, N1 = 10, N2 = 10, N3 = 10, N4 = 10, N = N0 * N1 * N2 * N3 * N4;
8: PetscRandom rdm;
9: PetscReal enorm;
10: Vec x, y, z, input, output;
11: Mat A;
12: PetscInt DIM, dim[5], vsize;
13: PetscReal fac;
15: PetscFunctionBeginUser;
16: PetscCall(PetscInitialize(&argc, &args, NULL, help));
17: PetscCheck(!PetscDefined(USE_COMPLEX), PETSC_COMM_WORLD, PETSC_ERR_SUP, "This example requires real numbers");
18: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
19: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
21: PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uni-processor example only");
22: PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &rdm));
23: PetscCall(PetscRandomSetFromOptions(rdm));
24: PetscCall(VecCreate(PETSC_COMM_SELF, &input));
25: PetscCall(VecSetSizes(input, N, N));
26: PetscCall(VecSetFromOptions(input));
27: PetscCall(VecSetRandom(input, rdm));
28: PetscCall(VecDuplicate(input, &output));
30: DIM = 5;
31: dim[0] = N0;
32: dim[1] = N1;
33: dim[2] = N2;
34: dim[3] = N3;
35: dim[4] = N4;
36: PetscCall(MatCreateFFT(PETSC_COMM_SELF, DIM, dim, MATFFTW, &A));
37: PetscCall(MatCreateVecs(A, &x, &y));
38: PetscCall(MatCreateVecs(A, &z, NULL));
40: PetscCall(VecGetSize(x, &vsize));
41: printf("The vector size of input from the main routine is %d\n", vsize);
43: PetscCall(VecGetSize(z, &vsize));
44: printf("The vector size of output from the main routine is %d\n", vsize);
46: PetscCall(InputTransformFFT(A, input, x));
48: PetscCall(MatMult(A, x, y));
49: PetscCall(MatMultTranspose(A, y, z));
51: PetscCall(OutputTransformFFT(A, z, output));
52: fac = 1.0 / (PetscReal)N;
53: PetscCall(VecScale(output, fac));
54: /*
55: PetscCall(VecAssemblyBegin(input));
56: PetscCall(VecAssemblyEnd(input));
57: PetscCall(VecAssemblyBegin(output));
58: PetscCall(VecAssemblyEnd(output));
60: PetscCall(VecView(input,PETSC_VIEWER_STDOUT_WORLD));
61: PetscCall(VecView(output,PETSC_VIEWER_STDOUT_WORLD));
62: */
63: PetscCall(VecAXPY(output, -1.0, input));
64: PetscCall(VecNorm(output, NORM_1, &enorm));
65: /* if (enorm > 1.e-14) { */
66: PetscCall(PetscPrintf(PETSC_COMM_SELF, " Error norm of |x - z| %e\n", enorm));
67: /* } */
69: PetscCall(VecDestroy(&output));
70: PetscCall(VecDestroy(&input));
71: PetscCall(VecDestroy(&x));
72: PetscCall(VecDestroy(&y));
73: PetscCall(VecDestroy(&z));
74: PetscCall(MatDestroy(&A));
75: PetscCall(PetscRandomDestroy(&rdm));
76: PetscCall(PetscFinalize());
77: return 0;
78: }