summaryrefslogtreecommitdiff
path: root/map65/libm65/afc65b.f90
blob: e0ebfbc01d964ef95883614d57a797e9206efefc (plain)
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
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

        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

        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

        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 (.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

  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 (.not. ieee_is_finite(a(4))) a(4) = 0.0
  a(4) = modulo(a(4), 180.0)

  ipol = modulo(nint(a(4)/45.0), 4) + 1
  
end subroutine afc65b

end module afc65b_mod