ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2fn4.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_eq_1(void);
13 void read_pass1_eq_2(void);
14 void read_pass1_eq_3(void);
15 
16 //#define ADDEBUG_PRINT
17 #if defined(ADDEBUG_PRINT)
18  extern int addebug_count;
19 #endif
20 
21 #ifndef OPT_LIB
22  #include <cassert>
23  #include <climits>
24 #endif
25 
31 {
32  if (ptr != _x.ptr)
33  {
35  if (allocated() )
36  {
37  //int nc=*ncopies;
38  initialize();
39  //*ncopies=nc;
40  double * xd=x.get_u_dot();
41  double * zd=get_u_dot();
42  *get_u()=*x.get_u();
43  for (unsigned int i=0;i<df1b2variable::nvar;i++)
44  {
45  *zd++ += *xd++;
46  }
47 
48  // WRITE WHATEVER ON TAPE
49  //df1b2tape->set_tapeinfo_header(&x,&z,this,xd);
50  // save stuff for first reverse pass
51  // need &x, &z, this,
52 
53  // !!! nov 1 02
54  // !!! nov 11 02
59  }
60  else // guts of copy constructor
61  {
62  if (x.ptr)
63  {
64  ptr=x.ptr;
65  ncopies=x.ncopies;
66  (*ncopies)++;
67  u=x.u;
68  u_dot=x.u_dot;
69  u_bar=x.u_bar;
70  u_dot_bar=x.u_dot_bar;
71  u_tilde=x.u_tilde;
72  u_dot_tilde=x.u_dot_tilde;
73  u_bar_tilde=x.u_bar_tilde;
74  u_dot_bar_tilde=x.u_dot_bar_tilde;
75  }
76  else
77  {
78  ptr=NULL;
79  ncopies=NULL;
80  u=NULL;
81  u_dot=NULL;
82  u_bar=NULL;
83  u_dot_bar=NULL;
84  u_tilde=NULL;
85  u_dot_tilde=NULL;
86  u_bar_tilde=NULL;
87  u_dot_bar_tilde=NULL;
88  }
89  }
90  }
91  return *this;
92 }
93 
94 void ad_read_pass1_eq(void);
95 
101  df1b2variable * pz)
102 {
104  //char * pg=0;
105  ncount++;
106 #if defined(CHECK_COUNT)
107  if (ncount >= ncount_check)
108  ncount_checker(ncount,ncount_check);
109 #endif
110 
111  constexpr size_t sizeofdf1b2_header = sizeof(df1b2_header);
112  size_t total_bytes = sizeofdf1b2_header + sizeofdf1b2_header;
113 // string identifier debug stuff
114 #if defined(SAFE_ALL)
115  char ids[]="GV";
116  int slen=strlen(ids);
117  total_bytes+=slen;
118 #endif
119  list.check_buffer_size(total_bytes);
120  void * tmpptr=list.bptr;
121 #if defined(SAFE_ALL)
122  memcpy(list,ids,slen);
123 #endif
124 
125  memcpy(list,(df1b2_header*)(px), sizeofdf1b2_header);
126  memcpy(list,(df1b2_header*)(pz), sizeofdf1b2_header);
127 
130  ++nlist;
131  return 0;
132 }
133 
139 {
141  {
142  case 1:
143  read_pass1_eq_1();
144  break;
145  case 2:
146  read_pass1_eq_2();
147  break;
148  case 3:
149  read_pass1_eq_3();
150  break;
151  default:
152  cerr << "illegal value for df1b2variable::pass = "
154  ad_exit(1);
155  }
156 }
157 
162 void read_pass1_eq_1(void)
163 {
164  // We are going backword for bptr and forward for bptr2
165  // the current entry+2 in bptr is the size of the record i.e
166  // points to the next record
167  unsigned int nvar=df1b2variable::nvar;
170  // nlist-=sizeof(int);
171  // get record size
172  int num_bytes=nlist.bptr->numbytes;
173  // backup the size of the record
174  list-=num_bytes;
175  list.saveposition(); // save pointer to beginning of record;
176 
177 #if defined(SAFE_ALL)
178  checkidentiferstring("GV",list);
179 #endif
180 
181  constexpr size_t sizeofdf1b2_header = sizeof(df1b2_header);
182  // get info from tape1
183  df1b2_header * px=(df1b2_header *) list.bptr;
184  list.bptr += sizeofdf1b2_header;
185  df1b2_header * pz=(df1b2_header *) list.bptr;
186  list.bptr += sizeofdf1b2_header;
187  list.restoreposition(); // save pointer to beginning of record;
188 
189  // Do first reverse paSS calculations
190  // ****************************************************************
191  // turn this off if no third derivatives are calculated
192  // if (!no_third_derivatives)
193  // {
194  // save for second reverse pass
195  // save identifier 1
198 
199  constexpr size_t sizeofdouble = sizeof(double);
200  size_t total_bytes = 2 * nvar * sizeofdouble;
201 
202 #if defined(SAFE_ALL)
203  char ids[]="HT";
204  int slen=strlen(ids);
205  total_bytes+=slen;
206 #endif
207 
208  list2.check_buffer_size(total_bytes);
209  void * tmpptr2=list2.bptr;
210 
211 #if defined(SAFE_ALL)
212  memcpy(list2,ids,slen);
213 #endif
214 
215  memcpy(list2,pz->get_u_bar(), nvar * sizeofdouble);
216  memcpy(list2,pz->get_u_dot_bar(), nvar * sizeofdouble);
217  *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
218  ++nlist2;
219  // }
220  //
221  // ****************************************************************
222 #if defined(PRINT_DERS)
223  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
224  print_derivatives(pz,"z");
225  print_derivatives(px,"x");
226 #endif
227 
228  double* pxu_bar = px->u_bar;
229  double* pzu_bar = pz->u_bar;
230  for (size_t i=0;i<nvar;i++)
231  {
232  *pxu_bar += *pzu_bar;
233  ++pxu_bar;
234  ++pzu_bar;
235 
236 #if defined(ADDEBUG_PRINT)
237  addebug_count++;
238  if (addebug_count == 49)
239  {
240  cout << "trap" << endl;
241  }
242  cout << px->u_bar[i] << " " << pz->u_bar[i] << " " << addebug_count << endl;
243 #endif
244  }
245  double* pxu_dot_bar = px->u_dot_bar;
246  double* pzu_dot_bar = pz->u_dot_bar;
247  for (size_t i=0;i<nvar;i++)
248  {
249  *pxu_dot_bar += *pzu_dot_bar;
250  ++pxu_dot_bar;
251  ++pzu_dot_bar;
252  }
253 
254  size_t size = nvar * sizeofdouble;
255  memset(pz->u_bar, 0, size);
256  memset(pz->u_dot_bar, 0, size);
257 #if defined(PRINT_DERS)
258  print_derivatives(px,"x");
259  print_derivatives(pz,"z");
260 #endif
261 }
262 
267 void read_pass1_eq_2(void)
268 {
269  //const int nlist_record_size=sizeof(int)+sizeof(char*);
270  // We are going forward for bptr and backword for bptr2
271  //
272  // list 1
273  //
274  unsigned int nvar=df1b2variable::nvar;
276 
277  constexpr size_t sizeofdf1b2_header = sizeof(df1b2_header);
278  size_t total_bytes = sizeofdf1b2_header + sizeofdf1b2_header;
279 #if defined(SAFE_ALL)
280  char ids[]="GV";
281  int slen=strlen(ids);
282  total_bytes+=slen;
283 #endif
284  list.check_buffer_size(total_bytes);
285 
286  list.saveposition(); // save pointer to beginning of record;
288  // nlist-=sizeof(int);
289  // get record size
290  int num_bytes=nlist.bptr->numbytes;
291  // nlist+=nlist_record_size;
292  //
293  // list 2
294  //
297  // get record size
298  int num_bytes2=*nlist2.bptr;
299  --nlist2;
300  // backup the size of the record
301  list2-=num_bytes2;
302  list2.saveposition(); // save pointer to beginning of record;
303 #if defined(SAFE_ALL)
304  checkidentiferstring("GV",list);
305  checkidentiferstring("HT",list2);
306 #endif
307  // save the pointer to the beginning of the record
308  // bptr and bptr2 now both point to the beginning of their records
309 
310  // get info from tape1
311  df1b2_header * px=(df1b2_header *) list.bptr;
312  list.bptr += sizeofdf1b2_header;
313  df1b2_header * pz=(df1b2_header *) list.bptr;
314  list.restoreposition(num_bytes); // save pointer to beginning of record;
315 
316  //double* zbar=(double*)list2.bptr;
317  //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
318 
319  list2.restoreposition(); // save pointer to beginning of record;
320 
321  double * x_bar_tilde=px->get_u_bar_tilde();
322  double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
323  double * z_bar_tilde=pz->get_u_bar_tilde();
324  double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
325  // Do second "reverse-reverse" pass calculations
326 
327 #if defined(PRINT_DERS)
328  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
329  print_derivatives(pz,"z");
330  print_derivatives(px,"x");
331 #endif
332 
333  constexpr size_t sizeofdouble = sizeof(double);
334  memset(z_bar_tilde, 0, nvar * sizeofdouble);
335  memset(z_dot_bar_tilde, 0, nvar * sizeofdouble);
336 
337  double* pz_bar_tilde = z_bar_tilde;
338  double* px_bar_tilde = x_bar_tilde;
339  for (size_t i=0;i<nvar;i++)
340  {
341  *pz_bar_tilde += *px_bar_tilde;
342  ++pz_bar_tilde;
343  ++px_bar_tilde;
344  }
345 
346  double* pz_dot_bar_tilde = z_dot_bar_tilde;
347  double* px_dot_bar_tilde = x_dot_bar_tilde;
348  for (size_t i=0;i<nvar;i++)
349  {
350  *pz_dot_bar_tilde += *px_dot_bar_tilde;
351  ++pz_dot_bar_tilde;
352  ++px_dot_bar_tilde;
353  }
354 
355 #if defined(PRINT_DERS)
356  print_derivatives(px,"x");
357  print_derivatives(pz,"z");
358 #endif
359 }
360 
365 void read_pass1_eq_3(void)
366 {
367  // We are going backword for bptr and forward for bptr2
368  // the current entry+2 in bptr is the size of the record i.e
369  // points to the next record
370  unsigned int nvar=df1b2variable::nvar;
373  // nlist-=sizeof(int);
374  // get record size
375  int num_bytes=nlist.bptr->numbytes;
376  // backup the size of the record
377  list-=num_bytes;
378  list.saveposition(); // save pointer to beginning of record;
379  // save the pointer to the beginning of the record
380 
381 #if defined(SAFE_ALL)
382  checkidentiferstring("GV",list);
383 #endif
384  constexpr size_t sizeofdf1b2_header = sizeof(df1b2_header);
385  // get info from tape1
386  df1b2_header * px=(df1b2_header *) list.bptr;
387  list.bptr+=sizeofdf1b2_header;
388  df1b2_header * pz=(df1b2_header *) list.bptr;
389  list.restoreposition(); // save pointer to beginning of record;
390 
391 #if defined(PRINT_DERS)
392  print_derivatives(" assign ", 1 ,1 ,0, 0,1);
393  print_derivatives(pz,"z");
394  print_derivatives(px,"x");
395 #endif
396 
397  *(px->u_tilde)+=*pz->u_tilde;
398 #if defined(ADDEBUG_PRINT)
399  addebug_count++;
400  if (addebug_count == 49)
401  {
402  cout << "trap" << endl;
403  }
404  cout << *(px->u_tilde) << " " << *(pz->u_tilde) << " " << addebug_count
405  << endl;
406 #endif
407  double* px_u_dot_tilde = px->u_dot_tilde;
408  double* pz_u_dot_tilde = pz->u_dot_tilde;
409  for (size_t i=0;i<nvar;i++)
410  {
411  *px_u_dot_tilde += *pz_u_dot_tilde;
412  ++px_u_dot_tilde;
413  ++pz_u_dot_tilde;
414  }
415 #if defined(PRINT_DERS)
416  print_derivatives(px,"x");
417  print_derivatives(pz,"z");
418 #endif
419  *(pz->u_tilde)=0;
420  constexpr size_t sizeofdouble = sizeof(double);
421  memset(pz->u_dot_tilde, 0, nvar * sizeofdouble);
422 }
char * bptr
Definition: df1b2fun.h:543
double * get_u_bar_tilde() const
Definition: df1b2fun.h:234
void ad_read_pass1_eq(void)
Description not yet available.
Definition: df1b2fn4.cpp:138
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_tilde
Definition: df1b2fun.h:201
void read_pass1_eq_2(void)
Description not yet available.
Definition: df1b2fn4.cpp:267
double * u_bar
Definition: df1b2fun.h:197
df1b2variable & operator=(const df3_one_variable &v)
Definition: df3fun.cpp:779
double * get_u() const
Definition: df1b2fun.h:228
#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
int adptr_diff(void *x, void *y)
Definition: df1b2fn2.cpp:110
exitptr ad_exit
Definition: gradstrc.cpp:53
void read_pass1_eq_3(void)
Description not yet available.
Definition: df1b2fn4.cpp:365
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
double * get_u_dot() const
Definition: df1b2fun.h:229
double * ptr
Definition: df1b2fun.h:269
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
static int no_derivatives
Definition: df1b2fun.h:759
fixed_smartlist2 nlist2
Definition: df1b2fun.h:756
int write_pass1_eq(const df1b2variable *px, df1b2variable *pz)
Description not yet available.
Definition: df1b2fn4.cpp:100
Description not yet available.
Definition: df1b2fun.h:527
int allocated(void)
Definition: df1b2fun.h:271
double * get_u_dot_bar_tilde() const
Definition: df1b2fun.h:235
void initialize(void)
Initialize.
Definition: df1b2fn2.cpp:129
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
short int * ncopies
Definition: df1b2fun.h:304
double * u
Definition: df1b2fun.h:195
int write_save_pass2_tilde_values(const df1b2variable *px)
Description not yet available.
Definition: df1b2fn7.cpp:32
int numbytes
Definition: df1b2fun.h:594
void saveposition(void)
Definition: df1b2fun.h:552
void read_pass1_eq_1(void)
Description not yet available.
Definition: df1b2fn4.cpp:162
test_smartlist list2
Definition: df1b2fun.h:755
double * u_dot
Definition: df1b2fun.h:196