ADMB Documentation  11.1x.2711
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_arr.cpp 2629 2014-11-12 20:52:01Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 #if defined(__TURBOC__)
00013   #pragma hdrstop
00014   #include <alloc.h>
00015 #endif
00016 
00017 #include <stdlib.h>
00018 
00019 #ifndef OPT_LIB
00020   #include <cassert>
00021   #include <climits>
00022 #endif
00023 
00024 //extern double * NULL_ADDRESS;
00025 //extern grad_stack  * GRAD_STACK1; //js
00026 //extern indvar_offset_list * INDVAR_LIST;
00027 //extern unsigned  MAX_NVAR_OFFSET;
00028 
00029 void * farptr_norm(void *);
00030 long int farptr_tolong(void *) ;
00031 
00036 dvar_vector& dvar_vector::shift(int min)
00037 {
00038   va += indexmin()-min;
00039   index_max=index_max-index_min+min;
00040   index_min=min;
00041   shape->shift(min);
00042   return *this;
00043 }
00044 
00049  dvar_vector::dvar_vector(const independent_variables& t)
00050  {
00051    int i;
00052    allocate(t.indexmin(),t.indexmax());
00053 
00054    for ( i=indexmin(); i<=indexmax(); i++)
00055    {
00056      va[i].x=(t.v)[i];
00057    }
00058 
00059    make_indvar_list(*this);
00060  }
00061 
00066  dvar_vector::dvar_vector(const dvector& t)
00067  {
00068    if (!t)
00069    {
00070      allocate();
00071    }
00072    else
00073    {
00074      va=NULL;
00075      allocate(t.indexmin(),t.indexmax());
00076      initialize();
00077      for (int i = indexmin(); i <= indexmax(); i++)
00078      {
00079        va[i].x=(t.v)[i];
00080      }
00081    }
00082  }
00083 
00084 
00085 //#ifdef __BORLANDC__
00086  //prevariable dvar_vector::elem(int i) { return (va+i); }
00087 //#endif
00088 
00093 dvar_vector::dvar_vector(unsigned int sz, double* x)
00094 {
00095 #ifndef OPT_LIB
00096   assert(sz > 0 && sz <= INT_MAX);
00097 #endif
00098   allocate(0, (int)(sz - 1));
00099   for (unsigned int i = 0; i < sz; i++)
00100   {
00101     va[i].x = x[i];
00102   }
00103 }
00104 
00109  dvar_vector::dvar_vector(int ncl,int nch)
00110  {
00111    if (ncl>nch)
00112      allocate();
00113    else
00114    {
00115      va=NULL;
00116      allocate(ncl,nch);
00117    }
00118    #ifdef SAFE_INITIALIZE
00119      initialize();
00120    #endif
00121 
00122    #ifdef DIAG
00123      cout << " Allocating dvar_vector with ptr_address\n  "
00124          << &va << "  pointing at  " << (va+indexmin()) << "\n";
00125    #endif
00126  }
00127 
00128 /*
00129  dvar_vector::dvar_vector(const ad_integer& ncl,const ad_integer& nch)
00130  {
00131    allocate(ncl,nch);
00132    #ifdef SAFE_INITIALIZE
00133      initialize();
00134    #endif
00135 
00136    #ifdef DIAG
00137      cout << " Allocating dvar_vector with ptr_address\n  "
00138          << &va << "  pointing at  " << (va+indexmin()) << "\n";
00139    #endif
00140  }
00141  */
00142 
00147  dvar_vector::dvar_vector(void)
00148  {
00149    allocate();
00150  }
00151 
00156 void make_indvar_list(const dvar_vector& t)
00157 {
00158   if (!gradient_structure::instances)
00159   {
00160     return;
00161   }
00162   if ((unsigned int)(t.indexmax()-t.indexmin()+1)
00163     > gradient_structure::MAX_NVAR_OFFSET)
00164   {
00165    if (ad_printf)
00166    {
00167      (*ad_printf)("Current maximum number of independent variables is %d\n",
00168         gradient_structure::MAX_NVAR_OFFSET);
00169      (*ad_printf)("  You need to increase the global variable "
00170      "MAX_NVAR_OFFSET to %d\n",t.indexmax()-t.indexmin()+1);
00171      (*ad_printf)("  This can be done by putting the line\n"
00172          "    gradient_structure::set_MAX_NVAR_OFFSET(%d);\n",
00173         t.indexmax()-t.indexmin()+1);
00174      (*ad_printf)("  before the declaration of the gradient_structure object.\n"
00175         " or the command line option -mno %d\n",
00176         t.indexmax()-t.indexmin()+1);
00177    }
00178    else
00179    {
00180      cerr << "Current maximum number of independent variables is "
00181           << gradient_structure::MAX_NVAR_OFFSET << "\n"
00182           <<  "  You need to increase the global variable MAX_NVAR_OFFSET to "
00183           << (t.indexmax()-t.indexmin()+1) << "\n"
00184           << "  This can be done by putting the line\n"
00185           << "    'gradient_structure::set_MAX_NVAR_OFFSET("
00186           << (t.indexmax()-t.indexmin()+1) << ");'\n"
00187           << "  before the declaration of the gradient_structure object.\n"
00188           << " or use the -mno 1149 command line option in AD Model Builder\n";
00189    }
00190    ad_exit(21);
00191   }
00192 
00193   for (int i=t.indexmin(); i<=t.indexmax(); i++)
00194   {
00195     unsigned int tmp = (unsigned int)(i - t.indexmin());
00196     gradient_structure::INDVAR_LIST->put_address(tmp,&(t.va[i].x));
00197   }
00198   gradient_structure::NVAR=t.indexmax()-t.indexmin()+1;
00199 }
00200 
00205 void copy_status(const ostream& _s, const dvar_vector& v)
00206    {
00207      ostream& s= (ostream&) _s;
00208      s << " copy_flag ";
00209      s <<"\n";
00210    }
00211 
00216    void dvar_vector::allocate(void)
00217    {
00218      shape=NULL;
00219      va=NULL;
00220    }
00221 
00226 void dvar_vector::allocate(const dvector& v1)
00227    {
00228      allocate(v1.indexmin(),v1.indexmax());
00229    }
00230 
00235 void dvar_vector::allocate(const dvar_vector& v1)
00236    {
00237      allocate(v1.indexmin(),v1.indexmax());
00238    }
00239 
00244 void dvar_vector::allocatec(const dvar_vector& t)
00245    {
00246      if (!(*this))
00247      {
00248        if (t.shape)
00249        {
00250          shape=t.shape;
00251          (shape->ncopies)++;
00252        }
00253        else
00254        {
00255          //cerr << "Making a copy of an unallocated dvar_vector"<<endl;
00256        }
00257        link_ptr=t.link_ptr;
00258        index_min=t.index_min;
00259        index_max=t.index_max;
00260        va = t.va;
00261      }
00262      else
00263      {
00264        cerr << "Trying to alocate to an already allocated dvar_vector" << endl;
00265      }
00266    }
00267 
00272    void dvar_vector::allocate(int ncl,int nch)
00273    {
00274      if (ncl>nch)
00275        allocate();
00276      else
00277      {
00278        index_min=ncl;
00279        index_max=nch;
00280 #ifndef OPT_LIB
00281        assert(nch >= ncl);
00282 #endif
00283        unsigned int itemp = (unsigned int)(nch - ncl + 1);
00284 /*
00285        if (itemp<=0)
00286        {
00287          cerr << "Error in dvar_vector constructor max index must be"
00288                  " >= minindex\n"
00289             << "minindex = " << ncl << " maxindex = " << nch <<endl;
00290          ad_exit(1);
00291        }
00292 */
00293        if ( (va = arr_new(itemp)) ==0)
00294        {
00295          cerr << " Error trying to allocate memory for dvar_vector\n";
00296          ad_exit(1);
00297        }
00298 
00299        if ( (shape=new vector_shapex(ncl,nch,va)) == NULL)
00300        {
00301          cerr << "Error trying to allocate memory for dvar_vector\n";
00302          ad_exit(1);
00303        }
00304 
00305        link_ptr=* (arr_link **) va;
00306        va -= indexmin();
00307        // if ( ((int)va) %8) cerr << "Array is not QWORD alligned" << endl;
00308 
00309        #ifdef DIAG
00310          myheapcheck("Leaving dvar_vector::allocate(ncl,nch)");
00311        #endif
00312      }
00313    }
00314 
00319    void dvar_vector::allocate(const ad_integer& ncl,const ad_integer& nch)
00320    {
00321      allocate(int(ncl),int(nch));
00322    }