SURFEX  V8_0
Surfex V8_0 release
 All Classes Files Functions Variables
sfx_oasis_init.F90
Go to the documentation of this file.
1 !SFX_LIC Copyright 1994-2014 CNRS, Meteo-France and Universite Paul Sabatier
2 !SFX_LIC This is part of the SURFEX software governed by the CeCILL-C licence
3 !SFX_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
4 !SFX_LIC for details. version 1.
5 SUBROUTINE sfx_oasis_init(HNAMELIST,KLOCAL_COMM,HINIT)
6 !!
7 !!
8 !! PURPOSE
9 !! --------
10 !!
11 !! Initialize coupled mode communication
12 !!
13 !!
14 !! METHOD
15 !! ------
16 !!
17 !! OASIS-MCT usage is controlled by environment variables
18 !! OASIS-MCT interface must be initialized before any DR_HOOK call
19 !!
20 !!
21 !! EXTERNAL
22 !! --------
23 !!
24 !!
25 !! REFERENCE
26 !! ---------
27 !!
28 !! S. Valcke et al., 2013: OASIS-MCT User Guide
29 !! CERFACS, Toulouse, France, 50 pp.
30 !! https://verc.enes.org/oasis/oasis-dedicated-user-support-1/documentation/oasis3-mct-user-guide
31 !!
32 !!
33 !! AUTHOR
34 !! ------
35 !!
36 !! B. Decharme, CNRM
37 !!
38 !! MODIFICATION
39 !! --------------
40 !!
41 !! Original 10/2013
42 !!
43 !-------------------------------------------------------------------------------
44 !
45 !* 0. DECLARATIONS
46 ! ------------
47 !
48 USE modd_sfx_oasis, ONLY : loasis, xruntime
49 !
50 #ifdef CPLOASIS
51 USE mod_oasis
52 #endif
53 !
54 IMPLICIT NONE
55 !
56 #ifdef CPLOASIS
57 include 'mpif.h'
58 #endif
59 !
60 !* 0.1 Declarations of arguments
61 ! -------------------------
62 !
63  CHARACTER(LEN=28), INTENT(IN ) :: hnamelist
64 INTEGER, INTENT(OUT) :: klocal_comm ! value of local communicator
65  CHARACTER(LEN=3), INTENT(IN ), OPTIONAL :: hinit ! choice of fields to initialize
66 !
67 !* 0.2 Declarations of local variables
68 ! -------------------------------
69 !
70  CHARACTER(LEN=9) :: yword, ytimerun
71  CHARACTER(LEN=1000):: yline, yfound
72 INTEGER :: ierr, iwork, irank
73 INTEGER :: icomp_id
74 INTEGER :: itimerun
75 LOGICAL :: gfound
76  CHARACTER(LEN=3) :: yinit
77 !
78 !
79 !* 0.3 Declarations of namelist variables
80 ! ----------------------------------
81 !
82  CHARACTER(LEN=6) :: cmodel_name ! component model name
83 !
84 namelist/nam_oasis/loasis,cmodel_name
85 !
86 !-------------------------------------------------------------------------------
87 !
88 ! ATTENTION : Do not introduce DR_HOOK in this routine
89 !
90 !* 0. Initialization:
91 ! ---------------
92 !
93 loasis = .false.
94  cmodel_name = 'surfex'
95 xruntime = 0.0
96 !
97 yinit = 'ALL'
98 IF(present(hinit))yinit=hinit
99 !
100 !-------------------------------------------------------------------------------
101 !
102 !* 1. Read namelist:
103 ! --------------
104 !
105 IF(len_trim(hnamelist)/=0)THEN
106 !
107  OPEN(unit=11,file=hnamelist,action='READ',form="FORMATTED",position="REWIND",status='OLD',iostat=ierr)
108 !
109  IF (ierr /= 0) THEN
110  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
111  WRITE(*,'(2A)')'SFX_OASIS_INIT: SFX NAMELIST NOT FOUND: ',trim(hnamelist)
112  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
113  CALL abort
114  stop
115  ENDIF
116 !
117  READ (unit=11,nml=nam_oasis,iostat=ierr)
118 !
119  CLOSE(unit=11)
120 !
121 ENDIF
122 !
123 !-------------------------------------------------------------------------------
124 !
125 !* 2. Setup OASIS
126 ! -----------
127 !
128 IF(loasis)THEN
129 !
130 #ifdef CPLOASIS
131  CALL oasis_init_comp(icomp_id,cmodel_name,ierr)
132  IF (ierr/=oasis_ok) THEN
133  WRITE(*,'(A)' )'SFX : Error initializing OASIS'
134  WRITE(*,'(A,I4)')'SFX : Return code from oasis_init_comp : ',ierr
135  CALL oasis_abort(icomp_id,cmodel_name,'SFX_OASIS_INIT: Error initializing OASIS')
136  CALL abort
137  stop
138  ENDIF
139  CALL mpi_comm_rank(mpi_comm_world,irank,iwork)
140 #endif
141 !
142  IF(irank==0)THEN
143  WRITE(*,'(A)')'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
144  WRITE(*,'(A)')'OASIS used for model : ',trim(cmodel_name)
145  WRITE(*,'(A)')'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
146  ENDIF
147 !
148 ELSE
149 !
150  klocal_comm=0
151  RETURN
152 !
153 ENDIF
154 !-------------------------------------------------------------------------------
155 !
156 !* 4. Get local communicator
157 ! ----------------------
158 !
159 #ifdef CPLOASIS
160  CALL oasis_get_localcomm(klocal_comm,ierr)
161 IF (ierr/=oasis_ok) THEN
162  IF(irank==0)THEN
163  WRITE(*,'(A)' )'SFX : Error getting local communicator from OASIS'
164  WRITE(*,'(A,I4)')'SFX : Return code from oasis_get_local_comm : ',ierr
165  ENDIF
166  CALL oasis_abort(icomp_id,cmodel_name,'SFX_OASIS_INIT: Error getting local communicator')
167  CALL abort
168  stop
169 ENDIF
170 #endif
171 !
172 !-------------------------------------------------------------------------------
173 !
174 !
175 IF(yinit=='PRE')THEN
176  RETURN
177 ENDIF
178 !
179 !* 5. Read total simulated time in namcouple
180 ! --------------------------------------
181 !
182 OPEN (unit=11,file ='namcouple',status='OLD',form ='FORMATTED',position="REWIND",iostat=ierr)
183 IF (ierr /= 0) THEN
184  IF(irank==0)THEN
185  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
186  WRITE(*,'(A)' )'SFX : OASIS namcouple not found'
187  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
188  ENDIF
189  CALL abort
190  stop
191 ENDIF
192 !
193 ytimerun=' $RUNTIME'
194 itimerun=-1
195 !
196 DO WHILE (itimerun==-1)
197  READ (unit = 11,fmt = '(A9)',iostat=ierr) yword
198  IF(ierr/=0)THEN
199  IF(irank==0)THEN
200  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
201  WRITE(*,'(A)' )'SFX : Problem $RUNTIME empty in namcouple'
202  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
203  ENDIF
204  CALL abort
205  stop
206  ENDIF
207  IF (yword==ytimerun)THEN
208  READ (unit = 11,fmt = '(A1000)',iostat=ierr) yline
209  IF(ierr/=0)THEN
210  IF(irank==0)THEN
211  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
212  WRITE(*,'(A)' )'SFX : Problem looking for $RUNTIME in namcouple'
213  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
214  ENDIF
215  CALL abort
216  stop
217  ENDIF
218  CALL found_timerun(yline, yfound, 1000, gfound)
219  IF (gfound) THEN
220  READ (yfound,fmt = '(I100)',iostat=ierr) itimerun
221  IF(ierr/=0)THEN
222  IF(irank==0)THEN
223  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
224  WRITE(*,'(A)' )'SFX : Problem reading $RUNTIME in namcouple'
225  WRITE(*,'(2A)' )'$RUNTIME = ', trim(yfound)
226  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
227  ENDIF
228  CALL abort
229  stop
230  ENDIF
231  ENDIF
232  ENDIF
233 ENDDO
234  CLOSE(11)
235 !
236 xruntime = REAL(itimerun)
237 !
238 WRITE(*,'(A)' )'-----------------------------'
239 !
240 !-------------------------------------------------------------------------------
241  CONTAINS
242 !-------------------------------------------------------------------------------
243 !
244 SUBROUTINE found_timerun(HIN, HOUT, KLEN, OFOUND)
245 !
246 IMPLICIT NONE
247 !
248 INTEGER , INTENT (IN ) :: klen
249  CHARACTER (LEN=*), INTENT (INOUT) :: hin
250  CHARACTER (LEN=*), INTENT (INOUT) :: hout
251 LOGICAL, INTENT (OUT ) :: ofound
252 !
253 !* ---------------------------- Local declarations -------------------
254 !
255  CHARACTER(LEN=1), PARAMETER :: yblank = ' '
256  CHARACTER(LEN=1), PARAMETER :: ynada = '#'
257 
258  CHARACTER(LEN=KLEN) :: yline
259  CHARACTER(LEN=KLEN) :: ywork
260 !
261 INTEGER :: ilen
262 INTEGER :: ierr
263 !
264 !
265 !* 1. Skip line if it is a comment
266 ! ----------------------------
267 !
268 DO WHILE (hin(1:1)==ynada)
269  READ (unit = 11, fmt = '(A9)',iostat=ierr) yline
270  IF(ierr/=0)THEN
271  IF(irank==0)THEN
272  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
273  WRITE(*,'(A)' )'SFX : Problem looking for $RUNTINE line in namcouple'
274  WRITE(*,'(A)' )'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
275  ENDIF
276  CALL abort
277  stop
278  ENDIF
279  hin(1:klen) = yline(1:klen)
280 ENDDO
281 !
282 !* Fill HOUT with blanks
283 !
284 hout = yblank
285 !
286 !* Fill temporary string and remove leading blanks
287 !
288 ywork = adjustl(hin)
289 !
290 IF(len_trim(ywork)<=0)THEN
291  ofound = .false.
292  RETURN
293 ENDIF
294 !
295 !* Find the length of this set of characters
296 !
297 ilen = index(ywork,yblank) - 1
298 !
299 !* Copy to HOUT
300 !
301 hout(1:ilen) = ywork(1:ilen)
302 !
303 ofound = .true.
304 !
305 END SUBROUTINE found_timerun
306 !
307 !-------------------------------------------------------------------------------
308 !
309 END SUBROUTINE sfx_oasis_init
subroutine found_timerun(HIN, HOUT, KLEN, OFOUND)
subroutine sfx_oasis_init(HNAMELIST, KLOCAL_COMM, HINIT)