xref: /libCEED/tests/t534-operator-f.f90 (revision d9b786505a4dfcb66b2fcd9e3b61dd507168515d)
1b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
2752c3701SJeremy L Thompson!
3752c3701SJeremy L Thompson! Header with QFunctions
4752c3701SJeremy L Thompson!
5752c3701SJeremy L Thompson      include 't534-operator-f.h'
6b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
7b7ec98d8SJeremy L Thompson      program test
81f9a83abSJed Brown      implicit none
9ec3da8bcSJed Brown      include 'ceed/fortran.h'
10b7ec98d8SJeremy L Thompson
11b7ec98d8SJeremy L Thompson      integer ceed,err,i,j,k
1215910d16Sjeremylt      integer stridesu(3),stridesqd(3)
1315910d16Sjeremylt      integer erestrictx,erestrictu,erestrictui,erestrictqi
14b7ec98d8SJeremy L Thompson      integer bx,bu
15b7ec98d8SJeremy L Thompson      integer qf_setup,qf_diff
16b7ec98d8SJeremy L Thompson      integer op_setup,op_diff
17b7ec98d8SJeremy L Thompson      integer qdata,x,a,u,v
18b7ec98d8SJeremy L Thompson      integer nelem,p,q,d
19b7ec98d8SJeremy L Thompson      integer row,col,offset
20b7ec98d8SJeremy L Thompson      parameter(nelem=6)
21b7ec98d8SJeremy L Thompson      parameter(p=3)
22b7ec98d8SJeremy L Thompson      parameter(q=4)
23b7ec98d8SJeremy L Thompson      parameter(d=2)
24b7ec98d8SJeremy L Thompson      integer ndofs,nqpts,nx,ny
25b7ec98d8SJeremy L Thompson      parameter(nx=3)
26b7ec98d8SJeremy L Thompson      parameter(ny=2)
27b7ec98d8SJeremy L Thompson      parameter(ndofs=(nx*2+1)*(ny*2+1))
28b7ec98d8SJeremy L Thompson      parameter(nqpts=nelem*q*q)
29b7ec98d8SJeremy L Thompson      integer indx(nelem*p*p)
30b7ec98d8SJeremy L Thompson      real*8 arrx(d*ndofs),aa(nqpts),uu(ndofs),vv(ndofs),atrue(ndofs)
31b7ec98d8SJeremy L Thompson      integer*8 xoffset,aoffset,uoffset,voffset
32b7ec98d8SJeremy L Thompson
33b7ec98d8SJeremy L Thompson      character arg*32
34b7ec98d8SJeremy L Thompson
35b7ec98d8SJeremy L Thompson      external setup,diff,diff_lin
36b7ec98d8SJeremy L Thompson
37b7ec98d8SJeremy L Thompson      call getarg(1,arg)
38b7ec98d8SJeremy L Thompson
39b7ec98d8SJeremy L Thompson      call ceedinit(trim(arg)//char(0),ceed,err)
40b7ec98d8SJeremy L Thompson
41b7ec98d8SJeremy L Thompson! DoF Coordinates
42b7ec98d8SJeremy L Thompson      do i=0,nx*2
43b7ec98d8SJeremy L Thompson        do j=0,ny*2
44b7ec98d8SJeremy L Thompson          arrx(i+j*(nx*2+1)+0*ndofs+1)=1.d0*i/(2*nx)
45b7ec98d8SJeremy L Thompson          arrx(i+j*(nx*2+1)+1*ndofs+1)=1.d0*j/(2*ny)
46b7ec98d8SJeremy L Thompson        enddo
47b7ec98d8SJeremy L Thompson      enddo
48b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,d*ndofs,x,err)
49b7ec98d8SJeremy L Thompson      xoffset=0
50b7ec98d8SJeremy L Thompson      call ceedvectorsetarray(x,ceed_mem_host,ceed_use_pointer,arrx,xoffset,err)
51b7ec98d8SJeremy L Thompson
52b7ec98d8SJeremy L Thompson! Qdata Vector
53b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,nqpts*d*(d+1)/2,qdata,err)
54b7ec98d8SJeremy L Thompson
55b7ec98d8SJeremy L Thompson! Element Setup
56b7ec98d8SJeremy L Thompson      do i=0,nelem-1
57b7ec98d8SJeremy L Thompson        col=mod(i,nx)
58b7ec98d8SJeremy L Thompson        row=i/nx
59b7ec98d8SJeremy L Thompson        offset=col*(p-1)+row*(nx*2+1)*(p-1)
60b7ec98d8SJeremy L Thompson        do j=0,p-1
61b7ec98d8SJeremy L Thompson          do k=0,p-1
62b7ec98d8SJeremy L Thompson            indx(p*(p*i+k)+j+1)=offset+k*(nx*2+1)+j
63b7ec98d8SJeremy L Thompson          enddo
64b7ec98d8SJeremy L Thompson        enddo
65b7ec98d8SJeremy L Thompson      enddo
66b7ec98d8SJeremy L Thompson
67b7ec98d8SJeremy L Thompson! Restrictions
68d979a051Sjeremylt      call ceedelemrestrictioncreate(ceed,nelem,p*p,d,ndofs,d*ndofs,&
69b7ec98d8SJeremy L Thompson     & ceed_mem_host,ceed_use_pointer,indx,erestrictx,err)
70b7ec98d8SJeremy L Thompson
71d979a051Sjeremylt      call ceedelemrestrictioncreate(ceed,nelem,p*p,1,1,ndofs,&
72b7ec98d8SJeremy L Thompson     & ceed_mem_host,ceed_use_pointer,indx,erestrictu,err)
737509a596Sjeremylt      stridesu=[1,q*q,q*q]
74d979a051Sjeremylt      call ceedelemrestrictioncreatestrided(ceed,nelem,q*q,1,nqpts,&
75d979a051Sjeremylt     & stridesu,erestrictui,err)
76b7ec98d8SJeremy L Thompson
777509a596Sjeremylt      stridesqd=[1,q*q,q*q*d*(d+1)/2]
78d979a051Sjeremylt      call ceedelemrestrictioncreatestrided(ceed,nelem,q*q,d*(d+1)/2,&
79d979a051Sjeremylt     & d*(d+1)/2*nqpts,stridesqd,erestrictqi,err)
80b7ec98d8SJeremy L Thompson
81b7ec98d8SJeremy L Thompson! Bases
82b7ec98d8SJeremy L Thompson      call ceedbasiscreatetensorh1lagrange(ceed,d,d,p,q,ceed_gauss,&
83b7ec98d8SJeremy L Thompson     & bx,err)
84b7ec98d8SJeremy L Thompson      call ceedbasiscreatetensorh1lagrange(ceed,d,1,p,q,ceed_gauss,&
85b7ec98d8SJeremy L Thompson     & bu,err)
86b7ec98d8SJeremy L Thompson
87b7ec98d8SJeremy L Thompson! QFunction - setup
88b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,setup,&
89b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
90b7ec98d8SJeremy L Thompson     &//'t531-operator.h:setup'//char(0),qf_setup,err)
91b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup,'dx',d*d,ceed_eval_grad,err)
92a61c78d6SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup,'weight',1,ceed_eval_weight,err)
93b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_setup,'qdata',d*(d+1)/2,ceed_eval_none,err)
94b7ec98d8SJeremy L Thompson
95b7ec98d8SJeremy L Thompson! Operator - setup
96442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_setup,ceed_qfunction_none,&
97442e7f0bSjeremylt     & ceed_qfunction_none,op_setup,err)
98b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup,'dx',erestrictx,&
99a8d32208Sjeremylt     & bx,ceed_vector_active,err)
100a61c78d6SJeremy L Thompson      call ceedoperatorsetfield(op_setup,'weight',ceed_elemrestriction_none,&
101a8d32208Sjeremylt     & bx,ceed_vector_none,err)
102b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup,'qdata',erestrictqi,&
103*356036faSJeremy L Thompson     ceed_basis_none,ceed_vector_active,err)
104b7ec98d8SJeremy L Thompson
105b7ec98d8SJeremy L Thompson! Apply Setup Operator
106b7ec98d8SJeremy L Thompson      call ceedoperatorapply(op_setup,x,qdata,ceed_request_immediate,err)
107b7ec98d8SJeremy L Thompson
108b7ec98d8SJeremy L Thompson! QFunction - apply
109b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,diff,&
110b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
111b7ec98d8SJeremy L Thompson     &//'t531-operator.h:diff'//char(0),qf_diff,err)
112b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_diff,'du',d,ceed_eval_grad,err)
113b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_diff,'qdata',d*(d+1)/2,ceed_eval_none,err)
114b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_diff,'dv',d,ceed_eval_grad,err)
115b7ec98d8SJeremy L Thompson
116b7ec98d8SJeremy L Thompson! Operator - apply
117442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_diff,ceed_qfunction_none,&
118442e7f0bSjeremylt     & ceed_qfunction_none,op_diff,err)
119b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_diff,'du',erestrictu,&
120a8d32208Sjeremylt     & bu,ceed_vector_active,err)
121b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_diff,'qdata',erestrictqi,&
122*356036faSJeremy L Thompson     ceed_basis_none,qdata,err)
123b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_diff,'dv',erestrictu,&
124a8d32208Sjeremylt     & bu,ceed_vector_active,err)
125b7ec98d8SJeremy L Thompson
126b7ec98d8SJeremy L Thompson! Assemble Diagonal
1272bba3ffaSJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,a,err)
12880ac2e43SJeremy L Thompson      call ceedoperatorlinearassemblediagonal(op_diff,a,&
129b7ec98d8SJeremy L Thompson     & ceed_request_immediate,err)
130b7ec98d8SJeremy L Thompson
131b7ec98d8SJeremy L Thompson! Manually assemble diagonal
132b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,u,err)
133b7ec98d8SJeremy L Thompson      call ceedvectorsetvalue(u,0.d0,err)
134b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,v,err)
135b7ec98d8SJeremy L Thompson      do i=1,ndofs
136b7ec98d8SJeremy L Thompson        call ceedvectorgetarray(u,ceed_mem_host,uu,uoffset,err)
137b7ec98d8SJeremy L Thompson        uu(i+uoffset)=1.d0
138b7ec98d8SJeremy L Thompson        if (i>1) then
139b7ec98d8SJeremy L Thompson          uu(i-1+uoffset)=0.d0
140b7ec98d8SJeremy L Thompson        endif
141b7ec98d8SJeremy L Thompson        call ceedvectorrestorearray(u,uu,uoffset,err)
142b7ec98d8SJeremy L Thompson
143b7ec98d8SJeremy L Thompson        call ceedoperatorapply(op_diff,u,v,ceed_request_immediate,err)
144b7ec98d8SJeremy L Thompson
145b7ec98d8SJeremy L Thompson        call ceedvectorgetarrayread(v,ceed_mem_host,vv,voffset,err)
146b7ec98d8SJeremy L Thompson        atrue(i)=vv(voffset+i)
147b7ec98d8SJeremy L Thompson        call ceedvectorrestorearrayread(v,vv,voffset,err)
148b7ec98d8SJeremy L Thompson      enddo
149b7ec98d8SJeremy L Thompson
150b7ec98d8SJeremy L Thompson! Check Output
151b7ec98d8SJeremy L Thompson      call ceedvectorgetarrayread(a,ceed_mem_host,aa,aoffset,err)
152b7ec98d8SJeremy L Thompson      do i=1,ndofs
1534f694d2fSjeremylt        if (abs(aa(aoffset+i)-atrue(i))>1.0d-13) then
154b7ec98d8SJeremy L Thompson! LCOV_EXCL_START
155b7ec98d8SJeremy L Thompson          write(*,*) '[',i,'] Error in assembly: ',aa(aoffset+i),' != ',&
156b7ec98d8SJeremy L Thompson     &      atrue(i)
157b7ec98d8SJeremy L Thompson! LCOV_EXCL_STOP
158b7ec98d8SJeremy L Thompson        endif
159b7ec98d8SJeremy L Thompson      enddo
160b7ec98d8SJeremy L Thompson      call ceedvectorrestorearrayread(a,aa,aoffset,err)
161b7ec98d8SJeremy L Thompson
162b7ec98d8SJeremy L Thompson! Cleanup
163b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_setup,err)
164b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_diff,err)
165b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_setup,err)
166b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_diff,err)
167b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictu,err)
168b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictx,err)
169b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictui,err)
170b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictqi,err)
171b7ec98d8SJeremy L Thompson      call ceedbasisdestroy(bu,err)
172b7ec98d8SJeremy L Thompson      call ceedbasisdestroy(bx,err)
173b7ec98d8SJeremy L Thompson      call ceedvectordestroy(x,err)
174b7ec98d8SJeremy L Thompson      call ceedvectordestroy(a,err)
175b7ec98d8SJeremy L Thompson      call ceedvectordestroy(u,err)
176b7ec98d8SJeremy L Thompson      call ceedvectordestroy(v,err)
177b7ec98d8SJeremy L Thompson      call ceedvectordestroy(qdata,err)
178b7ec98d8SJeremy L Thompson      call ceeddestroy(ceed,err)
179b7ec98d8SJeremy L Thompson      end
180b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
181