Actual source code: ex54f.F

petsc-master 2016-08-23
Report Typos and Errors
  1: !
  2: !   Description: Solve Ax=b.  A comes from an anisotropic 2D thermal problem with Q1 FEM on domain (-1,1)^2.
  3: !       Material conductivity given by tensor:
  4: !
  5: !       D = | 1 0       |
  6: !           | 0 epsilon |
  7: !
  8: !    rotated by angle 'theta' (-theta <90> in degrees) with anisotropic parameter 'epsilon' (-epsilon <0.0>).
  9: !    Blob right hand side centered at C (-blob_center C(1),C(2) <0,0>)
 10: !    Dirichlet BCs on y=-1 face.
 11: !
 12: !    -out_matlab will generate binary files for A,x,b and a ex54f.m file that reads them and plots them in matlab.
 13: !
 14: !    User can change anisotropic shape with function ex54_psi().  Negative theta will switch to a circular anisotropy.
 15: !
 16: !/*T
 17: !   Concepts: KSP^solving a system of linear equations
 18: !T*/
 19: ! -----------------------------------------------------------------------
 20:       program main
 21:       implicit none
 22: #include <petsc/finclude/petscsys.h>
 23: #include <petsc/finclude/petscvec.h>
 24: #include <petsc/finclude/petscmat.h>
 25: #include <petsc/finclude/petscksp.h>
 26: #include <petsc/finclude/petscpc.h>
 27: #include <petsc/finclude/petscviewer.h>
 28: #include <petsc/finclude/petscviewer.h90>
 29:       Vec              xvec,bvec,uvec
 30:       Mat              Amat
 31:       KSP              ksp
 32:       PetscErrorCode   ierr
 33:       PetscViewer viewer
 34:       PetscInt qj,qi,ne,M,Istart,Iend,geq,ix
 35:       PetscInt ki,kj,lint,nel,ll,j1,i1,ndf,f4
 36:       PetscInt f2,f9,f6
 37:       PetscInt :: idx(4)
 38:       PetscBool  flg,out_matlab
 39:       PetscMPIInt size,rank
 40:       PetscScalar::ss(4,4),val
 41:       PetscReal::shp(3,9),sg(3,9)
 42:       PetscReal::thk,a1,a2
 43:       PetscReal, external :: ex54_psi
 44:       PetscReal::theta,eps,h,x,y,xsj
 45:       PetscReal::coord(2,4),dd(2,2),ev(3),blb(2)

 47:       common /ex54_theta/ theta
 48: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 49: !                 Beginning of program
 50: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 51:       call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 52:       call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
 53:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 54: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 55: !                 set parameters
 56: !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 57:       f4 = 4
 58:       f2 = 2
 59:       f9 = 9
 60:       f6 = 6
 61:       ne = 9
 62:       call PetscOptionsGetInt(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,      &
 63:      &                        '-ne',ne,flg,ierr)
 64:       h = 2.0/real(ne)
 65:       M = (ne+1)*(ne+1)
 66:       theta = 90.0
 67: !     theta is input in degrees
 68:       call PetscOptionsGetReal(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,     &
 69:      &                         '-theta',theta,flg,ierr)
 70:       theta = theta / 57.2957795
 71:       eps = 1.0
 72:       call PetscOptionsGetReal(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,     &
 73:      &                         '-epsilon',eps,flg,ierr)
 74:       ki = 2
 75:       call PetscOptionsGetRealArray(PETSC_NULL_OBJECT,                     &
 76:      &           PETSC_NULL_CHARACTER,'-blob_center',blb,ki,flg,ierr)
 77:       if ( .not. flg ) then
 78:          blb(1) = 0.0
 79:          blb(2) = 0.0
 80:       else if ( ki .ne. 2 ) then
 81:          print *, 'error: ', ki,                                            &
 82:      &        ' arguments read for -blob_center.  Needs to be two.'
 83:       endif
 84:       call PetscOptionsGetBool(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,      &
 85:      &                         '-out_matlab',out_matlab,flg,ierr)
 86:       if (.not.flg) out_matlab = PETSC_FALSE;

 88:       ev(1) = 1.0
 89:       ev(2) = eps*ev(1)
 90: !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 91: !     Compute the matrix and right-hand-side vector that define
 92: !     the linear system, Ax = b.
 93: !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 94: !  Create matrix.  When using MatCreate(), the matrix format can
 95: !  be specified at runtime.
 96:       call MatCreate(PETSC_COMM_WORLD,Amat,ierr)
 97:       call MatSetSizes( Amat,PETSC_DECIDE, PETSC_DECIDE, M, M, ierr )
 98:       if ( size == 1 ) then
 99:          call MatSetType( Amat, MATAIJ, ierr )
