ADMB Documentation  11.1.2535
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn4.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2fn4.cpp 2535 2014-10-31 12:28:33Z 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_eq_1(void);
00013 void read_pass1_eq_2(void);
00014 void read_pass1_eq_3(void);
00015 
00016 //#define ADDEBUG_PRINT
00017 #if defined(ADDEBUG_PRINT)
00018   extern int addebug_count;
00019 #endif
00020 
00021 #ifndef OPT_LIB
00022   #include <cassert>
00023   #include <climits>
00024 #endif
00025 
00030 df1b2variable& df1b2variable::operator = (const df1b2variable& _x)
00031 {
00032   if (ptr != _x.ptr)
00033   {
00034     ADUNCONST(df1b2variable,x)
00035     if (allocated() )
00036     {
00037       //int nc=*ncopies;
00038       initialize();
00039       //*ncopies=nc;
00040       double * xd=x.get_u_dot();
00041       double * zd=get_u_dot();
00042       *get_u()=*x.get_u();
00043       for (int i=0;i<df1b2variable::nvar;i++)
00044       {
00045         *zd++ += *xd++;
00046       }
00047 
00048       // WRITE WHATEVER ON TAPE
00049       //df1b2tape->set_tapeinfo_header(&x,&z,this,xd);
00050       // save stuff for first reverse pass
00051       // need &x, &z, this,
00052 
00053       // !!! nov 1 02
00054       // !!! nov 11 02
00055       if (!df1b2_gradlist::no_derivatives)
00056         f1b2gradlist->write_save_pass2_tilde_values(this);
00057       if (!df1b2_gradlist::no_derivatives)
00058         f1b2gradlist->write_pass1_eq(&x,this);
00059     }
00060     else  // guts of copy constructor
00061     {
00062       if (x.ptr)
00063       {
00064         ptr=x.ptr;
00065         ncopies=x.ncopies;
00066         (*ncopies)++;
00067         u=x.u;
00068         u_dot=x.u_dot;
00069         u_bar=x.u_bar;
00070         u_dot_bar=x.u_dot_bar;
00071         u_tilde=x.u_tilde;
00072         u_dot_tilde=x.u_dot_tilde;
00073         u_bar_tilde=x.u_bar_tilde;
00074         u_dot_bar_tilde=x.u_dot_bar_tilde;
00075       }
00076       else
00077       {
00078         ptr=NULL;
00079         ncopies=NULL;
00080         u=NULL;
00081         u_dot=NULL;
00082         u_bar=NULL;
00083         u_dot_bar=NULL;
00084         u_tilde=NULL;
00085         u_dot_tilde=NULL;
00086         u_bar_tilde=NULL;
00087         u_dot_bar_tilde=NULL;
00088       }
00089     }
00090   }
00091   return *this;
00092 }
00093 
00094 void ad_read_pass1_eq(void);
00095 
00100 int df1b2_gradlist::write_pass1_eq(const df1b2variable * _px,
00101   df1b2variable * pz)
00102 {
00103   ADUNCONST(df1b2variable*,px)
00104   //char * pg=0;
00105   ncount++;
00106 #if defined(CHECK_COUNT)
00107   if (ncount >= ncount_check)
00108     ncount_checker(ncount,ncount_check);
00109 #endif
00110   //int nvar=df1b2variable::nvar;
00111 
00112   size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00113 // string identifier debug stuff
00114 #if defined(SAFE_ALL)
00115   char ids[]="GV";
00116   int slen=strlen(ids);
00117   total_bytes+=slen;
00118 #endif
00119   list.check_buffer_size(total_bytes);
00120   void * tmpptr=list.bptr;
00121 #if defined(SAFE_ALL)
00122   memcpy(list,ids,slen);
00123 #endif
00124 
00125   memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
00126   memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00127 
00128   nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00129   nlist.bptr->pf=(ADrfptr)(&ad_read_pass1_eq);
00130   ++nlist;
00131   return 0;
00132 }
00133 
00138 void ad_read_pass1_eq(void)
00139 {
00140   switch(df1b2variable::passnumber)
00141   {
00142   case 1:
00143     read_pass1_eq_1();
00144     break;
00145   case 2:
00146     read_pass1_eq_2();
00147     break;
00148   case 3:
00149     read_pass1_eq_3();
00150     break;
00151   default:
00152     cerr << "illegal value for df1b2variable::pass = "
00153          << df1b2variable::passnumber << endl;
00154     exit(1);
00155   }
00156 }
00157 
00162 void read_pass1_eq_1(void)
00163 {
00164   // We are going backword for bptr and forward for bptr2
00165   // the current entry+2 in bptr is the size of the record i.e
00166   // points to the next record
00167   int _nvar=df1b2variable::nvar;
00168 #ifndef OPT_LIB
00169   assert(_nvar > 0);
00170 #endif
00171   size_t nvar = (size_t)_nvar;
00172   fixed_smartlist & nlist=f1b2gradlist->nlist;
00173   test_smartlist& list=f1b2gradlist->list;
00174    // nlist-=sizeof(int);
00175   // get record size
00176   int num_bytes=nlist.bptr->numbytes;
00177   // backup the size of the record
00178   list-=num_bytes;
00179   list.saveposition(); // save pointer to beginning of record;
00180 
00181 #if defined(SAFE_ALL)
00182   checkidentiferstring("GV",list);
00183 #endif
00184 
00185   // get info from tape1
00186   df1b2_header * px=(df1b2_header *) list.bptr;
00187   list.bptr+=sizeof(df1b2_header);
00188   df1b2_header * pz=(df1b2_header *) list.bptr;
00189   list.bptr+=sizeof(df1b2_header);
00190   list.restoreposition(); // save pointer to beginning of record;
00191 
00192   // Do first reverse paSS calculations
00193   // ****************************************************************
00194   // turn this off if no third derivatives are calculated
00195   // if (!no_third_derivatives)
00196   // {
00197   // save for second reverse pass
00198   // save identifier 1
00199   fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
00200   test_smartlist& list2=f1b2gradlist->list2;
00201 
00202   size_t total_bytes=2*nvar*sizeof(double);
00203 
00204 #if defined(SAFE_ALL)
00205   char ids[]="HT";
00206   int slen=strlen(ids);
00207   total_bytes+=slen;
00208 #endif
00209 
00210   list2.check_buffer_size(total_bytes);
00211   void * tmpptr2=list2.bptr;
00212 
00213 #if defined(SAFE_ALL)
00214   memcpy(list2,ids,slen);
00215 #endif
00216 
00217   memcpy(list2,pz->get_u_bar(),nvar*(int)sizeof(double));
00218   memcpy(list2,pz->get_u_dot_bar(),nvar*(int)sizeof(double));
00219   *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
00220   ++nlist2;
00221   // }
00222   //
00223   // ****************************************************************
00224 #if defined(PRINT_DERS)
00225   print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00226   print_derivatives(pz,"z");
00227   print_derivatives(px,"x");
00228 #endif
00229 
00230   for (size_t i=0;i<nvar;i++)
00231   {
00232     px->u_bar[i]+=pz->u_bar[i];
00233 #if defined(ADDEBUG_PRINT)
00234     addebug_count++;
00235     if (addebug_count == 49)
00236     {
00237       cout << "trap" << endl;
00238     }
00239     cout << px->u_bar[i] << " " << pz->u_bar[i] << " " << addebug_count << endl;
00240 #endif
00241   }
00242   for (size_t i=0;i<nvar;i++)
00243   {
00244     px->u_dot_bar[i]+=pz->u_dot_bar[i];
00245   }
00246   for (size_t i=0;i<nvar;i++)
00247   {
00248     pz->u_bar[i]=0;
00249   }
00250   for (size_t i=0;i<nvar;i++)
00251   {
00252     pz->u_dot_bar[i]=0;
00253   }
00254 #if defined(PRINT_DERS)
00255   print_derivatives(px,"x");
00256   print_derivatives(pz,"z");
00257 #endif
00258 }
00259 
00264 void read_pass1_eq_2(void)
00265 {
00266   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00267   // We are going forward for bptr and backword for bptr2
00268   //
00269   // list 1
00270   //
00271   int _nvar=df1b2variable::nvar;
00272 #ifndef OPT_LIB
00273   assert(_nvar > 0);
00274 #endif
00275   size_t nvar = (size_t)_nvar;
00276   test_smartlist & list=f1b2gradlist->list;
00277 
00278   size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00279 #if defined(SAFE_ALL)
00280   char ids[]="GV";
00281   int slen=strlen(ids);
00282   total_bytes+=slen;
00283 #endif
00284   list.check_buffer_size(total_bytes);
00285 
00286   list.saveposition(); // save pointer to beginning of record;
00287   fixed_smartlist & nlist=f1b2gradlist->nlist;
00288    // nlist-=sizeof(int);
00289   // get record size
00290   int num_bytes=nlist.bptr->numbytes;
00291     // nlist+=nlist_record_size;
00292   //
00293   // list 2
00294   //
00295   test_smartlist & list2=f1b2gradlist->list2;
00296   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00297   // get record size
00298   int num_bytes2=*nlist2.bptr;
00299   --nlist2;
00300   // backup the size of the record
00301   list2-=num_bytes2;
00302   list2.saveposition(); // save pointer to beginning of record;
00303 #if defined(SAFE_ALL)
00304   checkidentiferstring("GV",list);
00305   checkidentiferstring("HT",list2);
00306 #endif
00307   // save the pointer to the beginning of the record
00308   // bptr and bptr2 now both point to the beginning of their records
00309 
00310   // get info from tape1
00311   df1b2_header * px=(df1b2_header *) list.bptr;
00312   list.bptr+=sizeof(df1b2_header);
00313   df1b2_header * pz=(df1b2_header *) list.bptr;
00314   list.restoreposition(num_bytes); // save pointer to beginning of record;
00315 
00316   //double* zbar=(double*)list2.bptr;
00317   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00318 
00319   list2.restoreposition(); // save pointer to beginning of record;
00320 
00321   double * x_bar_tilde=px->get_u_bar_tilde();
00322   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00323   double * z_bar_tilde=pz->get_u_bar_tilde();
00324   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00325   // Do second "reverse-reverse" pass calculations
00326 #if defined(PRINT_DERS)
00327  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00328  print_derivatives(pz,"z");
00329  print_derivatives(px,"x");
00330 #endif
00331 
00332   for (size_t i=0;i<nvar;i++)
00333   {
00334     z_bar_tilde[i]=0;
00335     z_dot_bar_tilde[i]=0;
00336   }
00337   for (size_t i=0;i<nvar;i++)
00338   {
00339     z_bar_tilde[i]+=x_bar_tilde[i];
00340   }
00341 
00342   for (size_t i=0;i<nvar;i++)
00343   {
00344     z_dot_bar_tilde[i]+=x_dot_bar_tilde[i];
00345   }
00346 #if defined(PRINT_DERS)
00347  print_derivatives(px,"x");
00348  print_derivatives(pz,"z");
00349 #endif
00350 }
00351 
00356 void read_pass1_eq_3(void)
00357 {
00358   // We are going backword for bptr and forward for bptr2
00359   // the current entry+2 in bptr is the size of the record i.e
00360   // points to the next record
00361   int _nvar=df1b2variable::nvar;
00362 #ifndef OPT_LIB
00363   assert(_nvar > 0);
00364 #endif
00365   size_t nvar = (size_t)_nvar;
00366   fixed_smartlist & nlist=f1b2gradlist->nlist;
00367   test_smartlist& list=f1b2gradlist->list;
00368    // nlist-=sizeof(int);
00369   // get record size
00370   int num_bytes=nlist.bptr->numbytes;
00371   // backup the size of the record
00372   list-=num_bytes;
00373   list.saveposition(); // save pointer to beginning of record;
00374   // save the pointer to the beginning of the record
00375 
00376 #if defined(SAFE_ALL)
00377   checkidentiferstring("GV",list);
00378 #endif
00379   // get info from tape1
00380   df1b2_header * px=(df1b2_header *) list.bptr;
00381   list.bptr+=sizeof(df1b2_header);
00382   df1b2_header * pz=(df1b2_header *) list.bptr;
00383   list.restoreposition(); // save pointer to beginning of record;
00384 
00385 #if defined(PRINT_DERS)
00386  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00387  print_derivatives(pz,"z");
00388  print_derivatives(px,"x");
00389 #endif
00390 
00391 
00392   *(px->u_tilde)+=*pz->u_tilde;
00393 #if defined(ADDEBUG_PRINT)
00394     addebug_count++;
00395     if (addebug_count == 49)
00396     {
00397       cout << "trap" << endl;
00398     }
00399     cout << *(px->u_tilde) << " " << *(pz->u_tilde) << " " << addebug_count
00400          << endl;
00401 #endif
00402   for (size_t i=0;i<nvar;i++)
00403   {
00404     px->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00405   }
00406 #if defined(PRINT_DERS)
00407  print_derivatives(px,"x");
00408  print_derivatives(pz,"z");
00409 #endif
00410   *(pz->u_tilde)=0;
00411   for (size_t i=0;i<nvar;i++)
00412   {
00413     pz->u_dot_tilde[i]=0;
00414   }
00415 }