Back to home page

MITgcm

 
 

    


File indexing completed on 2024-03-02 06:10:08 UTC

view on githubraw file Latest commit 5cf43646 on 2024-03-01 18:50:49 UTC
6d54cf9ca1 Ed H*0001 #include "AUTODIFF_OPTIONS.h"
bead363026 Jean*0002 #ifdef ALLOW_CTRL
                0003 # include "CTRL_OPTIONS.h"
                0004 #endif
4158fdaabc Patr*0005 
50d8204771 Jean*0006 C     ==================================================================
                0007 C     active_file_control.F: Routines to handle the I/O of active
                0008 C                            variables for the adjoint calculations.
                0009 C                            All files are direct access files.
                0010 C     Routines:
                0011 C     o  ACTIVE_READ_3D_RL  : Basic routine to handle active 3D read operations
                0012 C     o  ACTIVE_READ_3D_RS  : Basic routine to handle active 3D read operations
04904a066e Timo*0013 C     o  ACTIVE_READ_1D_RL  : Basic routine to handle active 1D read operations
                0014 C     o  ACTIVE_READ_1D_RS  : Basic routine to handle active 1D read operations
50d8204771 Jean*0015 C     o  ACTIVE_WRITE_3D_RL : Basic routine to handle active 3D write operations
                0016 C     o  ACTIVE_WRITE_3D_RS : Basic routine to handle active 3D write operations
04904a066e Timo*0017 C     o  ACTIVE_WRITE_1D_RL  : Basic routine to handle active 1D write operations
                0018 C     o  ACTIVE_WRITE_1D_RS  : Basic routine to handle active 1D write operations
50d8204771 Jean*0019 C
                0020 C        changed: Christian Eckert eckert@mit.edu 24-Apr-2000
                0021 C        - Added routines that do active writes on tiles
                0022 C                   instead of a whole thread.
                0023 C     ==================================================================
                0024 
                0025 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
                0026 CBOP
                0027 C     !ROUTINE: ACTIVE_READ_3D_RL
                0028 C     !INTERFACE:
                0029       SUBROUTINE ACTIVE_READ_3D_RL(
                0030      I                          activeVar_file,
                0031      O                          active_var,
                0032      I                          globalFile,
                0033      I                          useCurrentDir,
                0034      I                          lAdInit,
                0035      I                          iRec,
                0036      I                          myNr,
                0037      I                          theSimulationMode,
                0038      I                          myOptimIter,
                0039      I                          myThid )
                0040 C     !DESCRIPTION: \bv
                0041 C     ==================================================================
                0042 C     SUBROUTINE ACTIVE_READ_3D_RL
                0043 C     ==================================================================
                0044 C     o Read an active 3D _RL variable from file.
                0045 C     The variable *globalfile* can be used as a switch, which allows
                0046 C     to read from a global file. The adjoint files are, however, always
                0047 C     treated as tiled files.
                0048 C     started: Christian Eckert eckert@mit.edu    Jan-1999
                0049 C     ==================================================================
                0050 C     SUBROUTINE ACTIVE_READ_3D_RL
                0051 C     ==================================================================
                0052 C     \ev
4158fdaabc Patr*0053 
50d8204771 Jean*0054 C     !USES:
                0055       IMPLICIT NONE
4158fdaabc Patr*0056 
50d8204771 Jean*0057 C     == global variables ==
                0058 #include "EEPARAMS.h"
                0059 #include "SIZE.h"
                0060 #include "PARAMS.h"
5cf4364659 Mart*0061 #include "CTRL_SIZE.h"
4d72283393 Mart*0062 #include "CTRL.h"
50d8204771 Jean*0063 
                0064 C     !INPUT/OUTPUT PARAMETERS:
                0065 C     activeVar_file :: filename
                0066 C     active_var     :: array
                0067 C     globalFile     ::
                0068 C     useCurrentDir  :: always read from the current directory
                0069 C                        (even if "mdsioLocalDir" is set)
                0070 C     lAdInit        :: initialisation of corresponding adjoint variable
                0071 C                        and write to active file
                0072 C     iRec           :: record number
                0073 C     myNr           :: vertical array dimension
                0074 C     theSimulationMode :: forward mode or reverse mode simulation
                0075 C     myOptimIter    :: number of optimization iteration (default: 0)
                0076 C     myThid         :: thread number for this instance
                0077       CHARACTER*(*) activeVar_file
                0078       INTEGER  myNr
                0079       _RL      active_var(1-OLx:sNx+OLx,1-OLy:sNy+OLy,myNr,nSx,nSy)
                0080       LOGICAL  globalFile
                0081       LOGICAL  useCurrentDir
                0082       LOGICAL  lAdInit
                0083       INTEGER  iRec
                0084       INTEGER  theSimulationMode
                0085       INTEGER  myOptimIter
                0086       INTEGER  myThid
                0087 
                0088 C     !LOCAL VARIABLES:
                0089       CHARACTER*(2)  adpref
de57a2ec4b Mart*0090       CHARACTER*(MAX_LEN_FNAM) adfname
50d8204771 Jean*0091       INTEGER bi,bj
                0092       INTEGER i,j,k
32ce3e9e96 Mart*0093       INTEGER jRec
50d8204771 Jean*0094       INTEGER prec
                0095       LOGICAL w_globFile
                0096       _RS  dummyRS(1)
a1572bcee2 Gael*0097       _RL  active_data_t(1-OLx:sNx+OLx,1-OLy:sNy+OLy,Nr,nSx,nSy)
50d8204771 Jean*0098 CEOP
                0099 
                0100 C     force 64-bit io
                0101       prec = ctrlprec
32ce3e9e96 Mart*0102       jRec = -ABS(iRec)
50d8204771 Jean*0103 
                0104 C     >>>>>>>>>>>>>>>>>>> FORWARD RUN <<<<<<<<<<<<<<<<<<<
                0105       IF (theSimulationMode .EQ. FORWARD_SIMULATION) THEN
                0106 
                0107 C     Read the active variable from file.
                0108         CALL MDS_READ_FIELD(
                0109      I                activeVar_file, prec, useCurrentDir,
                0110      I                'RL', myNr, 1, myNr,
                0111      O                active_var, dummyRS,
                0112      I                iRec, myThid )
                0113 
                0114         IF ( lAdInit ) THEN
                0115 C     Initialise the corresponding adjoint variable on the
                0116 C     adjoint variable file. These files are tiled.
                0117 
                0118           DO bj = myByLo(myThid), myByHi(myThid)
                0119            DO bi = myBxLo(myThid), myBxHi(myThid)
                0120             DO k = 1, myNr
                0121              DO j=1,sNy
                0122               DO i=1,sNx
                0123                 active_data_t(i,j,k,bi,bj) = 0. _d 0
                0124               ENDDO
                0125              ENDDO
                0126             ENDDO
                0127            ENDDO
                0128           ENDDO
                0129 
f9d7cbfb72 Ou W*0130           adpref = 'ad'
                0131           CALL ADD_PREFIX( adpref, activeVar_file, adfname )
