ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2f27.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include <df1b2fun.h>
12 
13 #ifndef OPT_LIB
14  #include <cassert>
15 #endif
16 
17 void ncount_checker(int ncount,int ncount_check);
18 
19 void ad_read_pass1x(void);
20 void ad_read_pass2x(void);
21 void read_pass1_3x(void);
22 // should inline this
23 void read_pass1_1x(void);
24 void read_pass1_2x(void);
25 
31  df1b2variable * pz, df1b2function1 * pf)
32 {
34  ncount++;
35 #if defined(CHECK_COUNT)
36  if (ncount >= ncount_check)
37  ncount_checker(ncount,ncount_check);
38 #endif
39  unsigned int nvar=df1b2variable::nvar;
40 
41  size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header)+sizeof(char*)
42  +sizeof(double)+nvar*sizeof(double);
43 // string identifier debug stuff
44 #if defined(SAFE_ALL)
45  char ids[]="CX";
46  int slen=strlen(ids);
47  total_bytes+=slen;
48 #endif
49  list.check_buffer_size(total_bytes);
50  void * tmpptr=list.bptr;
51 #if defined(SAFE_ALL)
52  memcpy(list,ids,slen);
53 #endif
54 // end of string identifier debug stuff
55 
56  memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
57  memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
58  //list.bptr+=sizeof(df1b2_header);
59  memcpy(list,&pf,sizeof(char*));
60  //*(char**)(list.bptr)=(char*)pf;
61  //list.bptr+=sizeof(char*);
62  memcpy(list,px->get_u(),sizeof(double));
63  //list.bptr+=sizeof(double);
64  memcpy(list,px->get_u_dot(),nvar*sizeof(double));
65  //list.bptr+=nvar*sizeof(double);
66  // ***** write record size
69  ++nlist;
70  return 0;
71 }
72 
77 void ad_read_pass1x(void)
78 {
80  {
81  case 1:
82  read_pass1_1x();
83  break;
84  case 2:
85  read_pass1_2x();
86  break;
87  case 3:
88  read_pass1_3x();
89  break;
90  default:
91  cerr << "illegal value for df1b2variable::pass = "
93  ad_exit(1);
94  }
95 }
96 
101 void read_pass1_1x(void)
102 {
103  // We are going backword for bptr and forward for bptr2
104  // the current entry+2 in bptr is the size of the record i.e
105  // points to the next record
106  unsigned int nvar=df1b2variable::nvar;
109  // nlist-=sizeof(int);
110  // get record size
111  int num_bytes=nlist.bptr->numbytes;
112  // backup the size of the record
113  list-=num_bytes;
114  //list.bptr-=num_bytes;
115  list.saveposition(); // save pointer to beginning of record;
116  // save the pointer to the beginning of the record
117  double xu;
118  double * xdot;
119  //df1b2_header x,z;
120  df1b2function1 * pf;
121 
122  // get info from tape1
123  // get info from tape1
124 #if defined(SAFE_ARRAYS)
125  checkidentiferstring("CX",list);
126 #endif
127  df1b2_header * px=(df1b2_header *) list.bptr;
128  list.bptr+=sizeof(df1b2_header);
129  df1b2_header * pz=(df1b2_header *) list.bptr;
130  list.bptr+=sizeof(df1b2_header);
131  pf=*(df1b2function1 **) list.bptr;
132  list.bptr+=sizeof(char*);
133  memcpy(&xu,list.bptr,sizeof(double));
134  list.bptr+=sizeof(double);
135  xdot=(double*)list.bptr;
136  list.restoreposition(); // save pointer to beginning of record;
137 
138  // Do first reverse paSS calculations
139  // ****************************************************************
140  // turn this off if no third derivatives are calculated
141  // if (!no_third_derivatives)
142  // {
143  // save for second reverse pass
144  // save identifier 1
147 
148 
149  size_t total_bytes=2*nvar*sizeof(double);
150 // string identifier debug stuff
151 #if defined(SAFE_ALL)
152  char ids[]="DU";
153  int slen=strlen(ids);
154  total_bytes+=slen;
155 #endif
156 
157  list2.check_buffer_size(total_bytes);
158  void * tmpptr2=list2.bptr;
159 
160 #if defined(SAFE_ALL)
161  memcpy(list2,ids,slen);
162 #endif
163 
164  memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
165  memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
166  *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
167  ++nlist2;
168  // }
169  //
170  // ****************************************************************
171 #if defined(PRINT_DERS)
172  print_derivatives(pf->funname,(pf->df)(xu),(pf->df)(xu),(pf->d2f)(xu),
173  (pf->d3f)(xu),1);
174  print_derivatives(pz,"z");
175  print_derivatives(px,"x");
176 #endif
177 
178  double df=(pf->df)(xu);
179  double d2f=(pf->d2f)(xu);
180  //double d3f=(pf->d3f)(xu);
181 
182  for (size_t i=0;i<nvar;i++)
183  {
184  //px->u_bar[i]+=(pf->df)(xu)* pz->u_bar[i];
185  px->u_bar[i]+=df * pz->u_bar[i];
186  }
187  for (size_t i=0;i<nvar;i++)
188  {
189  //px->u_bar[i]+=(pf->d2f)(xu)*xdot[i]*pz->u_dot_bar[i];
190  px->u_bar[i]+=d2f*xdot[i]*pz->u_dot_bar[i];
191  }
192  for (size_t i=0;i<nvar;i++)
193  {
194  //px->u_dot_bar[i]+=(pf->df)(xu)*pz->u_dot_bar[i];
195  px->u_dot_bar[i]+=df*pz->u_dot_bar[i];
196  }
197 
198  // !!!!!!!!!!!!!!!!!!!!!!
199  for (size_t i=0;i<nvar;i++)
200  {
201  pz->u_bar[i]=0;
202  }
203  for (size_t i=0;i<nvar;i++)
204  {
205  pz->u_dot_bar[i]=0;
206  }
207 
208 #if defined(PRINT_DERS)
209  print_derivatives(pz,"z");
210  print_derivatives(px,"x");
211 #endif
212 }
213 
218 void read_pass1_2x(void)
219 {
220  //const int nlist_record_size=sizeof(int)+sizeof(char*);
221  // We are going forward for bptr and backword for bptr2
222  //
223  // list 1
224  //
225  unsigned int nvar=df1b2variable::nvar;
227 
228  size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header)+sizeof(char*)
229  +sizeof(double)+nvar*sizeof(double);
230 // string identifier debug stuff
231 #if defined(SAFE_ALL)
232  char ids[]="CX";
233  int slen=strlen(ids);
234  total_bytes+=slen;
235 #endif
236  list.check_buffer_size(total_bytes);
237 // end of string identifier debug stuff
238 
239  list.saveposition(); // save pointer to beginning of record;
241  // nlist-=sizeof(int);
242  // get record size
243  int num_bytes=nlist.bptr->numbytes;
244  // nlist+=nlist_record_size;
245  //
246  // list 2
247  //
250  // get record size
251  int num_bytes2=*nlist2.bptr;
252  --nlist2;
253  // backup the size of the record
254  list2-=num_bytes2;
255  list2.saveposition();
256  // save the pointer to the beginning of the record
257  // bptr and bptr2 now both point to the beginning of their records
258 
259  double xu;
260  double * xdot;
261  //df1b2_header x,z;
262  df1b2function1 * pf;
263 
264  // get info from tape1
265  // get info from tape1
266 #if defined(SAFE_ARRAYS)
267  checkidentiferstring("CX",list);
268  checkidentiferstring("DU",list2);
269 #endif
270  df1b2_header * px=(df1b2_header *) list.bptr;
271  list.bptr+=sizeof(df1b2_header);
272  df1b2_header * pz=(df1b2_header *) list.bptr;
273  list.bptr+=sizeof(df1b2_header);
274  pf=*(df1b2function1 **) list.bptr;
275  list.bptr+=sizeof(char*);
276  memcpy(&xu,list.bptr,sizeof(double));
277  list.bptr+=sizeof(double);
278  xdot=(double*)list.bptr;
279  list.restoreposition(num_bytes); // save pointer to beginning of record;
280 
281  double * zbar;
282  double * zdotbar;
283 
284  zbar=(double*)list2.bptr;
285  zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
286 
287  double * x_tilde=px->get_u_tilde();
288  double * x_dot_tilde=px->get_u_dot_tilde();
289  double * x_bar_tilde=px->get_u_bar_tilde();
290  double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
291  double * z_bar_tilde=pz->get_u_bar_tilde();
292  double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
293 #if defined(PRINT_DERS)
294  print_derivatives(pf->funname,(pf->df)(xu),(pf->df)(xu),(pf->d2f)(xu),
295  (pf->d3f)(xu),1);
296  print_derivatives(pz,"z");
297  print_derivatives(px,"x");
298 #endif
299  // Do second "reverse-reverse" pass calculations
300 
301  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
302  for (size_t i=0;i<nvar;i++)
303  {
304  z_bar_tilde[i]=0;
305  z_dot_bar_tilde[i]=0;
306  }
307 
308  double df=(pf->df)(xu);
309  double d2f=(pf->d2f)(xu);
310  double d3f=(pf->d3f)(xu);
311  for (size_t i=0;i<nvar;i++)
312  {
313  //*x_tilde+=(pf->d2f)(xu)*zbar[i]*x_bar_tilde[i];
314  *x_tilde+=d2f*zbar[i]*x_bar_tilde[i];
315  //z_bar_tilde[i]+=(pf->df)(xu)*x_bar_tilde[i];
316  z_bar_tilde[i]+=df*x_bar_tilde[i];
317  }
318 
319  for (size_t i=0;i<nvar;i++)
320  {
321  //*x_tilde+=(pf->d2f)(xu)*zdotbar[i]*x_dot_bar_tilde[i];
322  *x_tilde+=d2f*zdotbar[i]*x_dot_bar_tilde[i];
323  //z_dot_bar_tilde[i]+=(pf->df)(xu)*x_dot_bar_tilde[i];
324  z_dot_bar_tilde[i]+=df*x_dot_bar_tilde[i];
325  }
326 
327  for (size_t i=0;i<nvar;i++)
328  {
329  //x_dot_tilde[i]+=(pf->d2f)(xu)*zdotbar[i]*x_bar_tilde[i];
330  //z_dot_bar_tilde[i]+=(pf->d2f)(xu)*xdot[i]*x_bar_tilde[i];
331  //*x_tilde+=(pf->d3f)(xu)*xdot[i]*zdotbar[i]*x_bar_tilde[i];
332  x_dot_tilde[i]+=d2f*zdotbar[i]*x_bar_tilde[i];
333  z_dot_bar_tilde[i]+=d2f*xdot[i]*x_bar_tilde[i];
334  *x_tilde+=d3f*xdot[i]*zdotbar[i]*x_bar_tilde[i];
335  }
336  list2.restoreposition();
337 #if defined(PRINT_DERS)
338  print_derivatives(pz,"z");
339  print_derivatives(px,"x");
340 #endif
341 }
342 
347 void read_pass1_3x(void)
348 {
349  // We are going backword for bptr and forward for bptr2
350  // the current entry+2 in bptr is the size of the record i.e
351  // points to the next record
352  unsigned int nvar=df1b2variable::nvar;
355  // nlist-=sizeof(int);
356  // get record size
357  int num_bytes=nlist.bptr->numbytes;
358  // backup the size of the record
359  list-=num_bytes;
360  list.saveposition(); // save pointer to beginning of record;
361  // save the pointer to the beginning of the record
362  double xu;
363  double * xdot;
364  //df1b2_header x,z;
365  df1b2function1 * pf;
366 
367  // get info from tape1
368 #if defined(SAFE_ARRAYS)
369  checkidentiferstring("CX",list);
370 #endif
371  df1b2_header * px=(df1b2_header *) list.bptr;
372  list.bptr+=sizeof(df1b2_header);
373  df1b2_header * pz=(df1b2_header *) list.bptr;
374  list.bptr+=sizeof(df1b2_header);
375  pf=*(df1b2function1 **) list.bptr;
376  list.bptr+=sizeof(char*);
377  memcpy(&xu,list.bptr,sizeof(double));
378  list.bptr+=sizeof(double);
379  xdot=(double*)list.bptr;
380  list.restoreposition(); // save pointer to beginning of record;
381 
382 #if defined(PRINT_DERS)
383  print_derivatives(pf->funname,(pf->df)(xu),(pf->df)(xu),(pf->d2f)(xu),
384  (pf->d3f)(xu),1);
385  print_derivatives(pz,"z");
386  print_derivatives(px,"x");
387 #endif
388 
389  double df=(pf->df)(xu);
390  double d2f=(pf->d2f)(xu);
391  //*(px->u_tilde)+=(pf->df)(xu)* *(pz->u_tilde);
392  *(px->u_tilde)+=df * *(pz->u_tilde);
393  for (size_t i=0;i<nvar;i++)
394  {
395  //*(px->u_tilde)+=(pf->d2f)(xu)*xdot[i]*pz->u_dot_tilde[i];
396  *(px->u_tilde)+=d2f*xdot[i]*pz->u_dot_tilde[i];
397  }
398  for (size_t i=0;i<nvar;i++)
399  {
400  //px->u_dot_tilde[i]+=(pf->df)(xu)*pz->u_dot_tilde[i];
401  px->u_dot_tilde[i]+=df*pz->u_dot_tilde[i];
402  }
403  *(pz->u_tilde)=0;
404  for (size_t i=0;i<nvar;i++)
405  {
406  pz->u_dot_tilde[i]=0;
407  }
408 #if defined(PRINT_DERS)
409  print_derivatives(pz,"z");
410  print_derivatives(px,"x");
411 #endif
412 }
char * bptr
Definition: df1b2fun.h:543
double * get_u_bar_tilde() const
Definition: df1b2fun.h:234
double(* df)(double)
Definition: df1b2fun.h:411
void check_buffer_size(const size_t)
Description not yet available.
Definition: df1b2f10.cpp:185
df1b2_gradlist * f1b2gradlist
Definition: df1b2glo.cpp:49
void memcpy(test_smartlist &dest, void *source, const size_t nsize)
memcpy for test_smartlist
Definition: df1b2f10.cpp:367
void ad_read_pass1x(void)
Description not yet available.
Definition: df1b2f27.cpp:77
double * u_bar
Definition: df1b2fun.h:197
void read_pass1_1x(void)
Description not yet available.
Definition: df1b2f27.cpp:101
void read_pass1_2x(void)
Description not yet available.
Definition: df1b2f27.cpp:218
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
static int passnumber
Definition: df1b2fun.h:289
double * get_u_dot_tilde() const
Definition: df1b2fun.h:233
int adptr_diff(void *x, void *y)
Definition: df1b2fn2.cpp:110
exitptr ad_exit
Definition: gradstrc.cpp:53
void ncount_checker(int ncount, int ncount_check)
double * u_tilde
Definition: df1b2fun.h:199
void restoreposition(void)
Definition: df1b2fun.h:560
Description not yet available.
Definition: df1b2fun.h:266
Description not yet available.
Definition: df1b2fun.h:669
This is a function object for a function of one variable which stores the functions which calculate t...
Definition: df1b2fun.h:407
fixed_smartlist nlist
Definition: df1b2fun.h:754
Description not yet available.
Definition: df1b2fun.h:602
prnstream & endl(prnstream &)
double * u_dot_bar
Definition: df1b2fun.h:198
test_smartlist list
Definition: df1b2fun.h:753
void print_derivatives(const adstring &s, double f, double df, double d2f, double d3f, int bflag)
Description not yet available.
Definition: df1b2fn3.cpp:27
ADrfptr pf
Definition: df1b2fun.h:595
void read_pass1_3x(void)
Description not yet available.
Definition: df1b2f27.cpp:347
fixed_smartlist2 nlist2
Definition: df1b2fun.h:756
Description not yet available.
Definition: df1b2fun.h:527
double * get_u_dot_bar() const
Definition: df1b2fun.h:231
adstring funname
Definition: df1b2fun.h:414
double * get_u_tilde() const
Definition: df1b2fun.h:232
double * get_u_dot_bar_tilde() const
Definition: df1b2fun.h:235
double(* d2f)(double)
Definition: df1b2fun.h:412
static unsigned int nvar
Definition: df1b2fun.h:290
void(* ADrfptr)(void)
Definition: df1b2fun.cpp:139
int write_pass1x(const df1b2variable *_px, df1b2variable *pz, df1b2function1 *pf)
Description not yet available.
Definition: df1b2f27.cpp:30
double * u_dot_tilde
Definition: df1b2fun.h:200
Base class for df1b2variable.
Definition: df1b2fun.h:191
Description not yet available.
fixed_list_entry * bptr
Definition: df1b2fun.h:626
void checkidentiferstring(const char *ids, test_smartlist &list)
Description not yet available.
Definition: df1b2fn5.cpp:256
void ad_read_pass2x(void)
double * get_u_bar() const
Definition: df1b2fun.h:230
double(* d3f)(double)
Definition: df1b2fun.h:413
int numbytes
Definition: df1b2fun.h:594
void saveposition(void)
Definition: df1b2fun.h:552
test_smartlist list2
Definition: df1b2fun.h:755