ADMB Documentation  11.1.2192
 All Classes Files Functions Variables Typedefs Friends Defines
jacob3.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: jacob3.cpp 1946 2014-04-29 05:22:56Z johnoel $
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 uostream& ofs)
00061 {
00062   gradient_structure::jacobcalc(nvar,ofs);
00063 }
00064 
00069 void gradient_structure::jacobcalc(int nvar, const uostream& ofs)
00070 {
00071   dvector jac(1,nvar);
00072   unsigned int i;
00073   my_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   my_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   my_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       my_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     for (i=0; i< GRAD_LIST->nlinks; i++)
00167     {
00168       * (double*) (GRAD_LIST->dlink_addresses[i]) = 0;
00169     }
00170 
00171     double_and_int* tmp =
00172       (double_and_int*)gradient_structure::ARRAY_MEMBLOCK_BASE;
00173 
00174     unsigned long int max_last_offset
00175                = gradient_structure::ARR_LIST1->get_max_last_offset();
00176 
00177     unsigned int size = sizeof(double_and_int );
00178 
00179     for (i = 0; i < (max_last_offset/size); i++)
00180     {
00181       tmp->x = 0;
00182 #if defined (__ZTC__)
00183       tmp = (double_and_int*)_farptr_norm((void*)(++tmp));
00184 #else
00185       tmp++;
00186 #endif
00187     }
00188 
00189     * gradient_structure::GRAD_STACK1->ptr->dep_addr  = 1;
00190     //double* zptr = gradient_structure::GRAD_STACK1->ptr->dep_addr;
00191 
00192     int break_flag=1;
00193     do
00194     {
00195       gradient_structure::GRAD_STACK1->ptr++;
00196       #ifdef FAST_ASSEMBLER
00197         gradloop();
00198       #else
00199         //int counter=0;
00200       while (gradient_structure::GRAD_STACK1->ptr-- >
00201              gradient_structure::GRAD_STACK1->ptr_first)
00202       {
00203         //grad_stack_entry* grad_ptr =
00204         //gradient_structure::GRAD_STACK1->ptr;
00205         {
00206           (* gradient_structure::GRAD_STACK1->ptr->func)();
00207         }
00208       }
00209       #endif
00210 
00211   // back up the file one buffer size and read forward
00212       lpos = lseek(gradient_structure::GRAD_STACK1->_GRADFILE_PTR,
00213         -((long int)(sizeof(grad_stack_entry)*gradient_structure::
00214         GRAD_STACK1->length)),SEEK_CUR);
00215 
00216        break_flag=gradient_structure::
00217                   GRAD_STACK1->read_grad_stack_buffer(lpos);
00218     }  while (break_flag); // do
00219 
00220     int mindx = g.indexmin();
00221     for (i=0; i<(unsigned int)nvar; i++)
00222     {
00223       g[i+mindx] =  * gradient_structure::INDVAR_LIST->get_address(i);
00224     }
00225     gradient_structure::GRAD_STACK1->ptr =
00226          gradient_structure::GRAD_STACK1->ptr_first;
00227     //ofs << setprecision(10) << g << endl;
00228     ofs << g ;
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 }