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;
18: PetscInitialize(&argc, &args, (char *)0, help);
19: #if defined(PETSC_USE_COMPLEX)
20: SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "This example requires real numbers");
21: #endif
23: MPI_Comm_size(PETSC_COMM_WORLD, &size);
24: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
26: /* Create and set PETSc vectors 'input' and 'output' */
27: PetscRandomCreate(PETSC_COMM_WORLD, &rdm);
28: PetscRandomSetFromOptions(rdm);
30: VecCreate(PETSC_COMM_WORLD, &input);
31: VecSetSizes(input, PETSC_DECIDE, N0 * N1);
32: VecSetFromOptions(input);
33: VecSetRandom(input, rdm);
34: VecDuplicate(input, &output);
35: PetscObjectSetName((PetscObject)input, "Real space vector");
36: PetscObjectSetName((PetscObject)output, "Reconstructed vector");
38: /* Get FFTW vectors 'x', 'y' and 'z' */
39: DIM = 2;
40: dim[0] = N0;
41: dim[1] = N1;
42: MatCreateFFT(PETSC_COMM_WORLD, DIM, dim, MATFFTW, &A);
43: MatCreateVecsFFTW(A, &x, &y, &z);
45: /* Scatter PETSc vector 'input' to FFTW vector 'x' */
46: VecScatterPetscToFFTW(A, input, x);
48: /* Apply forward FFT */
49: MatMult(A, x, y);
51: /* Apply backward FFT */
52: MatMultTranspose(A, y, z);
54: /* Scatter FFTW vector 'z' to PETSc vector 'output' */
55: VecScatterFFTWToPetsc(A, z, output);
57: /* Check accuracy */
58: fac = 1.0 / (PetscReal)N;
59: VecScale(output, fac);
60: VecAXPY(output, -1.0, input);
61: VecNorm(output, NORM_1, &enorm);
62: if (enorm > 1.e-11 && rank == 0) PetscPrintf(PETSC_COMM_SELF, " Error norm of |x - z| %e\n", enorm);
64: /* Free spaces */
65: PetscRandomDestroy(&rdm);
66: VecDestroy(&input);
67: VecDestroy(&output);
68: VecDestroy(&x);
69: VecDestroy(&y);
70: VecDestroy(&z);
71: MatDestroy(&A);
73: PetscFinalize();
74: return 0;
75: }
77: /*TEST
79: build:
80: requires: fftw !complex
82: test:
83: output_file: output/ex148.out
85: test:
86: suffix: 2
87: nsize: 3
88: output_file: output/ex148.out
90: TEST*/