ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn4.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
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 (unsigned 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 
00111   size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00112 // string identifier debug stuff
00113 #if defined(SAFE_ALL)
00114   char ids[]="GV";
00115   int slen=strlen(ids);
00116   total_bytes+=slen;
00117 #endif
00118   list.check_buffer_size(total_bytes);
00119   void * tmpptr=list.bptr;
00120 #if defined(SAFE_ALL)
00121   memcpy(list,ids,slen);
00122 #endif
00123 
00124   memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
00125   memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00126 
00127   nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00128   nlist.bptr->pf=(ADrfptr)(&ad_read_pass1_eq);
00129   ++nlist;
00130   return 0;
00131 }
00132 
00137 void ad_read_pass1_eq(void)
00138 {
00139   switch(df1b2variable::passnumber)
00140   {
00141   case 1:
00142     read_pass1_eq_1();
00143     break;
00144   case 2:
00145     read_pass1_eq_2();
00146     break;
00147   case 3:
00148     read_pass1_eq_3();
00149     break;
00150   default:
00151     cerr << "illegal value for df1b2variable::pass = "
00152          << df1b2variable::passnumber << endl;
00153     exit(1);
00154   }
00155 }
00156 
00161 void read_pass1_eq_1(void)
00162 {
00163   // We are going backword for bptr and forward for bptr2
00164   // the current entry+2 in bptr is the size of the record i.e
00165   // points to the next record
00166   unsigned int nvar=df1b2variable::nvar;
00167   fixed_smartlist & nlist=f1b2gradlist->nlist;
00168   test_smartlist& list=f1b2gradlist->list;
00169    // nlist-=sizeof(int);
00170   // get record size
00171   int num_bytes=nlist.bptr->numbytes;
00172   // backup the size of the record
00173   list-=num_bytes;
00174   list.saveposition(); // save pointer to beginning of record;
00175 
00176 #if defined(SAFE_ALL)
00177   checkidentiferstring("GV",list);
00178 #endif
00179 
00180   // get info from tape1
00181   df1b2_header * px=(df1b2_header *) list.bptr;
00182   list.bptr+=sizeof(df1b2_header);
00183   df1b2_header * pz=(df1b2_header *) list.bptr;
00184   list.bptr+=sizeof(df1b2_header);
00185   list.restoreposition(); // save pointer to beginning of record;
00186 
00187   // Do first reverse paSS calculations
00188   // ****************************************************************
00189   // turn this off if no third derivatives are calculated
00190   // if (!no_third_derivatives)
00191   // {
00192   // save for second reverse pass
00193   // save identifier 1
00194   fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
00195   test_smartlist& list2=f1b2gradlist->list2;
00196 
00197   size_t total_bytes=2*nvar*sizeof(double);
00198 
00199 #if defined(SAFE_ALL)
00200   char ids[]="HT";
00201   int slen=strlen(ids);
00202   total_bytes+=slen;
00203 #endif
00204 
00205   list2.check_buffer_size(total_bytes);
00206   void * tmpptr2=list2.bptr;
00207 
00208 #if defined(SAFE_ALL)
00209   memcpy(list2,ids,slen);
00210 #endif
00211 
00212   memcpy(list2,pz->get_u_bar(),nvar*(int)sizeof(double));
00213   memcpy(list2,pz->get_u_dot_bar(),nvar*(int)sizeof(double));
00214   *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
00215   ++nlist2;
00216   // }
00217   //
00218   // ****************************************************************
00219 #if defined(PRINT_DERS)
00220   print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00221   print_derivatives(pz,"z");
00222   print_derivatives(px,"x");
00223 #endif
00224 
00225   for (size_t i=0;i<nvar;i++)
00226   {
00227     px->u_bar[i]+=pz->u_bar[i];
00228 #if defined(ADDEBUG_PRINT)
00229     addebug_count++;
00230     if (addebug_count == 49)
00231     {
00232       cout << "trap" << endl;
00233     }
00234     cout << px->u_bar[i] << " " << pz->u_bar[i] << " " << addebug_count << endl;
00235 #endif
00236   }
00237   for (size_t i=0;i<nvar;i++)
00238   {
00239     px->u_dot_bar[i]+=pz->u_dot_bar[i];
00240   }
00241   for (size_t i=0;i<nvar;i++)
00242   {
00243     pz->u_bar[i]=0;
00244   }
00245   for (size_t i=0;i<nvar;i++)
00246   {
00247     pz->u_dot_bar[i]=0;
00248   }
00249 #if defined(PRINT_DERS)
00250   print_derivatives(px,"x");
00251   print_derivatives(pz,"z");
00252 #endif
00253 }
00254 
00259 void read_pass1_eq_2(void)
00260 {
00261   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00262   // We are going forward for bptr and backword for bptr2
00263   //
00264   // list 1
00265   //
00266   unsigned int nvar=df1b2variable::nvar;
00267   test_smartlist & list=f1b2gradlist->list;
00268 
00269   size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00270 #if defined(SAFE_ALL)
00271   char ids[]="GV";
00272   int slen=strlen(ids);
00273   total_bytes+=slen;
00274 #endif
00275   list.check_buffer_size(total_bytes);
00276 
00277   list.saveposition(); // save pointer to beginning of record;
00278   fixed_smartlist & nlist=f1b2gradlist->nlist;
00279    // nlist-=sizeof(int);
00280   // get record size
00281   int num_bytes=nlist.bptr->numbytes;
00282     // nlist+=nlist_record_size;
00283   //
00284   // list 2
00285   //
00286   test_smartlist & list2=f1b2gradlist->list2;
00287   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00288   // get record size
00289   int num_bytes2=*nlist2.bptr;
00290   --nlist2;
00291   // backup the size of the record
00292   list2-=num_bytes2;
00293   list2.saveposition(); // save pointer to beginning of record;
00294 #if defined(SAFE_ALL)
00295   checkidentiferstring("GV",list);
00296   checkidentiferstring("HT",list2);
00297 #endif
00298   // save the pointer to the beginning of the record
00299   // bptr and bptr2 now both point to the beginning of their records
00300 
00301   // get info from tape1
00302   df1b2_header * px=(df1b2_header *) list.bptr;
00303   list.bptr+=sizeof(df1b2_header);
00304   df1b2_header * pz=(df1b2_header *) list.bptr;
00305   list.restoreposition(num_bytes); // save pointer to beginning of record;
00306 
00307   //double* zbar=(double*)list2.bptr;
00308   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00309 
00310   list2.restoreposition(); // save pointer to beginning of record;
00311 
00312   double * x_bar_tilde=px->get_u_bar_tilde();
00313   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00314   double * z_bar_tilde=pz->get_u_bar_tilde();
00315   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00316   // Do second "reverse-reverse" pass calculations
00317 #if defined(PRINT_DERS)
00318  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00319  print_derivatives(pz,"z");
00320  print_derivatives(px,"x");
00321 #endif
00322 
00323   for (size_t i=0;i<nvar;i++)
00324   {
00325     z_bar_tilde[i]=0;
00326     z_dot_bar_tilde[i]=0;
00327   }
00328   for (size_t i=0;i<nvar;i++)
00329   {
00330     z_bar_tilde[i]+=x_bar_tilde[i];
00331   }
00332 
00333   for (size_t i=0;i<nvar;i++)
00334   {
00335     z_dot_bar_tilde[i]+=x_dot_bar_tilde[i];
00336   }
00337 #if defined(PRINT_DERS)
00338  print_derivatives(px,"x");
00339  print_derivatives(pz,"z");
00340 #endif
00341 }
00342 
00347 void read_pass1_eq_3(void)
00348 {
00349   // We are going backword for bptr and forward for bptr2
00350   // the current entry+2 in bptr is the size of the record i.e
00351   // points to the next record
00352   unsigned int nvar=df1b2variable::nvar;
00353   fixed_smartlist & nlist=f1b2gradlist->nlist;
00354   test_smartlist& list=f1b2gradlist->list;
00355    // nlist-=sizeof(int);
00356   // get record size
00357   int num_bytes=nlist.bptr->numbytes;
00358   // backup the size of the record
00359   list-=num_bytes;
00360   list.saveposition(); // save pointer to beginning of record;
00361   // save the pointer to the beginning of the record
00362 
00363 #if defined(SAFE_ALL)
00364   checkidentiferstring("GV",list);
00365 #endif
00366   // get info from tape1
00367   df1b2_header * px=(df1b2_header *) list.bptr;
00368   list.bptr+=sizeof(df1b2_header);
00369   df1b2_header * pz=(df1b2_header *) list.bptr;
00370   list.restoreposition(); // save pointer to beginning of record;
00371 
00372 #if defined(PRINT_DERS)
00373  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00374  print_derivatives(pz,"z");
00375  print_derivatives(px,"x");
00376 #endif
00377 
00378 
00379   *(px->u_tilde)+=*pz->u_tilde;
00380 #if defined(ADDEBUG_PRINT)
00381     addebug_count++;
00382     if (addebug_count == 49)
00383     {
00384       cout << "trap" << endl;
00385     }
00386     cout << *(px->u_tilde) << " " << *(pz->u_tilde) << " " << addebug_count
00387          << endl;
00388 #endif
00389   for (size_t i=0;i<nvar;i++)
00390   {
00391     px->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00392   }
00393 #if defined(PRINT_DERS)
00394  print_derivatives(px,"x");
00395  print_derivatives(pz,"z");
00396 #endif
00397   *(pz->u_tilde)=0;
00398   for (size_t i=0;i<nvar;i++)
00399   {
00400     pz->u_dot_tilde[i]=0;
00401   }
00402 }