ADMB Documentation  11.1.2438
 All Classes Files Functions Variables Typedefs Friends Defines
df1b2fn7.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: df1b2fn7.cpp 2346 2014-09-15 21:57:43Z 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,int 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   int total_bytes=sizeof(df1b2_header);
00042 
00043 #if defined(SAFE_ALL)
00044   char ids[]="YS";
00045   int 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   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 (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   int nvar=df1b2variable::nvar;
00133   test_smartlist & list=f1b2gradlist->list;
00134 
00135   int total_bytes=sizeof(df1b2_header);
00136 #if defined(SAFE_ALL)
00137   char ids[]="YS";
00138   int 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 #ifndef OPT_LIB
00168   assert(total_bytes2 <= INT_MAX);
00169 #endif
00170 
00171   list3.check_buffer_size((int)total_bytes2);
00172 
00173   void * tmpptr3=list3.bptr;
00174 #if defined(SAFE_ALL)
00175   memcpy(list3,ids2,slen);
00176 #endif
00177 
00178   const int sizeofdouble = (int)sizeof(double);
00179   memcpy(list3,px->get_u_tilde(),sizeofdouble);
00180   memcpy(list3,px->get_u_dot_tilde(),nvar*sizeofdouble);
00181 
00182   zero_it(px->get_u_bar_tilde(),nvar);
00183   zero_it(px->get_u_dot_bar_tilde(),nvar);
00184   zero_it(px->get_u_dot_tilde(),nvar);
00185   zero_it(px->get_u_tilde(),1);
00186   *nlist3.bptr=adptr_diff(list3.bptr,tmpptr3);
00187   list.bptr+=sizeof(df1b2_header);
00188   ++nlist3;
00189 }
00190 
00195 void read_tilde_values_3(void)
00196 {
00197   // We are going backword for bptr and forward for bptr2
00198   // the current entry+2 in bptr is the size of the record i.e
00199   // points to the next record
00200   int nvar=df1b2variable::nvar;
00201   fixed_smartlist & nlist=f1b2gradlist->nlist;
00202   test_smartlist& list=f1b2gradlist->list;
00203    // nlist-=sizeof(int);
00204   // get record size
00205   int num_bytes=nlist.bptr->numbytes;
00206   // backup the size of the record
00207   list-=num_bytes;
00208   list.saveposition(); // save pointer to beginning of record;
00209 #if defined(SAFE_ALL)
00210   checkidentiferstring("YS",list);
00211 #endif
00212   df1b2_header * px=(df1b2_header *) list.bptr;
00213   list.restoreposition(); // save pointer to beginning of record;
00214   // save the pointer to the beginning of the record
00215   fixed_smartlist2& nlist3=f1b2gradlist->nlist3;
00216   test_smartlist& list3=f1b2gradlist->list3;
00217   --nlist3;
00218   // get record size
00219   int num_bytes3=*nlist3.bptr;
00220   // backup the size of the record
00221   list3-=num_bytes3;
00222   list3.saveposition(); // save pointer to beginning of record;
00223 #if defined(SAFE_ALL)
00224   checkidentiferstring("WF",list3);
00225 #endif
00226   memcpy(px->get_u_tilde(),list3.bptr,sizeof(double));
00227   list3.bptr+=sizeof(double);
00228   memcpy(px->get_u_dot_tilde(),list3.bptr,nvar*sizeof(double));
00229   list3.restoreposition(); // save pointer to beginning of record;
00230 }
00231 
00236 void zero_it(double * p,int n)
00237 {
00238   for (int i=0;i<n;i++) *p++=0.0;
00239 }