50d8204771 Jean*0132           CALL MDS_WRITE_FIELD(
                0133      I                adfname, prec, globalFile, useCurrentDir,
a1572bcee2 Gael*0134      I                'RL', Nr, 1, myNr,
50d8204771 Jean*0135      I                active_data_t, dummyRS,
                0136      I                iRec, myOptimIter, myThid )
                0137 
                0138         ENDIF
                0139 
                0140       ENDIF
                0141 
                0142 C     >>>>>>>>>>>>>>>>>>> ADJOINT RUN <<<<<<<<<<<<<<<<<<<
                0143       IF (theSimulationMode .EQ. REVERSE_SIMULATION) THEN
                0144 
                0145         CALL MDS_READ_FIELD(
                0146      I                activeVar_file, prec, useCurrentDir,
a1572bcee2 Gael*0147      I                'RL', Nr, 1, myNr,
50d8204771 Jean*0148      O                active_data_t, dummyRS,
                0149      I                iRec, myThid )
                0150 
                0151 C     Add active_var from appropriate location to data.
                0152         DO bj = myByLo(myThid), myByHi(myThid)
                0153          DO bi = myBxLo(myThid), myBxHi(myThid)
                0154           DO k = 1, myNr
                0155            DO j=1,sNy
                0156             DO i=1,sNx
                0157               active_data_t(i,j,k,bi,bj) = active_data_t(i,j,k,bi,bj)
                0158      &                                   + active_var(i,j,k,bi,bj)
                0159             ENDDO
                0160            ENDDO
                0161           ENDDO
                0162          ENDDO
                0163         ENDDO
                0164 
                0165 C     Store the result on disk.
                0166         w_globFile = .FALSE.
                0167         CALL MDS_WRITE_FIELD(
                0168      I                activeVar_file, prec, w_globFile, useCurrentDir,
a1572bcee2 Gael*0169      I                'RL', Nr, 1, myNr,
50d8204771 Jean*0170      I                active_data_t, dummyRS,
32ce3e9e96 Mart*0171      I                jRec, myOptimIter, myThid )
50d8204771 Jean*0172 
                0173 C     Set active_var to zero.
                0174         DO bj = myByLo(myThid), myByHi(myThid)
                0175          DO bi = myBxLo(myThid), myBxHi(myThid)
                0176           DO k = 1, myNr
                0177            DO j=1,sNy
                0178             DO i=1,sNx
                0179               active_var(i,j,k,bi,bj) = 0 _d 0
                0180             ENDDO
                0181            ENDDO
                0182           ENDDO
                0183          ENDDO
                0184         ENDDO
                0185 
                0186       ENDIF
                0187 
                0188 C     >>>>>>>>>>>>>>>>>>> TANGENT RUN <<<<<<<<<<<<<<<<<<<
                0189       IF (theSimulationMode .EQ. TANGENT_SIMULATION) THEN
                0190 C     Read the active variable from file.
                0191         CALL MDS_READ_FIELD(
                0192      I                activeVar_file, prec, useCurrentDir,
                0193      I                'RL', myNr, 1, myNr,
                0194      O                active_var, dummyRS,
                0195      I                iRec, myThid )
                0196       ENDIF
                0197 
                0198       RETURN
                0199       END
                0200 
                0201 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
4158fdaabc Patr*0202 CBOP
50d8204771 Jean*0203 C     !ROUTINE: ACTIVE_READ_3D_RS
4158fdaabc Patr*0204 C     !INTERFACE:
50d8204771 Jean*0205       SUBROUTINE ACTIVE_READ_3D_RS(
                0206      I                          activeVar_file,
                0207      O                          active_var,
                0208      I                          globalFile,
                0209      I                          useCurrentDir,
                0210      I                          lAdInit,
                0211      I                          iRec,
                0212      I                          myNr,
                0213      I                          theSimulationMode,
                0214      I                          myOptimIter,
                0215      I                          myThid )
4158fdaabc Patr*0216 
                0217 C     !DESCRIPTION: \bv
50d8204771 Jean*0218 C     ==================================================================
                0219 C     SUBROUTINE ACTIVE_READ_3D_RS
                0220 C     ==================================================================
                0221 C     o Read an active 3D _RS variable from file.
                0222 C     The variable *globalfile* can be used as a switch, which allows
                0223 C     to read from a global file. The adjoint files are, however, always
                0224 C     treated as tiled files.
                0225 C     started: Christian Eckert eckert@mit.edu    Jan-1999
                0226 C     ==================================================================
                0227 C     SUBROUTINE ACTIVE_READ_3D_RS
                0228 C     ==================================================================
4158fdaabc Patr*0229 C     \ev
                0230 
                0231 C     !USES:
50d8204771 Jean*0232       IMPLICIT NONE
4158fdaabc Patr*0233 
50d8204771 Jean*0234 C     == global variables ==
4158fdaabc Patr*0235 #include "EEPARAMS.h"
                0236 #include "SIZE.h"
                0237 #include "PARAMS.h"
5cf4364659 Mart*0238 #include "CTRL_SIZE.h"
4d72283393 Mart*0239 #include "CTRL.h"
4158fdaabc Patr*0240 
                0241 C     !INPUT/OUTPUT PARAMETERS:
50d8204771 Jean*0242 C     activeVar_file :: filename
                0243 C     active_var     :: array
                0244 C     globalFile     ::
                0245 C     useCurrentDir  :: always read from the current directory
                0246 C                        (even if "mdsioLocalDir" is set)
                0247 C     lAdInit        :: initialisation of corresponding adjoint variable
                0248 C                        and write to active file
                0249 C     iRec           :: record number
                0250 C     myNr           :: vertical array dimension
                0251 C     theSimulationMode :: forward mode or reverse mode simulation
                0252 C     myOptimIter    :: number of optimization iteration (default: 0)
                0253 C     myThid         :: thread number for this instance
                0254       CHARACTER*(*) activeVar_file
                0255       INTEGER  myNr
                0256       _RS      active_var(1-OLx:sNx+OLx,1-OLy:sNy+OLy,myNr,nSx,nSy)
                0257       LOGICAL  globalFile
                0258       LOGICAL  useCurrentDir
                0259       LOGICAL  lAdInit
                0260       INTEGER  iRec
                0261       INTEGER  theSimulationMode
                0262       INTEGER  myOptimIter
                0263       INTEGER  myThid
                0264 
4158fdaabc Patr*0265 C     !LOCAL VARIABLES:
50d8204771 Jean*0266       CHARACTER*(2)  adpref
de57a2ec4b Mart*0267       CHARACTER*(MAX_LEN_FNAM) adfname
50d8204771 Jean*0268       INTEGER bi,bj
                0269       INTEGER i,j,k
32ce3e9e96 Mart*0270       INTEGER jRec
50d8204771 Jean*0271       INTEGER prec
                0272       LOGICAL w_globFile
a1572bcee2 Gael*0273       _RS  active_data_t(1-OLx:sNx+OLx,1-OLy:sNy+OLy,Nr,nSx,nSy)
50d8204771 Jean*0274       _RL  dummyRL(1)
4158fdaabc Patr*0275 CEOP
                0276 
50d8204771 Jean*0277 C     force 64-bit io
                0278       prec = ctrlprec
