xref: /libCEED/tests/t535-operator-f.f90 (revision 442e7f0bef50859918b48bfcea0f6ad2d729e89e)
1b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
2b7ec98d8SJeremy L Thompson      subroutine setup_mass(ctx,q,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,&
3b7ec98d8SJeremy L Thompson&           u14,u15,u16,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,&
4b7ec98d8SJeremy L Thompson&           v16,ierr)
5b7ec98d8SJeremy L Thompson      real*8 ctx
6b7ec98d8SJeremy L Thompson      real*8 u1(1)
7b7ec98d8SJeremy L Thompson      real*8 u2(1)
8b7ec98d8SJeremy L Thompson      real*8 v1(1)
9b7ec98d8SJeremy L Thompson      integer q,ierr
10b7ec98d8SJeremy L Thompson
11b7ec98d8SJeremy L Thompson      do i=1,q
12b7ec98d8SJeremy L Thompson        v1(i)=u2(i)*(u1(i+q*0)*u1(i+q*3)-u1(i+q*1)*u1(i+q*2))
13b7ec98d8SJeremy L Thompson      enddo
14b7ec98d8SJeremy L Thompson
15b7ec98d8SJeremy L Thompson      ierr=0
16b7ec98d8SJeremy L Thompson      end
17b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
18b7ec98d8SJeremy L Thompson      subroutine setup_diff(ctx,q,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,&
19b7ec98d8SJeremy L Thompson&           u14,u15,u16,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,&
20b7ec98d8SJeremy L Thompson&           v16,ierr)
21b7ec98d8SJeremy L Thompson      real*8 ctx
22b7ec98d8SJeremy L Thompson      real*8 u1(1)
23b7ec98d8SJeremy L Thompson      real*8 u2(1)
24b7ec98d8SJeremy L Thompson      real*8 v1(1)
25b7ec98d8SJeremy L Thompson      real*8 w
26b7ec98d8SJeremy L Thompson      integer q,ierr
27b7ec98d8SJeremy L Thompson
28b7ec98d8SJeremy L Thompson      do i=1,q
29b7ec98d8SJeremy L Thompson        w=u2(i)/(u1(i+q*0)*u1(i+q*3)-u1(i+q*1)*u1(i+q*2))
30b7ec98d8SJeremy L Thompson        v1(i+q*0)=w*(u1(i+q*2)*u1(i+q*2)+u1(i+q*3)*u1(i+q*3))
31b7ec98d8SJeremy L Thompson        v1(i+q*1)=w*(u1(i+q*0)*u1(i+q*0)+u1(i+q*1)*u1(i+q*1))
32b7ec98d8SJeremy L Thompson        v1(i+q*2)=-w*(u1(i+q*0)*u1(i+q*2)+u1(i+q*2)*u1(i+q*3))
33b7ec98d8SJeremy L Thompson      enddo
34b7ec98d8SJeremy L Thompson
35b7ec98d8SJeremy L Thompson      ierr=0
36b7ec98d8SJeremy L Thompson      end
37b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
38b7ec98d8SJeremy L Thompson      subroutine apply(ctx,q,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,&
39b7ec98d8SJeremy L Thompson&           u15,u16,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,ierr)
40b7ec98d8SJeremy L Thompson      real*8 ctx
41b7ec98d8SJeremy L Thompson      real*8 u1(1)
42b7ec98d8SJeremy L Thompson      real*8 u2(1)
43b7ec98d8SJeremy L Thompson      real*8 u3(1)
44b7ec98d8SJeremy L Thompson      real*8 u4(1)
45b7ec98d8SJeremy L Thompson      real*8 v1(1)
46b7ec98d8SJeremy L Thompson      real*8 v2(1)
47b7ec98d8SJeremy L Thompson      real*8 du0,du1
48b7ec98d8SJeremy L Thompson      integer q,ierr
49b7ec98d8SJeremy L Thompson
50b7ec98d8SJeremy L Thompson      do i=1,q
51b7ec98d8SJeremy L Thompson!       mass
52b7ec98d8SJeremy L Thompson        v1(i) = u2(i)*u4(i)
53b7ec98d8SJeremy L Thompson!       diff
54b7ec98d8SJeremy L Thompson        du0=u1(i+q*0)
55b7ec98d8SJeremy L Thompson        du1=u1(i+q*1)
56b7ec98d8SJeremy L Thompson        v2(i+q*0)=u3(i+q*0)*du0+u3(i+q*2)*du1
57b7ec98d8SJeremy L Thompson        v2(i+q*1)=u3(i+q*2)*du0+u3(i+q*1)*du1
58b7ec98d8SJeremy L Thompson      enddo
59b7ec98d8SJeremy L Thompson
60b7ec98d8SJeremy L Thompson      ierr=0
61b7ec98d8SJeremy L Thompson      end
62b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
63b7ec98d8SJeremy L Thompson      program test
64b7ec98d8SJeremy L Thompson
65b7ec98d8SJeremy L Thompson      include 'ceedf.h'
66b7ec98d8SJeremy L Thompson
67b7ec98d8SJeremy L Thompson      integer ceed,err,i,j,k
68b7ec98d8SJeremy L Thompson      integer erestrictx,erestrictu,erestrictxi,erestrictui,erestrictqi
69b7ec98d8SJeremy L Thompson      integer bx,bu
70b7ec98d8SJeremy L Thompson      integer qf_setup_mass,qf_setup_diff,qf_apply
71b7ec98d8SJeremy L Thompson      integer op_setup_mass,op_setup_diff,op_apply
72b7ec98d8SJeremy L Thompson      integer qdata_mass,qdata_diff,x,a,u,v
73b7ec98d8SJeremy L Thompson      integer nelem,p,q,d
74b7ec98d8SJeremy L Thompson      integer row,col,offset
75b7ec98d8SJeremy L Thompson      parameter(nelem=6)
76b7ec98d8SJeremy L Thompson      parameter(p=3)
77b7ec98d8SJeremy L Thompson      parameter(q=4)
78b7ec98d8SJeremy L Thompson      parameter(d=2)
79b7ec98d8SJeremy L Thompson      integer ndofs,nqpts,nx,ny
80b7ec98d8SJeremy L Thompson      parameter(nx=3)
81b7ec98d8SJeremy L Thompson      parameter(ny=2)
82b7ec98d8SJeremy L Thompson      parameter(ndofs=(nx*2+1)*(ny*2+1))
83b7ec98d8SJeremy L Thompson      parameter(nqpts=nelem*q*q)
84b7ec98d8SJeremy L Thompson      integer indx(nelem*p*p)
85b7ec98d8SJeremy L Thompson      real*8 arrx(d*ndofs),aa(nqpts),uu(ndofs),vv(ndofs),atrue(ndofs)
86b7ec98d8SJeremy L Thompson      integer*8 xoffset,aoffset,uoffset,voffset
87b7ec98d8SJeremy L Thompson
88b7ec98d8SJeremy L Thompson      character arg*32
89b7ec98d8SJeremy L Thompson
90b7ec98d8SJeremy L Thompson      external setup_mass,setup_diff,apply,apply_lin
91b7ec98d8SJeremy L Thompson
92b7ec98d8SJeremy L Thompson      call getarg(1,arg)
93b7ec98d8SJeremy L Thompson
94b7ec98d8SJeremy L Thompson      call ceedinit(trim(arg)//char(0),ceed,err)
95b7ec98d8SJeremy L Thompson
96b7ec98d8SJeremy L Thompson! DoF Coordinates
97b7ec98d8SJeremy L Thompson      do i=0,nx*2
98b7ec98d8SJeremy L Thompson        do j=0,ny*2
99b7ec98d8SJeremy L Thompson          arrx(i+j*(nx*2+1)+0*ndofs+1)=1.d0*i/(2*nx)
100b7ec98d8SJeremy L Thompson          arrx(i+j*(nx*2+1)+1*ndofs+1)=1.d0*j/(2*ny)
101b7ec98d8SJeremy L Thompson        enddo
102b7ec98d8SJeremy L Thompson      enddo
103b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,d*ndofs,x,err)
104b7ec98d8SJeremy L Thompson      xoffset=0
105b7ec98d8SJeremy L Thompson      call ceedvectorsetarray(x,ceed_mem_host,ceed_use_pointer,arrx,xoffset,err)
106b7ec98d8SJeremy L Thompson
107b7ec98d8SJeremy L Thompson! Qdata Vector
108b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,nqpts,qdata_mass,err)
109b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,nqpts*d*(d+1)/2,qdata_diff,err)
110b7ec98d8SJeremy L Thompson
111b7ec98d8SJeremy L Thompson! Element Setup
112b7ec98d8SJeremy L Thompson      do i=0,nelem-1
113b7ec98d8SJeremy L Thompson        col=mod(i,nx)
114b7ec98d8SJeremy L Thompson        row=i/nx
115b7ec98d8SJeremy L Thompson        offset=col*(p-1)+row*(nx*2+1)*(p-1)
116b7ec98d8SJeremy L Thompson        do j=0,p-1
117b7ec98d8SJeremy L Thompson          do k=0,p-1
118b7ec98d8SJeremy L Thompson            indx(p*(p*i+k)+j+1)=offset+k*(nx*2+1)+j
119b7ec98d8SJeremy L Thompson          enddo
120b7ec98d8SJeremy L Thompson        enddo
121b7ec98d8SJeremy L Thompson      enddo
122b7ec98d8SJeremy L Thompson
123b7ec98d8SJeremy L Thompson! Restrictions
124b7ec98d8SJeremy L Thompson      call ceedelemrestrictioncreate(ceed,nelem,p*p,ndofs,d,&
125b7ec98d8SJeremy L Thompson     & ceed_mem_host,ceed_use_pointer,indx,erestrictx,err)
126b7ec98d8SJeremy L Thompson      call ceedelemrestrictioncreateidentity(ceed,nelem,p*p,&
127b7ec98d8SJeremy L Thompson     & nelem*p*p,d,erestrictxi,err)
128b7ec98d8SJeremy L Thompson
129b7ec98d8SJeremy L Thompson      call ceedelemrestrictioncreate(ceed,nelem,p*p,ndofs,1,&
130b7ec98d8SJeremy L Thompson     & ceed_mem_host,ceed_use_pointer,indx,erestrictu,err)
131b7ec98d8SJeremy L Thompson      call ceedelemrestrictioncreateidentity(ceed,nelem,q*q,nqpts,&
132b7ec98d8SJeremy L Thompson     & 1,erestrictui,err)
133b7ec98d8SJeremy L Thompson
134b7ec98d8SJeremy L Thompson      call ceedelemrestrictioncreateidentity(ceed,nelem,q*q,nqpts,&
135b7ec98d8SJeremy L Thompson     & d*(d+1)/2,erestrictqi,err)
136b7ec98d8SJeremy L Thompson
137b7ec98d8SJeremy L Thompson! Bases
138b7ec98d8SJeremy L Thompson      call ceedbasiscreatetensorh1lagrange(ceed,d,d,p,q,ceed_gauss,bx,err)
139b7ec98d8SJeremy L Thompson      call ceedbasiscreatetensorh1lagrange(ceed,d,1,p,q,ceed_gauss,bu,err)
140b7ec98d8SJeremy L Thompson
141b7ec98d8SJeremy L Thompson! QFunction - setup mass
142b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,setup_mass,&
143b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
144b7ec98d8SJeremy L Thompson     &//'t532-operator.h:setup_mass'//char(0),qf_setup_mass,err)
145b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup_mass,'dx',d*d,ceed_eval_grad,err)
146b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup_mass,'_weight',1,ceed_eval_weight,err)
147b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_setup_mass,'qdata',1,ceed_eval_none,err)
148b7ec98d8SJeremy L Thompson
149b7ec98d8SJeremy L Thompson! Operator - setup mass
150*442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_setup_mass,ceed_qfunction_none,&
151*442e7f0bSjeremylt     & ceed_qfunction_none,op_setup_mass,err)
152b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_mass,'dx',erestrictx,&
153b7ec98d8SJeremy L Thompson     & ceed_notranspose,bx,ceed_vector_active,err)
154b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_mass,'_weight',erestrictxi,&
155b7ec98d8SJeremy L Thompson     & ceed_notranspose,bx,ceed_vector_none,err)
156b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_mass,'qdata',erestrictui,&
157b7ec98d8SJeremy L Thompson     & ceed_notranspose,ceed_basis_collocated,ceed_vector_active,err)
158b7ec98d8SJeremy L Thompson
159b7ec98d8SJeremy L Thompson! QFunction - setup diff
160b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,setup_diff,&
161b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
162b7ec98d8SJeremy L Thompson     &//'t532-operator.h:setup_diff'//char(0),qf_setup_diff,err)
163b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup_diff,'dx',d*d,ceed_eval_grad,err)
164b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup_diff,'_weight',1,ceed_eval_weight,err)
165b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_setup_diff,'qdata',&
166b7ec98d8SJeremy L Thompson     & d*(d+1)/2,ceed_eval_none,err)
167b7ec98d8SJeremy L Thompson
168b7ec98d8SJeremy L Thompson! Operator - setup diff
169*442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_setup_diff,ceed_qfunction_none,&
170*442e7f0bSjeremylt     & ceed_qfunction_none,op_setup_diff,err)
171b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_diff,'dx',erestrictx,&
172b7ec98d8SJeremy L Thompson     & ceed_notranspose,bx,ceed_vector_active,err)
173b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_diff,'_weight',erestrictxi,&
174b7ec98d8SJeremy L Thompson     & ceed_notranspose,bx,ceed_vector_none,err)
175b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup_diff,'qdata',erestrictqi,&
176b7ec98d8SJeremy L Thompson     & ceed_notranspose,ceed_basis_collocated,ceed_vector_active,err)
177b7ec98d8SJeremy L Thompson
178b7ec98d8SJeremy L Thompson! Apply Setup Operators
179b7ec98d8SJeremy L Thompson      call ceedoperatorapply(op_setup_mass,x,qdata_mass,&
180b7ec98d8SJeremy L Thompson     & ceed_request_immediate,err)
181b7ec98d8SJeremy L Thompson      call ceedoperatorapply(op_setup_diff,x,qdata_diff,&
182b7ec98d8SJeremy L Thompson     & ceed_request_immediate,err)
183b7ec98d8SJeremy L Thompson
184b7ec98d8SJeremy L Thompson! QFunction - apply
185b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,apply,&
186b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
187b7ec98d8SJeremy L Thompson     &//'t532-operator.h:apply'//char(0),qf_apply,err)
188b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_apply,'du',d,ceed_eval_grad,err)
189b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_apply,'qdata_mass',1,ceed_eval_none,err)
190b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_apply,'qdata_diff',&
191b7ec98d8SJeremy L Thompson     & d*(d+1)/2,ceed_eval_none,err)
192b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_apply,'u',1,ceed_eval_interp,err)
193b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_apply,'v',1,ceed_eval_interp,err)
194b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_apply,'dv',d,ceed_eval_grad,err)
195b7ec98d8SJeremy L Thompson
196b7ec98d8SJeremy L Thompson! Operator - apply
197*442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_apply,ceed_qfunction_none,&
198*442e7f0bSjeremylt     & ceed_qfunction_none,op_apply,err)
199b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'du',erestrictu,&
200b7ec98d8SJeremy L Thompson     & ceed_notranspose,bu,ceed_vector_active,err)
201b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'qdata_mass',erestrictui,&
202b7ec98d8SJeremy L Thompson     & ceed_notranspose,ceed_basis_collocated,qdata_mass,err)
203b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'qdata_diff',erestrictqi,&
204b7ec98d8SJeremy L Thompson     & ceed_notranspose,ceed_basis_collocated,qdata_diff,err)
205b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'u',erestrictu,&
206b7ec98d8SJeremy L Thompson     & ceed_notranspose,bu,ceed_vector_active,err)
207b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'v',erestrictu,&
208b7ec98d8SJeremy L Thompson     & ceed_notranspose,bu,ceed_vector_active,err)
209b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_apply,'dv',erestrictu,&
210b7ec98d8SJeremy L Thompson     & ceed_notranspose,bu,ceed_vector_active,err)
211b7ec98d8SJeremy L Thompson
212b7ec98d8SJeremy L Thompson! Assemble Diagonal
213b7ec98d8SJeremy L Thompson      call ceedoperatorassemblelineardiagonal(op_apply,a,&
214b7ec98d8SJeremy L Thompson     & ceed_request_immediate,err)
215b7ec98d8SJeremy L Thompson
216b7ec98d8SJeremy L Thompson! Manually assemble diagonal
217b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,u,err)
218b7ec98d8SJeremy L Thompson      call ceedvectorsetvalue(u,0.d0,err)
219b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,v,err)
220b7ec98d8SJeremy L Thompson      do i=1,ndofs
221b7ec98d8SJeremy L Thompson        call ceedvectorgetarray(u,ceed_mem_host,uu,uoffset,err)
222b7ec98d8SJeremy L Thompson        uu(i+uoffset)=1.d0
223b7ec98d8SJeremy L Thompson        if (i>1) then
224b7ec98d8SJeremy L Thompson          uu(i-1+uoffset)=0.d0
225b7ec98d8SJeremy L Thompson        endif
226b7ec98d8SJeremy L Thompson        call ceedvectorrestorearray(u,uu,uoffset,err)
227b7ec98d8SJeremy L Thompson
228b7ec98d8SJeremy L Thompson        call ceedoperatorapply(op_apply,u,v,ceed_request_immediate,err)
229b7ec98d8SJeremy L Thompson
230b7ec98d8SJeremy L Thompson        call ceedvectorgetarrayread(v,ceed_mem_host,vv,voffset,err)
231b7ec98d8SJeremy L Thompson        atrue(i)=vv(voffset+i)
232b7ec98d8SJeremy L Thompson        call ceedvectorrestorearrayread(v,vv,voffset,err)
233b7ec98d8SJeremy L Thompson      enddo
234b7ec98d8SJeremy L Thompson
235b7ec98d8SJeremy L Thompson! Check Output
236b7ec98d8SJeremy L Thompson      call ceedvectorgetarrayread(a,ceed_mem_host,aa,aoffset,err)
237b7ec98d8SJeremy L Thompson      do i=1,ndofs
238b7ec98d8SJeremy L Thompson        if (abs(aa(aoffset+i)-atrue(i))>1.0d-14) then
239b7ec98d8SJeremy L Thompson! LCOV_EXCL_START
240b7ec98d8SJeremy L Thompson          write(*,*) '[',i,'] Error in assembly: ',aa(aoffset+i),' != ',&
241b7ec98d8SJeremy L Thompson     &      atrue(i)
242b7ec98d8SJeremy L Thompson! LCOV_EXCL_STOP
243b7ec98d8SJeremy L Thompson        endif
244b7ec98d8SJeremy L Thompson      enddo
245b7ec98d8SJeremy L Thompson      call ceedvectorrestorearrayread(a,aa,aoffset,err)
246b7ec98d8SJeremy L Thompson
247b7ec98d8SJeremy L Thompson! Cleanup
248b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_setup_mass,err)
249b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_setup_diff,err)
250b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_apply,err)
251b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_setup_mass,err)
252b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_setup_diff,err)
253b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_apply,err)
254b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictu,err)
255b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictx,err)
256b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictxi,err)
257b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictui,err)
258b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictqi,err)
259b7ec98d8SJeremy L Thompson      call ceedbasisdestroy(bu,err)
260b7ec98d8SJeremy L Thompson      call ceedbasisdestroy(bx,err)
261b7ec98d8SJeremy L Thompson      call ceedvectordestroy(x,err)
262b7ec98d8SJeremy L Thompson      call ceedvectordestroy(a,err)
263b7ec98d8SJeremy L Thompson      call ceedvectordestroy(u,err)
264b7ec98d8SJeremy L Thompson      call ceedvectordestroy(v,err)
265b7ec98d8SJeremy L Thompson      call ceedvectordestroy(qdata_mass,err)
266b7ec98d8SJeremy L Thompson      call ceedvectordestroy(qdata_diff,err)
267b7ec98d8SJeremy L Thompson      call ceeddestroy(ceed,err)
268b7ec98d8SJeremy L Thompson      end
269b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
270