ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn7.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 //#define  ADDEBUG_PRINT
00014 #if defined(ADDEBUG_PRINT)
00015     int addebug_count=0;
00016 #endif
00017 
00018 #ifndef OPT_LIB
00019   #include <cassert>
00020   #include <climits>
00021 #endif
00022 
00023 void ad_read_write_tilde_values(void);
00024 void read_tilde_values_1(void);
00025 void read_tilde_values_2(void);
00026 void read_tilde_values_3(void);
00027 
00032 int df1b2_gradlist::write_save_pass2_tilde_values(const df1b2variable* _px)
00033 {
00034   ADUNCONST(df1b2variable*, px)
00035   ncount++;
00036 #if defined(CHECK_COUNT)
00037   if (ncount >= ncount_check)
00038     ncount_checker(ncount,ncount_check);
00039 #endif
00040   size_t total_bytes=sizeof(df1b2_header);
00041 
00042 #if defined(SAFE_ALL)
00043   char ids[]="YS";
00044   size_t slen=strlen(ids);
00045   total_bytes+=slen;
00046 #endif
00047   list.check_buffer_size(total_bytes);
00048   void* tmpptr=list.bptr;
00049 #if defined(SAFE_ALL)
00050   memcpy(list,ids,slen);
00051 #endif
00052 
00053   memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
00054   //list.bptr+=sizeof(df1b2_header);
00055   // ***** write  record size
00056   nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
00057   nlist.bptr->pf=(ADrfptr)(&ad_read_write_tilde_values);
00058   ++nlist;
00059 
00060   return 0;
00061 }
00062 
00067 void ad_read_write_tilde_values(void)
00068 {
00069   switch(df1b2variable::passnumber)
00070   {
00071   case 1:
00072     read_tilde_values_1();
00073     break;
00074   case 2:
00075     read_tilde_values_2();
00076     break;
00077   case 3:
00078     read_tilde_values_3();
00079     break;
00080   default:
00081     cerr << "illegal value for df1b2variable::pass = "
00082          << df1b2variable::passnumber << endl;
00083     exit(1);
00084   }
00085 }
00086 
00091 void read_tilde_values_1(void)
00092 {
00093   // We are going backword for bptr and nbptr
00094   // and  forward for bptr2 and nbptr2
00095   // the current entry+2 in bptr is the size of the record i.e
00096   // points to the next record
00097   unsigned int nvar=df1b2variable::nvar;
00098   fixed_smartlist & nlist=f1b2gradlist->nlist;
00099   test_smartlist& list=f1b2gradlist->list;
00100    // nlist-=sizeof(int);
00101   // get record size
00102   int num_bytes=nlist.bptr->numbytes;
00103   // backup the size of the record
00104   list-=num_bytes;
00105   list.saveposition(); // save pointer to beginning of record;
00106 #if defined(SAFE_ALL)
00107   checkidentiferstring("YS",list);
00108 #endif
00109   df1b2_header * px=(df1b2_header *) list.bptr; // we don't need this here
00110   list.restoreposition(); // save pointer to beginning of record;
00111 
00112   // don't need this here for = since it zeroes this out.
00113   for (unsigned int i=0;i<nvar;i++)
00114   {
00115     px->u_bar[i]=0;
00116     px->u_dot_bar[i]=0;
00117   }
00118 }
00119 
00124 void read_tilde_values_2(void)
00125 {
00126   //const int nlist_record_size=sizeof(int)+sizeof(char*);
00127   // We are going forward for bptr and backword for bptr2
00128   //
00129   // list 1
00130   //
00131   unsigned int nvar=df1b2variable::nvar;
00132   test_smartlist & list=f1b2gradlist->list;
00133 
00134   size_t total_bytes=sizeof(df1b2_header);
00135 #if defined(SAFE_ALL)
00136   char ids[]="YS";
00137   size_t slen=strlen(ids);
00138   total_bytes+=slen;
00139 #endif
00140   list.check_buffer_size(total_bytes);
00141   list.saveposition(); // save pointer to beginning of record;
00142 #if defined(SAFE_ALL)
00143   checkidentiferstring("YS",list);
00144 #endif
00145 
00146   //fixed_smartlist & nlist=f1b2gradlist->nlist;
00147    // nlist-=sizeof(int);
00148   // get record size
00149   //int num_bytes=nlist.bptr->numbytes;
00150     // nlist+=nlist_record_size;
00151   df1b2_header* px = (df1b2_header*)list.bptr;
00152   //
00153   // list 3
00154   //
00155   fixed_smartlist2& nlist3=f1b2gradlist->nlist3;
00156   test_smartlist& list3=f1b2gradlist->list3;
00157 
00158   size_t total_bytes2=(nvar+1)*sizeof(double);
00159 
00160 #if defined(SAFE_ALL)
00161   char ids2[]="WF";
00162   size_t slen2=strlen(ids2);
00163   total_bytes2+=slen2;
00164 #endif
00165 
00166   list3.check_buffer_size(total_bytes2);
00167 
00168   void * tmpptr3=list3.bptr;
00169 #if defined(SAFE_ALL)
00170   memcpy(list3,ids2,slen);
00171 #endif
00172 
00173   const size_t sizeofdouble = sizeof(double);
00174   const size_t size = sizeofdouble * nvar;
00175   memcpy(list3, px->get_u_tilde(), sizeofdouble);
00176   memcpy(list3, px->get_u_dot_tilde(), size);
00177 
00178   memset(px->get_u_bar_tilde(), 0, size);
00179   memset(px->get_u_dot_bar_tilde(), 0, size);
00180   memset(px->get_u_dot_tilde(), 0, size);
00181   memset(px->get_u_tilde(), 0, sizeofdouble);
00182 
00183   *nlist3.bptr=adptr_diff(list3.bptr,tmpptr3);
00184   list.bptr+=sizeof(df1b2_header);
00185   ++nlist3;
00186 }
00187 
00192 void read_tilde_values_3(void)
00193 {
00194   // We are going backword for bptr and forward for bptr2
00195   // the current entry+2 in bptr is the size of the record i.e
00196   // points to the next record
00197   unsigned int nvar=df1b2variable::nvar;
00198   fixed_smartlist & nlist=f1b2gradlist->nlist;
00199   test_smartlist& list=f1b2gradlist->list;
00200    // nlist-=sizeof(int);
00201   // get record size
00202   int num_bytes=nlist.bptr->numbytes;
00203   // backup the size of the record
00204   list-=num_bytes;
00205   list.saveposition(); // save pointer to beginning of record;
00206 #if defined(SAFE_ALL)
00207   checkidentiferstring("YS",list);
00208 #endif
00209   df1b2_header * px=(df1b2_header *) list.bptr;
00210   list.restoreposition(); // save pointer to beginning of record;
00211   // save the pointer to the beginning of the record
00212   fixed_smartlist2& nlist3=f1b2gradlist->nlist3;
00213   test_smartlist& list3=f1b2gradlist->list3;
00214   --nlist3;
00215   // get record size
00216   int num_bytes3=*nlist3.bptr;
00217   // backup the size of the record
00218   list3-=num_bytes3;
00219   list3.saveposition(); // save pointer to beginning of record;
00220 #if defined(SAFE_ALL)
00221   checkidentiferstring("WF",list3);
00222 #endif
00223   memcpy(px->get_u_tilde(),list3.bptr,sizeof(double));
00224   list3.bptr+=sizeof(double);
00225   memcpy(px->get_u_dot_tilde(),list3.bptr,nvar*sizeof(double));
00226   list3.restoreposition(); // save pointer to beginning of record;
00227 }