ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2prc.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 #include <cassert>
14 #include <climits>
15 
16 /*
17 //int debugcounter=0;
18  df1b2variable operator / (double x,const df1b2variable& y)
19  {
20  return x*inv(y);
21  }
22 */
23 
29  {
30  return x*(1.0/y);
31  }
32 
38  {
40  df1b2variable z;
41  double * yd=y.get_u_dot();
42  double * zd=z.get_u_dot();
43  double yu=*y.get_u();
44 
45  *z.get_u()=x*yu;
46 
47  for (unsigned int i=0;i<df1b2variable::nvar;i++)
48  {
49  *zd++ = x * *yd++;
50  }
51 
52  // WRITE WHATEVER ON TAPE
55  return z;
56  }
57 
58 
59 void ad_read_pass2_prodc1(void);
60 
66  df1b2variable * pz)
67  {
69  ncount++;
70 #if defined(CHECK_COUNT)
71  if (ncount >= ncount_check)
72  cout << ncount << endl;
73 #endif
74  size_t nvar=df1b2variable::nvar;
75  constexpr size_t sizeofdouble = sizeof(double);
76 
77  //int total_bytes=3*sizeof(df1b2_header)+sizeof(char*)
78  // +2*(nvar+1)*sizeof(double);
79  size_t total_bytes=2*sizeof(df1b2_header)
80  +(nvar+2)*sizeofdouble;
81 // string identifier debug stuff
82 #if defined(SAFE_ALL)
83  char ids[]="DL";
84  size_t slen=strlen(ids);
85  total_bytes+=slen;
86 #endif
87 
88  list.check_buffer_size(total_bytes);
89  void * tmpptr=list.bptr;
90 #if defined(SAFE_ALL)
91  memcpy(list,ids,slen);
92 #endif
93 // end of string identifier debug stuff
94 
95  memcpy(list,&x,sizeofdouble);
96  memcpy(list,(df1b2_header*)(py),sizeof(df1b2_header));
97  memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
98  memcpy(list,py->get_u(),sizeofdouble);
99  memcpy(list,py->get_u_dot(),nvar*sizeofdouble);
100  // ***** write record size
102  if ((int)total_bytes != nlist.bptr->numbytes)
103  {
104  cerr << "error in byte calculation in "
105  " write_pass1_prod" << endl;
106  ad_exit(1);
107  }
109  ++nlist;
110  return 0;
111  }
112 
113 
114 void read_pass2_1_prodc1(void);
115 void read_pass2_2_prodc1(void);
116 void read_pass2_3_prodc1(void);
117 
123 {
125  {
126  case 1:
128  break;
129  case 2:
131  break;
132  case 3:
134  break;
135  default:
136  cerr << "illegal value for df1b2variable::pass = "
138  ad_exit(1);
139  }
140 }
141 
147 {
148  // vmon_begin();
149  // We are going backword for bptr and nbptr
150  // and forward for bptr2 and nbptr2
151  // the current entry+2 in bptr is the size of the record i.e
152  // points to the next record
153  //char * bptr=f1b2gradlist->bptr;
154  //char * bptr2=f1b2gradlist2->bptr;
155  size_t nvar = df1b2variable::nvar;
157  //f1b2gradlist->nlist-=sizeof(int);
158  int num_bytes=f1b2gradlist->nlist.bptr->numbytes;
159  list-=num_bytes;
160  list.saveposition(); // save pointer to beginning of record;
161  double yu;
162 
163  // get info from tape1
164 #if defined(SAFE_ALL)
166 #endif
167  char * bptr=f1b2gradlist->list.bptr;
168  double xu=*(double *) bptr;
169  bptr+=sizeof(double);
170  df1b2_header * py=(df1b2_header *) bptr;
171  bptr+=sizeof(df1b2_header);
172  df1b2_header * pz=(df1b2_header *) bptr;
173  bptr+=sizeof(df1b2_header);
174  memcpy(&yu,bptr,sizeof(double));
175  //bptr+=sizeof(double); double* ydot=(double*)bptr;
176 
177  list.restoreposition(); // save pointer to beginning of record;
178 
179  // ****************************************************************
180  // turn this off if no third derivatives are calculated
181  // if (!no_third_derivatives)
182  // {
183  // save for second reverse pass
184  // save identifier 1
185  test_smartlist & list2 = f1b2gradlist->list2;
186 
187  size_t total_bytes=2*nvar*sizeof(double);
188 // string identifier debug stuff
189 #if defined(SAFE_ALL)
190  char ids[]="QK";
191  size_t slen=strlen(ids);
192  total_bytes+=slen;
193 #endif
194 
195  list2.check_buffer_size(total_bytes);
196  void * tmpptr=list2.bptr;
197 #if defined(SAFE_ALL)
198  memcpy(list2,ids,slen);
199 #endif
200 
201  constexpr size_t sizeofdouble = sizeof(double);
203  /*
204  if (debugcounter++>569)
205  {
206  cout << debugcounter++ << endl;
207  memcpy(list2,pz->get_u_bar(),2*nvar*sizeof(double));
208  memcpy(list2,pz->get_u_bar(),nvar*sizeof(double));
209  memcpy(list2,pz->get_u_dot_bar(),nvar*sizeof(double));
210  }
211  else
212  */
213  {
214  memcpy(list2,pz->get_u_bar(),nvar*sizeofdouble);
215  memcpy(list2,pz->get_u_dot_bar(),nvar*sizeofdouble);
216  }
217  *nlist2.bptr=adptr_diff(list2.bptr,tmpptr);
218  ++nlist2;
219 
220  // Do first reverse pass calculations
221  double* py_u_bar = py->u_bar;
222  double* pz_u_bar = pz->u_bar;
223  for (size_t i = 0; i < nvar; ++i)
224  {
225  *py_u_bar += xu * *pz_u_bar;
226  ++pz_u_bar;
227  ++py_u_bar;
228  }
229 
230  double* py_u_dot_bar = py->u_dot_bar;
231  double* pz_u_dot_bar = pz->u_dot_bar;
232  for (size_t i = 0; i < nvar; ++i)
233  {
234  *py_u_dot_bar += xu * *pz_u_dot_bar;
235  ++pz_u_dot_bar;
236  ++py_u_dot_bar;
237  }
238 
239  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
240  /*
241  for (size_t i=0;i<nvar;i++)
242  {
243  pz->u_bar[i]=0;
244  }
245  for (size_t i=0;i<nvar;i++)
246  {
247  pz->u_dot_bar[i]=0;
248  }
249  */
250  memset(pz->u_bar, 0, nvar * sizeofdouble);
251  memset(pz->u_dot_bar, 0, nvar * sizeofdouble);
252 }
253 
259 {
260  //const int nlist_record_size=sizeof(int)+sizeof(char*);
261  // We are going forward for bptr and backword for bptr2
262  //
263  // list 1
264  //
265  unsigned int nvar=df1b2variable::nvar;
267 
268  size_t total_bytes=2*sizeof(df1b2_header)
269  +(nvar+2)*sizeof(double);
270 // string identifier debug stuff
271 #if defined(SAFE_ALL)
272  char ids[]="DL";
273  size_t slen=strlen(ids);
274  total_bytes+=slen;
275 #endif
276 
277  list.check_buffer_size(total_bytes);
278 // end of string identifier debug stuff
279 
280  list.saveposition(); // save pointer to beginning of record;
282  // nlist-=sizeof(int);
283  // get record size
284  int num_bytes=nlist.bptr->numbytes;
285  //
286  // list 2
287  //
290  // get record size
291  int num_bytes2=*nlist2.bptr;
292  --nlist2;
293  // backup the size of the record
294  list2-=num_bytes2;
295  list2.saveposition(); // save pointer to beginning of record;
296  // save the pointer to the beginning of the record
297  // bptr and bptr2 now both point to the beginning of their records
298 
299  double yu;
300  //df1b2_header x,z;
301  //df1b2function2 * pf;
302 
303  // get info from tape1
304  // get info from tape1
305 #if defined(SAFE_ALL)
306  checkidentiferstring("DL",list);
307  checkidentiferstring("QK",list2);
308 #endif
309  double xu=*(double *) list.bptr;
310  list.bptr+=sizeof(double);
311  df1b2_header * py=(df1b2_header *) list.bptr;
312  list.bptr+=sizeof(df1b2_header);
313  df1b2_header * pz=(df1b2_header *) list.bptr;
314  list.bptr+=sizeof(df1b2_header);
315  //pf=*(df1b2function2 **) list.bptr;
316  //list.bptr+=sizeof(char*);
317  memcpy(&yu,list.bptr,sizeof(double));
318  list.bptr+=sizeof(double);
319 
320  //double* ydot=(double*)list.bptr;
321  list.restoreposition(num_bytes); // save pointer to beginning of record;
322 
323  //double* zbar=(double*)list2.bptr;
324  //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
325 
326  list2.restoreposition(); // save pointer to beginning of record;
327 
328  //double * y_tilde=py->get_u_tilde();
329  //double * y_dot_tilde=py->get_u_dot_tilde();
330  double * y_bar_tilde=py->get_u_bar_tilde();
331  double * y_dot_bar_tilde=py->get_u_dot_bar_tilde();
332  double * z_bar_tilde=pz->get_u_bar_tilde();
333  double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
334  // Do second "reverse-reverse" pass calculations
335 
336  /*
337  for (size_t i=0;i<nvar;i++)
338  {
339  z_bar_tilde[i]=0;
340  z_dot_bar_tilde[i]=0;
341  }
342  */
343  constexpr size_t sizeofdouble = sizeof(double);
344  memset(z_bar_tilde, 0, nvar * sizeofdouble);
345  memset(z_dot_bar_tilde, 0, nvar * sizeofdouble);
346 
347  // start with y and add x
348  for (size_t i=0;i<nvar;i++)
349  {
350  *z_bar_tilde += xu * *y_bar_tilde;
351  ++y_bar_tilde;
352  ++z_bar_tilde;
353  }
354  for (size_t i=0;i<nvar;i++)
355  {
356  *z_dot_bar_tilde += xu * *y_dot_bar_tilde;
357  ++y_dot_bar_tilde;
358  ++z_dot_bar_tilde;
359  }
360 }
361 
367 {
368  // We are going backword for bptr and forward for bptr2
369  // the current entry+2 in bptr is the size of the record i.e
370  // points to the next record
371  unsigned int nvar=df1b2variable::nvar;
374  // nlist-=sizeof(int);
375  // get record size
376  int num_bytes=nlist.bptr->numbytes;
377  // backup the size of the record
378  list-=num_bytes;
379  list.saveposition(); // save pointer to beginning of record;
380  // save the pointer to the beginning of the record
381  double yu;
382  //df1b2_header z;
383  //df1b2function2 * pf;
384 
385  // get info from tape1
386  // get info from tape1
387 #if defined(SAFE_ALL)
388  checkidentiferstring("DL",list);
389 #endif
390  double xu=*(double*) list.bptr;
391  list.bptr+=sizeof(double);
392  df1b2_header * py=(df1b2_header *) list.bptr;
393  list.bptr+=sizeof(df1b2_header);
394  df1b2_header * pz=(df1b2_header *) list.bptr;
395  list.bptr+=sizeof(df1b2_header);
396  memcpy(&yu,list.bptr,sizeof(double));
397  list.bptr+=sizeof(double);
398 
399  //double * ydot = (double*)list.bptr;
400 
401  list.restoreposition(); // save pointer to beginning of record;
402 
403  *(py->u_tilde)+=xu* *(pz->u_tilde);
404  double* py_u_dot_tilde = py->u_dot_tilde;
405  double* pz_u_dot_tilde = pz->u_dot_tilde;
406  for (size_t i=0;i<nvar;i++)
407  {
408  *py_u_dot_tilde += xu * *pz_u_dot_tilde;
409  ++pz_u_dot_tilde;
410  ++py_u_dot_tilde;
411  }
412  *(pz->u_tilde)=0;
413  /*
414  for (size_t i=0;i<nvar;i++)
415  {
416  pz->u_dot_tilde[i]=0;
417  }
418  */
419  constexpr size_t sizeofdouble = sizeof(double);
420  memset(pz->u_dot_tilde, 0, nvar * sizeofdouble);
421 }
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
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
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
void read_pass2_2_prodc1(void)
Description not yet available.
Definition: df1b2prc.cpp:258
dmatrix operator*(const d3_array &t, const dvector &v)
Description not yet available.
Definition: d3arr12.cpp:17
fixed_smartlist nlist
Definition: df1b2fun.h:754
double * get_u_dot() const
Definition: df1b2fun.h:229
int write_pass1_prod(double x, const df1b2variable *py, df1b2variable *pz)
Description not yet available.
Definition: df1b2prc.cpp:65
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
ADrfptr pf
Definition: df1b2fun.h:595
static int no_derivatives
Definition: df1b2fun.h:759
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
void ad_read_pass2_prodc1(void)
Description not yet available.
Definition: df1b2prc.cpp:122
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
double * u_dot_tilde
Definition: df1b2fun.h:200
Base class for df1b2variable.
Definition: df1b2fun.h:191
void read_pass2_3_prodc1(void)
Description not yet available.
Definition: df1b2prc.cpp:366
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
d3_array operator/(const d3_array &m, const double d)
Author: David Fournier.
Definition: d3arr2b.cpp:14
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
void read_pass2_1_prodc1(void)
Description not yet available.
Definition: df1b2prc.cpp:146