-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_laplace_inversion.f90
587 lines (435 loc) · 20.3 KB
/
mo_laplace_inversion.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
MODULE mo_laplace_inversion
! This module serves numerical laplace-inversion
! Written Sebastian Mueller, June 2014
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2014 Sebastian Mueller
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
use mo_kind, only: i4, i8, SP, DP
use mo_combinatorics, only: binomcoeffi
use mo_functions, only: factorial
implicit none
PUBLIC :: NLInvSteh ! Laplace back-transformed function by Stehfest algorithm
! ------------------------------------------------------------------
! NAME
! NLInvSteh
! PURPOSE
! Calculates numerically the laplace-inversion of a given function for given time-values (and optionaly space-values)
! that are given in an array
! CALLING SEQUENCE
! out = NLInvStehvec(func, para, [r,] t, n)
! INTENT(IN)
!> REAL(sp/dp) :: func function to be inverted (f([r,]s,para))
!> REAL(sp/dp), DIMENSION(:) :: para Parameters for the given function
!> [REAL(sp/dp), DIMENSION(:) :: r input radius]
!> REAL(sp/dp), DIMENSION(:) :: t input time
!> integer(i4/i8) :: n boundary for the stehfest-algorithm (recommended: 6/8/10/12)
! INTENT(INOUT)
!> None
! INTENT(OUT)
!> None
! INTENT(IN), OPTIONAL
!> None
! INTENT(INOUT), OPTIONAL
!> None
! INTENT(OUT), OPTIONAL
!> None
! RETURNS
!> real(dp) :: NLInvSteh
! RESTRICTIONS
! 1) Stehfest-boundary n must be positiv
! EXAMPLE
! func(s) = 1/(s^2)
! NLInvSteh(func,t,12) ~ t
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
! Written, Sebastian Mueller, June 2014
INTERFACE NLInvSteh
MODULE PROCEDURE NLInvSteh_dp, NLInvSteh_sp, NLInvStehspace_dp, NLInvStehspace_sp, &
NLInvStehvec_dp, NLInvStehvec_sp, NLInvStehvecspace_dp, NLInvStehvecspace_sp
END INTERFACE NLInvSteh
! ------------------------------------------------------------------
PRIVATE
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
! c_n=csteh(n, m) computed from the Stehfestalg. m=N/2 n=n k=k (the values are all written in an array)
function csteh_dp(m)
implicit none
integer(i4), intent(in) :: m
real(dp), dimension(2*m) :: csteh_dp
! local variables
integer(i4) :: k, n
!Check if the given boundary is not to big
if (m>=11_i4) stop 'The Stehfest-boundary must be less than 22 for double-precision.'
csteh_dp = 0.0_dp
do n=1_i4, 2*m
do k=FLOOR((real(n,dp)+1.0_dp)/2.0_dp), min(n,m)
csteh_dp(n) = csteh_dp(n) + real(k,dp)**real(m+1_i4,dp)*real(binomcoeffi(int(2_i4*k,i8),int(k,i8)),dp)/&
real(factorial(int(m-k,i8))*factorial(int(n-k,i8))*factorial(int(2_i4*k-n,i8)),dp)
! csteh_dp(n) = csteh_dp(n) + real(k**(m+1_i4)*binomcoeffi(2_i4*k,k),dp)/&
! real(factorial(m-k)*factorial(n-k)*factorial(2_i4*k-n),dp)
end do
csteh_dp(n) = (-1.0_dp)**(n+m)*csteh_dp(n)
!write(*,*) "csteh_dp(",n,")= ", csteh_dp(n)
end do
end function csteh_dp
function csteh_sp(m)
implicit none
integer(i4), intent(in) :: m
real(sp), dimension(2*m) :: csteh_sp
! local variables
integer(i4) :: k, n
!Check if the given boundary is not to big
if (m>=7_i4) stop 'The Stehfest-boundary must be less than 14 for single-precision.'
csteh_sp = 0.0_sp
do n=1_i4, 2*m
do k=FLOOR((real(n,sp)+1.0_sp)/2.0_sp), min(n,m)
csteh_sp(n) = csteh_sp(n) + real(k,sp)**real(m+1_i4,sp)*real(binomcoeffi(int(2_i4*k,i8),int(k,i8)),sp)/&
real(factorial(int(m-k,i8))*factorial(int(n-k,i8))*factorial(int(2_i4*k-n,i8)),sp)
end do
csteh_sp(n) = (-1.0_sp)**(n+m)*csteh_sp(n)
!write(*,*) "csteh_sp(",n,")= ", csteh_sp(n)
end do
end function csteh_sp
!Stehfestalg for only-time functions with array-input
function NLInvStehvec_dp( func, para, t, n)
!Interface for the given function
INTERFACE
function func(s, para)
use mo_kind, only: dp
implicit none
real(dp), dimension(:), intent(in) :: s
real(dp), dimension(:), intent(in) :: para
real(dp), dimension(size(s)) :: func
end function func
END INTERFACE
real(dp), dimension(:), intent(in) :: para
real(dp), dimension(:), intent(in) :: t
integer(i4), intent(in) :: n
real(dp), dimension(size(t)) :: NLInvStehvec_dp
!intern variables
integer(i4) :: m,i
real(dp), dimension(size(t)) :: a
real(dp), dimension(2*Ceiling(real(n,dp)/2.0_dp)) :: csteh
real(dp), dimension(size(t), 2*Ceiling(real(n,dp)/2.0_dp)) :: funcpoints
!Check if the given boundary is a positiv number
if (n<=0_i4) stop 'The Stehfest-boundary n must be positiv.'
!Check if there are time-values
if (size(t)==0_i4) stop 'There are no given time-values.'
!If n is odd take the next bigger even number N=2m
m = Ceiling(real(n,dp)/2.0_dp)
!Define the coefficients c_n
csteh = csteh_dp(m)
!Evaluate all necessary function points to call the function only one time
!set all necessary s-values into a matrix
do i=1_i4, 2*m
funcpoints(:,i) = real(i,dp)/t*log(2.0_dp)
end do
!get the function-values for all needed s-values
funcpoints = reshape(func(pack(funcpoints,.true.), para), (/size(t),2*m/))
!Calculate the Lapalce-inversion
a = log(2.0_dp)/t
do i = 1, size(t)
NLInvStehvec_dp(i) = dot_product(csteh, funcpoints(i,:))
end do
NLInvStehvec_dp = a*NLInvStehvec_dp
end function NLInvStehvec_dp
!-----------------------------------------------------------------
function NLInvStehvec_sp( func, para, t, n)
!Interface for the given function
INTERFACE
function func(s, para)
use mo_kind, only: sp
implicit none
real(sp), dimension(:), intent(in) :: s
real(sp), dimension(:), intent(in) :: para
real(sp), dimension(size(s)) :: func
end function func
END INTERFACE
real(sp), dimension(:), intent(in) :: para
real(sp), dimension(:), intent(in) :: t
integer(i4), intent(in) :: n
real(sp), dimension(size(t)) :: NLInvStehvec_sp
!intern variables
integer(i4) :: m,i
real(sp), dimension(size(t)) :: a
real(sp), dimension(2*Ceiling(real(n,sp)/2.0_sp)) :: csteh
real(sp), dimension(size(t),2*Ceiling(real(n,sp)/2.0_sp)) :: funcpoints
!Check if the given boundary is a positiv number
if (n<=0_i4) stop 'The Stehfest-boundary n must be positiv.'
!Check if there are time-values
if (size(t)==0_i4) stop 'There are no given time-values.'
!If n is odd take the next bigger even number N=2m
m = Ceiling(real(n,sp)/2.0_sp)
!Define the coefficients c_n
csteh = csteh_sp(m)
!Evaluate all necessary function points to call the function only one time
!set all necessary s-values into a matrix
do i=1_i4, 2*m
funcpoints(:,i) = real(i,sp)/t*log(2.0_sp)
end do
!get the function-values for all needed s-values
funcpoints = reshape(func(pack(funcpoints,.true.), para), (/size(t),2*m/))
!Calculate the Lapalce-inversion
a = log(2.0_sp)/t
do i = 1, size(t)
NLInvStehvec_sp(i) = dot_product(csteh, funcpoints(i,:))
end do
NLInvStehvec_sp = a*NLInvStehvec_sp
end function NLInvStehvec_sp
!------------------------------------------------------------------
!Stehfestalg for space and time functions with array-input
function NLInvStehvecspace_dp( func, para, r, t, n)
!Interface for the given function with additional radial-values r
INTERFACE
function func(r, s, para)
use mo_kind, only: dp
implicit none
real(dp), dimension(:), intent(in) :: r
real(dp), dimension(:), intent(in) :: s
real(dp), dimension(:), intent(in) :: para
real(dp), dimension(size(r),size(s)) :: func
end function func
END INTERFACE
real(dp), dimension(:), intent(in) :: para
real(dp), dimension(:), intent(in) :: r
real(dp), dimension(:), intent(in) :: t
integer(i4), intent(in) :: n
real(dp), dimension(size(r),size(t)) :: NLInvStehvecspace_dp
!intern variables
integer(i4) :: m,i,j
real(dp), dimension(size(t)) :: a
real(dp), dimension(2*Ceiling(real(n,dp)/2.0_dp)) :: csteh
real(dp), dimension(size(t), 2*Ceiling(real(n,dp)/2.0_dp)) :: timepoints
real(dp), dimension(size(r), size(t), 2*Ceiling(real(n,dp)/2.0_dp)) :: funcpoints
!Check if the given boundary is a positiv number
if (n<=0_i4) stop 'The Stehfest-boundary n must be positiv.'
!Check if there are time-values and if they are valid
if (size(t)==0_i4) stop 'There are no given time-values.'
!Check if there are space-values and if they are valid
if (size(r)==0_i4) stop 'There are no given space-values.'
!If n is odd take the next bigger even number N=2m
m = Ceiling(real(n,dp)/2.0_dp)
!Define the coefficients c_n
csteh = csteh_dp(m)
!Evaluate all necessary function points to call the function only one time
!set all necessary s-values into a matrix
do i=1_i4, 2*m
timepoints(:,i) = real(i,dp)/t*log(2.0_dp)
end do
!get the function-values for all needed s-values
funcpoints = reshape(func(r,pack(timepoints,.true.), para), (/size(r),size(t),2*m/))
!Calculate the Lapalce-inversion
a = log(2.0_dp)/t
do i = 1, size(r)
do j = 1, size(t)
NLInvStehvecspace_dp(i,j) = dot_product(csteh, funcpoints(i,j,:))
end do
NLInvStehvecspace_dp(i,:) = a*NLInvStehvecspace_dp(i,:)
end do
end function NLInvStehvecspace_dp
function NLInvStehvecspace_sp( func, para, r, t, n)
!Interface for the given function with additional radial-values r
INTERFACE
function func(r, s, para)
use mo_kind, only: sp
implicit none
real(sp), dimension(:), intent(in) :: r
real(sp), dimension(:), intent(in) :: s
real(sp), dimension(:), intent(in) :: para
real(sp), dimension(size(r),size(s)) :: func
end function func
END INTERFACE
real(sp), dimension(:), intent(in) :: para
real(sp), dimension(:), intent(in) :: r
real(sp), dimension(:), intent(in) :: t
integer(i4), intent(in) :: n
real(sp), dimension(size(r),size(t)) :: NLInvStehvecspace_sp
!intern variables
integer(i4) :: m,i,j
real(sp), dimension(size(t)) :: a
real(sp), dimension(2*Ceiling(real(n,sp)/2.0_sp)) :: csteh
real(sp), dimension(size(t), 2*Ceiling(real(n,sp)/2.0_sp)) :: timepoints
real(sp), dimension(size(r), size(t), 2*Ceiling(real(n,sp)/2.0_sp)) :: funcpoints
!Check if the given boundary is a positiv number
if (n<=0_i4) stop 'The Stehfest-boundary n must be positiv.'
!Check if there are time-values and if they are valid
if (size(t)==0_i4) stop 'There are no given time-values.'
!Check if there are space-values and if they are valid
if (size(r)==0_i4) stop 'There are no given space-values.'
!If n is odd take the next bigger even number N=2m
m = Ceiling(real(n,sp)/2.0_sp)
!Define the coefficients c_n
csteh = csteh_sp(m)
!Evaluate all necessary time points to call the function only ONE time
!set all necessary s-values into a matrix
do i=1_i4, 2*m
timepoints(:,i) = real(i,sp)/t*log(2.0_sp)
end do
!get the function-values for all needed s-values
funcpoints = reshape(func(r,pack(timepoints,.true.), para), (/size(r),size(t),2*m/))
!Calculate the Lapalce-inversion
a = log(2.0_sp)/t
do i = 1, size(r)
do j = 1, size(t)
NLInvStehvecspace_sp(i,j) = dot_product(csteh, funcpoints(i,j,:))
end do
NLInvStehvecspace_sp(i,:) = a*NLInvStehvecspace_sp(i,:)
end do
end function NLInvStehvecspace_sp
!stehfest for single input value
function NLInvSteh_dp( func, para, t, n)
!Interface for the given function
INTERFACE
function func(s, para)
use mo_kind, only: dp
implicit none
real(dp), intent(in) :: s
real(dp), dimension(:), intent(in) :: para
real(dp) :: func
end function func
END INTERFACE
real(dp), dimension(:), intent(in) :: para
real(dp), intent(in) :: t
integer(i4), intent(in) :: n
real(dp) :: NLInvSteh_dp
!intern variables
integer(i4) :: m,i
real(dp), dimension(2*Ceiling(real(n,dp)/2.0_dp)) :: csteh, funcpoints
!Check if the given boundary is a positiv number
if (n<=0_i4) stop 'The Stehfest-boundary n must be positiv.'
!If n is odd take the next bigger even number N=2m
m = Ceiling(real(n,dp)/2.0_dp)
!Define the coefficients c_n
csteh = csteh_dp(m)
!Evaluate all necessary function points
!get the function-values for all needed s-values
do i=1_i4, 2*m
funcpoints(i) = func(real(i,dp)/t*log(2.0_dp), para)
end do
!Calculate the Lapalce-inversion
NLInvSteh_dp = (log(2.0_dp)/t)*dot_product(csteh, funcpoints)
end function NLInvSteh_dp
!------------------------------------------------------------------
!Stehfestalg for only-time functions with single-input
function NLInvSteh_sp( func, para, t, n)
!Interface for the given function
INTERFACE
function func(s, para)
use mo_kind, only: sp
implicit none
real(sp), intent(in) :: s
real(sp), dimension(:), intent(in) :: para
real(sp) :: func
end function func
END INTERFACE
real(sp), dimension(:), intent(in) :: para
real(sp), intent(in) :: t
integer(i4), intent(in) :: n
real(sp) :: NLInvSteh_sp
!intern variables
integer(i4) :: m,i
real(sp), dimension(2*Ceiling(real(n,sp)/2.0_sp)) :: csteh, funcpoints
!Check if the given boundary is a positiv number
if (n<=0_i4) stop 'The Stehfest-boundary n must be positiv.'
!If n is odd take the next bigger even number N=2m
m = Ceiling(real(n,sp)/2.0_sp)
!Define the coefficients c_n
csteh = csteh_sp(m)
!Evaluate all necessary function points
!get the function-values for all needed s-values
do i=1_i4, 2*m
funcpoints(i) = func(real(i,sp)/t*log(2.0_sp), para)
end do
!Calculate the Lapalce-inversion
NLInvSteh_sp = (log(2.0_sp)/t)*dot_product(csteh, funcpoints)
end function NLInvSteh_sp
!------------------------------------------------------------------
!Stehfestalg for space and time functions with single-input
function NLInvStehspace_dp( func, para, r, t, n)
!Interface for the given function with additional radial-values r
INTERFACE
function func(r, s, para)
use mo_kind, only: dp
implicit none
real(dp), intent(in) :: r
real(dp), intent(in) :: s
real(dp), dimension(:), intent(in) :: para
real(dp) :: func
end function func
END INTERFACE
real(dp), dimension(:), intent(in) :: para
real(dp), intent(in) :: r,t
integer(i4), intent(in) :: n
real(dp) :: NLInvStehspace_dp
!intern variables
integer(i4) :: m,i
real(dp), dimension(2*Ceiling(real(n,dp)/2.0_dp)) :: csteh, funcpoints
!Check if the given boundary is a positiv number
if (n<=0_i4) stop 'The Stehfest-boundary n must be positiv.'
!If n is odd take the next bigger even number N=2m
m = Ceiling(real(n,dp)/2.0_dp)
!Define the coefficients c_n
csteh = csteh_dp(m)
!get the function-values for all needed s-values
do i=1_i4, 2*m
funcpoints(i) = func(r, real(i,dp)/t*log(2.0_dp), para)
end do
!Calculate the Lapalce-inversion
NLInvStehspace_dp = (log(2.0_dp)/t)*dot_product(csteh, funcpoints)
end function NLInvStehspace_dp
function NLInvStehspace_sp( func, para, r, t, n)
!Interface for the given function with additional radial-values r
INTERFACE
function func(r, s, para)
use mo_kind, only: sp
implicit none
real(sp), intent(in) :: r
real(sp), intent(in) :: s
real(sp), dimension(:), intent(in) :: para
real(sp) :: func
end function func
END INTERFACE
real(sp), dimension(:), intent(in) :: para
real(sp), intent(in) :: r,t
integer(i4), intent(in) :: n
real(sp) :: NLInvStehspace_sp
!intern variables
integer(i4) :: m,i
real(sp), dimension(2*Ceiling(real(n,sp)/2.0_sp)) :: csteh, funcpoints
!Check if the given boundary is a positiv number
if (n<=0_i4) stop 'The Stehfest-boundary n must be positiv.'
!If n is odd take the next bigger even number N=2m
m = Ceiling(real(n,sp)/2.0_sp)
!Define the coefficients c_n
csteh = csteh_sp(m)
!get the function-values for all needed s-values
do i=1_i4, 2*m
funcpoints(i) = func(r, real(i,sp)/t*log(2.0_sp), para)
end do
!Calculate the Lapalce-inversion
NLInvStehspace_sp = (log(2.0_sp)/t)*dot_product(csteh, funcpoints)
end function NLInvStehspace_sp
end module mo_laplace_inversion