ADMB Documentation  11.1.2274
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2prb.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2prb.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 
00017   df1b2variable operator * (const df1b2variable& _x, double y)
00018   {
00019     ADUNCONST(df1b2variable,x)
00020     df1b2variable z;
00021     double * xd=x.get_u_dot();
00022     double * zd=z.get_u_dot();
00023     double xu=*x.get_u();
00024 
00025     *z.get_u()=y*xu;
00026 
00027     for (int i=0;i<df1b2variable::nvar;i++)
00028     {
00029       *zd++ = y * *xd++;
00030     }
00031 
00032     // WRITE WHATEVER ON TAPE
00033     if (!df1b2_gradlist::no_derivatives)
00034       f1b2gradlist->write_pass1_prod(&x,y,&z);
00035     return z;
00036   }
00037 
00038 void ad_read_pass2_prodc2(void);
00039 
00044  int df1b2_gradlist::write_pass1_prod(const df1b2variable * _px,double y,
00045    df1b2variable * pz)
00046  {
00047    ADUNCONST(df1b2variable*,px)
00048    ncount++;
00049 #if defined(CHECK_COUNT)
00050   if (ncount >= ncount_check)
00051     cout << ncount << endl;
00052 #endif
00053    int nvar=df1b2variable::nvar;
00054 
00055    //int total_bytes=3*sizeof(df1b2_header)+sizeof(char*)
00056    //  +2*(nvar+1)*sizeof(double);
00057    int total_bytes=2*sizeof(df1b2_header)
00058      +(nvar+2)*sizeof(double);
00059 // string identifier debug stuff
00060 #if defined(SAFE_ALL)
00061   char ids[]="DL";
00062   int slen=strlen(ids);
00063   total_bytes+=slen;
00064 #endif
00065   list.check_buffer_size(total_bytes);
00066   void * tmpptr=list.bptr;
00067 #if defined(SAFE_ALL)
00068   memcpy(list,ids,slen);
00069 #endif
00070 // end of string identifier debug stuff
00071 
00072    memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
00073    memcpy(list,&y,sizeof(double));
00074    memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00075    memcpy(list,px->get_u(),sizeof(double));
00076    memcpy(list,px->get_u_dot(),nvar*sizeof(double));
00077    // ***** write  record size
00078    nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00079    if (total_bytes != nlist.bptr->numbytes)
00080    {
00081      cerr << "error in byte calculation in "
00082        " write_pass1_prod" << endl;
00083      ad_exit(1);
00084    }
00085    nlist.bptr->pf=(ADrfptr)(&ad_read_pass2_prodc2);
00086       ++nlist;
00087    return 0;
00088  }
00089 
00090 
00091 void read_pass2_1_prodc2(void);
00092 void read_pass2_2_prodc2(void);
00093 void read_pass2_3_prodc2(void);
00094 
00099 void ad_read_pass2_prodc2(void)
00100 {
00101   switch(df1b2variable::passnumber)
00102   {
00103   case 1:
00104     read_pass2_1_prodc2();
00105     break;
00106   case 2:
00107     read_pass2_2_prodc2();
00108     break;
00109   case 3:
00110     read_pass2_3_prodc2();
00111     break;
00112   default:
00113     cerr << "illegal value for df1b2variable::pass = "
00114          << df1b2variable::passnumber << endl;
00115     exit(1);
00116   }
00117 }
00118 
00123 void read_pass2_1_prodc2(void)
00124 {
00125   // We are going backword for bptr and nbptr
00126   // and  forward for bptr2 and nbptr2
00127   // the current entry+2 in bptr is the size of the record i.e
00128   // points to the next record
00129   //char * bptr=f1b2gradlist->bptr;
00130   //char * bptr2=f1b2gradlist2->bptr;
00131   int nvar=df1b2variable::nvar;
00132   test_smartlist& list=f1b2gradlist->list;
00133   //f1b2gradlist->nlist-=sizeof(int);
00134   int num_bytes=f1b2gradlist->nlist.bptr->numbytes;
00135   list-=num_bytes;
00136   list.saveposition(); // save pointer to beginning of record;
00137   double xu;
00138 
00139   // get info from tape1
00140 #if defined(SAFE_ALL)
00141   checkidentiferstring("DL",f1b2gradlist->list);
00142 #endif
00143   char * bptr=f1b2gradlist->list.bptr;
00144   df1b2_header * px=(df1b2_header *) bptr;
00145   bptr+=sizeof(df1b2_header);
00146   double yu=*(double *) bptr;
00147   bptr+=sizeof(double);
00148   df1b2_header * pz=(df1b2_header *) bptr;
00149   bptr+=sizeof(df1b2_header);
00150   memcpy(&xu,bptr,sizeof(double));
00151   //bptr+=sizeof(double); double* xdot=(double*)bptr;
00152 
00153   list.restoreposition(); // save pointer to beginning of record;
00154 
00155   // ****************************************************************
00156   // turn this off if no third derivatives are calculated
00157   // if (!no_third_derivatives)
00158   // {
00159   // save for second reverse pass
00160   // save identifier 1
00161      test_smartlist & list2 = f1b2gradlist->list2;
00162 
00163 
00164    int total_bytes=2*nvar*sizeof(double);
00165 // string identifier debug stuff
00166 #if defined(SAFE_ALL)
00167   char ids[]="QK";
00168   int slen=strlen(ids);
00169   total_bytes+=slen;
00170 #endif
00171   list2.check_buffer_size(total_bytes);
00172   void * tmpptr=list2.bptr;
00173 #if defined(SAFE_ALL)
00174   memcpy(list2,ids,slen);
00175 #endif
00176 
00177   fixed_smartlist2 & nlist2 = f1b2gradlist->nlist2;
00178   memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
00179   memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
00180   *nlist2.bptr=adptr_diff(list2.bptr,tmpptr);
00181   ++nlist2;
00182 
00183   // Do first reverse pass calculations
00184   int i;
00185   for (i=0;i<nvar;i++)
00186   {
00187     px->u_bar[i]+=yu*pz->u_bar[i];
00188   }
00189 
00190   for (i=0;i<nvar;i++)
00191   {
00192     px->u_dot_bar[i]+=yu*pz->u_dot_bar[i];
00193   }
00194 
00195   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00196   for (i=0;i<nvar;i++)
00197   {
00198     pz->u_bar[i]=0;
00199   }
00200   for (i=0;i<nvar;i++)
00201   {
00202     pz->u_dot_bar[i]=0;
00203   }
00204 }
00205 
00210 void read_pass2_2_prodc2(void)
00211 {
00212   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00213   // We are going forward for bptr and backword for bptr2
00214   //
00215   // list 1
00216   //
00217   int nvar=df1b2variable::nvar;
00218   test_smartlist & list=f1b2gradlist->list;
00219 
00220    int total_bytes=2*sizeof(df1b2_header)
00221      +(nvar+2)*sizeof(double);
00222 // string identifier debug stuff
00223 #if defined(SAFE_ALL)
00224   char ids[]="DL";
00225   int slen=strlen(ids);
00226   total_bytes+=slen;
00227 #endif
00228   list.check_buffer_size(total_bytes);
00229 // end of string identifier debug stuff
00230 
00231   list.saveposition(); // save pointer to beginning of record;
00232   fixed_smartlist & nlist=f1b2gradlist->nlist;
00233    // nlist-=sizeof(int);
00234   // get record size
00235   int num_bytes=nlist.bptr->numbytes;
00236   //
00237   // list 2
00238   //
00239   test_smartlist & list2=f1b2gradlist->list2;
00240   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00241   // get record size
00242   int num_bytes2=*nlist2.bptr;
00243   --nlist2;
00244   // backup the size of the record
00245   list2-=num_bytes2;
00246   list2.saveposition(); // save pointer to beginning of record;
00247   // save the pointer to the beginning of the record
00248   // bptr and bptr2 now both point to the beginning of their records
00249 
00250   double xu;
00251   //df1b2_header x,z;
00252   //df1b2function2 * pf;
00253 
00254   // get info from tape1
00255   // get info from tape1
00256 #if defined(SAFE_ALL)
00257   checkidentiferstring("DL",list);
00258   checkidentiferstring("QK",list2);
00259 #endif
00260   df1b2_header * px=(df1b2_header *) list.bptr;
00261   list.bptr+=sizeof(df1b2_header);
00262   double yu=*(double *) list.bptr;
00263   list.bptr+=sizeof(double);
00264   df1b2_header * pz=(df1b2_header *) list.bptr;
00265   list.bptr+=sizeof(df1b2_header);
00266   memcpy(&xu,list.bptr,sizeof(double));
00267   list.bptr+=sizeof(double);
00268   //double* xdot=(double*)list.bptr;
00269   list.restoreposition(num_bytes); // save pointer to beginning of record;
00270 
00271   //double* zbar=(double*)list2.bptr;
00272   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00273 
00274   list2.restoreposition(); // save pointer to beginning of record;
00275 
00276   //double * x_tilde=px->get_u_tilde();
00277   //double * x_dot_tilde=px->get_u_dot_tilde();
00278   double * x_bar_tilde=px->get_u_bar_tilde();
00279   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00280   double * z_bar_tilde=pz->get_u_bar_tilde();
00281   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00282   // Do second "reverse-reverse" pass calculations
00283 
00284   int i;
00285 
00286   for (i=0;i<nvar;i++)
00287   {
00288     z_bar_tilde[i]=0;
00289     z_dot_bar_tilde[i]=0;
00290   }
00291 
00292   // start with y and add x
00293   for (i=0;i<nvar;i++)
00294   {
00295     z_bar_tilde[i]+=yu*x_bar_tilde[i];
00296   }
00297 
00298   for (i=0;i<nvar;i++)
00299   {
00300     z_dot_bar_tilde[i]+=yu*x_dot_bar_tilde[i];
00301   }
00302 }
00303 
00308 void read_pass2_3_prodc2(void)
00309 {
00310   // We are going backword for bptr and forward for bptr2
00311   // the current entry+2 in bptr is the size of the record i.e
00312   // points to the next record
00313   int nvar=df1b2variable::nvar;
00314   fixed_smartlist & nlist=f1b2gradlist->nlist;
00315   test_smartlist& list=f1b2gradlist->list;
00316    // nlist-=sizeof(int);
00317   // get record size
00318   int num_bytes=nlist.bptr->numbytes;
00319   // backup the size of the record
00320   list-=num_bytes;
00321   list.saveposition(); // save pointer to beginning of record;
00322   // save the pointer to the beginning of the record
00323   double xu;
00324   //df1b2_header z;
00325   //df1b2function2 * pf;
00326 
00327   // get info from tape1
00328   // get info from tape1
00329 #if defined(SAFE_ALL)
00330   checkidentiferstring("DL",list);
00331 #endif
00332   df1b2_header * px=(df1b2_header *) list.bptr;
00333   list.bptr+=sizeof(df1b2_header);
00334   double yu=*(double*) list.bptr;
00335   list.bptr+=sizeof(double);
00336   df1b2_header * pz=(df1b2_header *) list.bptr;
00337   list.bptr+=sizeof(df1b2_header);
00338   memcpy(&xu,list.bptr,sizeof(double));
00339   list.bptr+=sizeof(double);
00340   //double* xdot=(double*)list.bptr;
00341   list.restoreposition(); // save pointer to beginning of record;
00342   int i;
00343 
00344   *(px->u_tilde)+=yu* *(pz->u_tilde);
00345   for (i=0;i<nvar;i++)
00346   {
00347     px->u_dot_tilde[i]+=yu*pz->u_dot_tilde[i];
00348   }
00349   *(pz->u_tilde)=0;
00350   for (i=0;i<nvar;i++)
00351   {
00352     pz->u_dot_tilde[i]=0;
00353   }
00354 }