ADMB Documentation  11.1.2275
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2prc.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2prc.cpp 2243 2014-08-13 03:34:48Z 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_ALL)
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); double* ydot=(double*)bptr;
00171 
00172   list.restoreposition(); // save pointer to beginning of record;
00173 
00174   // ****************************************************************
00175   // turn this off if no third derivatives are calculated
00176   // if (!no_third_derivatives)
00177   // {
00178   // save for second reverse pass
00179   // save identifier 1
00180      test_smartlist & list2 = f1b2gradlist->list2;
00181 
00182 
00183    int total_bytes=2*nvar*sizeof(double);
00184 // string identifier debug stuff
00185 #if defined(SAFE_ALL)
00186   char ids[]="QK";
00187   int slen=strlen(ids);
00188   total_bytes+=slen;
00189 #endif
00190   list2.check_buffer_size(total_bytes);
00191   void * tmpptr=list2.bptr;
00192 #if defined(SAFE_ALL)
00193   memcpy(list2,ids,slen);
00194 #endif
00195 
00196   fixed_smartlist2 & nlist2 = f1b2gradlist->nlist2;
00197   /*
00198   if (debugcounter++>569)
00199   {
00200     cout << debugcounter++ << endl;
00201     memcpy(list2,pz->get_u_bar(),2*nvar*sizeof(double));
00202     memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
00203     memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
00204   }
00205   else
00206   */
00207   {
00208     memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
00209     memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
00210   }
00211   *nlist2.bptr=adptr_diff(list2.bptr,tmpptr);
00212   ++nlist2;
00213 
00214   // Do first reverse pass calculations
00215   int i;
00216   for (i=0;i<nvar;i++)
00217   {
00218     py->u_bar[i]+=xu*pz->u_bar[i];
00219   }
00220 
00221   for (i=0;i<nvar;i++)
00222   {
00223     py->u_dot_bar[i]+=xu*pz->u_dot_bar[i];
00224   }
00225 
00226   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00227   for (i=0;i<nvar;i++)
00228   {
00229     pz->u_bar[i]=0;
00230   }
00231   for (i=0;i<nvar;i++)
00232   {
00233     pz->u_dot_bar[i]=0;
00234   }
00235 }
00236 
00241 void read_pass2_2_prodc1(void)
00242 {
00243   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00244   // We are going forward for bptr and backword for bptr2
00245   //
00246   // list 1
00247   //
00248   int nvar=df1b2variable::nvar;
00249   test_smartlist & list=f1b2gradlist->list;
00250 
00251    int total_bytes=2*sizeof(df1b2_header)
00252      +(nvar+2)*sizeof(double);
00253 // string identifier debug stuff
00254 #if defined(SAFE_ALL)
00255   char ids[]="DL";
00256   int slen=strlen(ids);
00257   total_bytes+=slen;
00258 #endif
00259   list.check_buffer_size(total_bytes);
00260 // end of string identifier debug stuff
00261 
00262   list.saveposition(); // save pointer to beginning of record;
00263   fixed_smartlist & nlist=f1b2gradlist->nlist;
00264    // nlist-=sizeof(int);
00265   // get record size
00266   int num_bytes=nlist.bptr->numbytes;
00267   //
00268   // list 2
00269   //
00270   test_smartlist & list2=f1b2gradlist->list2;
00271   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00272   // get record size
00273   int num_bytes2=*nlist2.bptr;
00274   --nlist2;
00275   // backup the size of the record
00276   list2-=num_bytes2;
00277   list2.saveposition(); // save pointer to beginning of record;
00278   // save the pointer to the beginning of the record
00279   // bptr and bptr2 now both point to the beginning of their records
00280 
00281   double yu;
00282   //df1b2_header x,z;
00283   //df1b2function2 * pf;
00284 
00285   // get info from tape1
00286   // get info from tape1
00287 #if defined(SAFE_ALL)
00288   checkidentiferstring("DL",list);
00289   checkidentiferstring("QK",list2);
00290 #endif
00291   double xu=*(double *) list.bptr;
00292   list.bptr+=sizeof(double);
00293   df1b2_header * py=(df1b2_header *) list.bptr;
00294   list.bptr+=sizeof(df1b2_header);
00295   df1b2_header * pz=(df1b2_header *) list.bptr;
00296   list.bptr+=sizeof(df1b2_header);
00297   //pf=*(df1b2function2 **) list.bptr;
00298   //list.bptr+=sizeof(char*);
00299   memcpy(&yu,list.bptr,sizeof(double));
00300   list.bptr+=sizeof(double);
00301 
00302   //double* ydot=(double*)list.bptr;
00303   list.restoreposition(num_bytes); // save pointer to beginning of record;
00304 
00305   //double* zbar=(double*)list2.bptr;
00306   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00307 
00308   list2.restoreposition(); // save pointer to beginning of record;
00309 
00310   //double * y_tilde=py->get_u_tilde();
00311   //double * y_dot_tilde=py->get_u_dot_tilde();
00312   double * y_bar_tilde=py->get_u_bar_tilde();
00313   double * y_dot_bar_tilde=py->get_u_dot_bar_tilde();
00314   double * z_bar_tilde=pz->get_u_bar_tilde();
00315   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00316   // Do second "reverse-reverse" pass calculations
00317 
00318   int i;
00319 
00320   for (i=0;i<nvar;i++)
00321   {
00322     z_bar_tilde[i]=0;
00323     z_dot_bar_tilde[i]=0;
00324   }
00325 
00326   // start with y and add x
00327   for (i=0;i<nvar;i++)
00328   {
00329     z_bar_tilde[i]+=xu*y_bar_tilde[i];
00330   }
00331 
00332   for (i=0;i<nvar;i++)
00333   {
00334     z_dot_bar_tilde[i]+=xu*y_dot_bar_tilde[i];
00335   }
00336 }
00337 
00342 void read_pass2_3_prodc1(void)
00343 {
00344   // We are going backword for bptr and forward for bptr2
00345   // the current entry+2 in bptr is the size of the record i.e
00346   // points to the next record
00347   int nvar=df1b2variable::nvar;
00348   fixed_smartlist & nlist=f1b2gradlist->nlist;
00349   test_smartlist& list=f1b2gradlist->list;
00350    // nlist-=sizeof(int);
00351   // get record size
00352   int num_bytes=nlist.bptr->numbytes;
00353   // backup the size of the record
00354   list-=num_bytes;
00355   list.saveposition(); // save pointer to beginning of record;
00356   // save the pointer to the beginning of the record
00357   double yu;
00358   //df1b2_header z;
00359   //df1b2function2 * pf;
00360 
00361   // get info from tape1
00362   // get info from tape1
00363 #if defined(SAFE_ALL)
00364   checkidentiferstring("DL",list);
00365 #endif
00366   double xu=*(double*) list.bptr;
00367   list.bptr+=sizeof(double);
00368   df1b2_header * py=(df1b2_header *) list.bptr;
00369   list.bptr+=sizeof(df1b2_header);
00370   df1b2_header * pz=(df1b2_header *) list.bptr;
00371   list.bptr+=sizeof(df1b2_header);
00372   memcpy(&yu,list.bptr,sizeof(double));
00373   list.bptr+=sizeof(double);
00374 
00375   //double * ydot = (double*)list.bptr;
00376 
00377   list.restoreposition(); // save pointer to beginning of record;
00378   int i;
00379 
00380   *(py->u_tilde)+=xu* *(pz->u_tilde);
00381   for (i=0;i<nvar;i++)
00382   {
00383     py->u_dot_tilde[i]+=xu*pz->u_dot_tilde[i];
00384   }
00385   *(pz->u_tilde)=0;
00386   for (i=0;i<nvar;i++)
00387   {
00388     pz->u_dot_tilde[i]=0;
00389   }
00390 }