100:       else
101:          call MatSetType( Amat, MATMPIAIJ, ierr )
102:       endif
103:       call MatMPIAIJSetPreallocation(Amat,f9,PETSC_NULL_INTEGER,f6,                   &
104:      &     PETSC_NULL_INTEGER, ierr)
105:       call MatSetFromOptions( Amat, ierr )
106:       call MatSetUp( Amat, ierr )
107:       call MatGetOwnershipRange( Amat, Istart, Iend, ierr )
108: !  Create vectors.  Note that we form 1 vector from scratch and
109: !  then duplicate as needed.
110:       call MatCreateVecs( Amat, PETSC_NULL_OBJECT, xvec, ierr )
111:       call VecSetFromOptions( xvec, ierr )
112:       call VecDuplicate( xvec, bvec, ierr )
113:       call VecDuplicate( xvec, uvec, ierr )
114: !  Assemble matrix.
115: !   - Note that MatSetValues() uses 0-based row and column numbers
116: !     in Fortran as well as in C (as set here in the array "col").
117:       thk = 1.0              ! thickness
118:       nel = 4                   ! nodes per element (quad)
119:       ndf = 1
120:       call int2d(f2,sg)
121:       lint = 4
122:       ix = 0
123:       do geq=Istart,Iend-1,1
124:          qj = geq/(ne+1); qi = mod(geq,(ne+1))
125:          x = h*qi - 1.0; y = h*qj - 1.0 ! lower left corner (-1,-1)
126:          if ( qi < ne .and. qj < ne ) then
127:             coord(1,1) = x;   coord(2,1) = y
128:             coord(1,2) = x+h; coord(2,2) = y
129:             coord(1,3) = x+h; coord(2,3) = y+h
130:             coord(1,4) = x;   coord(2,4) = y+h
131: ! form stiff
132:             ss = 0.0
133:             do ll = 1,lint
134:                call shp2dquad(sg(1,ll),sg(2,ll),coord,shp,xsj,f2)
135:                xsj = xsj*sg(3,ll)*thk
136:                call thfx2d(ev,coord,shp,dd,f2,f2,f4,ex54_psi)
137:                j1 = 1
138:                do kj = 1,nel
139:                   a1 = (dd(1,1)*shp(1,kj) + dd(1,2)*shp(2,kj))*xsj
140:                   a2 = (dd(2,1)*shp(1,kj) + dd(2,2)*shp(2,kj))*xsj
141: !     Compute residual
142: !                  p(j1) = p(j1) - a1*gradt(1) - a2*gradt(2)
143: !     Compute tangent
144:                   i1 = 1
145:                   do ki = 1,nel
146:                      ss(i1,j1) = ss(i1,j1) + a1*shp(1,ki) + a2*shp(2,ki)
147:                      i1 = i1 + ndf
148:                   end do
149:                   j1 = j1 + ndf
150:                end do
151:             enddo

