ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2f11.cpp
Go to the documentation of this file.
1 
5 #include <df1b2fun.h>
6 #ifndef OPT_LIB
7  #include <cassert>
8  #include <climits>
9 #endif
10 
11 #if defined(CHECK_COUNT)
12 void ncount_checker(int ncount,int ncount_check)
13 {
14  //if (ncount ==6599)
15  // cout << ncount << endl;
16 }
17 #endif
18 
19 void ad_read_pass1(void);
20 void ad_read_pass2(void);
21 void read_pass1_3(void);
22 // should inline this
23 
29  df1b2variable * pz, df1b2function1 * pf)
30 {
32  ncount++;
33 #if defined(CHECK_COUNT)
34  if (ncount >= ncount_check)
35  ncount_checker(ncount,ncount_check);
36 #endif
37 
38  unsigned int nvar=df1b2variable::nvar;
39 
40  size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header)+sizeof(char*)
41  +sizeof(double)+nvar*sizeof(double);
42 // string identifier debug stuff
43 #if defined(SAFE_ALL)
44  char ids[]="CX";
45  int slen=strlen(ids);
46  total_bytes+=slen;
47 #endif
48 
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*(int)sizeof(double));
65  //list.bptr+=nvar*sizeof(double);
66  // ***** write record size
69  ++nlist;
70  return 0;
71 }
72 
77 void ad_read_pass1(void)
78 {
80  {
81  case 1:
82  read_pass1_1();
83  break;
84  case 2:
85  read_pass1_2();
86  break;
87  case 3:
88  read_pass1_3();
89  break;
90  default:
91  cerr << "illegal value for df1b2variable::pass = "
93  ad_exit(1);
94  }
95 }
96 
101 void read_pass1_1(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  //df1b2_header x,z;
119  df1b2function1 * pf;
120 
121  // get info from tape1
122  // get info from tape1
123 #if defined(SAFE_ALL)
124  checkidentiferstring("CX",list);
125 #endif
126  constexpr size_t sizeofdouble = sizeof(double);
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,sizeofdouble);
134  list.bptr+=sizeofdouble;
135  double* 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*sizeofdouble;
150 // string identifier debug stuff
151 #if defined(SAFE_ALL)
152  char ids[]="DU";
153  size_t 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*sizeofdouble);
165  memcpy(list2,pz->get_u_dot_bar(),nvar*sizeofdouble);
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  double* px_u_bari = px->u_bar;
183  double* pz_u_bari = pz->u_bar;
184  for (size_t i=0;i<nvar;i++)
185  {
186  //px->u_bar[i]+=(pf->df)(xu)* pz->u_bar[i];
187  *px_u_bari += df * *pz_u_bari;
188 
189  ++px_u_bari;
190  ++pz_u_bari;
191  }
192  px_u_bari = px->u_bar;
193  double* xdoti = xdot;
194  double* pz_u_dot_bari = pz->u_dot_bar;
195  for (size_t i=0;i<nvar;i++)
196  {
197  //px->u_bar[i]+=(pf->d2f)(xu)*xdot[i]*pz->u_dot_bar[i];
198  *px_u_bari += d2f * *xdoti * *pz_u_dot_bari;
199 
200  ++px_u_bari;
201  ++xdoti;
202  ++pz_u_dot_bari;
203  }
204  double* px_u_dot_bari = px->u_dot_bar;
205  pz_u_dot_bari = pz->u_dot_bar;
206  for (size_t i=0;i<nvar;i++)
207  {
208  //px->u_dot_bar[i]+=(pf->df)(xu)*pz->u_dot_bar[i];
209  *px_u_dot_bari += df * *pz_u_dot_bari;
210 
211  ++px_u_dot_bari;
212  ++pz_u_dot_bari;
213  }
214 
215  // !!!!!!!!!!!!!!!!!!!!!!
216  memset(pz->u_bar, 0, nvar * sizeofdouble);
217  memset(pz->u_dot_bar, 0, nvar * sizeofdouble);
218 
219 #if defined(PRINT_DERS)
220  print_derivatives(pz,"z");
221  print_derivatives(px,"x");
222 #endif
223 }
224 
229 void read_pass1_2(void)
230 {
231  //const int nlist_record_size=sizeof(int)+sizeof(char*);
232  // We are going forward for bptr and backword for bptr2
233  //
234  // list 1
235  //
236  unsigned int nvar=df1b2variable::nvar;
238 
239  size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header)+sizeof(char*)
240  +sizeof(double)+nvar*sizeof(double);
241 // string identifier debug stuff
242 #if defined(SAFE_ALL)
243  char ids[]="CX";
244  int slen=strlen(ids);
245  total_bytes+=slen;
246 #endif
247 
248  list.check_buffer_size(total_bytes);
249 // end of string identifier debug stuff
250 
251  list.saveposition(); // save pointer to beginning of record;
253  // nlist-=sizeof(int);
254  // get record size
255  int num_bytes=nlist.bptr->numbytes;
256  // nlist+=nlist_record_size;
257  //
258  // list 2
259  //
262  // get record size
263  int num_bytes2=*nlist2.bptr;
264  --nlist2;
265  // backup the size of the record
266  list2-=num_bytes2;
267  list2.saveposition();
268  // save the pointer to the beginning of the record
269  // bptr and bptr2 now both point to the beginning of their records
270 
271  double xu;
272  //df1b2_header x,z;
273  df1b2function1 * pf;
274 
275  // get info from tape1
276  // get info from tape1
277 #if defined(SAFE_ALL)
278  checkidentiferstring("CX",list);
279  checkidentiferstring("DU",list2);
280 #endif
281  df1b2_header * px=(df1b2_header *) list.bptr;
282  list.bptr+=sizeof(df1b2_header);
283  df1b2_header * pz=(df1b2_header *) list.bptr;
284  list.bptr+=sizeof(df1b2_header);
285  pf=*(df1b2function1 **) list.bptr;
286  list.bptr+=sizeof(char*);
287  memcpy(&xu,list.bptr,sizeof(double));
288  list.bptr+=sizeof(double);
289  double* xdot=(double*)list.bptr;
290  list.restoreposition(num_bytes); // save pointer to beginning of record;
291 
292  double* zbar=(double*)list2.bptr;
293  double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
294 
295  double * x_tilde=px->get_u_tilde();
296  double * x_dot_tilde=px->get_u_dot_tilde();
297  double * x_bar_tilde=px->get_u_bar_tilde();
298  double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
299  double * z_bar_tilde=pz->get_u_bar_tilde();
300  double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
301 #if defined(PRINT_DERS)
302  print_derivatives(pf->funname,(pf->df)(xu),(pf->df)(xu),(pf->d2f)(xu),
303  (pf->d3f)(xu),1);
304  print_derivatives(pz,"z");
305  print_derivatives(px,"x");
306 #endif
307  // Do second "reverse-reverse" pass calculations
308 
309  constexpr size_t sizeofdouble = sizeof(double);
310  memset(z_bar_tilde, 0, nvar * sizeofdouble);
311  memset(z_dot_bar_tilde, 0, nvar * sizeofdouble);
312 
313  double df=(pf->df)(xu);
314  double d2f=(pf->d2f)(xu);
315  double d3f=(pf->d3f)(xu);
316 
317  double* px_bar_tildei = x_bar_tilde;
318  double* pzbari = zbar;
319  double* pz_bar_tildei = z_bar_tilde;
320  for (size_t i=0;i<nvar;i++)
321  {
322  //*x_tilde+=(pf->d2f)(xu)*zbar[i]*x_bar_tilde[i];
323  *x_tilde += d2f * *pzbari * *px_bar_tildei;
324  //z_bar_tilde[i]+=(pf->df)(xu)*x_bar_tilde[i];
325  *pz_bar_tildei += df * *px_bar_tildei;
326 
327  ++px_bar_tildei;
328  ++pzbari;
329  ++pz_bar_tildei;
330  }
331 
332  double* pzdotbari = zdotbar;
333  double* pz_dot_bar_tildei = z_dot_bar_tilde;
334  double* px_dot_bar_tildei = x_dot_bar_tilde;
335  for (size_t i=0;i<nvar;i++)
336  {
337  //*x_tilde+=(pf->d2f)(xu)*zdotbar[i]*x_dot_bar_tilde[i];
338  *x_tilde+=d2f * *pzdotbari * *px_dot_bar_tildei;
339  //z_dot_bar_tilde[i]+=(pf->df)(xu)*x_dot_bar_tilde[i];
340  *pz_dot_bar_tildei += df * *px_dot_bar_tildei;
341 
342  ++pzdotbari;
343  ++pz_dot_bar_tildei;
344  ++px_dot_bar_tildei;
345  }
346 
347  px_bar_tildei = x_bar_tilde;
348  pzdotbari = zdotbar;
349  double* pxdoti = xdot;
350  double* px_dot_tildei = x_dot_tilde;
351  pz_dot_bar_tildei = z_dot_bar_tilde;
352  for (size_t i=0;i<nvar;i++)
353  {
354  //x_dot_tilde[i]+=(pf->d2f)(xu)*zdotbar[i]*x_bar_tilde[i];
355  //z_dot_bar_tilde[i]+=(pf->d2f)(xu)*xdot[i]*x_bar_tilde[i];
356  //*x_tilde+=(pf->d3f)(xu)*xdot[i]*zdotbar[i]*x_bar_tilde[i];
357  *px_dot_tildei += d2f * *pzdotbari * *px_bar_tildei;
358  *pz_dot_bar_tildei += d2f * *pxdoti * *px_bar_tildei;
359  *x_tilde += d3f * *pxdoti * *pzdotbari * *px_bar_tildei;
360 
361  ++px_bar_tildei;
362  ++pzdotbari;
363  ++pxdoti;
364  ++px_dot_tildei;
365  ++pz_dot_bar_tildei;
366  }
367  list2.restoreposition();
368 #if defined(PRINT_DERS)
369  print_derivatives(pz,"z");
370  print_derivatives(px,"x");
371 #endif
372 }
373 
378 void read_pass1_3(void)
379 {
380  // We are going backword for bptr and forward for bptr2
381  // the current entry+2 in bptr is the size of the record i.e
382  // points to the next record
383  unsigned int nvar=df1b2variable::nvar;
386  // nlist-=sizeof(int);
387  // get record size
388  int num_bytes=nlist.bptr->numbytes;
389  // backup the size of the record
390  list-=num_bytes;
391  list.saveposition(); // save pointer to beginning of record;
392  // save the pointer to the beginning of the record
393  double xu;
394  //df1b2_header x,z;
395  df1b2function1 * pf;
396 
397  // get info from tape1
398 #if defined(SAFE_ALL)
399  checkidentiferstring("CX",list);
400 #endif
401  df1b2_header * px=(df1b2_header *) list.bptr;
402  list.bptr+=sizeof(df1b2_header);
403  df1b2_header * pz=(df1b2_header *) list.bptr;
404  list.bptr+=sizeof(df1b2_header);
405  pf=*(df1b2function1 **) list.bptr;
406  list.bptr+=sizeof(char*);
407  memcpy(&xu,list.bptr,sizeof(double));
408  list.bptr+=sizeof(double);
409  double* xdot=(double*)list.bptr;
410  list.restoreposition(); // save pointer to beginning of record;
411 
412 #if defined(PRINT_DERS)
413  print_derivatives(pf->funname,(pf->df)(xu),(pf->df)(xu),(pf->d2f)(xu),
414  (pf->d3f)(xu),1);
415  print_derivatives(pz,"z");
416  print_derivatives(px,"x");
417 #endif
418 
419  double df=(pf->df)(xu);
420  double d2f=(pf->d2f)(xu);
421  //*(px->u_tilde)+=(pf->df)(xu)* *(pz->u_tilde);
422  *(px->u_tilde)+=df * *(pz->u_tilde);
423 
424  double* pz_u_dot_tildei = pz->u_dot_tilde;
425  double* pxdoti = xdot;
426  for (size_t i=0;i<nvar;i++)
427  {
428  //*(px->u_tilde)+=(pf->d2f)(xu)*xdot[i]*pz->u_dot_tilde[i];
429  *(px->u_tilde) += d2f * *pxdoti * *pz_u_dot_tildei;
430 
431  ++pxdoti;
432  ++pz_u_dot_tildei;
433  }
434 
435  double* px_u_dot_tildei = px->u_dot_tilde;
436  pz_u_dot_tildei = pz->u_dot_tilde;
437  for (size_t i=0;i<nvar;i++)
438  {
439  //px->u_dot_tilde[i]+=(pf->df)(xu)*pz->u_dot_tilde[i];
440  *px_u_dot_tildei += df * *pz_u_dot_tildei;
441 
442  ++px_u_dot_tildei;
443  ++pz_u_dot_tildei;
444  }
445  *(pz->u_tilde)=0;
446  constexpr size_t sizeofdouble = sizeof(double);
447  memset(pz->u_dot_tilde, 0, nvar * sizeofdouble);
448 #if defined(PRINT_DERS)
449  print_derivatives(pz,"z");
450  print_derivatives(px,"x");
451 #endif
452 }
char * bptr
Definition: df1b2fun.h:543
double * get_u_bar_tilde() const
Definition: df1b2fun.h:234
void ad_read_pass2(void)
Description not yet available.
Definition: df1b2f12.cpp:70
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 read_pass1_1(void)
Description not yet available.
Definition: df1b2f11.cpp:101
void memcpy(test_smartlist &dest, void *source, const size_t nsize)
memcpy for test_smartlist
Definition: df1b2f10.cpp:367
double * u_bar
Definition: df1b2fun.h:197
#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 ad_read_pass1(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: df1b2f11.cpp:77
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
void read_pass1_3(void)
Description not yet available.
Definition: df1b2f11.cpp:378
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
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
int write_pass1(const df1b2variable *px, const df1b2variable *py, df1b2variable *pz, df1b2function2 *pf)
Description not yet available.
Definition: df1b2f12.cpp:17
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
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 read_pass1_2(void)
Description not yet available.
Definition: df1b2f11.cpp:229
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