summaryrefslogtreecommitdiff
path: root/map65/libm65/afc65b.f90
diff options
context:
space:
mode:
Diffstat (limited to 'map65/libm65/afc65b.f90')
-rw-r--r--map65/libm65/afc65b.f90235
1 files changed, 176 insertions, 59 deletions
diff --git a/map65/libm65/afc65b.f90 b/map65/libm65/afc65b.f90
index 53f61ea4..e0ebfbc0 100644
--- a/map65/libm65/afc65b.f90
+++ b/map65/libm65/afc65b.f90
@@ -1,70 +1,187 @@
+module afc65b_mod
+ implicit none
+contains
+
subroutine afc65b(cx,cy,npts,fsample,nflip,ipol,xpol,ndphi,a,ccfbest,dtbest)
+ use fchisq_mod
+ use, intrinsic :: ieee_arithmetic, only: ieee_is_finite
+
+ implicit none
+
+ integer, parameter :: MAX_STEP_TRIES = 200
+ integer, parameter :: MAX_DOWNHILL_STEPS = 200
+ real, parameter :: MIN_STEP = 1.0e-4
+ real, parameter :: OBJECTIVE_REL_TOL = 1.0e-6
+
+ logical, intent(in) :: xpol
+ integer, intent(in) :: npts, nflip, ndphi
+ integer, intent(inout) :: ipol
+ complex, intent(in) :: cx(npts), cy(npts)
+ real, intent(inout) :: a(5)
+ real :: deltaa(5)
+ integer :: iter, j, nterms, step, downhill_steps
+ real, intent(out) :: ccfbest, dtbest
+ real :: chisq1, chisq2, chisqr, chisqr0
+ real :: delta, dtmax
+ real, intent(in) :: fsample
+ real :: ccfmax
+ real :: x0, xlast, xprev, xbest, xnext, candidate
+ real :: flast, fprev, fbest, fnext, fcandidate, curvature, tolerance
+ logical :: bracketed, improved, have_previous
+
+ ! Initial parameter guesses
+ a(1) = 0.0
+ a(2) = 0.0
+ a(3) = 0.0
+ if (ipol < 1 .or. ipol > 4) ipol = 1
+ a(4) = 45.0*(ipol - 1.0)
+
+ deltaa(1) = 2.0
+ deltaa(2) = 2.0
+ deltaa(3) = 2.0
+ deltaa(4) = 22.5
+ deltaa(5) = 0.05
+
+ nterms = 3
+ if (xpol) nterms = 4
+ if (ndphi /= 0) nterms = 3 ! don't fit pol when solving for dphi
+
+ ccfbest = 0.0
+ dtbest = 0.0
+ chisqr0 = 0.0
+ have_previous = .false.
+
+ if (npts < 1 .or. fsample <= 0.0 .or. .not. ieee_is_finite(fsample)) return
+
+ do iter = 1, 3
+ do j = 1, nterms
+ x0 = a(j)
+ chisq1 = fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
+ if (.not. ieee_is_finite(chisq1)) then
+ a(j) = x0
+ cycle
+ endif
+
+ delta = abs(deltaa(j))
+ if (.not. ieee_is_finite(delta) .or. delta < MIN_STEP) delta = MIN_STEP
+ improved = .false.
+ xlast = x0
+ flast = chisq1
+ xnext = xlast
- logical xpol
- complex cx(npts)
- complex cy(npts)
- real a(5),deltaa(5)
-
- a(1)=0.
- a(2)=0.
- a(3)=0.
- a(4)=45.0*(ipol-1.0)
- deltaa(1)=2.0
- deltaa(2)=2.0
- deltaa(3)=2.0
- deltaa(4)=22.5
- deltaa(5)=0.05
- nterms=3
- if(xpol) nterms=4
-
-! Don't fit polarization when solving for dphi
- if(ndphi.ne.0) nterms=3
-
-! Start the iteration
- chisqr=0.
- chisqr0=1.e6
- do iter=1,3 !One iteration is enough?
- do j=1,nterms
- chisq1=fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
- fn=0.
- delta=deltaa(j)
-10 a(j)=a(j)+delta
- chisq2=fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
- if(chisq2.eq.chisq1) go to 10
- if(chisq2.gt.chisq1) then
- delta=-delta !Reverse direction
- a(j)=a(j)+delta
- tmp=chisq1
- chisq1=chisq2
- chisq2=tmp
+ do step = 1, MAX_STEP_TRIES
+ xnext = xlast + delta
+ a(j) = xnext
+ chisq2 = fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
+ if (.not. ieee_is_finite(chisq2)) exit
+ tolerance = OBJECTIVE_REL_TOL * max(1.0, abs(chisq1), abs(chisq2))
+ if (abs(chisq2 - chisq1) > tolerance) then
+ xprev = xlast
+ fprev = flast
+ improved = .true.
+ exit
+ endif
+ xlast = xnext
+ flast = chisq2
+ end do
+
+ if (.not. improved) then
+ a(j) = x0
+ deltaa(j) = max(MIN_STEP, 0.5*delta)
+ cycle
+ endif
+
+ if (chisq2 > chisq1) then
+ delta = -delta
+ xnext = x0 + delta
+ a(j) = xnext
+ chisq2 = fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
+ tolerance = OBJECTIVE_REL_TOL * max(1.0, abs(chisq1), abs(chisq2))
+ if (.not. ieee_is_finite(chisq2) .or. chisq2 >= chisq1 - tolerance) then
+ a(j) = x0
+ deltaa(j) = max(MIN_STEP, 0.5*abs(delta))
+ cycle
+ endif
+ xprev = x0
+ fprev = chisq1
endif
-20 fn=fn+1.0
- a(j)=a(j)+delta
- chisq3=fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
- if(chisq3.lt.chisq2) then
- chisq1=chisq2
- chisq2=chisq3
- go to 20
+
+ xbest = xnext
+ fbest = chisq2
+ downhill_steps = 0
+ bracketed = .false.
+
+ do step = 1, MAX_DOWNHILL_STEPS
+ xnext = xbest + delta
+ a(j) = xnext
+ fnext = fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
+ if (.not. ieee_is_finite(fnext)) then
+ a(j) = xbest
+ exit
+ endif
+
+ tolerance = OBJECTIVE_REL_TOL * max(1.0, abs(fbest), abs(fnext))
+ if (fnext < fbest - tolerance) then
+ xprev = xbest
+ fprev = fbest
+ xbest = xnext
+ fbest = fnext
+ downhill_steps = downhill_steps + 1
+ else
+ a(j) = xbest
+ bracketed = .true.
+ exit
+ endif
+ end do
+
+ if (bracketed) then
+ curvature = fprev - 2.0*fbest + fnext
+ tolerance = OBJECTIVE_REL_TOL * max(1.0, abs(fprev), abs(fbest), abs(fnext))
+ if (curvature > tolerance) then
+ candidate = xbest + 0.5*delta*(fprev - fnext)/curvature
+ if (ieee_is_finite(candidate) .and. candidate > min(xprev, xnext) .and. &
+ candidate < max(xprev, xnext)) then
+ a(j) = candidate
+ fcandidate = fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
+ if (ieee_is_finite(fcandidate)) then
+ tolerance = OBJECTIVE_REL_TOL * max(1.0, abs(fbest), abs(fcandidate))
+ if (fcandidate < fbest - tolerance) then
+ xbest = candidate
+ fbest = fcandidate
+ endif
+ endif
+ endif
+ endif
endif
-! Find minimum of parabola defined by last three points
- delta=delta*(1./(1.+(chisq1-chisq2)/(chisq3-chisq2))+0.5)
- a(j)=a(j)-delta
- deltaa(j)=deltaa(j)*fn/3.
+ a(j) = xbest
+ if (downhill_steps < 3) then
+ deltaa(j) = max(MIN_STEP, abs(delta)*max(1, downhill_steps)/3.0)
+ else
+ deltaa(j) = abs(delta)
+ endif
enddo
- chisqr=fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
- if(chisqr/chisqr0.gt.0.9999) go to 30
- chisqr0=chisqr
+
+ chisqr = fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax)
+ if (.not. ieee_is_finite(chisqr)) exit
+ if (have_previous) then
+ tolerance = OBJECTIVE_REL_TOL * max(1.0, abs(chisqr), abs(chisqr0))
+ if (abs(chisqr - chisqr0) <= tolerance) exit
+ endif
+ chisqr0 = chisqr
+ have_previous = .true.
enddo
-30 ccfbest=ccfmax * (1378.125/fsample)**2
- dtbest=dtmax
+ if (ieee_is_finite(ccfmax)) ccfbest = max(0.0, ccfmax * (1378.125/fsample)**2)
+ if (ieee_is_finite(dtmax)) dtbest = dtmax
+ if (.not. ieee_is_finite(ccfbest)) ccfbest = 0.0
+ if (.not. ieee_is_finite(dtbest)) dtbest = 0.0
- if(a(4).lt.0.0) a(4)=a(4)+180.0
- if(a(4).ge.180.0) a(4)=a(4)-180.0
- if(nint(a(4)).eq.180) a(4)=0.
- ipol=nint(a(4)/45.0) + 1
- if(ipol.gt.4) ipol=ipol-4
+ if (.not. ieee_is_finite(a(4))) a(4) = 0.0
+ a(4) = modulo(a(4), 180.0)
- return
+ ipol = modulo(nint(a(4)/45.0), 4) + 1
+
end subroutine afc65b
+
+end module afc65b_mod