ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2fn3.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 void read_pass1_plus_eq_1(void);
13 void read_pass1_plus_eq_2(void);
14 void read_pass1_plus_eq_3(void);
15 //#define ADDEBUG_PRINT
16 #if defined(ADDEBUG_PRINT)
17  extern int addebug_count;
18 #endif
19 //#define PRINT_DERS
20 
21 
22 #if defined(PRINT_DERS)
23 
27 void print_derivatives(const adstring& s, double f, double df,
28  double d2f,double d3f,int bflag)
29 {
30  ostream * derout;
31  derout=&cout;
32  if (bflag)
33  {
34  *derout << " ---------------------------------------- " << endl;
35  }
36  *derout << "Function: " << s << " " << "f = " << f
37  << " df = " << df << " d2f = " << d2f << " d3f = " << d3f << endl;
38 }
39 
44 void print_derivatives(const adstring& s, double f, double df1,
45  double df2,double df11,double df12, double df22,
46  double df111, double df112, double df122, double df222,int bflag)
47 {
48  ostream * derout;
49  derout=&cout;
50  if (bflag)
51  {
52  *derout << endl << " --------------------------------- " << endl;
53  }
54  *derout << "Function: " << s << " " << "f = " << f
55  << " df1 = " << df1
56  << " df2 = " << df2
57  << " df11 = " << df11
58  << " df12 = " << df12
59  << " df22 = " << df22
60  << " df111 = " << df111
61  << " df112 = " << df112
62  << " df122 = " << df122
63  << " df222 = " << df222 << endl;
64 }
65 
70 void print_derivatives(df1b2_header * px,const char * s,
71  int bflag)
72 {
73  ostream * derout;
74  derout=&cout;
75  //*derout << derprintcount << " " << endl;
76  if (bflag)
77  {
78  *derout << endl << " --------------------------------- " << endl;
79  }
80  *derout << "pass " << df1b2variable::passnumber;
81  *derout << " variable " << s << " address "
82  << int(px->u) << endl;
83  *derout << "u\t\t = " << *px->u << " ";
84  *derout << endl;
85 
86  *derout << "udot\t\t = ";
87  for (unsigned int i=0;i<df1b2variable::nvar;i++)
88  *derout << px->u_dot[i] << " ";
89  *derout << endl;
90 
91  *derout << "u_bar\t\t = ";
92  for (unsigned int i=0;i<df1b2variable::nvar;i++)
93  *derout << px->u_bar[i] << " ";
94  *derout << endl;
95 
96  *derout << "u_dot_bar\t = ";
97  for (unsigned int i=0;i<df1b2variable::nvar;i++)
98  *derout << px->u_dot_bar[i] << " ";
99  *derout << endl;
100 
101  if (df1b2variable::passnumber>1)
102  {
103  *derout << "u_tilde\t\t = " << *px->u_tilde << " ";
104  *derout << endl;
105 
106  *derout << "u_dot_tilde\t = ";
107  for (unsigned int i=0;i<df1b2variable::nvar;i++)
108  *derout << px->u_dot_tilde[i] << " ";
109  *derout << endl;
110 
111  *derout << "u_bar_tilde\t = ";
112  for (unsigned int i=0;i<df1b2variable::nvar;i++)
113  *derout << px->u_bar_tilde[i] << " ";
114  *derout << endl;
115 
116  *derout << "u_dot_bar_tilde\t = ";
117  for (unsigned int i=0;i<df1b2variable::nvar;i++)
118  *derout << px->u_dot_bar_tilde[i] << " ";
119  *derout << endl;
120  }
121  *derout << endl;
122 }
123 #endif
124 
130 {
132  double * xd=x.get_u_dot();
133  double * zd=get_u_dot();
134  *get_u()+=*x.get_u();
135  for (unsigned int i=0;i<df1b2variable::nvar;i++)
136  {
137  *zd++ += *xd++;
138  }
139 
140  // WRITE WHATEVER ON TAPE
141  //df1b2tape->set_tapeinfo_header(&x,&z,this,xd);
142  // save stuff for first reverse pass
143  // need &x, &z, this,
146  return *this;
147 }
148 void ad_read_pass1_plus_eq(void);
149 
155  df1b2variable * pz)
156 {
157  ncount++;
158 #if defined(CHECK_COUNT)
159  if (ncount >= ncount_check)
160  ncount_checker(ncount,ncount_check);
161 #endif
162  //int nvar=df1b2variable::nvar;
166 
167  size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
168 #if defined(SAFE_ALL)
169  char ids[]="JK";
170  int slen=strlen(ids);
171  total_bytes+=slen;
172 #endif
173  list.check_buffer_size(total_bytes);
174  void * tmpptr=list.bptr;
175 #if defined(SAFE_ALL)
176  memcpy(list,ids,slen);
177 #endif
178 
179  memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
180  memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
181 
182  // ***** write record size
183  nlist.bptr->numbytes=adptr_diff(list.bptr,tmpptr);
185  ++nlist;
186  return 0;
187 }
188 
194 {
196  {
197  case 1:
199  break;
200  case 2:
202  break;
203  case 3:
205  break;
206  default:
207  cerr << "illegal value for df1b2variable::pass = "
209  ad_exit(1);
210  }
211 }
212 
218 {
219  // We are going backword for bptr and forward for bptr2
220  // the current entry+2 in bptr is the size of the record i.e
221  // points to the next record
222  unsigned int nvar=df1b2variable::nvar;
225  // nlist-=sizeof(int);
226  // get record size
227  int num_bytes=nlist.bptr->numbytes;
228  // backup the size of the record
229  list-=num_bytes;
230  list.saveposition(); // save pointer to beginning of record;
231  // save the pointer to the beginning of the record
232 #if defined(SAFE_ALL)
233  checkidentiferstring("JK",list);
234 #endif
235 
236  // get info from tape1
237  df1b2_header * px=(df1b2_header *) list.bptr;
238  list.bptr+=sizeof(df1b2_header);
239  df1b2_header * pz=(df1b2_header *) list.bptr;
240 
241  list.restoreposition(); // save pointer to beginning of record;
242 
243  // Do first reverse paSS calculations
244  // ****************************************************************
245  // turn this off if no third derivatives are calculated
246  // if (!no_third_derivatives)
247  // {
248  // save for second reverse pass
249  // save identifier 1
250  // fixed_smartlist2& nlist2=f1b2gradlist->nlist2;
251  // test_smartlist& list2=f1b2gradlist->list2;
252  //int total_bytes=2*nvar*sizeof(double);
253 // string identifier debug stuff
254 #if defined(SAFE_ALL)
255  //char ids[]="IL";
256  //int slen=strlen(ids);
257  //total_bytes+=slen;
258 #endif
259  //list2.check_buffer_size(total_bytes);
260  //void * tmpptr2=list2.bptr;
261 #if defined(SAFE_ALL)
262  //memcpy(list2,ids,slen);
263 #endif
264  //memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
265  //memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
266  //*nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
267  //nlist2++;
268  // }
269  //
270  // ****************************************************************
271 #if defined(PRINT_DERS)
272  print_derivatives(px,"x");
273  print_derivatives(pz,"z");
274 #endif
275 
276  double* pz_u_bar = pz->u_bar;
277  double* px_u_bar = px->u_bar;
278  for (unsigned int i = 0; i < nvar; ++i)
279  {
280  *px_u_bar += *pz_u_bar;
281  ++pz_u_bar;
282  ++px_u_bar;
283  }
284  double* pz_u_dot_bar = pz->u_dot_bar;
285  double* px_u_dot_bar = px->u_dot_bar;
286  for (unsigned int i = 0; i < nvar; ++i)
287  {
288  *px_u_dot_bar += *pz_u_dot_bar;
289  ++pz_u_dot_bar;
290  ++px_u_dot_bar;
291  }
292 #if defined(PRINT_DERS)
293  print_derivatives(px,"x");
294  print_derivatives(pz,"z");
295 #endif
296 }
297 
303 {
304  //const int nlist_record_size=sizeof(int)+sizeof(char*);
305  // We are going forward for bptr and backword for bptr2
306  //
307  // list 1
308  //
309  unsigned int nvar=df1b2variable::nvar;
311 
312  size_t total_bytes=sizeof(df1b2_header)+sizeof(df1b2_header);
313 #if defined(SAFE_ALL)
314  char ids[]="JK";
315  int slen=strlen(ids);
316  total_bytes+=slen;
317 #endif
318  list.check_buffer_size(total_bytes);
319 
320  list.saveposition(); // save pointer to beginning of record;
322  // nlist-=sizeof(int);
323  // get record size
324  int num_bytes=nlist.bptr->numbytes;
325  // nlist+=nlist_record_size;
326  //
327  // list 2
328  //
329  //test_smartlist & list2=f1b2gradlist->list2;
330  //fixed_smartlist2 & nlist2=f1b2gradlist->nlist2;
331  // get record size
332  //int num_bytes2=*nlist2.bptr;
333  //nlist2--;
334  // backup the size of the record
335  //list2-=num_bytes2;
336  //list2.saveposition(); // save pointer to beginning of record;
337  // save the pointer to the beginning of the record
338  // bptr and bptr2 now both point to the beginning of their records
339 #if defined(SAFE_ALL)
340  checkidentiferstring("JK",list);
341  //checkidentiferstring("IL",list2);
342 #endif
343 
344  // get info from tape1
345  df1b2_header * px=(df1b2_header *) list.bptr;
346  list.bptr+=sizeof(df1b2_header);
347  df1b2_header * pz=(df1b2_header *) list.bptr;
348 
349  list.restoreposition(num_bytes); // save pointer to beginning of record;
350 
351  //double * zbar=(double*)list2.bptr;
352  //double * zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
353 
354  double * x_bar_tilde=px->get_u_bar_tilde();
355  double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
356  double * z_bar_tilde=pz->get_u_bar_tilde();
357  double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
358  // Do second "reverse-reverse" pass calculations
359  for (unsigned int i = 0; i < nvar; ++i)
360  {
361  *z_bar_tilde += *x_bar_tilde;
362  ++z_bar_tilde;
363  ++x_bar_tilde;
364  }
365  for (unsigned int i = 0; i < nvar; ++i)
366  {
367  *z_dot_bar_tilde += *x_dot_bar_tilde;
368  ++x_dot_bar_tilde;
369  ++z_dot_bar_tilde;
370  }
371  //list2.restoreposition(); // save pointer to beginning of record;
372 #if defined(PRINT_DERS)
373  print_derivatives(px,"x");
374  print_derivatives(pz,"z");
375 #endif
376 }
377 
383 {
384  // We are going backword for bptr and forward for bptr2
385  // the current entry+2 in bptr is the size of the record i.e
386  // points to the next record
387  unsigned int nvar=df1b2variable::nvar;
390  // nlist-=sizeof(int);
391  // get record size
392  int num_bytes=nlist.bptr->numbytes;
393  // backup the size of the record
394  list-=num_bytes;
395  list.saveposition(); // save pointer to beginning of record;
396  // save the pointer to the beginning of the record
397 
398 #if defined(SAFE_ALL)
399  checkidentiferstring("JK",list);
400 #endif
401  // get info from tape1
402  df1b2_header * px=(df1b2_header *) list.bptr;
403  list.bptr+=sizeof(df1b2_header);
404  df1b2_header * pz=(df1b2_header *) list.bptr;
405 
406  list.restoreposition(); // save pointer to beginning of record;
407 
408  *(px->u_tilde)+=*pz->u_tilde;
409 
410  double* px_u_dot_tilde = px->u_dot_tilde;
411  double* pz_u_dot_tilde = pz->u_dot_tilde;
412  for (unsigned int i = 0; i < nvar; ++i)
413  {
414  *px_u_dot_tilde += *pz_u_dot_tilde;
415  ++pz_u_dot_tilde;
416  ++px_u_dot_tilde;
417  }
418 #if defined(PRINT_DERS)
419  print_derivatives(px,"x");
420  print_derivatives(pz,"z");
421 #endif
422 }
423 
429 {
430  if (value(x)>=0.0)
431  return x;
432  else
433  return -x;
434 }
435 
441 {
442  df1b2vector tmp(t1.indexmin(),t1.indexmax());
443  for (int i=t1.indexmin(); i<=t1.indexmax(); i++)
444  {
445  tmp(i)=fabs(t1(i));
446  }
447 
448  return(tmp);
449 }
450 
456 {
457  df1b2variable tmp;
458  int mmin=t1.indexmin();
459  int mmax=t1.indexmax();
460  tmp=t1(mmin);
461  for (int i=mmin+1; i<=mmax; i++)
462  {
463  if (value(tmp)<value(t1(i))) tmp=t1(i);
464  }
465  return(tmp);
466 }
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
void read_pass1_plus_eq_3(void)
Description not yet available.
Definition: df1b2fn3.cpp:382
double * u_bar_tilde
Definition: df1b2fun.h:201
double * u_bar
Definition: df1b2fun.h:197
double * get_u() const
Definition: df1b2fun.h:228
void read_pass1_plus_eq_2(void)
Description not yet available.
Definition: df1b2fn3.cpp:302
#define x
#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
Description not yet available.
Definition: df1b2fun.h:953
int adptr_diff(void *x, void *y)
Definition: df1b2fn2.cpp:110
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
int indexmin(void) const
Definition: df1b2fun.h:969
void ncount_checker(int ncount, int ncount_check)
double * u_tilde
Definition: df1b2fun.h:199
df1b2variable & operator+=(const df1b2variable &v)
Description not yet available.
Definition: df1b2fn3.cpp:129
void restoreposition(void)
Definition: df1b2fun.h:560
Description not yet available.
Definition: df1b2fun.h:266
fixed_smartlist nlist
Definition: df1b2fun.h:754
double * get_u_dot() const
Definition: df1b2fun.h:229
int write_pass1_pluseq(const df1b2variable *px, df1b2variable *pz)
Description not yet available.
Definition: df1b2fn3.cpp:154
double * u_dot_bar_tilde
Definition: df1b2fun.h:202
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 ad_read_pass1_plus_eq(void)
Description not yet available.
Definition: df1b2fn3.cpp:193
static int no_derivatives
Definition: df1b2fun.h:759
Description not yet available.
Definition: df1b2fun.h:527
double * get_u_dot_bar_tilde() const
Definition: df1b2fun.h:235
static unsigned int nvar
Definition: df1b2fun.h:290
void(* ADrfptr)(void)
Definition: df1b2fun.cpp:139
int indexmax(void) const
Definition: df1b2fun.h:970
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_plus_eq_1(void)
Description not yet available.
Definition: df1b2fn3.cpp:217
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
double * u
Definition: df1b2fun.h:195
#define max(a, b)
Definition: cbivnorm.cpp:189
int numbytes
Definition: df1b2fun.h:594
void saveposition(void)
Definition: df1b2fun.h:552
double * u_dot
Definition: df1b2fun.h:196