The following code serves as a simple example of the usage of
HYPRE. In this example, the structured-grid interface
(discussed in Chapter 3) is used
to enter the problem into HYPRE, and the PFMG
Multigrid
solver is used to solve the system. Since the structured-grid
interface currently only supports one underlying matrix class, there
are no choices to make here. If we were using the semi-structured
grid interface instead, then we would have to choose between the
SStruct
and ParCSR
matrix classes, depending on the
solver we want to use.
This example and all other examples in this manual are written in C, but HYPRE also supports Fortran. See Section 8.2 for details.
/*----------------------------------------------------------- * Set up the grid and stencil *-----------------------------------------------------------*/ HYPRE_StructGridCreate(MPI_COMM_WORLD, dim, &grid); HYPRE_StructGridSetExtents(grid, ilower, iupper); ... HYPRE_StructGridAssemble(grid); HYPRE_StructStencilCreate(dim, stencil_size, &stencil); HYPRE_StructStencilSetElement(stencil, 0, offset0); ... /*----------------------------------------------------------- * Set up the matrix, right-hand side, and initial guess *-----------------------------------------------------------*/ HYPRE_StructMatrixCreate(MPI_COMM_WORLD, grid, stencil, &A); HYPRE_StructMatrixInitialize(A); HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, nelts, elts, Avalues); ... HYPRE_StructMatrixAssemble(A); HYPRE_StructVectorCreate(MPI_COMM_WORLD, grid, &b); HYPRE_StructVectorInitialize(b); HYPRE_StructVectorSetBoxValues(b, ilower, iupper, bvalues); ... HYPRE_StructVectorAssemble(b); HYPRE_StructVectorCreate(MPI_COMM_WORLD, grid, &x); HYPRE_StructVectorInitialize(x); HYPRE_StructVectorSetBoxValues(x, ilower, iupper, xvalues); ... HYPRE_StructVectorAssemble(x); /*----------------------------------------------------------- * Set up the solver *-----------------------------------------------------------*/ HYPRE_StructPFMGCreate(MPI_COMM_WORLD, &solver); HYPRE_StructPFMGSetMaxIter(solver, 50); /* optional */ HYPRE_StructPFMGSetTol(solver, 1.0e-06); /* optional */ HYPRE_StructPFMGSetup(solver, A, b, x); /*----------------------------------------------------------- * Solve the linear system *-----------------------------------------------------------*/ HYPRE_StructPFMGSolve(solver, A, b, x); /*----------------------------------------------------------- * Get solution info and free up memory *-----------------------------------------------------------*/ HYPRE_StructVectorGetBoxValues(x, ilower, iupper, xvalues); ... HYPRE_StructPFMGDestroy(solver); HYPRE_StructGridDestroy(grid); HYPRE_StructStencilDestroy(stencil); HYPRE_StructMatrixDestroy(A); HYPRE_StructVectorDestroy(b); HYPRE_StructVectorDestroy(x);