32ce3e9e96 Mart*0279       jRec = -ABS(iRec)
4158fdaabc Patr*0280 
50d8204771 Jean*0281 C     >>>>>>>>>>>>>>>>>>> FORWARD RUN <<<<<<<<<<<<<<<<<<<
                0282       IF (theSimulationMode .EQ. FORWARD_SIMULATION) THEN
                0283 
                0284 C     Read the active variable from file.
                0285         CALL MDS_READ_FIELD(
                0286      I                activeVar_file, prec, useCurrentDir,
                0287      I                'RS', myNr, 1, myNr,
                0288      O                dummyRL, active_var,
                0289      I                iRec, myThid )
                0290 
                0291         IF ( lAdInit ) THEN
                0292 C     Initialise the corresponding adjoint variable on the
                0293 C     adjoint variable file. These files are tiled.
                0294 
                0295           DO bj = myByLo(myThid), myByHi(myThid)
                0296            DO bi = myBxLo(myThid), myBxHi(myThid)
                0297             DO k = 1, myNr
                0298              DO j=1,sNy
                0299               DO i=1,sNx
                0300                 active_data_t(i,j,k,bi,bj) = 0. _d 0
                0301               ENDDO
                0302              ENDDO
                0303             ENDDO
                0304            ENDDO
                0305           ENDDO
                0306 
f9d7cbfb72 Ou W*0307           adpref = 'ad'
                0308           CALL ADD_PREFIX( adpref, activeVar_file, adfname )
50d8204771 Jean*0309           CALL MDS_WRITE_FIELD(
                0310      I                adfname, prec, globalFile, useCurrentDir,
a1572bcee2 Gael*0311      I                'RS', Nr, 1, myNr,
50d8204771 Jean*0312      I                dummyRL, active_data_t,
                0313      I                iRec, myOptimIter, myThid )
                0314 
                0315         ENDIF
                0316 
                0317       ENDIF
                0318 
                0319 C     >>>>>>>>>>>>>>>>>>> ADJOINT RUN <<<<<<<<<<<<<<<<<<<
                0320       IF (theSimulationMode .EQ. REVERSE_SIMULATION) THEN
                0321 
                0322         CALL MDS_READ_FIELD(
                0323      I                activeVar_file, prec, useCurrentDir,
a1572bcee2 Gael*0324      I                'RS', Nr, 1, myNr,
50d8204771 Jean*0325      O                dummyRL, active_data_t,
                0326      I                iRec, myThid )
                0327 
                0328 C     Add active_var from appropriate location to data.
                0329         DO bj = myByLo(myThid), myByHi(myThid)
                0330          DO bi = myBxLo(myThid), myBxHi(myThid)
                0331           DO k = 1, myNr
                0332            DO j=1,sNy
                0333             DO i=1,sNx
                0334               active_data_t(i,j,k,bi,bj) = active_data_t(i,j,k,bi,bj)
                0335      &                                   + active_var(i,j,k,bi,bj)
                0336             ENDDO
                0337            ENDDO
                0338           ENDDO
                0339          ENDDO
                0340         ENDDO
                0341 
                0342 C     Store the result on disk.
                0343         w_globFile = .FALSE.
                0344         CALL MDS_WRITE_FIELD(
                0345      I                activeVar_file, prec, w_globFile, useCurrentDir,
a1572bcee2 Gael*0346      I                'RS', Nr, 1, myNr,
50d8204771 Jean*0347      I                dummyRL, active_data_t,
32ce3e9e96 Mart*0348      I                jRec, myOptimIter, myThid )
50d8204771 Jean*0349 
                0350 C     Set active_var to zero.
                0351         DO bj = myByLo(myThid), myByHi(myThid)
                0352          DO bi = myBxLo(myThid), myBxHi(myThid)
                0353           DO k = 1, myNr
                0354            DO j=1,sNy
                0355             DO i=1,sNx
                0356               active_var(i,j,k,bi,bj) = 0 _d 0
                0357             ENDDO
                0358            ENDDO
                0359           ENDDO
                0360          ENDDO
                0361         ENDDO
                0362 
                0363       ENDIF
                0364 
                0365 C     >>>>>>>>>>>>>>>>>>> TANGENT RUN <<<<<<<<<<<<<<<<<<<
                0366       IF (theSimulationMode .EQ. TANGENT_SIMULATION) THEN
                0367 C     Read the active variable from file.
                0368         CALL MDS_READ_FIELD(
                0369      I                activeVar_file, prec, useCurrentDir,
                0370      I                'RS', myNr, 1, myNr,
                0371      O                dummyRL, active_var,
                0372      I                iRec, myThid )
                0373       ENDIF
                0374 
                0375       RETURN
                0376       END
                0377 
                0378 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
                0379 CBOP
04904a066e Timo*0380 C     !ROUTINE: ACTIVE_READ_1D_RL
                0381 C     !INTERFACE:
                0382       SUBROUTINE ACTIVE_READ_1D_RL(
                0383      I                          activeVar_file,
                0384      O                          active_var,
                0385      I                          active_var_length,
                0386      I                          lAdInit,
                0387      I                          iRec,
                0388      I                          theSimulationMode,
                0389      I                          myOptimIter,
                0390      I                          myThid )
                0391 C     !DESCRIPTION: \bv
                0392 C     ==================================================================
                0393 C     SUBROUTINE ACTIVE_READ_1D_RL
                0394 C     ==================================================================
                0395 C     o Read an active 1D _RL vector from file.
                0396 C     o Read the same 1D vector to all tiles/processes, so global by
                0397 C       nature
                0398 C     o Note: rely on implicit dynamical allocation (local array size
                0399 C       is an argument of this S/R) for local array "active_data_t"
                0400 C     Modified from 3D: Tim Smith tsmith@oden.utexas.edu Oct-2019
                0401 C     ==================================================================
                0402 C     SUBROUTINE ACTIVE_READ_1D_RL
                0403 C     ==================================================================
                0404 C     \ev
                0405 
                0406 C     !USES:
                0407       IMPLICIT NONE
                0408 
                0409 C     == global variables ==
                0410 #include "EEPARAMS.h"
                0411 #include "SIZE.h"
                0412 #include "PARAMS.h"
5cf4364659 Mart*0413 #include "CTRL_SIZE.h"
4d72283393 Mart*0414 #include "CTRL.h"
04904a066e Timo*0415 
                0416 C     !INPUT/OUTPUT PARAMETERS:
                0417 C     activeVar_file :: filename
                0418 C     active_var     :: vector
                0419 C     active_var_length :: number of elements in active variable
                0420 C     useCurrentDir  :: always read from the current directory
                0421 C                        (even if "mdsioLocalDir" is set)
                0422 C     lAdInit        :: initialisation of corresponding adjoint variable
                0423 C                        and write to active file
                0424 C     theSimulationMode :: forward, tangent linear, or reverse mode simulation
                0425 C     myOptimIter    :: number of optimization iteration (default: 0)
                0426 C     myThid         :: thread number for this instance
                0427       CHARACTER*(*) activeVar_file
                0428       _RL      active_var(*)
                0429       INTEGER  active_var_length
                0430       LOGICAL  lAdInit
                0431       INTEGER  iRec
                0432       INTEGER  theSimulationMode
                0433       INTEGER  myOptimIter
                0434       INTEGER  myThid
                0435 
                0436 C     !FUNCTIONS:
                0437       INTEGER  ILNBLNK
                0438       EXTERNAL ILNBLNK
                0439 
                0440 C     !LOCAL VARIABLES:
                0441       CHARACTER*(2)  adpref
