mud34sp.f 14 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
c
c     file mud34sp.f
c
c     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
c     *                                                               *
c     *                  copyright (c) 2008 by UCAR                   *
c     *                                                               *
c     *       University Corporation for Atmospheric Research         *
c     *                                                               *
c     *                      all rights reserved                      *
c     *                                                               *
c     *                     MUDPACK  version 5.0.1                    *
c     *                                                               *
c     *                 A Fortran Package of Multigrid                *
c     *                                                               *
c     *                Subroutines and Example Programs               *
c     *                                                               *
c     *      for Solving Elliptic Partial Differential Equations      *
c     *                                                               *
c     *                             by                                *
c     *                                                               *
c     *                         John Adams                            *
c     *                                                               *
c     *                             of                                *
c     *                                                               *
c     *         the National Center for Atmospheric Research          *
c     *                                                               *
c     *                Boulder, Colorado  (80307)  U.S.A.             *
c     *                                                               *
c     *                   which is sponsored by                       *
c     *                                                               *
c     *              the National Science Foundation                  *
c     *                                                               *
c     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
c
c ... purpose
c
c     mud34sp attempts to improve the second order approximation generated
c     by mud3sp to a fourth order approximation using difference corrections
c     within multigrid cycling
c
c ... see documentation and test files provided in this distribution
c
c ... required MUDPACK files
c
c     mud3sp.f, mudcom.f
c
c
c
      subroutine mud34sp(work,phi,ierror)
      implicit none
      real phi(*),work(*)
      integer ierror
      integer intl,nxa,nxb,nyc,nyd,nze,nzf,ixp,jyq,kzr,iex,jey,kez,
     +nfx,nfy,nfz,iguess,maxcy,method,nwork,lwork,itero,
     +kcycle,iprer,ipost,intpol
      common/imud3sp/intl,nxa,nxb,nyc,nyd,nze,nzf,ixp,jyq,kzr,iex,jey,
     +kez,nfx,nfy,nfz,iguess,maxcy,method,nwork,lwork,itero,
     +kcycle,iprer,ipost,intpol
      integer nx,ny,nz
      ierror = 0
      nx = nfx
      ny = nfy
      nz = nfz
      if (min0(nx,ny,nz).lt.6) then
      ierror = 30
      return
      end if
      call md34sp(nx,ny,nz,phi,work)
      end

      subroutine md34sp(nx,ny,nz,phi,wk)
      implicit none
      integer nx,ny,nz
      real phi(nx,ny,nz),wk(*)
      integer intl,nxa,nxb,nyc,nyd,nze,nzf,ixp,jyq,kzr,iex,jey,kez,
     +nfx,nfy,nfz,iguess,maxcy,method,nwork,lwork,itero,
     +kcycle,iprer,ipost,intpol
      common/imud3sp/intl,nxa,nxb,nyc,nyd,nze,nzf,ixp,jyq,kzr,iex,jey,
     +kez,nfx,nfy,nfz,iguess,maxcy,method,nwork,lwork,itero,
     +kcycle,iprer,ipost,intpol
      integer kpbgn,krbgn,kcxbgn,kcybgn,kczbgn,nxk,nyk,nzk,ngrid,
     +        klevel,kcur,kps
      common/mud3spc/kpbgn(50),krbgn(50),kcxbgn(50),kcybgn(50),
     +kczbgn(50),nxk(50),nyk(50),nzk(50),ngrid,klevel,kcur,kps
      integer ipf,irf,i,j,k,ijk,kb,ncx,ncy,ncz,irc,ic,jc,kc
      integer icx,icy,icz,jn,kn,ipc
      ipf = kpbgn(ngrid)
      irf = krbgn(ngrid)
      icx = kcxbgn(ngrid)
      icy = kcybgn(ngrid)
      icz = kczbgn(ngrid)
c
c     estimate truncation error and pass down (weighted) to all grid levels
c
      call tr3sp(nx,ny,nz,phi,wk(irf),wk(icx),wk(icy),wk(icz))
c
c     set phif in wk(ipf) to zero
c
      do k=0,nz+1
	do j=0,ny+1
	  do i=0,nx+1
	    ijk = (nx+2)*(j+k*(ny+2))+i
	    wk(ipf+ijk) = 0.0
	  end do
	end do
      end do
      do kb=2,ngrid
	k = ngrid-kb+1
	nx = nxk(k+1)
	ny = nyk(k+1)
	nz = nzk(k+1)
	ncx = nxk(k)
	ncy = nyk(k)
	ncz = nzk(k)
	ipf = kpbgn(k+1)
	ipc = kpbgn(k)
	irf = krbgn(k+1)
	irc = krbgn(k)
