ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2f21.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  */
12 #include <df1b2fun.h>
13 
14 #ifndef OPT_LIB
15  #include <cassert>
16  #include <climits>
17 #endif
18 
19 void ad_read_pass1_der_values(void);
20 void read_pass1_1_dv(void);
21 void read_pass1_2_dv(void);
22 void read_pass1_3_dv(void);
23 
29  df1b2variable * pz, double df, double d2f, double d3f)
30 {
32  ncount++;
33  /*
34  if (28126<ncount && 28129> ncount)
35  cout << "trap" << endl;
36  */
37 #if defined(CHECK_COUNT)
38  if (ncount >= ncount_check)
39  ncount_checker(ncount,ncount_check);
40 #endif
41  unsigned int nvar=df1b2variable::nvar;
42 
43  size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header)
44  // +sizeof(char*)
45  +4*sizeof(double)+nvar*sizeof(double);
46 // string identifier debug stuff
47 #if defined(SAFE_ALL)
48  char ids[]="HU";
49  int slen=strlen(ids);
50  total_bytes+=slen;
51 #endif
52 
53  list.check_buffer_size(total_bytes);
54  void * tmpptr=list.bptr;
55 #if defined(SAFE_ALL)
56  memcpy(list,ids,slen);
57 #endif
58 // end of string identifier debug stuff
59 
60  memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
61  memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
62 
63  // replace this with the derivative values
64  //memcpy(list,&pf,sizeof(char*));
65 
66  memcpy(list,&df,sizeof(double));
67  memcpy(list,&d2f,sizeof(double));
68  memcpy(list,&d3f,sizeof(double));
69 
70 
71 
72  memcpy(list,px->get_u(),sizeof(double));
73  //list.bptr+=sizeof(double);
74  memcpy(list,px->get_u_dot(),nvar*(int)sizeof(double));
75  //list.bptr+=nvar*sizeof(double);
76  // ***** write record size
79  ++nlist;
80  return 0;
81 }
82 
88 {
90  {
91  case 1:
93  break;
94  case 2:
96  break;
97  case 3:
99  break;
100  default:
101  cerr << "illegal value for df1b2variable::pass = "
103  ad_exit(1);
104  }
105 }
106 
111 void read_pass1_1_dv(void)
112 {
113  // We are going backword for bptr and forward for bptr2
114  // the current entry+2 in bptr is the size of the record i.e
115  // points to the next record
116  unsigned int nvar=df1b2variable::nvar;
119  // nlist-=sizeof(int);
120  // get record size
121  int num_bytes=nlist.bptr->numbytes;
122  // backup the size of the record
123  list-=num_bytes;
124  //list.bptr-=num_bytes;
125  list.saveposition(); // save pointer to beginning of record;
126  // save the pointer to the beginning of the record
127  //df1b2_header x,z;
128  //df1b2function1 * pf;
129 
130  // get info from tape1
131  // get info from tape1
132 #if defined(SAFE_ALL)
133  checkidentiferstring("HU",list);
134 #endif
135  constexpr size_t sizeofdouble = sizeof(double);
136  df1b2_header * px=(df1b2_header *) list.bptr;
137  list.bptr+=sizeof(df1b2_header);
138  df1b2_header * pz=(df1b2_header *) list.bptr;
139  list.bptr+=sizeof(df1b2_header);
140  double df=*(double*) list.bptr;
141  list.bptr+=sizeofdouble;
142  double d2f=*(double*) list.bptr;
143  list.bptr+=sizeofdouble;
144  //double d3f=*(double*) list.bptr;
145  list.bptr+=sizeofdouble;
146  double xu;
147  memcpy(&xu,list.bptr,sizeofdouble);
148  list.bptr+=sizeofdouble;
149  double* xdot=(double*)list.bptr;
150  list.restoreposition(); // save pointer to beginning of record;
151 
152  // Do first reverse paSS calculations
153  // ****************************************************************
154  // turn this off if no third derivatives are calculated
155  // if (!no_third_derivatives)
156  // {
157  // save for second reverse pass
158  // save identifier 1
161 
162  size_t total_bytes=2*nvar*sizeofdouble;
163 // string identifier debug stuff
164 #if defined(SAFE_ALL)
165  char ids[]="JE";
166  int slen=strlen(ids);
167  total_bytes+=slen;
168 #endif
169 
170  list2.check_buffer_size(total_bytes);
171  void * tmpptr2=list2.bptr;
172 
173 #if defined(SAFE_ALL)
174  memcpy(list2,ids,slen);
175 #endif
176 
177  memcpy(list2,pz->get_u_bar(),nvar*sizeofdouble);
178  memcpy(list2,pz->get_u_dot_bar(),nvar*sizeofdouble);
179  *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
180  ++nlist2;
181  // }
182  //
183  // ****************************************************************
184 #if defined(PRINT_DERS)
185  print_derivatives(pz,"z");
186  print_derivatives(px,"x");
187 #endif
188 
189  //double df=(pf->df)(xu);
190  //double d2f=(pf->d2f)(xu);
191  //double d3f=(pf->d3f)(xu);
192 
193  double* px_u_bari = px->u_bar;
194  double* pz_u_bari = pz->u_bar;
195  for (size_t i=0;i<nvar;i++)
196  {
197  //px->u_bar[i]+=(pf->df)(xu)* pz->u_bar[i];
198  *px_u_bari += df * *pz_u_bari;
199 
200  ++px_u_bari;
201  ++pz_u_bari;
202  }
203  px_u_bari = px->u_bar;
204  double* pxdoti = xdot;
205  double* pz_u_dot_bari = pz->u_dot_bar;
206  for (size_t i=0;i<nvar;i++)
207  {
208  //px->u_bar[i]+=(pf->d2f)(xu)*xdot[i]*pz->u_dot_bar[i];
209  *px_u_bari += d2f * *pxdoti * *pz_u_dot_bari;
210 
211  ++px_u_bari;
212  ++pxdoti;
213  ++pz_u_dot_bari;
214  }
215  double* px_u_dot_bari = px->u_dot_bar;
216  pz_u_dot_bari = pz->u_dot_bar;
217  for (size_t i=0;i<nvar;i++)
218  {
219  //px->u_dot_bar[i]+=(pf->df)(xu)*pz->u_dot_bar[i];
220  *px_u_dot_bari += df * *pz_u_dot_bari;
221 
222  ++px_u_dot_bari;
223  ++pz_u_dot_bari;
224  }
225 
226  // !!!!!!!!!!!!!!!!!!!!!!
227  memset(pz->u_bar, 0, nvar * sizeofdouble);
228  memset(pz->u_dot_bar, 0, nvar * sizeofdouble);
229 
230 #if defined(PRINT_DERS)
231  print_derivatives(pz,"z");
232  print_derivatives(px,"x");
233 #endif
234 }
235 
240 void read_pass1_2_dv(void)
241 {
242  //const int nlist_record_size=sizeof(int)+sizeof(char*);
243  // We are going forward for bptr and backword for bptr2
244  //
245  // list 1
246  //
247  unsigned int nvar=df1b2variable::nvar;
249 
250  size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header)+sizeof(char*)
251  +sizeof(double)+nvar*sizeof(double);
252 // string identifier debug stuff
253 #if defined(SAFE_ALL)
254  char ids[]="HU";
255  int slen=strlen(ids);
256  total_bytes+=slen;
257 #endif
258 
259  list.check_buffer_size(total_bytes);
260 // end of string identifier debug stuff
261 
262  list.saveposition(); // save pointer to beginning of record;
264  // nlist-=sizeof(int);
265  // get record size
266  int num_bytes=nlist.bptr->numbytes;
267  // nlist+=nlist_record_size;
268  //
269  // list 2
270  //
273  // get record size
274  int num_bytes2=*nlist2.bptr;
275  --nlist2;
276  // backup the size of the record
277  list2-=num_bytes2;
278  list2.saveposition();
279  // save the pointer to the beginning of the record
280  // bptr and bptr2 now both point to the beginning of their records
281 
282  double xu;
283  double * xdot;
284  //df1b2_header x,z;
285  //df1b2function1 * pf;
286 
287  // get info from tape1
288  // get info from tape1
289 #if defined(SAFE_ALL)
290  checkidentiferstring("HU",list);
291  checkidentiferstring("JE",list2);
292 #endif
293  df1b2_header * px=(df1b2_header *) list.bptr;
294  list.bptr+=sizeof(df1b2_header);
295  df1b2_header * pz=(df1b2_header *) list.bptr;
296  list.bptr+=sizeof(df1b2_header);
297 
298  //pf=*(df1b2function1 **) list.bptr;
299 
300  constexpr size_t sizeofdouble = sizeof(double);
301  double df=*(double*) list.bptr;
302  list.bptr+=sizeofdouble;
303  double d2f=*(double*) list.bptr;
304  list.bptr+=sizeofdouble;
305  double d3f=*(double*) list.bptr;
306  list.bptr+=sizeofdouble;
307  //list.bptr+=sizeof(char*);
308  memcpy(&xu,list.bptr,sizeofdouble);
309  list.bptr+=sizeof(double);
310  xdot=(double*)list.bptr;
311  list.restoreposition(num_bytes); // save pointer to beginning of record;
312 
313  double* zbar = (double*)list2.bptr;
314  double* zdotbar = (double*)(list2.bptr+nvar*sizeofdouble);
315 
316  double* x_tilde=px->get_u_tilde();
317  double* x_dot_tilde=px->get_u_dot_tilde();
318  double* x_bar_tilde=px->get_u_bar_tilde();
319  double* x_dot_bar_tilde=px->get_u_dot_bar_tilde();
320  double* z_bar_tilde=pz->get_u_bar_tilde();
321  double* z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
322 #if defined(PRINT_DERS)
323  print_derivatives(pz,"z");
324  print_derivatives(px,"x");
325 #endif
326  // Do second "reverse-reverse" pass calculations
327 
328  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
329  memset(z_bar_tilde, 0, nvar * sizeofdouble);
330  memset(z_dot_bar_tilde, 0, nvar * sizeofdouble);
331 
332  //double df=(pf->df)(xu);
333  //double d2f=(pf->d2f)(xu);
334  //double d3f=(pf->d3f)(xu);
335  double* x_bar_tildei = x_bar_tilde;
336  double* zbari = zbar;
337  double* z_bar_tildei = z_bar_tilde;
338  for (size_t i=0;i<nvar;i++)
339  {
340  //*x_tilde+=(pf->d2f)(xu)*zbar[i]*x_bar_tilde[i];
341  *x_tilde += d2f * *zbari * *x_bar_tildei;
342  //z_bar_tilde[i]+=(pf->df)(xu)*x_bar_tilde[i];
343  *z_bar_tildei += df * *x_bar_tildei;
344 
345  ++x_bar_tildei;
346  ++zbari;
347  ++z_bar_tildei;
348  }
349 
350  double* x_dot_bar_tildei = x_dot_bar_tilde;
351  double* z_dot_bar_tildei = z_dot_bar_tilde;
352  double* zdotbari = zdotbar;
353  z_bar_tildei = z_bar_tilde;
354  for (size_t i=0;i<nvar;i++)
355  {
356  //*x_tilde+=(pf->d2f)(xu)*zdotbar[i]*x_dot_bar_tilde[i];
357  *x_tilde += d2f * *zdotbari * *x_dot_bar_tildei;
358  //z_dot_bar_tilde[i]+=(pf->df)(xu)*x_dot_bar_tilde[i];
359  *z_dot_bar_tildei += df * *x_dot_bar_tildei;
360 
361  ++x_dot_bar_tildei;
362  ++zdotbari;
363  ++z_dot_bar_tildei;
364  }
365  x_bar_tildei = x_bar_tilde;
366  zdotbari = zdotbar;
367  double* xdoti = xdot;
368  z_dot_bar_tildei = z_dot_bar_tilde;
369  double* x_dot_tildei = x_dot_tilde;
370  for (size_t i=0;i<nvar;i++)
371  {
372  //x_dot_tilde[i]+=(pf->d2f)(xu)*zdotbar[i]*x_bar_tilde[i];
373  //z_dot_bar_tilde[i]+=(pf->d2f)(xu)*xdot[i]*x_bar_tilde[i];
374  //*x_tilde+=(pf->d3f)(xu)*xdot[i]*zdotbar[i]*x_bar_tilde[i];
375  *x_dot_tildei += d2f * *zdotbari * *x_bar_tildei;
376  *z_dot_bar_tildei +=d2f * *xdoti * *x_bar_tildei;
377  *x_tilde += d3f * *xdoti * *zdotbari * *x_bar_tildei;
378 
379  ++x_bar_tildei;
380  ++zdotbari;
381  ++xdoti;
382  ++z_dot_bar_tildei;
383  ++x_dot_tildei;
384  }
385  list2.restoreposition();
386 #if defined(PRINT_DERS)
387  print_derivatives(pz,"z");
388  print_derivatives(px,"x");
389 #endif
390 }
391 
396 void read_pass1_3_dv(void)
397 {
398  // We are going backword for bptr and forward for bptr2
399  // the current entry+2 in bptr is the size of the record i.e
400  // points to the next record
401  unsigned int nvar=df1b2variable::nvar;
404  // nlist-=sizeof(int);
405  // get record size
406  int num_bytes=nlist.bptr->numbytes;
407  // backup the size of the record
408  list-=num_bytes;
409  list.saveposition(); // save pointer to beginning of record;
410  // save the pointer to the beginning of the record
411  //df1b2_header x,z;
412  //df1b2function1 * pf;
413 
414  // get info from tape1
415 #if defined(SAFE_ALL)
416  checkidentiferstring("HU",list);
417 #endif
418  df1b2_header * px=(df1b2_header *) list.bptr;
419  list.bptr+=sizeof(df1b2_header);
420  df1b2_header * pz=(df1b2_header *) list.bptr;
421  list.bptr+=sizeof(df1b2_header);
422  constexpr size_t sizeofdouble = sizeof(double);
423  //pf=*(df1b2function1 **) list.bptr;
424  //list.bptr+=sizeof(char*);
425  double df=*(double*) list.bptr;
426  list.bptr+=sizeofdouble;
427  double d2f=*(double*) list.bptr;
428  list.bptr+=sizeofdouble;
429  //double d3f=*(double*) list.bptr;
430  list.bptr+=sizeofdouble;
431  double xu;
432  memcpy(&xu,list.bptr,sizeofdouble);
433  list.bptr+=sizeofdouble;
434  double* xdot=(double*)list.bptr;
435  list.restoreposition(); // save pointer to beginning of record;
436 
437 #if defined(PRINT_DERS)
438  print_derivatives(pz,"z");
439  print_derivatives(px,"x");
440 #endif
441 
442  //double df=(pf->df)(xu);
443  //double d2f=(pf->d2f)(xu);
444  //*(px->u_tilde)+=(pf->df)(xu)* *(pz->u_tilde);
445  *(px->u_tilde)+=df * *(pz->u_tilde);
446 
447  double* pxdoti = xdot;
448  double* pz_u_dot_tildei = pz->u_dot_tilde;
449  for (size_t i=0;i<nvar;i++)
450  {
451  //*(px->u_tilde)+=(pf->d2f)(xu)*xdot[i]*pz->u_dot_tilde[i];
452  *(px->u_tilde) += d2f * *pxdoti * *pz_u_dot_tildei;
453 
454  ++pxdoti;
455  ++pz_u_dot_tildei;
456  }
457  double* px_u_dot_tildei = px->u_dot_tilde;
458  pz_u_dot_tildei = pz->u_dot_tilde;
459  for (size_t i=0;i<nvar;i++)
460  {
461  //px->u_dot_tilde[i]+=(pf->df)(xu)*pz->u_dot_tilde[i];
462  *px_u_dot_tildei += df * *pz_u_dot_tildei;
463 
464  ++px_u_dot_tildei;
465  ++pz_u_dot_tildei;
466  }
467  *(pz->u_tilde)=0;
468  memset(pz->u_dot_tilde, 0, nvar * sizeofdouble);
469 #if defined(PRINT_DERS)
470  print_derivatives(pz,"z");
471  print_derivatives(px,"x");
472 #endif
473 }
char * bptr
Definition: df1b2fun.h:543
double * get_u_bar_tilde() const
Definition: df1b2fun.h:234
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
double * u_bar
Definition: df1b2fun.h:197
void read_pass1_1_dv(void)
Description not yet available.
Definition: df1b2f21.cpp:111
#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 read_pass1_2_dv(void)
Description not yet available.
Definition: df1b2f21.cpp:240
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
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_3_dv(void)
Description not yet available.
Definition: df1b2f21.cpp:396
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
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
void ad_read_pass1_der_values(void)
Description not yet available.
Definition: df1b2f21.cpp:87
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
double * get_u_bar() const
Definition: df1b2fun.h:230
int numbytes
Definition: df1b2fun.h:594
void saveposition(void)
Definition: df1b2fun.h:552
test_smartlist list2
Definition: df1b2fun.h:755