ADMB Documentation  11.1.1916
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2prc.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2prc.cpp 1759 2014-03-06 19:34:41Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <df1b2fun.h>
00012   int debugcounter=0;
00013 
00014 /*
00015   df1b2variable operator / (double x,const df1b2variable& y)
00016   {
00017     return x*inv(y);
00018   }
00019 */
00020 
00025   df1b2variable operator / (const df1b2variable& x,double y)
00026   {
00027     return x*(1.0/y);
00028   }
00029 
00034   df1b2variable operator * (double x,const df1b2variable& _y)
00035   {
00036     ADUNCONST(df1b2variable,y)
00037     df1b2variable z;
00038     double * yd=y.get_u_dot();
00039     double * zd=z.get_u_dot();
00040     double yu=*y.get_u();
00041 
00042     *z.get_u()=x*yu;
00043 
00044     for (int i=0;i<df1b2variable::nvar;i++)
00045     {
00046       *zd++ = x * *yd++;
00047     }
00048 
00049     // WRITE WHATEVER ON TAPE
00050     if (!df1b2_gradlist::no_derivatives)
00051       f1b2gradlist->write_pass1_prod(x,&y,&z);
00052     return z;
00053   }
00054 
00055 
00056 void ad_read_pass2_prodc1(void);
00057 
00062  int df1b2_gradlist::write_pass1_prod(double x,const df1b2variable * _py,
00063    df1b2variable * pz)
00064  {
00065    ADUNCONST(df1b2variable*,py)
00066    ncount++;
00067 #if defined(CHECK_COUNT)
00068   if (ncount >= ncount_check)
00069     cout << ncount << endl;
00070 #endif
00071    int nvar=df1b2variable::nvar;
00072 
00073    //int total_bytes=3*sizeof(df1b2_header)+sizeof(char*)
00074    //  +2*(nvar+1)*sizeof(double);
00075    int total_bytes=2*sizeof(df1b2_header)
00076      +(nvar+2)*sizeof(double);
00077 // string identifier debug stuff
00078 #if defined(SAFE_ALL)
00079   char ids[]="DL";
00080   int slen=strlen(ids);
00081   total_bytes+=slen;
00082 #endif
00083   list.check_buffer_size(total_bytes);
00084   void * tmpptr=list.bptr;
00085 #if defined(SAFE_ALL)
00086   memcpy(list,ids,slen);
00087 #endif
00088 // end of string identifier debug stuff
00089 
00090    memcpy(list,&x,sizeof(double));
00091    memcpy(list,(df1b2_header*)(py),sizeof(df1b2_header));
00092    memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00093    memcpy(list,py->get_u(),sizeof(double));
00094    memcpy(list,py->get_u_dot(),nvar*sizeof(double));
00095    // ***** write  record size
00096    nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00097    if (total_bytes != nlist.bptr->numbytes)
00098    {
00099      cerr << "error in byte calculation in "
00100        " write_pass1_prod" << endl;
00101      ad_exit(1);
00102    }
00103    nlist.bptr->pf=(ADrfptr)(&ad_read_pass2_prodc1);
00104       ++nlist;
00105    return 0;
00106  }
00107 
00108 
00109 void read_pass2_1_prodc1(void);
00110 void read_pass2_2_prodc1(void);
00111 void read_pass2_3_prodc1(void);
00112 
00117 void ad_read_pass2_prodc1(void)
00118 {
00119   switch(df1b2variable::passnumber)
00120   {
00121   case 1:
00122     read_pass2_1_prodc1();
00123     break;
00124   case 2:
00125     read_pass2_2_prodc1();
00126     break;
00127   case 3:
00128     read_pass2_3_prodc1();
00129     break;
00130   default:
00131     cerr << "illegal value for df1b2variable::pass = "
00132          << df1b2variable::passnumber << endl;
00133     exit(1);
00134   }
00135 }
00136 
00141 void read_pass2_1_prodc1(void)
00142 {
00143   //  vmon_begin();
00144   // We are going backword for bptr and nbptr
00145   // and  forward for bptr2 and nbptr2
00146   // the current entry+2 in bptr is the size of the record i.e
00147   // points to the next record
00148   //char * bptr=f1b2gradlist->bptr;
00149   //char * bptr2=f1b2gradlist2->bptr;
00150   int nvar=df1b2variable::nvar;
00151   test_smartlist& list=f1b2gradlist->list;
00152   //f1b2gradlist->nlist-=sizeof(int);
00153   int num_bytes=f1b2gradlist->nlist.bptr->numbytes;
00154   list-=num_bytes;
00155   list.saveposition(); // save pointer to beginning of record;
00156   double yu;
00157 
00158   // get info from tape1
00159 #if defined(SAFE_ARRAYS)
00160   checkidentiferstring("DL",f1b2gradlist->list);
00161 #endif
00162   char * bptr=f1b2gradlist->list.bptr;
00163   double xu=*(double *) bptr;
00164   bptr+=sizeof(double);
00165   df1b2_header * py=(df1b2_header *) bptr;
00166   bptr+=sizeof(df1b2_header);
00167   df1b2_header * pz=(df1b2_header *) bptr;
00168   bptr+=sizeof(df1b2_header);
00169   memcpy(&yu,bptr,sizeof(double));
00170   bptr+=sizeof(double);
00171   //double * ydot=(double*)bptr;
00172 
00173   list.restoreposition(); // save pointer to beginning of record;
00174 
00175   // ****************************************************************
00176   // turn this off if no third derivatives are calculated
00177   // if (!no_third_derivatives)
00178   // {
00179   // save for second reverse pass
00180   // save identifier 1
00181      test_smartlist & list2 = f1b2gradlist->list2;
00182 
00183 
00184    int total_bytes=2*nvar*sizeof(double);
00185 // string identifier debug stuff
00186 #if defined(SAFE_ALL)
00187   char ids[]="QK";
00188   int slen=strlen(ids);
00189   total_bytes+=slen;
00190 #endif
00191   list2.check_buffer_size(total_bytes);
00192   void * tmpptr=list2.bptr;
00193 #if defined(SAFE_ALL)
00194   memcpy(list2,ids,slen);
00195 #endif
00196 
00197   fixed_smartlist2 & nlist2 = f1b2gradlist->nlist2;
00198   /*
00199   if (debugcounter++>569)
00200   {
00201     cout << debugcounter++ << endl;
00202     memcpy(list2,pz->get_u_bar(),2*nvar*sizeof(double));
00203     memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
00204     memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
00205   }
00206   else
00207   */
00208   {
00209     memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
00210     memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
00211   }
00212   *nlist2.bptr=adptr_diff(list2.bptr,tmpptr);
00213   ++nlist2;
00214 
00215   // Do first reverse pass calculations
00216   int i;
00217   for (i=0;i<nvar;i++)
00218   {
00219     py->u_bar[i]+=xu*pz->u_bar[i];
00220   }
00221 
00222   for (i=0;i<nvar;i++)
00223   {
00224     py->u_dot_bar[i]+=xu*pz->u_dot_bar[i];
00225   }
00226 
00227   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00228   for (i=0;i<nvar;i++)
00229   {
00230     pz->u_bar[i]=0;
00231   }
00232   for (i=0;i<nvar;i++)
00233   {
00234     pz->u_dot_bar[i]=0;
00235   }
00236 }
00237 
00242 void read_pass2_2_prodc1(void)
00243 {
00244   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00245   // We are going forward for bptr and backword for bptr2
00246   //
00247   // list 1
00248   //
00249   int nvar=df1b2variable::nvar;
00250   test_smartlist & list=f1b2gradlist->list;
00251 
00252    int total_bytes=2*sizeof(df1b2_header)
00253      +(nvar+2)*sizeof(double);
00254 // string identifier debug stuff
00255 #if defined(SAFE_ALL)
00256   char ids[]="DL";
00257   int slen=strlen(ids);
00258   total_bytes+=slen;
00259 #endif
00260   list.check_buffer_size(total_bytes);
00261 // end of string identifier debug stuff
00262 
00263   list.saveposition(); // save pointer to beginning of record;
00264   fixed_smartlist & nlist=f1b2gradlist->nlist;
00265    // nlist-=sizeof(int);
00266   // get record size
00267   int num_bytes=nlist.bptr->numbytes;
00268   //
00269   // list 2
00270   //
00271   test_smartlist & list2=f1b2gradlist->list2;
00272   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00273   // get record size
00274   int num_bytes2=*nlist2.bptr;
00275   --nlist2;
00276   // backup the size of the record
00277   list2-=num_bytes2;
00278   list2.saveposition(); // save pointer to beginning of record;
00279   // save the pointer to the beginning of the record
00280   // bptr and bptr2 now both point to the beginning of their records
00281 
00282   double yu;
00283   //df1b2_header x,z;
00284   //df1b2function2 * pf;
00285 
00286   // get info from tape1
00287   // get info from tape1
00288 #if defined(SAFE_ARRAYS)
00289   checkidentiferstring("DL",list);
00290   checkidentiferstring("QK",list2);
00291 #endif
00292   double xu=*(double *) list.bptr;
00293   list.bptr+=sizeof(double);
00294   df1b2_header * py=(df1b2_header *) list.bptr;
00295   list.bptr+=sizeof(df1b2_header);
00296   df1b2_header * pz=(df1b2_header *) list.bptr;
00297   list.bptr+=sizeof(df1b2_header);
00298   //pf=*(df1b2function2 **) list.bptr;
00299   //list.bptr+=sizeof(char*);
00300   memcpy(&yu,list.bptr,sizeof(double));
00301   list.bptr+=sizeof(double);
00302 
00303   //double* ydot=(double*)list.bptr;
00304   list.restoreposition(num_bytes); // save pointer to beginning of record;
00305 
00306   //double* zbar=(double*)list2.bptr;
00307   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00308 
00309   list2.restoreposition(); // save pointer to beginning of record;
00310 
00311   //double * y_tilde=py->get_u_tilde();
00312   //double * y_dot_tilde=py->get_u_dot_tilde();
00313   double * y_bar_tilde=py->get_u_bar_tilde();
00314   double * y_dot_bar_tilde=py->get_u_dot_bar_tilde();
00315   double * z_bar_tilde=pz->get_u_bar_tilde();
00316   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00317   // Do second "reverse-reverse" pass calculations
00318 
00319   int i;
00320 
00321   for (i=0;i<nvar;i++)
00322   {
00323     z_bar_tilde[i]=0;
00324     z_dot_bar_tilde[i]=0;
00325   }
00326 
00327   // start with y and add x
00328   for (i=0;i<nvar;i++)
00329   {
00330     z_bar_tilde[i]+=xu*y_bar_tilde[i];
00331   }
00332 
00333   for (i=0;i<nvar;i++)
00334   {
00335     z_dot_bar_tilde[i]+=xu*y_dot_bar_tilde[i];
00336   }
00337 }
00338 
00343 void read_pass2_3_prodc1(void)
00344 {
00345   // We are going backword for bptr and forward for bptr2
00346   // the current entry+2 in bptr is the size of the record i.e
00347   // points to the next record
00348   int nvar=df1b2variable::nvar;
00349   fixed_smartlist & nlist=f1b2gradlist->nlist;
00350   test_smartlist& list=f1b2gradlist->list;
00351    // nlist-=sizeof(int);
00352   // get record size
00353   int num_bytes=nlist.bptr->numbytes;
00354   // backup the size of the record
00355   list-=num_bytes;
00356   list.saveposition(); // save pointer to beginning of record;
00357   // save the pointer to the beginning of the record
00358   double yu;
00359   //df1b2_header z;
00360   //df1b2function2 * pf;
00361 
00362   // get info from tape1
00363   // get info from tape1
00364 #if defined(SAFE_ARRAYS)
00365   checkidentiferstring("DL",list);
00366 #endif
00367   double xu=*(double*) list.bptr;
00368   list.bptr+=sizeof(double);
00369   df1b2_header * py=(df1b2_header *) list.bptr;
00370   list.bptr+=sizeof(df1b2_header);
00371   df1b2_header * pz=(df1b2_header *) list.bptr;
00372   list.bptr+=sizeof(df1b2_header);
00373   memcpy(&yu,list.bptr,sizeof(double));
00374   list.bptr+=sizeof(double);
00375 
00376   //double * ydot = (double*)list.bptr;
00377 
00378   list.restoreposition(); // save pointer to beginning of record;
00379   int i;
00380 
00381   *(py->u_tilde)+=xu* *(pz->u_tilde);
00382   for (i=0;i<nvar;i++)
00383   {
00384     py->u_dot_tilde[i]+=xu*pz->u_dot_tilde[i];
00385   }
00386   *(pz->u_tilde)=0;
00387   for (i=0;i<nvar;i++)
00388   {
00389     pz->u_dot_tilde[i]=0;
00390   }
00391 }