-
Notifications
You must be signed in to change notification settings - Fork 4
/
cg_drag_torch_mod.f90
216 lines (159 loc) · 7.38 KB
/
cg_drag_torch_mod.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
module cg_drag_torch_mod
use iso_fortran_env, only: error_unit
! Imports primitives used to interface with C
use, intrinsic :: iso_c_binding, only: c_int64_t, c_float, c_char, c_null_char, c_ptr, c_loc
! Import library for interfacing with PyTorch
use ftorch
!-------------------------------------------------------------------
implicit none
private error_mesg, RADIAN, NOTE, WARNING, FATAL
public cg_drag_ML_init, cg_drag_ML_end, cg_drag_ML
!--------------------------------------------------------------------
! data used in this module to bind to FTorch
!
!--------------------------------------------------------------------
! model ML model type bound to python
!
!--------------------------------------------------------------------
type(torch_module) :: model
integer, parameter :: NOTE=0, WARNING=1, FATAL=2
real(kind=8), parameter :: PI = 4.0 * ATAN(1.0)
real(kind=8), parameter :: RADIAN = 180.0 / PI
!--------------------------------------------------------------------
!--------------------------------------------------------------------
contains
! PRIVATE ROUTINES
subroutine error_mesg (routine, message, level)
character(len=*), intent(in) :: routine, message
integer, intent(in) :: level
! input:
! routine name of the calling routine (character string)
! message message written to output (character string)
! level set to NOTE, MESSAGE, or FATAL (integer)
write(error_unit, '(a,":", a)') routine, message
end subroutine error_mesg
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!
! PUBLIC SUBROUTINES
!
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
!####################################################################
subroutine cg_drag_ML_init(model_dir, model_name)
!-----------------------------------------------------------------
! cg_drag_ML_init is called from cg_drag_init and initialises
! anything required for the ML calculation of cg_drag such as
! an ML model
!
!-----------------------------------------------------------------
!-----------------------------------------------------------------
! intent(in) variables:
!
! model_dir full filepath to the model directory
! model_name filename of the TorchScript model
!
!-----------------------------------------------------------------
character(len=1024), intent(in) :: model_dir
character(len=1024), intent(in) :: model_name
!-----------------------------------------------------------------
! Initialise the ML model to be used
model = torch_module_load(trim(model_dir)//"/"//trim(model_name)//c_null_char)
end subroutine cg_drag_ML_init
!####################################################################
subroutine cg_drag_ML_end
!-----------------------------------------------------------------
! cg_drag_ML_end is called from cg_drag_end and is a destructor
! for anything used in the ML part of calculating cg_drag such
! as an ML model.
!
!-----------------------------------------------------------------
! destroy the model
call torch_module_delete(model)
end subroutine cg_drag_ML_end
!####################################################################
subroutine cg_drag_ML(uuu, vvv, psfc, lat, gwfcng_x, gwfcng_y)
!-----------------------------------------------------------------
! cg_drag_ML returns the x and y gravity wave drag forcing
! terms following calculation using an external neural net.
!
!-----------------------------------------------------------------
!-----------------------------------------------------------------
! intent(in) variables:
!
! is,js starting subdomain i,j indices of data in
! the physics_window being integrated
! uuu,vvv arrays of model u and v wind
! psfc array of model surface pressure
! lat array of model latitudes at cell boundaries [radians]
!
! intent(out) variables:
!
! gwfcng_x time tendency for u eqn due to gravity-wave forcing
! [ m/s^2 ]
! gwfcng_y time tendency for v eqn due to gravity-wave forcing
! [ m/s^2 ]
!
!-----------------------------------------------------------------
real(kind=8), dimension(:,:,:), target, intent(in) :: uuu, vvv
real(kind=8), dimension(:,:), target, intent(in) :: psfc
real(kind=8), dimension(:,:), target :: lat
real(kind=8), dimension(:,:,:), intent(out), target :: gwfcng_x, gwfcng_y
!-----------------------------------------------------------------
!-------------------------------------------------------------------
! local variables:
!
! dtdz temperature lapse rate [ deg K/m ]
!
!---------------------------------------------------------------------
integer :: imax, jmax, kmax
integer(c_int), parameter :: dims_2D = 2
integer(c_int64_t) :: shape_2D(dims_2D)
integer(c_int) :: stride_2D(dims_2D)
integer(c_int), parameter :: dims_1D = 2
integer(c_int64_t) :: shape_1D(dims_1D)
integer(c_int) :: stride_1D(dims_1D)
integer(c_int), parameter :: dims_out = 2
integer(c_int64_t) :: shape_out(dims_out)
integer(c_int) :: stride_out(dims_out)
! Set up types of input and output data and the interface with C
type(torch_tensor) :: gwfcng_x_tensor, gwfcng_y_tensor
integer(c_int), parameter :: n_inputs = 3
type(torch_tensor), dimension(n_inputs), target :: model_input_arr
!----------------------------------------------------------------
! reshape tensors as required
imax = size(uuu, 1)
jmax = size(uuu, 2)
kmax = size(uuu, 3)
! pseudo-flatten data (nlat, nlon, n) --> (nlat*nlon, n)
! Note that the '1D' tensor has 2 dimensions, one of which is size 1
shape_2D = (/ imax*jmax, kmax /)
shape_1D = (/ imax*jmax, 1 /)
shape_out = (/ imax*jmax, kmax /)
stride_1D = (/ 1, 2 /)
stride_2D = (/ 1, 2 /)
stride_out = (/ 1, 2 /)
lat = lat*RADIAN
! Create input/output tensors from the above arrays
model_input_arr(3) = torch_tensor_from_blob(c_loc(lat), dims_1D, shape_1D, torch_kFloat64, torch_kCPU, stride_1D)
model_input_arr(2) = torch_tensor_from_blob(c_loc(psfc), dims_1D, shape_1D, torch_kFloat64, torch_kCPU, stride_1D)
! Zonal
model_input_arr(1) = torch_tensor_from_blob(c_loc(uuu), dims_2D, shape_2D, torch_kFloat64, torch_kCPU, stride_2D)
gwfcng_x_tensor = torch_tensor_from_blob(c_loc(gwfcng_x), dims_out, shape_out, torch_kFloat64, torch_kCPU, stride_out)
! Run model and Infer
call torch_module_forward(model, model_input_arr, n_inputs, gwfcng_x_tensor)
! Meridional
model_input_arr(1) = torch_tensor_from_blob(c_loc(vvv), dims_2D, shape_2D, torch_kFloat64, torch_kCPU, stride_2D)
gwfcng_y_tensor = torch_tensor_from_blob(c_loc(gwfcng_y), dims_out, shape_out, torch_kFloat64, torch_kCPU, stride_out)
! Run model and Infer
call torch_module_forward(model, model_input_arr, n_inputs, gwfcng_y_tensor)
write (*,*) gwfcng_x(1, 1, 1:10)
write (*,*) gwfcng_y(1, 1, 1:10)
! Cleanup
call torch_tensor_delete(model_input_arr(1))
call torch_tensor_delete(model_input_arr(2))
call torch_tensor_delete(model_input_arr(3))
call torch_tensor_delete(gwfcng_x_tensor)
call torch_tensor_delete(gwfcng_y_tensor)
! write(*,*) gwfcng_y(1:5, 1:5, 1)
end subroutine cg_drag_ML
!####################################################################
end module cg_drag_torch_mod