The matrix is set up in terms of the grid and stencil objects described in Sections 3.1 and 3.2. The coefficients associated with each stencil entry will typically vary from gridpoint to gridpoint, but in the example problem being considered, they are as follows over the entire grid (except at boundaries; see below):
On process 0, the following code will set up matrix values associated
with the center ( ) and south (
) stencil entries in
(3.2) / (3.3)
(boundaries are ignored here temporarily).
TheHYPRE_StructMatrix A; double values[36]; int stencil_indices[2] = {0,3}; int i; HYPRE_StructMatrixCreate(MPI_COMM_WORLD, grid, stencil, &A); HYPRE_StructMatrixInitialize(A); for (i = 0; i < 36; i += 2) { values[i] = 4.0; values[i+1] = -1.0; } HYPRE_StructMatrixSetBoxValues(A, ilower[0], iupper[0], 2, stencil_indices, values); HYPRE_StructMatrixSetBoxValues(A, ilower[1], iupper[1], 2, stencil_indices, values); /* set boundary conditions */ ... HYPRE_StructMatrixAssemble(A);
Create()
routine creates an empty matrix object. The
Initialize()
routine indicates that the matrix coefficients
(or values) are ready to be set. This routine may or may not involve
the allocation of memory for the coefficient data, depending on the
implementation. The optional Set
routines mentioned later in
this chapter and in the Reference Manual, should be called before this
step. The SetBoxValues()
routine sets the matrix coefficients
for some set of stencil entries over the gridpoints in some box. Note
that the box need not correspond to any of the boxes used to create
the grid, but values should be set for all gridpoints that this
process ``owns''. The Assemble()
routine is a collective call,
and finalizes the matrix assembly, making the matrix ``ready to use''.
Matrix coefficients that reach outside of the boundary should be set to zero. For efficiency reasons, HYPRE does not do this automatically. The most natural time to insure this is when the boundary conditions are being set, and this is most naturally done after the coefficients on the grid's interior have been set. For example, during the implementation of the Dirichlet boundary condition on the lower boundary of the grid in Figure 3.1, the ``south'' coefficient must be set to zero. To do this on process 0, the following code could be used:
int ilower[2] = {-3, 1}; int iupper[2] = { 2, 1}; /* create matrix and set interior coefficients */ ... /* implement boundary conditions */ ... for (i = 0; i < 12; i++) { values[i] = 0.0; } i = 3; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, &i, values); /* complete implementation of boundary conditions */ ...