ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2fn5.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_init_1(void);
13 void read_pass1_init_2(void);
14 void read_pass1_init_3(void);
15 
16 //#define ADDEBUG_PRINT
17 #if defined(ADDEBUG_PRINT)
18  extern int addebug_count;
19 #endif
20 
21 #ifdef DEBUG
22  #include <cassert>
23 #endif
24 
25 #ifndef OPT_LIB
26  #include <climits>
27 #endif
28 
29 void ad_read_pass1_initialize(void);
30 
37 {
38  initialize();
39  //ptr[blocksize-1]=8219.4;
40  get_ind_index()=-1;
41  *u=d;
42  //double * zd=get_u_dot();
43  // !!! nov 1 02
48 }
49 
55 {
56  // vectors are all 1,...,nvar
57  ptr = (double*) pool->alloc();
58  //AD_ALLOCATE(ptr,double,blocksize,df1b2variable)
59  initialize();
60  double * block=ptr;
61  //block[blocksize-1]=8219.4;
62  get_ind_index()=-1;
63  ncopies=&(((twointsandptr*)block)->ncopies);
64  *ncopies=0;
65  u=block+=2;
66  u_dot=block+=1; // u has size 1
67  u_bar=block+=nvar; // u_dot has size nvar
68  u_dot_bar=block+=nvar; // u_bar has size nvar
69  u_tilde=block+=nvar; // u_dot_bar has size nvar
70  u_dot_tilde=block+=1; // u_tilde has size 1
71  u_bar_tilde=block+=nvar; // u_dot_tilde has size nvar
72  u_dot_bar_tilde=block+=nvar; // u_bar_tilde has size nvar
73  *u=d;
76 }
77 
82 {
84  {
85  allocate();
86  }
87  else
88  {
89  ptr=0;
90  ncopies=0;
91  u=0;
92  u_dot=0;
93  u_bar=0;
94  u_dot_bar=0;
95  u_tilde=0;
96  u_dot_tilde=0;
97  u_bar_tilde=0;
99  }
100 }
101 
106 df1b2variable::df1b2variable(const newadkludge* z)
107 {
108  ptr=0;
109  ncopies=0;
110 #if defined(SAFE_ALL)
111  u=0;
112  u_dot=0;
113  u_bar=0;
114  u_dot_bar=0;
115  u_tilde=0;
116  u_dot_tilde=0;
117  u_bar_tilde=0;
118  u_dot_bar_tilde=0;
119 #endif
120 }
121 
127 {
128 #ifdef DEBUG
129  assert(pool);
130 #endif
131  // vectors are all 1,...,nvar
132  ptr = (double*) pool->alloc();
133  //cout << *(int*)ptr << endl;
134  //AD_ALLOCATE(ptr,double,blocksize,df1b2variable)
135  initialize();
136  double * block=ptr;
137  //block[blocksize-1]=8219.4;
138  get_ind_index()=-1;
139  ncopies=&(((twointsandptr*)block)->ncopies);
140  *ncopies=0;
141  u=block+=2;
142  u_dot=block+=1; // u has size 1
143  u_bar=block+=nvar; // u_dot has size nvar
144  u_dot_bar=block+=nvar; // u_bar has size nvar
145  u_tilde=block+=nvar; // u_dot_bar has size nvar
146  u_dot_tilde=block+=1; // u_tilde has size 1
147  u_bar_tilde=block+=nvar; // u_dot_tilde has size nvar
148  u_dot_bar_tilde=block+=nvar; // u_bar_tilde has size nvar
151 }
152 
158 {
159  // vectors are all 1,...,nvar
160  ptr = (double*) pool->alloc();
161  //AD_ALLOCATE(ptr,double,blocksize,df1b2variable)
162  initialize();
163  double * block=ptr;
164  block[blocksize-1]=8219.4;
165  get_ind_index()=-1;
166  ncopies=&(((twointsandptr*)block)->ncopies);
167  *ncopies=0;
168  u=block+=2;
169  u_dot=block+=1; // u has size 1
170  u_bar=block+=nvar; // u_dot has size nvar
171  u_dot_bar=block+=nvar; // u_bar has size nvar
172  u_tilde=block+=nvar; // u_dot_bar has size nvar
173  u_dot_tilde=block+=1; // u_tilde has size 1
174  u_bar_tilde=block+=nvar; // u_dot_tilde has size nvar
175  u_dot_bar_tilde=block+=nvar; // u_bar_tilde has size nvar
176  //if (!df1b2_gradlist::no_derivatives)
177  // f1b2gradlist->write_pass1_initialize(this);
178 }
179 
180 //int df1b2_gradlist::write_pass1_initialize(const df1b2variable * px,
181 // df1b2variable * pz)
182 
188 {
189  char * pg=0;
190  ncount++;
191 
192 #if defined(CHECK_COUNT)
193  if (ncount >= ncount_check)
194  ncount_checker(ncount,ncount_check);
195 #endif
196 
197  //int nvar=df1b2variable::nvar;
198  size_t total_bytes=sizeof(df1b2_header)+sizeof(char *);
199 
200 // string identifier debug stuff
201 #if defined(SAFE_ALL)
202  char ids[]="AZ";
203  size_t slen=strlen(ids);
204  total_bytes+=slen;
205 #endif
206 
207  list.check_buffer_size(total_bytes);
208  void * tmpptr=list.bptr;
209 
210 #if defined(SAFE_ALL)
211  memcpy(list,ids,slen);
212 #endif
213 // end of string identifier debug stuff
214 
215  memcpy(list,(df1b2_header*)(pz),sizeof(df1b2_header));
216  //list.bptr+=sizeof(df1b2_header);
217  // ***************** don't need this ***************
218  memcpy(list,&pg,sizeof(char *));
219  //*(char**)(list.bptr)=(char*)pg;
220  //list.bptr+=sizeof(char*);
221  // ***** write record size
224  ++nlist;
225  return 0;
226 }
227 
233 {
235  {
236  case 1:
238  break;
239  case 2:
241  break;
242  case 3:
244  break;
245  default:
246  cerr << "illegal value for df1b2variable::pass = "
248  ad_exit(1);
249  }
250 }
251 
256 void checkidentiferstring(const char * ids,test_smartlist& list)
257 {
258  size_t slen=strlen(ids);
259  char * s=(char*)list.bptr;
260  int ss=strncmp(ids,s,slen);
261  if (ss)
262  {
263  cerr << "Error in string identifer in list value should be " << ids
264  << endl << " it appears to be " << s[0] << s[1] << s[2] << s[3]
265  << " ... " << endl;
266  ad_exit(1);
267  }
268  list.bptr+=slen;
269 }
270 
276 {
277  // We are going backword for bptr and forward for bptr2
278  // the current entry+2 in bptr is the size of the record i.e
279  // points to the next record
280  size_t nvar = df1b2variable::nvar;
281 
284  // nlist-=sizeof(int);
285  // get record size
286  int num_bytes=nlist.bptr->numbytes;
287  // backup the size of the record
288  list-=num_bytes;
289  list.saveposition(); // save pointer to beginning of record;
290  // save the pointer to the beginning of the record
291  //df1b2_header z;
292 
293  // get info from tape1
294 #if defined(SAFE_ALL)
295  checkidentiferstring("AZ",list);
296 #endif
297  df1b2_header * pz=(df1b2_header *) list.bptr;
298  list.bptr+=sizeof(df1b2_header);
299  //df1b2function1* pf = *(df1b2function1**)list.bptr;
300  list.bptr+=sizeof(char*);
301  list.restoreposition(); // save pointer to beginning of record;
302 
303  // Do first reverse paSS calculations
304  // ****************************************************************
305  // turn this off if no third derivatives are calculated
306  // if (!no_third_derivatives)
307  // {
308  // save for second reverse pass
309  // save identifier 1
312 
313  size_t total_bytes=2*nvar*sizeof(double);
314 
315 // string identifier debug stuff
316 #if defined(SAFE_ALL)
317  char ids[]="EF";
318  size_t slen=strlen(ids);
319  total_bytes+=slen;
320 #endif
321 
322  list2.check_buffer_size(total_bytes);
323 
324  void * tmpptr2=list2.bptr;
325 #if defined(SAFE_ALL)
326  memcpy(list2,ids,slen);
327 #endif
328 
329  size_t sizeofdouble = sizeof(double);
330  memcpy(list2,pz->get_u_bar(),nvar*sizeofdouble);
331  //list2.bptr+=nvar*sizeof(double);
332  memcpy(list2,pz->get_u_dot_bar(),nvar*sizeofdouble);
333  //list2.bptr+=nvar*sizeof(double);
334  *nlist2.bptr=adptr_diff(list2.bptr,tmpptr2);
335  ++nlist2;
336  // }
337  //
338  // ****************************************************************
339 
340  // This should *NEVER* be used (I think)
341  /*
342  for (i=0;i<nvar;i++)
343  {
344  pz->u_bar[i]=0;
345  }
346  for (i=0;i<nvar;i++)
347  {
348  pz->u_dot_bar[i]=0;
349  }
350  */
351 }
352 
358 {
359  //const int nlist_record_size=sizeof(int)+sizeof(char*);
360  // We are going forward for bptr and backword for bptr2
361  //
362  // list 1
363  //
364  unsigned int nvar = df1b2variable::nvar;
366 
367  size_t total_bytes=sizeof(df1b2_header)+sizeof(char *);
368 // string identifier debug stuff
369 #if defined(SAFE_ALL)
370  char ids[]="AZ";
371  int slen=strlen(ids);
372  total_bytes+=slen;
373 #endif
374 // end of string identifier debug stuff
375  list.check_buffer_size(total_bytes);
376 
377  list.saveposition(); // save pointer to beginning of record;
379  // nlist-=sizeof(int);
380  // get record size
381  int num_bytes=nlist.bptr->numbytes;
382  // nlist+=nlist_record_size;
383  //
384  // list 2
385  //
388  // get record size
389  int num_bytes2=*nlist2.bptr;
390  --nlist2;
391  // backup the size of the record
392  list2-=num_bytes2;
393  // save the pointer to the beginning of the record
394  // bptr and bptr2 now both point to the beginning of their records
395 
396  //df1b2_header z;
397 
398  // get info from tape1
399  list2.saveposition(); // save pointer to beginning of record;
400 #if defined(SAFE_ALL)
401  checkidentiferstring("AZ",list);
402  checkidentiferstring("EF",list2);
403 #endif
404  df1b2_header * pz=(df1b2_header *) list.bptr;
405  list.bptr+=sizeof(df1b2_header);
406  //df1b2function1 * pf = *(df1b2function1**)list.bptr;
407  list.bptr+=sizeof(char*);
408  list.restoreposition(num_bytes); // save pointer to beginning of record;
409 
410  //double* zbar=(double*)list2.bptr;
411  //double* zdotbar=(double*)(list2.bptr+nvar*sizeof(double));
412 
413  list2.restoreposition(); // save pointer to beginning of record;
414 
415  double * z_bar_tilde=pz->get_u_bar_tilde();
416  double * z_dot_bar_tilde=pz->get_u_dot_bar_tilde();
417  // Do second "reverse-reverse" pass calculations
418 
419  // !!!! oct31 02
420 
421  *pz->get_u_tilde()=0.0;
422  for (size_t i=0;i<nvar;i++)
423  {
424  z_bar_tilde[i]=0;
425  z_dot_bar_tilde[i]=0;
426  }
427 }
428 
434 {
435  // We are going backword for bptr and forward for bptr2
436  // the current entry+2 in bptr is the size of the record i.e
437  // points to the next record
438  unsigned int nvar = df1b2variable::nvar;
441  // nlist-=sizeof(int);
442  // get record size
443  int num_bytes=nlist.bptr->numbytes;
444  // backup the size of the record
445  list-=num_bytes;
446  list.saveposition(); // save pointer to beginning of record;
447  // save the pointer to the beginning of the record
448  //df1b2_header z;
449  // df1b2function1 * pf;
450 
451  // get info from tape1
452 #if defined(SAFE_ALL)
453  checkidentiferstring("AZ",list);
454 #endif
455  df1b2_header * pz=(df1b2_header *) list.bptr;
456  list.restoreposition(); // save pointer to beginning of record;
457 
458  *(pz->u_tilde)=0;
459  for (size_t i=0;i<nvar;i++)
460  {
461  pz->u_dot_tilde[i]=0;
462  }
463 }
char * bptr
Definition: df1b2fun.h:543
double * get_u_bar_tilde() const
Definition: df1b2fun.h:234
static adpool * pool
Definition: df1b2fun.h:273
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 allocate(void)
Description not yet available.
Definition: df1b2fn5.cpp:126
double * u_bar_tilde
Definition: df1b2fun.h:201
double * u_bar
Definition: df1b2fun.h:197
df1b2variable & operator=(const df3_one_variable &v)
Definition: df3fun.cpp:779
void ad_read_pass1_initialize(void)
Description not yet available.
Definition: df1b2fn5.cpp:232
static int passnumber
Definition: df1b2fun.h:289
static int noallocate
Definition: df1b2fun.h:294
void read_pass1_init_2(void)
Description not yet available.
Definition: df1b2fn5.cpp:357
int adptr_diff(void *x, void *y)
Definition: df1b2fn2.cpp:110
exitptr ad_exit
Definition: gradstrc.cpp:53
void ncount_checker(int ncount, int ncount_check)
double * u_tilde
Definition: df1b2fun.h:199
adkludge1 * adkludgexxx
Definition: df1b2fn2.cpp:238
void read_pass1_init_3(void)
Description not yet available.
Definition: df1b2fn5.cpp:433
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 * 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
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
void * alloc(void)
Allocate memory for link*.
Definition: adpool.cpp:114
static unsigned int blocksize
Definition: df1b2fun.h:293
double * get_u_dot_bar() const
Definition: df1b2fun.h:231
void read_pass1_init_1(void)
Description not yet available.
Definition: df1b2fn5.cpp:275
double * get_u_tilde() const
Definition: df1b2fun.h:232
int write_pass1_initialize(df1b2variable *pz)
Description not yet available.
Definition: df1b2fn5.cpp:187
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
Description not yet available.
Definition: df1b2fun.h:79
double * u_dot_tilde
Definition: df1b2fun.h:200
Base class for df1b2variable.
Definition: df1b2fun.h:191
int & get_ind_index()
Definition: df1b2fun.h:236
Description not yet available.
df1b2variable(void)
Default constructor.
Definition: df1b2fn5.cpp:81
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
double * get_u_bar() const
Definition: df1b2fun.h:230
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
test_smartlist list2
Definition: df1b2fun.h:755
double * u_dot
Definition: df1b2fun.h:196