c
c     set phic in wk(ipc) to zero
c
	do kc=0,ncz+1
	  do jc=0,ncy+1
	    do ic=0,ncx+1
	      ijk = (kc*(ncy+2)+jc)*(ncx+2)+ic
	      wk(ipc+ijk) = 0.0
	    end do
	  end do
	end do
c
c      full weighting of truncation error from k+1 to k
c
	call res3(nx,ny,nz,wk(irf),ncx,ncy,ncz,wk(irc),nxa,nxb,
     +            nyc,nyd,nze,nzf)
      end do
c
c     execute one fmw(2,1) cycle for fourth order correction term
c
      kcycle = 2
      iprer = 2
      ipost = 1
      intpol = 3
      do k=1,ngrid-1
	kcur = k
	call kcymd3sp(wk)
	ipf = kpbgn(k+1)
	nx = nxk(k+1)
	ny = nyk(k+1)
	nz = nzk(k+1)
	ipc = kpbgn(k)
	ncx = nxk(k)
	ncy = nyk(k)
	ncz = nzk(k)
	call prolon3(ncx,ncy,ncz,wk(ipc),nx,ny,nz,wk(ipf),nxa,nxb,nyc,
     +               nyd,nze,nzf,intpol)
      end do
      kcur = ngrid
      call kcymd3sp(wk)
c
c     add fourth order correction term to solution
c
      do k=1,nz
	kn = k*(ny+2)*(nx+2)
	do j=1,ny
	  jn = j*(nx+2)
	  do i=1,nx
	    ijk = kn+jn+i+1
	    phi(i,j,k) = phi(i,j,k)+wk(ijk)
	  end do
	end do
      end do
      return
      end

      subroutine tr3sp(nx,ny,nz,u,r,cofx,cofy,cofz)
c
c     estimate truncation error using second order approximation in u
c
      implicit none
      integer nx,ny,nz
      real r(nx,ny,nz),u(nx,ny,nz)
      real cofx(nx,3),cofy(ny,3),cofz(nz,3)
      integer intl,nxa,nxb,nyc,nyd,nze,nzf,ixp,jyq,kzr,iex,jey,kez,
     +nfx,nfy,nfz,iguess,maxcy,method,nwork,lwork,itero,
     +kcycle,iprer,ipost,intpol
      common/imud3sp/intl,nxa,nxb,nyc,nyd,nze,nzf,ixp,jyq,kzr,iex,jey,
     +kez,nfx,nfy,nfz,iguess,maxcy,method,nwork,lwork,itero,
     +kcycle,iprer,ipost,intpol
      real xa,xb,yc,yd,ze,zf,tolmax,relmax
      real dlx,dly,dlz,dlxx,dlyy,dlzz,tdlx3,tdly3,tdlz3,dlx4,dly4,dlz4
      common/fmud3sp/xa,xb,yc,yd,ze,zf,tolmax,relmax
      common/pde3com/dlx,dly,dlz,dlxx,dlyy,dlzz,tdlx3,tdly3,tdlz3,
     +               dlx4,dly4,dlz4
      integer i,j,k,istart,ifinal,jstart,jfinal,kstart,kfinal
      real tx,ty,tz,cxx,cyy,czz,ux3,ux4,uy3,uy4,uz3,uz4,cx,cy,cz
      dlx = (xb-xa)/(nx-1)
      dly = (yd-yc)/(ny-1)
      dlz = (zf-ze)/(nz-1)
      dlxx = dlx*dlx
      dlyy = dly*dly
      dlzz = dlz*dlz
      tdlx3 = 2.*dlxx*dlx
      tdly3 = 2.*dlyy*dly
      tdlz3 = 2.*dlzz*dlz
      dlx4 = dlxx*dlxx
      dly4 = dlyy*dlyy
      dlz4 = dlzz*dlzz
c
c     set subscript limit to avoid specified (dirchlet) boundaries.
c
      istart=1
      ifinal=nx
      jstart=1
      jfinal=ny
      kstart=1
      kfinal=nz
      if(nxa.eq.1) istart=2
      if(nxb.eq.1) ifinal=nx-1
      if(nyc.eq.1) jstart=2
      if(nyd.eq.1) jfinal=ny-1
      if (nze.eq.1) kstart=2
      if (nzf.eq.1) kfinal=nz-1
      do k=1,nz
	do j=1,ny
	  do i=1,nx
	    r(i,j,k) = 0.0
	  end do
	end do
      end do
