Actual source code: ex148.c
1: static char help[] = "This program illustrates the use of PETSc-fftw interface for real 2D DFT.\n\
2: See ~petsc/src/mat/tests/ex158.c for general cases. \n\n";
4: #include <petscmat.h>
6: int main(int argc, char **args)
7: {
8: PetscMPIInt rank, size;
9: PetscInt N0 = 50, N1 = 50, N = N0 * N1;
10: PetscRandom rdm;
11: PetscReal enorm;
12: Vec x, y, z, input, output;
13: Mat A;
14: PetscInt DIM, dim[2];
15: PetscReal fac;
17: PetscFunctionBeginUser;
18: PetscCall(PetscInitialize(&argc, &args, NULL, help));
19: PetscCheck(!PetscDefined(USE_COMPLEX), PETSC_COMM_WORLD, PETSC_ERR_SUP, "This example requires real numbers");
21: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
22: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
24: /* Create and set PETSc vectors 'input' and 'output' */
25: PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rdm));
26: PetscCall(PetscRandomSetFromOptions(rdm));
28: PetscCall(VecCreate(PETSC_COMM_WORLD, &input));
29: PetscCall(VecSetSizes(input, PETSC_DECIDE, N0 * N1));
30: PetscCall(VecSetFromOptions(input));
31: PetscCall(VecSetRandom(input, rdm));
32: PetscCall(VecDuplicate(input, &output));
33: PetscCall(PetscObjectSetName((PetscObject)input, "Real space vector"));
34: PetscCall(PetscObjectSetName((PetscObject)output, "Reconstructed vector"));
36: /* Get FFTW vectors 'x', 'y' and 'z' */
37: DIM = 2;
38: dim[0] = N0;
39: dim[1] = N1;
40: PetscCall(MatCreateFFT(PETSC_COMM_WORLD, DIM, dim, MATFFTW, &A));
41: PetscCall(MatCreateVecsFFTW(A, &x, &y, &z));
43: /* Scatter PETSc vector 'input' to FFTW vector 'x' */
44: PetscCall(VecScatterPetscToFFTW(A, input, x));
46: /* Apply forward FFT */
47: PetscCall(MatMult(A, x, y));
49: /* Apply backward FFT */
50: PetscCall(MatMultTranspose(A, y, z));
52: /* Scatter FFTW vector 'z' to PETSc vector 'output' */
53: PetscCall(VecScatterFFTWToPetsc(A, z, output));
55: /* Check accuracy */
56: fac = 1.0 / (PetscReal)N;
57: PetscCall(VecScale(output, fac));
58: PetscCall(VecAXPY(output, -1.0, input));
59: PetscCall(VecNorm(output, NORM_1, &enorm));
60: if (enorm > 1.e-11 && rank == 0) PetscCall(PetscPrintf(PETSC_COMM_SELF, " Error norm of |x - z| %e\n", enorm));
62: /* Free spaces */
63: PetscCall(PetscRandomDestroy(&rdm));
64: PetscCall(VecDestroy(&input));
65: PetscCall(VecDestroy(&output));
66: PetscCall(VecDestroy(&x));
67: PetscCall(VecDestroy(&y));
68: PetscCall(VecDestroy(&z));
69: PetscCall(MatDestroy(&A));
71: PetscCall(PetscFinalize());
72: return 0;
73: }
75: /*TEST
77: build:
78: requires: fftw !complex
80: test:
81: output_file: output/empty.out
83: test:
84: suffix: 2
85: nsize: 3
86: output_file: output/empty.out
88: TEST*/