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