c
c     compute truncation on deep interior--this should vectorize
c
!$OMP PARALLEL DO PRIVATE(i,j,k,ux3,ux4,uy3,uy4,uz3,uz4)
!$OMP+PRIVATE(cxx,cx,cyy,cy,czz,cz,tx,ty,tz)
!$OMP+SHARED (r,u,cofx,cofy,cofz,nx,ny,nz,dlx,dly,dlz)
!$OMP+SHARED (dlxx,dlyy,dlzz,dlx4,dly4,dlz4,tdlx3,tdly3,tdlz3)
      do k=3,nz-2
	do j=3,ny-2
	  do i=3,nx-2
	    ux3=(-u(i-2,j,k)+2.0*u(i-1,j,k)-2.0*u(i+1,j,k)+u(i+2,j,k))
     +          /tdlx3
	    ux4=(u(i-2,j,k)-4.0*u(i-1,j,k)+6.0*u(i,j,k)-4.0*u(i+1,j,k)+
     +           u(i+2,j,k))/dlx4
	    uy3=(-u(i,j-2,k)+2.0*u(i,j-1,k)-2.0*u(i,j+1,k)+u(i,j+2,k))
     +          /tdly3
	    uy4=(u(i,j-2,k)-4.0*u(i,j-1,k)+6.0*u(i,j,k)-4.0*u(i,j+1,k)+
     +          u(i,j+2,k))/dly4
	    uz3=(-u(i,j,k-2)+2.0*u(i,j,k-1)-2.0*u(i,j,k+1)+u(i,j,k+2))
     +          /tdlz3
	    uz4=(u(i,j,k-2)-4.0*u(i,j,k-1)+6.0*u(i,j,k)-4.0*u(i,j,k+1)+
     +           u(i,j,k+2))/dlz4
c
c     reset continuous pde coefficients from discretized coefs
c
	    cxx = 0.5*dlxx*(cofx(i,1)+cofx(i,2))
	    cx = dlx*(cofx(i,2)-cofx(i,1))
	    cyy = 0.5*dlyy*(cofy(j,1)+cofy(j,2))
	    cy = dly*(cofy(j,2)-cofy(j,1))
	    czz = 0.5*dlzz*(cofz(k,1)+cofz(k,2))
	    cz = dlz*(cofz(k,2)-cofz(k,1))
	    tx = (cxx*ux4*0.5+cx*ux3)/6.0
	    ty = (cyy*uy4*0.5+cy*uy3)/6.0
	    tz = (czz*uz4*0.5+cz*uz3)/6.0
	    r(i,j,k) = dlxx*tx+dlyy*ty+dlzz*tz
	  end do
	end do
      end do
c
c     estimate truncation error at and near nonspecified boundaries
c
      do k=kstart,kfinal
	do i=istart,ifinal
	  do j=jstart,2
	    cxx = 0.5*dlxx*(cofx(i,1)+cofx(i,2))
	    cx = dlx*(cofx(i,2)-cofx(i,1))
	    cyy = 0.5*dlyy*(cofy(j,1)+cofy(j,2))
	    cy = dly*(cofy(j,2)-cofy(j,1))
	    czz = 0.5*dlzz*(cofz(k,1)+cofz(k,2))
	    cz = dlz*(cofz(k,2)-cofz(k,1))
	    call pde3(nx,ny,nz,u,i,j,k,ux3,ux4,uy3,uy4,uz3,uz4,
     +                nxa,nyc,nze)
	    tx = (cxx*ux4*0.5+cx*ux3)/6.0
	    ty = (cyy*uy4*0.5+cy*uy3)/6.0
	    tz = (czz*uz4*0.5+cz*uz3)/6.0
