ADMB Documentation  11.1x.2711
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2sum.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2sum.cpp 2601 2014-11-08 22:34:58Z 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, const df1b2variable& _y)
00018   {
00019     ADUNCONST(df1b2variable,x)
00020     ADUNCONST(df1b2variable,y)
00021     df1b2variable z;
00022     double * xd=x.get_u_dot();
00023     double * yd=y.get_u_dot();
00024     double * zd=z.get_u_dot();
00025     *z.get_u()=*x.get_u()+*y.get_u();
00026     for (unsigned int i=0;i<df1b2variable::nvar;i++)
00027     {
00028       *zd++ = *xd++ + *yd++;
00029     }
00030 
00031     // WRITE WHATEVER ON TAPE
00032     if (!df1b2_gradlist::no_derivatives)
00033       f1b2gradlist->write_pass1_sum(&x,&y,&z);
00034     return z;
00035   }
00036 
00037 void ad_read_pass2_sum(void);
00038 
00043  int df1b2_gradlist::write_pass1_sum(const df1b2variable * _px,
00044    const df1b2variable * _py,df1b2variable * pz)
00045  {
00046    ADUNCONST(df1b2variable*,px)
00047    ADUNCONST(df1b2variable*,py)
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    size_t total_bytes=3*sizeof(df1b2_header);
00056 // string identifier debug stuff
00057 #if defined(SAFE_ALL)
00058   char ids[]="TU";
00059   int slen=strlen(ids);
00060   total_bytes+=slen;
00061 #endif
00062   list.check_buffer_size(total_bytes);
00063   void * tmpptr=list.bptr;
00064 #if defined(SAFE_ALL)
00065   memcpy(list,ids,slen);
00066 #endif
00067 // end of string identifier debug stuff
00068 
00069    memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
00070    memcpy(list,(df1b2_header*)(py),sizeof(df1b2_header));
00071    memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00072    // ***** write  record size
00073    nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00074    nlist.bptr->pf=(ADrfptr)(&ad_read_pass2_sum);
00075       ++nlist;
00076   return 0;
00077  }
00078 
00079 
00080 void read_pass2_1_sum(void);
00081 void read_pass2_2_sum(void);
00082 void read_pass2_3_sum(void);
00083 
00088 void ad_read_pass2_sum(void)
00089 {
00090   switch(df1b2variable::passnumber)
00091   {
00092   case 1:
00093     read_pass2_1_sum();
00094     break;
00095   case 2:
00096     read_pass2_2_sum();
00097     break;
00098   case 3:
00099     read_pass2_3_sum();
00100     break;
00101   default:
00102     cerr << "illegal value for df1b2variable::pass = "
00103          << df1b2variable::passnumber << endl;
00104     exit(1);
00105   }
00106 }
00107 
00112 void read_pass2_1_sum(void)
00113 {
00114   // We are going backword for bptr and nbptr
00115   // and  forward for bptr2 and nbptr2
00116   // the current entry+2 in bptr is the size of the record i.e
00117   // points to the next record
00118   unsigned int nvar=df1b2variable::nvar;
00119   test_smartlist& list=f1b2gradlist->list;
00120   int num_bytes=f1b2gradlist->nlist.bptr->numbytes;
00121   list-=num_bytes;
00122   list.saveposition(); // save pointer to beginning of record;
00123 
00124   // get info from tape1
00125 #if defined(SAFE_ALL)
00126   checkidentiferstring("TU",f1b2gradlist->list);
00127 #endif
00128   char * bptr=f1b2gradlist->list.bptr;
00129   df1b2_header * px=(df1b2_header *) bptr;
00130   bptr+=sizeof(df1b2_header);
00131   df1b2_header * py=(df1b2_header *) bptr;
00132   bptr+=sizeof(df1b2_header);
00133   df1b2_header * pz=(df1b2_header *) bptr;
00134 
00135   list.restoreposition(); // save pointer to beginning of record;
00136 
00137   // ****************************************************************
00138   // turn this off if no third derivatives are calculated
00139   // if (!no_third_derivatives)
00140   // {
00141   // save for second reverse pass
00142   // save identifier 1
00143 
00144   for (unsigned int i=0;i<nvar;i++)
00145   {
00146     px->u_bar[i]+=pz->u_bar[i];
00147   }
00148   for (unsigned int i=0;i<nvar;i++)
00149   {
00150     py->u_bar[i]+=pz->u_bar[i];
00151   }
00152   for (unsigned int i=0;i<nvar;i++)
00153   {
00154     px->u_dot_bar[i]+=pz->u_dot_bar[i];
00155   }
00156   for (unsigned int i=0;i<nvar;i++)
00157   {
00158     py->u_dot_bar[i]+=pz->u_dot_bar[i];
00159   }
00160 
00161   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00162   for (unsigned int i=0;i<nvar;i++)
00163   {
00164     pz->u_bar[i]=0;
00165   }
00166   for (unsigned int i=0;i<nvar;i++)
00167   {
00168     pz->u_dot_bar[i]=0;
00169   }
00170 }
00171 
00176 void read_pass2_2_sum(void)
00177 {
00178   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00179   // We are going forward for bptr and backword for bptr2
00180   //
00181   // list 1
00182   //
00183   unsigned int nvar=df1b2variable::nvar;
00184   test_smartlist & list=f1b2gradlist->list;
00185 
00186   size_t total_bytes=3*sizeof(df1b2_header);
00187 // string identifier debug stuff
00188 #if defined(SAFE_ALL)
00189   char ids[]="BY";
00190   int slen=strlen(ids);
00191   total_bytes+=slen;
00192 #endif
00193   list.check_buffer_size(total_bytes);
00194 // end of string identifier debug stuff
00195 
00196   list.saveposition(); // save pointer to beginning of record;
00197   fixed_smartlist & nlist=f1b2gradlist->nlist;
00198   // get record size
00199   int num_bytes=nlist.bptr->numbytes;
00200   // get info from tape1
00201 #if defined(SAFE_ALL)
00202   checkidentiferstring("TU",list);
00203 #endif
00204   df1b2_header * px=(df1b2_header *) list.bptr;
00205   list.bptr+=sizeof(df1b2_header);
00206   df1b2_header * py=(df1b2_header *) list.bptr;
00207   list.bptr+=sizeof(df1b2_header);
00208   df1b2_header * pz=(df1b2_header *) list.bptr;
00209   list.restoreposition(num_bytes); // save pointer to beginning of record;
00210 
00211   double * x_bar_tilde=px->get_u_bar_tilde();
00212   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00213   double * y_bar_tilde=py->get_u_bar_tilde();
00214   double * y_dot_bar_tilde=py->get_u_dot_bar_tilde();
00215   double * z_bar_tilde=pz->get_u_bar_tilde();
00216   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00217   // Do second "reverse-reverse" pass calculations
00218 
00219   for (unsigned int i=0;i<nvar;i++)
00220   {
00221     z_bar_tilde[i]=0;
00222     z_dot_bar_tilde[i]=0;
00223   }
00224 
00225   // start with x and add y
00226   for (unsigned int i=0;i<nvar;i++)
00227   {
00228     z_bar_tilde[i]+=x_bar_tilde[i];
00229   }
00230 
00231   for (unsigned int i=0;i<nvar;i++)
00232   {
00233     z_dot_bar_tilde[i]+=x_dot_bar_tilde[i];
00234   }
00235 
00236   // start with y and add x
00237   for (unsigned int i=0;i<nvar;i++)
00238   {
00239     z_bar_tilde[i]+=y_bar_tilde[i];
00240   }
00241 
00242   for (unsigned int i=0;i<nvar;i++)
00243   {
00244     z_dot_bar_tilde[i]+=y_dot_bar_tilde[i];
00245   }
00246 }
00247 
00252 void read_pass2_3_sum(void)
00253 {
00254   // We are going backword for bptr and forward for bptr2
00255   // the current entry+2 in bptr is the size of the record i.e
00256   // points to the next record
00257   unsigned int nvar=df1b2variable::nvar;
00258   fixed_smartlist & nlist=f1b2gradlist->nlist;
00259   test_smartlist& list=f1b2gradlist->list;
00260    // nlist-=sizeof(int);
00261   // get record size
00262   int num_bytes=nlist.bptr->numbytes;
00263   // backup the size of the record
00264   list-=num_bytes;
00265   list.saveposition(); // save pointer to beginning of record;
00266   // save the pointer to the beginning of the record
00267   //df1b2_header x,z;
00268 
00269   // get info from tape1
00270   // get info from tape1
00271 #if defined(SAFE_ALL)
00272   checkidentiferstring("TU",list);
00273 #endif
00274   df1b2_header * px=(df1b2_header *) list.bptr;
00275   list.bptr+=sizeof(df1b2_header);
00276   df1b2_header * py=(df1b2_header *) list.bptr;
00277   list.bptr+=sizeof(df1b2_header);
00278   df1b2_header * pz=(df1b2_header *) list.bptr;
00279   list.bptr+=sizeof(df1b2_header);
00280 
00281   list.restoreposition(); // save pointer to beginning of record;
00282 
00283   *(px->u_tilde)+=*(pz->u_tilde);
00284   *(py->u_tilde)+=*(pz->u_tilde);
00285   for (unsigned int i=0;i<nvar;i++)
00286   {
00287     px->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00288     py->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00289   }
00290   *(pz->u_tilde)=0;
00291   for (unsigned int i=0;i<nvar;i++)
00292   {
00293     pz->u_dot_tilde[i]=0;
00294   }
00295 }