Ignore:
Timestamp:
Nov 9, 2021, 12:56:40 PM (3 years ago)
Author:
emillour
Message:

Generic GCM:
Fixed bug in tpindex (for low temperatures, between first and second
reference temperatures, temperature was wrongly set to tref(1)).
The input temperature was also allowed to be modified by the routine,
which is probably not a good idea and no longer the case.
Took this opportunity to turn tpindex into a module.
GM+EM

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LMDZ.GENERIC/libf/phystd/tpindex.F

    r1616 r2582  
     1      module tpindex_mod
     2     
     3      implicit none
     4     
     5      contains
     6     
    17      subroutine tpindex(pw,tw,qvar,pref,tref,wrefvar,LCOEF,MT,MP,
    28     &     NVAR,wratio)
     
    3440!     PW                 - The pressure to interpolate to
    3541!     TW                 - The temperature to interpolate to
    36 !     Pref(NP)           - The pressure grid array
    37 !     Tref(NT)           - The temperature grid array
     42!     Qvar               - The abundance to interpolate to
     43!     Pref               - The tabulated (log) pressure grid
     44!     Tref               - The tabulated temperature grid
     45!     WREFVAR            - The tabulated abundance grid
    3846!   
    3947!     Outputs
    4048!     -------
    41 !     TI                 - Interpolation term (pressure)
    42 !     UI                 - Interpolation term (temperature)
    4349!     MT                 - Temperature index (bottom left temperature)
    4450!                          of bounding box
    4551!     MP                 - Pressure index (bottom left pressure)
    4652!                          of bounding box
     53!     NVAR               -  index (bottom left )
     54!                          of bounding box
     55!     LCOEF(4)           - Interpolation coefficients
     56!     WRATIO             - weight for the interpolation of abundance
    4757!
    4858!     Authors
     
    5262!==================================================================
    5363
    54       use radinc_h
     64      use radinc_h, only: L_PINT, L_NTREF, L_REFVAR
    5565
    5666      implicit none
    5767
    58       real*8 Tref(L_NTREF)
    59       real*8 pref(L_PINT)
    60       real*8 wrefvar(L_REFVAR)
     68      REAL,INTENT(IN) :: Tref(L_NTREF)
     69      REAL,INTENT(IN) :: pref(L_PINT)
     70      REAL,INTENT(IN) :: wrefvar(L_REFVAR)
     71      REAL,INTENT(IN) :: PW, TW, Qvar
    6172
    62       integer MT, MP, N, M, NP, NVAR
    63       real*8  PW, TW, Qvar, wratio
    64       real*8  PWL, LCOEF(4), T, U
     73      INTEGER,INTENT(OUT) :: MT, MP, NVAR
     74      REAL,INTENT(OUT) :: LCOEF(4), wratio
     75     
     76      INTEGER :: N
     77      REAL :: PWL ! local value for log10(PW)
     78      REAL :: TWL ! local value for TW
     79      REAL :: T ! linear interpolation weight along log(pressure)
     80      REAL :: U ! linear interpolation weight along temperature
    6581
    66 C======================================================================C
    67  
     82!     1. For the interpolation along temperature
    6883!     Get the upper and lower temperature grid indicies that bound the
    69 !     requested temperature. If the requested temperature is outside
    70 !     the T-grid, set up to extrapolate from the appropriate end.
    71 !     TW : temperature to be interpolated
    72 !     TREF : grid array
    73 !     MT : index of TREF for bounding new temperature
    74 !     U : new index (real) for temperature interpolated
     84!     requested temperature. If the sought temperature is below
     85!     the T-grid minimum, assume that lower value. If the sought teperature
     86!     is above the T-grid maximum, extrapolate.
     87!     TW : temperature to interpolate to
     88!     TREF : reference temperature array
     89!     MT : index of TREF for (lower) bounding temperature
     90!     U : weight for interpolation in temperature
    7591
    76       IF(TW.LE.TREF(1)) THEN
     92      TWL = TW
     93
     94      IF(TWL.LT.TREF(1)) THEN
     95        ! Below lowest tabulated temperature
    7796        MT = 1
    78         IF (TW.LT.TREF(1)) THEN
    79          write(*,*) 'tpindex: Caution! Temperature of upper levels lower
     97        TWL=TREF(1)*1.00
     98        write(*,*) 'tpindex: Caution! Temperature of upper levels lower
    8099     $ than ref temperature for k-coef: k-coeff fixed for upper levels'
    81          write(*,*) "         TW=",TW
    82          write(*,*) "         TREF(1)=",TREF(1)
    83         ENDIF
    84       ELSE
     100        write(*,*) "         TW=",TWL
     101        write(*,*) "         TREF(1)=",TREF(1)
     102      ELSEIF(TWL.GE.TREF(L_NTREF)) THEN
     103       ! Above highest tabulated temperature
     104       MT = L_NTREF-1
     105      ELSE
     106        ! Regular case, find encompassing tabulated temperatures
    85107        do n=1,L_NTREF-1
    86           if(tw.gt.Tref(n) .and. TW.LE.TREF(N+1)) then
     108          IF (TWL.GE.TREF(N) .and. TWL.LT.TREF(N+1)) THEN
    87109            MT = n
    88             goto 10
    89           end if
    90         end do
    91 
    92         MT = L_NTREF-1
     110            EXIT
     111          END IF
     112        END DO
    93113     
    94    10   continue
    95114      END IF
    96115
    97       !TB15 : case low temp : MT=1: fixed TW right above tref(1)
    98       IF (MT.eq.1) THEN
    99          TW=tref(1)*1.00
    100 !         write(*,*) 'tpindex: Caution! Temperature of upper levels lower
    101 !     $than ref temperature for k-coef: k-coeff fixed for upper levels'
    102 !         write(*,*) "         TW=",TW
    103 !         write(*,*) "         TREF(1)=",TREF(1)
    104       ENDIF
     116!     weight for the temperature interpolation
     117      U = (TWL-TREF(MT))/(TREF(MT+1)-TREF(MT))
    105118
    106       U = (TW-TREF(MT))/(TREF(MT+1)-TREF(MT))
     119!     2. For interpolation along pressure
     120!     Get the upper and lower pressure grid indicies that bound the
     121!     requested pressure. If the sought pressure is below
     122!     the P-grid minimum, assume that lower value. If the sought pressure
     123!     is above the P-grid maximum, extrapolate.
    107124
    108 !     Get the upper and lower pressure grid indicies that bound the
    109 !     requested pressure. If the requested pressure is outside
    110 !     the P-grid, set up to extrapolate from the appropriate end.
     125      PWL = log10(PW)
    111126
    112       pwl = log10(pw)
     127      IF(PWL.LT.PREF(1)) THEN
     128        ! Below lowest tabulated pressure
     129        MP = 1
     130        PWL=Pref(1)*1.00
     131        write(*,*) 'tpindex: Caution! Pressure of upper levels lower
     132     $than ref pressure for k-coef: k-coeff fixed for upper levels'
     133        write(*,*) "         PWL=",PWL
     134        write(*,*) "         PREF(1)=",PREF(1)
     135      ELSEIF(PWL.GE.PREF(L_PINT)) THEN
     136        ! Above highest tabulated pressure
     137        MP = L_PINT-1
     138      ELSE
     139        ! Regular case, find encompassing tabulated pressures
     140        do n=1,L_PINT-1
     141          IF (PWL.GE.PREF(N) .and. PWL.LT.PREF(N+1)) THEN
     142            MP = n
     143            EXIT
     144          END IF
     145        END DO
     146     
     147      END IF
    113148
    114       do n=2,L_PINT-1
    115         if(pwl.le.Pref(n)) then
    116           MP = n-1
    117           goto 20
    118         end if
    119       end do
    120 
    121       MP = L_PINT-1
    122 
    123    20 continue
    124      
    125       !TB15 : case low pressure : n=2 : fixed pwl, right above pref(1)
    126       IF (MP.eq.1) THEN
    127         IF (PWL.LT.PREF(1)) THEN
    128          write(*,*) 'tpindex: Caution! Pressure of upper levels lower
    129      $than ref pressure for k-coef: k-coeff fixed for upper levels'
    130          write(*,*) "         PWL=",PWL
    131          write(*,*) "         PREF(1)=",PREF(1)
    132         ENDIF
    133         PWL=Pref(1)*1.00
    134       ENDIF
    135 
    136 !     interpolated pressure
     149!     weight for the pressure interpolation
    137150      T = (PWL-PREF(MP))/(PREF(MP+1)-PREF(MP))
    138151
    139 Fill in the interpolation coefficients
     152Build the interpolation coefficients (bilinear in temperature-log(pressure))
    140153      LCOEF(1) = (1.0-T)*(1.0-U)
    141154      LCOEF(2) = T*(1.0-U)
     
    143156      LCOEF(4) = (1.0-T)*U
    144157
     158!  3. For interpolation along abundances
    145159!  Get the indicies for abundance of the varying species. There are 10 sets of
    146160!  k-coefficients with differing amounts of variable vs. constant gas.
    147161
    148       IF(QVAR.le.WREFVAR(1)) then
     162      IF(QVAR.LE.WREFVAR(1)) THEN
     163        ! Below lowest tabulated abundance
    149164        NVAR   = 1
    150165        WRATIO = 0.0D0      ! put all the weight on the first point
    151       ELSEIF(QVAR.ge.WREFVAR(L_REFVAR)) then
     166      ELSEIF(QVAR.GE.WREFVAR(L_REFVAR)) THEN
     167        ! Above highest tabulated abundance
    152168        NVAR   = L_REFVAR-1 ! TB16 in order to not oversize NVAr when doing
    153169                                  !NVAR+1
    154170        WRATIO = 1.00D0     ! put all the weight on the last point
    155171      ELSE
     172        ! Regular case, find encompassing tabulated abundances
     173        ! and compute corresponding weight
    156174        DO N=2,L_REFVAR
    157           IF(QVAR.GE.WREFVAR(N-1) .and. QVAR.lt.WREFVAR(N)) then
     175          IF(QVAR.GE.WREFVAR(N-1) .and. QVAR.LT.WREFVAR(N)) THEN
    158176            NVAR   = N-1
    159177            WRATIO = (QVAR - WREFVAR(N-1))/(WREFVAR(N) - WREFVAR(N-1))
    160             GOTO 30
     178            EXIT
    161179          END IF
    162180        END DO
    163181      END IF
    164182
    165    30 CONTINUE
    166 
    167       return
    168       end
     183      end subroutine tpindex
     184     
     185      end module tpindex_mod
     186     
Note: See TracChangeset for help on using the changeset viewer.