-
Notifications
You must be signed in to change notification settings - Fork 68
/
coriolis.f90
245 lines (203 loc) · 7.33 KB
/
coriolis.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
!!
!! Copyright (C) 2019 Johns Hopkins University
!!
!! This file is part of lesgo.
!!
!! lesgo is free software: you can redistribute it and/or modify
!! it under the terms of the GNU General Public License as published by
!! the Free Software Foundation, either version 3 of the License, or
!! (at your option) any later version.
!!
!! lesgo is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU General Public License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with lesgo. If not, see <http://www.gnu.org/licenses/>.
!!
!*******************************************************************************
module coriolis
!*******************************************************************************
! This module contains all of the subroutines associated with scalar transport
use types, only : rprec
use pid_m
implicit none
save
private
public :: coriolis_init, coriolis_finalize, coriolis_calc
! Coriolis forcing
! 0->Off; 1->Fixed geostrophic wind;
! 2-> PID control of angle at specific height;
! 3-> interpolate from input file coriolis.dat
integer, public :: coriolis_forcing = 0
! fc -> coriolis parameter fc (dimensional)
! G -> geostrophic velocity (dimensional)
! alpha -> angle of geostrophic velocity
! (in radians measured counter-clockwise from x-direction)
real(rprec), public :: fc = 0.0001
real(rprec), public :: G = 8.0
real(rprec), public :: alpha = 0.78539816339
! phi_set -> angle of planar-averaged velocity
! (in radians measured counter-clockwise from x-direction)
! height_set -> height of angle set point (dimensional)
! Kp, Ki, Kd -> PID controller gains (dimensional)
integer, public :: pid_time = 0
real(rprec), public :: phi_set = 0.0
real(rprec), public :: height_set = 100
real(rprec), public :: Kp = 1e-4
real(rprec), public :: Ki = 3.8e-8
real(rprec), public :: Kd = 0.0
! Repeat_interval
real(rprec), public :: repeat_interval = 86400.0
! Geostrphic wind component
real(rprec) :: ug, vg
! PID controller
type(pid_t) :: pid
! Rotation rate
real(rprec) :: pid_rot_rate = 0._rprec
real(rprec), public :: phi_actual = 0._rprec
! Determining location of planar-averaging
logical :: plane_in_coord = .false.
integer :: k1, k2 ! Weighting locations
real(rprec) :: w1, w2 ! Weight values
! Interpolation of geostrophic wind
real(rprec), dimension(:), allocatable :: t_interp, alpha_interp, G_interp
contains
!*******************************************************************************
subroutine coriolis_init
!*******************************************************************************
! This subroutine initializes the variables for the coriolis
use param, only : z_i, u_star, L_z, nz, coord, nproc, dz, read_endian
use grid_m
use functions, only : binary_search, count_lines
logical :: exst
real(rprec) :: e_int
integer :: num_t, fid, i
! Non-dimensionalize
G = G/u_star
fc = fc*z_i/u_star
Ki = Ki*z_i/u_star
Kd = Kd*u_star/z_i
height_set = height_set/z_i
if (coriolis_forcing == 2) then
! Create PID controller
pid = pid_t(Kp, Ki, Kd, phi_set)
inquire (file='coriolis_pid.out', exist=exst)
if (exst) then
open(12, file='coriolis_pid.out', form='unformatted', convert=read_endian)
read(12) e_int, alpha
close(12)
pid%e_int = e_int
end if
! Create
if (height_set < 0.5*dz ) then
if (coord == 0) then
plane_in_coord = .true.
k1 = 1
k2 = 2
w1 = 1._rprec
w2 = 0._rprec
end if
else if (height_set > L_z) then
if (coord == nproc-1) then
plane_in_coord = .true.
k1 = Nz-2
k2 = Nz-1
w1 = 0._rprec
w2 = 1._rprec
end if
else
if (height_set >= grid%z(1) .and. height_set <= grid%z(nz)) then
plane_in_coord = .true.
k1 = binary_search(grid%z(1:), height_set)
k2 = k1 + 1
w1 = (height_set - grid%z(k1)) / dz
w2 = 1._rprec - w1
end if
end if
else if (coriolis_forcing == 3) then
! Count number of entries and allocate
num_t = count_lines('coriolis.dat')
allocate( t_interp(num_t) )
allocate( alpha_interp(num_t) )
allocate( G_interp(num_t) )
! Read values from file
open(newunit=fid, file='coriolis.dat', status='unknown', form='formatted', &
position='rewind')
do i = 1, num_t
read(fid,*) t_interp(i), G_interp(i), alpha_interp(i)
end do
end if
! Set components
ug = G*cos(alpha)
vg = G*sin(alpha)
end subroutine coriolis_init
!*******************************************************************************
subroutine coriolis_finalize
!*******************************************************************************
use param, only : read_endian
if (coriolis_forcing == 2) then
open(12, file='coriolis_pid.out', form='unformatted', convert=read_endian)
write(12) pid%e_int, alpha
close(12)
end if
end subroutine coriolis_finalize
!*******************************************************************************
subroutine coriolis_calc
!*******************************************************************************
use param, only : MPI_RPREC, comm, ierr, dt, total_time_dim, u_star, jt_total
use sim_param, only : u, v, RHSx, RHSy, nx, ny, nz
use functions, only : linear_interp
use mpi
real(rprec) :: ubar = 0, vbar = 0, temp
if (coriolis_forcing == 2) then
if (plane_in_coord) then
ubar = w1*sum(u(1:nx,:,k1))/(nx*ny) + w2*sum(u(1:nx,:,k2))/(nx*ny)
vbar = w1*sum(v(1:nx,:,k1))/(nx*ny) + w2*sum(v(1:nx,:,k2))/(nx*ny)
else
ubar = 0._rprec
vbar = 0._rprec
end if
#ifdef PPMPI
call MPI_AllReduce(ubar, temp, 1, MPI_RPREC, MPI_SUM, comm, ierr)
ubar = temp
call MPI_AllReduce(vbar, temp, 1, MPI_RPREC, MPI_SUM, comm, ierr)
vbar = temp
#endif
phi_actual = atan2(vbar,ubar)
if (jt_total < pid_time) then
! Use PID to get new angle
pid_rot_rate = -pid%advance(phi_actual, dt)
alpha = alpha - pid_rot_rate*dt
! Set components
ug = G*cos(alpha)
vg = G*sin(alpha)
else
pid_rot_rate = 0._rprec
end if
else if( coriolis_forcing == 3) then
! interpolate
alpha = linear_interp(t_interp, alpha_interp, &
mod(total_time_dim, repeat_interval))
G = linear_interp(t_interp, G_interp, &
mod(total_time_dim, repeat_interval))/u_star
! Set components
ug = G*cos(alpha)
vg = G*sin(alpha)
write(*,*) total_time_dim, G, alpha, ug, vg
end if
! Coriolis: add forcing to RHS
if (coriolis_forcing > 0) then
! This is to put in the coriolis forcing using coriol,ug and vg as
! precribed in param.f90. (ug,vg) specfies the geostrophic wind vector
! Note that ug and vg are non-dimensional (using u_star in param.f90)
RHSx(:,:,1:nz-1) = RHSx(:,:,1:nz-1) + fc * v(:,:,1:nz-1) - fc * vg
RHSy(:,:,1:nz-1) = RHSy(:,:,1:nz-1) - fc * u(:,:,1:nz-1) + fc * ug
if (coriolis_forcing == 2) then
RHSx(:,:,1:nz-1) = RHSx(:,:,1:nz-1) + pid_rot_rate * v(:,:,1:nz-1)
RHSy(:,:,1:nz-1) = RHSy(:,:,1:nz-1) - pid_rot_rate * u(:,:,1:nz-1)
end if
end if
end subroutine coriolis_calc
end module coriolis