ADMB Documentation  11.1.1907
 All Classes Files Functions Variables Typedefs Friends Defines
newfunc.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: newfunc.cpp 1667 2014-02-24 23:53:59Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <admodel.h>
00012 #include <df1b2fun.h>
00013 #include "f1b2locl.h"
00014 #include <adrndeff.h>
00015 
00016 static void xxx(double*,double*){;}
00017 //static void xxx(double[]){;}
00018 
00023 static void begin_local_calculations(int& nap)
00024 {
00025   df1b2_gradlist::set_no_derivatives();
00026   local_init_var::allocate_all();
00027   local_dep_var::allocate_all();
00028   nap=local_init_var::num_active_parameters;
00029 }
00030 
00031 int write_pass1(const df1b2variable * _px,
00032   const df1b2variable * _py,
00033   const df1b2variable * _pz,int nap);
00034 
00039 df1b2variable plocal_tester(int i,const df1b2variable& x ,
00040   const df1b2variable& y)
00041 {
00042   df1b2variable z;
00043   z=square(x+2.0*y+2.5);
00044   return z;
00045 }
00046 
00051 df1b2variable   local_tester(int i,local_init_df1b2variable x ,
00052   local_init_df1b2variable y)
00053 {
00054   int nap;
00055   local_dep_df1b2variable z;
00056   {
00057     begin_local_calculations(nap);
00058     z=square(x+2.0*y+2.5);
00059     local_init_var::end_local_calculations();
00060     // WRITE WHATEVER ON TAPE
00061   }
00062   if (!df1b2_gradlist::no_derivatives)
00063   {
00064     write_pass1(x.p,y.p,z.p,nap);
00065     //f1b2gradlist->write_pass1(&x,&y);
00066   }
00067   return *(z.p);
00068 }
00069  void ad_read_xxx(void);
00070 
00075 int write_pass1(const df1b2variable * px,
00076   const df1b2variable * py,
00077   const df1b2variable * pz,int nap)
00078  {
00079    //ADUNCONST(local_init_df1b2variable *,px)
00080 #if defined(CHECK_COUNT)
00081   if (ncount >= ncount_check)
00082     cout << ncount << endl;
00083 #endif
00084    int nvar=df1b2variable::nvar;
00085 
00086    //int total_bytes=3*sizeof(df1b2_header)+sizeof(char*)
00087    //  +2*(nvar+1)*sizeof(double);
00088    int total_bytes=3*sizeof(df1b2_header)
00089      +2*(nvar+1)*sizeof(double);
00090 // string identifier debug stuff
00091 #if defined(SAFE_ALL)
00092   char ids[]="DL";
00093   int slen=strlen(ids);
00094   total_bytes+=2*slen;
00095 #endif
00096   f1b2gradlist->list.check_buffer_size(total_bytes);
00097   void * tmpptr=f1b2gradlist->list.bptr;
00098 #if defined(SAFE_ALL)
00099   memcpy(f1b2gradlist->list,ids,slen);
00100 #endif
00101 // end of string identifier debug stuff
00102    df1b2_header& tmphead1=(df1b2_header&)(*px);
00103    df1b2_header& tmphead2=(df1b2_header&)(*py);
00104    df1b2_header& dephead1=(df1b2_header&)(*pz);
00105 
00106    // copy the headers
00107    memcpy(f1b2gradlist->list,&tmphead1,sizeof(df1b2_header));
00108    memcpy(f1b2gradlist->list,&tmphead2,sizeof(df1b2_header));
00109    memcpy(f1b2gradlist->list,&dephead1,sizeof(df1b2_header));
00110 
00111    // copy u
00112    memcpy(f1b2gradlist->list,px->get_u(),sizeof(double));
00113    memcpy(f1b2gradlist->list,py->get_u(),sizeof(double));
00114    memcpy(f1b2gradlist->list,pz->get_u(),sizeof(double));
00115 
00116    // copy u_dot
00117    int gnv=((twointsandptr*)(px->ptr))->nvar;
00118    memcpy(f1b2gradlist->list,&gnv,sizeof(int));
00119    memcpy(f1b2gradlist->list,&nap,sizeof(int));
00120    memcpy(f1b2gradlist->list,px->get_u_dot(),gnv*sizeof(double));
00121    memcpy(f1b2gradlist->list,py->get_u_dot(),gnv*sizeof(double));
00122    memcpy(f1b2gradlist->list,pz->get_u_dot(),gnv*sizeof(double));
00123 
00124 #if defined(SAFE_ALL)
00125   char ids2[]="EL";
00126   int slen2=strlen(ids2);
00127   memcpy(f1b2gradlist->list,ids2,slen2);
00128 #endif
00129    // ***** write  record size
00130    f1b2gradlist->nlist.bptr->numbytes=
00131       adptr_diff(f1b2gradlist->list.bptr,tmpptr);
00132    f1b2gradlist->nlist.bptr->pf=(ADrfptr)(&ad_read_xxx);
00133       ++(f1b2gradlist->nlist);
00134    return 0;
00135  }
00136 
00141 void read_xxx_1(void)
00142 {
00143   // We are going backword for bptr and forward for bptr2
00144   // the current entry+2 in bptr is the size of the record i.e
00145   // points to the next record
00146   //int nvar=df1b2variable::nvar;
00147   fixed_smartlist & nlist=f1b2gradlist->nlist;
00148   test_smartlist& list=f1b2gradlist->list;
00149    // nlist-=sizeof(int);
00150   // get record size
00151   int num_bytes=nlist.bptr->numbytes;
00152   // backup the size of the record
00153   list-=num_bytes;
00154   list.saveposition(); // save pointer to beginning of record;
00155   // save the pointer to the beginning of the record
00156 #if defined(SAFE_ARRAYS)
00157   checkidentiferstring("DL",list);
00158 #endif
00159   df1b2_header * px=(df1b2_header *) list.bptr;
00160   list.bptr+=sizeof(df1b2_header);
00161   df1b2_header * py=(df1b2_header *) list.bptr;
00162   list.bptr+=sizeof(df1b2_header);
00163   df1b2_header * pz=(df1b2_header *) list.bptr;
00164   list.bptr+=sizeof(df1b2_header);
00165   //double xu,xpu,yu,ypu,zu,zpu;
00166   double xu,yu,zu;
00167   memcpy(&xu,list.bptr,sizeof(double));
00168   list.bptr+=sizeof(double);
00169 
00170   memcpy(&yu,list.bptr,sizeof(double));
00171   list.bptr+=sizeof(double);
00172 
00173   memcpy(&zu,list.bptr,sizeof(double));
00174   list.bptr+=sizeof(double);
00175 
00176   int nvx;
00177   int nap;
00178   double *xudot,*yudot,*zudot;
00179   memcpy(&nvx,list.bptr,sizeof(int));
00180   list.bptr+=sizeof(int);
00181   memcpy(&nap,list.bptr,sizeof(int));
00182   list.bptr+=sizeof(int);
00183   xudot = (double*) list.bptr;
00184   list.bptr+=nvx*sizeof(double);
00185   yudot = (double*) list.bptr;
00186   list.bptr+=nvx*sizeof(double);
00187   zudot = (double*) list.bptr;
00188   list.bptr+=nvx*sizeof(double);
00189 #if defined(SAFE_ARRAYS)
00190   checkidentiferstring("EL",list);
00191 #endif
00192   xxx(xudot,yudot);
00193   xxx(zudot,yudot);
00194 
00195   list.restoreposition(); // save pointer to beginning of record;
00196 
00197   f1b2gradlist=localf1b2gradlist;
00198   df1b2variable::save_adpool_pointer();
00199 
00200 
00201   adpool * tmppool=df1b2variable::pool;
00202   if (tmppool)
00203   {
00204     //cout << tmppool << endl;
00205     // check if current pool is the right size
00206     if (tmppool->nvar != nap)
00207     {
00208       // check sizes of other pools
00209       int found_pool_flag=0;
00210       for (int i=0;i<df1b2variable::adpool_counter;i++)
00211       {
00212         if (df1b2variable::adpool_vector[i]->nvar == nap)
00213         {
00214           adpool * tmp = df1b2variable::pool;
00215           df1b2variable::pool=df1b2variable::adpool_vector[i];
00216           df1b2variable::nvar=nap;
00217           if (!tmp->on_adpool_vector())
00218           {
00219             df1b2variable::adpool_vector[df1b2variable::adpool_counter]=tmp;
00220             df1b2variable::nvar_vector[df1b2variable::adpool_counter]=
00221               tmp->nvar;
00222             //df1b2variable::adpool_counter++;
00223             df1b2variable::increment_adpool_counter();
00224             tmp->on_adpool_vector()=1;
00225           }
00226           found_pool_flag=1;
00227           break;
00228         }
00229       }
00230       if (!found_pool_flag)
00231       {
00232         cerr << "coudn't find right sized pool -- this can't happen"
00233              << endl;
00234         ad_exit(1);
00235       }
00236     }
00237   }
00238 
00239   df1b2_gradlist::set_yes_derivatives();
00240   //local_init_df1b2variable x(xu,xpudot), y(yu,ypudot);
00241 
00242   local_init_pass1_df1b2variable x(xu,xudot);
00243   local_init_pass1_df1b2variable y(yu,yudot);
00244 
00245   //local_init_pass1_var::allocate_all();
00246   local_init_pass1_var::set_dot_all();
00247 
00248   //local_dep_df1b2variable z(zudot,zpudot);
00249   local_dep_df1b2variable z;
00250 
00251 
00252   z=square(x+2.0*y+2.5);
00253 
00254   df1b2_gradlist::set_no_derivatives();
00255 
00256   set_dependent_variable(z);
00257   df1b2variable::passnumber=1;
00258   df1b2_gradcalc1();
00259   df1b2variable::restore_adpool_pointer();
00260   f1b2gradlist=globalf1b2gradlist;
00261 
00262   double * zprime=z.get_u_dot();   // size nvp
00263   double * pxubar=px->get_u_bar();
00264   double * pyubar=py->get_u_bar();
00265   double * pzubar=pz->get_u_bar();
00266   double * xudotbar=px->get_u_dot_bar();
00267   double * yudotbar=py->get_u_dot_bar();
00268   double * pzudotbar=pz->get_u_dot_bar();
00269 
00270   //double zdottmp[100];
00271   {
00272     for (int i=0;i<nvx;i++)
00273     {
00274       //zdottmp[i] = zprime[0] * xudot[i] + zprime[1] * yudot[i];
00275      // pzubar[0]+=xudot[i]*pzudotbar[i];
00276      // pzubar[1]+=xudot[i]*pzudotbar[i];
00277       xudotbar[i]+=zprime[0]*pzudotbar[i];
00278       yudotbar[i]+=zprime[1]*pzudotbar[i];
00279 
00280       pxubar[i]+=(x.u_bar[0]*xudot[i]+x.u_bar[1]*yudot[i])*pzudotbar[i];
00281       pyubar[i]+=(y.u_bar[0]*xudot[i]+y.u_bar[1]*yudot[i])*pzudotbar[i];
00282 
00283       pxubar[i]+=zprime[0]*pzubar[i];
00284       pyubar[i]+=zprime[1]*pzubar[i];
00285     }
00286   }
00287 }
00288 
00289 void read_xxx_2(void);
00290 void read_xxx_3(void){;}
00291 
00296 void ad_read_xxx(void)
00297 {
00298   switch(df1b2variable::passnumber)
00299   {
00300   case 1:
00301     read_xxx_1();
00302     break;
00303   case 2:
00304     read_xxx_2();
00305     break;
00306   case 3:
00307     read_xxx_3();
00308     break;
00309   default:
00310     cerr << "illegal value for df1b2variable::pass = "
00311          << df1b2variable::passnumber << endl;
00312     exit(1);
00313   }
00314 }
00315 
00320 void read_xxx_2(void)
00321 {
00322   // We are going backword for bptr and forward for bptr2
00323   // the current entry+2 in bptr is the size of the record i.e
00324   // points to the next record
00325   int nvar=df1b2variable::nvar;
00326   //fixed_smartlist & nlist=f1b2gradlist->nlist;
00327   test_smartlist& list=f1b2gradlist->list;
00328   int total_bytes=3*sizeof(df1b2_header)
00329     +2*(nvar+1)*sizeof(double);
00330   //int num_bytes=nlist.bptr->numbytes;
00331   f1b2gradlist->list.check_buffer_size(total_bytes);
00332   list.saveposition(); // save pointer to beginning of record;
00333   // save the pointer to the beginning of the record
00334 #if defined(SAFE_ARRAYS)
00335   checkidentiferstring("DL",list);
00336 #endif
00337   //df1b2_header * px=(df1b2_header *) list.bptr;
00338   list.bptr+=sizeof(df1b2_header);
00339   //df1b2_header * py=(df1b2_header *) list.bptr;
00340   list.bptr+=sizeof(df1b2_header);
00341   //df1b2_header * pz=(df1b2_header *) list.bptr;
00342   list.bptr+=sizeof(df1b2_header);
00343   //double xu,xpu,yu,ypu,zu,zpu;
00344   double xu,yu,zu;
00345   memcpy(&xu,list.bptr,sizeof(double));
00346   list.bptr+=sizeof(double);
00347 
00348   memcpy(&yu,list.bptr,sizeof(double));
00349   list.bptr+=sizeof(double);
00350 
00351   memcpy(&zu,list.bptr,sizeof(double));
00352   list.bptr+=sizeof(double);
00353 
00354   int nvx;
00355   int nap;
00356   double *xudot,*yudot,*zudot;
00357   memcpy(&nvx,list.bptr,sizeof(int));
00358   list.bptr+=sizeof(int);
00359   memcpy(&nap,list.bptr,sizeof(int));
00360   list.bptr+=sizeof(int);
00361   xudot = (double*) list.bptr;
00362   list.bptr+=nvx*sizeof(double);
00363   yudot = (double*) list.bptr;
00364   list.bptr+=nvx*sizeof(double);
00365   zudot = (double*) list.bptr;
00366   list.bptr+=nvx*sizeof(double);
00367 #if defined(SAFE_ARRAYS)
00368   checkidentiferstring("EL",list);
00369 #endif
00370   xxx(xudot,yudot);
00371   xxx(zudot,yudot);
00372 
00373   list.restoreposition(); // save pointer to beginning of record;
00374 
00375   f1b2gradlist=localf1b2gradlist;
00376   f1b2gradlist->reset();
00377   df1b2variable::save_adpool_pointer();
00378 
00379 
00380   adpool * tmppool=df1b2variable::pool;
00381   if (tmppool)
00382   {
00383     //cout << tmppool << endl;
00384     // check if current pool is the right size
00385     if (tmppool->nvar != nap)
00386     {
00387       // check sizes of other pools
00388       int found_pool_flag=0;
00389       for (int i=0;i<df1b2variable::adpool_counter;i++)
00390       {
00391         if (df1b2variable::adpool_vector[i]->nvar == nap)
00392         {
00393           adpool * tmp = df1b2variable::pool;
00394           df1b2variable::pool=df1b2variable::adpool_vector[i];
00395           df1b2variable::nvar=nap;
00396           if (!tmp->on_adpool_vector())
00397           {
00398             df1b2variable::adpool_vector[df1b2variable::adpool_counter]=tmp;
00399             df1b2variable::nvar_vector[df1b2variable::adpool_counter]=
00400               tmp->nvar;
00401             //df1b2variable::adpool_counter++;
00402             df1b2variable::increment_adpool_counter();
00403             tmp->on_adpool_vector()=1;
00404           }
00405           found_pool_flag=1;
00406           break;
00407         }
00408       }
00409       if (!found_pool_flag)
00410       {
00411         cerr << "coudn't find right sized pool -- this can't happen"
00412              << endl;
00413         ad_exit(1);
00414       }
00415     }
00416   }
00417 
00418   df1b2_gradlist::set_yes_derivatives();
00419   //local_init_df1b2variable x(xu,xpudot), y(yu,ypudot);
00420 
00421   local_init_pass1_df1b2variable x(xu,xudot);
00422   local_init_pass1_df1b2variable y(yu,yudot);
00423 
00424   //local_init_pass1_var::allocate_all();
00425   local_init_pass1_var::set_dot_all();
00426 
00427   //local_dep_df1b2variable z(zudot,zpudot);
00428   local_dep_df1b2variable z;
00429 
00430 
00431   z=square(x+2.0*y+2.5);
00432 
00433   df1b2_gradlist::set_no_derivatives();
00434 
00435   set_dependent_variable(z);
00436   df1b2variable::passnumber=1;
00437   df1b2_gradcalc1();
00438 
00439   x.get_u_bar_tilde()[0]=1;
00440   x.get_u_bar_tilde()[1]=0;
00441   y.get_u_bar_tilde()[0]=0;
00442   y.get_u_bar_tilde()[1]=0;
00443 
00444   df1b2variable::passnumber=2;
00445   df1b2_gradcalc1();
00446 
00447   df1b2variable::passnumber=3;
00448   df1b2_gradcalc1();
00449 
00450   x.get_u_bar_tilde()[0]=0;
00451   x.get_u_bar_tilde()[1]=1;
00452   y.get_u_bar_tilde()[0]=0;
00453   y.get_u_bar_tilde()[1]=0;
00454 
00455   df1b2variable::passnumber=2;
00456   df1b2_gradcalc1();
00457 
00458   df1b2variable::passnumber=3;
00459   df1b2_gradcalc1();
00460 }