xref: /libCEED/tests/t536-operator-f.f90 (revision d9b786505a4dfcb66b2fcd9e3b61dd507168515d)
1b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
2b7ec98d8SJeremy L Thompson!
3b7ec98d8SJeremy L Thompson! Header with common subroutine
4b7ec98d8SJeremy L Thompson!
5b7ec98d8SJeremy L Thompson      include 't320-basis-f.h'
6752c3701SJeremy L Thompson!
7752c3701SJeremy L Thompson! Header with QFunctions
8752c3701SJeremy L Thompson!
9752c3701SJeremy L Thompson      include 't535-operator-f.h'
10b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
11b7ec98d8SJeremy L Thompson      program test
121f9a83abSJed Brown      implicit none
13ec3da8bcSJed Brown      include 'ceed/fortran.h'
14b7ec98d8SJeremy L Thompson
15b7ec98d8SJeremy L Thompson      integer ceed,err,i
1615910d16Sjeremylt      integer stridesu(3),stridesqd(3)
1715910d16Sjeremylt      integer erestrictx,erestrictu,erestrictui,erestrictqi
18b7ec98d8SJeremy L Thompson      integer bx,bu
19b7ec98d8SJeremy L Thompson      integer qf_setup_mass,qf_setup_diff,qf_apply
20b7ec98d8SJeremy L Thompson      integer op_setup_mass,op_setup_diff,op_apply
21b7ec98d8SJeremy L Thompson      integer qdata_mass,qdata_diff,x,a,u,v
22b7ec98d8SJeremy L Thompson      integer nelem,p,q,d
23b7ec98d8SJeremy L Thompson      integer row,col,offset
24b7ec98d8SJeremy L Thompson      parameter(nelem=12)
25b7ec98d8SJeremy L Thompson      parameter(p=6)
26b7ec98d8SJeremy L Thompson      parameter(q=4)
27b7ec98d8SJeremy L Thompson      parameter(d=2)
28b7ec98d8SJeremy L Thompson      integer ndofs,nqpts,nx,ny
29b7ec98d8SJeremy L Thompson      parameter(nx=3)
30b7ec98d8SJeremy L Thompson      parameter(ny=2)
31b7ec98d8SJeremy L Thompson      parameter(ndofs=(nx*2+1)*(ny*2+1))
32a8c028e3SNatalie Beams      parameter(nqpts=nelem*q)
33b7ec98d8SJeremy L Thompson      integer indx(nelem*p*p)
34b7ec98d8SJeremy L Thompson      real*8 arrx(d*ndofs),aa(nqpts),uu(ndofs),vv(ndofs),atrue(ndofs)
35b7ec98d8SJeremy L Thompson      integer*8 xoffset,aoffset,uoffset,voffset
36b7ec98d8SJeremy L Thompson
37b7ec98d8SJeremy L Thompson      real*8 qref(d*q)
38b7ec98d8SJeremy L Thompson      real*8 qweight(q)
39b7ec98d8SJeremy L Thompson      real*8 interp(p*q)
40b7ec98d8SJeremy L Thompson      real*8 grad(d*p*q)
411f9a83abSJed Brown      real*8 val
42b7ec98d8SJeremy L Thompson      character arg*32
43b7ec98d8SJeremy L Thompson
443bd813ffSjeremylt      external setup_mass,setup_diff,apply
45b7ec98d8SJeremy L Thompson
46b7ec98d8SJeremy L Thompson      call getarg(1,arg)
47b7ec98d8SJeremy L Thompson
48b7ec98d8SJeremy L Thompson      call ceedinit(trim(arg)//char(0),ceed,err)
49b7ec98d8SJeremy L Thompson
50b7ec98d8SJeremy L Thompson! DoF Coordinates
51b7ec98d8SJeremy L Thompson      do i=0,ndofs-1
52b7ec98d8SJeremy L Thompson        arrx(i+1)=mod(i,(nx*2+1))
53b7ec98d8SJeremy L Thompson        arrx(i+1)=arrx(i+1)*(1.d0/(nx*2.d0))
54b7ec98d8SJeremy L Thompson        val=(i/(nx*2+1))
55b7ec98d8SJeremy L Thompson        arrx(i+1+ndofs)=val*(1.d0/(ny*2.d0))
56b7ec98d8SJeremy L Thompson      enddo
57b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,d*ndofs,x,err)
58b7ec98d8SJeremy L Thompson      xoffset=0
59b7ec98d8SJeremy L Thompson      call ceedvectorsetarray(x,ceed_mem_host,ceed_use_pointer,arrx,xoffset,err)
60b7ec98d8SJeremy L Thompson
61b7ec98d8SJeremy L Thompson! Qdata Vector
62b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,nqpts,qdata_mass,err)
63b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,nqpts*d*(d+1)/2,qdata_diff,err)
64b7ec98d8SJeremy L Thompson
65b7ec98d8SJeremy L Thompson! Element Setup
66b7ec98d8SJeremy L Thompson      do i=0,5
67b7ec98d8SJeremy L Thompson        col=mod(i,nx)
68b7ec98d8SJeremy L Thompson        row=i/nx
69b7ec98d8SJeremy L Thompson        offset=col*2+row*(nx*2+1)*2
70b7ec98d8SJeremy L Thompson
71b7ec98d8SJeremy L Thompson        indx(i*2*p+1)=2+offset
72b7ec98d8SJeremy L Thompson        indx(i*2*p+2)=9+offset
73b7ec98d8SJeremy L Thompson        indx(i*2*p+3)=16+offset
74b7ec98d8SJeremy L Thompson        indx(i*2*p+4)=1+offset
75b7ec98d8SJeremy L Thompson        indx(i*2*p+5)=8+offset
76b7ec98d8SJeremy L Thompson        indx(i*2*p+6)=0+offset
77b7ec98d8SJeremy L Thompson
78b7ec98d8SJeremy L Thompson        indx(i*2*p+7)=14+offset
79b7ec98d8SJeremy L Thompson        indx(i*2*p+8)=7+offset
80b7ec98d8SJeremy L Thompson        indx(i*2*p+9)=0+offset
81b7ec98d8SJeremy L Thompson        indx(i*2*p+10)=15+offset
82b7ec98d8SJeremy L Thompson        indx(i*2*p+11)=8+offset
83b7ec98d8SJeremy L Thompson        indx(i*2*p+12)=16+offset
84b7ec98d8SJeremy L Thompson      enddo
85b7ec98d8SJeremy L Thompson
86b7ec98d8SJeremy L Thompson! Restrictions
87d979a051Sjeremylt      call ceedelemrestrictioncreate(ceed,nelem,p,d,ndofs,d*ndofs,&
887509a596Sjeremylt     & ceed_mem_host,ceed_use_pointer,indx,erestrictx,err)
89b7ec98d8SJeremy L Thompson
90d979a051Sjeremylt      call ceedelemrestrictioncreate(ceed,nelem,p,1,1,ndofs,&
917509a596Sjeremylt     & ceed_mem_host,ceed_use_pointer,indx,erestrictu,err)
927509a596Sjeremylt      stridesu=[1,q,q]
93d979a051Sjeremylt      call ceedelemrestrictioncreatestrided(ceed,nelem,q,1,nqpts,&
94d979a051Sjeremylt     & stridesu,erestrictui,err)
95b7ec98d8SJeremy L Thompson
967509a596Sjeremylt      stridesqd=[1,q,q*d*(d+1)/2]
97d979a051Sjeremylt      call ceedelemrestrictioncreatestrided(ceed,nelem,q,d*(d+1)/2,&
98d979a051Sjeremylt     & d*(d+1)/2*nqpts,stridesqd,erestrictqi,err)
99b7ec98d8SJeremy L Thompson
100b7ec98d8SJeremy L Thompson! Bases
101b7ec98d8SJeremy L Thompson      call buildmats(qref,qweight,interp,grad)
102b7ec98d8SJeremy L Thompson      call ceedbasiscreateh1(ceed,ceed_triangle,d,p,q,interp,grad,qref,qweight,&
103b7ec98d8SJeremy L Thompson     & bx,err)
104b7ec98d8SJeremy L Thompson      call buildmats(qref,qweight,interp,grad)
105b7ec98d8SJeremy L Thompson      call ceedbasiscreateh1(ceed,ceed_triangle,1,p,q,interp,grad,qref,qweight,&
106b7ec98d8SJeremy L Thompson     & bu,err)
107b7ec98d8SJeremy L Thompson
108b7ec98d8SJeremy L Thompson! QFunction - setup mass
109b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,setup_mass,&
110b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
111b7ec98d8SJeremy L Thompson     &//'t532-operator.h:setup_mass'//char(0),qf_setup_mass,err)
112b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup_mass,'dx',d*d,ceed_eval_grad,err)
113a61c78d6SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup_mass,'weight',1,ceed_eval_weight,err)
114b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_setup_mass,'qdata',1,ceed_eval_none,err)
115b7ec98d8SJeremy L Thompson
116b7ec98d8SJeremy L Thompson! Operator - setup mass
117442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_setup_mass,ceed_qfunction_none,&
118442e7f0bSjeremylt     & ceed_qfunction_none,op_setup_mass,err)
119b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_mass,'dx',erestrictx,&
120a8d32208Sjeremylt     & bx,ceed_vector_active,err)
121a61c78d6SJeremy L Thompson      call ceedoperatorsetfield(op_setup_mass,'weight',&
12215910d16Sjeremylt     & ceed_elemrestriction_none,bx,ceed_vector_none,err)
123b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_mass,'qdata',erestrictui,&
124*356036faSJeremy L Thompson     ceed_basis_none,ceed_vector_active,err)
125b7ec98d8SJeremy L Thompson
126b7ec98d8SJeremy L Thompson! QFunction - setup diff
127b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,setup_diff,&
128b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
129b7ec98d8SJeremy L Thompson     &//'t532-operator.h:setup_diff'//char(0),qf_setup_diff,err)
130b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup_diff,'dx',d*d,ceed_eval_grad,err)
131a61c78d6SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup_diff,'weight',1,ceed_eval_weight,err)
132b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_setup_diff,'qdata',&
133b7ec98d8SJeremy L Thompson     & d*(d+1)/2,ceed_eval_none,err)
134b7ec98d8SJeremy L Thompson
135b7ec98d8SJeremy L Thompson! Operator - setup diff
136442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_setup_diff,ceed_qfunction_none,&
137442e7f0bSjeremylt     & ceed_qfunction_none,op_setup_diff,err)
138b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_diff,'dx',erestrictx,&
139a8d32208Sjeremylt     & bx,ceed_vector_active,err)
140a61c78d6SJeremy L Thompson      call ceedoperatorsetfield(op_setup_diff,'weight',&
14115910d16Sjeremylt     & ceed_elemrestriction_none,bx,ceed_vector_none,err)
142b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_diff,'qdata',erestrictqi,&
143*356036faSJeremy L Thompson     ceed_basis_none,ceed_vector_active,err)
144b7ec98d8SJeremy L Thompson
145b7ec98d8SJeremy L Thompson! Apply Setup Operators
146b7ec98d8SJeremy L Thompson      call ceedoperatorapply(op_setup_mass,x,qdata_mass,&
147b7ec98d8SJeremy L Thompson     & ceed_request_immediate,err)
148b7ec98d8SJeremy L Thompson      call ceedoperatorapply(op_setup_diff,x,qdata_diff,&
149b7ec98d8SJeremy L Thompson     & ceed_request_immediate,err)
150b7ec98d8SJeremy L Thompson
151b7ec98d8SJeremy L Thompson! QFunction - apply
152b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,apply,&
153b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
154b7ec98d8SJeremy L Thompson     &//'t532-operator.h:apply'//char(0),qf_apply,err)
155b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_apply,'du',d,ceed_eval_grad,err)
156a61c78d6SJeremy L Thompson      call ceedqfunctionaddinput(qf_apply,'mass qdata',1,ceed_eval_none,err)
157a61c78d6SJeremy L Thompson      call ceedqfunctionaddinput(qf_apply,'diff qdata',&
158b7ec98d8SJeremy L Thompson     & d*(d+1)/2,ceed_eval_none,err)
159b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_apply,'u',1,ceed_eval_interp,err)
160b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_apply,'v',1,ceed_eval_interp,err)
161b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_apply,'dv',d,ceed_eval_grad,err)
162b7ec98d8SJeremy L Thompson
163b7ec98d8SJeremy L Thompson! Operator - apply
164442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_apply,ceed_qfunction_none,&
165442e7f0bSjeremylt     & ceed_qfunction_none,op_apply,err)
166b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'du',erestrictu,&
167a8d32208Sjeremylt     & bu,ceed_vector_active,err)
168a61c78d6SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'mass qdata',erestrictui,&
169*356036faSJeremy L Thompson     ceed_basis_none,qdata_mass,err)
170a61c78d6SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'diff qdata',erestrictqi,&
171*356036faSJeremy L Thompson     ceed_basis_none,qdata_diff,err)
172b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'u',erestrictu,&
173a8d32208Sjeremylt     & bu,ceed_vector_active,err)
174b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'v',erestrictu,&
175a8d32208Sjeremylt     & bu,ceed_vector_active,err)
176b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'dv',erestrictu,&
177a8d32208Sjeremylt     & bu,ceed_vector_active,err)
178b7ec98d8SJeremy L Thompson
179b7ec98d8SJeremy L Thompson! Assemble Diagonal
1802bba3ffaSJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,a,err)
18180ac2e43SJeremy L Thompson      call ceedoperatorlinearassemblediagonal(op_apply,a,&
182b7ec98d8SJeremy L Thompson     & ceed_request_immediate,err)
183b7ec98d8SJeremy L Thompson
184b7ec98d8SJeremy L Thompson! Manually assemble diagonal
185b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,u,err)
186b7ec98d8SJeremy L Thompson      call ceedvectorsetvalue(u,0.d0,err)
187b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,v,err)
188b7ec98d8SJeremy L Thompson      do i=1,ndofs
189b7ec98d8SJeremy L Thompson        call ceedvectorgetarray(u,ceed_mem_host,uu,uoffset,err)
190b7ec98d8SJeremy L Thompson        uu(i+uoffset)=1.d0
191b7ec98d8SJeremy L Thompson        if (i>1) then
192b7ec98d8SJeremy L Thompson          uu(i-1+uoffset)=0.d0
193b7ec98d8SJeremy L Thompson        endif
194b7ec98d8SJeremy L Thompson        call ceedvectorrestorearray(u,uu,uoffset,err)
195b7ec98d8SJeremy L Thompson
196b7ec98d8SJeremy L Thompson        call ceedoperatorapply(op_apply,u,v,ceed_request_immediate,err)
197b7ec98d8SJeremy L Thompson
198b7ec98d8SJeremy L Thompson        call ceedvectorgetarrayread(v,ceed_mem_host,vv,voffset,err)
199b7ec98d8SJeremy L Thompson        atrue(i)=vv(voffset+i)
200b7ec98d8SJeremy L Thompson        call ceedvectorrestorearrayread(v,vv,voffset,err)
201b7ec98d8SJeremy L Thompson      enddo
202b7ec98d8SJeremy L Thompson
203b7ec98d8SJeremy L Thompson! Check Output
204b7ec98d8SJeremy L Thompson      call ceedvectorgetarrayread(a,ceed_mem_host,aa,aoffset,err)
205b7ec98d8SJeremy L Thompson      do i=1,ndofs
206b7ec98d8SJeremy L Thompson        if (abs(aa(aoffset+i)-atrue(i))>1.0d-14) then
207b7ec98d8SJeremy L Thompson! LCOV_EXCL_START
208b7ec98d8SJeremy L Thompson          write(*,*) '[',i,'] Error in assembly: ',aa(aoffset+i),' != ',&
209b7ec98d8SJeremy L Thompson     &      atrue(i)
210b7ec98d8SJeremy L Thompson! LCOV_EXCL_STOP
211b7ec98d8SJeremy L Thompson        endif
212b7ec98d8SJeremy L Thompson      enddo
213b7ec98d8SJeremy L Thompson      call ceedvectorrestorearrayread(a,aa,aoffset,err)
214b7ec98d8SJeremy L Thompson
215b7ec98d8SJeremy L Thompson! Cleanup
216b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_setup_mass,err)
217b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_setup_diff,err)
218b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_apply,err)
219b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_setup_mass,err)
220b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_setup_diff,err)
221b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_apply,err)
222b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictu,err)
223b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictx,err)
224b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictui,err)
225b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictqi,err)
226b7ec98d8SJeremy L Thompson      call ceedbasisdestroy(bu,err)
227b7ec98d8SJeremy L Thompson      call ceedbasisdestroy(bx,err)
228b7ec98d8SJeremy L Thompson      call ceedvectordestroy(x,err)
229b7ec98d8SJeremy L Thompson      call ceedvectordestroy(a,err)
230b7ec98d8SJeremy L Thompson      call ceedvectordestroy(u,err)
231b7ec98d8SJeremy L Thompson      call ceedvectordestroy(v,err)
232b7ec98d8SJeremy L Thompson      call ceedvectordestroy(qdata_mass,err)
233b7ec98d8SJeremy L Thompson      call ceedvectordestroy(qdata_diff,err)
234b7ec98d8SJeremy L Thompson      call ceeddestroy(ceed,err)
235b7ec98d8SJeremy L Thompson      end
236b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
237