-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmulti_schism.F90
244 lines (184 loc) · 8.45 KB
/
multi_schism.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
! This code is a main driver
! program for running multiple schism components concurrently
!
! @copyright (C) 2018, 2019, 2020-2021 Helmholtz-Zentrum Geesthacht
! @author Richard Hofmeister
! @author Carsten Lemmen <[email protected]>
! @author Y Joseph Zhang <[email protected]>
!
! @license Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! http://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.
!
! This version accounts for halo(ghost) zone, because ESMF by default
! partitions among nodes instead of elements
#define ESMF_CONTEXT line=__LINE__,file=ESMF_FILENAME,method=ESMF_METHOD
#define ESMF_ERR_PASSTHRU msg="SCHISM subroutine call returned error"
#undef ESMF_FILENAME
#define ESMF_FILENAME "multi_schism.F90"
#define _SCHISM_LOG_AND_FINALIZE_ON_ERROR_(X) if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, ESMF_CONTEXT, rcToReturn=X)) call ESMF_Finalize(rc=localrc, endflag=ESMF_END_ABORT)
#define ESMF_METHOD "main"
program main
use esmf
use schism_esmf_cap, only: schismSetServices => SetServices
use schism_esmf_util, only: clockCreateFrmParam
implicit none
type(ESMF_GridComp), allocatable :: schism_components(:)
type(ESMF_State), allocatable :: schism_imports(:), schism_exports(:)
type(ESMF_TimeInterval) :: timestep
type(ESMF_Time) :: start_time, stop_time
type(ESMF_Clock) :: clock
type(ESMF_Vm) :: vm
type(ESMF_Log) :: log
integer(ESMF_KIND_I4) :: petCountLocal, schismCount = 8
integer(ESMF_KIND_I4) :: rc, petCount, i, j, inum, localrc
integer(ESMF_KIND_I4), allocatable :: petlist(:)
real(ESMF_KIND_R8), pointer :: ptr1d(:)
logical :: isPresent
character(len=ESMF_MAXSTR) :: filename = 'multi_schism.cfg', message
type(ESMF_Config) :: config
type(ESMF_Config), allocatable :: configList(:)
call ESMF_Initialize(defaultCalKind=ESMF_CALKIND_GREGORIAN, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
call ESMF_LogSet(flush=.true., rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
! Inquire the parallel environment about available
! resources, and partition the environment to use
! PETs for SCHISM.
call ESMF_VMGetGlobal(vm, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
!PET is persistent globally
call ESMF_VMGet(vm, petCount=petCount, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
!Get config info (# of schism instances). Always specify
inquire (file=filename, exist=isPresent)
if (isPresent) then
config = ESMF_ConfigCreate(rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
call ESMF_ConfigLoadFile(config, filename=filename, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
call ESMF_ConfigGetAttribute(config, value=schismCount, label='count:', &
!default: value used if label is not found
default=min(petCount, 8), rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
end if
if (schismCount > 999 .or. schismCount < 1) then
write (message, '(A,I3,A)') 'Number of instances ', schismCount, &
'must be in the range [1,999]'
call ESMF_LogWrite(trim(message), ESMF_LOGMSG_ERROR)
localrc = ESMF_RC_VAL_OUTOFRANGE
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
elseif (schismCount > petCount) then
write (message, '(A)') 'Requested number of instances exceeds available PETs'
call ESMF_LogWrite(trim(message), ESMF_LOGMSG_WARNING)
write (message, '(A,I3,A,I3)') 'Reduced from ', schismCount, ' to ', petCount
call ESMF_LogWrite(trim(message), ESMF_LOGMSG_WARNING)
schismCount = petCount
else
write (message, '(A,I3)') 'Number of SCHISM instances ', schismCount
call ESMF_LogWrite(trim(message), ESMF_LOGMSG_INFO)
end if
! Create all components on their respective parallel
! environment provided by each petList
allocate (schism_components(schismCount), stat=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
allocate (configList(schismCount), stat=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
do i = 1, schismCount
petCountLocal = petCount / schismCount
allocate (petlist(petCountLocal), stat=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
do j = 1, petCountLocal
petList(j) = (i - 1) * petCountLocal + j - 1 ! PET #; 0 based
end do
write (message, '(A,I3.3)') 'schism_', i
!write(0, *) trim(message), 'list=', petList, 'petCount=', petCount, petCountLocal
schism_components(i) = ESMF_GridCompCreate(name=trim(adjustl(message)), &
petList=petlist, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
!configList(i) = ESMF_ConfigCreate(rc=localrc)
!> @todo the SetAttribute implementation is buggy and thus not enabled, we
!> use for now the attribute of the component.
!>
!call ESMF_ConfigSetAttribute(configList(i), value=i, &
! label='schismInstance:', rc=localrc)
call ESMF_AttributeSet(schism_components(i), name='input_directory', &
value=trim(message), rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
!call ESMF_GridCompSet(schism_components(i), config=configList(i), rc=localrc)
!_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
deallocate (petList)
end do ! loop over schismCount
write (message, '(A,I3,A)') 'Created ', schismCount, ' instances of SCHISM'
allocate (schism_exports(schismCount))
allocate (schism_imports(schismCount))
do i = 1, schismCount
call ESMF_GridCompSetServices(schism_components(i), &
schismSetServices, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
write (message, '(A,I3.3)') 'schism_', i
schism_exports(i) = ESMF_StateCreate( &
name='schismExport_'//trim(adjustl(message)), rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
schism_imports(i) = ESMF_StateCreate( &
name='schismImport_'//trim(adjustl(message)), rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
end do
do i = 1, schismCount
!Init phase 0
call ESMF_GridCompInitialize(schism_components(i), importState=schism_imports(i), &
exportState=schism_exports(i), phase=0, clock=clock, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
end do
!Get info on simulation period
filename = './global.nml'
clock = clockCreateFrmParam(filename, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
do i = 1, schismCount
call ESMF_GridCompInitialize(schism_components(i), importState=schism_imports(i), &
exportState=schism_exports(i), phase=1, clock=clock, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
end do
do i = 1, schismCount
call ESMF_StateReconcile(schism_imports(i), vm=vm, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
call ESMF_StateReconcile(schism_exports(i), vm=vm, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
end do
! Loop over coupling timesteps until stopTime
do while (.not. (ESMF_ClockIsStopTime(clock)))
do i = 1, schismCount
call ESMF_GridCompRun(schism_components(i), importState=schism_imports(i), &
exportState=schism_exports(i), clock=clock, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
end do
call ESMF_ClockAdvance(clock, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
end do
do i = 1, schismCount
call ESMF_GridCompFinalize(schism_components(i), importState=schism_imports(i), &
exportState=schism_exports(i), clock=clock, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
call ESMF_StateDestroy(schism_imports(i), rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
call ESMF_StateDestroy(schism_exports(i), rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
call ESMF_GridCompDestroy(schism_components(i), rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
end do
deallocate (schism_exports)
deallocate (schism_imports)
deallocate (schism_components)
deallocate (configList)
call ESMF_ClockDestroy(clock, rc=localrc)
_SCHISM_LOG_AND_FINALIZE_ON_ERROR_(rc)
call ESMF_Finalize(rc=localrc)
end program main