ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
jacob2.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00013 #include <sys/stat.h>
00014 #include <fcntl.h>
00015 #include <string.h>
00016 
00017 #ifdef __TURBOC__
00018   #pragma hdrstop
00019   #include <iostream.h>
00020 #endif
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 
00025 #ifdef __SUN__
00026   #include <iostream.h>
00027   #include <sys/stat.h>
00028   #include <sys/types.h>
00029   #include <unistd.h>
00030   #ifdef _MSC_VER
00031     #define lseek _lseek
00032     #define  read _read
00033     #define write _write
00034     #define open _open
00035     #define close _close
00036   #endif
00037 #endif
00038 
00039 #if !defined(_MSC_VER)
00040   #include <iostream>
00041   using namespace std;
00042   #include <sys/stat.h>
00043   #include <sys/types.h>
00044   #include <unistd.h>
00045 #endif
00046 
00047 #if defined(__NDPX__ )
00048   extern "C" {
00049     int lseek(int, int, int);
00050     int read(int, char*, int);
00051   };
00052 #endif
00053 
00054 #include <math.h>
00055 
00060 void jacobcalc(int nvar, const ofstream& ofs)
00061 {
00062   gradient_structure::jacobcalc(nvar,ofs);
00063 }
00064 
00069 void gradient_structure::jacobcalc(int nvar, const ofstream& _ofs)
00070 {
00071   ADUNCONST(ofstream,ofs)
00072   dvector jac(1,nvar);
00073   off_t lpos;
00074   int depvar_count=DEPVARS_INFO->depvar_count;
00075 
00076   int& _GRADFILE_PTR=GRAD_STACK1->_GRADFILE_PTR;
00077   // check to see if anything has been written into the file
00078   off_t last_gpos=lseek(_GRADFILE_PTR,0L,SEEK_CUR);
00079 
00080   //save current contents of the buffer so we can get them later
00081   if (last_gpos)
00082   {
00083     GRAD_STACK1->write_grad_stack_buffer();
00084   }
00085 
00086   // check to see if anything has been written into the file
00087   off_t last_cpos=lseek(fp->file_ptr,0L,SEEK_CUR);
00088 
00089   //save current contents of the buffer so we can get them later
00090   if (last_cpos)
00091   {
00092     fp->write_cmpdif_stack_buffer();
00093   }
00094 
00095   // save variable values if desired
00096   if (save_var_flag)
00097   {
00098     save_arrays();
00099     save_variables();
00100   }
00101   // now evalueate the jacobian
00102   for (int ijac=1;ijac<=depvar_count;ijac++)
00103   {
00104     dvector& g=jac;
00105     //max_num_dependent_variables=ndv;
00106     if (depvar_count>DEPVARS_INFO->max_num_dependent_variables)
00107     {
00108       cout << "maximum number of depdendent variables of "
00109          << DEPVARS_INFO->max_num_dependent_variables << " exceeded "
00110          << endl
00111          << "use gradient_structure::set_NUM_DEPENDENT_VARIABLES(int i);"
00112          << endl << "to increase the number of dependent variables"
00113          << endl;
00114       ad_exit(1);
00115     }
00116 
00117     fp->offset=DEPVARS_INFO->cmpdif_buffer_position(ijac);
00118     fp->toffset=fp->offset;
00119     _GRADFILE_PTR=DEPVARS_INFO->grad_file_count(ijac);
00120     fp->file_ptr=DEPVARS_INFO->cmpdif_file_count(ijac);
00121     lpos=DEPVARS_INFO->grad_file_position(ijac);
00122     // position the cmpdif file correctly;
00123     if (last_cpos)
00124     {
00125       off_t cmp_lpos=DEPVARS_INFO->cmpdif_file_position(ijac);
00126       lseek(fp->file_ptr,cmp_lpos,SEEK_SET);
00127       fp->read_cmpdif_stack_buffer(cmp_lpos);
00128     }
00129     GRAD_STACK1->_GRADFILE_PTR = GRAD_STACK1->gradfile_handle();
00130 
00131     if (last_gpos)
00132     {
00133       // just use the end of the buffer
00134       GRAD_STACK1->set_gbuffer_pointers();
00135 
00136       // check to sere if buffer was written into the beginning of
00137       // the next file
00138       if ( (GRAD_STACK1->_GRADFILE_PTR == GRAD_STACK1->_GRADFILE_PTR1)
00139          && (lpos == GRAD_STACK1->end_pos1) && (lpos>0) )
00140       {
00141         // get the next file
00142         GRAD_STACK1->increment_current_gradfile_ptr();
00143         lpos=0;
00144       }
00145       // and position the file to the begginig of the buffer image
00146       lseek(_GRADFILE_PTR,lpos,SEEK_SET);
00147       // now fill the buffer with the appropriate stuff
00148       GRAD_STACK1->read_grad_stack_buffer(lpos);
00149       // now reposition the grad_buffer pointer
00150     }
00151     GRAD_STACK1->ptr=
00152          (grad_stack_entry *)DEPVARS_INFO->grad_buffer_position(ijac);
00153 
00154     if(GRAD_STACK1->ptr <= GRAD_STACK1->ptr_first)
00155     {
00156 #ifdef SAFE_ALL
00157         cerr << "warning -- calling gradcalc when no calculations generating"
00158          << endl << "derivative information have occurred" << endl;
00159 #endif
00160       g.initialize();
00161       return;
00162     }    // current is one past the end so -- it
00163 
00164     gradient_structure::GRAD_STACK1->ptr--;
00165 
00166     GRAD_LIST->initialize();
00167 
00168     double_and_int* tmp =
00169       (double_and_int*)gradient_structure::ARRAY_MEMBLOCK_BASE;
00170 
00171     unsigned long int max_last_offset
00172                = gradient_structure::ARR_LIST1->get_max_last_offset();
00173 
00174     size_t size = sizeof(double_and_int );
00175 
00176     for (unsigned int i = 0; i < (max_last_offset/size); i++)
00177     {
00178       tmp->x = 0;
00179 #if defined (__ZTC__)
00180       tmp = (double_and_int*)_farptr_norm((void*)(++tmp));
00181 #else
00182       tmp++;
00183 #endif
00184     }
00185 
00186     * gradient_structure::GRAD_STACK1->ptr->dep_addr  = 1;
00187     //double* zptr = gradient_structure::GRAD_STACK1->ptr->dep_addr;
00188 
00189     int break_flag=1;
00190 
00191     do
00192     {
00193       gradient_structure::GRAD_STACK1->ptr++;
00194       #ifdef FAST_ASSEMBLER
00195         gradloop();
00196       #else
00197         //int counter=0;
00198       while (gradient_structure::GRAD_STACK1->ptr-- >
00199              gradient_structure::GRAD_STACK1->ptr_first)
00200       {
00201         //grad_stack_entry* grad_ptr =
00202         //gradient_structure::GRAD_STACK1->ptr;
00203         {
00204           (* gradient_structure::GRAD_STACK1->ptr->func)();
00205         }
00206       }
00207       #endif
00208 
00209   // back up the file one buffer size and read forward
00210       off_t offset = (off_t)(sizeof(grad_stack_entry)
00211         * gradient_structure::GRAD_STACK1->length);
00212       lpos = lseek(gradient_structure::GRAD_STACK1->_GRADFILE_PTR,
00213         -offset, SEEK_CUR);
00214 
00215        break_flag=gradient_structure::
00216                   GRAD_STACK1->read_grad_stack_buffer(lpos);
00217     }  while (break_flag); // do
00218 
00219     int mindx = g.indexmin();
00220     for (int i=0; i < nvar; i++)
00221     {
00222       g[i+mindx] =  * gradient_structure::INDVAR_LIST->get_address(i);
00223     }
00224     gradient_structure::GRAD_STACK1->ptr =
00225          gradient_structure::GRAD_STACK1->ptr_first;
00226     //ofs << setprecision(10) << g << endl;
00227     ofs.precision(10);
00228     ofs << g << endl;
00229   }// loop over dep vars
00230   DEPVARS_INFO->depvar_count=0;
00231   if (gradient_structure::save_var_flag)
00232   {
00233     gradient_structure::restore_arrays();
00234     gradient_structure::restore_variables();
00235   }
00236 }