ADMB Documentation  11.1.2500
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn8.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2fn8.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 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 (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   int 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   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   int i;
00140 
00141   // Do first reverse paSS calculations
00142   // ****************************************************************
00143   // turn this off if no third derivatives are calculated
00144   // if (!no_third_derivatives)
00145   // {
00146   // save for second reverse pass
00147   // save identifier 1
00148   //   fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
00149   //   test_smartlist& list2=f1b2gradlist->list2;
00150   //int total_bytes=2*nvar*sizeof(double);
00151 // string identifier debug stuff
00152 #if defined(SAFE_ALL)
00153   //char ids[]="KM";
00154   //int slen=strlen(ids);
00155   //total_bytes+=slen;
00156 #endif
00157   //list2.check_buffer_size(total_bytes);
00158   //void * tmpptr2=list2.bptr;
00159 #if defined(SAFE_ALL)
00160   //memcpy(list2,ids,slen);
00161 #endif
00162      //memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
00163      //memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
00164      //*nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
00165      //nlist2++;
00166   // }
00167   //
00168   // ****************************************************************
00169 
00170   for (i=0;i<nvar;i++)
00171   {
00172     px->u_bar[i]-=pz->u_bar[i];
00173   }
00174   for (i=0;i<nvar;i++)
00175   {
00176     px->u_dot_bar[i]-=pz->u_dot_bar[i];
00177   }
00178 #if defined(PRINT_DERS)
00179  print_derivatives(px,"x");
00180  print_derivatives(pz,"z");
00181 #endif
00182 }
00183 
00188 void read_pass1_minus_eq_2(void)
00189 {
00190   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00191   // We are going forward for bptr and backword for bptr2
00192   //
00193   // list 1
00194   //
00195   int nvar=df1b2variable::nvar;
00196   test_smartlist & list=f1b2gradlist->list;
00197 
00198   int total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00199 #if defined(SAFE_ALL)
00200   char ids[]="LC";
00201   int slen=strlen(ids);
00202   total_bytes+=slen;
00203 #endif
00204   list.check_buffer_size(total_bytes);
00205 
00206   list.saveposition(); // save pointer to beginning of record;
00207   fixed_smartlist & nlist=f1b2gradlist->nlist;
00208    // nlist-=sizeof(int);
00209   // get record size
00210   int num_bytes=nlist.bptr->numbytes;
00211     // nlist+=nlist_record_size;
00212   //
00213   // list 2
00214   //
00215   //test_smartlist & list2=f1b2gradlist->list2;
00216   //fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00217   // get record size
00218   //int num_bytes2=*nlist2.bptr;
00219   //nlist2--;
00220   // backup the size of the record
00221   //list2-=num_bytes2;
00222   //list2.saveposition(); // save pointer to beginning of record;
00223   // save the pointer to the beginning of the record
00224   // bptr and bptr2 now both point to the beginning of their records
00225 #if defined(SAFE_ALL)
00226   checkidentiferstring("LC",list);
00227   //checkidentiferstring("KM",list2);
00228 #endif
00229 
00230 
00231   // get info from tape1
00232   df1b2_header * px=(df1b2_header *) list.bptr;
00233   list.bptr+=sizeof(df1b2_header);
00234   df1b2_header * pz=(df1b2_header *) list.bptr;
00235 
00236   list.restoreposition(num_bytes); // save pointer to beginning of record;
00237 
00238 
00239   //double * zbar=(double*)list2.bptr;
00240   //double * zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00241 
00242   double * x_bar_tilde=px->get_u_bar_tilde();
00243   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00244   double * z_bar_tilde=pz->get_u_bar_tilde();
00245   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00246   // Do second "reverse-reverse" pass calculations
00247   int i;
00248   for (i=0;i<nvar;i++)
00249   {
00250     z_bar_tilde[i]-=x_bar_tilde[i];
00251   }
00252 
00253   for (i=0;i<nvar;i++)
00254   {
00255     z_dot_bar_tilde[i]-=x_dot_bar_tilde[i];
00256   }
00257   //list2.restoreposition(); // save pointer to beginning of record;
00258 #if defined(PRINT_DERS)
00259  print_derivatives(px,"x");
00260  print_derivatives(pz,"z");
00261 #endif
00262 }
00263 
00268 void read_pass1_minus_eq_3(void)
00269 {
00270   // We are going backword for bptr and forward for bptr2
00271   // the current entry+2 in bptr is the size of the record i.e
00272   // points to the next record
00273   int nvar=df1b2variable::nvar;
00274   fixed_smartlist & nlist=f1b2gradlist->nlist;
00275   test_smartlist& list=f1b2gradlist->list;
00276    // nlist-=sizeof(int);
00277   // get record size
00278   int num_bytes=nlist.bptr->numbytes;
00279   // backup the size of the record
00280   list-=num_bytes;
00281   list.saveposition(); // save pointer to beginning of record;
00282   // save the pointer to the beginning of the record
00283 
00284 #if defined(SAFE_ALL)
00285   checkidentiferstring("LC",list);
00286 #endif
00287   // get info from tape1
00288   df1b2_header * px=(df1b2_header *) list.bptr;
00289   list.bptr+=sizeof(df1b2_header);
00290   df1b2_header * pz=(df1b2_header *) list.bptr;
00291 
00292   list.restoreposition(); // save pointer to beginning of record;
00293   int i;
00294 
00295   // Do first reverse paSS calculations
00296 
00297   *(px->u_tilde)-=*pz->u_tilde;
00298   for (i=0;i<nvar;i++)
00299   {
00300     px->u_dot_tilde[i]-=pz->u_dot_tilde[i];
00301   }
00302 #if defined(PRINT_DERS)
00303  print_derivatives(px,"x");
00304  print_derivatives(pz,"z");
00305 #endif
00306 }