New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Changeset 4598 for branches – NEMO

Changeset 4598 for branches


Ignore:
Timestamp:
2014-03-27T09:56:56+01:00 (10 years ago)
Author:
pabouttier
Message:

Add routines to allow restartability of PRNG, see Ticket #1284

Location:
branches/2012/dev_v3_4_STABLE_2012/NEMOGCM/NEMO/OPATAM_SRC
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2012/dev_v3_4_STABLE_2012/NEMOGCM/NEMO/OPATAM_SRC/mt19937ar.f90

    r3611 r4598  
    5959   !-----------------------------------------------------------------------! 
    6060   USE par_kind, ONLY : wp 
     61   USE dom_oce , ONLY : nproc 
     62   USE lib_mpp , ONLY : get_unit 
     63 
    6164   PRIVATE 
    6265   INTEGER, PARAMETER :: jpn = 624           ! period parameters 
     
    8588      & mtrand_real53,& 
    8689      & init_by_array,& 
    87       & init_mtrand 
     90      & init_mtrand,  & 
     91      & mtrand_seeddump, & 
     92      & mtrand_seedread 
    8893CONTAINS 
    8994   SUBROUTINE init_mtrand(ks) 
     
    9398      INTEGER :: ks 
    9499      ! 
    95       mt(0) = IAND(INT(ks,kind=8),jpall) 
    96       DO mti = 1, jpn-1 
    97          mt(mti) = 1812433253 * IEOR(mt(mti-1),ISHFT(mt(mti-1),-30)) + mti 
    98          ! See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.  
    99          ! In the previous versions, MSBs of the seed affect    
    100          ! only MSBs of the array mt[].                         
    101          ! modified by Makoto Matsumoto  
    102          mt(mti) = IAND(mt(mti),jpall) 
    103          ! for >32 bit machines 
    104       END DO 
    105       l_mtinit = .TRUE. 
    106 ! 
     100      IF (.NOT.l_mtinit) THEN 
     101         mt(0) = IAND(INT(ks,kind=8),jpall) 
     102         DO mti = 1, jpn-1 
     103            mt(mti) = 1812433253 * IEOR(mt(mti-1),ISHFT(mt(mti-1),-30)) + mti 
     104            ! See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier.  
     105            ! In the previous versions, MSBs of the seed affect    
     106            ! only MSBs of the array mt[].                         
     107            ! modified by Makoto Matsumoto  
     108            mt(mti) = IAND(mt(mti),jpall) 
     109            ! for >32 bit machines 
     110         END DO 
     111         l_mtinit = .TRUE. 
     112      ENDIF 
    107113   END SUBROUTINE init_mtrand 
     114 
    108115   SUBROUTINE init_by_array(kinit_key,key_length) 
    109116!----------------------------------------------------------------------- 
     
    231238      mtrand_res53 = ( za * 67108864._wp + zb ) / 9007199254740992._wp 
    232239   END FUNCTION mtrand_res53 
     240 
     241 
     242   SUBROUTINE mtrand_seeddump(cdfile) 
     243!----------------------------------------------------------------------- 
     244!     Dumps the seed in a restart file. 
     245!----------------------------------------------------------------------- 
     246      CHARACTER(len=*) :: cdfile 
     247      INTEGER :: iunit 
     248 
     249      iunit = get_unit() 
     250      OPEN( unit=iunit, file = TRIM(cdfile), form = 'unformatted', & 
     251         &  access = 'sequential', status = 'new' ) 
     252      WRITE( iunit ) mt 
     253      WRITE( iunit ) mti 
     254      CLOSE( iunit ) 
     255 
     256   END SUBROUTINE mtrand_seeddump 
     257 
     258   SUBROUTINE mtrand_seedread(cdfile) 
     259!----------------------------------------------------------------------- 
     260!     Reads the seed from a restart file. 
     261!----------------------------------------------------------------------- 
     262      CHARACTER(len=*) :: cdfile 
     263      INTEGER :: iunit 
     264 
     265      iunit = get_unit() 
     266      OPEN( unit=iunit, file = TRIM(cdfile), form = 'unformatted', & 
     267         &  access = 'sequential', status = 'old' ) 
     268      READ( iunit ) mt 
     269      READ( iunit ) mti 
     270      CLOSE( iunit ) 
     271      l_mtinit = .TRUE. 
     272 
     273   END SUBROUTINE mtrand_seedre 
     274    
    233275END MODULE mt19937ar 
  • branches/2012/dev_v3_4_STABLE_2012/NEMOGCM/NEMO/OPATAM_SRC/ran_num.F90

    r3611 r4598  
    1515   USE mt19937ar, ONLY : & 
    1616    & init_mtrand,       & 
    17     & mtrand_real1 
     17    & mtrand_real1,      & 
     18    & mtrand_seeddump,   & 
     19    & mtrand_seedread 
    1820  
    1921   IMPLICIT NONE 
     
    2527   PUBLIC & 
    2628      & gaustb, & 
    27       & gaustb_2d 
     29      & gaustb_2d, & 
     30      & random_seeddump, & 
     31      & random_seedread 
    2832 
    2933CONTAINS 
     
    260264   END FUNCTION psrandom 
    261265 
     266    
     267   SUBROUTINE random_seeddump(cdfile) 
     268      !!---------------------------------------------------------------------- 
     269      !!               ***  ROUTINE psrandom *** 
     270      !! 
     271      !! ** Purpose : Pseudo-Random number generator. 
     272      !! 
     273      !! ** Method  : Dump the seed 
     274      !! 
     275      !! ** Action  : 
     276      !! 
     277      !! History : 
     278      !!        ! 2013-03 (K. Mogensen) Original code 
     279      !!---------------------------------------------------------------------- 
     280      !! * Arguments 
     281      CHARACTER(len=128) :: cdfile 
     282      CALL mtrand_seeddump(cdfile) 
     283 
     284   END SUBROUTINE random_seeddump 
     285 
     286   SUBROUTINE random_seedread(cdfile) 
     287      !!---------------------------------------------------------------------- 
     288      !!               ***  ROUTINE psrandom *** 
     289      !! 
     290      !! ** Purpose : Pseudo-Random number generator. 
     291      !! 
     292      !! ** Method  : Read the seed 
     293      !! 
     294      !! ** Action  : 
     295      !! 
     296      !! History : 
     297      !!        ! 2013-03 (K. Mogensen) Original code 
     298      !!---------------------------------------------------------------------- 
     299      !! * Arguments 
     300      CHARACTER(len=128) :: cdfile 
     301      CALL mtrand_seedread(cdfile) 
     302 
     303   END SUBROUTINE random_seedread 
    262304 
    263305END MODULE ran_num 
Note: See TracChangeset for help on using the changeset viewer.