153:             idx(1) = geq; idx(2) = geq+1; idx(3) = geq+(ne+1)+1
154:             idx(4) = geq+(ne+1)
155:             if ( qj > 0 ) then
156:                call MatSetValues(Amat,f4,idx,f4,idx,ss,ADD_VALUES,ierr)
157:             else                !     a BC
158:                do ki=1,4,1
159:                   do kj=1,4,1
160:                      if (ki<3 .or. kj<3 ) then
161:                         if ( ki==kj ) then
162:                            ss(ki,kj) = .1*ss(ki,kj)
163:                         else
164:                            ss(ki,kj) = 0.0
165:                         endif
166:                      endif
167:                   enddo
168:                enddo
169:                call MatSetValues(Amat,f4,idx,f4,idx,ss,ADD_VALUES,ierr)
170:             endif               ! BC
171:          endif                  ! add element
172:          if ( qj > 0 ) then      ! set rhs

174:             val = h*h*exp(-100.*((x+h/2)-blb(1))**2)*                            &
175:      &           exp(-100*((y+h/2)-blb(2))**2)
176:             call VecSetValues(bvec,1,geq,val,INSERT_VALUES,ierr)
177:          endif
178:       enddo
179:       call MatAssemblyBegin(Amat,MAT_FINAL_ASSEMBLY,ierr)
180:       call MatAssemblyEnd(Amat,MAT_FINAL_ASSEMBLY,ierr)
181:       call VecAssemblyBegin(bvec,ierr)
182:       call VecAssemblyEnd(bvec,ierr)

184: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
185: !          Create the linear solver and set various options
186: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

188: !  Create linear solver context

190:       call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)

192: !  Set operators. Here the matrix that defines the linear system
193: !  also serves as the preconditioning matrix.

195:       call KSPSetOperators(ksp,Amat,Amat,ierr)

197: !  Set runtime options, e.g.,
198: !      -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
199: !  These options will override those specified above as long as
200: !  KSPSetFromOptions() is called _after_ any other customization
201: !  routines.

203:       call KSPSetFromOptions(ksp,ierr)

205: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
206: !                      Solve the linear system
207: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

209:       call KSPSolve(ksp,bvec,xvec,ierr)
210:       CHKERRQ(ierr)


213: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
214: !                      output
215: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
216:       if ( out_matlab ) then
217:          call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'Amat',                &
218:      &        FILE_MODE_WRITE,viewer,ierr)
219:          call MatView(Amat,viewer,ierr)
220:          call PetscViewerDestroy(viewer,ierr)

222:          call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'Bvec',                &
223:      &        FILE_MODE_WRITE,viewer,ierr)
224:          call VecView(bvec,viewer,ierr)
225:          call PetscViewerDestroy(viewer,ierr)

227:          call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'Xvec',                &
228:      &        FILE_MODE_WRITE,viewer,ierr)
229:          call VecView(xvec,viewer,ierr)
230:          call PetscViewerDestroy(viewer,ierr)

232:          call MatMult(Amat,xvec,uvec,ierr)
233:          val = -1.0
234:          call VecAXPY(uvec,val,bvec,ierr)
235:          call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'Rvec',                &
236:      &        FILE_MODE_WRITE,viewer,ierr)
237:          call VecView(uvec,viewer,ierr)
238:          call PetscViewerDestroy(viewer,ierr)

240:          if ( rank == 0 ) then
241:             open(1,file='ex54f.m', FORM='formatted')
242:             write (1,*) 'A = PetscBinaryRead(''Amat'');'
243:             write (1,*) '[m n] = size(A);'
244:             write (1,*) 'mm = sqrt(m);'
245:             write (1,*) 'b = PetscBinaryRead(''Bvec'');'
246:             write (1,*) 'x = PetscBinaryRead(''Xvec'');'
247:             write (1,*) 'r = PetscBinaryRead(''Rvec'');'
248:             write (1,*) 'bb = reshape(b,mm,mm);'
249:             write (1,*) 'xx = reshape(x,mm,mm);'
250:             write (1,*) 'rr = reshape(r,mm,mm);'
251: !            write (1,*) 'imagesc(bb')'
252: !            write (1,*) 'title('RHS'),'
253:             write (1,*) 'figure,'
254:             write (1,*) 'imagesc(xx'')'
255:             write (1,2002) eps,theta*57.2957795
256:             write (1,*) 'figure,'
257:             write (1,*) 'imagesc(rr'')'
258:             write (1,*) 'title(''Residual''),'
259:             close(1)
260:          endif
261:       endif
262:  2002 format('title(''Solution: esp='',d9.3,'', theta='',g8.3,''),')
263: !  Free work space.  All PETSc objects should be destroyed when they
264: !  are no longer needed.

