xref: /libCEED/tests/t534-operator-f.f90 (revision 2bba3ffaf13cc90ae8de830effb3b11a0650a2fc)
1b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
2b7ec98d8SJeremy L Thompson      subroutine setup(ctx,q,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,&
3b7ec98d8SJeremy L Thompson&           u15,u16,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,ierr)
4b7ec98d8SJeremy L Thompson      real*8 ctx
5b7ec98d8SJeremy L Thompson      real*8 u1(1)
6b7ec98d8SJeremy L Thompson      real*8 u2(1)
7b7ec98d8SJeremy L Thompson      real*8 v1(1)
8b7ec98d8SJeremy L Thompson      real*8 w
9b7ec98d8SJeremy L Thompson      integer q,ierr
10b7ec98d8SJeremy L Thompson
11b7ec98d8SJeremy L Thompson      do i=1,q
12b7ec98d8SJeremy L Thompson        w=u2(i)/(u1(i+q*0)*u1(i+q*3)-u1(i+q*1)*u1(i+q*2))
13b7ec98d8SJeremy L Thompson        v1(i+q*0)=w*(u1(i+q*2)*u1(i+q*2)+u1(i+q*3)*u1(i+q*3))
14b7ec98d8SJeremy L Thompson        v1(i+q*1)=-w*(u1(i+q*0)*u1(i+q*2)+u1(i+q*2)*u1(i+q*3))
15b7ec98d8SJeremy L Thompson        v1(i+q*2)=w*(u1(i+q*0)*u1(i+q*0)+u1(i+q*1)*u1(i+q*1))
16b7ec98d8SJeremy L Thompson      enddo
17b7ec98d8SJeremy L Thompson
18b7ec98d8SJeremy L Thompson      ierr=0
19b7ec98d8SJeremy L Thompson      end
20b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
21b7ec98d8SJeremy L Thompson      subroutine diff(ctx,q,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,&
22b7ec98d8SJeremy L Thompson&           u15,u16,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,ierr)
23b7ec98d8SJeremy L Thompson      real*8 ctx
24b7ec98d8SJeremy L Thompson      real*8 u1(1)
25b7ec98d8SJeremy L Thompson      real*8 u2(1)
26b7ec98d8SJeremy L Thompson      real*8 v1(1)
27b7ec98d8SJeremy L Thompson      real*8 du0,du1
28b7ec98d8SJeremy L Thompson      integer q,ierr
29b7ec98d8SJeremy L Thompson
30b7ec98d8SJeremy L Thompson      do i=1,q
31b7ec98d8SJeremy L Thompson        du0=u1(i+q*0)
32b7ec98d8SJeremy L Thompson        du1=u1(i+q*1)
335107b09fSJeremy L Thompson        v1(i+q*0)=u2(i+q*0)*du0+u2(i+q*2)*du1
345107b09fSJeremy L Thompson        v1(i+q*1)=u2(i+q*2)*du0+u2(i+q*1)*du1
35b7ec98d8SJeremy L Thompson      enddo
36b7ec98d8SJeremy L Thompson
37b7ec98d8SJeremy L Thompson      ierr=0
38b7ec98d8SJeremy L Thompson      end
39b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
40b7ec98d8SJeremy L Thompson      program test
411f9a83abSJed Brown      implicit none
42b7ec98d8SJeremy L Thompson      include 'ceedf.h'
43b7ec98d8SJeremy L Thompson
44b7ec98d8SJeremy L Thompson      integer ceed,err,i,j,k
4515910d16Sjeremylt      integer stridesu(3),stridesqd(3)
4615910d16Sjeremylt      integer erestrictx,erestrictu,erestrictui,erestrictqi
47b7ec98d8SJeremy L Thompson      integer bx,bu
48b7ec98d8SJeremy L Thompson      integer qf_setup,qf_diff
49b7ec98d8SJeremy L Thompson      integer op_setup,op_diff
50b7ec98d8SJeremy L Thompson      integer qdata,x,a,u,v
51b7ec98d8SJeremy L Thompson      integer nelem,p,q,d
52b7ec98d8SJeremy L Thompson      integer row,col,offset
53b7ec98d8SJeremy L Thompson      parameter(nelem=6)
54b7ec98d8SJeremy L Thompson      parameter(p=3)
55b7ec98d8SJeremy L Thompson      parameter(q=4)
56b7ec98d8SJeremy L Thompson      parameter(d=2)
57b7ec98d8SJeremy L Thompson      integer ndofs,nqpts,nx,ny
58b7ec98d8SJeremy L Thompson      parameter(nx=3)
59b7ec98d8SJeremy L Thompson      parameter(ny=2)
60b7ec98d8SJeremy L Thompson      parameter(ndofs=(nx*2+1)*(ny*2+1))
61b7ec98d8SJeremy L Thompson      parameter(nqpts=nelem*q*q)
62b7ec98d8SJeremy L Thompson      integer indx(nelem*p*p)
63b7ec98d8SJeremy L Thompson      real*8 arrx(d*ndofs),aa(nqpts),uu(ndofs),vv(ndofs),atrue(ndofs)
64b7ec98d8SJeremy L Thompson      integer*8 xoffset,aoffset,uoffset,voffset
65b7ec98d8SJeremy L Thompson
66b7ec98d8SJeremy L Thompson      character arg*32
67b7ec98d8SJeremy L Thompson
68b7ec98d8SJeremy L Thompson      external setup,diff,diff_lin
69b7ec98d8SJeremy L Thompson
70b7ec98d8SJeremy L Thompson      call getarg(1,arg)
71b7ec98d8SJeremy L Thompson
72b7ec98d8SJeremy L Thompson      call ceedinit(trim(arg)//char(0),ceed,err)
73b7ec98d8SJeremy L Thompson
74b7ec98d8SJeremy L Thompson! DoF Coordinates
75b7ec98d8SJeremy L Thompson      do i=0,nx*2
76b7ec98d8SJeremy L Thompson        do j=0,ny*2
77b7ec98d8SJeremy L Thompson          arrx(i+j*(nx*2+1)+0*ndofs+1)=1.d0*i/(2*nx)
78b7ec98d8SJeremy L Thompson          arrx(i+j*(nx*2+1)+1*ndofs+1)=1.d0*j/(2*ny)
79b7ec98d8SJeremy L Thompson        enddo
80b7ec98d8SJeremy L Thompson      enddo
81b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,d*ndofs,x,err)
82b7ec98d8SJeremy L Thompson      xoffset=0
83b7ec98d8SJeremy L Thompson      call ceedvectorsetarray(x,ceed_mem_host,ceed_use_pointer,arrx,xoffset,err)
84b7ec98d8SJeremy L Thompson
85b7ec98d8SJeremy L Thompson! Qdata Vector
86b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,nqpts*d*(d+1)/2,qdata,err)
87b7ec98d8SJeremy L Thompson
88b7ec98d8SJeremy L Thompson! Element Setup
89b7ec98d8SJeremy L Thompson      do i=0,nelem-1
90b7ec98d8SJeremy L Thompson        col=mod(i,nx)
91b7ec98d8SJeremy L Thompson        row=i/nx
92b7ec98d8SJeremy L Thompson        offset=col*(p-1)+row*(nx*2+1)*(p-1)
93b7ec98d8SJeremy L Thompson        do j=0,p-1
94b7ec98d8SJeremy L Thompson          do k=0,p-1
95b7ec98d8SJeremy L Thompson            indx(p*(p*i+k)+j+1)=offset+k*(nx*2+1)+j
96b7ec98d8SJeremy L Thompson          enddo
97b7ec98d8SJeremy L Thompson        enddo
98b7ec98d8SJeremy L Thompson      enddo
99b7ec98d8SJeremy L Thompson
100b7ec98d8SJeremy L Thompson! Restrictions
101d979a051Sjeremylt      call ceedelemrestrictioncreate(ceed,nelem,p*p,d,ndofs,d*ndofs,&
102b7ec98d8SJeremy L Thompson     & ceed_mem_host,ceed_use_pointer,indx,erestrictx,err)
103b7ec98d8SJeremy L Thompson
104d979a051Sjeremylt      call ceedelemrestrictioncreate(ceed,nelem,p*p,1,1,ndofs,&
105b7ec98d8SJeremy L Thompson     & ceed_mem_host,ceed_use_pointer,indx,erestrictu,err)
1067509a596Sjeremylt      stridesu=[1,q*q,q*q]
107d979a051Sjeremylt      call ceedelemrestrictioncreatestrided(ceed,nelem,q*q,1,nqpts,&
108d979a051Sjeremylt     & stridesu,erestrictui,err)
109b7ec98d8SJeremy L Thompson
1107509a596Sjeremylt      stridesqd=[1,q*q,q*q*d*(d+1)/2]
111d979a051Sjeremylt      call ceedelemrestrictioncreatestrided(ceed,nelem,q*q,d*(d+1)/2,&
112d979a051Sjeremylt     & d*(d+1)/2*nqpts,stridesqd,erestrictqi,err)
113b7ec98d8SJeremy L Thompson
114b7ec98d8SJeremy L Thompson! Bases
115b7ec98d8SJeremy L Thompson      call ceedbasiscreatetensorh1lagrange(ceed,d,d,p,q,ceed_gauss,&
116b7ec98d8SJeremy L Thompson     & bx,err)
117b7ec98d8SJeremy L Thompson      call ceedbasiscreatetensorh1lagrange(ceed,d,1,p,q,ceed_gauss,&
118b7ec98d8SJeremy L Thompson     & bu,err)
119b7ec98d8SJeremy L Thompson
120b7ec98d8SJeremy L Thompson! QFunction - setup
121b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,setup,&
122b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
123b7ec98d8SJeremy L Thompson     &//'t531-operator.h:setup'//char(0),qf_setup,err)
124b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup,'dx',d*d,ceed_eval_grad,err)
125b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_setup,'_weight',1,ceed_eval_weight,err)
126b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_setup,'qdata',d*(d+1)/2,ceed_eval_none,err)
127b7ec98d8SJeremy L Thompson
128b7ec98d8SJeremy L Thompson! Operator - setup
129442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_setup,ceed_qfunction_none,&
130442e7f0bSjeremylt     & ceed_qfunction_none,op_setup,err)
131b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup,'dx',erestrictx,&
132a8d32208Sjeremylt     & bx,ceed_vector_active,err)
13315910d16Sjeremylt      call ceedoperatorsetfield(op_setup,'_weight',ceed_elemrestriction_none,&
134a8d32208Sjeremylt     & bx,ceed_vector_none,err)
135b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_setup,'qdata',erestrictqi,&
136a8d32208Sjeremylt     & ceed_basis_collocated,ceed_vector_active,err)
137b7ec98d8SJeremy L Thompson
138b7ec98d8SJeremy L Thompson! Apply Setup Operator
139b7ec98d8SJeremy L Thompson      call ceedoperatorapply(op_setup,x,qdata,ceed_request_immediate,err)
140b7ec98d8SJeremy L Thompson
141b7ec98d8SJeremy L Thompson! QFunction - apply
142b7ec98d8SJeremy L Thompson      call ceedqfunctioncreateinterior(ceed,1,diff,&
143b7ec98d8SJeremy L Thompson     &SOURCE_DIR&
144b7ec98d8SJeremy L Thompson     &//'t531-operator.h:diff'//char(0),qf_diff,err)
145b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_diff,'du',d,ceed_eval_grad,err)
146b7ec98d8SJeremy L Thompson      call ceedqfunctionaddinput(qf_diff,'qdata',d*(d+1)/2,ceed_eval_none,err)
147b7ec98d8SJeremy L Thompson      call ceedqfunctionaddoutput(qf_diff,'dv',d,ceed_eval_grad,err)
148b7ec98d8SJeremy L Thompson
149b7ec98d8SJeremy L Thompson! Operator - apply
150442e7f0bSjeremylt      call ceedoperatorcreate(ceed,qf_diff,ceed_qfunction_none,&
151442e7f0bSjeremylt     & ceed_qfunction_none,op_diff,err)
152b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_diff,'du',erestrictu,&
153a8d32208Sjeremylt     & bu,ceed_vector_active,err)
154b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_diff,'qdata',erestrictqi,&
155a8d32208Sjeremylt     & ceed_basis_collocated,qdata,err)
156b7ec98d8SJeremy L Thompson      call ceedoperatorsetfield(op_diff,'dv',erestrictu,&
157a8d32208Sjeremylt     & bu,ceed_vector_active,err)
158b7ec98d8SJeremy L Thompson
159b7ec98d8SJeremy L Thompson! Assemble Diagonal
160*2bba3ffaSJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,a,err)
16180ac2e43SJeremy L Thompson      call ceedoperatorlinearassemblediagonal(op_diff,a,&
162b7ec98d8SJeremy L Thompson     & ceed_request_immediate,err)
163b7ec98d8SJeremy L Thompson
164b7ec98d8SJeremy L Thompson! Manually assemble diagonal
165b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,u,err)
166b7ec98d8SJeremy L Thompson      call ceedvectorsetvalue(u,0.d0,err)
167b7ec98d8SJeremy L Thompson      call ceedvectorcreate(ceed,ndofs,v,err)
168b7ec98d8SJeremy L Thompson      do i=1,ndofs
169b7ec98d8SJeremy L Thompson        call ceedvectorgetarray(u,ceed_mem_host,uu,uoffset,err)
170b7ec98d8SJeremy L Thompson        uu(i+uoffset)=1.d0
171b7ec98d8SJeremy L Thompson        if (i>1) then
172b7ec98d8SJeremy L Thompson          uu(i-1+uoffset)=0.d0
173b7ec98d8SJeremy L Thompson        endif
174b7ec98d8SJeremy L Thompson        call ceedvectorrestorearray(u,uu,uoffset,err)
175b7ec98d8SJeremy L Thompson
176b7ec98d8SJeremy L Thompson        call ceedoperatorapply(op_diff,u,v,ceed_request_immediate,err)
177b7ec98d8SJeremy L Thompson
178b7ec98d8SJeremy L Thompson        call ceedvectorgetarrayread(v,ceed_mem_host,vv,voffset,err)
179b7ec98d8SJeremy L Thompson        atrue(i)=vv(voffset+i)
180b7ec98d8SJeremy L Thompson        call ceedvectorrestorearrayread(v,vv,voffset,err)
181b7ec98d8SJeremy L Thompson      enddo
182b7ec98d8SJeremy L Thompson
183b7ec98d8SJeremy L Thompson! Check Output
184b7ec98d8SJeremy L Thompson      call ceedvectorgetarrayread(a,ceed_mem_host,aa,aoffset,err)
185b7ec98d8SJeremy L Thompson      do i=1,ndofs
1864f694d2fSjeremylt        if (abs(aa(aoffset+i)-atrue(i))>1.0d-13) then
187b7ec98d8SJeremy L Thompson! LCOV_EXCL_START
188b7ec98d8SJeremy L Thompson          write(*,*) '[',i,'] Error in assembly: ',aa(aoffset+i),' != ',&
189b7ec98d8SJeremy L Thompson     &      atrue(i)
190b7ec98d8SJeremy L Thompson! LCOV_EXCL_STOP
191b7ec98d8SJeremy L Thompson        endif
192b7ec98d8SJeremy L Thompson      enddo
193b7ec98d8SJeremy L Thompson      call ceedvectorrestorearrayread(a,aa,aoffset,err)
194b7ec98d8SJeremy L Thompson
195b7ec98d8SJeremy L Thompson! Cleanup
196b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_setup,err)
197b7ec98d8SJeremy L Thompson      call ceedqfunctiondestroy(qf_diff,err)
198b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_setup,err)
199b7ec98d8SJeremy L Thompson      call ceedoperatordestroy(op_diff,err)
200b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictu,err)
201b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictx,err)
202b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictui,err)
203b7ec98d8SJeremy L Thompson      call ceedelemrestrictiondestroy(erestrictqi,err)
204b7ec98d8SJeremy L Thompson      call ceedbasisdestroy(bu,err)
205b7ec98d8SJeremy L Thompson      call ceedbasisdestroy(bx,err)
206b7ec98d8SJeremy L Thompson      call ceedvectordestroy(x,err)
207b7ec98d8SJeremy L Thompson      call ceedvectordestroy(a,err)
208b7ec98d8SJeremy L Thompson      call ceedvectordestroy(u,err)
209b7ec98d8SJeremy L Thompson      call ceedvectordestroy(v,err)
210b7ec98d8SJeremy L Thompson      call ceedvectordestroy(qdata,err)
211b7ec98d8SJeremy L Thompson      call ceeddestroy(ceed,err)
212b7ec98d8SJeremy L Thompson      end
213b7ec98d8SJeremy L Thompson!-----------------------------------------------------------------------
214