ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jacob2.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 "fvar.hpp"
12 
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <string.h>
16 
17 #ifdef __TURBOC__
18  #pragma hdrstop
19  #include <iostream.h>
20 #endif
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #ifdef __SUN__
26  #include <iostream.h>
27  #include <sys/stat.h>
28  #include <sys/types.h>
29  #include <unistd.h>
30 #endif
31 
32 #if !defined(_MSC_VER)
33  #include <iostream>
34  #include <sys/stat.h>
35  #include <sys/types.h>
36  #include <unistd.h>
37 #endif
38 
39 #if defined(__NDPX__ )
40  extern "C" {
41  int LSEEK(int, int, int);
42  int read(int, char*, int);
43  };
44 #endif
45 
46 #include <math.h>
47 
48 #ifndef OPT_LIB
49  #include <cassert>
50 #endif
51 
56 void jacobcalc(int nvar, const ofstream& ofs)
57 {
59 }
60 
65 void gradient_structure::jacobcalc(int nvar, const ofstream& _ofs)
66 {
67  ADUNCONST(ofstream,ofs)
68  dvector jac(1,nvar);
69  OFF_T lpos;
70  int depvar_count=DEPVARS_INFO->depvar_count;
71 
72  int& _GRADFILE_PTR = GRAD_STACK1->_GRADFILE_PTR;
73  // check to see if anything has been written into the file
74  OFF_T last_gpos=LSEEK(_GRADFILE_PTR,0L,SEEK_CUR);
75 
76  //save current contents of the buffer so we can get them later
77  if (last_gpos)
78  {
80  }
81 
82  // check to see if anything has been written into the file
83  OFF_T last_cpos=LSEEK(fp->file_ptr,0L,SEEK_CUR);
84 
85  //save current contents of the buffer so we can get them later
86  if (last_cpos)
87  {
89  }
90 
91  // save variable values if desired
92  if (save_var_flag)
93  {
94  save_arrays();
96  }
97  // now evalueate the jacobian
98  for (int ijac=1;ijac<=depvar_count;ijac++)
99  {
100  dvector& g=jac;
101  //max_num_dependent_variables=ndv;
102  if (depvar_count>DEPVARS_INFO->max_num_dependent_variables)
103  {
104  cout << "maximum number of depdendent variables of "
105  << DEPVARS_INFO->max_num_dependent_variables << " exceeded "
106  << endl
107  << "use gradient_structure::set_NUM_DEPENDENT_VARIABLES(int i);"
108  << endl << "to increase the number of dependent variables"
109  << endl;
110  ad_exit(1);
111  }
112 
114  fp->toffset=fp->offset;
115  _GRADFILE_PTR=DEPVARS_INFO->grad_file_count(ijac);
117  lpos=DEPVARS_INFO->grad_file_position(ijac);
118  // position the cmpdif file correctly;
119  if (last_cpos)
120  {
121  OFF_T cmp_lpos=DEPVARS_INFO->cmpdif_file_position(ijac);
122 #ifndef OPT_LIB
123  OFF_T ret = LSEEK(fp->file_ptr,cmp_lpos,SEEK_SET);
124  assert(ret >= 0);
125 #else
126  LSEEK(fp->file_ptr,cmp_lpos,SEEK_SET);
127 #endif
128  fp->read_cmpdif_stack_buffer(cmp_lpos);
129  }
131 
132  if (last_gpos)
133  {
134  // just use the end of the buffer
136 
137  // check to sere if buffer was written into the beginning of
138  // the next file
140  && (lpos == GRAD_STACK1->end_pos1) && (lpos>0) )
141  {
142  // get the next file
144  lpos=0;
145  }
146  // and position the file to the begginig of the buffer image
147  LSEEK(_GRADFILE_PTR,lpos,SEEK_SET);
148  // now fill the buffer with the appropriate stuff
150  // now reposition the grad_buffer pointer
151  }
152  GRAD_STACK1->ptr=
154 
156  {
157 #ifdef DIAG
158  cerr << "warning -- calling gradcalc when no calculations generating"
159  << endl << "derivative information have occurred" << endl;
160 #endif
161  g.initialize();
162  return;
163  } // current is one past the end so -- it
164 
165  GRAD_STACK1->ptr--;
166 
168 
170 
171  unsigned long int max_last_offset = ARR_LIST1->get_max_last_offset();
172 
173  size_t size = sizeof(double_and_int );
174 
175  for (unsigned int i = 0; i < (max_last_offset/size); i++)
176  {
177  tmp->x = 0;
178 #if defined (__ZTC__)
179  tmp = (double_and_int*)_farptr_norm((void*)(++tmp));
180 #else
181  tmp++;
182 #endif
183  }
184 
185  *GRAD_STACK1->ptr->dep_addr = 1;
186  //double* zptr = GRAD_STACK1->ptr->dep_addr;
187 
188  int break_flag=1;
189 
190  do
191  {
192  GRAD_STACK1->ptr++;
193  #ifdef FAST_ASSEMBLER
194  gradloop();
195  #else
196  //int counter=0;
197  while (GRAD_STACK1->ptr-- > GRAD_STACK1->ptr_first)
198  {
199  //grad_stack_entry* grad_ptr =
200  //gradient_structure::get()->GRAD_STACK1->ptr;
201  {
202  (*GRAD_STACK1->ptr->func)();
203  }
204  }
205  #endif
206 
207  // back up the file one buffer size and read forward
208  OFF_T offset = (OFF_T)(sizeof(grad_stack_entry)
209  *GRAD_STACK1->length);
211  -offset, SEEK_CUR);
212 
213  break_flag = GRAD_STACK1->read_grad_stack_buffer(lpos);
214  } while (break_flag); // do
215 
216  double* pgj = g.get_v() + g.indexmin();
217  for (int i = 0; i < nvar; ++i)
218  {
220  ++pgj;
221  }
222 
224  //ofs << setprecision(10) << g << endl;
225  ofs.precision(10);
226  ofs << g << endl;
227  }// loop over dep vars
230  {
231  restore_arrays();
233  }
234 }
indvar_offset_list * INDVAR_LIST
void read(const test_smartlist &, void *, int nsize)
void set_gbuffer_pointers(void)
Description not yet available.
Definition: gradstak.cpp:551
double * get_address(const int &i)
Definition: fvar.hpp:1292
void jacobcalc(int nvar, const dmatrix &jac)
Description not yet available.
Definition: jacobclc.cpp:75
dependent_variables_information * DEPVARS_INFO
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
void jacobcalc(int nvar, const dmatrix &g)
Description not yet available.
Definition: jacobclc.cpp:66
OFF_T end_pos1
Definition: fvar.hpp:953
Holds derivative information for arithmetic operators and math library functions. ...
Definition: fvar.hpp:894
void write_grad_stack_buffer()
Definition: fvar.hpp:976
exitptr ad_exit
Definition: gradstrc.cpp:53
void initialize()
Definition: ddlist.cpp:129
int _GRADFILE_PTR1
Definition: fvar.hpp:943
#define LSEEK
Definition: fvar.hpp:75
Holds the data for the prevariable class.
Definition: fvar.hpp:191
void write_cmpdif_stack_buffer()
Description not yet available.
Definition: df_file.cpp:350
void read_cmpdif_stack_buffer(OFF_T &lpos)
Description not yet available.
Definition: df_file.cpp:328
humungous_pointer ARRAY_MEMBLOCK_BASE
Definition: fvar.hpp:2074
prnstream & endl(prnstream &)
unsigned long int max_last_offset
unsigned long int get_max_last_offset()
Definition: fvar.hpp:2103
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
static _THREAD gradient_structure * _instance
#define OFF_T
Definition: fvar.hpp:74
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
grad_stack_entry * ptr_first
Definition: fvar.hpp:924
static _THREAD DF_FILE * fp
void save_variables()
Save variables to a buffer.
Definition: sgradclc.cpp:353
int & gradfile_handle()
Definition: gradstak.cpp:534
grad_stack_entry * ptr
Definition: fvar.hpp:934
int read_grad_stack_buffer(OFF_T &lpos)
Description not yet available.
Definition: gst_read.cpp:76
void restore_variables()
Restore variables from buffer.
Definition: sgradclc.cpp:360
size_t length
Definition: fvar.hpp:930
void save_arrays()
Compute the gradient from the data stored in the global gradient_structure.
Definition: sgradclc.cpp:223
int _GRADFILE_PTR
Definition: fvar.hpp:942
static _THREAD grad_stack * GRAD_STACK1
void(* func)(void)
&lt; Pointer to function (if any) to be used for derivative calculation
Definition: fvar.hpp:898
double *& get_v(void)
Definition: dvector.h:148
double * dep_addr
Pointer to dependent variable.
Definition: fvar.hpp:899
void increment_current_gradfile_ptr(void)
If there is another file set the handle to point to it otherwise we are out of room.
Definition: gradstak.cpp:503
double x
&lt; value of the variable
Definition: fvar.hpp:195