c
c     adjust truncation error terms at mixed boundaries
c
	    if(nxa.ne.0 .and. (i.eq.1 .or. i.eq.nx)) then
	      if (i.eq.1) then
		cxx = 0.5*dlxx*cofx(i,2)
		tx = cxx*(ux4*0.25+ux3/dlx)/3.0
	      else
		cxx = 0.5*dlxx*cofx(i,1)
		tx = cxx*(ux4*0.25-ux3/dlx)/3.0
	      end if
	    end if
	    if (nyc.ne.0 .and. j.eq.1) then
	      cyy = 0.5*dlyy*cofy(j,2)
	      ty=cyy*(uy4*0.25+uy3/dly)/3.0
	    end if
	    if(nze.ne.0 .and. (k.eq.1 .or. k.eq.nz)) then
	      if (k.eq.1) then
		czz = 0.5*dlzz*cofz(k,2)
		tz = czz*(uz4*0.25+uz3/dlz)/3.0
	      else
		czz = 0.5*dlzz*cofz(k,1)
		tz = czz*(uz4*0.25-uz3/dlz)/3.0
	      end if
	    end if
	    r(i,j,k)=dlxx*tx+dlyy*ty+dlzz*tz
	  end do
	  do j=ny-1,jfinal
	    cxx = 0.5*dlxx*(cofx(i,1)+cofx(i,2))
	    cx = dlx*(cofx(i,2)-cofx(i,1))
	    cyy = 0.5*dlyy*(cofy(j,1)+cofy(j,2))
	    cy = dly*(cofy(j,2)-cofy(j,1))
	    czz = 0.5*dlzz*(cofz(k,1)+cofz(k,2))
	    cz = dlz*(cofz(k,2)-cofz(k,1))
	    call pde3(nx,ny,nz,u,i,j,k,ux3,ux4,uy3,uy4,uz3,uz4,
     +                nxa,nyc,nze)
	    tx = (cxx*ux4*0.5+cx*ux3)/6.0
	    ty = (cyy*uy4*0.5+cy*uy3)/6.0
	    tz = (czz*uz4*0.5+cz*uz3)/6.0
c
c     adjust truncation error terms at mixed boundaries
c
	    if(nxa.ne.0 .and. (i.eq.1 .or. i.eq.nx)) then
	      if (i.eq.1) then
		cxx = 0.5*dlxx*cofx(i,2)
		tx = cxx*(ux4*0.25+ux3/dlx)/3.0
	      else
		cxx = 0.5*dlxx*cofx(i,1)
		tx = cxx*(ux4*0.25-ux3/dlx)/3.0
	      end if
	    end if
	    if (nyc.ne.0 .and. j.eq.ny) then
	      cyy = 0.5*dlyy*cofy(j,1)
	      ty=cyy*(uy4*0.25-uy3/dly)/3.0
	    end if
	    if(nze.ne.0 .and. (k.eq.1 .or. k.eq.nz)) then
	      if (k.eq.1) then
		czz = 0.5*dlzz*cofz(k,2)
		tz = czz*(uz4*0.25+uz3/dlz)/3.0
	      else
		czz = 0.5*dlzz*cofz(k,1)
		tz = czz*(uz4*0.25-uz3/dlz)/3.0
	      end if
	    end if
	    r(i,j,k)=dlxx*tx+dlyy*ty+dlzz*tz
	  end do
	end do
	do j=3,ny-2
	  do i=istart,2
	    cxx = 0.5*dlxx*(cofx(i,1)+cofx(i,2))
	    cx = dlx*(cofx(i,2)-cofx(i,1))
	    cyy = 0.5*dlyy*(cofy(j,1)+cofy(j,2))
	    cy = dly*(cofy(j,2)-cofy(j,1))
	    czz = 0.5*dlzz*(cofz(k,1)+cofz(k,2))
	    cz = dlz*(cofz(k,2)-cofz(k,1))
	    call pde3(nx,ny,nz,u,i,j,k,ux3,ux4,uy3,uy4,uz3,uz4,
     +                nxa,nyc,nze)
	    tx = (cxx*ux4*0.5+cx*ux3)/6.0
	    ty = (cyy*uy4*0.5+cy*uy3)/6.0
	    tz = (czz*uz4*0.5+cz*uz3)/6.0
