download the original source code.
  1 c     
  2 c     Example 12
  3 c     
  4 c     Interface:    Semi-Structured interface (SStruct)
  5 c     
  6 c     Compile with: make ex12f (may need to edit HYPRE_DIR in Makefile)
  7 c  
  8 c     Sample runs:  mpirun -np 2 ex12f
  9 c  
 10 c     Description: The grid layout is the same as ex1, but with nodal
 11 c     unknowns. The solver is PCG preconditioned with either PFMG or
 12 c     BoomerAMG, set with 'precond_id' below.
 13 c  
 14 c     We recommend viewing the Struct examples before viewing this and
 15 c     the other SStruct examples.  This is one of the simplest SStruct
 16 c     examples, used primarily to demonstrate how to set up
 17 c     non-cell-centered problems, and to demonstrate how easy it is to
 18 c     switch between structured solvers (PFMG) and solvers designed for
 19 c     more general settings (AMG).
 20 c  
 21 
 22       program ex12f
 23 
 24       implicit none
 25 
 26       include 'mpif.h'
 27       include 'HYPREf.h'
 28 
 29       integer    ierr
 30       integer    i, j, myid, num_procs
 31 
 32       integer*8  grid
 33       integer*8  graph
 34       integer*8  stencil
 35       integer*8  A
 36       integer*8  b
 37       integer*8  x
 38 
 39       integer    nparts
 40       integer    nvars
 41       integer    part
 42       integer    var
 43 
 44       integer    precond_id, object_type
 45 
 46       integer    ilower(2), iupper(2)
 47       integer    vartypes(1)
 48       integer    offsets(2,5)
 49       integer    ent
 50       integer    nentries, nvalues, stencil_indices(5)
 51 
 52       double precision  values(100), tol
 53 
 54 c     This comes from 'sstruct_mv/HYPRE_sstruct_mv.h'
 55       integer    HYPRE_SSTRUCT_VARIABLE_NODE
 56       parameter( HYPRE_SSTRUCT_VARIABLE_NODE = 1 )
 57 
 58       integer*8  sA
 59       integer*8  sb
 60       integer*8  sx
 61       integer*8  parA
 62       integer*8  parb
 63       integer*8  parx
 64       integer*8  solver
 65       integer*8  precond
 66 
 67       character*32  matfile
 68 
 69 c     We only have one part and one variable
 70       nparts = 1
 71       nvars  = 1
 72       part   = 0
 73       var    = 0
 74 
 75 c     Initialize MPI
 76       call MPI_Init(ierr)
 77       call MPI_Comm_rank(MPI_COMM_WORLD, myid, ierr)
 78       call MPI_Comm_size(MPI_COMM_WORLD, num_procs, ierr)
 79 
 80       if (num_procs .ne. 2) then
 81          if (myid .eq. 0) then
 82             print *, "Must run with 2 processors!"
 83             stop
 84          endif
 85       endif
 86 
 87 c     Set preconditioner id (PFMG = 1, BoomerAMG = 2)
 88       precond_id = 1
 89 
 90       if (precond_id .eq. 1) then
 91          object_type = HYPRE_STRUCT
 92       else if (precond_id .eq. 2) then
 93          object_type = HYPRE_PARCSR
 94       else
 95          if (myid .eq. 0) then
 96             print *, "Invalid solver!"
 97             stop
 98          endif
 99       endif
