aboutsummaryrefslogtreecommitdiff
path: root/src/AHFinder_min.F
blob: de9856e03f0b1ad1831aff7d13e0975241158e5e (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
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
/*@@
  @file      AHFinder_min.F
  @date      April 1998
  @author    Miguel Alcubierre
  @desc 
             Minimization routine.
  @enddesc 
@@*/

#include "cctk.h"
#include "cctk_Parameters.h"
#include "cctk_Arguments.h"

      subroutine AHFinder_min(CCTK_ARGUMENTS,NN,status,logf)

      use AHFinder_dat

      implicit none

      DECLARE_CCTK_ARGUMENTS
      DECLARE_CCTK_PARAMETERS

      logical status,found

      integer i,NN
      integer l,m
      integer iter,itmax

      CCTK_REAL FTOL,FRET
      CCTK_REAL zero,half,one

      CCTK_REAL, dimension (NN)    :: P
      CCTK_REAL, dimension (NN,NN) :: XI

      character(len=200) :: logf

!     Description of variables:
!
!     i,j,l,m   Counters.
!
!     ITER      Number of iterations taken in minimization.
!     ITMAX     Maximum number of iterations allowed.
!
!     FTOL      Fractional tolerance for return criterion.
!
!     FRET      Value of fucntion on return from minimization.
!
!     P         Vector of coefficients.
!
!     XI        Matrix of initial directions.


!     ***********************************
!     ***   DEFINE  {zero,half,one}   ***
!     ***********************************

      zero = 0.0D0
      half = 0.5D0
      one  = 1.0D0


!     *********************************
!     ***   FIND INITIAL VECTOR P   ***
!     *********************************

      P = zero
      i = 0
 
!     Copy {xc,yc,zc}.

      if (wander) then
         if (.not.refx) then
            i = i+1
            P(1) = xc
         end if
         if (.not.refy) then
            i = i+1
            P(2) = yc
         end if
         if (.not.refz) then
            i = i+1
            P(3) = zc
         end if
      end if

!     Copy c0(l).

      do l=0,lmax,1+stepz
         i = i+1
         P(i) = c0(l)
      end do

!     Copy {cc,cs}.

      if (nonaxi) then

!        Copy cc(l,m).

         do l=1,lmax
            do m=1+stepx,l,1+stepx
               if (stepz*mod(l-m,2).eq.0) then
                  i = i+1
                  P(i) = cc(l,m)
               end if
            end do
         end do

!        Copy cs(l,m).

         if (.not.refy) then
            do l=1,lmax
               do m=1,l,1+stepx
                  if (stepz*mod(l-m,2).eq.0) then
                     i = i+1
                     P(i) = cs(l,m)
                  end if
               end do
            end do
         end if

      end if


!     *********************************************
!     ***   FIND MATRIX OF INITIAL DIRECTIONS   ***
!     *********************************************

!     The initial set of directions are taken to be
!     the unit vectors (the first index identifies the
!     components, and second index the vectors).

      XI = zero

      do i=1,NN
         XI(i,i) = one
      end do


!     ****************************************
!     ***   CALL MINIMIZATION SUBROUTINE   ***
!     ****************************************

!     Find ITMAX.

      ITMAX = ahf_maxiter

!     Find FTOL.  FTOL is the fractional tolerance in the
!     function value so that failure to decrease by more
!     than this amount on one interation signals that we
!     are done.

      FTOL = ahf_tol

!     Here I call the routine POWELL from Numerical Recipies.

      found = .false.
      firstfun = .true.

      call POWELL(CCTK_ARGUMENTS,P,XI,NN,FTOL,ITER,ITMAX,FRET,found)


!     ****************************
!     ***   FIND  {c0,cc,cs}   ***
!     ****************************

      i = 0

!     Find {xc,yc,zc}.

      if (wander) then
         if (.not.refx) then
            i = i+1
            xc = P(1)
         end if
         if (.not.refy) then
            i = i+1
            yc = P(2)
         end if
         if (.not.refz) then
            i = i+1
            zc = P(3)
         end if
      end if

!     Find c0(l).

      do l=0,lmax,1+stepz
         i = i+1
         c0(l) = P(i)
      end do

!     Find {cc,cs}.

      if (nonaxi) then

!        Find cc(l,m).

         do l=1,lmax
            do m=1+stepx,l,1+stepx
               if (stepz*mod(l-m,2).eq.0) then
                  i = i+1
                  cc(l,m) = P(i)
               end if
            end do
         end do

!        Find cs(l,m).

         if (.not.refy) then
            do l=1,lmax
               do m=1,l,1+stepx
                  if (stepz*mod(l-m,2).eq.0) then
                     i = i+1
                     cs(l,m) = P(i)
                  end if
               end do
            end do
         end if

      end if


!     ***************************************
!     ***   DECIDE ON THE RETURN STATUS   ***
!     ***************************************

!     If a minimum was found, set status to true.

      if (found) then

         status = .true.

!     If no minimum was found, it might still be because
!     there were to many iterations, in which case there
!     is a minimum, we just did not converge to it in the
!     maximum number of iterations allowed.

      else

         if (ITER.eq.ITMAX) then

            status = .true.

            if ((verbose.or.logfile).and.(myproc.eq.0)) then

               write(*,*)
               write(*,*) 'AHFinder:  Too many iterations.'

               if (logfile) then
                  open(11,file=logf,form='formatted',
     .            status='old',position='append')
                  write(11,*)
                  write(11,*) 'AHFinder:  Too many iterations.'
                  close(11)
               end if

            end if

         else

            status = .false.

         end if

      end if


!     ***************
!     ***   END   ***
!     ***************

      end subroutine AHFinder_min