! Copyright 2022, Francois BOUCHUT, Anne MANGENEY, Marc PERUZZETTO, Antoine LUCAS,  Jacques SAINTE-MARIE
! This program is distributed under the terms of the Lesser GPL v3
! This file is part of SHALTOP.


module spatial_reconstruction

  use topography
  use states
  use parameters
  use bound_conditions

  implicit double precision (a-h,o-z)

  double precision,dimension(:,:), allocatable :: rhopow,zniv
  double precision,dimension(:), allocatable :: ddxthet1,ddxota,ddxp,ddxpt
  double precision,dimension(:), allocatable :: ddythet1,ddyota,ddyp,ddypt
  double precision,dimension(:), allocatable :: ddxrhopow,ddxu,ddxut,ddxzniv
  double precision,dimension(:), allocatable :: ddyrhopow,ddyu,ddyut,ddyzniv
  
contains

  !-------------------------------------------
  !Allocate tables
  subroutine spatial_reconstruction_allocate
    allocate(rhopow(0:nx+1,0:ny+1),zniv(0:nx+1,0:ny+1))
    allocate(ddxthet1(0:nx),ddxota(0:nx),ddxp(0:nx),ddxpt(0:nx))
    allocate(ddythet1(0:ny),ddyota(0:ny),ddyp(0:ny),ddypt(0:ny))
    allocate(ddxrhopow(0:nx),ddxu(0:nx),ddxut(0:nx),ddxzniv(0:nx))
    allocate(ddyrhopow(0:ny),ddyu(0:ny),ddyut(0:ny),ddyzniv(0:ny))
  end subroutine spatial_reconstruction_allocate

  subroutine spatial_reconstruction_init
    
    if (icomp.gt.0) then

       if (mordre.eq.1) then
          
          do j=1,ny
             do i=0,nx
                delz(i,j)=z(i+1,j)-z(i,j)
             enddo
          enddo
          do j=0,ny
             do i=1,nx
                delzy(i,j)=z(i,j+1)-z(i,j)
             enddo
          enddo
          do j=1,ny
             do i=1,nx
                delzc(i,j)=0.d0
                delzcy(i,j)=0.d0
             enddo
          enddo

          do j=0,ny+1
             do i=0,nx+1
                pg(i,j)=p(i,j)
                pd(i,j)=p(i,j)
                ps(i,j)=p(i,j)
                pn(i,j)=p(i,j)
                ptg(i,j)=pt(i,j)
                ptd(i,j)=pt(i,j)
                pts(i,j)=pt(i,j)
                ptn(i,j)=pt(i,j)
                thetg(i,j)=thet(i,j)
                thetd(i,j)=thet(i,j)
                thets(i,j)=thet(i,j)
                thetn(i,j)=thet(i,j)
                otag(i,j)=ota(i,j)
                otad(i,j)=ota(i,j)
                otas(i,j)=ota(i,j)
                otan(i,j)=ota(i,j)
             enddo
          enddo
          
       else
          
          do j=0,ny+1
             do i=1,nx-1
                ddxthet1(i)=amortrec*petmod(1.d0/thet(i+1,j)-2.d0/thet(i,j) +1.d0/thet(i-1,j),&
                     &1.d0/thet(i+2,j) -2.d0/thet(i+1,j)+1.d0/thet(i,j))
                ddxota(i)=amortrec*petmod(ota(i+1,j)-2.d0*ota(i,j)+ota(i-1,j) ,&
                     &ota(i+2,j)-2.d0*ota(i+1,j)+ota(i,j))
             enddo
             ddxthet1(0)=0.d0
             ddxota(0)=0.d0
             ddxthet1(nx)=0.d0
             ddxota(nx)=0.d0
             do i=1,nx
                dthet1=petmod(1.d0/thet(i+1,j)-1.d0/thet(i,j)&
                     &-.5d0*ddxthet1(i) ,1.d0/thet(i,j)-1.d0/thet(i-1,j)+.5d0*ddxthet1(i-1))/2.d0
                dthet1lim=petmod(1.d0/thet(i+1,j)-1.d0/thet(i,j) ,1.d0/thet(i,j)-1.d0/thet(i-1,j))
                dthet1=petmod(dthet1,dthet1lim)
                thetg(i,j)=1.d0/(1.d0/thet(i,j)-dthet1)
                thetd(i,j)=1.d0/(1.d0/thet(i,j)+dthet1)
                dota=petmod(ota(i+1,j)-ota(i,j)-.5d0*ddxota(i) ,ota(i,j)-ota(i-1,j)+.5d0*ddxota(i-1))/2.d0
                dotalim=petmod(ota(i+1,j)-ota(i,j),ota(i,j)-ota(i-1,j))
                dota=petmod(dota,dotalim)
                otag(i,j)=ota(i,j)-dota
                otad(i,j)=ota(i,j)+dota
             enddo
             thetd(0,j)=.5d0*thet(0,j)+.5d0*thet(1,j)
             otad(0,j)=.5d0*ota(0,j)+.5d0*ota(1,j)
             thetg(nx+1,j)=.5d0*thet(nx+1,j)+.5d0*thet(nx,j)
             otag(nx+1,j)=.5d0*ota(nx+1,j)+.5d0*ota(nx,j)
          enddo

          do i=0,nx+1
             do j=1,ny-1
                ddythet1(j)=amortrec*petmod(1.d0/thet(i,j+1)-2.d0/thet(i,j)+1.d0/thet(i,j-1),&
                     &1.d0/thet(i,j+2) -2.d0/thet(i,j+1)+1.d0/thet(i,j))
                ddyota(j)=amortrec*petmod(ota(i,j+1)-2.d0*ota(i,j)+ota(i,j-1) ,&
                     &ota(i,j+2)-2.d0*ota(i,j+1)+ota(i,j))
             enddo
             ddythet1(0)=0.d0
             ddyota(0)=0.d0
             ddythet1(ny)=0.d0
             ddyota(ny)=0.d0
             do j=1,ny
                dthet1=petmod(1.d0/thet(i,j+1)-1.d0/thet(i,j)&
                     &-.5d0*ddythet1(j) ,1.d0/thet(i,j)-1.d0/thet(i,j-1)+.5d0*ddythet1(j-1))/2.d0
                dthet1lim=petmod(1.d0/thet(i,j+1)-1.d0/thet(i,j) ,1.d0/thet(i,j)-1.d0/thet(i,j-1))
                dthet1=petmod(dthet1,dthet1lim)
                thets(i,j)=1.d0/(1.d0/thet(i,j)-dthet1)
                thetn(i,j)=1.d0/(1.d0/thet(i,j)+dthet1)
                dota=petmod(ota(i,j+1)-ota(i,j)-.5d0*ddyota(j) ,ota(i,j)-ota(i,j-1)+.5d0*ddyota(j-1))/2.d0
                dotalim=petmod(ota(i,j+1)-ota(i,j),ota(i,j)-ota(i,j-1))
                dota=petmod(dota,dotalim)
                otas(i,j)=ota(i,j)-dota
                otan(i,j)=ota(i,j)+dota
             enddo
             thetn(i,0)=.5d0*thet(i,0)+.5d0*thet(i,1)
             otan(i,0)=.5d0*ota(i,0)+.5d0*ota(i,1)
             thets(i,ny+1)=.5d0*thet(i,ny+1)+.5d0*thet(i,ny)
             otas(i,ny+1)=.5d0*ota(i,ny+1)+.5d0*ota(i,ny)
          enddo
          
       end if
       
    end if

  end subroutine spatial_reconstruction_init

  !------------------------------------------------------------------------------------------------
  !Spatial reconstruction states and topo in time loop

  subroutine spatial_reconstruction_time_loop

    if (mordre.eq.1) then
       do j=0,ny+1
          do i=0,nx+1
             rhog(i,j)=rho(i,j)
             rhod(i,j)=rho(i,j)
             rhos(i,j)=rho(i,j)
             rhon(i,j)=rho(i,j)
             ug(i,j)=u(i,j)
             ud(i,j)=u(i,j)
             us(i,j)=u(i,j)
             un(i,j)=u(i,j)
             utg(i,j)=ut(i,j)
             utd(i,j)=ut(i,j)
             uts(i,j)=ut(i,j)
             utn(i,j)=ut(i,j)
          enddo
       enddo
    endif
    
    !creconstruction d'ordre 2
    if (.not.(mordre.eq.1)) then
       do j=0,ny+1
          do i=0,nx+1
             rhopow(i,j)=rho(i,j)*ota(i,j)
             zniv(i,j)=grav*(rhopow(i,j)+z(i,j))
          enddo
       enddo
       
       do j=1,ny
          do i=1,nx-1
             ddxp(i)=amortrec*petmod(p(i+1,j)-2.d0*p(i,j)+p(i-1,j) &
                  & ,p(i+2,j)-2.d0*p(i+1,j)+p(i,j))
             ddxpt(i)=amortrec*petmod(pt(i+1,j)-2.d0*pt(i,j)+pt(i-1,j)&
                  & ,pt(i+2,j)-2.d0*pt(i+1,j)+pt(i,j))
             ddxrhopow(i)=amortrec*petmod(rhopow(i+1,j)-2.d0*rhopow(i,j)&
                  & +rhopow(i-1,j),rhopow(i+2,j)-2.d0*rhopow(i+1,j)+rhopow(i,j))
             ddxzniv(i)=amortrec*petmod(zniv(i+1,j)-2.d0*zniv(i,j)+zniv(i-1,j)&
                  & ,zniv(i+2,j)-2.d0*zniv(i+1,j)+zniv(i,j))
             ddxu(i)=amortrec*petmod(u(i+1,j)-2.d0*u(i,j)+u(i-1,j)&
                  & ,u(i+2,j)-2.d0*u(i+1,j)+u(i,j))
             ddxut(i)=amortrec*petmod(ut(i+1,j)-2.d0*ut(i,j)+ut(i-1,j)&
                  & ,ut(i+2,j)-2.d0*ut(i+1,j)+ut(i,j))
          enddo
          ddxp(0)=0.d0
          ddxpt(0)=0.d0
          ddxrhopow(0)=0.d0
          ddxzniv(0)=0.d0
          ddxu(0)=0.d0
          ddxut(0)=0.d0
          ddxp(nx)=0.d0
          ddxpt(nx)=0.d0
          ddxrhopow(nx)=0.d0
          ddxzniv(nx)=0.d0
          ddxu(nx)=0.d0
          ddxut(nx)=0.d0
          do i=1,nx
             dxrhopow=petmod(rhopow(i+1,j)-rhopow(i,j)-.5d0*ddxrhopow(i) &
                  & ,rhopow(i,j)-rhopow(i-1,j)+.5d0*ddxrhopow(i-1))/2.d0
             dxrhopowlim=petmod(rhopow(i+1,j)-rhopow(i,j)&
                  & ,rhopow(i,j)-rhopow(i-1,j))*.9d0
             dxrhopow=petmod(dxrhopow,dxrhopowlim)
             rhopowg=rhopow(i,j)-dxrhopow
             rhopowd=rhopow(i,j)+dxrhopow
             dxrho=((rhopowd/otad(i,j))&
                  & -(rhopowg/otag(i,j))&
                  & )/(1.d0/thetd(i,j)&
                  & +1.d0/thetg(i,j))/thet(i,j)
             dxrholim=petmod((rho(i+1,j)-rho(i,j))*thetg(i,j)/thet(i,j),(rho(i,j)-rho(i-1,j))*thetd(i,j)/thet(i,j))*.9d0
             dxrho=petmod(dxrho,dxrholim)
             rhog(i,j)=rho(i,j)-dxrho*thet(i,j)/thetd(i,j)
             rhod(i,j)=rho(i,j)+dxrho*thet(i,j)/thetg(i,j)
             dxzniv=petmod(zniv(i+1,j)-zniv(i,j)-.5d0*ddxzniv(i)&
                  & ,zniv(i,j)-zniv(i-1,j)+.5d0*ddxzniv(i-1))/2.d0
             dxznivlim=petmod(zniv(i+1,j)-zniv(i,j),zniv(i,j)-zniv(i-1,j))*.9d0
             dxzniv=petmod(dxzniv,dxznivlim)
             znivg=zniv(i,j)-dxzniv
             znivd=zniv(i,j)+dxzniv
             zg(i,j)=(znivg-grav*rhog(i,j)&
                  & *otag(i,j))/grav
             zd(i,j)=(znivd-grav*rhod(i,j)&
                  & *otad(i,j))/grav
             dxu=petmod(u(i+1,j)-u(i,j)-.5d0*ddxu(i)&
                  & ,u(i,j)-u(i-1,j)+.5d0*ddxu(i-1))/2.d0
             dxut=petmod(ut(i+1,j)-ut(i,j)-.5d0*ddxut(i)&
                  & ,ut(i,j)-ut(i-1,j)+.5d0*ddxut(i-1))/2.d0
             if (rho(i,j).gt.0.d0) then
                ug(i,j)=u(i,j)-dxu*rhod(i,j)/rho(i,j)*thet(i,j)/thetd(i,j)
                ud(i,j)=u(i,j)+dxu*rhog(i,j)/rho(i,j)*thet(i,j)/thetg(i,j)
                utg(i,j)=ut(i,j)-dxut*rhod(i,j)/rho(i,j)*thet(i,j)/thetd(i,j)
                utd(i,j)=ut(i,j)+dxut*rhog(i,j)/rho(i,j)*thet(i,j)/thetg(i,j)
             else
                ug(i,j)=u(i,j)-dxu
                ud(i,j)=u(i,j)+dxu
                utg(i,j)=ut(i,j)-dxut
                utd(i,j)=ut(i,j)+dxut
             endif
             dp=petmod(p(i+1,j)-p(i,j)-.5d0*ddxp(i)&
                  & ,p(i,j)-p(i-1,j)+.5d0*ddxp(i-1))/2.d0
             dpt=petmod(pt(i+1,j)-pt(i,j)-.5d0*ddxpt(i)&
                  & ,pt(i,j)-pt(i-1,j)+.5d0*ddxpt(i-1))/2.d0
             dpmax=max(abs(p(i+1,j)-p(i,j)-.5d0*ddxp(i))&
                  & ,abs(p(i,j)-p(i-1,j)+.5d0*ddxp(i-1)))/2.d0
             dptmax=max(abs(pt(i+1,j)-pt(i,j)-.5d0*ddxpt(i))&
                  & ,abs(pt(i,j)-pt(i-1,j)+.5d0*ddxpt(i-1)))/2.d0
             dxumax=max(abs(u(i+1,j)-u(i,j)-.5d0*ddxu(i))&
                  & ,abs(u(i,j)-u(i-1,j)+.5d0*ddxu(i-1)))/2.d0
             dxutmax=max(abs(ut(i+1,j)-ut(i,j)-.5d0*ddxut(i))&
                  & ,abs(ut(i,j)-ut(i-1,j)+.5d0*ddxut(i-1)))/2.d0
             dxrhopowmax=max(abs(rhopow(i+1,j)-rhopow(i,j)-.5d0*ddxrhopow(i))&
                  & ,abs(rhopow(i,j)-rhopow(i-1,j)+.5d0*ddxrhopow(i-1)))/2.d0
             if ((dxumax+dxutmax.gt.0.d0).and.(dxrhopowmax.gt.0.d0)) then
                dpplim=(dpmax+dptmax)*min((abs(dxu)+abs(dxut))/(dxumax+dxutmax)&
                     & ,abs(dxrhopow)/dxrhopowmax)
                absdpp=abs(dp)+abs(dpt)
                if (absdpp.gt.dpplim) then
                   dp=dp/absdpp*dpplim
                   dpt=dpt/absdpp*dpplim
                endif
             endif
             pg(i,j)=p(i,j)-dp
             pd(i,j)=p(i,j)+dp
             ptg(i,j)=pt(i,j)-dpt
             ptd(i,j)=pt(i,j)+dpt
          enddo
          pd(0,j)=.5d0*p(0,j)+.5d0*p(1,j)
          ptd(0,j)=.5d0*pt(0,j)+.5d0*pt(1,j)
          pg(nx+1,j)=.5d0*p(nx+1,j)+.5d0*p(nx,j)
          ptg(nx+1,j)=.5d0*pt(nx+1,j)+.5d0*pt(nx,j)
          zg(0,j)=1.5d0*z(0,j)-.5d0*z(1,j)
          zd(0,j)=.5*z(0,j)+.5*z(1,j)
          zg(nx+1,j)=.5d0*z(nx+1,j)+.5d0*z(nx,j)
          zd(nx+1,j)=1.5d0*z(nx+1,j)-.5d0*z(nx,j)
          do i=0,nx
             delz(i,j)=zg(i+1,j)-zd(i,j)
          enddo
          do i=1,nx
             delzc(i,j)=zd(i,j)-zg(i,j)
          enddo
       enddo

       do i=1,nx
          do j=1,ny-1
             ddyp(j)=amortrec*petmod(p(i,j+1)-2.d0*p(i,j)+p(i,j-1)&
                  & ,p(i,j+2)-2.d0*p(i,j+1)+p(i,j))
             ddypt(j)=amortrec*petmod(pt(i,j+1)-2.d0*pt(i,j)+pt(i,j-1)&
                  & ,pt(i,j+2)-2.d0*pt(i,j+1)+pt(i,j))
             ddyrhopow(j)=amortrec*petmod(rhopow(i,j+1)-2.d0*rhopow(i,j)&
                  & +rhopow(i,j-1),rhopow(i,j+2)-2.d0*rhopow(i,j+1)+rhopow(i,j))
             ddyzniv(j)=amortrec*petmod(zniv(i,j+1)-2.d0*zniv(i,j)+zniv(i,j-1)&
                  & ,zniv(i,j+2)-2.d0*zniv(i,j+1)+zniv(i,j))
             ddyu(j)=amortrec*petmod(u(i,j+1)-2.d0*u(i,j)+u(i,j-1)&
                  & ,u(i,j+2)-2.d0*u(i,j+1)+u(i,j))
             ddyut(j)=amortrec*petmod(ut(i,j+1)-2.d0*ut(i,j)+ut(i,j-1)&
                  & ,ut(i,j+2)-2.d0*ut(i,j+1)+ut(i,j))
          enddo
          ddyp(0)=0.d0
          ddypt(0)=0.d0
          ddyrhopow(0)=0.d0
          ddyzniv(0)=0.d0
          ddyu(0)=0.d0
          ddyut(0)=0.d0
          ddyp(ny)=0.d0
          ddypt(ny)=0.d0
          ddyrhopow(ny)=0.d0
          ddyzniv(ny)=0.d0
          ddyu(ny)=0.d0
          ddyut(ny)=0.d0
          do j=1,ny
             dyrhopow=petmod(rhopow(i,j+1)-rhopow(i,j)-.5d0*ddyrhopow(j)&
                  & ,rhopow(i,j)-rhopow(i,j-1)+.5d0*ddyrhopow(j-1))/2.d0
             dyrhopowlim=petmod(rhopow(i,j+1)-rhopow(i,j)&
                  & ,rhopow(i,j)-rhopow(i,j-1))*.9d0
             dyrhopow=petmod(dyrhopow,dyrhopowlim)
             rhopows=rhopow(i,j)-dyrhopow
             rhopown=rhopow(i,j)+dyrhopow
             dyrho=((rhopown/otan(i,j))&
                  & -(rhopows/otas(i,j))&
                  & )/(1.d0/thetn(i,j)&
                  & +1.d0/thets(i,j))/thet(i,j)
             dyrholim=petmod((rho(i,j+1)-rho(i,j))*thets(i,j)/thet(i,j)&
                  & ,(rho(i,j)-rho(i,j-1))*thetn(i,j)/thet(i,j))*.9d0
             dyrho=petmod(dyrho,dyrholim)
             rhos(i,j)=rho(i,j)-dyrho*thet(i,j)/thetn(i,j)
             rhon(i,j)=rho(i,j)+dyrho*thet(i,j)/thets(i,j)
             dyzniv=petmod(zniv(i,j+1)-zniv(i,j)-.5d0*ddyzniv(j)&
                  & ,zniv(i,j)-zniv(i,j-1)+.5d0*ddyzniv(j-1))/2.d0
             dyznivlim=petmod(zniv(i,j+1)-zniv(i,j),zniv(i,j)-zniv(i,j-1))*.9d0
             dyzniv=petmod(dyzniv,dyznivlim)
             znivs=zniv(i,j)-dyzniv
             znivn=zniv(i,j)+dyzniv
             zs(i,j)=(znivs-grav*rhos(i,j)&
                  & *otas(i,j))/grav
             zn(i,j)=(znivn-grav*rhon(i,j)&
                  & *otan(i,j))/grav
             dyu=petmod(u(i,j+1)-u(i,j)-.5d0*ddyu(j)&
                  & ,u(i,j)-u(i,j-1)+.5d0*ddyu(j-1))/2.d0
             dyut=petmod(ut(i,j+1)-ut(i,j)-.5d0*ddyut(j)&
                  & ,ut(i,j)-ut(i,j-1)+.5d0*ddyut(j-1))/2.d0
             if (rho(i,j).gt.0.d0) then
                us(i,j)=u(i,j)-dyu*rhon(i,j)/rho(i,j)*thet(i,j)/thetn(i,j)
                un(i,j)=u(i,j)+dyu*rhos(i,j)/rho(i,j)*thet(i,j)/thets(i,j)
                uts(i,j)=ut(i,j)-dyut*rhon(i,j)/rho(i,j)*thet(i,j)/thetn(i,j)
                utn(i,j)=ut(i,j)+dyut*rhos(i,j)/rho(i,j)*thet(i,j)/thets(i,j)
             else
                us(i,j)=u(i,j)-dyu
                un(i,j)=u(i,j)+dyu
                uts(i,j)=ut(i,j)-dyut
                utn(i,j)=ut(i,j)+dyut
             endif
             dp=petmod(p(i,j+1)-p(i,j)-.5d0*ddyp(j)&
                  & ,p(i,j)-p(i,j-1)+.5d0*ddyp(j-1))/2.d0
             dpt=petmod(pt(i,j+1)-pt(i,j)-.5d0*ddypt(j)&
                  & ,pt(i,j)-pt(i,j-1)+.5d0*ddypt(j-1))/2.d0
             dpmax=max(abs(p(i,j+1)-p(i,j)-.5d0*ddyp(j))&
                  & ,abs(p(i,j)-p(i,j-1)+.5d0*ddyp(j-1)))/2.d0
             dptmax=max(abs(pt(i,j+1)-pt(i,j)-.5d0*ddypt(j))&
                  & ,abs(pt(i,j)-pt(i,j-1)+.5d0*ddypt(j-1)))/2.d0
             dyumax=max(abs(u(i,j+1)-u(i,j)-.5d0*ddyu(j))&
                  & ,abs(u(i,j)-u(i,j-1)+.5d0*ddyu(j-1)))/2.d0
             dyutmax=max(abs(ut(i,j+1)-ut(i,j)-.5d0*ddyut(j))&
                  & ,abs(ut(i,j)-ut(i,j-1)+.5d0*ddyut(j-1)))/2.d0
             dyrhopowmax=max(abs(rhopow(i,j+1)-rhopow(i,j)-.5d0*ddyrhopow(j))&
                  & ,abs(rhopow(i,j)-rhopow(i,j-1)+.5d0*ddyrhopow(j-1)))/2.d0
             if ((dyumax+dyutmax.gt.0.d0).and.(dyrhopowmax.gt.0.d0)) then
                dpplim=(dpmax+dptmax)*min((abs(dyu)+abs(dyut))/(dyumax+dyutmax)&
                     & ,abs(dyrhopow)/dyrhopowmax)
                absdpp=abs(dp)+abs(dpt)
                if (absdpp.gt.dpplim) then
                   dp=dp/absdpp*dpplim
                   dpt=dpt/absdpp*dpplim
                endif
             endif
             ps(i,j)=p(i,j)-dp
             pn(i,j)=p(i,j)+dp
             pts(i,j)=pt(i,j)-dpt
             ptn(i,j)=pt(i,j)+dpt
          enddo
          pn(i,0)=.5d0*p(i,0)+.5d0*p(i,1)
          ptn(i,0)=.5d0*pt(i,0)+.5d0*pt(i,1)
          ps(i,ny+1)=.5d0*p(i,ny+1)+.5d0*p(i,ny)
          pts(i,ny+1)=.5d0*pt(i,ny+1)+.5d0*pt(i,ny)
          zs(i,0)=1.5d0*z(i,0)-.5d0*z(i,1)
          zn(i,0)=.5d0*z(i,0)+.5d0*z(i,1)
          zs(i,ny+1)=.5d0*z(i,ny+1)+.5d0*z(i,ny)
          zn(i,ny+1)=1.5d0*z(i,ny+1)-.5d0*z(i,ny)
          do j=0,ny
             delzy(i,j)=zs(i,j+1)-zn(i,j)
          enddo
          do j=1,ny
             delzcy(i,j)=zn(i,j)-zs(i,j)
          enddo
       enddo

       !cconditions aux limites des reconstructions
       !cgauche
       !cperiodique

       call bound_conditions_reconstruct

    endif

  end subroutine spatial_reconstruction_time_loop

  !--------------------------------------------------------------------------------------------------
  !Minmod function  
  function petmod(x,y)
    implicit double precision (a-h,o-z)

    if ((x.gt.0.d0).and.(y.gt.0.d0)) then
       petmod=min(x,y)
    else if ((x.lt.0.d0).and.(y.lt.0.d0)) then
       petmod=max(x,y)
    else
       petmod=0.d0
    endif
  end function petmod

  end module spatial_reconstruction
