!        Generated by TAPENADE     (INRIA, Ecuador team)
!  tapenade 3.x
!
!  Differentiation of check_domain in reverse (adjoint) mode (with options noISIZE i8):
!   gradient     of useful results: *temp *conc *pres
!   with respect to varying inputs: *temp *conc *pres
!   Plus diff mem management of: temp:in conc:in pres:in
! MIT License
!
! Copyright (c) 2020 SHEMAT-Suite
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
!>    @brief domain of validity for module ice
!>    @param[in] ismpl local sample index
SUBROUTINE CHECK_DOMAIN_AD(ismpl)
  use arrays

  USE ARRAYS_AD

  USE MOD_GENRL
  USE MOD_GENRLC
  USE MOD_CONC
  USE MOD_LINFOS
  IMPLICIT NONE
  INTEGER :: ismpl
  INTEGER :: i, j, k, l
!     counts the directions (thickness large enough)
  INTEGER :: icountp, icountt, icountc
!     [csmin] reasonable physical value when not [cmin]
  DOUBLE PRECISION :: pmin, pmax, tmin, tmax, cmin, csmin, cmax, hmin, &
& hmax, dpmax, dtmax, dcmax, dhmax, dpmin, dtmin, dcmin, dhmin
  PARAMETER (pmin=0.01d6, pmax=150.d6, tmin=-60.0d0, tmax=350.0, cmin=&
&   0.d0, csmin=1.d-30, cmax=1.d0)
  INTRINSIC TRIM
  INTRINSIC MIN
  INTRINSIC MAX
  INTEGER :: branch
  DO k=1,k0
    DO j=1,j0
      DO i=1,i0
        IF (pres(i, j, k, ismpl) .LT. pmin) THEN
          CALL PUSHREAL8(pres(i, j, k, ismpl))
          pres(i, j, k, ismpl) = pmin
          CALL PUSHCONTROL1B(0)
        ELSE
          CALL PUSHCONTROL1B(1)
        END IF
        IF (pres(i, j, k, ismpl) .GT. pmax) THEN
          CALL PUSHREAL8(pres(i, j, k, ismpl))
          pres(i, j, k, ismpl) = pmax
          CALL PUSHCONTROL1B(0)
        ELSE
          CALL PUSHCONTROL1B(1)
        END IF
        IF (temp(i, j, k, ismpl) .LT. tmin) THEN
          CALL PUSHREAL8(temp(i, j, k, ismpl))
          temp(i, j, k, ismpl) = tmin
          CALL PUSHCONTROL1B(0)
        ELSE
          CALL PUSHCONTROL1B(1)
        END IF
        IF (temp(i, j, k, ismpl) .GT. tmax) THEN
          CALL PUSHREAL8(temp(i, j, k, ismpl))
          temp(i, j, k, ismpl) = tmax
          CALL PUSHCONTROL1B(1)
        ELSE
          CALL PUSHCONTROL1B(0)
        END IF
        DO l=1,ntrac
          IF (conc(i, j, k, l, ismpl) .LT. cmin) THEN
            CALL PUSHREAL8(conc(i, j, k, l, ismpl))
            conc(i, j, k, l, ismpl) = cmin
            CALL PUSHCONTROL1B(0)
          ELSE
            CALL PUSHCONTROL1B(1)
          END IF
!aw-later                  if (conc(i,j,k,l,ismpl).gt.cmax) then
!aw-later                     icountc = icountc +1
!aw-later                     dcmax = max(dcmax, conc(i,j,k,l,ismpl))
!aw-later                     conc(i,j,k,l,ismpl) = cmax
!aw-later                  endif
!                 use reasonable physical value [csmin]
          IF (conc(i, j, k, l, ismpl) .LT. csmin) THEN
!                    to avoid numerically instabilities
            CALL PUSHREAL8(conc(i, j, k, l, ismpl))
            conc(i, j, k, l, ismpl) = cmin
            CALL PUSHCONTROL1B(1)
          ELSE
            CALL PUSHCONTROL1B(0)
          END IF
        END DO
      END DO
    END DO
  END DO
  DO k=k0,1,-1
    DO j=j0,1,-1
      DO i=i0,1,-1
        DO l=ntrac,1,-1
          CALL POPCONTROL1B(branch)
          IF (branch .NE. 0) THEN
            CALL POPREAL8(conc(i, j, k, l, ismpl))
            conc_ad(i, j, k, l, ismpl) = 0.D0
          END IF
          CALL POPCONTROL1B(branch)
          IF (branch .EQ. 0) THEN
            CALL POPREAL8(conc(i, j, k, l, ismpl))
            conc_ad(i, j, k, l, ismpl) = 0.D0
          END IF
        END DO
        CALL POPCONTROL1B(branch)
        IF (branch .NE. 0) THEN
          CALL POPREAL8(temp(i, j, k, ismpl))
          temp_ad(i, j, k, ismpl) = 0.D0
        END IF
        CALL POPCONTROL1B(branch)
        IF (branch .EQ. 0) THEN
          CALL POPREAL8(temp(i, j, k, ismpl))
          temp_ad(i, j, k, ismpl) = 0.D0
        END IF
        CALL POPCONTROL1B(branch)
        IF (branch .EQ. 0) THEN
          CALL POPREAL8(pres(i, j, k, ismpl))
          pres_ad(i, j, k, ismpl) = 0.D0
        END IF
        CALL POPCONTROL1B(branch)
        IF (branch .EQ. 0) THEN
          CALL POPREAL8(pres(i, j, k, ismpl))
          pres_ad(i, j, k, ismpl) = 0.D0
        END IF
      END DO
    END DO
  END DO
END SUBROUTINE CHECK_DOMAIN_AD

