-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRCMR_routines.f90
executable file
·328 lines (266 loc) · 9.51 KB
/
RCMR_routines.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
317
318
319
320
321
322
323
324
325
326
327
328
! Copyright (C) 2002 Regents of the University of Michigan, portions used with permission
! For more information, see http://csem.engin.umich.edu/tools/swmf
!----------------------------------------------------------------------------
! $Id: RCMR_routines.f90,v 1.5 2013/10/24 18:53:28 agburr Exp $
!
! Author: Asad, UMichigan, 2/2013
!
! Comments: Routines to run RCMR data assimilation
!
! AGB 3/31/13: Added comments, changed variable names to be more descriptive,
! changed subroutine format for consistency with GITM files,
! removed test output files, removed unused variables, streamlined
! MPI calls.
! AGB 10/23/13: Adapted to allow driving of photoelectron heating efficiency
!-----------------------------------------------------------------------------
subroutine run_RCMR
use ModInputs
use ModTime
use ModGITM
use ModMpi
use ModRCMR
use ModSatellites, only: SatCurrentDat, SatAltDat, nRCMRSat, RCMRSat
use ModEUV, only: sza
implicit none
integer :: status(MPI_STATUS_SIZE), iError
real :: output_est, ulimit, llimit
double precision :: localVar
call MPI_BARRIER(iCommGITM, iError)
localVar = SatAltDat(RCMRSat(1))
call MPI_REDUCE(localVar, SatAltDat(RCMRSat(1)), 1, MPI_DOUBLE_PRECISION, &
MPI_MAX, 0, iCommGITM, iError)
if (iProc==0) then
do mm=0,(nProcs-1)
if (Sat_Proc(mm + 1) .ne. 0) then
SatAltDat(RCMRSat(1)) = Sat_Proc(mm + 1)
end if
end do
end if
! AGB: Initialize the output estimate
if(RCMROutType == "F107") then
output_est = f107_est
llimit = 70.0
ulimit = 400.0
else if(RCMROutType == "PHOTOELECTRON") then
output_est = PhotoElectronHeatingEfficiency_est
llimit = 0.02
ulimit = 0.2
else
write (*,*) "ERROR: unknown RCMR output type", RCMROutType
end if
! Begin the first RCMR loop
if (mod((istep-1)*Dts,Measure_Dts) == 0.0 .AND. TRUTH_or_ID==1 .AND. &
iProc==0) then
zp(:,uStep) = SatAltDat(RCMRSat(1)) - SatCurrentDat(RCMRSat(1))
! Set the averaged error
if (uStep <= 120) then
zav(:,ustep) = sum(zp(:,1:uStep))/uStep
else
zav(:,ustep) = sum(zp(:,uStep-120+1:uStep))/120
end if
y(1,uStep) = 1.0e12*zav(1,uStep)
z(:,uStep) = 1.0e12*zav(1,uStep)
if (ustep <= l_dim) then
y_mat(:,l_dim-ustep+1:l_dim) = y(:,1:ustep)
z_mat(:,l_dim-ustep+1:l_dim) = z(:,1:ustep)
if (ustep>1) then
u_mat(:,l_dim-ustep+2:l_dim) = u(:,1:ustep-1)
end if
else if (ustep > l_dim) then
y_mat = y(:,ustep-(l_dim)+1:ustep)
u_mat = u(:,ustep-(l_dim):ustep-1)
z_mat = z(:,ustep-(l_dim)+1:ustep)
end if
if (ustep<=Pc) then
Pcc = ustep-1
if (Pcc < 0) then
Pcc=0
end if
else
Pcc=Pc
end if
lu2=lu*lu
if (ustep>C_on) then
control_on = 1
end if
CALL RCMR_Function(llimit, ulimit, Nc, ustep, s_dhat, lz, lu, lu2, ly, &
l_dim, Pc, Pcc, control_on, dbuffer, C_on, dhat, eta, lambda, usum, &
T, R2, y_mat, z_mat, u_mat, P1, u_out, theta1, UB)
! Enforce the realistic physical limitations
if (u_out(1,1) < llimit) then
u_out(1,1) = llimit
end if
if (u_out(1,1) >= ulimit) then
u_out(1,1) = ulimit
end if
up(1,ustep) = u_out(1,1)
! Find the averaged error
if (uStep <= 90) then
u(:,ustep) = sum(up(:,1:uStep))/uStep
else
u(:,ustep) = sum(up(:,uStep-90+1:uStep))/90
end if
! Save the output if GITM is initialized
! AGB Question: would it not be better to ask this before calculating
! a new output estimate?
if (ustep > C_on) then
output_est = u(1, ustep)
else if (ustep <= C_on) then
u(:,ustep) = output_est
end if
if(RCMROutType == 'F107') then
f107_est = output_est
f107a_est = f107_est
else if(RCMROutType == "PHOTOELECTRON") then
PhotoElectronHeatingEfficiency_est = output_est
end if
ustep = ustep + 1
end if
! END of the first RCMR loop
! Send the F10.7 Estimate to all the different processors
! AGB Question: why use MPI_SCATTER and not MPI_Bcast?
if (iProc==0) then
scattered(:) = output_est
end if
call MPI_SCATTER(scattered, 1, mpi_double_precision, scatter, 1, &
mpi_double_precision, 0, iCommGITM, iError)
if(RCMROutType == 'F107') then
f107_est = scatter
f107a_est = f107_est
else if(RCMROutType == "PHOTOELECTRON") then
PhotoElectronHeatingEfficiency_est = scatter
end if
end subroutine run_RCMR
subroutine RCMR_Function(llimit, ulimit, Nc, ustep, s_dhat, lz, lu, lu2, ly, &
l_dim, Pc, Pcc, control_on, dbuffer, C_on, dhat, eta, lambda, usum, T, &
R2, y_mat, z_mat, u_mat, P1, u_out, theta1, UB)
use ModBlasLapack, only: LAPACK_getrf, LAPACK_getrs
implicit none
! *
! Input variables
! *
real, intent(in) :: llimit, ulimit
integer, intent(in) :: Nc, ustep, s_dhat, lu, ly, lz, l_dim, Pc, Pcc
integer, intent(in) :: control_on, dbuffer, C_on
integer, dimension(1,s_dhat), intent(in) :: dhat
double precision :: eta
double precision, dimension(1,1), intent(in) :: lambda, usum
double precision, dimension(s_dhat*lz, lu), intent(in) :: T
double precision, dimension(s_dhat*lz,s_dhat*lz), intent(in) :: R2
double precision, dimension(ly, l_dim), intent(in) :: y_mat ! y_data
double precision, dimension(lz, l_dim), intent(in) :: z_mat ! z_data
double precision, dimension(lu, l_dim), intent(in) :: u_mat ! u_data
! *
! Input and output variables
! *
double precision, dimension(lu*Nc+ly*Nc,lu*Nc+ly*Nc) :: P1
! AGB MOVED FROM INPUT
integer :: iii, lb_z, errorhandler, lu2
double precision, dimension(1,1) :: xi_RLS, r_RLS
double precision, dimension(lu*Nc+ly*Nc,1) :: pi_RLS, k_RLS
double precision, dimension(lu, 1) :: umod
! *
! Work variables
! *
integer, dimension(lu) :: PIVOTARRAY
integer, dimension(1,s_dhat) :: dhatfl ! used with flipped dhat
double precision intr_a
double precision, dimension(lu2) :: WORK
double precision, dimension(1,1) :: intr_b
double precision, dimension(s_dhat*lz,1) :: zmod ! mat dim r=lz, c=size(dhat, control_on)
double precision, dimension(lu,1) :: Us
double precision, dimension(lz,1) :: zr
double precision, dimension(lu,lu) :: To_invert, Idty, Inverted !FA=lu, SA=lu
double precision, dimension(ly,Nc) :: py
double precision, dimension(ly*(Nc),1) :: pym
double precision, dimension(lu, Nc) :: pu
double precision, dimension(lu*Nc,1) :: pum
double precision, dimension(lu*Nc+ly*Nc,1) :: H1
double precision, dimension(1,lu*Nc+ly*Nc) :: Hm
double precision, dimension(1,lu*Nc+ly*Nc) :: K1
! *
! Output variables
! *
double precision, dimension(1,1), intent(out) :: UB
double precision, dimension(lu,1), intent(out) :: u_out
double precision, dimension(lu,Nc*ly+Nc*lu), intent(out) :: theta1
! Identity matrix
iDty = 0.0;
Inverted = 0.0;
do iii=1,lu
Idty(iii,iii) = 1.0
Inverted(iii,iii) = 1.0
end do
! Initialization
zmod = 0.0
! *
! In this section, we calculate Uhat
! *
if (ustep > maxval(dhat)) then
zmod(1:lz,1) = z_mat(:,l_dim)
dhatfl(1,:) = dhat(1,s_dhat:1:-1)
if (s_dhat>1) then
make_zmod: do iii=1,(s_dhat-1)
lb_z = dhat(1,s_dhat)
zmod(1+(iii*lz):1+(iii*lz)+lz-1,1) = z_mat(:, &
l_dim-( dhat(1,s_dhat)-dhatfl(1,iii+1)))
end do make_zmod
end if
umod(:,1) = u_mat(:,l_dim-dhat(1,s_dhat)+1);
zr(:,1) = z_mat(:,size(z_mat,2));
To_Invert = matmul(matmul(transpose(T),R2),T) &
+ eta*matmul(matmul(transpose(zr),zr),Idty)
! invert this
call LAPACK_getrf(lu, lu, To_Invert, lu, PIVOTARRAY, errorHandler)
CALL LAPACK_getrs('n', lu, lu, To_Invert, lu, PIVOTARRAY, Inverted, lu, &
errorHandler)
Us = matmul(matmul(matmul(Inverted,transpose(T)),R2), &
(-zmod+matmul(T,umod)))
UB(1,1) = Us(1,1)
if (Us(1,1)>=ulimit) then
Us(1,1) = ulimit
else if(Us(1,1)<=llimit) then
Us(1,1) = llimit
end if
if (ustep > dbuffer) then
!Strictly proper
py = y_mat(:,l_dim-Nc-dhat(1,s_dhat):l_dim-dhat(1,s_dhat)-1)
!Exactly proper
!py = y_mat(:,l_dim-Nc-dhat(1,s_dhat)+1:l_dim-dhat(1,s_dhat))
pym = reshape(py, shape(pym))
pu = u_mat(:,(l_dim+1)-Nc-dhat(1,s_dhat):(l_dim+1)-dhat(1,s_dhat)-1)
if(minval(pu) == 0.0) then
pu = 0.0
end if
pum = reshape(pu, shape(pum))
H1(1:ly*(Nc),1) = pym(:,1)
H1(ly*(Nc)+1:ly*(Nc)+lu*Nc,1) = pum(:,1)
Hm = matmul(transpose(H1), P1)
pi_RLS = matmul(P1,H1)
r_RLS = 1/(lambda(1,1)+matmul(transpose(H1),pi_RLS))
k_RLS = r_RLS(1,1)*pi_RLS
xi_RLS = Us-matmul(theta1, H1)
theta1 = theta1 + matmul(xi_RLS,transpose(k_RLS))
P1 = (P1 - matmul(k_RLS,transpose(pi_RLS)))
py = y_mat(:,l_dim-Nc:l_dim-1) !Strictly proper
!py = y_mat(:,l_dim-Nc+1:l_dim) !Exactly proper
pym = reshape(py, shape(pym))
pu = u_mat(:,l_dim-Nc+1:l_dim)
pum = reshape(pu, shape(pum))
H1(1:ly*(Nc),1) = pym(:,1)
H1(ly*(Nc)+1:ly*(Nc)+lu*Nc,1) = pum(:,1)
if (control_on == 1) then
u_out = matmul(theta1,H1)
else
u_out = 0.0
end if
else
u_out = 0.0
theta1 = theta1
end if
else
u_out = 0.0
theta1 = theta1
end if
return
end subroutine RCMR_Function