00001 subroutine sourceterm_AHfinder(sou)
00002   use phys_constant, only : long
00003   use grid_parameter, only : nrg, ntg, npg
00004   use def_horizon, only : ahz
00005   use coordinate_grav_r, only : rg, hrg
00006   use def_metric, only : psi, alph, tfkij
00007   use interface_grgrad_4th_gridpoint_bhex
00008   use interface_grgrad_midpoint_r3rd_type0
00009   use interface_interpo_linear_type0
00010   use interface_interpo_linear_type0_2Dsurf
00011   use make_array_3d
00012   implicit none
00013   real(long), pointer :: sou(:,:)
00014   real(long), pointer :: dFrhdx_grid(:,:,:), dFrhdx(:,:,:)
00015   real(long), pointer :: dFrhdy_grid(:,:,:), dFrhdy(:,:,:)
00016   real(long), pointer :: dFrhdz_grid(:,:,:), dFrhdz(:,:,:)
00017   real(long), pointer :: Frh(:,:,:), dfn(:,:,:), dfn_grid(:,:,:)
00018   real(long), pointer :: fnc0(:,:,:), fnc2(:,:,:)
00019   real(long), external :: lagint_4th
00020   real(long) :: sgmu(3,3), aij(3,3)
00021   real(long) :: r4(4), f4(4)
00022   real(long) :: detr, dfnw, dfnw_grid, dlpfdf, pdfkabsab, psi2, psi4, 
00023                dfdx, dfdy, dfdz, dFrhdxgc, dFrhdygc, dFrhdzgc, &
00024                rgv, sxd, sxu, syd, syu, szd, szu, p2dfnw, term2
00025   integer :: ia, ib, ii, ipg, ir0, irg, irg0, itg
00026 
00027   call alloc_array3d(Frh ,0,nrg,0,ntg,0,npg)
00028   call alloc_array3d(dFrhdx_grid,0,nrg,0,ntg,0,npg)
00029   call alloc_array3d(dFrhdy_grid,0,nrg,0,ntg,0,npg)
00030   call alloc_array3d(dFrhdz_grid,0,nrg,0,ntg,0,npg)
00031   call alloc_array3d(dfn_grid,0,nrg,0,ntg,0,npg)
00032   call alloc_array3d(fnc0,0,nrg,0,ntg,0,npg)
00033   call alloc_array3d(dFrhdx,1,nrg,1,ntg,1,npg)
00034   call alloc_array3d(dFrhdy,1,nrg,1,ntg,1,npg)
00035   call alloc_array3d(dFrhdz,1,nrg,1,ntg,1,npg)
00036   call alloc_array3d(dfn,1,nrg,1,ntg,1,npg)
00037   call alloc_array3d(fnc2,1,nrg,1,ntg,1,npg)
00038 
00039 
00040   do ipg = 0, npg
00041     do itg = 0, ntg
00042       do irg = 0, nrg
00043         Frh(irg,itg,ipg) = rg(irg) - ahz(itg,ipg)
00044       end do
00045     end do
00046   end do
00047 
00048   do ipg = 0, npg
00049     do itg = 0, ntg
00050       do irg = 0, nrg
00051         call grgrad_4th_gridpoint_bhex(Frh,dfdx,dfdy,dfdz,irg,itg,ipg)
00052         dFrhdx_grid(irg,itg,ipg) = dfdx
00053         dFrhdy_grid(irg,itg,ipg) = dfdy
00054         dFrhdz_grid(irg,itg,ipg) = dfdz
00055         dfn_grid(irg,itg,ipg) = dsqrt(dabs(dfdx*dfdx + dfdy*dfdy + dfdz*dfdz))
00056         dfnw_grid = dfn_grid(irg,itg,ipg)
00057         psi4 = psi(irg,itg,ipg)**4
00058         fnc0(irg,itg,ipg) = dlog(psi4/dfnw_grid)
00059       end do
00060     end do
00061   end do
00062 
00063   do ipg = 1, npg
00064     do itg = 1, ntg
00065       do irg = 1, nrg
00066         call grgrad_midpoint_r3rd_type0(Frh,dfdx,dfdy,dfdz,irg,itg,ipg,'bh')
00067         dFrhdx(irg,itg,ipg) = dfdx
00068         dFrhdy(irg,itg,ipg) = dfdy
00069         dFrhdz(irg,itg,ipg) = dfdz
00070         dfn(irg,itg,ipg) = dsqrt(dabs(dfdx*dfdx + dfdy*dfdy + dfdz*dfdz))
00071         dfnw = dfn(irg,itg,ipg)
00072         call interpo_linear_type0(psi2,psi,irg,itg,ipg)
00073         psi2 = psi2**2
00074         fnc2(irg,itg,ipg) = psi2*dfnw
00075       end do
00076     end do
00077   end do
00078 
00079 
00080 
00081   do ipg = 1, npg
00082     do itg = 1, ntg
00083       call interpo_linear_type0_2Dsurf(rgv,ahz,itg,ipg)
00084       irg0 = 1
00085       do irg = 1, nrg-1
00086         detr = (hrg(irg+1)-rgv)*(hrg(irg)-rgv)
00087         if(detr <= 0.0d0) then
00088           irg0 = irg
00089           exit
00090         end if
00091       end do
00092       ir0 = min0(max0(irg0-1,1),nrg-3)
00093 
00094       do ii = 1, 4
00095         irg = ir0-1 + ii
00096         r4(ii) = hrg(irg)
00097 
00098         dFrhdxgc = dFrhdx(irg,itg,ipg)
00099         dFrhdygc = dFrhdy(irg,itg,ipg)
00100         dFrhdzgc = dFrhdz(irg,itg,ipg)
00101         call grgrad_midpoint_r3rd_type0(fnc0,dfdx,dfdy,dfdz,irg,itg,ipg,'bh')
00102         dlpfdf = dfdx*dFrhdxgc + dfdy*dFrhdygc + dfdz*dFrhdzgc
00103 
00104         dfnw = dfn(irg,itg,ipg)
00105         p2dfnw = fnc2(irg,itg,ipg)
00106         sxd = dFrhdxgc/dfnw
00107         syd = dFrhdygc/dfnw
00108         szd = dFrhdzgc/dfnw
00109         sxu = sxd ; syu = syd ; szu = szd
00110 
00111         sgmu(1,1) = 1.0d0 - sxu*sxu
00112         sgmu(1,2) =       - sxu*syu
00113         sgmu(1,3) =       - sxu*szu
00114         sgmu(2,1) =       - syu*sxu
00115         sgmu(2,2) = 1.0d0 - syu*syu
00116         sgmu(2,3) =       - syu*szu
00117         sgmu(3,1) =       - szu*sxu
00118         sgmu(3,2) =       - szu*syu
00119         sgmu(3,3) = 1.0d0 - szu*szu
00120 
00121         aij(1,1) = tfkij(irg,itg,ipg,1,1)
00122         aij(1,2) = tfkij(irg,itg,ipg,1,2)
00123         aij(1,3) = tfkij(irg,itg,ipg,1,3)
00124         aij(2,1) = tfkij(irg,itg,ipg,2,1)
00125         aij(2,2) = tfkij(irg,itg,ipg,2,2)
00126         aij(2,3) = tfkij(irg,itg,ipg,2,3)
00127         aij(3,1) = tfkij(irg,itg,ipg,3,1)
00128         aij(3,2) = tfkij(irg,itg,ipg,3,2)
00129         aij(3,3) = tfkij(irg,itg,ipg,3,3)
00130 
00131 
00132         term2 = 0.0d0
00133         do ib = 1,3
00134           do ia = 1,3
00135             term2 = term2 + aij(ia,ib)*sgmu(ia,ib)
00136           end do
00137         end do
00138         pdfkabsab = p2dfnw*term2
00139 
00140         f4(ii) = dlpfdf - pdfkabsab
00141 
00142       end do
00143 
00144       sou(itg,ipg) = rgv**2*lagint_4th(r4,f4,rgv)
00145 
00146     end do
00147   end do
00148 
00149   deallocate(Frh)
00150   deallocate(dFrhdx_grid)
00151   deallocate(dFrhdy_grid)
00152   deallocate(dFrhdz_grid)
00153   deallocate(dfn_grid)
00154   deallocate(fnc0)
00155   deallocate(dFrhdx)
00156   deallocate(dFrhdy)
00157   deallocate(dFrhdz)
00158   deallocate(dfn)
00159   deallocate(fnc2)
00160 end subroutine sourceterm_AHfinder