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