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