de57a2ec4b Mart*0442       CHARACTER*(MAX_LEN_FNAM) adfname
04904a066e Timo*0443       INTEGER i, bi, bj
32ce3e9e96 Mart*0444       INTEGER jRec
04904a066e Timo*0445       INTEGER prec
                0446       INTEGER il
                0447       INTEGER ioUnit
                0448       _RS  dummyRS(1)
                0449       _RL  active_data_t(active_var_length)
                0450 CEOP
                0451 
                0452 C     force 64-bit io
                0453       prec = ctrlprec
32ce3e9e96 Mart*0454       jRec = -ABS(iRec)
04904a066e Timo*0455 
                0456 C     set these to zero otherwise looks for tiled file
                0457       bi=0
                0458       bj=0
                0459 
                0460 C     ioUnit = 0 => open, read, and close the file... see mdsio_readvec_loc
                0461       ioUnit = 0
                0462 
                0463 C     >>>>>>>>>>>>>>>>>>> FORWARD RUN <<<<<<<<<<<<<<<<<<<
                0464       IF (theSimulationMode .EQ. FORWARD_SIMULATION) THEN
                0465 
                0466 C     Read the active variable from file.
                0467         CALL MDS_READVEC_LOC(
                0468      I                activeVar_file, prec, ioUnit,
                0469      I                'RL', active_var_length,
                0470      O                active_var, dummyRS,
                0471      I                bi, bj,
                0472      I                iRec, myThid )
                0473 
                0474         IF ( lAdInit ) THEN
                0475 C     Initialise the corresponding adjoint variable on the
                0476 C     adjoint variable file. These files are tiled.
                0477 
                0478           DO i = 1, active_var_length
                0479             active_data_t(i) = 0. _d 0
                0480           ENDDO
f9d7cbfb72 Ou W*0481 
                0482           adpref = 'ad'
                0483           il = ILNBLNK( activeVar_file )
                0484           WRITE(adfname,'(2A)') adpref, activeVar_file(1:il)
04904a066e Timo*0485           IF ( myProcId.EQ.0 ) THEN
                0486             CALL MDS_WRITEVEC_LOC(
                0487      I                adfname, prec, ioUnit,
                0488      I                'RL', active_var_length,
                0489      O                active_data_t, dummyRS,
                0490      I                bi, bj,
                0491      I                iRec, myOptimIter, myThid )
                0492 
                0493           ENDIF
                0494         ENDIF
                0495 
                0496       ENDIF
                0497 
                0498 C     >>>>>>>>>>>>>>>>>>> ADJOINT RUN <<<<<<<<<<<<<<<<<<<
                0499       IF (theSimulationMode .EQ. REVERSE_SIMULATION) THEN
                0500 
                0501         CALL MDS_READVEC_LOC(
                0502      I                activeVar_file, prec, ioUnit,
                0503      I                'RL', active_var_length,
                0504      O                active_data_t, dummyRS,
                0505      I                bi, bj,
                0506      I                iRec, myThid )
                0507 
                0508 C     Add active_var from appropriate location to data.
                0509         DO i = 1, active_var_length
                0510           active_data_t(i) = active_data_t(i) + active_var(i)
                0511         ENDDO
                0512 
                0513 C     Store the result on disk.
                0514         IF ( myProcId.EQ.0 ) THEN
                0515           CALL MDS_WRITEVEC_LOC(
                0516      I                activeVar_file, prec, ioUnit,
                0517      I                'RL', active_var_length,
                0518      O                active_data_t, dummyRS,
                0519      I                bi, bj,
32ce3e9e96 Mart*0520      I                jRec, myOptimIter, myThid )
04904a066e Timo*0521         ENDIF
                0522 
                0523 C     Set active_var to zero.
                0524         DO i = 1, active_var_length
                0525           active_data_t(i) = 0. _d 0
                0526         ENDDO
                0527 
                0528       ENDIF
                0529 
                0530 C     >>>>>>>>>>>>>>>>>>> TANGENT RUN <<<<<<<<<<<<<<<<<<<
                0531       IF (theSimulationMode .EQ. TANGENT_SIMULATION) THEN
                0532 C     Read the active variable from file.
                0533         CALL MDS_READVEC_LOC(
                0534      I                activeVar_file, prec, ioUnit,
                0535      I                'RL', active_var_length,
                0536      O                active_var, dummyRS,
                0537      I                bi, bj,
                0538      I                iRec, myThid )
                0539       ENDIF
                0540 
                0541       RETURN
                0542       END
                0543 
                0544 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
                0545 CBOP
                0546 C     !ROUTINE: ACTIVE_READ_1D_RS
                0547 C     !INTERFACE:
                0548       SUBROUTINE ACTIVE_READ_1D_RS(
                0549      I                          activeVar_file,
                0550      O                          active_var,
                0551      I                          active_var_length,
                0552      I                          lAdInit,
                0553      I                          iRec,
                0554      I                          theSimulationMode,
                0555      I                          myOptimIter,
                0556      I                          myThid )
                0557 C     !DESCRIPTION: \bv
                0558 C     ==================================================================
                0559 C     SUBROUTINE ACTIVE_READ_1D_RS
                0560 C     ==================================================================
                0561 C     o Read an active 1D _RS vector from file.
                0562 C     o Read the same 1D vector to all tiles/processes, so global by
                0563 C       nature
                0564 C     o Note: rely on implicit dynamical allocation (local array size
                0565 C       is an argument of this S/R) for local array "active_data_t"
                0566 C     Modified from 3D: Tim Smith tsmith@oden.utexas.edu Oct-2019
                0567 C     ==================================================================
                0568 C     SUBROUTINE ACTIVE_READ_1D_RS
                0569 C     ==================================================================
                0570 C     \ev
                0571 
                0572 C     !USES:
                0573       IMPLICIT NONE
                0574 
                0575 C     == global variables ==
                0576 #include "EEPARAMS.h"
                0577 #include "SIZE.h"
                0578 #include "PARAMS.h"
5cf4364659 Mart*0579 #include "CTRL_SIZE.h"
4d72283393 Mart*0580 #include "CTRL.h"
04904a066e Timo*0581 
                0582 C     !INPUT/OUTPUT PARAMETERS:
                0583 C     activeVar_file :: filename
                0584 C     active_var     :: vector
                0585 C     active_var_length :: number of elements in active variable
                0586 C     useCurrentDir  :: always read from the current directory
                0587 C                        (even if "mdsioLocalDir" is set)
                0588 C     lAdInit        :: initialisation of corresponding adjoint variable
                0589 C                        and write to active file
                0590 C     theSimulationMode :: forward, tangent linear, or reverse mode simulation
                0591 C     myOptimIter    :: number of optimization iteration (default: 0)
                0592 C     myThid         :: thread number for this instance
                0593       CHARACTER*(*) activeVar_file
                0594       _RS      active_var(*)
                0595       INTEGER  active_var_length
                0596       LOGICAL  lAdInit
                0597       INTEGER  iRec
                0598       INTEGER  theSimulationMode
                0599       INTEGER  myOptimIter
                0600       INTEGER  myThid
                0601 
                0602 C     !FUNCTIONS:
                0603       INTEGER  ILNBLNK
                0604       EXTERNAL ILNBLNK
                0605 
                0606 C     !LOCAL VARIABLES:
                0607       CHARACTER*(2)  adpref