c
c     adjust truncation error terms at mixed boundaries
c
	    if(nxa.ne.0 .and. i.eq.1) then
	      cxx = 0.5*dlxx*cofx(i,2)
	      tx = cxx*(ux4*0.25+ux3/dlx)/3.0
	    end if
	    if(nze.ne.0 .and. (k.eq.1 .or. k.eq.nz)) then
	      if (k.eq.1) then
		czz = 0.5*dlzz*cofz(k,2)
		tz = czz*(uz4*0.25+uz3/dlz)/3.0
	      else
		czz = 0.5*dlzz*cofz(k,1)
		tz = czz*(uz4*0.25-uz3/dlz)/3.0
	      end if
	    end if
	    r(i,j,k)=dlxx*tx+dlyy*ty+dlzz*tz
	  end do
	  do i=nx-1,ifinal
	    cxx = 0.5*dlxx*(cofx(i,1)+cofx(i,2))
	    cx = dlx*(cofx(i,2)-cofx(i,1))
	    cyy = 0.5*dlyy*(cofy(j,1)+cofy(j,2))
	    cy = dly*(cofy(j,2)-cofy(j,1))
	    czz = 0.5*dlzz*(cofz(k,1)+cofz(k,2))
	    cz = dlz*(cofz(k,2)-cofz(k,1))
	    call pde3(nx,ny,nz,u,i,j,k,ux3,ux4,uy3,uy4,uz3,uz4,
     +                nxa,nyc,nze)
	    tx = (cxx*ux4*0.5+cx*ux3)/6.0
	    ty = (cyy*uy4*0.5+cy*uy3)/6.0
	    tz = (czz*uz4*0.5+cz*uz3)/6.0
c
c     adjust truncation error terms at mixed boundaries
c
	    if(nxa.ne.0 .and. i.eq.nx) then
	      cxx = 0.5*dlxx*cofx(i,1)
	      tx = cxx*(ux4*0.25-ux3/dlx)/3.0
	    end if
	    if(nze.ne.0 .and. (k.eq.1 .or. k.eq.nz)) then
	      if (k.eq.1) then
		czz = 0.5*dlzz*cofz(k,2)
		tz = czz*(uz4*0.25+uz3/dlz)/3.0
	      else
		czz = 0.5*dlzz*cofz(k,1)
		tz = czz*(uz4*0.25-uz3/dlz)/3.0
	      end if
	    end if
	    r(i,j,k)=dlxx*tx+dlyy*ty+dlzz*tz
	  end do
	end do
      end do
      do i=3,nx-2
	do j=3,ny-2
	  do k=kstart,2
	    cxx = 0.5*dlxx*(cofx(i,1)+cofx(i,2))
	    cx = dlx*(cofx(i,2)-cofx(i,1))
	    cyy = 0.5*dlyy*(cofy(j,1)+cofy(j,2))
	    cy = dly*(cofy(j,2)-cofy(j,1))
	    czz = 0.5*dlzz*(cofz(k,1)+cofz(k,2))
	    cz = dlz*(cofz(k,2)-cofz(k,1))
	    call pde3(nx,ny,nz,u,i,j,k,ux3,ux4,uy3,uy4,uz3,uz4,
     +                nxa,nyc,nze)
	    tx = (cxx*ux4*0.5+cx*ux3)/6.0
	    ty = (cyy*uy4*0.5+cy*uy3)/6.0
	    tz = (czz*uz4*0.5+cz*uz3)/6.0
	    if(nze.ne.0 .and. k.eq.1) then
	      czz = 0.5*dlzz*cofz(k,2)
	      tz = czz*(uz4*0.25+uz3/dlz)/3.0
	    end if
	    r(i,j,k)=dlxx*tx+dlyy*ty+dlzz*tz
	  end do
	  do k=nz-1,kfinal
	    cxx = 0.5*dlxx*(cofx(i,1)+cofx(i,2))
	    cx = dlx*(cofx(i,2)-cofx(i,1))
	    cyy = 0.5*dlyy*(cofy(j,1)+cofy(j,2))
	    cy = dly*(cofy(j,2)-cofy(j,1))
	    czz = 0.5*dlzz*(cofz(k,1)+cofz(k,2))
	    cz = dlz*(cofz(k,2)-cofz(k,1))
	    call pde3(nx,ny,nz,u,i,j,k,ux3,ux4,uy3,uy4,uz3,uz4,
     +                nxa,nyc,nze)
	    tx = (cxx*ux4*0.5+cx*ux3)/6.0
	    ty = (cyy*uy4*0.5+cy*uy3)/6.0
	    tz = (czz*uz4*0.5+cz*uz3)/6.0
	    if(nze.ne.0 .and. k.eq.nz) then
	      czz = 0.5*dlzz*cofz(k,1)
	      tz = czz*(uz4*0.25-uz3/dlz)/3.0
	    end if
	    r(i,j,k)=dlxx*tx+dlyy*ty+dlzz*tz
	  end do
	end do
      end do
      return
      end