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