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