00001
00002
00003
00004
00005
00006
00011 #include <df1b2fun.h>
00012 #ifndef OPT_LIB
00013 #include <cassert>
00014 #include <climits>
00015 #endif
00016
00017 #if defined(CHECK_COUNT)
00018 void ncount_checker(int ncount,int ncount_check)
00019 {
00020
00021
00022 }
00023 #endif
00024
00025 void ad_read_pass1(void);
00026 void ad_read_pass2(void);
00027 void read_pass1_3(void);
00028
00029
00034 int df1b2_gradlist::write_pass1(const df1b2variable * _px,
00035 df1b2variable * pz, df1b2function1 * pf)
00036 {
00037 ADUNCONST(df1b2variable*,px)
00038 ncount++;
00039 #if defined(CHECK_COUNT)
00040 if (ncount >= ncount_check)
00041 ncount_checker(ncount,ncount_check);
00042 #endif
00043
00044 unsigned int nvar=df1b2variable::nvar;
00045
00046 size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header)+sizeof(char*)
00047 +sizeof(double)+nvar*sizeof(double);
00048
00049 #if defined(SAFE_ALL)
00050 char ids[]="CX";
00051 int slen=strlen(ids);
00052 total_bytes+=slen;
00053 #endif
00054
00055 list.check_buffer_size(total_bytes);
00056 void * tmpptr=list.bptr;
00057 #if defined(SAFE_ALL)
00058 memcpy(list,ids,slen);
00059 #endif
00060
00061
00062 memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
00063 memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00064
00065 memcpy(list,&pf,sizeof(char*));
00066
00067
00068 memcpy(list,px->get_u(),sizeof(double));
00069
00070 memcpy(list,px->get_u_dot(),nvar*(int)sizeof(double));
00071
00072
00073 nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00074 nlist.bptr->pf=(ADrfptr)(&ad_read_pass1);
00075 ++nlist;
00076 return 0;
00077 }
00078
00083 void ad_read_pass1(void)
00084 {
00085 switch(df1b2variable::passnumber)
00086 {
00087 case 1:
00088 read_pass1_1();
00089 break;
00090 case 2:
00091 read_pass1_2();
00092 break;
00093 case 3:
00094 read_pass1_3();
00095 break;
00096 default:
00097 cerr << "illegal value for df1b2variable::pass = "
00098 << df1b2variable::passnumber << endl;
00099 exit(1);
00100 }
00101 }
00102
00107 void read_pass1_1(void)
00108 {
00109
00110
00111
00112 unsigned int nvar=df1b2variable::nvar;
00113 fixed_smartlist & nlist=f1b2gradlist->nlist;
00114 test_smartlist& list=f1b2gradlist->list;
00115
00116
00117 int num_bytes=nlist.bptr->numbytes;
00118
00119 list-=num_bytes;
00120
00121 list.saveposition();
00122
00123 double xu;
00124 double * xdot;
00125
00126 df1b2function1 * pf;
00127
00128
00129
00130 #if defined(SAFE_ALL)
00131 checkidentiferstring("CX",list);
00132 #endif
00133 df1b2_header * px=(df1b2_header *) list.bptr;
00134 list.bptr+=sizeof(df1b2_header);
00135 df1b2_header * pz=(df1b2_header *) list.bptr;
00136 list.bptr+=sizeof(df1b2_header);
00137 pf=*(df1b2function1 **) list.bptr;
00138 list.bptr+=sizeof(char*);
00139 memcpy(&xu,list.bptr,sizeof(double));
00140 list.bptr+=sizeof(double);
00141 xdot=(double*)list.bptr;
00142 list.restoreposition();
00143
00144
00145
00146
00147
00148
00149
00150
00151 fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
00152 test_smartlist& list2=f1b2gradlist->list2;
00153
00154
00155 size_t total_bytes=2*nvar*sizeof(double);
00156
00157 #if defined(SAFE_ALL)
00158 char ids[]="DU";
00159 size_t slen=strlen(ids);
00160 total_bytes+=slen;
00161 #endif
00162
00163 list2.check_buffer_size(total_bytes);
00164 void * tmpptr2=list2.bptr;
00165
00166 #if defined(SAFE_ALL)
00167 memcpy(list2,ids,slen);
00168 #endif
00169
00170 const int sizeofdouble = (int)sizeof(double);
00171 memcpy(list2,pz->get_u_bar(),nvar*sizeofdouble);
00172 memcpy(list2,pz->get_u_dot_bar(),nvar*sizeofdouble);
00173 *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
00174 ++nlist2;
00175
00176
00177
00178 #if defined(PRINT_DERS)
00179 print_derivatives(pf->funname,(pf->df)(xu),(pf->df)(xu),(pf->d2f)(xu),
00180 (pf->d3f)(xu),1);
00181 print_derivatives(pz,"z");
00182 print_derivatives(px,"x");
00183 #endif
00184
00185 double df=(pf->df)(xu);
00186 double d2f=(pf->d2f)(xu);
00187
00188
00189 for (size_t i=0;i<nvar;i++)
00190 {
00191
00192 px->u_bar[i]+=df * pz->u_bar[i];
00193 }
00194 for (size_t i=0;i<nvar;i++)
00195 {
00196
00197 px->u_bar[i]+=d2f*xdot[i]*pz->u_dot_bar[i];
00198 }
00199 for (size_t i=0;i<nvar;i++)
00200 {
00201
00202 px->u_dot_bar[i]+=df*pz->u_dot_bar[i];
00203 }
00204
00205
00206 for (size_t i=0;i<nvar;i++)
00207 {
00208 pz->u_bar[i]=0;
00209 }
00210 for (size_t i=0;i<nvar;i++)
00211 {
00212 pz->u_dot_bar[i]=0;
00213 }
00214
00215 #if defined(PRINT_DERS)
00216 print_derivatives(pz,"z");
00217 print_derivatives(px,"x");
00218 #endif
00219 }
00220
00225 void read_pass1_2(void)
00226 {
00227
00228
00229
00230
00231
00232 unsigned int nvar=df1b2variable::nvar;
00233 test_smartlist & list=f1b2gradlist->list;
00234
00235 size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header)+sizeof(char*)
00236 +sizeof(double)+nvar*sizeof(double);
00237
00238 #if defined(SAFE_ALL)
00239 char ids[]="CX";
00240 int slen=strlen(ids);
00241 total_bytes+=slen;
00242 #endif
00243
00244 list.check_buffer_size(total_bytes);
00245
00246
00247 list.saveposition();
00248 fixed_smartlist & nlist=f1b2gradlist->nlist;
00249
00250
00251 int num_bytes=nlist.bptr->numbytes;
00252
00253
00254
00255
00256 test_smartlist & list2=f1b2gradlist->list2;
00257 fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00258
00259 int num_bytes2=*nlist2.bptr;
00260 --nlist2;
00261
00262 list2-=num_bytes2;
00263 list2.saveposition();
00264
00265
00266
00267 double xu;
00268 double * xdot;
00269
00270 df1b2function1 * pf;
00271
00272
00273
00274 #if defined(SAFE_ALL)
00275 checkidentiferstring("CX",list);
00276 checkidentiferstring("DU",list2);
00277 #endif
00278 df1b2_header * px=(df1b2_header *) list.bptr;
00279 list.bptr+=sizeof(df1b2_header);
00280 df1b2_header * pz=(df1b2_header *) list.bptr;
00281 list.bptr+=sizeof(df1b2_header);
00282 pf=*(df1b2function1 **) list.bptr;
00283 list.bptr+=sizeof(char*);
00284 memcpy(&xu,list.bptr,sizeof(double));
00285 list.bptr+=sizeof(double);
00286 xdot=(double*)list.bptr;
00287 list.restoreposition(num_bytes);
00288
00289 double* zbar=(double*)list2.bptr;
00290 double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00291
00292 double * x_tilde=px->get_u_tilde();
00293 double * x_dot_tilde=px->get_u_dot_tilde();
00294 double * x_bar_tilde=px->get_u_bar_tilde();
00295 double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00296 double * z_bar_tilde=pz->get_u_bar_tilde();
00297 double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00298 #if defined(PRINT_DERS)
00299 print_derivatives(pf->funname,(pf->df)(xu),(pf->df)(xu),(pf->d2f)(xu),
00300 (pf->d3f)(xu),1);
00301 print_derivatives(pz,"z");
00302 print_derivatives(px,"x");
00303 #endif
00304
00305
00306
00307 for (size_t i=0;i<nvar;i++)
00308 {
00309 z_bar_tilde[i]=0;
00310 z_dot_bar_tilde[i]=0;
00311 }
00312
00313 double df=(pf->df)(xu);
00314 double d2f=(pf->d2f)(xu);
00315 double d3f=(pf->d3f)(xu);
00316 for (size_t i=0;i<nvar;i++)
00317 {
00318
00319 *x_tilde+=d2f*zbar[i]*x_bar_tilde[i];
00320
00321 z_bar_tilde[i]+=df*x_bar_tilde[i];
00322 }
00323
00324 for (size_t i=0;i<nvar;i++)
00325 {
00326
00327 *x_tilde+=d2f*zdotbar[i]*x_dot_bar_tilde[i];
00328
00329 z_dot_bar_tilde[i]+=df*x_dot_bar_tilde[i];
00330 }
00331
00332 for (size_t i=0;i<nvar;i++)
00333 {
00334
00335
00336
00337 x_dot_tilde[i]+=d2f*zdotbar[i]*x_bar_tilde[i];
00338 z_dot_bar_tilde[i]+=d2f*xdot[i]*x_bar_tilde[i];
00339 *x_tilde+=d3f*xdot[i]*zdotbar[i]*x_bar_tilde[i];
00340 }
00341 list2.restoreposition();
00342 #if defined(PRINT_DERS)
00343 print_derivatives(pz,"z");
00344 print_derivatives(px,"x");
00345 #endif
00346 }
00347
00352 void read_pass1_3(void)
00353 {
00354
00355
00356
00357 unsigned int nvar=df1b2variable::nvar;
00358 fixed_smartlist & nlist=f1b2gradlist->nlist;
00359 test_smartlist& list=f1b2gradlist->list;
00360
00361
00362 int num_bytes=nlist.bptr->numbytes;
00363
00364 list-=num_bytes;
00365 list.saveposition();
00366
00367 double xu;
00368 double * xdot;
00369
00370 df1b2function1 * pf;
00371
00372
00373 #if defined(SAFE_ALL)
00374 checkidentiferstring("CX",list);
00375 #endif
00376 df1b2_header * px=(df1b2_header *) list.bptr;
00377 list.bptr+=sizeof(df1b2_header);
00378 df1b2_header * pz=(df1b2_header *) list.bptr;
00379 list.bptr+=sizeof(df1b2_header);
00380 pf=*(df1b2function1 **) list.bptr;
00381 list.bptr+=sizeof(char*);
00382 memcpy(&xu,list.bptr,sizeof(double));
00383 list.bptr+=sizeof(double);
00384 xdot=(double*)list.bptr;
00385 list.restoreposition();
00386
00387 #if defined(PRINT_DERS)
00388 print_derivatives(pf->funname,(pf->df)(xu),(pf->df)(xu),(pf->d2f)(xu),
00389 (pf->d3f)(xu),1);
00390 print_derivatives(pz,"z");
00391 print_derivatives(px,"x");
00392 #endif
00393
00394 double df=(pf->df)(xu);
00395 double d2f=(pf->d2f)(xu);
00396
00397 *(px->u_tilde)+=df * *(pz->u_tilde);
00398 for (size_t i=0;i<nvar;i++)
00399 {
00400
00401 *(px->u_tilde)+=d2f*xdot[i]*pz->u_dot_tilde[i];
00402 }
00403 for (size_t i=0;i<nvar;i++)
00404 {
00405
00406 px->u_dot_tilde[i]+=df*pz->u_dot_tilde[i];
00407 }
00408 *(pz->u_tilde)=0;
00409 for (size_t i=0;i<nvar;i++)
00410 {
00411 pz->u_dot_tilde[i]=0;
00412 }
00413 #if defined(PRINT_DERS)
00414 print_derivatives(pz,"z");
00415 print_derivatives(px,"x");
00416 #endif
00417 }