ADMB Documentation  11.1.2490
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn4.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2fn4.cpp 2330 2014-09-12 19:17:55Z 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   int 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   fixed_smartlist & nlist=f1b2gradlist->nlist;
00169   test_smartlist& list=f1b2gradlist->list;
00170    // nlist-=sizeof(int);
00171   // get record size
00172   int num_bytes=nlist.bptr->numbytes;
00173   // backup the size of the record
00174   list-=num_bytes;
00175   list.saveposition(); // save pointer to beginning of record;
00176 
00177 #if defined(SAFE_ALL)
00178   checkidentiferstring("GV",list);
00179 #endif
00180 
00181   // get info from tape1
00182   df1b2_header * px=(df1b2_header *) list.bptr;
00183   list.bptr+=sizeof(df1b2_header);
00184   df1b2_header * pz=(df1b2_header *) list.bptr;
00185   list.bptr+=sizeof(df1b2_header);
00186   list.restoreposition(); // save pointer to beginning of record;
00187 
00188   // Do first reverse paSS calculations
00189   // ****************************************************************
00190   // turn this off if no third derivatives are calculated
00191   // if (!no_third_derivatives)
00192   // {
00193   // save for second reverse pass
00194   // save identifier 1
00195   fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
00196   test_smartlist& list2=f1b2gradlist->list2;
00197 
00198   size_t total_bytes=2*nvar*sizeof(double);
00199 
00200 #if defined(SAFE_ALL)
00201   char ids[]="HT";
00202   int slen=strlen(ids);
00203   total_bytes+=slen;
00204 #endif
00205 
00206 #ifndef OPT_LIB
00207   assert(total_bytes <= (size_t)INT_MAX);
00208 #endif
00209 
00210   list2.check_buffer_size((int)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 (int 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 (int i=0;i<nvar;i++)
00243   {
00244     px->u_dot_bar[i]+=pz->u_dot_bar[i];
00245   }
00246   for (int i=0;i<nvar;i++)
00247   {
00248     pz->u_bar[i]=0;
00249   }
00250   for (int 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   test_smartlist & list=f1b2gradlist->list;
00273 
00274   int total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00275 #if defined(SAFE_ALL)
00276   char ids[]="GV";
00277   int slen=strlen(ids);
00278   total_bytes+=slen;
00279 #endif
00280   list.check_buffer_size(total_bytes);
00281 
00282   list.saveposition(); // save pointer to beginning of record;
00283   fixed_smartlist & nlist=f1b2gradlist->nlist;
00284    // nlist-=sizeof(int);
00285   // get record size
00286   int num_bytes=nlist.bptr->numbytes;
00287     // nlist+=nlist_record_size;
00288   //
00289   // list 2
00290   //
00291   test_smartlist & list2=f1b2gradlist->list2;
00292   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00293   // get record size
00294   int num_bytes2=*nlist2.bptr;
00295   --nlist2;
00296   // backup the size of the record
00297   list2-=num_bytes2;
00298   list2.saveposition(); // save pointer to beginning of record;
00299 #if defined(SAFE_ALL)
00300   checkidentiferstring("GV",list);
00301   checkidentiferstring("HT",list2);
00302 #endif
00303   // save the pointer to the beginning of the record
00304   // bptr and bptr2 now both point to the beginning of their records
00305 
00306   // get info from tape1
00307   df1b2_header * px=(df1b2_header *) list.bptr;
00308   list.bptr+=sizeof(df1b2_header);
00309   df1b2_header * pz=(df1b2_header *) list.bptr;
00310   list.restoreposition(num_bytes); // save pointer to beginning of record;
00311 
00312   //double* zbar=(double*)list2.bptr;
00313   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00314 
00315   list2.restoreposition(); // save pointer to beginning of record;
00316 
00317   double * x_bar_tilde=px->get_u_bar_tilde();
00318   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00319   double * z_bar_tilde=pz->get_u_bar_tilde();
00320   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00321   // Do second "reverse-reverse" pass calculations
00322 #if defined(PRINT_DERS)
00323  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00324  print_derivatives(pz,"z");
00325  print_derivatives(px,"x");
00326 #endif
00327 
00328   for (int i=0;i<nvar;i++)
00329   {
00330     z_bar_tilde[i]=0;
00331     z_dot_bar_tilde[i]=0;
00332   }
00333   for (int i=0;i<nvar;i++)
00334   {
00335     z_bar_tilde[i]+=x_bar_tilde[i];
00336   }
00337 
00338   for (int i=0;i<nvar;i++)
00339   {
00340     z_dot_bar_tilde[i]+=x_dot_bar_tilde[i];
00341   }
00342 #if defined(PRINT_DERS)
00343  print_derivatives(px,"x");
00344  print_derivatives(pz,"z");
00345 #endif
00346 }
00347 
00352 void read_pass1_eq_3(void)
00353 {
00354   // We are going backword for bptr and forward for bptr2
00355   // the current entry+2 in bptr is the size of the record i.e
00356   // points to the next record
00357   int nvar=df1b2variable::nvar;
00358   fixed_smartlist & nlist=f1b2gradlist->nlist;
00359   test_smartlist& list=f1b2gradlist->list;
00360    // nlist-=sizeof(int);
00361   // get record size
00362   int num_bytes=nlist.bptr->numbytes;
00363   // backup the size of the record
00364   list-=num_bytes;
00365   list.saveposition(); // save pointer to beginning of record;
00366   // save the pointer to the beginning of the record
00367 
00368 #if defined(SAFE_ALL)
00369   checkidentiferstring("GV",list);
00370 #endif
00371   // get info from tape1
00372   df1b2_header * px=(df1b2_header *) list.bptr;
00373   list.bptr+=sizeof(df1b2_header);
00374   df1b2_header * pz=(df1b2_header *) list.bptr;
00375   list.restoreposition(); // save pointer to beginning of record;
00376   int i;
00377 
00378 #if defined(PRINT_DERS)
00379  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00380  print_derivatives(pz,"z");
00381  print_derivatives(px,"x");
00382 #endif
00383 
00384 
00385   *(px->u_tilde)+=*pz->u_tilde;
00386 #if defined(ADDEBUG_PRINT)
00387     addebug_count++;
00388     if (addebug_count == 49)
00389     {
00390       cout << "trap" << endl;
00391     }
00392     cout << *(px->u_tilde) << " " << *(pz->u_tilde) << " " << addebug_count
00393          << endl;
00394 #endif
00395   for (i=0;i<nvar;i++)
00396   {
00397     px->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00398   }
00399 #if defined(PRINT_DERS)
00400  print_derivatives(px,"x");
00401  print_derivatives(pz,"z");
00402 #endif
00403   *(pz->u_tilde)=0;
00404   for (i=0;i<nvar;i++)
00405   {
00406     pz->u_dot_tilde[i]=0;
00407   }
00408 }