ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
def_eval.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 #ifdef __ZTC__
00023   #include <iostream.hpp>
00024 #endif
00025 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 
00029 #ifdef __SUN__
00030   #include <iostream.h>
00031   #include <sys/stat.h>
00032   #include <sys/types.h>
00033   #include <unistd.h>
00034 #endif
00035 
00036 #ifdef _MSC_VER
00037   #define lseek _lseek
00038   #define  read _read
00039   #define write _write
00040 #else
00041   #include <iostream>
00042   using namespace std;
00043   #include <sys/stat.h>
00044   #include <sys/types.h>
00045   #include <unistd.h>
00046 #endif
00047 
00048 #if defined (__WAT32__)
00049   #include <io.h>
00050 #endif
00051 
00052 #include <math.h>
00053 
00054 #ifdef ISZERO
00055   #undef ISZERO
00056 #endif
00057 #define ISZERO(d) ((d)==0.0)
00058 //#define XXX 3.70255042e-17
00059 
00060   //ofstream gradlog("grad.log");
00061 
00066   void default_evaluation(void)
00067   {
00068     // checks to see how many independent variables there are
00069     grad_stack_entry * grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00070     double * zptr = grad_ptr->dep_addr;
00071     double z = *zptr;
00072     *zptr = 0;
00073     // Check to see if first independent variable is a NULL adress
00074     if  ( grad_ptr->ind_addr1 )
00075     {
00076       * grad_ptr->ind_addr1 += z * grad_ptr->mult1 ;
00077         //gradlog <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00078         //if (fabs(* grad_ptr->ind_addr1+XXX)<1.e-16)
00079          // cout <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00080 #if defined(USE_DDOUBLE)
00081       if  ( grad_ptr->mult2 !=0)
00082 #else
00083       if  (!ISZERO(grad_ptr->mult2))
00084 #endif
00085       {
00086         * grad_ptr->ind_addr2 += z * grad_ptr->mult2 ;
00087         //gradlog <<  setprecision(13) << * grad_ptr->ind_addr2  << endl;
00088         //if (fabs(* grad_ptr->ind_addr2+XXX)<1.e-16)
00089          // cout <<  setprecision(13) << * grad_ptr->ind_addr2  << endl;
00090       }
00091     }
00092   }
00093 
00098   void default_evaluation1(void)
00099   {
00100     // there is one independent variable
00101     grad_stack_entry * grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00102     double z = * grad_ptr->dep_addr;
00103     * grad_ptr->dep_addr=0.;
00104     * grad_ptr->ind_addr1 += z;
00105     //gradlog <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00106     //if (fabs(* grad_ptr->ind_addr1+XXX)<1.e-16)
00107     //cout << setscientific() <<  setprecision(13) << * grad_ptr->ind_addr1
00108     //<< endl;
00109   }
00110 
00115   void default_evaluation1m(void)
00116   {
00117     // there is one independent variable
00118     grad_stack_entry * grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00119     double z = * grad_ptr->dep_addr;
00120     * grad_ptr->dep_addr=0.;
00121     * grad_ptr->ind_addr1 -= z;
00122     //gradlog <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00123      //   if (fabs(* grad_ptr->ind_addr1+XXX)<1.e-16)
00124       //    cout <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00125   }
00126 
00131   void default_evaluation0(void)
00132   {
00133     // there is one independent variable
00134     * gradient_structure::GRAD_STACK1->ptr->dep_addr=0.;
00135   }
00136 
00141   void default_evaluation2(void)
00142   {
00143     //char ch;
00144     // there is one independent variable
00145     grad_stack_entry * grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00146     //cout << setprecision(16) << *grad_ptr->dep_addr << endl;
00147     //cout << setprecision(16) << *grad_ptr->ind_addr1 << endl;
00148     //cout << setprecision(16) << grad_ptr->mult1 << endl;
00149     //cin >> ch;
00150     double z = * grad_ptr->dep_addr;
00151     * grad_ptr->dep_addr=0.;
00152     * grad_ptr->ind_addr1 += z * grad_ptr->mult1 ;
00153     //gradlog <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00154      //   if (fabs(* grad_ptr->ind_addr1+XXX)<1.e-16)
00155       //    cout <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00156   }
00157 
00162   void default_evaluation3(void)
00163   {
00164     // there are two independent variables
00165     grad_stack_entry * grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00166     double z = * grad_ptr->dep_addr;
00167     * grad_ptr->dep_addr=0.;
00168     * grad_ptr->ind_addr1 += z * grad_ptr->mult1 ;
00169     * grad_ptr->ind_addr2 += z * grad_ptr->mult2 ;
00170     //gradlog <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00171      //   if (fabs(* grad_ptr->ind_addr1+XXX)<1.e-16)
00172       //    cout <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00173   }
00174 
00179   void default_evaluation3ind(void)
00180   {
00181     // there are three independent variables
00182     grad_stack_entry *  grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00183 
00184     double mult3=grad_ptr->mult1 ;
00185     double * ind_addr3=grad_ptr->ind_addr1;
00186     if (gradient_structure::GRAD_STACK1->ptr-- ==
00187       gradient_structure::GRAD_STACK1->ptr_first)
00188     {
00189       // \todo Need test
00190       // back up the file one buffer size and read forward
00191       off_t offset = (off_t)(sizeof(grad_stack_entry)
00192         * gradient_structure::GRAD_STACK1->length);
00193       off_t lpos=lseek(gradient_structure::GRAD_STACK1->_GRADFILE_PTR,
00194         -offset, SEEK_CUR);
00195 
00196       gradient_structure::GRAD_STACK1->read_grad_stack_buffer(lpos);
00197       //gradient_structure::GRAD_STACK1->ptr++;
00198     }
00199 
00200     grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00201     double z = * grad_ptr->dep_addr;
00202     * grad_ptr->dep_addr=0.;
00203     * grad_ptr->ind_addr1 += z * grad_ptr->mult1 ;
00204     * grad_ptr->ind_addr2 += z * grad_ptr->mult2 ;
00205     * ind_addr3 += z * mult3 ;
00206   }
00207 
00212   void default_evaluation4ind(void)
00213   {
00214     // there are three independent variables
00215     grad_stack_entry *  grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00216 
00217     double * ind_addr3=grad_ptr->ind_addr1;
00218     double mult3=grad_ptr->mult1 ;
00219     double * ind_addr4=grad_ptr->ind_addr2;
00220     double mult4=grad_ptr->mult2 ;
00221     if (gradient_structure::GRAD_STACK1->ptr-- ==
00222       gradient_structure::GRAD_STACK1->ptr_first)
00223     {
00224       // \todo Need test
00225       // back up the file one buffer size and read forward
00226       off_t offset = (off_t)(sizeof(grad_stack_entry)
00227         * gradient_structure::GRAD_STACK1->length);
00228       off_t lpos=lseek(gradient_structure::GRAD_STACK1->_GRADFILE_PTR,
00229         -offset, SEEK_CUR);
00230 
00231       gradient_structure::GRAD_STACK1->read_grad_stack_buffer(lpos);
00232       //gradient_structure::GRAD_STACK1->ptr++;
00233     }
00234 
00235     grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00236     double z = * grad_ptr->dep_addr;
00237     * grad_ptr->dep_addr=0.;
00238     * grad_ptr->ind_addr1 += z * grad_ptr->mult1 ;
00239     * grad_ptr->ind_addr2 += z * grad_ptr->mult2 ;
00240     * ind_addr3 += z * mult3 ;
00241     * ind_addr4 += z * mult4 ;
00242     //gradlog <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00243      //   if (fabs(* grad_ptr->ind_addr1+XXX)<1.e-16)
00244       //    cout <<  setprecision(13) << * grad_ptr->ind_addr1  << endl;
00245     //gradlog <<  setprecision(13) << * grad_ptr->ind_addr2  << endl;
00246      //   if (fabs(* grad_ptr->ind_addr2+XXX)<1.e-16)
00247       //    cout <<  setprecision(13) << * grad_ptr->ind_addr2  << endl;
00248     //gradlog <<  setprecision(13) << * ind_addr3  << endl;
00249      //   if (fabs(* ind_addr3+XXX)<1.e-16)
00250       //    cout <<  setprecision(13) << * ind_addr3  << endl;
00251     //gradlog <<  setprecision(13) << * ind_addr4  << endl;
00252      //   if (fabs(* ind_addr4+XXX)<1.e-16)
00253       //    cout <<  setprecision(13) << * ind_addr4  << endl;
00254   }
00255 
00260   void default_evaluation4(void)
00261   {
00262     // there are two independent variables
00263     grad_stack_entry * grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00264     double z = * grad_ptr->dep_addr;
00265     * grad_ptr->dep_addr=0.;
00266     * grad_ptr->ind_addr1 += z;
00267     * grad_ptr->ind_addr2 += z;
00268   }
00269 
00274   void default_evaluation4m(void)
00275   {
00276     // there are two independent variables
00277     grad_stack_entry * grad_ptr = gradient_structure::GRAD_STACK1->ptr;
00278     double z = * grad_ptr->dep_addr;
00279     * grad_ptr->dep_addr=0.;
00280     * grad_ptr->ind_addr1 += z;
00281     * grad_ptr->ind_addr2 -= z;
00282   }