ADMB Documentation  11.1.2490
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn5.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2fn5.cpp 2344 2014-09-15 21:26:09Z 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_init_1(void);
00013 void read_pass1_init_2(void);
00014 void read_pass1_init_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 
00026 void ad_read_pass1_initialize(void);
00027 
00033 void df1b2variable::operator=(double d)
00034 {
00035   initialize();
00036   //ptr[blocksize-1]=8219.4;
00037   get_ind_index()=-1;
00038   *u=d;
00039   //double * zd=get_u_dot();
00040   // !!! nov 1 02
00041   if (!df1b2_gradlist::no_derivatives)
00042     f1b2gradlist->write_save_pass2_tilde_values(this);
00043   if (!df1b2_gradlist::no_derivatives)
00044     f1b2gradlist->write_pass1_initialize(this);
00045 }
00046 
00051 df1b2variable::df1b2variable(double d)
00052 {
00053   // vectors are all 1,...,nvar
00054   ptr = (double*) pool->alloc();
00055   //AD_ALLOCATE(ptr,double,blocksize,df1b2variable)
00056   initialize();
00057   double * block=ptr;
00058   //block[blocksize-1]=8219.4;
00059   get_ind_index()=-1;
00060   ncopies=&(((twointsandptr*)block)->ncopies);
00061   *ncopies=0;
00062   u=block+=2;
00063   u_dot=block+=1;  // u has size 1
00064   u_bar=block+=nvar;  // u_dot has size  nvar
00065   u_dot_bar=block+=nvar;  // u_bar has size  nvar
00066   u_tilde=block+=nvar; // u_dot_bar has size  nvar
00067   u_dot_tilde=block+=1;  // u_tilde has size 1
00068   u_bar_tilde=block+=nvar; // u_dot_tilde has size nvar
00069   u_dot_bar_tilde=block+=nvar;  // u_bar_tilde has size nvar
00070   *u=d;
00071   if (!df1b2_gradlist::no_derivatives)
00072     f1b2gradlist->write_pass1_initialize(this);
00073 }
00074 
00078 df1b2variable::df1b2variable(void)
00079 {
00080   if (!df1b2variable::noallocate)
00081   {
00082     allocate();
00083   }
00084   else
00085   {
00086     ptr=0;
00087     ncopies=0;
00088     u=0;
00089     u_dot=0;
00090     u_bar=0;
00091     u_dot_bar=0;
00092     u_tilde=0;
00093     u_dot_tilde=0;
00094     u_bar_tilde=0;
00095     u_dot_bar_tilde=0;
00096   }
00097 }
00098 
00103 df1b2variable::df1b2variable(const newadkludge* z)
00104 {
00105   ptr=0;
00106 #if defined(SAFE_ALL)
00107   ncopies=0;
00108   u=0;
00109   u_dot=0;
00110   u_bar=0;
00111   u_dot_bar=0;
00112   u_tilde=0;
00113   u_dot_tilde=0;
00114   u_bar_tilde=0;
00115   u_dot_bar_tilde=0;
00116 #endif
00117 }
00118 
00123 void df1b2variable::allocate(void)
00124 {
00125   // vectors are all 1,...,nvar
00126   ptr = (double*) pool->alloc();
00127   //cout << *(int*)ptr << endl;
00128   //AD_ALLOCATE(ptr,double,blocksize,df1b2variable)
00129   initialize();
00130   double * block=ptr;
00131   //block[blocksize-1]=8219.4;
00132   get_ind_index()=-1;
00133   ncopies=&(((twointsandptr*)block)->ncopies);
00134   *ncopies=0;
00135   u=block+=2;
00136   u_dot=block+=1;  // u has size 1
00137   u_bar=block+=nvar;  // u_dot has size  nvar
00138   u_dot_bar=block+=nvar;  // u_bar has size  nvar
00139   u_tilde=block+=nvar; // u_dot_bar has size  nvar
00140   u_dot_tilde=block+=1;  // u_tilde has size 1
00141   u_bar_tilde=block+=nvar; // u_dot_tilde has size nvar
00142   u_dot_bar_tilde=block+=nvar;  // u_bar_tilde has size nvar
00143   if (!df1b2_gradlist::no_derivatives)
00144     f1b2gradlist->write_pass1_initialize(this);
00145 }
00146 
00151 df1b2variable::df1b2variable(adkludge1 * adkludgexxx)
00152 {
00153   // vectors are all 1,...,nvar
00154   ptr = (double*) pool->alloc();
00155   //AD_ALLOCATE(ptr,double,blocksize,df1b2variable)
00156   initialize();
00157   double * block=ptr;
00158   block[blocksize-1]=8219.4;
00159   get_ind_index()=-1;
00160   ncopies=&(((twointsandptr*)block)->ncopies);
00161   *ncopies=0;
00162   u=block+=2;
00163   u_dot=block+=1;  // u has size 1
00164   u_bar=block+=nvar;  // u_dot has size  nvar
00165   u_dot_bar=block+=nvar;  // u_bar has size  nvar
00166   u_tilde=block+=nvar; // u_dot_bar has size  nvar
00167   u_dot_tilde=block+=1;  // u_tilde has size 1
00168   u_bar_tilde=block+=nvar; // u_dot_tilde has size nvar
00169   u_dot_bar_tilde=block+=nvar;  // u_bar_tilde has size nvar
00170   //if (!df1b2_gradlist::no_derivatives)
00171    // f1b2gradlist->write_pass1_initialize(this);
00172 }
00173 
00174 //int df1b2_gradlist::write_pass1_initialize(const df1b2variable * px,
00175 //  df1b2variable * pz)
00176 
00181 int df1b2_gradlist::write_pass1_initialize(df1b2variable * pz)
00182 {
00183   char * pg=0;
00184   ncount++;
00185 
00186 #if defined(CHECK_COUNT)
00187   if (ncount >= ncount_check)
00188     ncount_checker(ncount,ncount_check);
00189 #endif
00190 
00191   //int nvar=df1b2variable::nvar;
00192   size_t total_bytes=sizeof(df1b2_header)+sizeof(char *);
00193 
00194 // string identifier debug stuff
00195 #if defined(SAFE_ALL)
00196   char ids[]="AZ";
00197   size_t slen=strlen(ids);
00198   total_bytes+=slen;
00199 #endif
00200 
00201 #ifndef OPT_LIB
00202   assert(total_bytes <= INT_MAX);
00203 #endif
00204 
00205   list.check_buffer_size((int)total_bytes);
00206   void * tmpptr=list.bptr;
00207 
00208 #if defined(SAFE_ALL)
00209   memcpy(list,ids,slen);
00210 #endif
00211 // end of string identifier debug stuff
00212 
00213   memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00214   //list.bptr+=sizeof(df1b2_header);
00215   // ***************** don't need this ***************
00216   memcpy(list,&pg,sizeof(char *));
00217   //*(char**)(list.bptr)=(char*)pg;
00218   //list.bptr+=sizeof(char*);
00219   // ***** write  record size
00220   nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00221   nlist.bptr->pf=(ADrfptr)(&ad_read_pass1_initialize);
00222   ++nlist;
00223   return 0;
00224 }
00225 
00230 void ad_read_pass1_initialize(void)
00231 {
00232   switch(df1b2variable::passnumber)
00233   {
00234   case 1:
00235     read_pass1_init_1();
00236     break;
00237   case 2:
00238     read_pass1_init_2();
00239     break;
00240   case 3:
00241     read_pass1_init_3();
00242     break;
00243   default:
00244     cerr << "illegal value for df1b2variable::pass = "
00245          << df1b2variable::passnumber << endl;
00246     exit(1);
00247   }
00248 }
00249 
00254 void checkidentiferstring(const char * ids,test_smartlist& list)
00255 {
00256   size_t slen=strlen(ids);
00257   char * s=(char*)list.bptr;
00258   int ss=strncmp(ids,s,slen);
00259   if (ss)
00260   {
00261     cerr << "Error in string identifer in list value should be " << ids
00262      << endl << " it appears to be " << s[0] << s[1] << s[2] << s[3]
00263      << " ... " << endl;
00264     ad_exit(1);
00265   }
00266   list.bptr+=slen;
00267 }
00268 
00273 void read_pass1_init_1(void)
00274 {
00275   // We are going backword for bptr and forward for bptr2
00276   // the current entry+2 in bptr is the size of the record i.e
00277   // points to the next record
00278   int nvar=df1b2variable::nvar;
00279   fixed_smartlist & nlist=f1b2gradlist->nlist;
00280   test_smartlist& list=f1b2gradlist->list;
00281    // nlist-=sizeof(int);
00282   // get record size
00283   int num_bytes=nlist.bptr->numbytes;
00284   // backup the size of the record
00285   list-=num_bytes;
00286   list.saveposition(); // save pointer to beginning of record;
00287   // save the pointer to the beginning of the record
00288   //df1b2_header z;
00289 
00290   // get info from tape1
00291 #if defined(SAFE_ALL)
00292   checkidentiferstring("AZ",list);
00293 #endif
00294   df1b2_header * pz=(df1b2_header *) list.bptr;
00295   list.bptr+=sizeof(df1b2_header);
00296   //df1b2function1* pf = *(df1b2function1**)list.bptr;
00297   list.bptr+=sizeof(char*);
00298   list.restoreposition(); // save pointer to beginning of record;
00299   //int i;
00300 
00301   // Do first reverse paSS calculations
00302   // ****************************************************************
00303   // turn this off if no third derivatives are calculated
00304   // if (!no_third_derivatives)
00305   // {
00306   // save for second reverse pass
00307   // save identifier 1
00308      fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
00309      test_smartlist& list2=f1b2gradlist->list2;
00310 
00311   size_t total_bytes=2*nvar*sizeof(double);
00312 
00313 // string identifier debug stuff
00314 #if defined(SAFE_ALL)
00315   char ids[]="EF";
00316   size_t slen=strlen(ids);
00317   total_bytes+=slen;
00318 #endif
00319 
00320 #ifndef OPT_LIB
00321   assert(total_bytes <= INT_MAX);
00322 #endif
00323 
00324   list2.check_buffer_size((int)total_bytes);
00325 
00326   void * tmpptr2=list2.bptr;
00327 #if defined(SAFE_ALL)
00328   memcpy(list2,ids,slen);
00329 #endif
00330 
00331   const int sizeofdouble = sizeof(double);
00332   memcpy(list2,pz->get_u_bar(),nvar*sizeofdouble);
00333   //list2.bptr+=nvar*sizeof(double);
00334   memcpy(list2,pz->get_u_dot_bar(),nvar*sizeofdouble);
00335   //list2.bptr+=nvar*sizeof(double);
00336   *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
00337   ++nlist2;
00338   // }
00339   //
00340   // ****************************************************************
00341 
00342   // This should *NEVER* be used (I think)
00343  /*
00344   for (i=0;i<nvar;i++)
00345   {
00346     pz->u_bar[i]=0;
00347   }
00348   for (i=0;i<nvar;i++)
00349   {
00350     pz->u_dot_bar[i]=0;
00351   }
00352   */
00353 }
00354 
00359 void read_pass1_init_2(void)
00360 {
00361   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00362   // We are going forward for bptr and backword for bptr2
00363   //
00364   // list 1
00365   //
00366   int nvar=df1b2variable::nvar;
00367   test_smartlist & list=f1b2gradlist->list;
00368 
00369   int total_bytes=sizeof(df1b2_header)+sizeof(char *);
00370 // string identifier debug stuff
00371 #if defined(SAFE_ALL)
00372   char ids[]="AZ";
00373   int slen=strlen(ids);
00374   total_bytes+=slen;
00375 #endif
00376 // end of string identifier debug stuff
00377   list.check_buffer_size(total_bytes);
00378 
00379   list.saveposition(); // save pointer to beginning of record;
00380   fixed_smartlist & nlist=f1b2gradlist->nlist;
00381    // nlist-=sizeof(int);
00382   // get record size
00383   int num_bytes=nlist.bptr->numbytes;
00384     // nlist+=nlist_record_size;
00385   //
00386   // list 2
00387   //
00388   test_smartlist & list2=f1b2gradlist->list2;
00389   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00390   // get record size
00391   int num_bytes2=*nlist2.bptr;
00392   --nlist2;
00393   // backup the size of the record
00394   list2-=num_bytes2;
00395   // save the pointer to the beginning of the record
00396   // bptr and bptr2 now both point to the beginning of their records
00397 
00398   //df1b2_header z;
00399 
00400   // get info from tape1
00401   list2.saveposition(); // save pointer to beginning of record;
00402 #if defined(SAFE_ALL)
00403   checkidentiferstring("AZ",list);
00404   checkidentiferstring("EF",list2);
00405 #endif
00406   df1b2_header * pz=(df1b2_header *) list.bptr;
00407   list.bptr+=sizeof(df1b2_header);
00408   //df1b2function1 * pf = *(df1b2function1**)list.bptr;
00409   list.bptr+=sizeof(char*);
00410   list.restoreposition(num_bytes); // save pointer to beginning of record;
00411 
00412   //double* zbar=(double*)list2.bptr;
00413   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00414 
00415   list2.restoreposition(); // save pointer to beginning of record;
00416 
00417   double * z_bar_tilde=pz->get_u_bar_tilde();
00418   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00419   // Do second "reverse-reverse" pass calculations
00420   int i;
00421 
00422   // !!!! oct31 02
00423 
00424   *pz->get_u_tilde()=0.0;
00425   for (i=0;i<nvar;i++)
00426   {
00427     z_bar_tilde[i]=0;
00428     z_dot_bar_tilde[i]=0;
00429   }
00430 }
00431 
00436 void read_pass1_init_3(void)
00437 {
00438   // We are going backword for bptr and forward for bptr2
00439   // the current entry+2 in bptr is the size of the record i.e
00440   // points to the next record
00441   int nvar=df1b2variable::nvar;
00442   fixed_smartlist & nlist=f1b2gradlist->nlist;
00443   test_smartlist& list=f1b2gradlist->list;
00444    // nlist-=sizeof(int);
00445   // get record size
00446   int num_bytes=nlist.bptr->numbytes;
00447   // backup the size of the record
00448   list-=num_bytes;
00449   list.saveposition(); // save pointer to beginning of record;
00450   // save the pointer to the beginning of the record
00451   //df1b2_header z;
00452   // df1b2function1 * pf;
00453 
00454   // get info from tape1
00455 #if defined(SAFE_ALL)
00456   checkidentiferstring("AZ",list);
00457 #endif
00458   df1b2_header * pz=(df1b2_header *) list.bptr;
00459   list.restoreposition(); // save pointer to beginning of record;
00460 
00461   *(pz->u_tilde)=0;
00462   for (int i=0;i<nvar;i++)
00463   {
00464     pz->u_dot_tilde[i]=0;
00465   }
00466 }