!        Generated by TAPENADE     (INRIA, Ecuador team)
!  tapenade 3.x
!
!  Differentiation of omp_old_save in reverse (adjoint) mode (with options noISIZE i8):
!   gradient     of useful results: *concold *temp *headold *head
!                *tempold *presold *conc *pres
!   with respect to varying inputs: *concold *temp *headold *head
!                *tempold *presold *conc *pres
!   Plus diff mem management of: concold:in temp:in headold:in
!                head:in tempold:in presold:in conc:in pres:in
!> @brief save current state as an old version
!> @param[in] level level number (which old version)
!> @param[in] ismpl local sample index
SUBROUTINE OMP_OLD_SAVE_AD(level, ismpl)
  use arrays

  USE ARRAYS_AD

  USE MOD_GENRL
  USE MOD_CONC
  IMPLICIT NONE
! local sample index
  INTEGER :: ismpl
! cgen level index
  INTEGER, INTENT(IN) :: level
! directional cell-indices
  INTEGER :: i, j, k
! counter for concentration
  INTEGER :: l
! Start position in array for process
  INTEGER :: tpos
! Number of array elements for process
  INTEGER :: tanz
  INTRINSIC ABS
  INTRINSIC MAX
  INTEGER :: abs0
  INTEGER :: abs1
  INTEGER :: abs2
  INTEGER :: abs3
  INTEGER :: max1
  INTEGER :: max2
  INTEGER :: max3
  INTEGER :: max4
  INTEGER :: branch
! OpenMP partition, get tpos, tanz
  CALL OMP_PART(i0*j0*k0, tpos, tanz)
! Get i, j, k indices for tpos
  CALL IJK_M(tpos, i, j, k)
  IF (ismpl .GE. 0.) THEN
    abs0 = ismpl
  ELSE
    abs0 = -ismpl
  END IF
  IF (1 .LT. ismpl) THEN
    max1 = ismpl
  ELSE
    max1 = 1
  END IF
! Copy all variable arrays to old: var -> varold
  IF (ismpl .GE. 0.) THEN
    abs1 = ismpl
  ELSE
    abs1 = -ismpl
  END IF
  IF (1 .LT. ismpl) THEN
    max2 = ismpl
  ELSE
    max2 = 1
  END IF
  IF (ismpl .GE. 0.) THEN
    abs2 = ismpl
  ELSE
    abs2 = -ismpl
  END IF
  IF (1 .LT. ismpl) THEN
    max3 = ismpl
  ELSE
    max3 = 1
  END IF
  DO l=1,ntrans
    IF (ismpl .GE. 0.) THEN
      CALL PUSHINTEGER8(abs3)
      abs3 = ismpl
      CALL PUSHCONTROL1B(1)
    ELSE
      CALL PUSHINTEGER8(abs3)
      abs3 = -ismpl
      CALL PUSHCONTROL1B(0)
    END IF
    IF (1 .LT. ismpl) THEN
      CALL PUSHINTEGER8(max4)
      max4 = ismpl
      CALL PUSHCONTROL1B(0)
    ELSE
      CALL PUSHINTEGER8(max4)
      max4 = 1
      CALL PUSHCONTROL1B(1)
    END IF
  END DO
  DO l=ntrans,1,-1
    CALL DCOPY_AD(tanz, conc(i, j, k, l, abs3), conc_ad(i, j, k, l, abs3&
&           ), 1, concold(tpos, l, level, max4), concold_ad(tpos, l, &
&           level, max4), 1)
    CALL POPCONTROL1B(branch)
    IF (branch .EQ. 0) THEN
      CALL POPINTEGER8(max4)
    ELSE
      CALL POPINTEGER8(max4)
    END IF
    CALL POPCONTROL1B(branch)
    IF (branch .EQ. 0) THEN
      CALL POPINTEGER8(abs3)
    ELSE
      CALL POPINTEGER8(abs3)
    END IF
  END DO
  CALL DCOPY_AD(tanz, pres(i, j, k, abs2), pres_ad(i, j, k, abs2), 1, &
&         presold(tpos, level, max3), presold_ad(tpos, level, max3), 1)
  CALL DCOPY_AD(tanz, temp(i, j, k, abs1), temp_ad(i, j, k, abs1), 1, &
&         tempold(tpos, level, max2), tempold_ad(tpos, level, max2), 1)
  CALL DCOPY_AD(tanz, head(i, j, k, abs0), head_ad(i, j, k, abs0), 1, &
&         headold(tpos, level, max1), headold_ad(tpos, level, max1), 1)
END SUBROUTINE OMP_OLD_SAVE_AD

