ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/freemol/trunk/src/mengine/src/tncg.c
Revision: 119
Committed: Tue Jun 16 21:36:00 2009 UTC (11 years ago) by wdelano
File size: 6477 byte(s)
Log Message:
printf( to fprintf(pcmlogfile,
Line File contents
1 #define EXTERN extern
2
3
4 #include "pcwin.h"
5 #include "pcmod.h"
6 #include "utility.h"
7 #include "job_control.h"
8 #include <sys/timeb.h>
9 #include <time.h>
10
11 // mode
12 #define NONE 0
13 #define NEWTON 1
14 #define TNCG 2
15 #define DTNCG 3
16 #define AUTO 4
17 // method
18 #define DIAG 5
19 #define SSOR 6
20 #define ICCG 7
21 #define BLOCK 8
22 #define FULL 9
23 // search results
24 #define Success 0
25 #define ReSearch 1
26 #define WideAngle 2
27 #define BadIntpln 3
28 #define IntplnErr 4
29 #define blank 5
30
31 void outminstat(int , double ,double );
32 void tncg(double,int,int,int *,double *,double *, double, double (*)(), double (*)());
33 double fgvalue(double *, double *);
34 void search(int,double *,double *,double *,double *,double,double *,int *,double (*)(),int *);
35 void solve(int,int,int,int, double *,double *,double *,double *,int *,
36 int *,int *,double *,int *,int *,int *, double (*)());
37 void hmatrix(int,double *,double *,int *, int *, int *, double *);
38
39 EXTERN struct t_minvar{
40 double cappa, stpmin, stpmax, angmax;
41 int intmax;
42 } minvar;
43
44 double hesscut;
45
46 void tncg(double start_time,int nvar,int imethod,int *iter,double *x,double *minimum, double grdmin,double (*fgvalue) (),double (*hmatrix) ())
47 {
48 int i, iter_tn, iter_cg, fg_call, newhess;
49 int *h_init, *h_stop, *h_index; // h_init[maxvar], h_stop[maxvar], h_index[MAXHESS];
50 double *x_old, *g, *p, *h_diag; // x_old[maxvar], g[maxvar], p[maxvar], h_diag[maxvar];
51 double *h; // h[MAXHESS];
52 double f, angle, rms, x_move, f_move, f_old, g_norm, g_rms;
53 int done, negtest, nextiter, maxiter;
54 double fctmin;
55 int mode, hmode, status, maxvar, maxhess, method;
56 int maxerr,nerr;
57 double end_time,old_time,ttime;
58 struct timeval tv;
59
60 old_time = start_time;
61 maxerr = 3;
62 nerr = 0;
63 maxvar = 3*natom;
64 if (natom < 500)
65 maxhess = (3*natom*(3*natom-1))/2;
66 else if (natom < 800)
67 maxhess = (3*natom*(3*natom-1))/3;
68 else
69 maxhess = (3*natom*(3*natom-1))/20;
70
71
72 h_init = ivector(0,maxvar);
73 h_stop = ivector(0,maxvar);
74 h_index = ivector(0,maxhess);
75 x_old = dvector(0,maxvar);
76 g = dvector(0,maxvar);
77 p = dvector(0,maxvar);
78 h_diag = dvector(0,maxvar);
79 h = dvector(0,maxhess);
80
81 rms = sqrt((float) nvar)/ sqrt(3.0);
82
83 fctmin = -1000000.0;
84 nextiter = 1;
85 newhess = 1;
86 done = FALSE;
87 minvar.cappa = 0.1;
88 minvar.stpmin = 1.0e-20;
89 minvar.stpmax = 1.5;
90 minvar.angmax = 180.0;
91 minvar.intmax = 8;
92 negtest = TRUE;
93 maxiter = 1000;
94
95 iter_tn = nextiter - 1;
96 maxiter += iter_tn;
97
98
99 iter_cg = 0;
100 fg_call = 1;
101 f = fgvalue(x,g);
102 f_old = f;
103 g_norm = 0.0;
104 for (i=0; i < nvar; i++)
105 {
106 x_old[i] = x[i];
107 g_norm += g[i]*g[i];
108 }
109 g_norm = sqrt(g_norm);
110 f_move = 0.5 * minvar.stpmax * g_norm;
111 g_rms = g_norm / rms;
112
113 done = FALSE;
114
115 if (g_rms <= grdmin)
116 {
117 done = TRUE;
118 *minimum = f;
119 } else if ( f <= fctmin)
120 {
121 done = TRUE;
122 *minimum = f;
123 } else if (iter_tn >= maxiter)
124 {
125 done = TRUE;
126 *minimum = f;
127 }
128 while ( ! done)
129 {
130 iter_tn++;
131
132
133 // mode = tcng method = diag
134 if ( g_rms > 3.0)
135 mode = TNCG;
136 else
137 mode = DTNCG;
138
139 hesscut = 0.0;
140 if (g_rms > 10.0)
141 {
142 method = DIAG;
143 hesscut = 1.0;
144 } else if (nvar < 10)
145 {
146 method = SSOR;
147 hesscut = 1.0;
148 }else if (g_rms < 1.0)
149 {
150 method = ICCG;
151 hesscut = .001*nvar;
152 if (hesscut > 0.1) hesscut = 0.1;
153 } else
154 {
155 method = ICCG;
156 hesscut = 0.001*nvar;
157 if (hesscut > 1.0) hesscut = 1.0;
158 }
159 hmode = FULL;
160
161 if ( (iter_tn%newhess) != 0) hmode = NONE;
162 if (mode == DTNCG && method == NONE) hmode = NONE;
163 if (mode == DTNCG && method == DIAG) hmode = DIAG;
164
165 hmatrix(hmode, x, h, h_init, h_stop, h_index, h_diag);
166
167 solve(mode,method,negtest,nvar, p,x,g,h,h_init,
168 h_stop,h_index,h_diag,&iter_tn,&iter_cg,&fg_call,fgvalue);
169 search(nvar,&f,g,x,p,f_move,&angle,&fg_call,fgvalue, &status);
170
171 if (status != Success)
172 {
173 nerr++;
174 if (nerr > maxerr)
175 {
176 // fprintf(pcmlogfile,"Error in tncg :: bad search\n");
177 done = TRUE;
178 }
179 }
180
181 f_move = f_old - f;
182 f_old = f;
183 x_move = 0.0;
184 g_norm = 0.0;
185 for(i=0; i < nvar; i++)
186 {
187 x_move += (x[i]-x_old[i])*(x[i]-x_old[i]);
188 x_old[i] = x[i];
189 g_norm += g[i]*g[i];
190 }
191
192 x_move = sqrt(x_move);
193 x_move = x_move / rms;
194 g_norm = sqrt(g_norm);
195 g_rms = g_norm / rms;
196
197 if (iter_tn >= maxiter)
198 done = TRUE;
199
200 if (f_move <= 0.0000001)
201 {
202 done = TRUE;
203 }
204
205 if (g_rms <= grdmin)
206 done = TRUE;
207 else if (f <= fctmin)
208 done = TRUE;
209
210 // check monitor
211 if (job_control.monitor)
212 {
213 gettimeofday(&tv,NULL);
214 end_time = tv.tv_sec + (tv.tv_usec/(double)1000000.0);
215 ttime = (end_time-old_time);
216 if ( ttime > 0 && (ttime > job_control.interval) )
217 {
218 // fprintf(pcmlogfile,"Times: %f %f %f\n",start_time,end_time,ttime);
219 old_time = end_time;
220 write_sdf(0);
221 }
222 }
223
224 if (done)
225 {
226 *minimum = f;
227 *iter = iter_tn;
228 free_ivector(h_init ,0,maxvar);
229 free_ivector(h_stop ,0,maxvar);
230 free_ivector(h_index ,0,maxhess);
231 free_dvector(x_old ,0,maxvar);
232 free_dvector(g ,0,maxvar);
233 free_dvector(p ,0,maxvar);
234 free_dvector(h_diag ,0,maxvar);
235 free_dvector(h ,0,maxhess);
236
237 return;
238
239 }
240 }
241 free_ivector(h_init ,0,maxvar);
242 free_ivector(h_stop ,0,maxvar);
243 free_ivector(h_index ,0,maxhess);
244 free_dvector(x_old ,0,maxvar);
245 free_dvector(g ,0,maxvar);
246 free_dvector(p ,0,maxvar);
247 free_dvector(h_diag ,0,maxvar);
248 free_dvector(h ,0,maxhess);
249
250 return;
251 }