de57a2ec4b Mart*0608       CHARACTER*(MAX_LEN_FNAM) adfname
04904a066e Timo*0609       INTEGER i, bi, bj
32ce3e9e96 Mart*0610       INTEGER jRec
04904a066e Timo*0611       INTEGER prec
                0612       INTEGER il
                0613       INTEGER ioUnit
                0614       _RS  active_data_t(active_var_length)
                0615       _RL  dummyRL(1)
                0616 CEOP
                0617 
                0618 C     force 64-bit io
                0619       prec = ctrlprec
32ce3e9e96 Mart*0620       jRec = -ABS(iRec)
04904a066e Timo*0621 
                0622 C     set these to zero otherwise looks for tiled file
                0623       bi=0
                0624       bj=0
                0625 
                0626 C     ioUnit = 0 => open, read, and close the file... see mdsio_readvec_loc
                0627       ioUnit = 0
                0628 
                0629 C     >>>>>>>>>>>>>>>>>>> FORWARD RUN <<<<<<<<<<<<<<<<<<<
                0630       IF (theSimulationMode .EQ. FORWARD_SIMULATION) THEN
                0631 
                0632 C     Read the active variable from file.
                0633         CALL MDS_READVEC_LOC(
                0634      I                activeVar_file, prec, ioUnit,
                0635      I                'RS', active_var_length,
                0636      O                dummyRL, active_var,
                0637      I                bi, bj,
                0638      I                iRec, myThid )
                0639 
                0640         IF ( lAdInit ) THEN
                0641 C     Initialise the corresponding adjoint variable on the
                0642 C     adjoint variable file. These files are tiled.
                0643 
                0644           DO i = 1, active_var_length
                0645             active_data_t(i) = 0. _d 0
                0646           ENDDO
f9d7cbfb72 Ou W*0647 
                0648           adpref = 'ad'
                0649           il = ILNBLNK( activeVar_file )
                0650           WRITE(adfname,'(2A)') adpref, activeVar_file(1:il)
04904a066e Timo*0651           IF ( myProcId.EQ.0 ) THEN
                0652             CALL MDS_WRITEVEC_LOC(
                0653      I                adfname, prec, ioUnit,
                0654      I                'RS', active_var_length,
                0655      O                dummyRL, active_data_t,
                0656      I                bi, bj,
                0657      I                iRec, myOptimIter, myThid )
                0658 
                0659           ENDIF
                0660         ENDIF
                0661 
                0662       ENDIF
                0663 
                0664 C     >>>>>>>>>>>>>>>>>>> ADJOINT RUN <<<<<<<<<<<<<<<<<<<
                0665       IF (theSimulationMode .EQ. REVERSE_SIMULATION) THEN
                0666 
                0667         CALL MDS_READVEC_LOC(
                0668      I                activeVar_file, prec, ioUnit,
                0669      I                'RS', active_var_length,
                0670      O                dummyRL, active_data_t,
                0671      I                bi, bj,
                0672      I                iRec, myThid )
                0673 
                0674 C     Add active_var from appropriate location to data.
                0675         DO i = 1, active_var_length
                0676           active_data_t(i) = active_data_t(i) + active_var(i)
                0677         ENDDO
                0678 
                0679 C     Store the result on disk.
                0680         IF ( myProcId.EQ.0 ) THEN
                0681           CALL MDS_WRITEVEC_LOC(
                0682      I                activeVar_file, prec, ioUnit,
                0683      I                'RS', active_var_length,
                0684      O                dummyRL, active_data_t,
                0685      I                bi, bj,
32ce3e9e96 Mart*0686      I                jRec, myOptimIter, myThid )
04904a066e Timo*0687 
                0688         ENDIF
                0689 C     Set active_var to zero.
                0690         DO i = 1, active_var_length
                0691           active_data_t(i) = 0. _d 0
                0692         ENDDO
                0693 
                0694       ENDIF
                0695 
                0696 C     >>>>>>>>>>>>>>>>>>> TANGENT RUN <<<<<<<<<<<<<<<<<<<
                0697       IF (theSimulationMode .EQ. TANGENT_SIMULATION) THEN
                0698 C     Read the active variable from file.
                0699         CALL MDS_READVEC_LOC(
                0700      I                activeVar_file, prec, ioUnit,
                0701      I                'RS', active_var_length,
                0702      O                dummyRL, active_var,
                0703      I                bi, bj,
                0704      I                iRec, myThid )
                0705       ENDIF
                0706 
                0707       RETURN
                0708       END
                0709 
                0710 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
                0711 CBOP
50d8204771 Jean*0712 C     !ROUTINE: ACTIVE_WRITE_3D_RL
                0713 C     !INTERFACE:
                0714       SUBROUTINE ACTIVE_WRITE_3D_RL(
                0715      I                          activeVar_file,
                0716      I                          active_var,
                0717      I                          globalFile,
                0718      I                          useCurrentDir,
                0719      I                          iRec,
                0720      I                          myNr,
                0721      I                          theSimulationMode,
                0722      I                          myOptimIter,
                0723      I                          myThid )
                0724 
                0725 C     !DESCRIPTION: \bv
                0726 C     ==================================================================
                0727 C     SUBROUTINE ACTIVE_WRITE_3D_RL
                0728 C     ==================================================================
                0729 C     o Write an active 3D _RL variable to a file.
                0730 C     started: Christian Eckert eckert@mit.edu    Jan-1999
                0731 C     ==================================================================
                0732 C     SUBROUTINE ACTIVE_WRITE_3D_RL
                0733 C     ==================================================================
                0734 C     \ev
                0735 
                0736 C     !USES:
                0737       IMPLICIT NONE
                0738 
                0739 C     == global variables ==
                0740 #include "EEPARAMS.h"
                0741 #include "SIZE.h"
                0742 #include "PARAMS.h"
5cf4364659 Mart*0743 #include "CTRL_SIZE.h"
4d72283393 Mart*0744 #include "CTRL.h"
4158fdaabc Patr*0745 
50d8204771 Jean*0746 C     !INPUT/OUTPUT PARAMETERS:
                0747 C     activeVar_file :: filename
                0748 C     active_var     :: array
                0749 C     globalFile     ::
                0750 C     useCurrentDir  :: always write to the current directory
                0751 C                        (even if "mdsioLocalDir" is set)
                0752 C     iRec           :: record number
                0753 C     myNr           :: vertical array dimension
                0754 C     theSimulationMode :: forward mode or reverse mode simulation
                0755 C     myOptimIter    :: number of optimization iteration (default: 0)
                0756 C     myThid         :: thread number for this instance
                0757       CHARACTER*(*) activeVar_file
                0758       INTEGER  myNr
                0759       _RL      active_var(1-OLx:sNx+OLx,1-OLy:sNy+OLy,myNr,nSx,nSy)
                0760       LOGICAL  globalFile
                0761       LOGICAL  useCurrentDir
                0762       INTEGER  iRec
                0763       INTEGER  theSimulationMode
                0764       INTEGER  myOptimIter
                0765       INTEGER  myThid
                0766 
                0767 C     !LOCAL VARIABLES:
                0768       INTEGER  i,j,k
                0769       INTEGER  bi,bj
