ADMB Documentation  11.1x.2706
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn8.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2fn8.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 void read_pass1_minus_eq_1(void);
00013 void read_pass1_minus_eq_2(void);
00014 void read_pass1_minus_eq_3(void);
00015 //#define ADDEBUG_PRINT
00016 #if defined(ADDEBUG_PRINT)
00017   extern int addebug_count;
00018 #endif
00019 //#define PRINT_DERS
00020 
00025 df1b2variable& df1b2variable::operator -= (const df1b2variable& _x)
00026 {
00027   ADUNCONST(df1b2variable,x)
00028   double * xd=x.get_u_dot();
00029   double * zd=get_u_dot();
00030   *get_u()-=*x.get_u();
00031   for (unsigned int i=0;i<df1b2variable::nvar;i++)
00032   {
00033     *zd++ -= *xd++;
00034   }
00035 
00036   // WRITE WHATEVER ON TAPE
00037   //df1b2tape->set_tapeinfo_header(&x,&z,this,xd);
00038   // save stuff for first reverse pass
00039   // need &x, &z, this,
00040   if (!df1b2_gradlist::no_derivatives)
00041     f1b2gradlist->write_pass1_minuseq(&x,this);
00042   return *this;
00043 }
00044 
00045 void ad_read_pass1_minus_eq(void);
00046 
00051 int df1b2_gradlist::write_pass1_minuseq(const df1b2variable * _px,
00052   df1b2variable * pz)
00053 {
00054   ncount++;
00055 #if defined(CHECK_COUNT)
00056   if (ncount >= ncount_check)
00057     ncount_checker(ncount,ncount_check);
00058 #endif
00059   //int nvar=df1b2variable::nvar;
00060   ADUNCONST(df1b2variable*,px)
00061   fixed_smartlist & nlist=f1b2gradlist->nlist;
00062   test_smartlist& list=f1b2gradlist->list;
00063 
00064   size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00065 #if defined(SAFE_ALL)
00066   char ids[]="LC";
00067   int slen=strlen(ids);
00068   total_bytes+=slen;
00069 #endif
00070   list.check_buffer_size(total_bytes);
00071   void * tmpptr=list.bptr;
00072 #if defined(SAFE_ALL)
00073   memcpy(list,ids,slen);
00074 #endif
00075 
00076   memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
00077   memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00078 
00079   // ***** write  record size
00080   nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00081   nlist.bptr->pf=(ADrfptr)(&ad_read_pass1_minus_eq);
00082   ++nlist;
00083   return 0;
00084 }
00085 
00090 void ad_read_pass1_minus_eq(void)
00091 {
00092   switch(df1b2variable::passnumber)
00093   {
00094   case 1:
00095     read_pass1_minus_eq_1();
00096     break;
00097   case 2:
00098     read_pass1_minus_eq_2();
00099     break;
00100   case 3:
00101     read_pass1_minus_eq_3();
00102     break;
00103   default:
00104     cerr << "illegal value for df1b2variable::pass = "
00105          << df1b2variable::passnumber << endl;
00106     exit(1);
00107   }
00108 }
00109 
00114 void read_pass1_minus_eq_1(void)
00115 {
00116   // We are going backword for bptr and forward for bptr2
00117   // the current entry+2 in bptr is the size of the record i.e
00118   // points to the next record
00119   unsigned int nvar=df1b2variable::nvar;
00120   fixed_smartlist & nlist=f1b2gradlist->nlist;
00121   test_smartlist& list=f1b2gradlist->list;
00122    // nlist-=sizeof(int);
00123   // get record size
00124   int num_bytes=nlist.bptr->numbytes;
00125   // backup the size of the record
00126   list-=num_bytes;
00127   list.saveposition(); // save pointer to beginning of record;
00128   // save the pointer to the beginning of the record
00129 #if defined(SAFE_ALL)
00130   checkidentiferstring("LC",list);
00131 #endif
00132 
00133   // get info from tape1
00134   df1b2_header * px=(df1b2_header *) list.bptr;
00135   list.bptr+=sizeof(df1b2_header);
00136   df1b2_header * pz=(df1b2_header *) list.bptr;
00137 
00138   list.restoreposition(); // save pointer to beginning of record;
00139 
00140   // Do first reverse paSS calculations
00141   // ****************************************************************
00142   // turn this off if no third derivatives are calculated
00143   // if (!no_third_derivatives)
00144   // {
00145   // save for second reverse pass
00146   // save identifier 1
00147   //   fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
00148   //   test_smartlist& list2=f1b2gradlist->list2;
00149   //int total_bytes=2*nvar*sizeof(double);
00150 // string identifier debug stuff
00151 #if defined(SAFE_ALL)
00152   //char ids[]="KM";
00153   //int slen=strlen(ids);
00154   //total_bytes+=slen;
00155 #endif
00156   //list2.check_buffer_size(total_bytes);
00157   //void * tmpptr2=list2.bptr;
00158 #if defined(SAFE_ALL)
00159   //memcpy(list2,ids,slen);
00160 #endif
00161      //memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
00162      //memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
00163      //*nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
00164      //nlist2++;
00165   // }
00166   //
00167   // ****************************************************************
00168 
00169   for (unsigned int i=0;i<nvar;i++)
00170   {
00171     px->u_bar[i]-=pz->u_bar[i];
00172   }
00173   for (unsigned int i=0;i<nvar;i++)
00174   {
00175     px->u_dot_bar[i]-=pz->u_dot_bar[i];
00176   }
00177 #if defined(PRINT_DERS)
00178  print_derivatives(px,"x");
00179  print_derivatives(pz,"z");
00180 #endif
00181 }
00182 
00187 void read_pass1_minus_eq_2(void)
00188 {
00189   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00190   // We are going forward for bptr and backword for bptr2
00191   //
00192   // list 1
00193   //
00194   unsigned int nvar=df1b2variable::nvar;
00195   test_smartlist & list=f1b2gradlist->list;
00196 
00197   size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00198 #if defined(SAFE_ALL)
00199   char ids[]="LC";
00200   int slen=strlen(ids);
00201   total_bytes+=slen;
00202 #endif
00203   list.check_buffer_size(total_bytes);
00204 
00205   list.saveposition(); // save pointer to beginning of record;
00206   fixed_smartlist & nlist=f1b2gradlist->nlist;
00207    // nlist-=sizeof(int);
00208   // get record size
00209   int num_bytes=nlist.bptr->numbytes;
00210     // nlist+=nlist_record_size;
00211   //
00212   // list 2
00213   //
00214   //test_smartlist & list2=f1b2gradlist->list2;
00215   //fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00216   // get record size
00217   //int num_bytes2=*nlist2.bptr;
00218   //nlist2--;
00219   // backup the size of the record
00220   //list2-=num_bytes2;
00221   //list2.saveposition(); // save pointer to beginning of record;
00222   // save the pointer to the beginning of the record
00223   // bptr and bptr2 now both point to the beginning of their records
00224 #if defined(SAFE_ALL)
00225   checkidentiferstring("LC",list);
00226   //checkidentiferstring("KM",list2);
00227 #endif
00228 
00229 
00230   // get info from tape1
00231   df1b2_header * px=(df1b2_header *) list.bptr;
00232   list.bptr+=sizeof(df1b2_header);
00233   df1b2_header * pz=(df1b2_header *) list.bptr;
00234 
00235   list.restoreposition(num_bytes); // save pointer to beginning of record;
00236 
00237 
00238   //double * zbar=(double*)list2.bptr;
00239   //double * zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00240 
00241   double * x_bar_tilde=px->get_u_bar_tilde();
00242   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00243   double * z_bar_tilde=pz->get_u_bar_tilde();
00244   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00245   // Do second "reverse-reverse" pass calculations
00246   for (unsigned int i=0;i<nvar;i++)
00247   {
00248     z_bar_tilde[i]-=x_bar_tilde[i];
00249   }
00250 
00251   for (unsigned int i=0;i<nvar;i++)
00252   {
00253     z_dot_bar_tilde[i]-=x_dot_bar_tilde[i];
00254   }
00255   //list2.restoreposition(); // save pointer to beginning of record;
00256 #if defined(PRINT_DERS)
00257  print_derivatives(px,"x");
00258  print_derivatives(pz,"z");
00259 #endif
00260 }
00261 
00266 void read_pass1_minus_eq_3(void)
00267 {
00268   // We are going backword for bptr and forward for bptr2
00269   // the current entry+2 in bptr is the size of the record i.e
00270   // points to the next record
00271   unsigned int nvar=df1b2variable::nvar;
00272   fixed_smartlist & nlist=f1b2gradlist->nlist;
00273   test_smartlist& list=f1b2gradlist->list;
00274    // nlist-=sizeof(int);
00275   // get record size
00276   int num_bytes=nlist.bptr->numbytes;
00277   // backup the size of the record
00278   list-=num_bytes;
00279   list.saveposition(); // save pointer to beginning of record;
00280   // save the pointer to the beginning of the record
00281 
00282 #if defined(SAFE_ALL)
00283   checkidentiferstring("LC",list);
00284 #endif
00285   // get info from tape1
00286   df1b2_header * px=(df1b2_header *) list.bptr;
00287   list.bptr+=sizeof(df1b2_header);
00288   df1b2_header * pz=(df1b2_header *) list.bptr;
00289 
00290   list.restoreposition(); // save pointer to beginning of record;
00291 
00292   // Do first reverse paSS calculations
00293 
00294   *(px->u_tilde)-=*pz->u_tilde;
00295   for (unsigned int i=0;i<nvar;i++)
00296   {
00297     px->u_dot_tilde[i]-=pz->u_dot_tilde[i];
00298   }
00299 #if defined(PRINT_DERS)
00300  print_derivatives(px,"x");
00301  print_derivatives(pz,"z");
00302 #endif
00303 }