ADMB Documentation  11.1.2192
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2sum.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2sum.cpp 1919 2014-04-22 22:02:01Z 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 (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    int 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   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   int i;
00145   for (i=0;i<nvar;i++)
00146   {
00147     px->u_bar[i]+=pz->u_bar[i];
00148   }
00149   for (i=0;i<nvar;i++)
00150   {
00151     py->u_bar[i]+=pz->u_bar[i];
00152   }
00153   for (i=0;i<nvar;i++)
00154   {
00155     px->u_dot_bar[i]+=pz->u_dot_bar[i];
00156   }
00157   for (i=0;i<nvar;i++)
00158   {
00159     py->u_dot_bar[i]+=pz->u_dot_bar[i];
00160   }
00161 
00162   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00163   for (i=0;i<nvar;i++)
00164   {
00165     pz->u_bar[i]=0;
00166   }
00167   for (i=0;i<nvar;i++)
00168   {
00169     pz->u_dot_bar[i]=0;
00170   }
00171 }
00172 
00177 void read_pass2_2_sum(void)
00178 {
00179   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00180   // We are going forward for bptr and backword for bptr2
00181   //
00182   // list 1
00183   //
00184   int nvar=df1b2variable::nvar;
00185   test_smartlist & list=f1b2gradlist->list;
00186 
00187   int total_bytes=3*sizeof(df1b2_header);
00188 // string identifier debug stuff
00189 #if defined(SAFE_ALL)
00190   char ids[]="BY";
00191   int slen=strlen(ids);
00192   total_bytes+=slen;
00193 #endif
00194   list.check_buffer_size(total_bytes);
00195 // end of string identifier debug stuff
00196 
00197   list.saveposition(); // save pointer to beginning of record;
00198   fixed_smartlist & nlist=f1b2gradlist->nlist;
00199   // get record size
00200   int num_bytes=nlist.bptr->numbytes;
00201   // get info from tape1
00202 #if defined(SAFE_ALL)
00203   checkidentiferstring("TU",list);
00204 #endif
00205   df1b2_header * px=(df1b2_header *) list.bptr;
00206   list.bptr+=sizeof(df1b2_header);
00207   df1b2_header * py=(df1b2_header *) list.bptr;
00208   list.bptr+=sizeof(df1b2_header);
00209   df1b2_header * pz=(df1b2_header *) list.bptr;
00210   list.restoreposition(num_bytes); // save pointer to beginning of record;
00211 
00212   double * x_bar_tilde=px->get_u_bar_tilde();
00213   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00214   double * y_bar_tilde=py->get_u_bar_tilde();
00215   double * y_dot_bar_tilde=py->get_u_dot_bar_tilde();
00216   double * z_bar_tilde=pz->get_u_bar_tilde();
00217   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00218   // Do second "reverse-reverse" pass calculations
00219   int i;
00220 
00221   for (i=0;i<nvar;i++)
00222   {
00223     z_bar_tilde[i]=0;
00224     z_dot_bar_tilde[i]=0;
00225   }
00226 
00227   // start with x and add y
00228   for (i=0;i<nvar;i++)
00229   {
00230     z_bar_tilde[i]+=x_bar_tilde[i];
00231   }
00232 
00233   for (i=0;i<nvar;i++)
00234   {
00235     z_dot_bar_tilde[i]+=x_dot_bar_tilde[i];
00236   }
00237 
00238   // start with y and add x
00239   for (i=0;i<nvar;i++)
00240   {
00241     z_bar_tilde[i]+=y_bar_tilde[i];
00242   }
00243 
00244   for (i=0;i<nvar;i++)
00245   {
00246     z_dot_bar_tilde[i]+=y_dot_bar_tilde[i];
00247   }
00248 }
00249 
00254 void read_pass2_3_sum(void)
00255 {
00256   // We are going backword for bptr and forward for bptr2
00257   // the current entry+2 in bptr is the size of the record i.e
00258   // points to the next record
00259   int nvar=df1b2variable::nvar;
00260   fixed_smartlist & nlist=f1b2gradlist->nlist;
00261   test_smartlist& list=f1b2gradlist->list;
00262    // nlist-=sizeof(int);
00263   // get record size
00264   int num_bytes=nlist.bptr->numbytes;
00265   // backup the size of the record
00266   list-=num_bytes;
00267   list.saveposition(); // save pointer to beginning of record;
00268   // save the pointer to the beginning of the record
00269   //df1b2_header x,z;
00270 
00271   // get info from tape1
00272   // get info from tape1
00273 #if defined(SAFE_ALL)
00274   checkidentiferstring("TU",list);
00275 #endif
00276   df1b2_header * px=(df1b2_header *) list.bptr;
00277   list.bptr+=sizeof(df1b2_header);
00278   df1b2_header * py=(df1b2_header *) list.bptr;
00279   list.bptr+=sizeof(df1b2_header);
00280   df1b2_header * pz=(df1b2_header *) list.bptr;
00281   list.bptr+=sizeof(df1b2_header);
00282 
00283   list.restoreposition(); // save pointer to beginning of record;
00284   int i;
00285 
00286   *(px->u_tilde)+=*(pz->u_tilde);
00287   *(py->u_tilde)+=*(pz->u_tilde);
00288   for (i=0;i<nvar;i++)
00289   {
00290     px->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00291     py->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00292   }
00293   *(pz->u_tilde)=0;
00294   for (i=0;i<nvar;i++)
00295   {
00296     pz->u_dot_tilde[i]=0;
00297   }
00298 }