ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2prb.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 #ifndef OPT_LIB
14  #include <cassert>
15  #include <climits>
16 #endif
17 
23  {
25  df1b2variable z;
26  double * xd=x.get_u_dot();
27  double * zd=z.get_u_dot();
28  double xu=*x.get_u();
29 
30  *z.get_u()=y*xu;
31 
32  for (unsigned int i=0;i<df1b2variable::nvar;i++)
33  {
34  *zd++ = y * *xd++;
35  }
36 
37  // WRITE WHATEVER ON TAPE
40  return z;
41  }
42 
43 void ad_read_pass2_prodc2(void);
44 
50  df1b2variable * pz)
51  {
53  ncount++;
54 #if defined(CHECK_COUNT)
55  if (ncount >= ncount_check)
56  cout << ncount << endl;
57 #endif
58  size_t nvar=df1b2variable::nvar;
59 
60  //int total_bytes=3*sizeof(df1b2_header)+sizeof(char*)
61  // +2*(nvar+1)*sizeof(double);
62  size_t total_bytes=2*sizeof(df1b2_header)
63  +(nvar+2)*sizeof(double);
64 // string identifier debug stuff
65 #if defined(SAFE_ALL)
66  char ids[]="DL";
67  size_t slen=strlen(ids);
68  total_bytes+=slen;
69 #endif
70 
71  list.check_buffer_size(total_bytes);
72  void * tmpptr=list.bptr;
73 #if defined(SAFE_ALL)
74  memcpy(list,ids,slen);
75 #endif
76 // end of string identifier debug stuff
77 
78  memcpy(list,(df1b2_header*)(px),sizeof(df1b2_header));
79  memcpy(list,&y,sizeof(double));
80  memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
81  size_t sizeofdouble = sizeof(double);
82  memcpy(list,px->get_u(),sizeofdouble);
83  memcpy(list,px->get_u_dot(),nvar*sizeofdouble);
84  // ***** write record size
86  if ((int)total_bytes != nlist.bptr->numbytes)
87  {
88  cerr << "error in byte calculation in "
89  " write_pass1_prod" << endl;
90  ad_exit(1);
91  }
93  ++nlist;
94  return 0;
95  }
96 
97 
98 void read_pass2_1_prodc2(void);
99 void read_pass2_2_prodc2(void);
100 void read_pass2_3_prodc2(void);
101 
107 {
109  {
110  case 1:
112  break;
113  case 2:
115  break;
116  case 3:
118  break;
119  default:
120  cerr << "illegal value for df1b2variable::pass = "
122  ad_exit(1);
123  }
124 }
125 
131 {
132  // We are going backword for bptr and nbptr
133  // and forward for bptr2 and nbptr2
134  // the current entry+2 in bptr is the size of the record i.e
135  // points to the next record
136  //char * bptr=f1b2gradlist->bptr;
137  //char * bptr2=f1b2gradlist2->bptr;
138  size_t nvar=df1b2variable::nvar;
140  //f1b2gradlist->nlist-=sizeof(int);
141  int num_bytes=f1b2gradlist->nlist.bptr->numbytes;
142  list-=num_bytes;
143  list.saveposition(); // save pointer to beginning of record;
144  double xu;
145 
146  // get info from tape1
147 #if defined(SAFE_ALL)
149 #endif
150  size_t sizeofdouble = sizeof(double);
151  char * bptr=f1b2gradlist->list.bptr;
152  df1b2_header * px=(df1b2_header *) bptr;
153  bptr+=sizeof(df1b2_header);
154  double yu=*(double *) bptr;
155  bptr+=sizeofdouble;
156  df1b2_header * pz=(df1b2_header *) bptr;
157  bptr+=sizeof(df1b2_header);
158  memcpy(&xu,bptr,sizeofdouble);
159  //bptr+=sizeof(double); double* xdot=(double*)bptr;
160 
161  list.restoreposition(); // save pointer to beginning of record;
162 
163  // ****************************************************************
164  // turn this off if no third derivatives are calculated
165  // if (!no_third_derivatives)
166  // {
167  // save for second reverse pass
168  // save identifier 1
169  test_smartlist & list2 = f1b2gradlist->list2;
170 
171  size_t total_bytes=2*nvar*sizeof(double);
172 // string identifier debug stuff
173 #if defined(SAFE_ALL)
174  char ids[]="QK";
175  size_t slen=strlen(ids);
176  total_bytes+=slen;
177 #endif
178 
179  list2.check_buffer_size(total_bytes);
180  void * tmpptr=list2.bptr;
181 #if defined(SAFE_ALL)
182  memcpy(list2,ids,slen);
183 #endif
184 
186  memcpy(list2,pz->get_u_bar(),nvar*sizeofdouble);
187  memcpy(list2,pz->get_u_dot_bar(),nvar*sizeofdouble);
188  *nlist2.bptr=adptr_diff(list2.bptr,tmpptr);
189  ++nlist2;
190 
191  // Do first reverse pass calculations
192  double* px_u_bari = px->u_bar;
193  double* pz_u_bari = pz->u_bar;
194  for (size_t i=0;i<nvar;i++)
195  {
196  *px_u_bari += yu * *pz_u_bari;
197 
198  ++px_u_bari;
199  ++pz_u_bari;
200  }
201  double* px_u_dot_bari = px->u_dot_bar;
202  double* pz_u_dot_bari = pz->u_dot_bar;
203  for (size_t i=0;i<nvar;i++)
204  {
205  *px_u_dot_bari += yu * *pz_u_dot_bari;
206 
207  ++px_u_dot_bari;
208  ++pz_u_dot_bari;
209  }
210 
211  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
212  memset(pz->u_bar, 0, nvar * sizeofdouble);
213  memset(pz->u_dot_bar, 0, nvar * sizeofdouble);
214 }
215 
221 {
222  //const int nlist_record_size=sizeof(int)+sizeof(char*);
223  // We are going forward for bptr and backword for bptr2
224  //
225  // list 1
226  //
227  unsigned int nvar=df1b2variable::nvar;
229 
230  size_t total_bytes=2*sizeof(df1b2_header)
231  +(nvar+2)*sizeof(double);
232 // string identifier debug stuff
233 #if defined(SAFE_ALL)
234  char ids[]="DL";
235  size_t slen=strlen(ids);
236  total_bytes+=slen;
237 #endif
238 
239  list.check_buffer_size(total_bytes);
240 // end of string identifier debug stuff
241 
242  list.saveposition(); // save pointer to beginning of record;
244  // nlist-=sizeof(int);
245  // get record size
246  int num_bytes=nlist.bptr->numbytes;
247  //
248  // list 2
249  //
252  // get record size
253  int num_bytes2=*nlist2.bptr;
254  --nlist2;
255  // backup the size of the record
256  list2-=num_bytes2;
257  list2.saveposition(); // save pointer to beginning of record;
258  // save the pointer to the beginning of the record
259  // bptr and bptr2 now both point to the beginning of their records
260 
261  double xu;
262  //df1b2_header x,z;
263  //df1b2function2 * pf;
264 
265  // get info from tape1
266  // get info from tape1
267 #if defined(SAFE_ALL)
268  checkidentiferstring("DL",list);
269  checkidentiferstring("QK",list2);
270 #endif
271  df1b2_header * px=(df1b2_header *) list.bptr;
272  list.bptr+=sizeof(df1b2_header);
273  double yu=*(double *) list.bptr;
274  list.bptr+=sizeof(double);
275  df1b2_header * pz=(df1b2_header *) list.bptr;
276  list.bptr+=sizeof(df1b2_header);
277  memcpy(&xu,list.bptr,sizeof(double));
278  list.bptr+=sizeof(double);
279  //double* xdot=(double*)list.bptr;
280  list.restoreposition(num_bytes); // save pointer to beginning of record;
281 
282  //double* zbar=(double*)list2.bptr;
283  //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
284 
285  list2.restoreposition(); // save pointer to beginning of record;
286 
287  //double * x_tilde=px->get_u_tilde();
288  //double * x_dot_tilde=px->get_u_dot_tilde();
289  double * x_bar_tilde=px->get_u_bar_tilde();
290  double * x_dot_bar_tilde=px->get_u_dot_bar_tilde();
291  double * z_bar_tilde=pz->get_u_bar_tilde();
292  double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
293  // Do second "reverse-reverse" pass calculations
294 
295  for (size_t i=0;i<nvar;i++)
296  {
297  z_bar_tilde[i]=0;
298  z_dot_bar_tilde[i]=0;
299  }
300 
301  // start with y and add x
302  for (size_t i=0;i<nvar;i++)
303  {
304  z_bar_tilde[i]+=yu*x_bar_tilde[i];
305  }
306 
307  for (size_t i=0;i<nvar;i++)
308  {
309  z_dot_bar_tilde[i]+=yu*x_dot_bar_tilde[i];
310  }
311 }
312 
318 {
319  // We are going backword for bptr and forward for bptr2
320  // the current entry+2 in bptr is the size of the record i.e
321  // points to the next record
322  unsigned int nvar=df1b2variable::nvar;
325  // nlist-=sizeof(int);
326  // get record size
327  int num_bytes=nlist.bptr->numbytes;
328  // backup the size of the record
329  list-=num_bytes;
330  list.saveposition(); // save pointer to beginning of record;
331  // save the pointer to the beginning of the record
332  double xu;
333  //df1b2_header z;
334  //df1b2function2 * pf;
335 
336  // get info from tape1
337  // get info from tape1
338 #if defined(SAFE_ALL)
339  checkidentiferstring("DL",list);
340 #endif
341  df1b2_header * px=(df1b2_header *) list.bptr;
342  list.bptr+=sizeof(df1b2_header);
343  double yu=*(double*) list.bptr;
344  list.bptr+=sizeof(double);
345  df1b2_header * pz=(df1b2_header *) list.bptr;
346  list.bptr+=sizeof(df1b2_header);
347  memcpy(&xu,list.bptr,sizeof(double));
348  list.bptr+=sizeof(double);
349  //double* xdot=(double*)list.bptr;
350  list.restoreposition(); // save pointer to beginning of record;
351 
352  *(px->u_tilde)+=yu* *(pz->u_tilde);
353  for (size_t i=0;i<nvar;i++)
354  {
355  px->u_dot_tilde[i]+=yu*pz->u_dot_tilde[i];
356  }
357  *(pz->u_tilde)=0;
358  for (size_t i=0;i<nvar;i++)
359  {
360  pz->u_dot_tilde[i]=0;
361  }
362 }
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
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
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_2_prodc2(void)
Description not yet available.
Definition: df1b2prb.cpp:220
void read_pass2_3_prodc2(void)
Description not yet available.
Definition: df1b2prb.cpp:317
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 ad_read_pass2_prodc2(void)
Description not yet available.
Definition: df1b2prb.cpp:106
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_prodc2(void)
Description not yet available.
Definition: df1b2prb.cpp:130