diff options
| author | Christoph Berg <myon@debian.org> | 2026-09-26 23:24:03 +0200 |
|---|---|---|
| committer | Christoph Berg <myon@debian.org> | 2026-09-26 23:24:03 +0200 |
| commit | 059831534c7e884f0a607ed86b582fd00d821731 (patch) | |
| tree | 76bcb67a78f8aa2654fd34c2ad801a62136b0170 /map65/libm65/afc65b.f90 | |
| parent | d83a420f0517d7fc47aeb288a6a0cf4891784fcc (diff) | |
New upstream version 3.2.0~rc1+dfsgupstream/3.2.0_rc1+dfsg
Diffstat (limited to 'map65/libm65/afc65b.f90')
| -rw-r--r-- | map65/libm65/afc65b.f90 | 190 |
1 files changed, 133 insertions, 57 deletions
diff --git a/map65/libm65/afc65b.f90 b/map65/libm65/afc65b.f90 index 8083d924..e0ebfbc0 100644 --- a/map65/libm65/afc65b.f90 +++ b/map65/libm65/afc65b.f90 @@ -3,20 +3,31 @@ module afc65b_mod contains subroutine afc65b(cx,cy,npts,fsample,nflip,ipol,xpol,ndphi,a,ccfbest,dtbest) - use debug_log use fchisq_mod + use, intrinsic :: ieee_arithmetic, only: ieee_is_finite implicit none - - logical :: xpol - integer :: npts, nflip, ipol, ndphi - complex :: cx(npts), cy(npts) - real :: a(5), deltaa(5) - integer :: iter, j, nterms - real :: ccfbest, dtbest - real :: chisq1, chisq2, chisq3, chisqr, chisqr0 - real :: delta, dtmax, fn, tmp - real :: fsample, ccfmax + + 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 @@ -33,78 +44,143 @@ subroutine afc65b(cx,cy,npts,fsample,nflip,ipol,xpol,ndphi,a,ccfbest,dtbest) nterms = 3 if (xpol) nterms = 4 + if (ndphi /= 0) nterms = 3 ! don't fit pol when solving for dphi - ! Don't fit polarization when solving for dphi - if (ndphi /= 0) nterms = 3 + ccfbest = 0.0 + dtbest = 0.0 + chisqr0 = 0.0 + have_previous = .false. - ! Start the iteration - chisqr = 0.0 - chisqr0 = 1.0e6 + if (npts < 1 .or. fsample <= 0.0 .or. .not. ieee_is_finite(fsample)) return - do iter = 1, 3 ! One iteration is enough? + do iter = 1, 3 do j = 1, nterms + x0 = a(j) chisq1 = fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax) - fn = 0.0 - delta = deltaa(j) + if (.not. ieee_is_finite(chisq1)) then + a(j) = x0 + cycle + endif - ! Find first step where chisq changes - do - a(j) = a(j) + delta + 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 + + do step = 1, MAX_STEP_TRIES + xnext = xlast + delta + a(j) = xnext chisq2 = fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax) - if (chisq2 /= chisq1) exit + 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 we stepped uphill, reverse direction and swap labels + if (.not. improved) then + a(j) = x0 + deltaa(j) = max(MIN_STEP, 0.5*delta) + cycle + endif + if (chisq2 > chisq1) then - delta = -delta - a(j) = a(j) + delta - tmp = chisq1 - chisq1 = chisq2 - chisq2 = tmp + 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 - ! Walk while chisq keeps improving - do - fn = fn + 1.0 - a(j) = a(j) + delta - chisq3 = fchisq(cx,cy,npts,fsample,nflip,a,ccfmax,dtmax) - if (chisq3 < chisq2) then - chisq1 = chisq2 - chisq2 = chisq3 + 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 - ! Guard against division by zero in flat regions - if (chisq3 == chisq2) cycle + 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.0/(1.0 + (chisq1 - chisq2)/(chisq3 - chisq2)) + 0.5) - a(j) = a(j) - delta - deltaa(j) = deltaa(j)*fn/3.0 + 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 > 0.9999) exit + 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 - ccfbest = ccfmax * (1378.125/fsample)**2 - dtbest = dtmax - - ! Wrap polarization angle into [0,180) - if (a(4) < 0.0) a(4) = a(4) + 180.0 - if (a(4) >= 180.) a(4) = a(4) - 180.0 - if (nint(a(4)) == 180) a(4) = 0.0 + 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) < 0.0 .or. a(4) >= 180.0) then - ! print *, 'afc65b: bad a(4) after wrap, set to 0. was ', a(4) - a(4) = 0.0 - endif + if (.not. ieee_is_finite(a(4))) a(4) = 0.0 + a(4) = modulo(a(4), 180.0) - ipol = nint(a(4)/45.0) + 1 - if (ipol > 4) ipol = ipol - 4 + ipol = modulo(nint(a(4)/45.0), 4) + 1 end subroutine afc65b |
