ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2min3.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <df1b2fun.h>
00012 
00013   df1b2variable operator - (const df1b2variable& _y,double x)
00014   {
00015     ADUNCONST(df1b2variable,y)
00016     df1b2variable z;
00017     double * yd=y.get_u_dot();
00018     double * zd=z.get_u_dot();
00019     *z.get_u()=*y.get_u()-x;
00020     for (unsigned int i=0;i<df1b2variable::nvar;i++)
00021     {
00022       *zd++ = *yd++;
00023     }
00024 
00025     // WRITE WHATEVER ON TAPE
00026     if (!df1b2_gradlist::no_derivatives)
00027       f1b2gradlist->write_pass1_minusvc(&y,&z);
00028     return z;
00029   }
00030 
00031 void ad_read_pass2_minusvc(void);
00032 
00033  int df1b2_gradlist::write_pass1_minusvc(const df1b2variable * _py,
00034    df1b2variable * pz)
00035  {
00036    ADUNCONST(df1b2variable*,py)
00037    ncount++;
00038 #if defined(CHECK_COUNT)
00039   if (ncount >= ncount_check)
00040     cout << ncount << endl;
00041 #endif
00042    //int nvar=df1b2variable::nvar;
00043 
00044    size_t total_bytes=2*sizeof(df1b2_header);
00045 // string identifier debug stuff
00046 #if defined(SAFE_ALL)
00047   char ids[]="LK";
00048   int slen=strlen(ids);
00049   total_bytes+=slen;
00050 #endif
00051   list.check_buffer_size(total_bytes);
00052   void * tmpptr=list.bptr;
00053 #if defined(SAFE_ALL)
00054   memcpy(list,ids,slen);
00055 #endif
00056 // end of string identifier debug stuff
00057 
00058    memcpy(list,(df1b2_header*)(py),sizeof(df1b2_header));
00059    memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
00060    // ***** write  record size
00061    nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00062    nlist.bptr->pf=(ADrfptr)(&ad_read_pass2_minusvc);
00063       ++nlist;
00064    return 0;
00065  }
00066 
00067 
00068 void read_pass2_1_minusvc(void);
00069 void read_pass2_2_minusvc(void);
00070 void read_pass2_3_minusvc(void);
00071 
00072 void ad_read_pass2_minusvc(void)
00073 {
00074   switch(df1b2variable::passnumber)
00075   {
00076   case 1:
00077     read_pass2_1_minusvc();
00078     break;
00079   case 2:
00080     read_pass2_2_minusvc();
00081     break;
00082   case 3:
00083     read_pass2_3_minusvc();
00084     break;
00085   default:
00086     cerr << "illegal value for df1b2variable::pass = "
00087          << df1b2variable::passnumber << endl;
00088     exit(1);
00089   }
00090 }
00091 
00092 void read_pass2_1_minusvc(void)
00093 {
00094   // We are going backword for bptr and nbptr
00095   // and  forward for bptr2 and nbptr2
00096   // the current entry+2 in bptr is the size of the record i.e
00097   // points to the next record
00098   unsigned int nvar=df1b2variable::nvar;
00099   test_smartlist& list=f1b2gradlist->list;
00100   int num_bytes=f1b2gradlist->nlist.bptr->numbytes;
00101   list-=num_bytes;
00102   list.saveposition(); // save pointer to beginning of record;
00103 
00104   // get info from tape1
00105 #if defined(SAFE_ALL)
00106   checkidentiferstring("LK",f1b2gradlist->list);
00107 #endif
00108   char * bptr=f1b2gradlist->list.bptr;
00109   df1b2_header * py=(df1b2_header *) bptr;
00110   bptr+=sizeof(df1b2_header);
00111   df1b2_header * pz=(df1b2_header *) bptr;
00112 
00113   list.restoreposition(); // save pointer to beginning of record;
00114 
00115   // ****************************************************************
00116   // turn this off if no third derivatives are calculated
00117   // if (!no_third_derivatives)
00118   // {
00119   // save for second reverse pass
00120   // save identifier 1
00121 
00122   for (unsigned int i=0;i<nvar;i++)
00123   {
00124     py->u_bar[i]+=pz->u_bar[i];
00125   }
00126   for (unsigned int i=0;i<nvar;i++)
00127   {
00128     py->u_dot_bar[i]+=pz->u_dot_bar[i];
00129   }
00130 
00131   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00132   for (unsigned int i=0;i<nvar;i++)
00133   {
00134     pz->u_bar[i]=0;
00135   }
00136   for (unsigned int i=0;i<nvar;i++)
00137   {
00138     pz->u_dot_bar[i]=0;
00139   }
00140 }
00141 
00142 void read_pass2_2_minusvc(void)
00143 {
00144   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00145   // We are going forward for bptr and backword for bptr2
00146   //
00147   // list 1
00148   //
00149   unsigned int nvar=df1b2variable::nvar;
00150   test_smartlist & list=f1b2gradlist->list;
00151 
00152   size_t total_bytes=2*sizeof(df1b2_header);
00153 // string identifier debug stuff
00154 #if defined(SAFE_ALL)
00155   char ids[]="BY";
00156   int slen=strlen(ids);
00157   total_bytes+=slen;
00158 #endif
00159   list.check_buffer_size(total_bytes);
00160 // end of string identifier debug stuff
00161 
00162   list.saveposition(); // save pointer to beginning of record;
00163   fixed_smartlist & nlist=f1b2gradlist->nlist;
00164   // get record size
00165   int num_bytes=nlist.bptr->numbytes;
00166   // get info from tape1
00167 #if defined(SAFE_ALL)
00168   checkidentiferstring("LK",list);
00169 #endif
00170   df1b2_header * py=(df1b2_header *) list.bptr;
00171   list.bptr+=sizeof(df1b2_header);
00172   df1b2_header * pz=(df1b2_header *) list.bptr;
00173   list.restoreposition(num_bytes); // save pointer to beginning of record;
00174 
00175   double * y_bar_tilde=py->get_u_bar_tilde();
00176   double * y_dot_bar_tilde=py->get_u_dot_bar_tilde();
00177   double * z_bar_tilde=pz->get_u_bar_tilde();
00178   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00179   // Do second "reverse-reverse" pass calculations
00180 
00181   for (unsigned int i=0;i<nvar;i++)
00182   {
00183     z_bar_tilde[i]=0;
00184     z_dot_bar_tilde[i]=0;
00185   }
00186 
00187 
00188   // start with y and add x
00189   for (unsigned int i=0;i<nvar;i++)
00190   {
00191     z_bar_tilde[i]+=y_bar_tilde[i];
00192   }
00193 
00194   for (unsigned int i=0;i<nvar;i++)
00195   {
00196     z_dot_bar_tilde[i]+=y_dot_bar_tilde[i];
00197   }
00198 }
00199 
00200 void read_pass2_3_minusvc(void)
00201 {
00202   // We are going backword for bptr and forward for bptr2
00203   // the current entry+2 in bptr is the size of the record i.e
00204   // points to the next record
00205   unsigned int nvar=df1b2variable::nvar;
00206   fixed_smartlist & nlist=f1b2gradlist->nlist;
00207   test_smartlist& list=f1b2gradlist->list;
00208    // nlist-=sizeof(int);
00209   // get record size
00210   int num_bytes=nlist.bptr->numbytes;
00211   // backup the size of the record
00212   list-=num_bytes;
00213   list.saveposition(); // save pointer to beginning of record;
00214   // save the pointer to the beginning of the record
00215   //df1b2_header x,z;
00216 
00217   // get info from tape1
00218   // get info from tape1
00219 #if defined(SAFE_ALL)
00220   checkidentiferstring("LK",list);
00221 #endif
00222   df1b2_header * py=(df1b2_header *) list.bptr;
00223   list.bptr+=sizeof(df1b2_header);
00224   df1b2_header * pz=(df1b2_header *) list.bptr;
00225   list.bptr+=sizeof(df1b2_header);
00226 
00227   list.restoreposition(); // save pointer to beginning of record;
00228 
00229   // !!!!! changed sign DF feb 7 2010
00230   //*(py->u_tilde)-=*(pz->u_tilde);
00231   *(py->u_tilde)+=*(pz->u_tilde);
00232   for (unsigned int i=0;i<nvar;i++)
00233   {
00234     py->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00235   }
00236   *(pz->u_tilde)=0;
00237   for (unsigned int i=0;i<nvar;i++)
00238   {
00239     pz->u_dot_tilde[i]=0;
00240   }
00241 }