32ce3e9e96 Mart*0770       INTEGER  jRec
50d8204771 Jean*0771       INTEGER  prec
                0772       _RS  dummyRS(1)
a1572bcee2 Gael*0773       _RL  active_data_t(1-OLx:sNx+OLx,1-OLy:sNy+OLy,Nr,nSx,nSy)
50d8204771 Jean*0774 CEOP
4158fdaabc Patr*0775 
50d8204771 Jean*0776 C     force 64-bit io
                0777       prec = ctrlprec
32ce3e9e96 Mart*0778       jRec = -ABS(iRec)
50d8204771 Jean*0779 
                0780 C     >>>>>>>>>>>>>>>>>>> FORWARD RUN <<<<<<<<<<<<<<<<<<<
                0781       IF (theSimulationMode .EQ. FORWARD_SIMULATION) THEN
                0782         CALL MDS_WRITE_FIELD(
                0783      I                activeVar_file, prec, globalFile, useCurrentDir,
                0784      I                'RL', myNr, 1, myNr,
                0785      I                active_var, dummyRS,
                0786      I                iRec, myOptimIter, myThid )
                0787       ENDIF
                0788 
                0789 C     >>>>>>>>>>>>>>>>>>> ADJOINT RUN <<<<<<<<<<<<<<<<<<<
                0790       IF (theSimulationMode .EQ. REVERSE_SIMULATION) THEN
                0791 
                0792         CALL MDS_READ_FIELD(
                0793      I                activeVar_file, prec, useCurrentDir,
a1572bcee2 Gael*0794      I                'RL', Nr, 1, myNr,
50d8204771 Jean*0795      O                active_data_t, dummyRS,
                0796      I                iRec, myThid )
                0797 
                0798 C     Add active_var from appropriate location to data.
                0799         DO bj = myByLo(myThid), myByHi(myThid)
                0800          DO bi = myBxLo(myThid), myBxHi(myThid)
                0801           DO k = 1, myNr
                0802            DO j=1,sNy
                0803             DO i=1,sNx
                0804               active_var(i,j,k,bi,bj) = active_var(i,j,k,bi,bj)
                0805      &                                + active_data_t(i,j,k,bi,bj)
                0806               active_data_t(i,j,k,bi,bj) = 0. _d 0
                0807             ENDDO
                0808            ENDDO
                0809           ENDDO
                0810          ENDDO
                0811         ENDDO
                0812         CALL MDS_WRITE_FIELD(
                0813      I                activeVar_file, prec, globalFile, useCurrentDir,
a1572bcee2 Gael*0814      I                'RL', Nr, 1, myNr,
50d8204771 Jean*0815      I                active_data_t, dummyRS,
32ce3e9e96 Mart*0816      I                jRec, myOptimIter, myThid )
50d8204771 Jean*0817 
                0818       ENDIF
                0819 
                0820 C     >>>>>>>>>>>>>>>>>>> TANGENT RUN <<<<<<<<<<<<<<<<<<<
                0821       IF (theSimulationMode .EQ. TANGENT_SIMULATION) THEN
                0822         CALL MDS_WRITE_FIELD(
                0823      I                activeVar_file, prec, globalFile, useCurrentDir,
                0824      I                'RL', myNr, 1, myNr,
                0825      I                active_var, dummyRS,
                0826      I                iRec, myOptimIter, myThid )
                0827       ENDIF
                0828 
                0829       RETURN
                0830       END
                0831 
                0832 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
4158fdaabc Patr*0833 CBOP
50d8204771 Jean*0834 C     !ROUTINE: ACTIVE_WRITE_3D_RS
4158fdaabc Patr*0835 C     !INTERFACE:
50d8204771 Jean*0836       SUBROUTINE ACTIVE_WRITE_3D_RS(
                0837      I                          activeVar_file,
                0838      I                          active_var,
                0839      I                          globalFile,
                0840      I                          useCurrentDir,
                0841      I                          iRec,
                0842      I                          myNr,
                0843      I                          theSimulationMode,
                0844      I                          myOptimIter,
                0845      I                          myThid )
4158fdaabc Patr*0846 
                0847 C     !DESCRIPTION: \bv
50d8204771 Jean*0848 C     ==================================================================
                0849 C     SUBROUTINE ACTIVE_WRITE_3D_RS
                0850 C     ==================================================================
                0851 C     o Write an active 3D _RS variable to a file.
                0852 C     started: Christian Eckert eckert@mit.edu    Jan-1999
                0853 C     ==================================================================
                0854 C     SUBROUTINE ACTIVE_WRITE_3D_RS
                0855 C     ==================================================================
4158fdaabc Patr*0856 C     \ev
                0857 
                0858 C     !USES:
50d8204771 Jean*0859       IMPLICIT NONE
4158fdaabc Patr*0860 
50d8204771 Jean*0861 C     == global variables ==
4158fdaabc Patr*0862 #include "EEPARAMS.h"
                0863 #include "SIZE.h"
                0864 #include "PARAMS.h"
5cf4364659 Mart*0865 #include "CTRL_SIZE.h"
4d72283393 Mart*0866 #include "CTRL.h"
4158fdaabc Patr*0867 
                0868 C     !INPUT/OUTPUT PARAMETERS:
50d8204771 Jean*0869 C     activeVar_file :: filename
                0870 C     active_var     :: array
                0871 C     globalFile     ::
                0872 C     useCurrentDir  :: always write to the current directory
                0873 C                        (even if "mdsioLocalDir" is set)
                0874 C     iRec           :: record number
                0875 C     myNr           :: vertical array dimension
                0876 C     theSimulationMode :: forward mode or reverse mode simulation
                0877 C     myOptimIter    :: number of optimization iteration (default: 0)
                0878 C     myThid         :: thread number for this instance
                0879       CHARACTER*(*) activeVar_file
                0880       INTEGER  myNr
                0881       _RS      active_var(1-OLx:sNx+OLx,1-OLy:sNy+OLy,myNr,nSx,nSy)
                0882       LOGICAL  globalFile
                0883       LOGICAL  useCurrentDir
                0884       INTEGER  iRec
                0885       INTEGER  theSimulationMode
                0886       INTEGER  myOptimIter
                0887       INTEGER  myThid
4158fdaabc Patr*0888 
                0889 C     !LOCAL VARIABLES:
50d8204771 Jean*0890       INTEGER  i,j,k
                0891       INTEGER  bi,bj
32ce3e9e96 Mart*0892       INTEGER  jRec
50d8204771 Jean*0893       INTEGER  prec
a1572bcee2 Gael*0894       _RS  active_data_t(1-OLx:sNx+OLx,1-OLy:sNy+OLy,Nr,nSx,nSy)
50d8204771 Jean*0895       _RL  dummyRL(1)
4158fdaabc Patr*0896 CEOP
                0897 
50d8204771 Jean*0898 C     force 64-bit io
                0899       prec = ctrlprec
