Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RFLO_ReadRandomState.F90
Go to the documentation of this file.
1 ! *********************************************************************
2 ! * Rocstar Simulation Suite *
3 ! * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4 ! * *
5 ! * Illinois Rocstar LLC *
6 ! * Champaign, IL *
7 ! * www.illinoisrocstar.com *
8 ! * sales@illinoisrocstar.com *
9 ! * *
10 ! * License: See LICENSE file in top level of distribution package or *
11 ! * http://opensource.org/licenses/NCSA *
12 ! *********************************************************************
13 ! *********************************************************************
14 ! * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15 ! * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16 ! * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17 ! * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18 ! * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19 ! * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20 ! * Arising FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21 ! * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22 ! *********************************************************************
23 !******************************************************************************
24 !
25 ! Purpose: read state of random number generator for structured code
26 !
27 ! Description: the following solution formats are supported:
28 ! - RocfloMP ASCII
29 ! - RocfloMP binary.
30 !
31 ! Input: regions = dimensions of all regions.
32 !
33 ! Output: regions(:)%randData = state of RNG for each region
34 ! global%currentTime = physical time
35 !
36 ! Notes: none.
37 !
38 !******************************************************************************
39 !
40 ! $Id: RFLO_ReadRandomState.F90,v 1.3 2008/12/06 08:44:07 mtcampbe Exp $
41 !
42 ! Copyright: (c) 2003 by the University of Illinois
43 !
44 !******************************************************************************
45 
46 SUBROUTINE rflo_readrandomstate( regions )
47 
48  USE moddatatypes
49  USE moddatastruct, ONLY : t_region
50  USE modglobal, ONLY : t_global
51  USE moderror
52  USE modmpi
53  USE modparameters
54  USE modrandom
55 
57  IMPLICIT NONE
58 
59 #include "Indexing.h"
60 
61 ! ... parameters
62  TYPE(t_region), POINTER :: regions(:)
63 
64 ! ... loop variables
65  INTEGER :: ireg, i
66 
67 ! ... local variables
68  INTEGER, PARAMETER :: n_rvar = 1
69  INTEGER, PARAMETER :: n_ivar_extra = 1
70  INTEGER, PARAMETER :: randstate_offset = n_ivar_extra+1
71  INTEGER, PARAMETER :: n_randstate = rand_total_size+n_ivar_extra
72 
73  CHARACTER(2*CHRLEN+18) :: fname, fhead
74  CHARACTER(CHRLEN) :: rcsidentstring, msg
75 
76 #ifdef MPI
77  INTEGER :: status(mpi_status_size)
78 #endif
79  INTEGER :: errorflag, mti, randstate(n_randstate,1)
80 
81  REAL(RFREAL) :: rvar(n_rvar,1)
82 
83  LOGICAL :: fileexists
84 
85  TYPE(t_global), POINTER :: global
86 
87 !******************************************************************************
88 
89  global => regions(1)%global
90 
91  CALL registerfunction( global,'RFLO_ReadRandomState',&
92  'RFLO_ReadRandomState.F90' )
93 
94 ! begin -----------------------------------------------------------------------
95 
96 ! determine if RNG state file exists ------------------------------------------
97 
98  fhead = trim(global%inDir)//trim(global%casename)
99 
100  SELECT CASE(global%solutFormat)
101  CASE (format_ascii)
102  fhead = trim(fhead)//'.randa_'
103  CASE (format_binary)
104  fhead = trim(fhead)//'.randb_'
105  CASE default
106  CALL errorstop( global,err_unknown_format,__line__ )
107  END SELECT
108 
109  IF (global%flowType == flow_unsteady) THEN
110  WRITE(fname,'(A,ES11.5)') trim(fhead), global%timeStamp
111  ELSE
112  WRITE(fname,'(A,I6.6)') trim(fhead), global%currentIter
113  ENDIF
114 
115  INQUIRE(file=fname,exist=fileexists)
116 
117  IF (.NOT.fileexists) THEN
118  IF (global%myProcid == masterproc .AND. &
119  global%currentTime > 0._rfreal) THEN
120  WRITE(stdout,'(A)') solver_name// &
121  '### WARNING: No Random State file found: '// &
122  'Random Number Generator re-initialized'
123  ENDIF ! MASTERPROC
124  go to 999
125  ENDIF ! fileExists
126 
127 ! open random state file (only master proc.) ----------------------------------
128 
129  IF (global%myProcid == masterproc) THEN
130 
131  SELECT CASE(global%solutFormat)
132  CASE (format_ascii)
133  OPEN(if_rand_state,file=fname,form= 'formatted',status='old', &
134  iostat=errorflag)
135  CASE (format_binary)
136  OPEN(if_rand_state,file=fname,form='unformatted',status='old', &
137  iostat=errorflag)
138  CASE default
139  CALL errorstop( global,err_unknown_format,__line__ )
140  END SELECT
141 
142  global%error = errorflag
143  IF (global%error /= 0) &
144  CALL errorstop( global,err_file_open,__line__,'File: '//trim(fname) )
145 
146 ! - Read time stamp of RNG state, and yield an error if not the current time --
147 
148  CALL rflo_readdatafilereal( global,if_rand_state,global%solutFormat, &
149  n_rvar,1,rvar )
150 
151  IF (global%flowType==flow_unsteady.AND.global%currentTime/=rvar(1,1)) THEN
152  WRITE(msg,1000) rvar(1,1),global%currentTime
153  CALL errorstop( global,err_time_solution,__line__,msg//' File: '// &
154  trim(fname) )
155  ENDIF
156 
157  ENDIF ! MASTERPROC
158 
159 ! Receive data from master ----------------------------------------------------
160 
161  DO ireg=1,global%nRegions
162 
163 ! - Read the RNG state for each region (only master) --------------------------
164 
165  IF (global%myProcid == masterproc) THEN
166 
167  CALL rflo_readdatafileint( global,if_rand_state,global%solutFormat, &
168  n_randstate,1,randstate )
169 
170  IF (randstate(1,1) /= ireg) &
171  CALL errorstop( global,err_region_number,__line__,'File: '// &
172  trim(fname) )
173 
174 ! --- Allowable values of mti are within array bounds 0:RAND_BUFFER_SIZE-1,
175 ! --- or the special case mti = RAND_BUFFER_SIZE (generates array refill)
176 
177  mti = randstate(rand_mti_index+randstate_offset,1)
178 
179  IF (mti < 0 .OR. mti > rand_buffer_size) &
180  CALL errorstop( global,err_exceeds_decl_mem,__line__ )
181 
182 #ifdef MPI
183  IF (regions(ireg)%procid /= masterproc) THEN
184 
185  CALL mpi_send( randstate,n_randstate,mpi_integer, &
186  regions(ireg)%procid,ireg, &
187  global%mpiComm,global%mpierr )
188  IF (global%mpierr /=0) CALL errorstop( global,err_mpi_trouble,__line__ )
189 
190  ENDIF
191 
192  ELSE ! not the master
193 
194  IF (regions(ireg)%procid == global%myProcid) THEN
195 
196  CALL mpi_recv( randstate,n_randstate,mpi_integer,masterproc,ireg, &
197  global%mpiComm,status,global%mpierr )
198  IF (global%mpierr /=0) CALL errorstop( global,err_mpi_trouble,__line__ )
199 
200  ENDIF
201 #endif
202 
203  ENDIF ! MASTERPROC
204 
205 ! - copy temporary array into randData
206 
207  IF (regions(ireg)%procid == global%myProcid) THEN
208  DO i=0,rand_total_size-1
209  regions(ireg)%randData%mt(i) = randstate(i+randstate_offset,1)
210  ENDDO ! i
211  ENDIF ! global%myProcid
212 
213  ENDDO ! iReg
214 
215 ! finalize --------------------------------------------------------------------
216 
217  IF (global%myProcid == masterproc) THEN
218  CLOSE(if_rand_state,iostat=errorflag)
219  global%error = errorflag
220  IF (global%error /= 0) &
221  CALL errorstop( global,err_file_close,__line__,'File: '//trim(fname) )
222  ENDIF
223 
224 999 CONTINUE
225  CALL deregisterfunction( global )
226 
227 1000 FORMAT('Time in file is= ',es12.5,' but it should be= ',es12.5,'.')
228 
229 END SUBROUTINE rflo_readrandomstate
230 
231 !******************************************************************************
232 !
233 ! RCS Revision history:
234 !
235 ! $Log: RFLO_ReadRandomState.F90,v $
236 ! Revision 1.3 2008/12/06 08:44:07 mtcampbe
237 ! Updated license.
238 !
239 ! Revision 1.2 2008/11/19 22:17:21 mtcampbe
240 ! Added Illinois Open Source License/Copyright
241 !
242 ! Revision 1.1 2004/11/29 21:25:16 wasistho
243 ! lower to upper case
244 !
245 ! Revision 1.1 2003/11/21 22:28:42 fnajjar
246 ! Added Read capability for Random Number Generator
247 !
248 !******************************************************************************
249 
250 
251 
252 
253 
254 
255 
subroutine registerfunction(global, funName, fileName)
Definition: ModError.F90:449
int status() const
Obtain the status of the attribute.
Definition: Attribute.h:240
subroutine rflo_readdatafileint(global, fileId, form, nDim1, nDim2, ivar)
blockLoc i
Definition: read.cpp:79
**********************************************************************Rocstar Simulation Suite Illinois Rocstar LLC All rights reserved ****Illinois Rocstar LLC IL **www illinoisrocstar com **sales illinoisrocstar com WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **Arising OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE **********************************************************************INTERFACE SUBROUTINE form
subroutine rflo_readrandomstate(regions)
subroutine errorstop(global, errorCode, errorLine, addMessage)
Definition: ModError.F90:483
subroutine deregisterfunction(global)
Definition: ModError.F90:469
subroutine rflo_readdatafilereal(global, fileId, form, nDim1, nDim2, var)