ADMB Documentation  11.2.2853
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn5.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2fn5.cpp 2601 2014-11-08 22:34:58Z 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   list.check_buffer_size(total_bytes);
00202   void * tmpptr=list.bptr;
00203 
00204 #if defined(SAFE_ALL)
00205   memcpy(list,ids,slen);
00206 #endif
00207 // end of string identifier debug stuff
00208 
00209   memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00210   //list.bptr+=sizeof(df1b2_header);
00211   // ***************** don't need this ***************
00212   memcpy(list,&pg,sizeof(char *));
00213   //*(char**)(list.bptr)=(char*)pg;
00214   //list.bptr+=sizeof(char*);
00215   // ***** write  record size
00216   nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00217   nlist.bptr->pf=(ADrfptr)(&ad_read_pass1_initialize);
00218   ++nlist;
00219   return 0;
00220 }
00221 
00226 void ad_read_pass1_initialize(void)
00227 {
00228   switch(df1b2variable::passnumber)
00229   {
00230   case 1:
00231     read_pass1_init_1();
00232     break;
00233   case 2:
00234     read_pass1_init_2();
00235     break;
00236   case 3:
00237     read_pass1_init_3();
00238     break;
00239   default:
00240     cerr << "illegal value for df1b2variable::pass = "
00241          << df1b2variable::passnumber << endl;
00242     exit(1);
00243   }
00244 }
00245 
00250 void checkidentiferstring(const char * ids,test_smartlist& list)
00251 {
00252   size_t slen=strlen(ids);
00253   char * s=(char*)list.bptr;
00254   int ss=strncmp(ids,s,slen);
00255   if (ss)
00256   {
00257     cerr << "Error in string identifer in list value should be " << ids
00258      << endl << " it appears to be " << s[0] << s[1] << s[2] << s[3]
00259      << " ... " << endl;
00260     ad_exit(1);
00261   }
00262   list.bptr+=slen;
00263 }
00264 
00269 void read_pass1_init_1(void)
00270 {
00271   // We are going backword for bptr and forward for bptr2
00272   // the current entry+2 in bptr is the size of the record i.e
00273   // points to the next record
00274   unsigned int nvar=df1b2variable::nvar;
00275 
00276   fixed_smartlist & nlist=f1b2gradlist->nlist;
00277   test_smartlist& list=f1b2gradlist->list;
00278    // nlist-=sizeof(int);
00279   // get record size
00280   int num_bytes=nlist.bptr->numbytes;
00281   // backup the size of the record
00282   list-=num_bytes;
00283   list.saveposition(); // save pointer to beginning of record;
00284   // save the pointer to the beginning of the record
00285   //df1b2_header z;
00286 
00287   // get info from tape1
00288 #if defined(SAFE_ALL)
00289   checkidentiferstring("AZ",list);
00290 #endif
00291   df1b2_header * pz=(df1b2_header *) list.bptr;
00292   list.bptr+=sizeof(df1b2_header);
00293   //df1b2function1* pf = *(df1b2function1**)list.bptr;
00294   list.bptr+=sizeof(char*);
00295   list.restoreposition(); // save pointer to beginning of record;
00296 
00297   // Do first reverse paSS calculations
00298   // ****************************************************************
00299   // turn this off if no third derivatives are calculated
00300   // if (!no_third_derivatives)
00301   // {
00302   // save for second reverse pass
00303   // save identifier 1
00304      fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
00305      test_smartlist& list2=f1b2gradlist->list2;
00306 
00307   size_t total_bytes=2*nvar*sizeof(double);
00308 
00309 // string identifier debug stuff
00310 #if defined(SAFE_ALL)
00311   char ids[]="EF";
00312   size_t slen=strlen(ids);
00313   total_bytes+=slen;
00314 #endif
00315 
00316   list2.check_buffer_size(total_bytes);
00317 
00318   void * tmpptr2=list2.bptr;
00319 #if defined(SAFE_ALL)
00320   memcpy(list2,ids,slen);
00321 #endif
00322 
00323   const int sizeofdouble = sizeof(double);
00324   memcpy(list2,pz->get_u_bar(),nvar*sizeofdouble);
00325   //list2.bptr+=nvar*sizeof(double);
00326   memcpy(list2,pz->get_u_dot_bar(),nvar*sizeofdouble);
00327   //list2.bptr+=nvar*sizeof(double);
00328   *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
00329   ++nlist2;
00330   // }
00331   //
00332   // ****************************************************************
00333 
00334   // This should *NEVER* be used (I think)
00335  /*
00336   for (i=0;i<nvar;i++)
00337   {
00338     pz->u_bar[i]=0;
00339   }
00340   for (i=0;i<nvar;i++)
00341   {
00342     pz->u_dot_bar[i]=0;
00343   }
00344   */
00345 }
00346 
00351 void read_pass1_init_2(void)
00352 {
00353   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00354   // We are going forward for bptr and backword for bptr2
00355   //
00356   // list 1
00357   //
00358   unsigned int nvar = df1b2variable::nvar;
00359   test_smartlist & list=f1b2gradlist->list;
00360 
00361   size_t total_bytes=sizeof(df1b2_header)+sizeof(char *);
00362 // string identifier debug stuff
00363 #if defined(SAFE_ALL)
00364   char ids[]="AZ";
00365   int slen=strlen(ids);
00366   total_bytes+=slen;
00367 #endif
00368 // end of string identifier debug stuff
00369   list.check_buffer_size(total_bytes);
00370 
00371   list.saveposition(); // save pointer to beginning of record;
00372   fixed_smartlist & nlist=f1b2gradlist->nlist;
00373    // nlist-=sizeof(int);
00374   // get record size
00375   int num_bytes=nlist.bptr->numbytes;
00376     // nlist+=nlist_record_size;
00377   //
00378   // list 2
00379   //
00380   test_smartlist & list2=f1b2gradlist->list2;
00381   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00382   // get record size
00383   int num_bytes2=*nlist2.bptr;
00384   --nlist2;
00385   // backup the size of the record
00386   list2-=num_bytes2;
00387   // save the pointer to the beginning of the record
00388   // bptr and bptr2 now both point to the beginning of their records
00389 
00390   //df1b2_header z;
00391 
00392   // get info from tape1
00393   list2.saveposition(); // save pointer to beginning of record;
00394 #if defined(SAFE_ALL)
00395   checkidentiferstring("AZ",list);
00396   checkidentiferstring("EF",list2);
00397 #endif
00398   df1b2_header * pz=(df1b2_header *) list.bptr;
00399   list.bptr+=sizeof(df1b2_header);
00400   //df1b2function1 * pf = *(df1b2function1**)list.bptr;
00401   list.bptr+=sizeof(char*);
00402   list.restoreposition(num_bytes); // save pointer to beginning of record;
00403 
00404   //double* zbar=(double*)list2.bptr;
00405   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00406 
00407   list2.restoreposition(); // save pointer to beginning of record;
00408 
00409   double * z_bar_tilde=pz->get_u_bar_tilde();
00410   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00411   // Do second "reverse-reverse" pass calculations
00412 
00413   // !!!! oct31 02
00414 
00415   *pz->get_u_tilde()=0.0;
00416   for (size_t i=0;i<nvar;i++)
00417   {
00418     z_bar_tilde[i]=0;
00419     z_dot_bar_tilde[i]=0;
00420   }
00421 }
00422 
00427 void read_pass1_init_3(void)
00428 {
00429   // We are going backword for bptr and forward for bptr2
00430   // the current entry+2 in bptr is the size of the record i.e
00431   // points to the next record
00432   unsigned int nvar = df1b2variable::nvar;
00433   fixed_smartlist & nlist=f1b2gradlist->nlist;
00434   test_smartlist& list=f1b2gradlist->list;
00435    // nlist-=sizeof(int);
00436   // get record size
00437   int num_bytes=nlist.bptr->numbytes;
00438   // backup the size of the record
00439   list-=num_bytes;
00440   list.saveposition(); // save pointer to beginning of record;
00441   // save the pointer to the beginning of the record
00442   //df1b2_header z;
00443   // df1b2function1 * pf;
00444 
00445   // get info from tape1
00446 #if defined(SAFE_ALL)
00447   checkidentiferstring("AZ",list);
00448 #endif
00449   df1b2_header * pz=(df1b2_header *) list.bptr;
00450   list.restoreposition(); // save pointer to beginning of record;
00451 
00452   *(pz->u_tilde)=0;
00453   for (size_t i=0;i<nvar;i++)
00454   {
00455     pz->u_dot_tilde[i]=0;
00456   }
00457 }