32ce3e9e96 Mart*0900       jRec = -ABS(iRec)
50d8204771 Jean*0901 
                0902 C     >>>>>>>>>>>>>>>>>>> FORWARD RUN <<<<<<<<<<<<<<<<<<<
                0903       IF (theSimulationMode .EQ. FORWARD_SIMULATION) THEN
                0904         CALL MDS_WRITE_FIELD(
                0905      I                activeVar_file, prec, globalFile, useCurrentDir,
                0906      I                'RS', myNr, 1, myNr,
                0907      I                dummyRL, active_var,
                0908      I                iRec, myOptimIter, myThid )
                0909       ENDIF
                0910 
                0911 C     >>>>>>>>>>>>>>>>>>> ADJOINT RUN <<<<<<<<<<<<<<<<<<<
                0912       IF (theSimulationMode .EQ. REVERSE_SIMULATION) THEN
                0913 
                0914         CALL MDS_READ_FIELD(
                0915      I                activeVar_file, prec, useCurrentDir,
a1572bcee2 Gael*0916      I                'RS', Nr, 1, myNr,
50d8204771 Jean*0917      O                dummyRL, active_data_t,
                0918      I                iRec, myThid )
                0919 
                0920 C     Add active_var from appropriate location to data.
                0921         DO bj = myByLo(myThid), myByHi(myThid)
                0922          DO bi = myBxLo(myThid), myBxHi(myThid)
                0923           DO k = 1, myNr
                0924            DO j=1,sNy
                0925             DO i=1,sNx
                0926               active_var(i,j,k,bi,bj) = active_var(i,j,k,bi,bj)
                0927      &                                + active_data_t(i,j,k,bi,bj)
                0928               active_data_t(i,j,k,bi,bj) = 0. _d 0
                0929             ENDDO
                0930            ENDDO
                0931           ENDDO
                0932          ENDDO
                0933         ENDDO
                0934         CALL MDS_WRITE_FIELD(
                0935      I                activeVar_file, prec, globalFile, useCurrentDir,
a1572bcee2 Gael*0936      I                'RS', Nr, 1, myNr,
50d8204771 Jean*0937      I                dummyRL, active_data_t,
32ce3e9e96 Mart*0938      I                jRec, myOptimIter, myThid )
50d8204771 Jean*0939 
                0940       ENDIF
                0941 
                0942 C     >>>>>>>>>>>>>>>>>>> TANGENT RUN <<<<<<<<<<<<<<<<<<<
                0943       IF (theSimulationMode .EQ. TANGENT_SIMULATION) THEN
                0944         CALL MDS_WRITE_FIELD(
                0945      I                activeVar_file, prec, globalFile, useCurrentDir,
                0946      I                'RS', myNr, 1, myNr,
                0947      I                dummyRL, active_var,
                0948      I                iRec, myOptimIter, myThid )
                0949       ENDIF
                0950 
                0951       RETURN
                0952       END
04904a066e Timo*0953 
                0954 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
                0955 CBOP
                0956 C     !ROUTINE: ACTIVE_WRITE_1D_RL
                0957 C     !INTERFACE:
                0958       SUBROUTINE ACTIVE_WRITE_1D_RL(
                0959      I                          activeVar_file,
                0960      O                          active_var,
                0961      I                          active_var_length,
                0962      I                          iRec,
                0963      I                          theSimulationMode,
                0964      I                          myOptimIter,
                0965      I                          myThid )
                0966 C     !DESCRIPTION: \bv
                0967 C     ==================================================================
                0968 C     SUBROUTINE ACTIVE_WRITE_1D_RL
                0969 C     ==================================================================
                0970 C     o Write an active 1D _RL vector from file.
                0971 C     o Write the same 1D vector to all tiles/processes, so global by
                0972 C       nature
                0973 C     Modified from 3D: Tim Smith tsmith@oden.utexas.edu Oct-2019
                0974 C     ==================================================================
                0975 C     SUBROUTINE ACTIVE_WRITE_1D_RL
                0976 C     ==================================================================
                0977 C     \ev
                0978 
                0979 C     !USES:
                0980       IMPLICIT NONE
                0981 
                0982 C     == global variables ==
                0983 #include "EEPARAMS.h"
                0984 #include "SIZE.h"
                0985 #include "PARAMS.h"
5cf4364659 Mart*0986 #include "CTRL_SIZE.h"
4d72283393 Mart*0987 #include "CTRL.h"
04904a066e Timo*0988 
                0989 C     !INPUT/OUTPUT PARAMETERS:
                0990 C     activeVar_file :: filename
                0991 C     active_var     :: vector
                0992 C     active_var_length :: number of elements in active variable
                0993 C     useCurrentDir  :: always read from the current directory
                0994 C                        (even if "mdsioLocalDir" is set)
                0995 C     theSimulationMode :: forward, tangent linear, or reverse mode simulation
                0996 C     myOptimIter    :: number of optimization iteration (default: 0)
                0997 C     myThid         :: thread number for this instance
                0998       CHARACTER*(*) activeVar_file
                0999       _RL      active_var(*)
                1000       INTEGER  active_var_length
                1001       INTEGER  iRec
                1002       INTEGER  theSimulationMode
                1003       INTEGER  myOptimIter
                1004       INTEGER  myThid
                1005 
                1006 C     !LOCAL VARIABLES:
                1007       INTEGER i, bi, bj
32ce3e9e96 Mart*1008       INTEGER jRec
04904a066e Timo*1009       INTEGER prec
                1010       INTEGER ioUnit
                1011       _RS  dummyRS(1)
                1012       _RL  active_data_t(active_var_length)
                1013 CEOP
                1014 
                1015 C     force 64-bit io
                1016       prec = ctrlprec
