Actual source code: ex54f.F90
petsc-3.10.5 2019-03-28
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: !! requires: !single
19: !T*/
22: ! -----------------------------------------------------------------------
23: program main
24: #include <petsc/finclude/petscksp.h>
25: use petscksp
26: implicit none
28: Vec xvec,bvec,uvec
29: Mat Amat
30: KSP ksp
31: PetscErrorCode ierr
32: PetscViewer viewer
33: PetscInt qj,qi,ne,M,Istart,Iend,geq,ix
34: PetscInt ki,kj,lint,nel,ll,j1,i1,ndf,f4
35: PetscInt f2,f9,f6,one
36: PetscInt :: idx(4)
37: PetscBool flg,out_matlab
38: PetscMPIInt size,rank
39: PetscScalar::ss(4,4),val
40: PetscReal::shp(3,9),sg(3,9)
41: PetscReal::thk,a1,a2
42: PetscReal, external :: ex54_psi
43: PetscReal::theta,eps,h,x,y,xsj
44: PetscReal::coord(2,4),dd(2,2),ev(3),blb(2)
46: common /ex54_theta/ theta
47: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48: ! Beginning of program
49: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
51: if (ierr .ne. 0) then
52: print*,'Unable to initialize PETSc'
53: stop
54: endif
55: call MPI_Comm_size(PETSC_COMM_WORLD,size,ierr)
56: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
57: one = 1
58: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
59: ! set parameters
60: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
61: f4 = 4
62: f2 = 2
63: f9 = 9
64: f6 = 6
65: ne = 9
66: call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-ne',ne,flg,ierr)
67: h = 2.0/real(ne)
68: M = (ne+1)*(ne+1)
69: theta = 90.0
70: ! theta is input in degrees
71: call PetscOptionsGetReal(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-theta',theta,flg,ierr)
72: theta = theta / 57.2957795
73: eps = 1.0
74: call PetscOptionsGetReal(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-epsilon',eps,flg,ierr)
75: ki = 2
76: call PetscOptionsGetRealArray(PETSC_NULL_OPTIONS,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,' arguments read for -blob_center. Needs to be two.'
82: endif
83: call PetscOptionsGetBool(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-out_matlab',out_matlab,flg,ierr)
84: if (.not.flg) out_matlab = PETSC_FALSE;
86: ev(1) = 1.0
87: ev(2) = eps*ev(1)
88: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
89: ! Compute the matrix and right-hand-side vector that define
90: ! the linear system, Ax = b.
91: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
92: ! Create matrix. When using MatCreate(), the matrix format can
93: ! be specified at runtime.
94: call MatCreate(PETSC_COMM_WORLD,Amat,ierr)
95: call MatSetSizes( Amat,PETSC_DECIDE, PETSC_DECIDE, M, M, ierr )
96: if ( size == 1 ) then
97: call MatSetType( Amat, MATAIJ, ierr )
98: else
99: call MatSetType( Amat, MATMPIAIJ, ierr )
100: endif
101: call MatMPIAIJSetPreallocation(Amat,f9,PETSC_NULL_INTEGER,f6,PETSC_NULL_INTEGER, ierr)
102: call MatSetFromOptions( Amat, ierr )
103: call MatSetUp( Amat, ierr )
104: call MatGetOwnershipRange( Amat, Istart, Iend, ierr )
105: ! Create vectors. Note that we form 1 vector from scratch and
106: ! then duplicate as needed.
107: call MatCreateVecs( Amat, PETSC_NULL_VEC, xvec, ierr )
108: call VecSetFromOptions( xvec, ierr )
109: call VecDuplicate( xvec, bvec, ierr )
110: call VecDuplicate( xvec, uvec, ierr )
111: ! Assemble matrix.
112: ! - Note that MatSetValues() uses 0-based row and column numbers
113: ! in Fortran as well as in C (as set here in the array "col").
114: thk = 1.0 ! thickness
115: nel = 4 ! nodes per element (quad)
116: ndf = 1
117: call int2d(f2,sg)
118: lint = 4
119: ix = 0
120: do geq=Istart,Iend-1,1
121: qj = geq/(ne+1); qi = mod(geq,(ne+1))
122: x = h*qi - 1.0; y = h*qj - 1.0 ! lower left corner (-1,-1)
123: if ( qi < ne .and. qj < ne ) then
124: coord(1,1) = x; coord(2,1) = y
125: coord(1,2) = x+h; coord(2,2) = y
126: coord(1,3) = x+h; coord(2,3) = y+h
127: coord(1,4) = x; coord(2,4) = y+h
128: ! form stiff
129: ss = 0.0
130: do ll = 1,lint
131: call shp2dquad(sg(1,ll),sg(2,ll),coord,shp,xsj,f2)
132: xsj = xsj*sg(3,ll)*thk
133: call thfx2d(ev,coord,shp,dd,f2,f2,f4,ex54_psi)
134: j1 = 1
135: do kj = 1,nel
136: a1 = (dd(1,1)*shp(1,kj) + dd(1,2)*shp(2,kj))*xsj
137: a2 = (dd(2,1)*shp(1,kj) + dd(2,2)*shp(2,kj))*xsj
138: ! Compute residual
139: ! p(j1) = p(j1) - a1*gradt(1) - a2*gradt(2)
140: ! Compute tangent
141: i1 = 1
142: do ki = 1,nel
143: ss(i1,j1) = ss(i1,j1) + a1*shp(1,ki) + a2*shp(2,ki)
144: i1 = i1 + ndf
145: end do
146: j1 = j1 + ndf
147: end do
148: enddo
150: idx(1) = geq; idx(2) = geq+1; idx(3) = geq+(ne+1)+1
151: idx(4) = geq+(ne+1)
152: if ( qj > 0 ) then
153: call MatSetValues(Amat,f4,idx,f4,idx,ss,ADD_VALUES,ierr)
154: else ! a BC
155: do ki=1,4,1
156: do kj=1,4,1
157: if (ki<3 .or. kj<3 ) then
158: if ( ki==kj ) then
159: ss(ki,kj) = .1*ss(ki,kj)
160: else
161: ss(ki,kj) = 0.0
162: endif
163: endif
164: enddo
165: enddo
166: call MatSetValues(Amat,f4,idx,f4,idx,ss,ADD_VALUES,ierr)
167: endif ! BC
168: endif ! add element
169: if ( qj > 0 ) then ! set rhs
170: val = h*h*exp(-100*((x+h/2)-blb(1))**2)*exp(-100*((y+h/2)-blb(2))**2)
171: call VecSetValues(bvec,one,geq,val,INSERT_VALUES,ierr)
172: endif
173: enddo
174: call MatAssemblyBegin(Amat,MAT_FINAL_ASSEMBLY,ierr)
175: call MatAssemblyEnd(Amat,MAT_FINAL_ASSEMBLY,ierr)
176: call VecAssemblyBegin(bvec,ierr)
177: call VecAssemblyEnd(bvec,ierr)
179: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180: ! Create the linear solver and set various options
181: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
183: ! Create linear solver context
185: call KSPCreate(PETSC_COMM_WORLD,ksp,ierr)
187: ! Set operators. Here the matrix that defines the linear system
188: ! also serves as the preconditioning matrix.
190: call KSPSetOperators(ksp,Amat,Amat,ierr)
192: ! Set runtime options, e.g.,
193: ! -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
194: ! These options will override those specified above as long as
195: ! KSPSetFromOptions() is called _after_ any other customization
196: ! routines.
198: call KSPSetFromOptions(ksp,ierr)
200: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
201: ! Solve the linear system
202: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
204: call KSPSolve(ksp,bvec,xvec,ierr)
205: CHKERRA(ierr)
208: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
209: ! output
210: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
211: if ( out_matlab ) then
212: call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'Amat',FILE_MODE_WRITE,viewer,ierr)
213: call MatView(Amat,viewer,ierr)
214: call PetscViewerDestroy(viewer,ierr)
216: call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'Bvec',FILE_MODE_WRITE,viewer,ierr)
217: call VecView(bvec,viewer,ierr)
218: call PetscViewerDestroy(viewer,ierr)
220: call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'Xvec',FILE_MODE_WRITE,viewer,ierr)
221: call VecView(xvec,viewer,ierr)
222: call PetscViewerDestroy(viewer,ierr)
224: call MatMult(Amat,xvec,uvec,ierr)
225: val = -1.0
226: call VecAXPY(uvec,val,bvec,ierr)
227: call PetscViewerBinaryOpen(PETSC_COMM_WORLD,'Rvec',FILE_MODE_WRITE,viewer,ierr)
228: call VecView(uvec,viewer,ierr)
229: call PetscViewerDestroy(viewer,ierr)
231: if ( rank == 0 ) then
232: open(1,file='ex54f.m', FORM='formatted')
233: write (1,*) 'A = PetscBinaryRead(''Amat'');'
234: write (1,*) '[m n] = size(A);'
235: write (1,*) 'mm = sqrt(m);'
236: write (1,*) 'b = PetscBinaryRead(''Bvec'');'
237: write (1,*) 'x = PetscBinaryRead(''Xvec'');'
238: write (1,*) 'r = PetscBinaryRead(''Rvec'');'
239: write (1,*) 'bb = reshape(b,mm,mm);'
240: write (1,*) 'xx = reshape(x,mm,mm);'
241: write (1,*) 'rr = reshape(r,mm,mm);'
242: ! write (1,*) 'imagesc(bb')'
243: ! write (1,*) 'title('RHS'),'
244: write (1,*) 'figure,'
245: write (1,*) 'imagesc(xx'')'
246: write (1,2002) eps,theta*57.2957795
247: write (1,*) 'figure,'
248: write (1,*) 'imagesc(rr'')'
249: write (1,*) 'title(''Residual''),'
250: close(1)
251: endif
252: endif
253: 2002 format('title(''Solution: esp='',d9.3,'', theta='',g8.3,''),')
254: ! Free work space. All PETSc objects should be destroyed when they
255: ! are no longer needed.
257: call VecDestroy(xvec,ierr)
258: call VecDestroy(bvec,ierr)
259: call VecDestroy(uvec,ierr)
260: call MatDestroy(Amat,ierr)
261: call KSPDestroy(ksp,ierr)
262: call PetscFinalize(ierr)
264: end
266: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
267: ! thfx2d - compute material tensor
268: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
269: ! Compute thermal gradient and flux
271: subroutine thfx2d(ev,xl,shp,dd,ndm,ndf,nel,dir)
272: implicit none
274: PetscInt ndm,ndf,nel,i
275: PetscReal ev(2),xl(ndm,nel),shp(3,*),dir
276: PetscReal xx,yy,psi,cs,sn,c2,s2,dd(2,2)
278: xx = 0.0
279: yy = 0.0
280: do i = 1,nel
281: xx = xx + shp(3,i)*xl(1,i)
282: yy = yy + shp(3,i)*xl(2,i)
283: end do
284: psi = dir(xx,yy)
285: ! Compute thermal flux
286: cs = cos(psi)
287: sn = sin(psi)
288: c2 = cs*cs
289: s2 = sn*sn
290: cs = cs*sn
292: dd(1,1) = c2*ev(1) + s2*ev(2)
293: dd(2,2) = s2*ev(1) + c2*ev(2)
294: dd(1,2) = cs*(ev(1) - ev(2))
295: dd(2,1) = dd(1,2)
297: ! flux(1) = -dd(1,1)*gradt(1) - dd(1,2)*gradt(2)
298: ! flux(2) = -dd(2,1)*gradt(1) - dd(2,2)*gradt(2)
300: end
302: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
303: ! shp2dquad - shape functions - compute derivatives w/r natural coords.
304: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
305: subroutine shp2dquad(s,t,xl,shp,xsj,ndm)
306: !-----[--.----+----.----+----.-----------------------------------------]
307: ! Purpose: Shape function routine for 4-node isoparametric quads
308: !
309: ! Inputs:
310: ! s,t - Natural coordinates of point
311: ! xl(ndm,*) - Nodal coordinates for element
312: ! ndm - Spatial dimension of mesh
314: ! Outputs:
315: ! shp(3,*) - Shape functions and derivatives at point
316: ! shp(1,i) = dN_i/dx or dN_i/dxi_1
317: ! shp(2,i) = dN_i/dy or dN_i/dxi_2
318: ! shp(3,i) = N_i
319: ! xsj - Jacobian determinant at point
320: !-----[--.----+----.----+----.-----------------------------------------]
321: implicit none
322: PetscInt ndm
323: PetscReal xo,xs,xt, yo,ys,yt, xsm,xsp,xtm
324: PetscReal xtp, ysm,ysp,ytm,ytp
325: PetscReal s,t, xsj,xsj1, sh,th,sp,tp,sm
326: PetscReal tm, xl(ndm,4),shp(3,4)
328: ! Set up interpolations
330: sh = 0.5*s
331: th = 0.5*t
332: sp = 0.5 + sh
333: tp = 0.5 + th
334: sm = 0.5 - sh
335: tm = 0.5 - th
336: shp(3,1) = sm*tm
337: shp(3,2) = sp*tm
338: shp(3,3) = sp*tp
339: shp(3,4) = sm*tp
341: ! Set up natural coordinate functions (times 4)
343: xo = xl(1,1)-xl(1,2)+xl(1,3)-xl(1,4)
344: xs = -xl(1,1)+xl(1,2)+xl(1,3)-xl(1,4) + xo*t
345: xt = -xl(1,1)-xl(1,2)+xl(1,3)+xl(1,4) + xo*s
346: yo = xl(2,1)-xl(2,2)+xl(2,3)-xl(2,4)
347: ys = -xl(2,1)+xl(2,2)+xl(2,3)-xl(2,4) + yo*t
348: yt = -xl(2,1)-xl(2,2)+xl(2,3)+xl(2,4) + yo*s
350: ! Compute jacobian (times 16)
352: xsj1 = xs*yt - xt*ys
354: ! Divide jacobian by 16 (multiply by .0625)
356: xsj = 0.0625*xsj1
357: if (xsj1.eq.0.0) then
358: xsj1 = 1.0
359: else
360: xsj1 = 1.0/xsj1
361: endif
363: ! Divide functions by jacobian
365: xs = (xs+xs)*xsj1
366: xt = (xt+xt)*xsj1
367: ys = (ys+ys)*xsj1
368: yt = (yt+yt)*xsj1
370: ! Multiply by interpolations
372: ytm = yt*tm
373: ysm = ys*sm
374: ytp = yt*tp
375: ysp = ys*sp
376: xtm = xt*tm
377: xsm = xs*sm
378: xtp = xt*tp
379: xsp = xs*sp
381: ! Compute shape functions
383: shp(1,1) = - ytm+ysm
384: shp(1,2) = ytm+ysp
385: shp(1,3) = ytp-ysp
386: shp(1,4) = - ytp-ysm
387: shp(2,1) = xtm-xsm
388: shp(2,2) = - xtm-xsp
389: shp(2,3) = - xtp+xsp
390: shp(2,4) = xtp+xsm
392: end
394: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
395: ! int2d
396: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
397: subroutine int2d(l,sg)
398: !-----[--.----+----.----+----.-----------------------------------------]
399: ! Purpose: Form Gauss points and weights for two dimensions
401: ! Inputs:
402: ! l - Number of points/direction
404: ! Outputs:
405: ! lint - Total number of points
406: ! sg(3,*) - Array of points and weights
407: !-----[--.----+----.----+----.-----------------------------------------]
408: implicit none
409: PetscInt l,i,lint,lr(9),lz(9)
410: PetscReal g,third,sg(3,*)
411: data lr/-1,1,1,-1,0,1,0,-1,0/,lz/-1,-1,1,1,-1,0,1,0,0/
412: data third / 0.3333333333333333 /
414: ! Set number of total points
416: lint = l*l
418: ! 2x2 integration
419: g = sqrt(third)
420: do i = 1,4
421: sg(1,i) = g*lr(i)
422: sg(2,i) = g*lz(i)
423: sg(3,i) = 1.0
424: end do
426: end
428: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
429: ! ex54_psi - anusotropic material direction
430: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
431: PetscReal function ex54_psi(x,y)
432: implicit none
433: PetscReal x,y,theta
434: common /ex54_theta/ theta
435: ex54_psi = theta
436: if ( theta < 0. ) then ! circular
437: if (y==0) then
438: ex54_psi = 2.0*atan(1.0)
439: else
440: ex54_psi = atan(-x/y)
441: endif
442: endif
443: end
445: !
446: !/*TEST
447: !
448: ! build:
449: ! requires: !libpgf90
450: !
451: ! test:
452: ! nsize: 4
453: ! args: -ne 39 -theta 30.0 -epsilon 1.e-1 -blob_center 0.,0. -ksp_type cg -pc_type gamg -pc_gamg_type agg -pc_gamg_agg_nsmooths 1 -mg_levels_ksp_chebyshev_esteig 0,0.05,0,1.05 -mat_coarsen_type hem -pc_gamg_square_graph 0 -ksp_monitor_short -mg_levels_esteig_ksp_type cg
454: ! requires: !single
455: !
456: !TEST*/