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