-
Notifications
You must be signed in to change notification settings - Fork 0
/
GP.F90
460 lines (403 loc) · 11.9 KB
/
GP.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
module GP
#include "definition.h"
use gp_data
real :: rt2 = SQRT(2.)
contains
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!! Quadrature Rules for Integrated Kernel!!!!!!
function intg_kernel(x, y, eldel) result(f)
implicit none
real, intent(IN) :: x, y, eldel
real :: f
f = 0.
if (gp_quad == 'exact') then
f = quad_exact(x, y, eldel)
!!$ elseif (gp_quad == 'midpt') then
!!$ f = K(x, y)
!!$ elseif (gp_quad == 'trap') then
!!$ f = 1./4. * ( &
!!$ K(x + 0.5, y + 0.5) + K(x - 0.5, y + 0.5) + &
!!$ K(x + 0.5, y - 0.5) + K(x - 0.5, y - 0.5) )
!!$ elseif (gp_quad == 'simpson') then
!!$ f = 1./36. * ( &
!!$ K(x+0.5,y+0.5) + 4.*K(x,y+0.5) + K(x-0.5,y+0.5) + 4.*( &
!!$ K(x+0.5,y ) + 4.*K(x,y ) + K(x-0.5,y ) ) + &
!!$ K(x+0.5,y-0.5) + 4.*K(x,y-0.5) + K(x-0.5,y-0.5) )
end if
return
end function intg_kernel
function quad_cross(x, t, eldel) result(f)
implicit none
real, intent(IN) :: x, t, eldel
real :: f
f = 0.
if (gp_quad == 'midpt') then
!f = K(x, t)
!!$ elseif (gp_quad == 'trap') then
!!$ f = 0.5*( K(x-0.5, t) + K(x+0.5, t) )
!!$ elseif (gp_quad == 'simpson') then
!!$ f = 1./6. * ( K(x-0.5, t) + 4.*K(x,t) + K(x+0.5,t) )
elseif (gp_quad == 'exact') then
f = eldel*SQRT(.5*PI)*int_egrand(x,t,eldel)
end if
return
end function quad_cross
function intg_predvec(x, eldel) result(T)
implicit none
real, intent(IN) :: x, eldel
real, dimension(2) :: T
T(1) = quad_cross(x, -0.5, eldel)
T(2) = quad_cross(x, 0.5, eldel)
return
end function intg_predvec
function K(x, y) result(f)
implicit none
real, intent(IN) :: x, y
real :: f
f = 0.
if (gp_kernel == 'matern') then
!f = matern(x,y)
elseif (gp_kernel == 'SE') then
!f = SE(x,y)
elseif (gp_kernel == 'RQ') then
!f = RQ(x,y)
elseif (gp_kernel == 'NN') then
!f = NN(x,y)
elseif (gp_kernel == 'GB') then
!f = gibbs(x,y)
end if
return
end function K
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!! Kernel Functions !!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!! Squared Exponential!!!!!!!!!!!!!!!!!!
function SE(x, y, eldel) result(f)
implicit none
real, intent(IN) :: x, y, eldel
real :: f, r
r = abs(x-y)
f = EXP( -0.5*(r/eldel)**2 )
return
end function SE
!!$ function SE_der_cov_dxy(x, y) result(f)
!!$ !this is the covariance of the derivative at x and the data at y use SE kernel
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f
!!$ f = SE(x,y)*(y - x)*gr_dx/(gp_el**2)
!!$ return
!!$ end function SE_der_cov_dxy
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!! SE Kernel !!!!!!!!!!!!!!!!!!!
function d2_SE(x, y, eldel) result(f)
implicit none
real, intent(IN) :: x, y, eldel
real :: f, ell
f = SE(x,y,eldel)/(eldel**2)*(((x-y)/eldel)**2-1.)
return
end function d2_SE
function d4_SE(x, y, eldel) result(f)
implicit none
real, intent(IN) :: x, y, eldel
real :: f, ell, xmy
xmy = (x - y)/eldel
f = SE(x,y,eldel)/(eldel**4)*( (xmy)**4 - 6.*(xmy)**2 + 3. )
return
end function d4_SE
function int_egrand(x, t, eldel) result(f)
implicit none
real, intent(IN) :: x, t, eldel
real :: f
!eldel = gp_eldel*SQRT(2.)
!rt2 = SQRT(2.)
f = ERF( (x + .5 - t)/(rt2*eldel)) - ERF( (x - .5 - t)/(rt2*eldel) )
!f = ERF( (x + .5*gr_dx - t)/eldel) - ERF( (x - .5*gr_dx - t)/eldel )
end function int_egrand
function quad_exact(x1,x2,eldel) result(Integ)
!exact quadrature, only good for SE kernel
real, intent(IN) :: x1, x2, eldel
real :: Integ, yxp, yxn, yxm
!eldel = gp_eldel*SQRT(2.)
yxp = (x1 - x2 + 1.)/(rt2*eldel)
yxn = (x1 -x2)/(rt2*eldel)
yxm = (x1 - x2 -1.)/(rt2*eldel)
Integ = SQRT(PI)*(eldel)**2 *( yxp*ERF(yxp) + yxm*ERF(yxm) &
- 2.*( yxn*ERF(yxn) + 1./SQRT(PI) *EXP(-yxn**2) ) &
+ 1./SQRT(PI) * ( EXP(-yxp**2) + exp(-yxm**2) ) )
return
end function quad_exact
function quad_mid(x1, x2) result(Integ)
!midpoint quadrature rule
implicit none
real, intent(IN) :: x1, x2
real :: Integ, eldel
eldel = gp_eldel*SQRT(2.)
!Integ = 0.5*SQRT(PI)*eldel*int_egrand(x2, x1, eldel)
return
end function quad_mid
function quad_simps(x1,x2) result(Integ)
!simpson's quadrature rule
implicit none
real, intent(IN) :: x1, x2
real :: Integ, eldel
eldel = gp_eldel*SQRT(2.)
!Integ = 0.5*SQRT(PI)*eldel*( int_egrand(x2,x1-0.5) + 4.*int_egrand(x2,x1) + int_egrand(x2,x1+0.5) )/6.
return
end function quad_simps
function quad_trap(x1,x2) result(Integ)
!trapezoidal quadrature rule
implicit none
real, intent(IN) :: x1, x2
real :: Integ, eldel
eldel = gp_eldel*SQRT(2.)
!Integ = 0.25*SQRT(PI)*eldel*( int_egrand(x2,x1-0.5) + int_egrand(x2,x1+0.5) )
return
end function quad_trap
function int_SEcov(x1, x2, eldel) result(Integ)
!integrates the covariance between cells centered at x1 & x2 in units of x/delta (see eq 15)
!cell ranges from x-1/2 to x+1/2
implicit none
real, intent(IN) :: x1, x2, eldel
real :: Integ, dN, t, fa, fb, yxp, yxn, yxm
integer :: i,N
Integ = 0.
if (gp_quad == 'exact') then
Integ = quad_exact(x1,x2,eldel)
elseif (gp_quad == 'midpt') then
Integ = quad_mid(x1,x2)
elseif (gp_quad == 'trap') then
Integ = quad_trap(x1,x2)
elseif (gp_quad == 'simpson') then
Integ = quad_simps(x1,x2)
end if
return
end function int_SEcov
function cross_cor(x,eldel) result(T)
!returns the cross-correlation between the left and right states and the cell centered at x
!see eq 24
implicit none
real, intent(IN) :: x, eldel
real, dimension(2) :: T
!eldel = gp_eldel*SQRT(2.)
!gp_eldel = gp_el/gr_dx
T(1) = int_egrand(x, -.5, eldel)
T(2) = int_egrand(x, 0.5, eldel)
T = T*.5*eldel*SQRT(PI)
end function cross_cor
!!$ !!!!!!!!!!!!!!!! Matern Kernel!!!!!!!!!!!!!!!!!!!!
!!$
!!$ function matern(x,y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f
!!$
!!$ f = 0.
!!$
!!$ if (gp_matern_nu == 0.5) then
!!$ f = mat_1h(x,y)
!!$ elseif (gp_matern_nu == 1.5) then
!!$ f = mat_3h(x,y)
!!$ elseif (gp_matern_nu == 2.5) then
!!$ f = mat_5h(x,y)
!!$ elseif (gp_matern_nu == 3.5) then
!!$ f = mat_7h(x,y)
!!$ elseif (gp_matern_nu == 4.5) then
!!$ f = mat_9h(x,y)
!!$ elseif (gp_matern_nu == 5.5) then
!!$ f = mat_11h(x,y)
!!$ end if
!!$ return
!!$ end function matern
!!$
!!$!!!!!! half integer matern kernels!!!!!!!!!!!!!!!!
!!$
!!$
!!$ function mat_1h(x, y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f, r
!!$ r = abs(x-y)
!!$ f = EXP(-r/gp_eldel)
!!$ return
!!$ end function mat_1h
!!$
!!$ function mat_3h(x, y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f, r, rt3li
!!$ r = abs(x-y)
!!$ rt3li = SQRT(3.)/gp_eldel
!!$ f = (1 + rt3li*r)*EXP(-rt3li*r)
!!$ return
!!$ end function mat_3h
!!$
!!$ function mat_5h(x, y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f, r, rt5li
!!$ r = abs(x-y)
!!$ rt5li = SQRT(5.)/gp_eldel
!!$ f = (1 + rt5li*r + 5./3. * (r/gp_eldel)**2)*EXP(-rt5li*r)
!!$ return
!!$ end function mat_5h
!!$
!!$ function mat_7h(x, y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f, r, rt7li
!!$ r = abs(x-y)
!!$ rt7li = SQRT(7.)/gp_eldel
!!$
!!$ f = ( 1 + rt7li*r + 14./5.*(r/gp_eldel)**2 + 7.*SQRT(7.)/15.*(r/gp_eldel)**3 )*EXP(-rt7li*r)
!!$
!!$ return
!!$ end function mat_7h
!!$
!!$ function mat_9h(x, y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f, r, rli
!!$ r = abs(x-y)
!!$ rli = r/gp_eldel
!!$
!!$ f = ( 1. + 3.*rli + 27./7.*rli**2 + 18./7.*rli**3 + 27./35.*rli**4 )*EXP(-3.*rli)
!!$
!!$ return
!!$ end function mat_9h
!!$
!!$ function mat_11h(x,y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f, r, rli
!!$ r = abs(x-y)
!!$ rli = r/gp_eldel
!!$
!!$ f = ( 1. + SQRT(11.)*rli + 44./9.*rli**2 + 11./9.*SQRT(11.)*rli**3 + 121./63.*rli**4 + &
!!$ 121./945.*SQRT(11.)*rli**5 )*EXP(-SQRT(11.)*rli)
!!$
!!$ return
!!$ end function mat_11h
!!$
!!$
!!$
!!$!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!$!!!!!!!!!!!!!!!!!!!! Exponential Kernel !!!!!!!!!!
!!$
!!$ function intg_exp(x, t) result(f)
!!$ !prediction vector for exponential kernel
!!$ implicit none
!!$ real, intent(IN) :: x,t
!!$ real :: f
!!$ f = 0.
!!$ if (x < t) then
!!$ f = gp_eldel*(EXP((x+0.5)/gp_eldel) - EXP((x-0.5)/gp_eldel) )*EXP(-t/gp_eldel)
!!$ elseif (x > t) then
!!$ f = gp_eldel*(EXP(-(x-0.5)/gp_eldel) - EXP(-(x+0.5)/gp_eldel) )*EXP(t/gp_eldel)
!!$ end if
!!$ return
!!$ end function intg_exp
!!$
!!$ function EXP_exact(x1,x2) result(Integ)
!!$ !exact quadrature for exponential kernel
!!$ implicit none
!!$ real, intent(IN) :: x1, x2
!!$ real :: Integ, x, y
!!$ Integ = 0.
!!$ if (x1 .ne. x2) then
!!$ x = MAX(x1, x2)
!!$ y = MIN(x1, x2)
!!$
!!$ Integ = gp_eldel**2 *( EXP(-(x-0.5)/gp_eldel) - EXP(-(x+0.5)/gp_eldel) )*&
!!$ ( EXP((y+0.5)/gp_eldel) - EXP((y-0.5)/gp_eldel) )
!!$ elseif (x1 == x2) then
!!$ x = x1
!!$ y = x2
!!$ Integ = gp_eldel*( &
!!$ 2. + gp_eldel*(&
!!$ EXP( (y-0.5)/gp_eldel)*( EXP(-(x+0.5)/gp_eldel) - EXP(-(x-0.5)/gp_eldel) ) - &
!!$ EXP(-(y+0.5)/gp_eldel)*( EXP( (x+0.5)/gp_eldel) - EXP( (x-0.5)/gp_eldel) ) &
!!$ )&
!!$ )
!!$ end if
!!$ return
!!$ end function EXP_exact
!!$
!!$ function int_EXPcov(x1, x2) result(Integ)
!!$
!!$ implicit none
!!$ real, intent(IN) :: x1, x2
!!$ real :: Integ
!!$ Integ = 0.
!!$ if (gp_quad == 'exact') then
!!$ Integ = EXP_exact(x1,x2)
!!$ end if
!!$ return
!!$ end function int_EXPcov
!!$
!!$ function cross_EXP(x) result(T)
!!$ !cross correlation for the prediction using the exponential kernel
!!$ implicit none
!!$ real, intent(IN) :: x
!!$ real, dimension(2) :: T
!!$
!!$ T(1) = intg_EXP(x, -0.5)
!!$ T(2) = intg_EXP(x, 0.5)
!!$ return
!!$ end function cross_EXP
!!$ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!$!!!!!!!!!!!!!!!! Rational Quad. !!!!!!!!!!!!!!!!!!
!!$
!!$ function RQ(x, y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f, r
!!$ r = abs(x-y)
!!$ f = (1. + r**2/(2.*gp_RQ_alpha*gp_eldel**2))**(-gp_RQ_alpha)
!!$ return
!!$ end function RQ
!!$ !!!!!!!!!!! Neural Network (NN) !!!!!!!!!!!!!!!!!!
!!$
!!$ function NN(x,y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f, sig0, xx, yy, xy
!!$
!!$ sig0 = 1.
!!$ !print *, sig0, x, y
!!$ xx = sig0**2 + x*x/(gp_eldel**2)
!!$ yy = sig0**2 + y*y/(gp_eldel**2)
!!$ xy = sig0**2 + x*y/(gp_eldel**2)
!!$ f = 2./PI * ASIN(2.*xy/SQRT((1.+2.*xx)*(1.+2.*yy)))
!!$ return
!!$ end function NN
!!$
!!$!!!!!!!!!!!! Gibbs Covariance !!!!!!!!!!!!!!!!!!!!
!!$
!!$ function lscale(x) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x
!!$ real :: f, a, r
!!$
!!$ a = 0.8
!!$ r = x - 1.
!!$ f = 1. - 0.6*EXP(-0.5*(r/a)**2)
!!$ f = f*gp_eldel
!!$ return
!!$ end function lscale
!!$
!!$ function gibbs(x, y) result(f)
!!$ implicit none
!!$ real, intent(IN) :: x, y
!!$ real :: f, r, lx, ly
!!$
!!$ lx = lscale(x)
!!$ ly = lscale(y)
!!$ r = abs(x-y)
!!$
!!$ f = exp( -(r**2)/(lx**2+ly**2) )
!!$ f = f*SQRT( (2.*lx*ly)/(lx**2+ly**2) )
!!$
!!$ return
!!$ end function gibbs
end module GP