39 subroutine lininter(n,ref_array,ref_value,array,res,flin)
42 integer,
intent(in) :: n
43 real(
r_kind),
dimension(n),
intent(in) :: ref_array
44 real(
r_kind),
dimension(n),
intent(in) :: array
45 real(
r_kind),
intent(in) :: ref_value
46 real(
r_kind),
intent(out) :: res
47 logical,
intent(in),
optional :: flin
54 if (.not.
present(flin))
then
62 if (ref_value .le. ref_array(i))
exit
68 else if (ii.gt.n)
then
71 grad = (ref_value-ref_array(ii-1))/(ref_array(ii)-ref_array(ii-1))
72 res = array(ii-1) + grad*(array(ii)-array(ii-1))
101 integer,
intent(in) :: n
102 real(
r_kind),
dimension(n),
intent(in) :: xp
103 real(
r_kind),
intent(in) :: xb
104 real(
r_kind),
dimension(n),
intent(in) :: yp
105 real(
r_kind),
intent(out) :: res
106 logical,
intent(in),
optional :: flin
107 integer,
intent(in),
optional :: itype
109 integer :: interp_type
113 if (.not.
present(itype))
then
119 if (.not.
present(flin))
then
126 if (interp_type .eq. itype_linear)
then
127 call lininter(n,xp,xb,yp,res,linear)
128 else if (interp_type .eq. itype_cubic)
then
129 call cubinter(n,xp,xb,yp,res,linear)
130 else if (interp_type .eq. itype_akima)
then
132 else if (interp_type .eq. itype_makima)
then
134 else if (interp_type .eq. itype_pchip)
then
139 ". Try to change interp_mode to a valid value.",&
163 integer,
intent(in) :: n
164 real(
r_kind),
dimension(n),
intent(in) :: xp
165 real(
r_kind),
intent(in) :: xb
166 real(
r_kind),
dimension(n),
intent(in) :: yp
167 real(
r_kind),
intent(out) :: res
168 logical,
intent(in),
optional :: flin
171 real(
r_kind) :: x,x1,x2,x3,x4,x12,x13,x14,x23,x24,x34
174 if(
present(flin))
then
182 if (xb .le. xp(ii))
exit
186 else if (x.ge.xp(n))
then
188 else if(ii.le.2)
then
192 res= (yp(1)*x2 - yp(2)*x1)/x12
193 else if(ii.eq.n)
then
197 res= (yp(n-1)*x2 - yp(n)*x1)/x12
199 k= min(max(ii-2,2),n-3)
213 res= yp(k) *x2*x3*x4/(x12*x13*x14) &
214 - yp(k+1)*x1*x3*x4/(x12*x23*x24) &
215 + yp(k+2)*x1*x2*x4/(x13*x23*x34) &
216 - yp(k+3)*x1*x2*x3/(x14*x24*x34)
244 integer,
intent(in) :: n
245 real(
r_kind),
dimension(n),
intent(in) :: xp
246 real(
r_kind),
intent(in) :: xb
247 real(
r_kind),
dimension(n),
intent(in) :: yp
248 real(
r_kind),
intent(out) :: res
249 logical,
intent(in),
optional :: flin
252 real(
r_kind) :: a, b, c, d
253 real(
r_kind) :: hi, him1, hip1
254 real(
r_kind) :: si, sim1, sip1
256 real(
r_kind) :: yi_prime, yip1_prime
261 if(
present(flin))
then
269 if (xb .le. xp(ii))
exit
274 if (xb.le.xp(1))
then
277 else if (xb.ge.xp(n))
then
280 else if(ii.lt.2)
then
284 res= (yp(ii)*x2 - yp(ii+1)*x1)/(x2-x1)
286 else if(ii.gt.n-2)
then
290 res= (yp(ii)*x2 - yp(ii+1)*x1)/(x2-x1)
295 si = (yp(ii+1)-yp(ii))/hi
297 him1 = xp(ii)-xp(ii-1)
298 sim1 = (yp(ii)-yp(ii-1))/him1
300 hip1 = xp(ii+2)-xp(ii+1)
301 sip1 = (yp(ii+2)-yp(ii+1))/hip1
303 pi = (hi*sim1+him1*si)/(hi+him1)
304 pip1 = (hip1*si+hi*sip1)/(hip1+hi)
307 if (si*sim1 .le. 0)
then
309 elseif ((dabs(
pi)>2*dabs(sim1)) .or. (dabs(
pi)>2*dabs(si)))
then
310 yi_prime = 2*sign(1.d0,si)*min(dabs(sim1),dabs(si))
315 if (sip1*si .le. 0)
then
317 elseif ((dabs(pip1)>2*dabs(si)) .or. (dabs(pip1)>2*dabs(sip1)))
then
318 yip1_prime = 2*sign(1.d0,si)*min(dabs(sip1),dabs(si))
323 a = (yi_prime+yip1_prime-2*si)/hi**2
324 b = (3*si-2*yi_prime-yip1_prime)/hi
330 res = a*delt**3 + b*delt**2 + c*delt + d
365 integer,
intent(in) :: n
366 real(
r_kind),
dimension(n),
intent(in) :: xp
367 real(
r_kind),
intent(in) :: xb
368 real(
r_kind),
dimension(n),
intent(in) :: yp
369 real(
r_kind),
intent(out) :: res
370 logical,
intent(in),
optional :: flin
372 real(
r_kind) :: xval(6), yval(6), mval(5), sval(2)
374 real(
r_kind) :: a, b, c, d
379 if(
present(flin))
then
393 if (xb .le. xp(ii))
exit
398 if (xb.le.xp(1))
then
401 else if (xb.ge.xp(n))
then
404 else if(ii.lt.3)
then
408 res= (yp(ii)*x2 - yp(ii+1)*x1)/(x2-x1)
410 else if(ii.gt.n-3)
then
414 res= (yp(ii)*x2 - yp(ii+1)*x1)/(x2-x1)
419 xval(1:6) = xp(ii-2:ii+3)
420 yval(1:6) = yp(ii-2:ii+3)
422 mval(1:5) = (yval(2:6) - yval(1:5)) / (xval(2:6) - xval(1:5))
426 sval(1) = ((dabs(mval(i+1)-mval(i))*mval(i-1) + dabs(mval(i-1)-mval(i-2))*mval(i)))
427 denom = (dabs(mval(i+1)-mval(i)) + dabs(mval(i-1)-mval(i-2)))
429 if (denom .gt. 1e-8)
then
430 sval(1) = sval(1)/denom
432 sval(1) = (mval(i-1)+mval(i))/2d0
434 sval(2) = ((dabs(mval(i+2)-mval(i+1))*mval(i) + dabs(mval(i)-mval(i-1))*mval(i+1)))
435 denom = (dabs(mval(i+2)-mval(i+1)) + dabs(mval(i)-mval(i-1)))
436 if (denom .gt. 1e-8)
then
437 sval(2) = sval(2)/denom
439 sval(2) = (mval(i)+mval(i+1))/2d0
444 c = (3.0d0 * mval(i) - 2.0d0 * sval(1) - sval(2)) / (xval(i+1) - xval(i))
445 d = (sval(1) + sval(2) - 2.0d0 * mval(i)) / (xval(i+1) - xval(i))**2
447 res = a + b * (xb - xval(i)) + c * (xb - xval(i))**2 + d * (xb - xval(i))**3
482 integer,
intent(in) :: n
483 real(
r_kind),
dimension(n),
intent(in) :: xp
484 real(
r_kind),
intent(in) :: xb
485 real(
r_kind),
dimension(n),
intent(in) :: yp
486 real(
r_kind),
intent(out) :: res
487 logical,
intent(in),
optional :: flin
489 real(
r_kind) :: xval(6), yval(6), mval(5), sval(2)
491 real(
r_kind) :: a, b, c, d
496 if(
present(flin))
then
510 if (xb .le. xp(ii))
exit
515 if (xb.le.xp(1))
then
518 else if (xb.ge.xp(n))
then
521 else if(ii.lt.3)
then
525 res= (yp(ii)*x2 - yp(ii+1)*x1)/(x2-x1)
527 else if(ii.gt.n-3)
then
531 res= (yp(ii)*x2 - yp(ii+1)*x1)/(x2-x1)
536 xval(1:6) = xp(ii-2:ii+3)
537 yval(1:6) = yp(ii-2:ii+3)
539 mval(1:5) = (yval(2:6) - yval(1:5)) / (xval(2:6) - xval(1:5))
543 sval(1) = ((dabs(mval(i+1)-mval(i)) + dabs(mval(i+1)+mval(i))/2d0)*mval(i-1) + &
544 (dabs(mval(i-1)-mval(i-2)) + dabs(mval(i-1)+mval(i-2))/2d0)*mval(i))
546 denom = ((dabs(mval(i+1)-mval(i)) + dabs(mval(i+1)+mval(i))/2d0) + &
547 (dabs(mval(i-1)-mval(i-2)) + dabs(mval(i-1)+mval(i-2))/2d0))
549 if (denom .gt. 1e-8)
then
550 sval(1) = sval(1)/denom
552 sval(1) = (mval(i-1)+mval(i))/2d0
554 sval(2) = (((dabs(mval(i+2)-mval(i+1)) + dabs(mval(i+2)+mval(i+1))/2d0)*mval(i) + &
555 (dabs(mval(i)-mval(i-1)) + dabs(mval(i)+mval(i-1))/2d0) *mval(i+1)))
556 denom = ((dabs(mval(i+2)-mval(i+1)) + dabs(mval(i+2)+mval(i+1))/2d0) + &
557 (dabs(mval(i)-mval(i-1)) + dabs(mval(i)+mval(i-1))/2d0))
558 if (denom .gt. 1e-8)
then
559 sval(2) = sval(2)/denom
561 sval(2) = (mval(i)+mval(i+1))/2d0
566 c = (3.0d0 * mval(i) - 2.0d0 * sval(1) - sval(2)) / (xval(i+1) - xval(i))
567 d = (sval(1) + sval(2) - 2.0d0 * mval(i)) / (xval(i+1) - xval(i))**2
569 res = a + b * (xb - xval(i)) + c * (xb - xval(i))**2 + d * (xb - xval(i))**3
588 integer,
intent(in) :: x_dim, y_dim
589 real(
r_kind),
intent(in) :: xval,yval
590 real(
r_kind),
dimension(x_dim),
intent(in) :: x
591 real(
r_kind),
dimension(y_dim),
intent(in) :: y
592 integer,
dimension(2,2),
intent(out) :: indices
598 if (xval .lt. x(i))
exit
605 if (yval .lt. y(i))
exit
624 integer,
intent(in) :: x_dim, y_dim
625 real(
r_kind),
dimension(x_dim, y_dim),
intent(in) :: f
626 real(
r_kind),
dimension(x_dim),
intent(in) :: x
627 real(
r_kind),
dimension(y_dim),
intent(in) :: y
628 real(
r_kind),
dimension(x_dim,y_dim),
intent(inout) :: dfx
629 real(
r_kind),
dimension(x_dim,y_dim),
intent(inout) :: dfy
630 real(
r_kind),
dimension(x_dim,y_dim),
intent(inout) :: dfxy
639 if ((i .gt. 1) .and. (i .lt. x_dim) )
then
640 h1 = abs(x(i) -x(i-1))
641 h2 = abs(x(i+1)-x(i))
642 dfx(i,j) = h1**2*f(i+1,j)+(h2**2-h1**2)*f(i,j)-h2**2*f(i-1,j)
643 dfx(i,j) = dfx(i,j) / (h1*h2*(h1+h2))
644 elseif (i .eq. 1)
then
645 h2 = abs(x(i+1)-x(i))
646 dfx(i,j) = (f(i+1,j)-f(i,j)) / (h2)
647 elseif (i .eq. x_dim)
then
648 h1 = abs(x(i)-x(i-1))
649 dfx(i,j) = (f(i,j)-f(i-1,j)) / (h1)
653 if ((j .gt.1) .and. (j .lt. y_dim))
then
654 h1 = abs(y(j) -y(j-1))
655 h2 = abs(y(j+1)-y(j))
656 dfy(i,j) = h1**2*f(i,j+1)+(h2**2-h1**2)*f(i,j)-h2**2*f(i,j-1)
657 dfy(i,j) = dfy(i,j) / (h1*h2*(h1+h2))
658 elseif (j .eq. 1)
then
659 h2 = abs(y(j+1)-y(j))
660 dfy(i,j) = (f(i,j+1)-f(i,j)) / (h2)
661 elseif (j .eq. y_dim)
then
662 h1 = abs(y(j)-y(j-1))
663 dfy(i,j) = (f(i,j)-f(i,j-1)) / (h1)
671 if ((j .gt. 1) .and. (j .lt. y_dim) )
then
672 h1 = abs(y(j) -y(j-1))
673 h2 = abs(y(j+1)-y(j))
674 dfxy(i,j) = h1**2*dfx(i,j+1)+(h2**2-h1**2)*dfx(i,j)-h2**2*dfx(i,j-1)
675 dfxy(i,j) = dfxy(i,j) / (h1*h2*(h1+h2))
676 elseif (j .eq. 1)
then
677 h2 = abs(y(j+1)-y(j))
678 dfxy(i,j) = (dfx(i,j+1)-dfx(i,j)) / (h2)
679 elseif (j .eq. y_dim)
then
680 h1 = abs(y(j)-y(j-1))
681 dfxy(i,j) = (dfx(i,j)-dfx(i,j-1)) / (h1)
698 function lininter_2d(xin,yin,x,y,f,x_dim,y_dim,indice,extrapolation)
result (wr)
701 integer,
intent(in) :: x_dim, y_dim
702 real(
r_kind),
intent(in) :: xin,yin
703 real(
r_kind),
dimension(x_dim),
intent(in) :: x
704 real(
r_kind),
dimension(y_dim),
intent(in) :: y
705 real(
r_kind),
dimension(x_dim,y_dim),
intent(in) :: f
706 integer,
optional,
intent(in) :: extrapolation
707 integer,
dimension(2,2),
optional,
intent(in) :: indice
710 integer,
dimension(2,2) :: ind
712 real(
r_kind) :: x1, x2, y1, y2
714 real(
r_kind),
dimension(2,2) :: weights
720 if (.not.
present(indice))
then
723 ind(:,:) = indice(:,:)
727 if (.not.
present(extrapolation)) extr=1
728 if (
present(extrapolation)) extr=extrapolation
731 if ((xval .lt. x(1)) .or. (yval .lt. y(1)))
then
735 "inter_module",270003)
741 "inter_module",270004)
743 elseif ((xval .gt. x(x_dim)) .or. (yval .gt. y(y_dim)))
then
747 "inter_module",270003)
749 xval= min(x(x_dim),xval)
750 yval= min(y(y_dim),yval)
753 "inter_module",270004)
764 weights(1,1) = ((x2-xval)*(y2-yval))/((x2-x1)*(y2-y1))
765 weights(1,2) = ((x2-xval)*(yval-y1))/((x2-x1)*(y2-y1))
766 weights(2,1) = ((xval-x1)*(y2-yval))/((x2-x1)*(y2-y1))
767 weights(2,2) = ((xval-x1)*(yval-y1))/((x2-x1)*(y2-y1))
770 wr = weights(1,1)*f(ind(1,1),ind(2,1)) + &
771 weights(1,2)*f(ind(1,1),ind(2,2)) + &
772 weights(2,1)*f(ind(1,2),ind(2,1)) + &
773 weights(2,2)*f(ind(1,2),ind(2,2))
794 function cubinter_2d(xin,yin,x,y,f,dfx,dfy,dfxy,x_dim,y_dim,indice,extrapolation)
result (wr)
797 integer,
intent(in) :: x_dim, y_dim
798 real(
r_kind),
intent(in) :: xin,yin
799 real(
r_kind),
dimension(x_dim),
intent(in) :: x
800 real(
r_kind),
dimension(y_dim),
intent(in) :: y
801 real(
r_kind),
dimension(x_dim,y_dim),
intent(in) :: f,dfx,dfy,dfxy
802 integer,
optional,
intent(in) :: extrapolation
803 integer,
dimension(2,2),
optional,
intent(in) :: indice
807 real(
r_kind),
dimension(16,16) :: a_inv
808 real(
r_kind),
dimension(16) :: vec,alphas
809 integer,
dimension(2,2) :: ind
810 real(
r_kind),
dimension(4,4) :: alphas_r
811 real(
r_kind) :: x1, x2, y1, y2
820 if (.not.
present(indice))
then
823 ind(:,:) = indice(:,:)
827 if (.not.
present(extrapolation)) extr=2
828 if (
present(extrapolation)) extr=extrapolation
831 if ((xval .lt. x(1)) .or. (yval .lt. y(1)))
then
835 "inter_module",270003)
841 "inter_module",270004)
843 elseif ((xval .gt. x(x_dim)) .or. (yval .gt. y(y_dim)))
then
847 "inter_module",270003)
849 xval= min(x(x_dim),xval)
850 yval= min(y(y_dim),yval)
853 "inter_module",270004)
864 xbar = (xval-x1)/(x2-x1)
865 ybar = (yval-y1)/(y2-y1)
869 a_inv = reshape( (/ 1, 0,-3, 2, 0, 0, 0, 0,-3, 0, 9,-6, 2, 0,-6, 4, &
870 0, 0, 3,-2, 0, 0, 0, 0, 0, 0,-9, 6, 0, 0, 6,-4, &
871 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,-9, 6,-2, 0, 6,-4, &
872 0, 0, 0,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,1)),&
892 (x2-x1)*(y2-y1)*dfxy(ind(1,1),ind(2,2)),(x2-x1)*(y2-y1)*dfxy(ind(1,2),ind(2,2))/)
895 alphas = matmul(a_inv,vec)
897 alphas_r = reshape(alphas, (/4,4/))
903 wr = wr + alphas_r(i,j)*xbar**(i-1)*ybar**(j-1)
934 integer,
intent(in) :: n
935 real(
r_kind),
dimension(n),
intent(in) :: xp
936 real(
r_kind),
intent(in) :: xb
937 real(
r_kind),
dimension(n),
intent(in) :: yp
938 real(
r_kind),
intent(inout) :: res
939 logical,
intent(in),
optional :: flin
940 integer,
intent(in),
optional :: itype
943 integer,
parameter :: maxiter=50
945 real(
r_kind) :: m,y1,y2,xnew,b
946 real(
r_kind),
parameter :: diff=1d-4
947 real(
r_kind),
parameter :: tol =1d-10
950 integer :: interp_type
953 if (.not.
present(itype))
then
959 if (.not.
present(flin))
then
969 inner_loop:
do ii=1,maxiter
971 call interp1d(n,yp,x1,xp,y1,linear,interp_type)
972 call interp1d(n,yp,x2,xp,y2,linear,interp_type)
995 if (abs(x1-xnew) .lt. tol)
then