266:       call VecDestroy(xvec,ierr)
267:       call VecDestroy(bvec,ierr)
268:       call VecDestroy(uvec,ierr)
269:       call MatDestroy(Amat,ierr)
270:       call KSPDestroy(ksp,ierr)
271:       call PetscFinalize(ierr)

273:       end

275: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
276: !     thfx2d - compute material tensor
277: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

279:       subroutine thfx2d(ev,xl,shp,dd,ndm,ndf,nel,dir)

281: !     Compute thermal gradient and flux

283:       implicit  none

285:       PetscInt   ndm,ndf,nel,i
286:       PetscReal ev(2),xl(ndm,nel),shp(3,*),dir
287:       PetscReal xx,yy,psi,cs,sn,c2,s2,dd(2,2)

289:       xx       = 0.0
290:       yy       = 0.0
291:       do i = 1,nel
292:         xx       = xx       + shp(3,i)*xl(1,i)
293:         yy       = yy       + shp(3,i)*xl(2,i)
294:       end do
295:       psi = dir(xx,yy)
296: !     Compute thermal flux
297:       cs  = cos(psi)
298:       sn  = sin(psi)
299:       c2  = cs*cs
300:       s2  = sn*sn
301:       cs  = cs*sn

303:       dd(1,1) = c2*ev(1) + s2*ev(2)
304:       dd(2,2) = s2*ev(1) + c2*ev(2)
305:       dd(1,2) = cs*(ev(1) - ev(2))
306:       dd(2,1) = dd(1,2)

308: !      flux(1) = -dd(1,1)*gradt(1) - dd(1,2)*gradt(2)
309: !      flux(2) = -dd(2,1)*gradt(1) - dd(2,2)*gradt(2)

311:       end

313: !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
314: !     shp2dquad - shape functions - compute derivatives w/r natural coords.
315: !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
316:        subroutine shp2dquad(s,t,xl,shp,xsj,ndm)
317: !-----[--.----+----.----+----.-----------------------------------------]
318: !      Purpose: Shape function routine for 4-node isoparametric quads
319: !
320: !      Inputs:
321: !         s,t       - Natural coordinates of point
322: !         xl(ndm,*) - Nodal coordinates for element
323: !         ndm       - Spatial dimension of mesh

325: !      Outputs:
326: !         shp(3,*)  - Shape functions and derivatives at point
327: !                     shp(1,i) = dN_i/dx  or dN_i/dxi_1
328: !                     shp(2,i) = dN_i/dy  or dN_i/dxi_2
329: !                     shp(3,i) = N_i
330: !         xsj       - Jacobian determinant at point
331: !-----[--.----+----.----+----.-----------------------------------------]
332:       implicit  none
333:       PetscInt  ndm
334:       PetscReal xo,xs,xt, yo,ys,yt, xsm,xsp,xtm
335:       PetscReal xtp, ysm,ysp,ytm,ytp
336:       PetscReal s,t, xsj,xsj1, sh,th,sp,tp,sm
337:       PetscReal tm, xl(ndm,4),shp(3,4)

339: !     Set up interpolations