100 
101 c-----------------------------------------------------------------------
102 c     1. Set up the grid.  Here we use only one part.  Each processor
103 c     describes the piece of the grid that it owns.
104 c-----------------------------------------------------------------------
105 
106 c     Create an empty 2D grid object
107       call HYPRE_SStructGridCreate(MPI_COMM_WORLD, 2, nparts, grid,
108      +     ierr)
109 
110 c     Add boxes to the grid
111       if (myid .eq. 0) then
112          ilower(1) = -3
113          ilower(2) =  1
114          iupper(1) = -1
115          iupper(2) =  2
116          call HYPRE_SStructGridSetExtents(grid, part, ilower, iupper,
117      +        ierr)
118       else if (myid .eq. 1) then
119          ilower(1) =  0
120          ilower(2) =  1
121          iupper(1) =  2
122          iupper(2) =  4
123          call HYPRE_SStructGridSetExtents(grid, part, ilower, iupper,
124      +        ierr)
125       endif
126 
127 c     Set the variable type and number of variables on each part
128       vartypes(1) = HYPRE_SSTRUCT_VARIABLE_NODE
129       call HYPRE_SStructGridSetVariables(grid, part, nvars, vartypes,
130      +     ierr)
131 
132 c     This is a collective call finalizing the grid assembly
133       call HYPRE_SStructGridAssemble(grid, ierr)
134 
135 c-----------------------------------------------------------------------
136 c     2. Define the discretization stencil
137 c-----------------------------------------------------------------------
138 
139 c     Create an empty 2D, 5-pt stencil object
140       call HYPRE_SStructStencilCreate(2, 5, stencil, ierr)
141 
142 c     Define the geometry of the stencil.  Each represents a relative
143 c     offset (in the index space).
144       offsets(1,1) =  0
145       offsets(2,1) =  0
146       offsets(1,2) = -1
147       offsets(2,2) =  0
148       offsets(1,3) =  1
149       offsets(2,3) =  0
150       offsets(1,4) =  0
151       offsets(2,4) = -1
152       offsets(1,5) =  0
153       offsets(2,5) =  1
154 
155 c     Assign numerical values to the offsets so that we can easily refer
156 c     to them - the last argument indicates the variable for which we
157 c     are assigning this stencil
158       do ent = 1, 5
159          call HYPRE_SStructStencilSetEntry(stencil,
160      +        ent-1, offsets(1,ent), var, ierr)
161       enddo
162 
163 c-----------------------------------------------------------------------
164 c     3. Set up the Graph - this determines the non-zero structure of
165 c     the matrix and allows non-stencil relationships between the parts
166 c-----------------------------------------------------------------------
167 
168 c     Create the graph object
169       call HYPRE_SStructGraphCreate(MPI_COMM_WORLD, grid, graph, ierr)
170 
171 c     Now we need to tell the graph which stencil to use for each
172 c     variable on each part (we only have one variable and one part)
173       call HYPRE_SStructGraphSetStencil(graph, part, var, stencil, ierr)
174 
175 c     Here we could establish connections between parts if we had more
176 c     than one part using the graph. For example, we could use
177 c     HYPRE_GraphAddEntries() routine or HYPRE_GridSetNeighborPart()
178 
179 c     Assemble the graph
180       call HYPRE_SStructGraphAssemble(graph, ierr)
181 
182 c-----------------------------------------------------------------------
183 c     4. Set up a SStruct Matrix
184 c-----------------------------------------------------------------------
185 
186 c     Create an empty matrix object
187       call HYPRE_SStructMatrixCreate(MPI_COMM_WORLD, graph, A, ierr)
188 
189 c     Set the object type (by default HYPRE_SSTRUCT). This determines
190 c     the data structure used to store the matrix.  For PFMG we use
191 c     HYPRE_STRUCT, and for BoomerAMG we use HYPRE_PARCSR (set above).
192       call HYPRE_SStructMatrixSetObjectTyp(A, object_type, ierr)
193 
194 c     Get ready to set values
195       call HYPRE_SStructMatrixInitialize(A, ierr)
196 
197 c     Set the matrix coefficients.  Each processor assigns coefficients
198 c     for the boxes in the grid that it owns.  Note that the
199 c     coefficients associated with each stencil entry may vary from grid
200 c     point to grid point if desired.  Here, we first set the same
201 c     stencil entries for each grid point.  Then we make modifications
202 c     to grid points near the boundary.  Note that the ilower values are
203 c     different from those used in ex1 because of the way nodal
204 c     variables are referenced.  Also note that some of the stencil
205 c     values are set on both processor 0 and processor 1.  See the User
206 c     and Reference manuals for more details.
207 
208 c     Stencil entry labels correspond to the offsets defined above
209       do i = 1, 5
210          stencil_indices(i) = i-1
211       enddo
212       nentries = 5
213 
214       if (myid .eq. 0) then
215          ilower(1) = -4
216          ilower(2) =  0
217          iupper(1) = -1
218          iupper(2) =  2
219 c        12 grid points, each with 5 stencil entries
220          nvalues = 60 
221       else if (myid .eq. 1) then
222          ilower(1) = -1
223          ilower(2) =  0
224          iupper(1) =  2
225          iupper(2) =  4
226 c        12 grid points, each with 5 stencil entries
227          nvalues = 100 
228       endif
229 
230       do i = 1, nvalues, nentries
231          values(i) = 4.0
232          do j = 1, nentries-1
233             values(i+j) = -1.0
234          enddo
235       enddo
236 
237       call HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper,
238      +     var, nentries, stencil_indices, values, ierr)
239 
240 c     Set the coefficients reaching outside of the boundary to 0.  Note
241 c     that both ilower *and* iupper may be different from those in ex1.
242 
243       do i = 1, 5
244          values(i) = 0.0
245       enddo
246 
247       if (myid .eq. 0) then
248 
249 c        values below our box
250          ilower(1) = -4
251          ilower(2) =  0
252          iupper(1) = -1
253          iupper(2) =  0
254          stencil_indices(1) = 3
255          call HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper,
256      +        var, 1, stencil_indices, values, ierr)
257 c        values to the left of our box
258          ilower(1) = -4
259          ilower(2) =  0
260          iupper(1) = -4
261          iupper(2) =  2
262          stencil_indices(1) = 1
263          call HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper,
264      +        var, 1, stencil_indices, values, ierr)
265 c        values above our box
266          ilower(1) = -4
267          ilower(2) =  2
268          iupper(1) = -2
269          iupper(2) =  2
270          stencil_indices(1) = 4
271          call HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper,
272      +        var, 1, stencil_indices, values, ierr)
273 
274       else if (myid .eq. 1) then
275 
276 c        values below our box
277          ilower(1) = -1
278          ilower(2) =  0
279          iupper(1) =  2
280          iupper(2) =  0
281          stencil_indices(1) = 3
282          call HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper,
283      +        var, 1, stencil_indices, values, ierr)
284 c        values to the right of our box
285          ilower(1) =  2
286          ilower(2) =  0
287          iupper(1) =  2
288          iupper(2) =  4
289          stencil_indices(1) = 2
290          call HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper,
291      +        var, 1, stencil_indices, values, ierr)
292 c        values above our box
293          ilower(1) = -1
294          ilower(2) =  4
295          iupper(1) =  2
296          iupper(2) =  4
297          stencil_indices(1) = 4
298          call HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper,
299      +        var, 1, stencil_indices, values, ierr)
300 c        values to the left of our box
301 c        (that do not border the other box on proc. 0)
302          ilower(1) = -1
303          ilower(2) =  3
304          iupper(1) = -1
305          iupper(2) =  4
306          stencil_indices(1) = 1
307          call HYPRE_SStructMatrixSetBoxValues(A, part, ilower, iupper,
308      +        var, 1, stencil_indices, values, ierr)
309 
310       endif
311 
312 c     This is a collective call finalizing the matrix assembly
313       call HYPRE_SStructMatrixAssemble(A, ierr)
314 
315 c      matfile = 'ex12f.out'
316 c      matfile(10:10) = char(0)
317 c      call HYPRE_SStructMatrixPrint(matfile, A, 0, ierr)
318 
319 c     Create an empty vector object
320       call HYPRE_SStructVectorCreate(MPI_COMM_WORLD, grid, b, ierr)
321       call HYPRE_SStructVectorCreate(MPI_COMM_WORLD, grid, x, ierr)
322 
323 c     As with the matrix, set the appropriate object type for the vectors
324       call HYPRE_SStructVectorSetObjectTyp(b, object_type, ierr)
325       call HYPRE_SStructVectorSetObjectTyp(x, object_type, ierr)
326 
327 c     Indicate that the vector coefficients are ready to be set
328       call HYPRE_SStructVectorInitialize(b, ierr)
329       call HYPRE_SStructVectorInitialize(x, ierr)
330 
331 c     Set the vector coefficients.  Again, note that the ilower values
332 c     are different from those used in ex1, and some of the values are
333 c     set on both processors.
334 
335       if (myid .eq. 0) then
336 
337          ilower(1) = -4
338          ilower(2) =  0
339          iupper(1) = -1
340          iupper(2) =  2
341 
342          do i = 1, 12
343             values(i) = 1.0
344          enddo
345          call HYPRE_SStructVectorSetBoxValues(b, part, ilower, iupper,
346      +        var, values, ierr)
347          do i = 1, 12
348             values(i) = 0.0
349          enddo
350          call HYPRE_SStructVectorSetBoxValues(x, part, ilower, iupper,
351      +        var, values, ierr)
352 
353       else if (myid .eq. 1) then
354 
355          ilower(1) =  0
356          ilower(2) =  1
357          iupper(1) =  2
358          iupper(2) =  4
359 
360          do i = 1, 20
361             values(i) = 1.0
362          enddo
363          call HYPRE_SStructVectorSetBoxValues(b, part, ilower, iupper,
364      +        var, values, ierr)
365          do i = 1, 20
366             values(i) = 0.0
367          enddo
368          call HYPRE_SStructVectorSetBoxValues(x, part, ilower, iupper,
369      +        var, values, ierr)
370 
371       endif
372 
373 c     This is a collective call finalizing the vector assembly
374       call HYPRE_SStructVectorAssemble(b, ierr)
375       call HYPRE_SStructVectorAssemble(x, ierr)
376 
377 c-----------------------------------------------------------------------
378 c     6. Set up and use a solver (See the Reference Manual for
379 c     descriptions of all of the options.)
380 c-----------------------------------------------------------------------
381 
382       tol = 1.0E-6
383 
384       if (precond_id .eq. 1) then
385 
386 c        PFMG
387 
388 c        Because we are using a struct solver, we need to get the object
389 c        of the matrix and vectors to pass in to the struct solvers
390          call HYPRE_SStructMatrixGetObject(A, sA, ierr)
391          call HYPRE_SStructVectorGetObject(b, sb, ierr)
392          call HYPRE_SStructVectorGetObject(x, sx, ierr)
393 
394 c        Create an empty PCG Struct solver
395          call HYPRE_StructPCGCreate(MPI_COMM_WORLD, solver, ierr)
396 c        Set PCG parameters
397          call HYPRE_StructPCGSetTol(solver, tol, ierr)
398          call HYPRE_StructPCGSetPrintLevel(solver, 2, ierr)
399          call HYPRE_StructPCGSetMaxIter(solver, 50, ierr)
400 
401 c        Create the Struct PFMG solver for use as a preconditioner
402          call HYPRE_StructPFMGCreate(MPI_COMM_WORLD, precond, ierr)
403 c        Set PFMG parameters
404          call HYPRE_StructPFMGSetMaxIter(precond, 1, ierr)
405          call HYPRE_StructPFMGSetTol(precond, 0.0, ierr)
406          call HYPRE_StructPFMGSetZeroGuess(precond, ierr)
407          call HYPRE_StructPFMGSetNumPreRelax(precond, 2, ierr)
408          call HYPRE_StructPFMGSetNumPostRelax(precond, 2, ierr)
409 c        Non-Galerkin coarse grid (more efficient for this problem)
410          call HYPRE_StructPFMGSetRAPType(precond, 1, ierr)
411 c        R/B Gauss-Seidel
412          call HYPRE_StructPFMGSetRelaxType(precond, 2, ierr)
413 c        Skip relaxation on some levels (more efficient for this problem)
414          call HYPRE_StructPFMGSetSkipRelax(precond, 1, ierr)
415 c        Set preconditioner (PFMG = 1) and solve
416          call HYPRE_StructPCGSetPrecond(solver, 1, precond, ierr)
417          call HYPRE_StructPCGSetup(solver, sA, sb, sx, ierr)
418          call HYPRE_StructPCGSolve(solver, sA, sb, sx, ierr)
419 
420 c        Free memory
421          call HYPRE_StructPCGDestroy(solver, ierr)
422          call HYPRE_StructPFMGDestroy(precond, ierr)
423 
424       else if (precond_id .eq. 2) then
425 
426 c        BoomerAMG
427 
428 c        Because we are using a struct solver, we need to get the object
429 c        of the matrix and vectors to pass in to the struct solvers
430          call HYPRE_SStructMatrixGetObject(A, parA, ierr)
431          call HYPRE_SStructVectorGetObject(b, parb, ierr)
432          call HYPRE_SStructVectorGetObject(x, parx, ierr)
433 
434 c        Create an empty PCG Struct solver
435          call HYPRE_ParCSRPCGCreate(MPI_COMM_WORLD, solver, ierr)
436 c        Set PCG parameters
437          call HYPRE_ParCSRPCGSetTol(solver, tol, ierr)
438          call HYPRE_ParCSRPCGSetPrintLevel(solver, 2, ierr)
439          call HYPRE_ParCSRPCGSetMaxIter(solver, 50, ierr)
440 
441 c        Create the BoomerAMG solver for use as a preconditioner
442          call HYPRE_BoomerAMGCreate(precond, ierr)
443 c        Set BoomerAMG parameters
444          call HYPRE_BoomerAMGSetMaxIter(precond, 1, ierr)
445          call HYPRE_BoomerAMGSetTol(precond, 0.0, ierr)
446 c        Print amg solution info
447          call HYPRE_BoomerAMGSetPrintLevel(precond, 1, ierr)
448          call HYPRE_BoomerAMGSetCoarsenType(precond, 6, ierr)
449 c        Sym G.S./Jacobi hybrid
450          call HYPRE_BoomerAMGSetRelaxType(precond, 6, ierr)
451          call HYPRE_BoomerAMGSetNumSweeps(precond, 1, ierr)
452 c        Set preconditioner (BoomerAMG = 2) and solve
453          call HYPRE_ParCSRPCGSetPrecond(solver, 2, precond, ierr)
454          call HYPRE_ParCSRPCGSetup(solver, parA, parb, parx, ierr)
455          call HYPRE_ParCSRPCGSolve(solver, parA, parb, parx, ierr)
456 
457 c        Free memory
458          call HYPRE_ParCSRPCGDestroy(solver, ierr)
459          call HYPRE_BoomerAMGDestroy(precond, ierr)
460 
461       endif
462 
463 c     Free memory
464       call HYPRE_SStructGridDestroy(grid, ierr)
465       call HYPRE_SStructStencilDestroy(stencil, ierr)
466       call HYPRE_SStructGraphDestroy(graph, ierr)
467       call HYPRE_SStructMatrixDestroy(A, ierr)
468       call HYPRE_SStructVectorDestroy(b, ierr)
469       call HYPRE_SStructVectorDestroy(x, ierr)
470 
471 c     Finalize MPI
472       call MPI_Finalize(ierr)
473 
474       stop
475       end


syntax highlighted by Code2HTML, v. 0.9.1