ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2sumc.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
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 + (double x, const df1b2variable& _y)
00018   {
00019     ADUNCONST(df1b2variable,y)
00020     df1b2variable z;
00021     double * yd=y.get_u_dot();
00022     double * zd=z.get_u_dot();
00023     *z.get_u()=x+*y.get_u();
00024     for (unsigned int i=0;i<df1b2variable::nvar;i++)
00025     {
00026       *zd++ = *yd++;
00027     }
00028 
00029     // WRITE WHATEVER ON TAPE
00030     if (!df1b2_gradlist::no_derivatives)
00031       f1b2gradlist->write_pass1_sum(x,&y,&z);
00032     return z;
00033   }
00034 
00035 void ad_read_pass2_sumc(void);
00036 
00041  int df1b2_gradlist::write_pass1_sum(double x,const df1b2variable * _py,
00042    df1b2variable * pz)
00043  {
00044    ADUNCONST(df1b2variable*,py)
00045    ncount++;
00046 #if defined(CHECK_COUNT)
00047   if (ncount >= ncount_check)
00048     cout << ncount << endl;
00049 #endif
00050 
00051    size_t total_bytes=2*sizeof(df1b2_header);
00052 // string identifier debug stuff
00053 #if defined(SAFE_ALL)
00054   char ids[]="TU";
00055   int slen=strlen(ids);
00056   total_bytes+=slen;
00057 #endif
00058   list.check_buffer_size(total_bytes);
00059   void * tmpptr=list.bptr;
00060 #if defined(SAFE_ALL)
00061   memcpy(list,ids,slen);
00062 #endif
00063 // end of string identifier debug stuff
00064 
00065    memcpy(list,(df1b2_header*)(py),sizeof(df1b2_header));
00066    memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00067    // ***** write  record size
00068    nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00069    nlist.bptr->pf=(ADrfptr)(&ad_read_pass2_sumc);
00070       ++nlist;
00071   return 0;
00072  }
00073 
00074 
00075 void read_pass2_1_sumc(void);
00076 void read_pass2_2_sumc(void);
00077 void read_pass2_3_sumc(void);
00078 
00083 void ad_read_pass2_sumc(void)
00084 {
00085   switch(df1b2variable::passnumber)
00086   {
00087   case 1:
00088     read_pass2_1_sumc();
00089     break;
00090   case 2:
00091     read_pass2_2_sumc();
00092     break;
00093   case 3:
00094     read_pass2_3_sumc();
00095     break;
00096   default:
00097     cerr << "illegal value for df1b2variable::pass = "
00098          << df1b2variable::passnumber << endl;
00099     exit(1);
00100   }
00101 }
00102 
00107 void read_pass2_1_sumc(void)
00108 {
00109   // We are going backword for bptr and nbptr
00110   // and  forward for bptr2 and nbptr2
00111   // the current entry+2 in bptr is the size of the record i.e
00112   // points to the next record
00113   unsigned int nvar=df1b2variable::nvar;
00114   test_smartlist& list=f1b2gradlist->list;
00115   int num_bytes=f1b2gradlist->nlist.bptr->numbytes;
00116   list-=num_bytes;
00117   list.saveposition(); // save pointer to beginning of record;
00118 
00119   // get info from tape1
00120 #if defined(SAFE_ALL)
00121   checkidentiferstring("TU",f1b2gradlist->list);
00122 #endif
00123   char * bptr=f1b2gradlist->list.bptr;
00124   df1b2_header * py=(df1b2_header *) bptr;
00125   bptr+=sizeof(df1b2_header);
00126   df1b2_header * pz=(df1b2_header *) bptr;
00127 
00128   list.restoreposition(); // save pointer to beginning of record;
00129 
00130   // ****************************************************************
00131   // turn this off if no third derivatives are calculated
00132   // if (!no_third_derivatives)
00133   // {
00134   // save for second reverse pass
00135   // save identifier 1
00136 
00137   for (unsigned int i=0;i<nvar;i++)
00138   {
00139     py->u_bar[i]+=pz->u_bar[i];
00140   }
00141   for (unsigned int i=0;i<nvar;i++)
00142   {
00143     py->u_dot_bar[i]+=pz->u_dot_bar[i];
00144   }
00145 
00146   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00147   for (unsigned int i=0;i<nvar;i++)
00148   {
00149     pz->u_bar[i]=0;
00150   }
00151   for (unsigned int i=0;i<nvar;i++)
00152   {
00153     pz->u_dot_bar[i]=0;
00154   }
00155 }
00156 
00161 void read_pass2_2_sumc(void)
00162 {
00163   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00164   // We are going forward for bptr and backword for bptr2
00165   //
00166   // list 1
00167   //
00168   unsigned int nvar=df1b2variable::nvar;
00169   test_smartlist & list=f1b2gradlist->list;
00170 
00171   size_t total_bytes=2*sizeof(df1b2_header);
00172 // string identifier debug stuff
00173 #if defined(SAFE_ALL)
00174   char ids[]="BY";
00175   int slen=strlen(ids);
00176   total_bytes+=slen;
00177 #endif
00178   list.check_buffer_size(total_bytes);
00179 // end of string identifier debug stuff
00180 
00181   list.saveposition(); // save pointer to beginning of record;
00182   fixed_smartlist & nlist=f1b2gradlist->nlist;
00183   // get record size
00184   int num_bytes=nlist.bptr->numbytes;
00185   // get info from tape1
00186 #if defined(SAFE_ALL)
00187   checkidentiferstring("TU",list);
00188 #endif
00189   df1b2_header * py=(df1b2_header *) list.bptr;
00190   list.bptr+=sizeof(df1b2_header);
00191   df1b2_header * pz=(df1b2_header *) list.bptr;
00192   list.restoreposition(num_bytes); // save pointer to beginning of record;
00193 
00194   double * y_bar_tilde=py->get_u_bar_tilde();
00195   double * y_dot_bar_tilde=py->get_u_dot_bar_tilde();
00196   double * z_bar_tilde=pz->get_u_bar_tilde();
00197   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00198   // Do second "reverse-reverse" pass calculations
00199 
00200   for (unsigned int i=0;i<nvar;i++)
00201   {
00202     z_bar_tilde[i]=0;
00203     z_dot_bar_tilde[i]=0;
00204   }
00205 
00206   // start with y and add x
00207   for (unsigned int i=0;i<nvar;i++)
00208   {
00209     z_bar_tilde[i]+=y_bar_tilde[i];
00210   }
00211 
00212   for (unsigned int i=0;i<nvar;i++)
00213   {
00214     z_dot_bar_tilde[i]+=y_dot_bar_tilde[i];
00215   }
00216 }
00217 
00222 void read_pass2_3_sumc(void)
00223 {
00224   // We are going backword for bptr and forward for bptr2
00225   // the current entry+2 in bptr is the size of the record i.e
00226   // points to the next record
00227   unsigned int nvar=df1b2variable::nvar;
00228   fixed_smartlist & nlist=f1b2gradlist->nlist;
00229   test_smartlist& list=f1b2gradlist->list;
00230    // nlist-=sizeof(int);
00231   // get record size
00232   int num_bytes=nlist.bptr->numbytes;
00233   // backup the size of the record
00234   list-=num_bytes;
00235   list.saveposition(); // save pointer to beginning of record;
00236   // save the pointer to the beginning of the record
00237   //df1b2_header x,z;
00238 
00239   // get info from tape1
00240   // get info from tape1
00241 #if defined(SAFE_ALL)
00242   checkidentiferstring("TU",list);
00243 #endif
00244   df1b2_header * py=(df1b2_header *) list.bptr;
00245   list.bptr+=sizeof(df1b2_header);
00246   df1b2_header * pz=(df1b2_header *) list.bptr;
00247 
00248   list.restoreposition(); // save pointer to beginning of record;
00249 
00250   *(py->u_tilde)+=*(pz->u_tilde);
00251   for (unsigned int i=0;i<nvar;i++)
00252   {
00253     py->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00254   }
00255   *(pz->u_tilde)=0;
00256   for (unsigned int i=0;i<nvar;i++)
00257   {
00258     pz->u_dot_tilde[i]=0;
00259   }
00260 }