341:       sh = 0.5*s
342:       th = 0.5*t
343:       sp = 0.5 + sh
344:       tp = 0.5 + th
345:       sm = 0.5 - sh
346:       tm = 0.5 - th
347:       shp(3,1) =   sm*tm
348:       shp(3,2) =   sp*tm
349:       shp(3,3) =   sp*tp
350:       shp(3,4) =   sm*tp

352: !     Set up natural coordinate functions (times 4)

354:       xo =  xl(1,1)-xl(1,2)+xl(1,3)-xl(1,4)
355:       xs = -xl(1,1)+xl(1,2)+xl(1,3)-xl(1,4) + xo*t
356:       xt = -xl(1,1)-xl(1,2)+xl(1,3)+xl(1,4) + xo*s
357:       yo =  xl(2,1)-xl(2,2)+xl(2,3)-xl(2,4)
358:       ys = -xl(2,1)+xl(2,2)+xl(2,3)-xl(2,4) + yo*t
359:       yt = -xl(2,1)-xl(2,2)+xl(2,3)+xl(2,4) + yo*s

361: !     Compute jacobian (times 16)

363:       xsj1 = xs*yt - xt*ys

365: !     Divide jacobian by 16 (multiply by .0625)

367:       xsj = 0.0625*xsj1
368:       if (xsj1.eq.0.0) then
369:          xsj1 = 1.0
370:       else
371:          xsj1 = 1.0/xsj1
372:       endif

374: !     Divide functions by jacobian

376:       xs  = (xs+xs)*xsj1
377:       xt  = (xt+xt)*xsj1
378:       ys  = (ys+ys)*xsj1
379:       yt  = (yt+yt)*xsj1

381: !     Multiply by interpolations

383:       ytm =  yt*tm
384:       ysm =  ys*sm
385:       ytp =  yt*tp
386:       ysp =  ys*sp
387:       xtm =  xt*tm
388:       xsm =  xs*sm
389:       xtp =  xt*tp
390:       xsp =  xs*sp

392: !     Compute shape functions

394:       shp(1,1) = - ytm+ysm
395:       shp(1,2) =   ytm+ysp
396:       shp(1,3) =   ytp-ysp
397:       shp(1,4) = - ytp-ysm
398:       shp(2,1) =   xtm-xsm
399:       shp(2,2) = - xtm-xsp
400:       shp(2,3) = - xtp+xsp
401:       shp(2,4) =   xtp+xsm

403:       end

405: !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
406: !     int2d
407: !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
408:       subroutine int2d(l,sg)
409: !-----[--.----+----.----+----.-----------------------------------------]
410: !     Purpose: Form Gauss points and weights for two dimensions

412: !     Inputs:
413: !     l       - Number of points/direction

415: !     Outputs:
416: !     lint    - Total number of points
417: !     sg(3,*) - Array of points and weights
418: !-----[--.----+----.----+----.-----------------------------------------]
419:       implicit  none
420:       PetscInt   l,i,lint,lr(9),lz(9)
421:       PetscReal    g,third,sg(3,*)
422:       data      lr/-1,1,1,-1,0,1,0,-1,0/,lz/-1,-1,1,1,-1,0,1,0,0/
423:       data      third / 0.3333333333333333 /

425: !     Set number of total points

427:       lint = l*l

429: !     2x2 integration
430:       g = sqrt(third)
431:       do i = 1,4
432:          sg(1,i) = g*lr(i)
433:          sg(2,i) = g*lz(i)
434:          sg(3,i) = 1.0
435:       end do

437:       end

439: !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
440: !     ex54_psi - anusotropic material direction
441: !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
442:       PetscReal function ex54_psi(x,y)
443:       implicit  none
444:       PetscReal x,y,theta
445:       common /ex54_theta/ theta
446:       ex54_psi = theta
447:       if ( theta < 0. ) then     ! circular
448:          if (y==0) then
449:             ex54_psi = 2.0*atan(1.0)
450:          else
451:             ex54_psi = atan(-x/y)
452:          endif
453:       endif
454:       end