void hypre_F90_IFACE(hypre_structpcgsetprintlevel, HYPRE_STRUCTPCGSETPRINTLEVEL) ( hypre_F90_Obj *solver, hypre_F90_Int *print_level, hypre_F90_Int *ierr ) { *ierr = (hypre_F90_Int) ( HYPRE_StructPCGSetPrintLevel( hypre_F90_PassObj (HYPRE_StructSolver, solver), hypre_F90_PassInt (print_level) ) ); }
int main (int argc, char *argv[]) { int i, j, myid, num_procs; int vis = 0; HYPRE_StructGrid grid; HYPRE_StructStencil stencil; HYPRE_StructMatrix A; HYPRE_StructVector b; HYPRE_StructVector x; HYPRE_StructSolver solver; /* Initialize MPI */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myid); MPI_Comm_size(MPI_COMM_WORLD, &num_procs); if (num_procs != 2) { if (myid == 0) printf("Must run with 2 processors!\n"); MPI_Finalize(); return(0); } /* Parse command line */ { int arg_index = 0; int print_usage = 0; while (arg_index < argc) { if ( strcmp(argv[arg_index], "-vis") == 0 ) { arg_index++; vis = 1; } else if ( strcmp(argv[arg_index], "-help") == 0 ) { print_usage = 1; break; } else { arg_index++; } } if ((print_usage) && (myid == 0)) { printf("\n"); printf("Usage: %s [<options>]\n", argv[0]); printf("\n"); printf(" -vis : save the solution for GLVis visualization\n"); printf("\n"); } if (print_usage) { MPI_Finalize(); return (0); } } /* 1. Set up a grid. Each processor describes the piece of the grid that it owns. */ { /* Create an empty 2D grid object */ HYPRE_StructGridCreate(MPI_COMM_WORLD, 2, &grid); /* Add boxes to the grid */ if (myid == 0) { int ilower[2]={-3,1}, iupper[2]={-1,2}; HYPRE_StructGridSetExtents(grid, ilower, iupper); } else if (myid == 1) { int ilower[2]={0,1}, iupper[2]={2,4}; HYPRE_StructGridSetExtents(grid, ilower, iupper); } /* This is a collective call finalizing the grid assembly. The grid is now ``ready to be used'' */ HYPRE_StructGridAssemble(grid); } /* 2. Define the discretization stencil */ { /* Create an empty 2D, 5-pt stencil object */ HYPRE_StructStencilCreate(2, 5, &stencil); /* Define the geometry of the stencil. Each represents a relative offset (in the index space). */ { int entry; int offsets[5][2] = {{0,0}, {-1,0}, {1,0}, {0,-1}, {0,1}}; /* Assign each of the 5 stencil entries */ for (entry = 0; entry < 5; entry++) HYPRE_StructStencilSetElement(stencil, entry, offsets[entry]); } } /* 3. Set up a Struct Matrix */ { /* Create an empty matrix object */ HYPRE_StructMatrixCreate(MPI_COMM_WORLD, grid, stencil, &A); /* Indicate that the matrix coefficients are ready to be set */ HYPRE_StructMatrixInitialize(A); /* Set the matrix coefficients. Each processor assigns coefficients for the boxes in the grid that it owns. Note that the coefficients associated with each stencil entry may vary from grid point to grid point if desired. Here, we first set the same stencil entries for each grid point. Then we make modifications to grid points near the boundary. */ if (myid == 0) { int ilower[2]={-3,1}, iupper[2]={-1,2}; int stencil_indices[5] = {0,1,2,3,4}; /* labels for the stencil entries - these correspond to the offsets defined above */ int nentries = 5; int nvalues = 30; /* 6 grid points, each with 5 stencil entries */ double values[30]; /* We have 6 grid points, each with 5 stencil entries */ for (i = 0; i < nvalues; i += nentries) { values[i] = 4.0; for (j = 1; j < nentries; j++) values[i+j] = -1.0; } HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, nentries, stencil_indices, values); } else if (myid == 1) { int ilower[2]={0,1}, iupper[2]={2,4}; int stencil_indices[5] = {0,1,2,3,4}; int nentries = 5; int nvalues = 60; /* 12 grid points, each with 5 stencil entries */ double values[60]; for (i = 0; i < nvalues; i += nentries) { values[i] = 4.0; for (j = 1; j < nentries; j++) values[i+j] = -1.0; } HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, nentries, stencil_indices, values); } /* Set the coefficients reaching outside of the boundary to 0 */ if (myid == 0) { double values[3]; for (i = 0; i < 3; i++) values[i] = 0.0; { /* values below our box */ int ilower[2]={-3,1}, iupper[2]={-1,1}; int stencil_indices[1] = {3}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* values to the left of our box */ int ilower[2]={-3,1}, iupper[2]={-3,2}; int stencil_indices[1] = {1}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* values above our box */ int ilower[2]={-3,2}, iupper[2]={-1,2}; int stencil_indices[1] = {4}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } } else if (myid == 1) { double values[4]; for (i = 0; i < 4; i++) values[i] = 0.0; { /* values below our box */ int ilower[2]={0,1}, iupper[2]={2,1}; int stencil_indices[1] = {3}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* values to the right of our box */ int ilower[2]={2,1}, iupper[2]={2,4}; int stencil_indices[1] = {2}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* values above our box */ int ilower[2]={0,4}, iupper[2]={2,4}; int stencil_indices[1] = {4}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* values to the left of our box (that do not border the other box on proc. 0) */ int ilower[2]={0,3}, iupper[2]={0,4}; int stencil_indices[1] = {1}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } } /* This is a collective call finalizing the matrix assembly. The matrix is now ``ready to be used'' */ HYPRE_StructMatrixAssemble(A); } /* 4. Set up Struct Vectors for b and x. Each processor sets the vectors corresponding to its boxes. */ { /* Create an empty vector object */ HYPRE_StructVectorCreate(MPI_COMM_WORLD, grid, &b); HYPRE_StructVectorCreate(MPI_COMM_WORLD, grid, &x); /* Indicate that the vector coefficients are ready to be set */ HYPRE_StructVectorInitialize(b); HYPRE_StructVectorInitialize(x); /* Set the vector coefficients */ if (myid == 0) { int ilower[2]={-3,1}, iupper[2]={-1,2}; double values[6]; /* 6 grid points */ for (i = 0; i < 6; i ++) values[i] = 1.0; HYPRE_StructVectorSetBoxValues(b, ilower, iupper, values); for (i = 0; i < 6; i ++) values[i] = 0.0; HYPRE_StructVectorSetBoxValues(x, ilower, iupper, values); } else if (myid == 1) { int ilower[2]={0,1}, iupper[2]={2,4}; double values[12]; /* 12 grid points */ for (i = 0; i < 12; i ++) values[i] = 1.0; HYPRE_StructVectorSetBoxValues(b, ilower, iupper, values); for (i = 0; i < 12; i ++) values[i] = 0.0; HYPRE_StructVectorSetBoxValues(x, ilower, iupper, values); } /* This is a collective call finalizing the vector assembly. The vectors are now ``ready to be used'' */ HYPRE_StructVectorAssemble(b); HYPRE_StructVectorAssemble(x); } /* 5. Set up and use a solver (See the Reference Manual for descriptions of all of the options.) */ { /* Create an empty PCG Struct solver */ HYPRE_StructPCGCreate(MPI_COMM_WORLD, &solver); /* Set some parameters */ HYPRE_StructPCGSetTol(solver, 1.0e-06); /* convergence tolerance */ HYPRE_StructPCGSetPrintLevel(solver, 2); /* amount of info. printed */ /* Setup and solve */ HYPRE_StructPCGSetup(solver, A, b, x); HYPRE_StructPCGSolve(solver, A, b, x); } /* Save the solution for GLVis visualization, see vis/glvis-ex1.sh */ if (vis) { GLVis_PrintStructGrid(grid, "vis/ex1.mesh", myid, NULL, NULL); GLVis_PrintStructVector(x, "vis/ex1.sol", myid); GLVis_PrintData("vis/ex1.data", myid, num_procs); } /* Free memory */ HYPRE_StructGridDestroy(grid); HYPRE_StructStencilDestroy(stencil); HYPRE_StructMatrixDestroy(A); HYPRE_StructVectorDestroy(b); HYPRE_StructVectorDestroy(x); HYPRE_StructPCGDestroy(solver); /* Finalize MPI */ MPI_Finalize(); return (0); }
int main (int argc, char *argv[]) { int myid, num_procs; HYPRE_SStructGrid grid; HYPRE_SStructGraph graph; HYPRE_SStructStencil stencil; HYPRE_SStructMatrix A; HYPRE_SStructVector b; HYPRE_SStructVector x; /* We are using struct solvers for this example */ HYPRE_StructSolver solver; HYPRE_StructSolver precond; int object_type; /* Initialize MPI */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myid); MPI_Comm_size(MPI_COMM_WORLD, &num_procs); if (num_procs != 2) { if (myid ==0) printf("Must run with 2 processors!\n"); MPI_Finalize(); return(0); } /* 1. Set up the 2D grid. This gives the index space in each part. Here we only use one part and one variable. (So the part id is 0 and the variable id is 0) */ { int ndim = 2; int nparts = 1; int part = 0; /* Create an empty 2D grid object */ HYPRE_SStructGridCreate(MPI_COMM_WORLD, ndim, nparts, &grid); /* Set the extents of the grid - each processor sets its grid boxes. Each part has its own relative index space numbering, but in this example all boxes belong to the same part. */ /* Processor 0 owns two boxes in the grid. */ if (myid == 0) { /* Add a new box to the grid */ { int ilower[2] = {-3, 1}; int iupper[2] = {-1, 2}; HYPRE_SStructGridSetExtents(grid, part, ilower, iupper); } /* Add a new box to the grid */ { int ilower[2] = {0, 1}; int iupper[2] = {2, 4}; HYPRE_SStructGridSetExtents(grid, part, ilower, iupper); } } /* Processor 1 owns one box in the grid. */ else if (myid == 1) { /* Add a new box to the grid */ { int ilower[2] = {3, 1}; int iupper[2] = {6, 4}; HYPRE_SStructGridSetExtents(grid, part, ilower, iupper); } } /* Set the variable type and number of variables on each part. */ { int i; int nvars = 1; HYPRE_SStructVariable vartypes[1] = {HYPRE_SSTRUCT_VARIABLE_CELL}; for (i = 0; i< nparts; i++) HYPRE_SStructGridSetVariables(grid, i, nvars, vartypes); } /* Now the grid is ready to use */ HYPRE_SStructGridAssemble(grid); } /* 2. Define the discretization stencil(s) */ { /* Create an empty 2D, 5-pt stencil object */ HYPRE_SStructStencilCreate(2, 5, &stencil); /* Define the geometry of the stencil. Each represents a relative offset (in the index space). */ { int entry; int offsets[5][2] = {{0,0}, {-1,0}, {1,0}, {0,-1}, {0,1}}; int var = 0; /* Assign numerical values to the offsets so that we can easily refer to them - the last argument indicates the variable for which we are assigning this stencil - we are just using one variable in this example so it is the first one (0) */ for (entry = 0; entry < 5; entry++) HYPRE_SStructStencilSetEntry(stencil, entry, offsets[entry], var); } } /* 3. Set up the Graph - this determines the non-zero structure of the matrix and allows non-stencil relationships between the parts */ { int var = 0; int part = 0; /* Create the graph object */ HYPRE_SStructGraphCreate(MPI_COMM_WORLD, grid, &graph); /* See MatrixSetObjectType below */ object_type = HYPRE_STRUCT; HYPRE_SStructGraphSetObjectType(graph, object_type); /* Now we need to tell the graph which stencil to use for each variable on each part (we only have one variable and one part) */ HYPRE_SStructGraphSetStencil(graph, part, var, stencil); /* Here we could establish connections between parts if we had more than one part using the graph. For example, we could use HYPRE_GraphAddEntries() routine or HYPRE_GridSetNeighborBox() */ /* Assemble the graph */ HYPRE_SStructGraphAssemble(graph); } /* 4. Set up a SStruct Matrix */ { int i,j; int part = 0; int var = 0; /* Create the empty matrix object */ HYPRE_SStructMatrixCreate(MPI_COMM_WORLD, graph, &A); /* Set the object type (by default HYPRE_SSTRUCT). This determines the data structure used to store the matrix. If you want to use unstructured solvers, e.g. BoomerAMG, the object type should be HYPRE_PARCSR. If the problem is purely structured (with one part), you may want to use HYPRE_STRUCT to access the structured solvers. Here we have a purely structured example. */ object_type = HYPRE_STRUCT; HYPRE_SStructMatrixSetObjectType(A, object_type); /* Get ready to set values */ HYPRE_SStructMatrixInitialize(A); /* Each processor must set the stencil values for their boxes on each part. In this example, we only set stencil entries and therefore use HYPRE_SStructMatrixSetBoxValues. If we need to set non-stencil entries, we have to use HYPRE_SStructMatrixSetValues (shown in a later example). */ if (myid == 0) { /* Set the matrix coefficients for some set of stencil entries over all the gridpoints in my first box (account for boundary grid points later) */ { int ilower[2] = {-3, 1}; int iupper[2] = {-1, 2}; int nentries = 5; int nvalues = 30; /* 6 grid points, each with 5 stencil entries */ double values[30]; int stencil_indices[5]; for (j = 0; j < nentries; j++) /* label the stencil indices - these correspond to the offsets defined above */ stencil_indices[j] = j; for (i = 0; i < nvalues; i += nentries) { values[i] = 4.0; for (j = 1; j < nentries; j++) values[i+j] = -1.0; } HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, nentries, stencil_indices, values); } /* Set the matrix coefficients for some set of stencil entries over the gridpoints in my second box */ { int ilower[2] = {0, 1}; int iupper[2] = {2, 4}; int nentries = 5; int nvalues = 60; /* 12 grid points, each with 5 stencil entries */ double values[60]; int stencil_indices[5]; for (j = 0; j < nentries; j++) stencil_indices[j] = j; for (i = 0; i < nvalues; i += nentries) { values[i] = 4.0; for (j = 1; j < nentries; j++) values[i+j] = -1.0; } HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, nentries, stencil_indices, values); } } else if (myid == 1) { /* Set the matrix coefficients for some set of stencil entries over the gridpoints in my box */ { int ilower[2] = {3, 1}; int iupper[2] = {6, 4}; int nentries = 5; int nvalues = 80; /* 16 grid points, each with 5 stencil entries */ double values[80]; int stencil_indices[5]; for (j = 0; j < nentries; j++) stencil_indices[j] = j; for (i = 0; i < nvalues; i += nentries) { values[i] = 4.0; for (j = 1; j < nentries; j++) values[i+j] = -1.0; } HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, nentries, stencil_indices, values); } } /* For each box, set any coefficients that reach ouside of the boundary to 0 */ if (myid == 0) { int maxnvalues = 6; double values[6]; for (i = 0; i < maxnvalues; i++) values[i] = 0.0; { /* Values below our first AND second box */ int ilower[2] = {-3, 1}; int iupper[2] = { 2, 1}; int stencil_indices[1] = {3}; HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, 1, stencil_indices, values); } { /* Values to the left of our first box */ int ilower[2] = {-3, 1}; int iupper[2] = {-3, 2}; int stencil_indices[1] = {1}; HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, 1, stencil_indices, values); } { /* Values above our first box */ int ilower[2] = {-3, 2}; int iupper[2] = {-1, 2}; int stencil_indices[1] = {4}; HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, 1, stencil_indices, values); } { /* Values to the left of our second box (that do not border the first box). */ int ilower[2] = { 0, 3}; int iupper[2] = { 0, 4}; int stencil_indices[1] = {1}; HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, 1, stencil_indices, values); } { /* Values above our second box */ int ilower[2] = { 0, 4}; int iupper[2] = { 2, 4}; int stencil_indices[1] = {4}; HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, 1, stencil_indices, values); } } else if (myid == 1) { int maxnvalues = 4; double values[4]; for (i = 0; i < maxnvalues; i++) values[i] = 0.0; { /* Values below our box */ int ilower[2] = { 3, 1}; int iupper[2] = { 6, 1}; int stencil_indices[1] = {3}; HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, 1, stencil_indices, values); } { /* Values to the right of our box */ int ilower[2] = { 6, 1}; int iupper[2] = { 6, 4}; int stencil_indices[1] = {2}; HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, 1, stencil_indices, values); } { /* Values above our box */ int ilower[2] = { 3, 4}; int iupper[2] = { 6, 4}; int stencil_indices[1] = {4}; HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper, var, 1, stencil_indices, values); } } /* This is a collective call finalizing the matrix assembly. The matrix is now ``ready to be used'' */ HYPRE_SStructMatrixAssemble(A); } /* 5. Set up SStruct Vectors for b and x */ { int i; /* We have one part and one variable. */ int part = 0; int var = 0; /* Create an empty vector object */ HYPRE_SStructVectorCreate(MPI_COMM_WORLD, grid, &b); HYPRE_SStructVectorCreate(MPI_COMM_WORLD, grid, &x); /* As with the matrix, set the object type for the vectors to be the struct type */ object_type = HYPRE_STRUCT; HYPRE_SStructVectorSetObjectType(b, object_type); HYPRE_SStructVectorSetObjectType(x, object_type); /* Indicate that the vector coefficients are ready to be set */ HYPRE_SStructVectorInitialize(b); HYPRE_SStructVectorInitialize(x); if (myid == 0) { /* Set the vector coefficients over the gridpoints in my first box */ { int ilower[2] = {-3, 1}; int iupper[2] = {-1, 2}; int nvalues = 6; /* 6 grid points */ double values[6]; for (i = 0; i < nvalues; i ++) values[i] = 1.0; HYPRE_SStructVectorSetBoxValues(b, part, ilower, iupper, var, values); for (i = 0; i < nvalues; i ++) values[i] = 0.0; HYPRE_SStructVectorSetBoxValues(x, part, ilower, iupper, var, values); } /* Set the vector coefficients over the gridpoints in my second box */ { int ilower[2] = { 0, 1}; int iupper[2] = { 2, 4}; int nvalues = 12; /* 12 grid points */ double values[12]; for (i = 0; i < nvalues; i ++) values[i] = 1.0; HYPRE_SStructVectorSetBoxValues(b, part, ilower, iupper, var, values); for (i = 0; i < nvalues; i ++) values[i] = 0.0; HYPRE_SStructVectorSetBoxValues(x, part, ilower, iupper, var, values); } } else if (myid == 1) { /* Set the vector coefficients over the gridpoints in my box */ { int ilower[2] = { 3, 1}; int iupper[2] = { 6, 4}; int nvalues = 16; /* 16 grid points */ double values[16]; for (i = 0; i < nvalues; i ++) values[i] = 1.0; HYPRE_SStructVectorSetBoxValues(b, part, ilower, iupper, var, values); for (i = 0; i < nvalues; i ++) values[i] = 0.0; HYPRE_SStructVectorSetBoxValues(x, part, ilower, iupper, var, values); } } /* This is a collective call finalizing the vector assembly. The vectors are now ``ready to be used'' */ HYPRE_SStructVectorAssemble(b); HYPRE_SStructVectorAssemble(x); } /* 6. Set up and use a solver (See the Reference Manual for descriptions of all of the options.) */ { HYPRE_StructMatrix sA; HYPRE_StructVector sb; HYPRE_StructVector sx; /* Because we are using a struct solver, we need to get the object of the matrix and vectors to pass in to the struct solvers */ HYPRE_SStructMatrixGetObject(A, (void **) &sA); HYPRE_SStructVectorGetObject(b, (void **) &sb); HYPRE_SStructVectorGetObject(x, (void **) &sx); /* Create an empty PCG Struct solver */ HYPRE_StructPCGCreate(MPI_COMM_WORLD, &solver); /* Set PCG parameters */ HYPRE_StructPCGSetTol(solver, 1.0e-06); HYPRE_StructPCGSetPrintLevel(solver, 2); HYPRE_StructPCGSetMaxIter(solver, 50); /* Create the Struct SMG solver for use as a preconditioner */ HYPRE_StructSMGCreate(MPI_COMM_WORLD, &precond); /* Set SMG parameters */ HYPRE_StructSMGSetMaxIter(precond, 1); HYPRE_StructSMGSetTol(precond, 0.0); HYPRE_StructSMGSetZeroGuess(precond); HYPRE_StructSMGSetNumPreRelax(precond, 1); HYPRE_StructSMGSetNumPostRelax(precond, 1); /* Set preconditioner and solve */ HYPRE_StructPCGSetPrecond(solver, HYPRE_StructSMGSolve, HYPRE_StructSMGSetup, precond); HYPRE_StructPCGSetup(solver, sA, sb, sx); HYPRE_StructPCGSolve(solver, sA, sb, sx); } /* Free memory */ HYPRE_SStructGridDestroy(grid); HYPRE_SStructStencilDestroy(stencil); HYPRE_SStructGraphDestroy(graph); HYPRE_SStructMatrixDestroy(A); HYPRE_SStructVectorDestroy(b); HYPRE_SStructVectorDestroy(x); HYPRE_StructPCGDestroy(solver); HYPRE_StructSMGDestroy(precond); /* Finalize MPI */ MPI_Finalize(); return (0); }
int main (int argc, char *argv[]) { int i, j; int myid, num_procs; HYPRE_StructGrid grid; HYPRE_StructStencil stencil; HYPRE_StructMatrix A; HYPRE_StructVector b; HYPRE_StructVector x; HYPRE_StructSolver solver; HYPRE_StructSolver precond; /* Initialize MPI */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myid); MPI_Comm_size(MPI_COMM_WORLD, &num_procs); if (num_procs != 2) { if (myid ==0) printf("Must run with 2 processors!\n"); MPI_Finalize(); return(0); } /* 1. Set up a grid */ { /* Create an empty 2D grid object */ HYPRE_StructGridCreate(MPI_COMM_WORLD, 2, &grid); /* Processor 0 owns two boxes in the grid. */ if (myid == 0) { /* Add a new box to the grid */ { int ilower[2] = {-3, 1}; int iupper[2] = {-1, 2}; HYPRE_StructGridSetExtents(grid, ilower, iupper); } /* Add a new box to the grid */ { int ilower[2] = {0, 1}; int iupper[2] = {2, 4}; HYPRE_StructGridSetExtents(grid, ilower, iupper); } } /* Processor 1 owns one box in the grid. */ else if (myid == 1) { /* Add a new box to the grid */ { int ilower[2] = {3, 1}; int iupper[2] = {6, 4}; HYPRE_StructGridSetExtents(grid, ilower, iupper); } } /* This is a collective call finalizing the grid assembly. The grid is now ``ready to be used'' */ HYPRE_StructGridAssemble(grid); } /* 2. Define the discretization stencil */ { /* Create an empty 2D, 5-pt stencil object */ HYPRE_StructStencilCreate(2, 5, &stencil); /* Define the geometry of the stencil. Each represents a relative offset (in the index space). */ { int entry; int offsets[5][2] = {{0,0}, {-1,0}, {1,0}, {0,-1}, {0,1}}; /* Assign each of the 5 stencil entries */ for (entry = 0; entry < 5; entry++) HYPRE_StructStencilSetElement(stencil, entry, offsets[entry]); } } /* 3. Set up a Struct Matrix */ { /* Create an empty matrix object */ HYPRE_StructMatrixCreate(MPI_COMM_WORLD, grid, stencil, &A); /* Indicate that the matrix coefficients are ready to be set */ HYPRE_StructMatrixInitialize(A); if (myid == 0) { /* Set the matrix coefficients for some set of stencil entries over all the gridpoints in my first box (account for boundary grid points later) */ { int ilower[2] = {-3, 1}; int iupper[2] = {-1, 2}; int nentries = 5; int nvalues = 30; /* 6 grid points, each with 5 stencil entries */ double values[30]; int stencil_indices[5]; for (j = 0; j < nentries; j++) /* label the stencil indices - these correspond to the offsets defined above */ stencil_indices[j] = j; for (i = 0; i < nvalues; i += nentries) { values[i] = 4.0; for (j = 1; j < nentries; j++) values[i+j] = -1.0; } HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, nentries, stencil_indices, values); } /* Set the matrix coefficients for some set of stencil entries over the gridpoints in my second box */ { int ilower[2] = {0, 1}; int iupper[2] = {2, 4}; int nentries = 5; int nvalues = 60; /* 12 grid points, each with 5 stencil entries */ double values[60]; int stencil_indices[5]; for (j = 0; j < nentries; j++) stencil_indices[j] = j; for (i = 0; i < nvalues; i += nentries) { values[i] = 4.0; for (j = 1; j < nentries; j++) values[i+j] = -1.0; } HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, nentries, stencil_indices, values); } } else if (myid == 1) { /* Set the matrix coefficients for some set of stencil entries over the gridpoints in my box */ { int ilower[2] = {3, 1}; int iupper[2] = {6, 4}; int nentries = 5; int nvalues = 80; /* 16 grid points, each with 5 stencil entries */ double values[80]; int stencil_indices[5]; for (j = 0; j < nentries; j++) stencil_indices[j] = j; for (i = 0; i < nvalues; i += nentries) { values[i] = 4.0; for (j = 1; j < nentries; j++) values[i+j] = -1.0; } HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, nentries, stencil_indices, values); } } /* For each box, set any coefficients that reach ouside of the boundary to 0 */ if (myid == 0) { int maxnvalues = 6; double values[6]; for (i = 0; i < maxnvalues; i++) values[i] = 0.0; { /* Values below our first AND second box */ int ilower[2] = {-3, 1}; int iupper[2] = { 2, 1}; int stencil_indices[1] = {3}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* Values to the left of our first box */ int ilower[2] = {-3, 1}; int iupper[2] = {-3, 2}; int stencil_indices[1] = {1}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* Values above our first box */ int ilower[2] = {-3, 2}; int iupper[2] = {-1, 2}; int stencil_indices[1] = {4}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* Values to the left of our second box (that do not border the first box). */ int ilower[2] = { 0, 3}; int iupper[2] = { 0, 4}; int stencil_indices[1] = {1}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* Values above our second box */ int ilower[2] = { 0, 4}; int iupper[2] = { 2, 4}; int stencil_indices[1] = {4}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } } else if (myid == 1) { int maxnvalues = 4; double values[4]; for (i = 0; i < maxnvalues; i++) values[i] = 0.0; { /* Values below our box */ int ilower[2] = { 3, 1}; int iupper[2] = { 6, 1}; int stencil_indices[1] = {3}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* Values to the right of our box */ int ilower[2] = { 6, 1}; int iupper[2] = { 6, 4}; int stencil_indices[1] = {2}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } { /* Values above our box */ int ilower[2] = { 3, 4}; int iupper[2] = { 6, 4}; int stencil_indices[1] = {4}; HYPRE_StructMatrixSetBoxValues(A, ilower, iupper, 1, stencil_indices, values); } } /* This is a collective call finalizing the matrix assembly. The matrix is now ``ready to be used'' */ HYPRE_StructMatrixAssemble(A); } /* 4. Set up Struct Vectors for b and x */ { /* Create an empty vector object */ HYPRE_StructVectorCreate(MPI_COMM_WORLD, grid, &b); HYPRE_StructVectorCreate(MPI_COMM_WORLD, grid, &x); /* Indicate that the vector coefficients are ready to be set */ HYPRE_StructVectorInitialize(b); HYPRE_StructVectorInitialize(x); if (myid == 0) { /* Set the vector coefficients over the gridpoints in my first box */ { int ilower[2] = {-3, 1}; int iupper[2] = {-1, 2}; int nvalues = 6; /* 6 grid points */ double values[6]; for (i = 0; i < nvalues; i ++) values[i] = 1.0; HYPRE_StructVectorSetBoxValues(b, ilower, iupper, values); for (i = 0; i < nvalues; i ++) values[i] = 0.0; HYPRE_StructVectorSetBoxValues(x, ilower, iupper, values); } /* Set the vector coefficients over the gridpoints in my second box */ { int ilower[2] = { 0, 1}; int iupper[2] = { 2, 4}; int nvalues = 12; /* 12 grid points */ double values[12]; for (i = 0; i < nvalues; i ++) values[i] = 1.0; HYPRE_StructVectorSetBoxValues(b, ilower, iupper, values); for (i = 0; i < nvalues; i ++) values[i] = 0.0; HYPRE_StructVectorSetBoxValues(x, ilower, iupper, values); } } else if (myid == 1) { /* Set the vector coefficients over the gridpoints in my box */ { int ilower[2] = { 3, 1}; int iupper[2] = { 6, 4}; int nvalues = 16; /* 16 grid points */ double values[16]; for (i = 0; i < nvalues; i ++) values[i] = 1.0; HYPRE_StructVectorSetBoxValues(b, ilower, iupper, values); for (i = 0; i < nvalues; i ++) values[i] = 0.0; HYPRE_StructVectorSetBoxValues(x, ilower, iupper, values); } } /* This is a collective call finalizing the vector assembly. The vectors are now ``ready to be used'' */ HYPRE_StructVectorAssemble(b); HYPRE_StructVectorAssemble(x); } /* 5. Set up and use a solver (See the Reference Manual for descriptions of all of the options.) */ { /* Create an empty PCG Struct solver */ HYPRE_StructPCGCreate(MPI_COMM_WORLD, &solver); /* Set PCG parameters */ HYPRE_StructPCGSetTol(solver, 1.0e-06); HYPRE_StructPCGSetPrintLevel(solver, 2); HYPRE_StructPCGSetMaxIter(solver, 50); /* Use symmetric SMG as preconditioner */ HYPRE_StructSMGCreate(MPI_COMM_WORLD, &precond); HYPRE_StructSMGSetMaxIter(precond, 1); HYPRE_StructSMGSetTol(precond, 0.0); HYPRE_StructSMGSetZeroGuess(precond); HYPRE_StructSMGSetNumPreRelax(precond, 1); HYPRE_StructSMGSetNumPostRelax(precond, 1); /* Set preconditioner and solve */ HYPRE_StructPCGSetPrecond(solver, HYPRE_StructSMGSolve, HYPRE_StructSMGSetup, precond); HYPRE_StructPCGSetup(solver, A, b, x); HYPRE_StructPCGSolve(solver, A, b, x); } /* Free memory */ HYPRE_StructGridDestroy(grid); HYPRE_StructStencilDestroy(stencil); HYPRE_StructMatrixDestroy(A); HYPRE_StructVectorDestroy(b); HYPRE_StructVectorDestroy(x); HYPRE_StructPCGDestroy(solver); HYPRE_StructSMGDestroy(precond); /* Finalize MPI */ MPI_Finalize(); return (0); }