ADMB Documentation  11.1.2490
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2min3.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2min3.cpp 1917 2014-04-22 18:23:30Z johnoel $
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 (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    int 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   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   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_minusvc(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_ALL)
00169   checkidentiferstring("LK",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 
00190   // start with y and add x
00191   for (i=0;i<nvar;i++)
00192   {
00193     z_bar_tilde[i]+=y_bar_tilde[i];
00194   }
00195 
00196   for (i=0;i<nvar;i++)
00197   {
00198     z_dot_bar_tilde[i]+=y_dot_bar_tilde[i];
00199   }
00200 }
00201 
00202 void read_pass2_3_minusvc(void)
00203 {
00204   // We are going backword for bptr and forward for bptr2
00205   // the current entry+2 in bptr is the size of the record i.e
00206   // points to the next record
00207   int nvar=df1b2variable::nvar;
00208   fixed_smartlist & nlist=f1b2gradlist->nlist;
00209   test_smartlist& list=f1b2gradlist->list;
00210    // nlist-=sizeof(int);
00211   // get record size
00212   int num_bytes=nlist.bptr->numbytes;
00213   // backup the size of the record
00214   list-=num_bytes;
00215   list.saveposition(); // save pointer to beginning of record;
00216   // save the pointer to the beginning of the record
00217   //df1b2_header x,z;
00218 
00219   // get info from tape1
00220   // get info from tape1
00221 #if defined(SAFE_ALL)
00222   checkidentiferstring("LK",list);
00223 #endif
00224   df1b2_header * py=(df1b2_header *) list.bptr;
00225   list.bptr+=sizeof(df1b2_header);
00226   df1b2_header * pz=(df1b2_header *) list.bptr;
00227   list.bptr+=sizeof(df1b2_header);
00228 
00229   list.restoreposition(); // save pointer to beginning of record;
00230   int i;
00231 
00232   // !!!!! changed sign DF feb 7 2010
00233   //*(py->u_tilde)-=*(pz->u_tilde);
00234   *(py->u_tilde)+=*(pz->u_tilde);
00235   for (i=0;i<nvar;i++)
00236   {
00237     py->u_dot_tilde[i]+=pz->u_dot_tilde[i];
00238   }
00239   *(pz->u_tilde)=0;
00240   for (i=0;i<nvar;i++)
00241   {
00242     pz->u_dot_tilde[i]=0;
00243   }
00244 }