ADMB Documentation  11.1.1916
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2prb.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2prb.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 
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_ARRAYS)
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);
00152   //double * xdot=(double*)bptr;
00153 
00154   list.restoreposition(); // save pointer to beginning of record;
00155 
00156   // ****************************************************************
00157   // turn this off if no third derivatives are calculated
00158   // if (!no_third_derivatives)
00159   // {
00160   // save for second reverse pass
00161   // save identifier 1
00162      test_smartlist & list2 = f1b2gradlist->list2;
00163 
00164 
00165    int total_bytes=2*nvar*sizeof(double);
00166 // string identifier debug stuff
00167 #if defined(SAFE_ALL)
00168   char ids[]="QK";
00169   int slen=strlen(ids);
00170   total_bytes+=slen;
00171 #endif
00172   list2.check_buffer_size(total_bytes);
00173   void * tmpptr=list2.bptr;
00174 #if defined(SAFE_ALL)
00175   memcpy(list2,ids,slen);
00176 #endif
00177 
00178   fixed_smartlist2 & nlist2 = f1b2gradlist->nlist2;
00179   memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
00180   memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
00181   *nlist2.bptr=adptr_diff(list2.bptr,tmpptr);
00182   ++nlist2;
00183 
00184   // Do first reverse pass calculations
00185   int i;
00186   for (i=0;i<nvar;i++)
00187   {
00188     px->u_bar[i]+=yu*pz->u_bar[i];
00189   }
00190 
00191   for (i=0;i<nvar;i++)
00192   {
00193     px->u_dot_bar[i]+=yu*pz->u_dot_bar[i];
00194   }
00195 
00196   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00197   for (i=0;i<nvar;i++)
00198   {
00199     pz->u_bar[i]=0;
00200   }
00201   for (i=0;i<nvar;i++)
00202   {
00203     pz->u_dot_bar[i]=0;
00204   }
00205 }
00206 
00211 void read_pass2_2_prodc2(void)
00212 {
00213   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00214   // We are going forward for bptr and backword for bptr2
00215   //
00216   // list 1
00217   //
00218   int nvar=df1b2variable::nvar;
00219   test_smartlist & list=f1b2gradlist->list;
00220 
00221    int total_bytes=2*sizeof(df1b2_header)
00222      +(nvar+2)*sizeof(double);
00223 // string identifier debug stuff
00224 #if defined(SAFE_ALL)
00225   char ids[]="DL";
00226   int slen=strlen(ids);
00227   total_bytes+=slen;
00228 #endif
00229   list.check_buffer_size(total_bytes);
00230 // end of string identifier debug stuff
00231 
00232   list.saveposition(); // save pointer to beginning of record;
00233   fixed_smartlist & nlist=f1b2gradlist->nlist;
00234    // nlist-=sizeof(int);
00235   // get record size
00236   int num_bytes=nlist.bptr->numbytes;
00237   //
00238   // list 2
00239   //
00240   test_smartlist & list2=f1b2gradlist->list2;
00241   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00242   // get record size
00243   int num_bytes2=*nlist2.bptr;
00244   --nlist2;
00245   // backup the size of the record
00246   list2-=num_bytes2;
00247   list2.saveposition(); // save pointer to beginning of record;
00248   // save the pointer to the beginning of the record
00249   // bptr and bptr2 now both point to the beginning of their records
00250 
00251   double xu;
00252   //df1b2_header x,z;
00253   //df1b2function2 * pf;
00254 
00255   // get info from tape1
00256   // get info from tape1
00257 #if defined(SAFE_ARRAYS)
00258   checkidentiferstring("DL",list);
00259   checkidentiferstring("QK",list2);
00260 #endif
00261   df1b2_header * px=(df1b2_header *) list.bptr;
00262   list.bptr+=sizeof(df1b2_header);
00263   double yu=*(double *) list.bptr;
00264   list.bptr+=sizeof(double);
00265   df1b2_header * pz=(df1b2_header *) list.bptr;
00266   list.bptr+=sizeof(df1b2_header);
00267   memcpy(&xu,list.bptr,sizeof(double));
00268   list.bptr+=sizeof(double);
00269   //double* xdot=(double*)list.bptr;
00270   list.restoreposition(num_bytes); // save pointer to beginning of record;
00271 
00272   //double* zbar=(double*)list2.bptr;
00273   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00274 
00275   list2.restoreposition(); // save pointer to beginning of record;
00276 
00277   //double * x_tilde=px->get_u_tilde();
00278   //double * x_dot_tilde=px->get_u_dot_tilde();
00279   double * x_bar_tilde=px->get_u_bar_tilde();
00280   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00281   double * z_bar_tilde=pz->get_u_bar_tilde();
00282   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00283   // Do second "reverse-reverse" pass calculations
00284 
00285   int i;
00286 
00287   for (i=0;i<nvar;i++)
00288   {
00289     z_bar_tilde[i]=0;
00290     z_dot_bar_tilde[i]=0;
00291   }
00292 
00293   // start with y and add x
00294   for (i=0;i<nvar;i++)
00295   {
00296     z_bar_tilde[i]+=yu*x_bar_tilde[i];
00297   }
00298 
00299   for (i=0;i<nvar;i++)
00300   {
00301     z_dot_bar_tilde[i]+=yu*x_dot_bar_tilde[i];
00302   }
00303 }
00304 
00309 void read_pass2_3_prodc2(void)
00310 {
00311   // We are going backword for bptr and forward for bptr2
00312   // the current entry+2 in bptr is the size of the record i.e
00313   // points to the next record
00314   int nvar=df1b2variable::nvar;
00315   fixed_smartlist & nlist=f1b2gradlist->nlist;
00316   test_smartlist& list=f1b2gradlist->list;
00317    // nlist-=sizeof(int);
00318   // get record size
00319   int num_bytes=nlist.bptr->numbytes;
00320   // backup the size of the record
00321   list-=num_bytes;
00322   list.saveposition(); // save pointer to beginning of record;
00323   // save the pointer to the beginning of the record
00324   double xu;
00325   //df1b2_header z;
00326   //df1b2function2 * pf;
00327 
00328   // get info from tape1
00329   // get info from tape1
00330 #if defined(SAFE_ARRAYS)
00331   checkidentiferstring("DL",list);
00332 #endif
00333   df1b2_header * px=(df1b2_header *) list.bptr;
00334   list.bptr+=sizeof(df1b2_header);
00335   double yu=*(double*) list.bptr;
00336   list.bptr+=sizeof(double);
00337   df1b2_header * pz=(df1b2_header *) list.bptr;
00338   list.bptr+=sizeof(df1b2_header);
00339   memcpy(&xu,list.bptr,sizeof(double));
00340   list.bptr+=sizeof(double);
00341   //double* xdot=(double*)list.bptr;
00342   list.restoreposition(); // save pointer to beginning of record;
00343   int i;
00344 
00345   *(px->u_tilde)+=yu* *(pz->u_tilde);
00346   for (i=0;i<nvar;i++)
00347   {
00348     px->u_dot_tilde[i]+=yu*pz->u_dot_tilde[i];
00349   }
00350   *(pz->u_tilde)=0;
00351   for (i=0;i<nvar;i++)
00352   {
00353     pz->u_dot_tilde[i]=0;
00354   }
00355 }