ADMB Documentation  11.1.1890
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2min2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2min2.cpp 2011-07-27 add by weihai,created by Dave$
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 - (double x, const df1b2variable& _y)
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()=x-*y.get_u();
00020     for (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_minuscv(&y,&z);
00028     return z;
00029   }
00030 
00031 void ad_read_pass2_minuscv(void);
00032 
00033  int df1b2_gradlist::write_pass1_minuscv(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    int total_bytes=2*sizeof(df1b2_header);
00045 // string identifier debug stuff
00046 #if defined(SAFE_ALL)
00047   char ids[]="GU";
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_minuscv);
00063       ++nlist;
00064    return 0;
00065  }
00066 
00067 
00068 void read_pass2_1_minuscv(void);
00069 void read_pass2_2_minuscv(void);
00070 void read_pass2_3_minuscv(void);
00071 
00072 void ad_read_pass2_minuscv(void)
00073 {
00074   switch(df1b2variable::passnumber)
00075   {
00076   case 1:
00077     read_pass2_1_minuscv();
00078     break;
00079   case 2:
00080     read_pass2_2_minuscv();
00081     break;
00082   case 3:
00083     read_pass2_3_minuscv();
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_minuscv(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   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_ARRAYS)
00106   checkidentiferstring("GU",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   int i;
00123   for (i=0;i<nvar;i++)
00124   {
00125     py->u_bar[i]-=pz->u_bar[i];
00126   }
00127   for (i=0;i<nvar;i++)
00128   {
00129     py->u_dot_bar[i]-=pz->u_dot_bar[i];
00130   }
00131 
00132   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00133   for (i=0;i<nvar;i++)
00134   {
00135     pz->u_bar[i]=0;
00136   }
00137   for (i=0;i<nvar;i++)
00138   {
00139     pz->u_dot_bar[i]=0;
00140   }
00141 }
00142 
00143 void read_pass2_2_minuscv(void)
00144 {
00145   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00146   // We are going forward for bptr and backword for bptr2
00147   //
00148   // list 1
00149   //
00150   int nvar=df1b2variable::nvar;
00151   test_smartlist & list=f1b2gradlist->list;
00152 
00153   int total_bytes=2*sizeof(df1b2_header);
00154 // string identifier debug stuff
00155 #if defined(SAFE_ALL)
00156   char ids[]="BY";
00157   int slen=strlen(ids);
00158   total_bytes+=slen;
00159 #endif
00160   list.check_buffer_size(total_bytes);
00161 // end of string identifier debug stuff
00162 
00163   list.saveposition(); // save pointer to beginning of record;
00164   fixed_smartlist & nlist=f1b2gradlist->nlist;
00165   // get record size
00166   int num_bytes=nlist.bptr->numbytes;
00167   // get info from tape1
00168 #if defined(SAFE_ARRAYS)
00169   checkidentiferstring("GU",list);
00170 #endif
00171   df1b2_header * py=(df1b2_header *) list.bptr;
00172   list.bptr+=sizeof(df1b2_header);
00173   df1b2_header * pz=(df1b2_header *) list.bptr;
00174   list.restoreposition(num_bytes); // save pointer to beginning of record;
00175 
00176   double * y_bar_tilde=py->get_u_bar_tilde();
00177   double * y_dot_bar_tilde=py->get_u_dot_bar_tilde();
00178   double * z_bar_tilde=pz->get_u_bar_tilde();
00179   double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
00180   // Do second "reverse-reverse" pass calculations
00181   int i;
00182 
00183   for (i=0;i<nvar;i++)
00184   {
00185     z_bar_tilde[i]=0;
00186     z_dot_bar_tilde[i]=0;
00187   }
00188 
00189   // start with y and add x
00190   for (i=0;i<nvar;i++)
00191   {
00192     z_bar_tilde[i]-=y_bar_tilde[i];
00193   }
00194 
00195   for (i=0;i<nvar;i++)
00196   {
00197     z_dot_bar_tilde[i]-=y_dot_bar_tilde[i];
00198   }
00199 }
00200 
00201 void read_pass2_3_minuscv(void)
00202 {
00203   // We are going backword for bptr and forward for bptr2
00204   // the current entry+2 in bptr is the size of the record i.e
00205   // points to the next record
00206   int nvar=df1b2variable::nvar;
00207   fixed_smartlist & nlist=f1b2gradlist->nlist;
00208   test_smartlist& list=f1b2gradlist->list;
00209    // nlist-=sizeof(int);
00210   // get record size
00211   int num_bytes=nlist.bptr->numbytes;
00212   // backup the size of the record
00213   list-=num_bytes;
00214   list.saveposition(); // save pointer to beginning of record;
00215   // save the pointer to the beginning of the record
00216   //df1b2_header x,z;
00217 
00218   // get info from tape1
00219   // get info from tape1
00220 #if defined(SAFE_ARRAYS)
00221   checkidentiferstring("GU",list);
00222 #endif
00223   df1b2_header * py=(df1b2_header *) list.bptr;
00224   list.bptr+=sizeof(df1b2_header);
00225   df1b2_header * pz=(df1b2_header *) list.bptr;
00226   list.bptr+=sizeof(df1b2_header);
00227 
00228   list.restoreposition(); // save pointer to beginning of record;
00229   int i;
00230 
00231   *(py->u_tilde)-=*(pz->u_tilde);
00232   for (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 (i=0;i<nvar;i++)
00238   {
00239     pz->u_dot_tilde[i]=0;
00240   }
00241 }