32ce3e9e96 Mart*1017       jRec = -ABS(iRec)
04904a066e Timo*1018 
                1019 C     set these to zero otherwise mdsio_read/writevec_loc looks for tiled file
                1020       bi=0
                1021       bj=0
                1022 
                1023 C     ioUnit = 0 => open, read, and close the file... see mdsio_readvec_loc
                1024       ioUnit = 0
                1025 
                1026 C     >>>>>>>>>>>>>>>>>>> FORWARD RUN <<<<<<<<<<<<<<<<<<<
                1027       IF (theSimulationMode .EQ. FORWARD_SIMULATION) THEN
                1028 
                1029 C     Read the active variable from file.
                1030         IF ( myProcId.EQ.0 ) THEN
                1031           CALL MDS_WRITEVEC_LOC(
                1032      I                activeVar_file, prec, ioUnit,
                1033      I                'RL', active_var_length,
                1034      O                active_var, dummyRS,
                1035      I                bi, bj,
                1036      I                iRec, myOptimIter, myThid )
                1037 
                1038         ENDIF
                1039       ENDIF
                1040 
                1041 C     >>>>>>>>>>>>>>>>>>> ADJOINT RUN <<<<<<<<<<<<<<<<<<<
                1042       IF (theSimulationMode .EQ. REVERSE_SIMULATION) THEN
                1043 
                1044         CALL MDS_READVEC_LOC(
                1045      I                activeVar_file, prec, ioUnit,
                1046      I                'RL', active_var_length,
                1047      O                active_data_t, dummyRS,
                1048      I                bi, bj,
                1049      I                iRec, myThid )
                1050 
                1051 C     Add active_var from appropriate location to data.
                1052         DO i = 1, active_var_length
                1053           active_var(i) = active_var(i) + active_data_t(i)
                1054           active_data_t(i) = 0. _d 0
                1055         ENDDO
                1056 
                1057 C     Store the result on disk.
                1058         IF ( myProcId.EQ.0 ) THEN
                1059           CALL MDS_WRITEVEC_LOC(
                1060      I                activeVar_file, prec, ioUnit,
                1061      I                'RL', active_var_length,
                1062      O                active_data_t, dummyRS,
                1063      I                bi, bj,
32ce3e9e96 Mart*1064      I                jRec, myOptimIter, myThid )
04904a066e Timo*1065         ENDIF
                1066       ENDIF
                1067 
                1068 C     >>>>>>>>>>>>>>>>>>> TANGENT RUN <<<<<<<<<<<<<<<<<<<
                1069       IF (theSimulationMode .EQ. TANGENT_SIMULATION) THEN
                1070 C     Read the active variable from file.
                1071         IF ( myProcId.EQ.0 ) THEN
                1072           CALL MDS_WRITEVEC_LOC(
                1073      I                activeVar_file, prec, ioUnit,
                1074      I                'RL', active_var_length,
                1075      O                active_var, dummyRS,
                1076      I                bi, bj,
                1077      I                iRec, myOptimIter, myThid )
                1078         ENDIF
                1079       ENDIF
                1080 
                1081       RETURN
                1082       END
                1083 
                1084 C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
                1085 CBOP
                1086 C     !ROUTINE: ACTIVE_WRITE_1D_RS
                1087 C     !INTERFACE:
                1088       SUBROUTINE ACTIVE_WRITE_1D_RS(
                1089      I                          activeVar_file,
                1090      O                          active_var,
                1091      I                          active_var_length,
                1092      I                          iRec,
                1093      I                          theSimulationMode,
                1094      I                          myOptimIter,
                1095      I                          myThid )
                1096 C     !DESCRIPTION: \bv
                1097 C     ==================================================================
                1098 C     SUBROUTINE ACTIVE_WRITE_1D_RS
                1099 C     ==================================================================
                1100 C     o Write an active 1D _RS vector from file.
                1101 C     o Write the same 1D vector to all tiles/processes, so global by
                1102 C       nature
                1103 C     Modified from 3D: Tim Smith tsmith@oden.utexas.edu Oct-2019
                1104 C     ==================================================================
                1105 C     SUBROUTINE ACTIVE_WRITE_1D_RS
                1106 C     ==================================================================
                1107 C     \ev
                1108 
                1109 C     !USES:
                1110       IMPLICIT NONE
                1111 
                1112 C     == global variables ==
                1113 #include "EEPARAMS.h"
                1114 #include "SIZE.h"
                1115 #include "PARAMS.h"
5cf4364659 Mart*1116 #include "CTRL_SIZE.h"
4d72283393 Mart*1117 #include "CTRL.h"
04904a066e Timo*1118 
                1119 C     !INPUT/OUTPUT PARAMETERS:
                1120 C     activeVar_file :: filename
                1121 C     active_var     :: vector
                1122 C     active_var_length :: number of elements in active variable
                1123 C     useCurrentDir  :: always read from the current directory
                1124 C                        (even if "mdsioLocalDir" is set)
                1125 C     theSimulationMode :: forward, tangent linear, or reverse mode simulation
                1126 C     myOptimIter    :: number of optimization iteration (default: 0)
                1127 C     myThid         :: thread number for this instance
                1128       CHARACTER*(*) activeVar_file
                1129       _RS      active_var(*)
                1130       INTEGER  active_var_length
                1131       INTEGER  iRec
                1132       INTEGER  theSimulationMode
                1133       INTEGER  myOptimIter
                1134       INTEGER  myThid
                1135 
                1136 C     !LOCAL VARIABLES:
                1137       INTEGER i, bi, bj
32ce3e9e96 Mart*1138       INTEGER jRec
04904a066e Timo*1139       INTEGER prec
                1140       INTEGER ioUnit
                1141       _RS  active_data_t(active_var_length)
                1142       _RL  dummyRL(1)
                1143 CEOP
                1144 
                1145 C     force 64-bit io
                1146       prec = ctrlprec
32ce3e9e96 Mart*1147       jRec = -ABS(iRec)
04904a066e Timo*1148 
                1149 C     set these to zero otherwise mdsio_read/writevec_loc looks for tiled file
                1150       bi=0
                1151       bj=0
                1152 
                1153 C     ioUnit = 0 => open, read, and close the file... see mdsio_readvec_loc
                1154       ioUnit = 0
                1155 
                1156 C     >>>>>>>>>>>>>>>>>>> FORWARD RUN <<<<<<<<<<<<<<<<<<<
                1157       IF (theSimulationMode .EQ. FORWARD_SIMULATION) THEN
                1158 
                1159 C     Read the active variable from file.
                1160         IF ( myProcId.EQ.0 ) THEN
                1161           CALL MDS_WRITEVEC_LOC(
                1162      I                activeVar_file, prec, ioUnit,
                1163      I                'RS', active_var_length,
                1164      O                dummyRL, active_var,
                1165      I                bi, bj,
                1166      I                iRec, myOptimIter, myThid )
                1167 
                1168         ENDIF
                1169       ENDIF
                1170 
                1171 C     >>>>>>>>>>>>>>>>>>> ADJOINT RUN <<<<<<<<<<<<<<<<<<<
                1172       IF (theSimulationMode .EQ. REVERSE_SIMULATION) THEN
                1173 
                1174         CALL MDS_READVEC_LOC(
                1175      I                activeVar_file, prec, ioUnit,
                1176      I                'RL', active_var_length,
                1177      O                dummyRL, active_data_t,
                1178      I                bi, bj,
                1179      I                iRec, myThid )
                1180 
                1181 C     Add active_var from appropriate location to data.
                1182         DO i = 1, active_var_length
                1183           active_var(i) = active_var(i) + active_data_t(i)
                1184           active_data_t(i) = 0. _d 0
                1185         ENDDO
                1186 
                1187 C     Store the result on disk.
                1188         IF ( myProcId.EQ.0 ) THEN
                1189           CALL MDS_WRITEVEC_LOC(
                1190      I                activeVar_file, prec, ioUnit,
                1191      I                'RL', active_var_length,
                1192      O                dummyRL, active_data_t,
                1193      I                bi, bj,
32ce3e9e96 Mart*1194      I                jRec, myOptimIter, myThid )
04904a066e Timo*1195 
                1196         ENDIF
                1197       ENDIF
                1198 
                1199 C     >>>>>>>>>>>>>>>>>>> TANGENT RUN <<<<<<<<<<<<<<<<<<<
                1200       IF (theSimulationMode .EQ. TANGENT_SIMULATION) THEN
                1201 C     Read the active variable from file.
                1202         IF ( myProcId.EQ.0 ) THEN
                1203           CALL MDS_WRITEVEC_LOC(
                1204      I                activeVar_file, prec, ioUnit,
                1205      I                'RL', active_var_length,
                1206      O                dummyRL, active_var,
                1207      I                bi, bj,
                1208      I                iRec, myOptimIter, myThid )
                1209         ENDIF
                1210       ENDIF
                1211 
                1212       RETURN
                1213       END