forked from pmarguinaud/inv_trans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inv_trans_field_api.F90
316 lines (252 loc) · 11 KB
/
inv_trans_field_api.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
MODULE INV_TRANS_FIELD_API_MODULE
USE FIELD_MODULE
USE PARKIND1 ,ONLY : JPIM ,JPRB
USE PRINT_DEBUG_FIELD_API
IMPLICIT NONE
ABSTRACT INTERFACE
SUBROUTINE FSPGL_INTF (KM,KSL,KDGL,KFIELDS,PR1MU2,PFIELD,&
& KPTRU,KFLDUV,KFLDSC,KFLDPTRUV)
USE PARKIND1 ,ONLY : JPIM ,JPRB
INTEGER(KIND=JPIM),INTENT(IN) :: KM
INTEGER(KIND=JPIM),INTENT(IN) :: KSL
INTEGER(KIND=JPIM),INTENT(IN) :: KDGL
REAL(KIND=JPRB) ,INTENT(IN) :: PR1MU2(KDGL)
INTEGER(KIND=JPIM),INTENT(IN) :: KFIELDS
REAL(KIND=JPRB) ,INTENT(INOUT),TARGET :: PFIELD(2*KFIELDS,0:KDGL+1)
INTEGER(KIND=JPIM),INTENT(IN) :: KPTRU
INTEGER(KIND=JPIM),INTENT(IN) :: KFLDUV
INTEGER(KIND=JPIM),INTENT(IN) :: KFLDSC
INTEGER(KIND=JPIM),INTENT(IN) :: KFLDPTRUV(KFLDUV)
END SUBROUTINE FSPGL_INTF
END INTERFACE
CONTAINS
! Spectral to grid space transformation
SUBROUTINE INV_TRANS_FIELD_API(SPVORS,SPDIVS,SPSCALARS, &
& US, VS, VORS,DIVS,SCALARS, &
& DUS, DVS, DSCALARSEW, DSCALARSNS, &
& VSETUVS, VSETS, &
& LDACC, LDVERBOSE, &
& FSPGL_PROC)
TYPE(FIELD_BASIC_PTR),OPTIONAL :: SPVORS(:), SPDIVS(:) !Spectral vector fields : vorticity and divergence fields (in)
TYPE(FIELD_BASIC_PTR),OPTIONAL :: SPSCALARS(:) !Spectral scalar fields (in)
TYPE(FIELD_BASIC_PTR),OPTIONAL :: US(:),VS(:) !Grid vector fields (out)
TYPE(FIELD_BASIC_PTR),OPTIONAL :: VORS(:),DIVS(:) !Grid vector fields :vorticity and divergence (out)
TYPE(FIELD_BASIC_PTR),OPTIONAL :: SCALARS(:) !Grid scalar fields (out)
TYPE(FIELD_BASIC_PTR),OPTIONAL :: DUS(:),DVS(:) !Grid vector fields derivatives EW (out)
TYPE(FIELD_BASIC_PTR),OPTIONAL :: DSCALARSEW(:), DSCALARSNS(:) !Grid scalar fields derivatives EW and NS (out)
INTEGER(KIND=JPIM),OPTIONAL :: VSETUVS(:) !Meta data vector fields
INTEGER(KIND=JPIM),OPTIONAL :: VSETS(:) !Meta data scalar fields
LOGICAL, OPTIONAL :: LDACC
LOGICAL, OPTIONAL :: LDVERBOSE
PROCEDURE (FSPGL_INTF), OPTIONAL :: FSPGL_PROC
! Local variables
! Temporary arrays
TYPE(FIELD_1RB_VIEW), ALLOCATABLE :: SPVORL(:), SPDIVL(:)
TYPE(FIELD_1RB_VIEW), ALLOCATABLE :: SPSCALARL(:)
TYPE(FIELD_2RB_VIEW), ALLOCATABLE :: UL(:),VL(:)
TYPE(FIELD_2RB_VIEW), ALLOCATABLE :: VORL(:),DIVL(:)
TYPE(FIELD_2RB_VIEW), ALLOCATABLE :: SCALARL(:)
TYPE(FIELD_2RB_VIEW), ALLOCATABLE :: DUL(:),DVL(:)
TYPE(FIELD_2RB_VIEW), ALLOCATABLE :: DSCALAREWL(:), DSCALARNSL(:)
REAL(KIND=JPRB),ALLOCATABLE :: ZSPVOR(:,:),ZSPDIV(:,:) ! Spectral vector fields (in)
REAL(KIND=JPRB),ALLOCATABLE :: ZSPSCALAR(:,:) ! Spectral scalar fields (in)
REAL(KIND=JPRB),ALLOCATABLE :: ZGPUV(:,:,:,:) ! Grid vector fields (out)
REAL(KIND=JPRB),ALLOCATABLE :: ZGP(:,:,:) ! Grid scalar fields (out)
INTEGER(KIND=JPIM) :: IFLDSUV ! Number of input spectral vector fields
INTEGER(KIND=JPIM) :: IFLDS ! Number of input spectral scalar fields
INTEGER(KIND=JPIM) :: IFLDG ! Number of output scalar fields
INTEGER(KIND=JPIM) :: KFLDG ! Size of output scalar fields array
INTEGER(KIND=JPIM) :: IFLDGUV ! Number of output vector fields
INTEGER(KIND=JPIM) :: KFLDGUV ! Size of output vector fields array
INTEGER(KIND=JPIM) :: OFFSET
INTEGER(KIND=JPIM) :: ISPEC2 ! Size of spectral fields (truncation)
INTEGER(KIND=JPIM) :: IPROMA,IGPBLKS ! Size of output grid fields
INTEGER(KIND=JPIM) :: JFLD ! field counter
LOGICAL :: LDSCDERS ! indicating if derivatives of scalar variables are req.
LOGICAL :: LDVORGP ! indicating if grid-point vorticity is req.
LOGICAL :: LDDIVGP ! indicating if grid-point divergence is req.
LOGICAL :: LDUVDER ! indicating if E-W derivatives of u and v are req.
LOGICAL :: LLVERBOSE ! indicating if verbose output is req.
#include "inv_trans.h"
LLVERBOSE = .FALSE.
IF (PRESENT(LDVERBOSE)) LLVERBOSE = LDVERBOSE
! 1. VECTOR FIELDS TRANSFORMATION
! Check if all provided vector field information is consistent
IF (PRESENT(US) .NEQV. PRESENT(VS))STOP "US/VS"
IF (PRESENT(US) .NEQV. PRESENT(SPVORS))STOP "US/VORS"
IF (PRESENT(US) .NEQV. PRESENT(SPDIVS))STOP "US/DIVS"
IF (PRESENT(US) .NEQV. PRESENT(VSETUVS))STOP "US/VSETUVS"
KFLDGUV = 0
! Do we have vector fields?
IF (PRESENT(US)) THEN
IF ((SIZE(US)/= SIZE(VS)).OR.(SIZE(US)/= SIZE(SPDIVS)).OR.(SIZE(US)/= SIZE(SPVORS))) THEN
PRINT*,"INVALID LIST SIZES:"
PRINT*," US = ",SIZE(US)
PRINT*," VS = ",SIZE(VS)
PRINT*," VORS = ",SIZE(SPVORS)
PRINT*," DIVS = ",SIZE(SPDIVS)
STOP 1
ENDIF
WRITE(*,*)"SPVORL"
SPVORL = LS (SPVORS, LDACC)
WRITE(*,*)"SPDIVL"
SPDIVL = LS (SPDIVS, LDACC)
WRITE(*,*)"UL"
UL = LG (US)
WRITE(*,*)"VL"
VL = LG (VS)
IF ((SIZE (UL) /= SIZE (VL)) .OR. (SIZE (SPVORL) /= SIZE (SPDIVL)) .OR. (SIZE (UL) /= SIZE (VSETUVS))) THEN
PRINT*,"ERROR: INCONSISTENT NUMBER OF FIELD_VIEW FOR VECTORS:"
WRITE(*,*) " UL = ", SIZE (UL)
WRITE(*,*) " VL = ", SIZE (VL)
WRITE(*,*) " SPVORL = ", SIZE (SPVORL)
WRITE(*,*) " SPDIVL = ", SIZE (SPDIVL)
WRITE(*,*) " VSETUVS = ", SIZE (VSETUVS)
STOP 1
ENDIF
IPROMA = SIZE(UL(1)%V,1)
IGPBLKS = SIZE(UL(1)%V,2)
IFLDGUV = SIZE(UL) ! Number of vector fields
ISPEC2 = SIZE(SPVORL(1)%V,1) ! Size of spectral fields
IFLDSUV = SIZE(SPVORL) ! Number of input spectral vector fields
LDUVDER = .FALSE.
LDVORGP = .FALSE.
LDDIVGP = .FALSE.
LDSCDERS = .FALSE.
KFLDGUV = KFLDGUV + 2
IF (PRESENT(DUS) .AND. PRESENT(DVS)) THEN
LDUVDER = .TRUE.
KFLDGUV = KFLDGUV + 2
DUL = LG (DUS)
DVL = LG (DVS)
ENDIF
IF (PRESENT(VORS)) THEN
LDVORGP = .TRUE.
KFLDGUV = KFLDGUV + 1
VORL = LG (VORS)
ENDIF
IF (PRESENT(DIVS)) THEN
LDDIVGP = .TRUE.
KFLDGUV = KFLDGUV + 1
DIVL = LG (DIVS)
ENDIF
! Allocate vector field input in spectral space
ALLOCATE(ZSPVOR(ISPEC2,IFLDSUV))
ALLOCATE(ZSPDIV(ISPEC2,IFLDSUV))
! Allocate vector field output in grid space
ALLOCATE(ZGPUV(IPROMA,IFLDGUV,KFLDGUV,IGPBLKS))
! Copy from fields to temporary arrays (1D copy thanks to FIELD VIEW)
DO JFLD=1,IFLDSUV
ZSPVOR(:,JFLD) = SPVORL(JFLD)%V(:)
ZSPDIV(:,JFLD) = SPDIVL(JFLD)%V(:)
ENDDO
ELSE
! US is not provided, we do not have to compute the corresponding vector output
IFLDSUV = 0
ALLOCATE(ZGPUV(0,IFLDSUV,2,0),ZSPVOR(0,0),ZSPDIV(0,0))
ENDIF
! 2. SCALAR FIELDS TRANSFORMATION
! Check if all provided scalar field information is consistent
IF (PRESENT(SCALARS) .NEQV. PRESENT(SPSCALARS))STOP "GRID/SPEC"
IF (PRESENT(SCALARS) .NEQV. PRESENT(VSETS))STOP "GRID/VSETS"
IF (PRESENT(SCALARS)) THEN
IF ((SIZE(SCALARS)/= SIZE(SPSCALARS))) THEN
PRINT*,"ERROR: INCONSISTENT NUMBER OF FIELD_VIEW FOR SCALARS:"
PRINT*," SIZE(GRID) = ",SIZE(SCALARS)
PRINT*," SIZE(SPEC) = ",SIZE(SPSCALARS)
STOP 1
ENDIF
SCALARL = LG (SCALARS, LDACC)
SPSCALARL = LS (SPSCALARS, LDACC)
IF (SIZE (SCALARL) /= SIZE (VSETS)) THEN
WRITE(*,*) " SIZE (SCALARL) = ", SIZE (SCALARL)
WRITE(*,*) " SIZE (SPSCALARS) = ", SIZE (SPSCALARS)
WRITE(*,*) " SIZE (VSETS) = ", SIZE (VSETS)
STOP 1
ENDIF
IPROMA = SIZE(SCALARL(1)%V,1)
IGPBLKS = SIZE(SCALARL(1)%V,2)
IFLDG = SIZE(SCALARL) ! Number of output scalar fields in grid space
ISPEC2 = SIZE(SPSCALARL(1)%V,1)
IFLDS = SIZE(SPSCALARL) ! Number of input scalar fields in spectral space
KFLDG = IFLDG
IF (PRESENT(DSCALARSEW) .AND. PRESENT(DSCALARSNS)) THEN
LDSCDERS = .TRUE.
KFLDG = KFLDG + 2 * IFLDG
DSCALAREWL = LG (DSCALARSEW)
DSCALARNSL = LG (DSCALARSNS)
ENDIF
! Allocate scalar field input in spectral space
ALLOCATE(ZSPSCALAR(ISPEC2,IFLDS))
! Allocate scalar field output in grid space
ALLOCATE(ZGP(IPROMA,KFLDG,IGPBLKS))
! Copy scalar spectral fields to temporary arrays (1D copy thanks to FIELD VIEW)
DO JFLD=1,IFLDS
ZSPSCALAR(:,JFLD) = SPSCALARL(JFLD)%V(:)
ENDDO
ELSE
IFLDG = 0
ALLOCATE(ZGP(0,IFLDG,0),ZSPSCALAR(0,0))
ENDIF
! 3. CALL INV_TRANS
IF (LLVERBOSE) THEN
CALL PRINT_DEBUG_INV_TRANS_FIELD_API(ZSPVOR, ZSPDIV, ZSPSCALAR, ZGPUV, ZGP, &
& SPVORS,SPDIVS,SPSCALARS, &
& US, VS, VORS,DIVS,SCALARS, &
& DUS, DVS, DSCALARSEW, DSCALARSNS, &
& VSETUVS, VSETS, IFLDSUV,IFLDS,IFLDG,IFLDGUV, &
& LDSCDERS,LDVORGP,LDDIVGP, LDUVDER, &
& ISPEC2, IPROMA,IGPBLKS,KFLDG,KFLDGUV)
ENDIF
IF (PRESENT (FSPGL_PROC)) THEN
CALL INV_TRANS(PSPVOR = ZSPVOR,PSPDIV = ZSPDIV,PGPUV = ZGPUV,KVSETUV = VSETUVS, &
& PSPSC2 = ZSPSCALAR,PGP2 = ZGP,KVSETSC2 = VSETS, &
& LDSCDERS = LDSCDERS, LDVORGP = LDVORGP, LDDIVGP = LDDIVGP, LDUVDER = LDUVDER, &
& KPROMA = IPROMA,FSPGL_PROC=FSPGL_PROC, NGPBLKS = IGPBLKS)
ELSE
CALL INV_TRANS(PSPVOR = ZSPVOR,PSPDIV = ZSPDIV,PGPUV = ZGPUV,KVSETUV = VSETUVS, &
& PSPSC2 = ZSPSCALAR,PGP2 = ZGP,KVSETSC2 = VSETS, &
& LDSCDERS = LDSCDERS, LDVORGP = LDVORGP, LDDIVGP = LDDIVGP, LDUVDER = LDUVDER, &
& KPROMA = IPROMA, NGPBLKS = IGPBLKS)
ENDIF
! 4. Copy back data to fields
! Copy vector fields back from temporary vector arrays
OFFSET = 1
DO JFLD=1,IFLDGUV
UL(JFLD)%V(:,:) = ZGPUV(:,JFLD,OFFSET,:)
VL(JFLD)%V(:,:) = ZGPUV(:,JFLD,OFFSET + 1,:)
ENDDO
OFFSET = OFFSET + 2
! copy derivatives, divergences and vorticities back from temporary vector arrays
IF (LDUVDER) THEN
DO JFLD=1,IFLDGUV
DUL(JFLD)%V(:,:) = ZGPUV(:,JFLD,OFFSET ,:)
DVL(JFLD)%V(:,:) = ZGPUV(:,JFLD,OFFSET + 1 ,:)
ENDDO
OFFSET = OFFSET + 2
ENDIF
IF (LDVORGP) THEN
DO JFLD=1,IFLDGUV
VORL(JFLD)%V(:,:) = ZGPUV(:,JFLD,OFFSET,:)
ENDDO
OFFSET = OFFSET + 1
ENDIF
IF (LDDIVGP) THEN
DO JFLD=1,IFLDGUV
DIVL(JFLD)%V(:,:) = ZGPUV(:,JFLD,OFFSET,:)
ENDDO
OFFSET = OFFSET + 1
ENDIF
! Copy scalar fields back from temporary scalar arrays
DO JFLD=1,IFLDG
SCALARL(JFLD)%V(:,:) = ZGP(:,JFLD,:)
ENDDO
OFFSET = IFLDG
IF (LDSCDERS) THEN
DO JFLD=1,IFLDG
DSCALAREWL(JFLD)%V(:,:) = ZGP(:,OFFSET+JFLD,:)
DSCALARNSL(JFLD)%V(:,:) = ZGP(:,2 * OFFSET+JFLD,:)
ENDDO
ENDIF
END SUBROUTINE INV_TRANS_FIELD_API
END MODULE INV_TRANS_FIELD_API_MODULE