ADMB Documentation  11.1.2270
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn4.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2fn4.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_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 
00025 df1b2variable& df1b2variable::operator = (const df1b2variable& _x)
00026 {
00027   if (ptr != _x.ptr)
00028   {
00029     ADUNCONST(df1b2variable,x)
00030     if (allocated() )
00031     {
00032       //int nc=*ncopies;
00033       initialize();
00034       //*ncopies=nc;
00035       double * xd=x.get_u_dot();
00036       double * zd=get_u_dot();
00037       *get_u()=*x.get_u();
00038       for (int i=0;i<df1b2variable::nvar;i++)
00039       {
00040         *zd++ += *xd++;
00041       }
00042 
00043       // WRITE WHATEVER ON TAPE
00044       //df1b2tape->set_tapeinfo_header(&x,&z,this,xd);
00045       // save stuff for first reverse pass
00046       // need &x, &z, this,
00047 
00048       // !!! nov 1 02
00049       // !!! nov 11 02
00050       if (!df1b2_gradlist::no_derivatives)
00051         f1b2gradlist->write_save_pass2_tilde_values(this);
00052       if (!df1b2_gradlist::no_derivatives)
00053         f1b2gradlist->write_pass1_eq(&x,this);
00054     }
00055     else  // guts of copy constructor
00056     {
00057       if (x.ptr)
00058       {
00059         ptr=x.ptr;
00060         ncopies=x.ncopies;
00061         (*ncopies)++;
00062         u=x.u;
00063         u_dot=x.u_dot;
00064         u_bar=x.u_bar;
00065         u_dot_bar=x.u_dot_bar;
00066         u_tilde=x.u_tilde;
00067         u_dot_tilde=x.u_dot_tilde;
00068         u_bar_tilde=x.u_bar_tilde;
00069         u_dot_bar_tilde=x.u_dot_bar_tilde;
00070       }
00071       else
00072       {
00073         ptr=NULL;
00074         ncopies=NULL;
00075         u=NULL;
00076         u_dot=NULL;
00077         u_bar=NULL;
00078         u_dot_bar=NULL;
00079         u_tilde=NULL;
00080         u_dot_tilde=NULL;
00081         u_bar_tilde=NULL;
00082         u_dot_bar_tilde=NULL;
00083       }
00084     }
00085   }
00086   return *this;
00087 }
00088 
00089 void ad_read_pass1_eq(void);
00090 
00095 int df1b2_gradlist::write_pass1_eq(const df1b2variable * _px,
00096   df1b2variable * pz)
00097 {
00098   ADUNCONST(df1b2variable*,px)
00099   //char * pg=0;
00100   ncount++;
00101 #if defined(CHECK_COUNT)
00102   if (ncount >= ncount_check)
00103     ncount_checker(ncount,ncount_check);
00104 #endif
00105   //int nvar=df1b2variable::nvar;
00106 
00107   int total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00108 // string identifier debug stuff
00109 #if defined(SAFE_ALL)
00110   char ids[]="GV";
00111   int slen=strlen(ids);
00112   total_bytes+=slen;
00113 #endif
00114   list.check_buffer_size(total_bytes);
00115   void * tmpptr=list.bptr;
00116 #if defined(SAFE_ALL)
00117   memcpy(list,ids,slen);
00118 #endif
00119 
00120   memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
00121   memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00122 
00123   nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00124   nlist.bptr->pf=(ADrfptr)(&ad_read_pass1_eq);
00125   ++nlist;
00126   return 0;
00127 }
00128 
00133 void ad_read_pass1_eq(void)
00134 {
00135   switch(df1b2variable::passnumber)
00136   {
00137   case 1:
00138     read_pass1_eq_1();
00139     break;
00140   case 2:
00141     read_pass1_eq_2();
00142     break;
00143   case 3:
00144     read_pass1_eq_3();
00145     break;
00146   default:
00147     cerr << "illegal value for df1b2variable::pass = "
00148          << df1b2variable::passnumber << endl;
00149     exit(1);
00150   }
00151 }
00152 
00157 void read_pass1_eq_1(void)
00158 {
00159   // We are going backword for bptr and forward for bptr2
00160   // the current entry+2 in bptr is the size of the record i.e
00161   // points to the next record
00162   int nvar=df1b2variable::nvar;
00163   fixed_smartlist & nlist=f1b2gradlist->nlist;
00164   test_smartlist& list=f1b2gradlist->list;
00165    // nlist-=sizeof(int);
00166   // get record size
00167   int num_bytes=nlist.bptr->numbytes;
00168   // backup the size of the record
00169   list-=num_bytes;
00170   list.saveposition(); // save pointer to beginning of record;
00171 
00172 #if defined(SAFE_ALL)
00173   checkidentiferstring("GV",list);
00174 #endif
00175 
00176   // get info from tape1
00177   df1b2_header * px=(df1b2_header *) list.bptr;
00178   list.bptr+=sizeof(df1b2_header);
00179   df1b2_header * pz=(df1b2_header *) list.bptr;
00180   list.bptr+=sizeof(df1b2_header);
00181   list.restoreposition(); // save pointer to beginning of record;
00182   int i;
00183 
00184   // Do first reverse paSS calculations
00185   // ****************************************************************
00186   // turn this off if no third derivatives are calculated
00187   // if (!no_third_derivatives)
00188   // {
00189   // save for second reverse pass
00190   // save identifier 1
00191      fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
00192      test_smartlist& list2=f1b2gradlist->list2;
00193 
00194 
00195      int total_bytes=2*nvar*sizeof(double);
00196 #if defined(SAFE_ALL)
00197   char ids[]="HT";
00198   int slen=strlen(ids);
00199   total_bytes+=slen;
00200 #endif
00201   list2.check_buffer_size(total_bytes);
00202   void * tmpptr2=list2.bptr;
00203 #if defined(SAFE_ALL)
00204   memcpy(list2,ids,slen);
00205 #endif
00206 
00207 
00208      memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
00209      memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
00210      *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
00211      ++nlist2;
00212   // }
00213   //
00214   // ****************************************************************
00215 #if defined(PRINT_DERS)
00216  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00217  print_derivatives(pz,"z");
00218  print_derivatives(px,"x");
00219 #endif
00220 
00221   for (i=0;i<nvar;i++)
00222   {
00223     px->u_bar[i]+=pz->u_bar[i];
00224 #if defined(ADDEBUG_PRINT)
00225     addebug_count++;
00226     if (addebug_count == 49)
00227     {
00228       cout << "trap" << endl;
00229     }
00230     cout << px->u_bar[i] << " " << pz->u_bar[i] << " " << addebug_count << endl;
00231 #endif
00232   }
00233   for (i=0;i<nvar;i++)
00234   {
00235     px->u_dot_bar[i]+=pz->u_dot_bar[i];
00236   }
00237   for (i=0;i<nvar;i++)
00238   {
00239     pz->u_bar[i]=0;
00240   }
00241   for (i=0;i<nvar;i++)
00242   {
00243     pz->u_dot_bar[i]=0;
00244   }
00245 #if defined(PRINT_DERS)
00246  print_derivatives(px,"x");
00247  print_derivatives(pz,"z");
00248 #endif
00249 }
00250 
00255 void read_pass1_eq_2(void)
00256 {
00257   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00258   // We are going forward for bptr and backword for bptr2
00259   //
00260   // list 1
00261   //
00262   int nvar=df1b2variable::nvar;
00263   test_smartlist & list=f1b2gradlist->list;
00264 
00265   int total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
00266 #if defined(SAFE_ALL)
00267   char ids[]="GV";
00268   int slen=strlen(ids);
00269   total_bytes+=slen;
00270 #endif
00271   list.check_buffer_size(total_bytes);
00272 
00273   list.saveposition(); // save pointer to beginning of record;
00274   fixed_smartlist & nlist=f1b2gradlist->nlist;
00275    // nlist-=sizeof(int);
00276   // get record size
00277   int num_bytes=nlist.bptr->numbytes;
00278     // nlist+=nlist_record_size;
00279   //
00280   // list 2
00281   //
00282   test_smartlist & list2=f1b2gradlist->list2;
00283   fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
00284   // get record size
00285   int num_bytes2=*nlist2.bptr;
00286   --nlist2;
00287   // backup the size of the record
00288   list2-=num_bytes2;
00289   list2.saveposition(); // save pointer to beginning of record;
00290 #if defined(SAFE_ALL)
00291   checkidentiferstring("GV",list);
00292   checkidentiferstring("HT",list2);
00293 #endif
00294   // save the pointer to the beginning of the record
00295   // bptr and bptr2 now both point to the beginning of their records
00296 
00297   // get info from tape1
00298   df1b2_header * px=(df1b2_header *) list.bptr;
00299   list.bptr+=sizeof(df1b2_header);
00300   df1b2_header * pz=(df1b2_header *) list.bptr;
00301   list.restoreposition(num_bytes); // save pointer to beginning of record;
00302 
00303   //double* zbar=(double*)list2.bptr;
00304   //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
00305 
00306   list2.restoreposition(); // save pointer to beginning of record;
00307 
00308   double * x_bar_tilde=px->get_u_bar_tilde();
00309   double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
00310   double * z_bar_tilde=pz->get_u_bar_tilde();
00311   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00312   // Do second "reverse-reverse" pass calculations
00313 #if defined(PRINT_DERS)
00314  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00315  print_derivatives(pz,"z");
00316  print_derivatives(px,"x");
00317 #endif
00318 
00319   int i;
00320   for (i=0;i<nvar;i++)
00321   {
00322     z_bar_tilde[i]=0;
00323     z_dot_bar_tilde[i]=0;
00324   }
00325   for (i=0;i<nvar;i++)
00326   {
00327     z_bar_tilde[i]+=x_bar_tilde[i];
00328   }
00329 
00330   for (i=0;i<nvar;i++)
00331   {
00332     z_dot_bar_tilde[i]+=x_dot_bar_tilde[i];
00333   }
00334 #if defined(PRINT_DERS)
00335  print_derivatives(px,"x");
00336  print_derivatives(pz,"z");
00337 #endif
00338 }
00339 
00344 void read_pass1_eq_3(void)
00345 {
00346   // We are going backword for bptr and forward for bptr2
00347   // the current entry+2 in bptr is the size of the record i.e
00348   // points to the next record
00349   int nvar=df1b2variable::nvar;
00350   fixed_smartlist & nlist=f1b2gradlist->nlist;
00351   test_smartlist& list=f1b2gradlist->list;
00352    // nlist-=sizeof(int);
00353   // get record size
00354   int num_bytes=nlist.bptr->numbytes;
00355   // backup the size of the record
00356   list-=num_bytes;
00357   list.saveposition(); // save pointer to beginning of record;
00358   // save the pointer to the beginning of the record
00359 
00360 #if defined(SAFE_ALL)
00361   checkidentiferstring("GV",list);
00362 #endif
00363   // get info from tape1
00364   df1b2_header * px=(df1b2_header *) list.bptr;
00365   list.bptr+=sizeof(df1b2_header);
00366   df1b2_header * pz=(df1b2_header *) list.bptr;
00367   list.restoreposition(); // save pointer to beginning of record;
00368   int i;
00369 
00370 #if defined(PRINT_DERS)
00371  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
00372  print_derivatives(pz,"z");
00373  print_derivatives(px,"x");
00374 #endif
00375 
00376 
00377   *(px->u_tilde)+=*pz->u_tilde;
00378 #if defined(ADDEBUG_PRINT)
00379     addebug_count++;
00380     if (addebug_count == 49)
00381     {
00382       cout << "trap" << endl;
00383     }
00384     cout << *(px->u_tilde) << " " << *(pz->u_tilde) << " " << addebug_count
00385          << endl;
00386 #endif
00387   for (i=0;i<nvar;i++)
00388   {
00389     px->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00390   }
00391 #if defined(PRINT_DERS)
00392  print_derivatives(px,"x");
00393  print_derivatives(pz,"z");
00394 #endif
00395   *(pz->u_tilde)=0;
00396   for (i=0;i<nvar;i++)
00397   {
00398     pz->u_dot_tilde[i]=0;
00399   }
00400 }