ADMB Documentation  11.1x.2735
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_arr.cpp 2724 2014-11-25 21:56:43Z 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   allocate(t.indexmin(),t.indexmax());
00052 
00053   for (int i=indexmin(); i<=indexmax(); i++)
00054   {
00055      va[i].x=(t.v)[i];
00056   }
00057 
00058   make_indvar_list(*this);
00059 }
00060 
00065  dvar_vector::dvar_vector(const dvector& t)
00066  {
00067    if (!t)
00068    {
00069      allocate();
00070    }
00071    else
00072    {
00073      va=NULL;
00074      allocate(t.indexmin(),t.indexmax());
00075      initialize();
00076      for (int i = indexmin(); i <= indexmax(); i++)
00077      {
00078        va[i].x=(t.v)[i];
00079      }
00080    }
00081  }
00082 
00083 
00084 //#ifdef __BORLANDC__
00085  //prevariable dvar_vector::elem(int i) { return (va+i); }
00086 //#endif
00087 
00092 dvar_vector::dvar_vector(unsigned int sz, double* x)
00093 {
00094 #ifndef OPT_LIB
00095   assert(sz > 0 && sz <= INT_MAX);
00096 #endif
00097   allocate(0, (int)(sz - 1));
00098   for (unsigned int i = 0; i < sz; i++)
00099   {
00100     va[i].x = x[i];
00101   }
00102 }
00103 
00108  dvar_vector::dvar_vector(int ncl,int nch)
00109  {
00110    if (ncl>nch)
00111      allocate();
00112    else
00113    {
00114      va=NULL;
00115      allocate(ncl,nch);
00116    }
00117    #ifdef SAFE_INITIALIZE
00118      initialize();
00119    #endif
00120 
00121    #ifdef DIAG
00122      cout << " Allocating dvar_vector with ptr_address\n  "
00123          << &va << "  pointing at  " << (va+indexmin()) << "\n";
00124    #endif
00125  }
00126 
00127 /*
00128  dvar_vector::dvar_vector(const ad_integer& ncl,const ad_integer& nch)
00129  {
00130    allocate(ncl,nch);
00131    #ifdef SAFE_INITIALIZE
00132      initialize();
00133    #endif
00134 
00135    #ifdef DIAG
00136      cout << " Allocating dvar_vector with ptr_address\n  "
00137          << &va << "  pointing at  " << (va+indexmin()) << "\n";
00138    #endif
00139  }
00140  */
00141 
00146  dvar_vector::dvar_vector(void)
00147  {
00148    allocate();
00149  }
00150 
00155 void make_indvar_list(const dvar_vector& t)
00156 {
00157   if (!gradient_structure::instances)
00158   {
00159     return;
00160   }
00161   if ((unsigned int)(t.indexmax()-t.indexmin()+1)
00162     > gradient_structure::MAX_NVAR_OFFSET)
00163   {
00164    if (ad_printf)
00165    {
00166      (*ad_printf)("Current maximum number of independent variables is %d\n",
00167         gradient_structure::MAX_NVAR_OFFSET);
00168      (*ad_printf)("  You need to increase the global variable "
00169      "MAX_NVAR_OFFSET to %d\n",t.indexmax()-t.indexmin()+1);
00170      (*ad_printf)("  This can be done by putting the line\n"
00171          "    gradient_structure::set_MAX_NVAR_OFFSET(%d);\n",
00172         t.indexmax()-t.indexmin()+1);
00173      (*ad_printf)("  before the declaration of the gradient_structure object.\n"
00174         " or the command line option -mno %d\n",
00175         t.indexmax()-t.indexmin()+1);
00176    }
00177    else
00178    {
00179      cerr << "Current maximum number of independent variables is "
00180           << gradient_structure::MAX_NVAR_OFFSET << "\n"
00181           <<  "  You need to increase the global variable MAX_NVAR_OFFSET to "
00182           << (t.indexmax()-t.indexmin()+1) << "\n"
00183           << "  This can be done by putting the line\n"
00184           << "    'gradient_structure::set_MAX_NVAR_OFFSET("
00185           << (t.indexmax()-t.indexmin()+1) << ");'\n"
00186           << "  before the declaration of the gradient_structure object.\n"
00187           << " or use the -mno 1149 command line option in AD Model Builder\n";
00188    }
00189    ad_exit(21);
00190   }
00191 
00192   for (int i=t.indexmin(); i<=t.indexmax(); i++)
00193   {
00194     unsigned int tmp = (unsigned int)(i - t.indexmin());
00195     gradient_structure::INDVAR_LIST->put_address(tmp,&(t.va[i].x));
00196   }
00197   gradient_structure::NVAR=t.indexmax()-t.indexmin()+1;
00198 }
00199 
00204 void copy_status(const ostream& _s, const dvar_vector& v)
00205    {
00206      ostream& s= (ostream&) _s;
00207      s << " copy_flag ";
00208      s <<"\n";
00209    }
00210 
00215    void dvar_vector::allocate(void)
00216    {
00217      shape=NULL;
00218      va=NULL;
00219    }
00220 
00225 void dvar_vector::allocate(const dvector& v1)
00226    {
00227      allocate(v1.indexmin(),v1.indexmax());
00228    }
00229 
00234 void dvar_vector::allocate(const dvar_vector& v1)
00235    {
00236      allocate(v1.indexmin(),v1.indexmax());
00237    }
00238 
00243 void dvar_vector::allocatec(const dvar_vector& t)
00244    {
00245      if (!(*this))
00246      {
00247        if (t.shape)
00248        {
00249          shape=t.shape;
00250          (shape->ncopies)++;
00251        }
00252        else
00253        {
00254          //cerr << "Making a copy of an unallocated dvar_vector"<<endl;
00255        }
00256        link_ptr=t.link_ptr;
00257        index_min=t.index_min;
00258        index_max=t.index_max;
00259        va = t.va;
00260      }
00261      else
00262      {
00263        cerr << "Trying to alocate to an already allocated dvar_vector" << endl;
00264      }
00265    }
00266 
00271 void dvar_vector::allocate(int ncl, int nch)
00272 {
00273   if (ncl > nch)
00274   {
00275     allocate();
00276   }
00277   else
00278   {
00279     index_min=ncl;
00280     index_max=nch;
00281 #ifndef OPT_LIB
00282     assert(nch >= ncl);
00283 #endif
00284     unsigned int itemp = (unsigned int)(nch - ncl + 1);
00285 /*
00286     if (itemp<=0)
00287     {
00288          cerr << "Error in dvar_vector constructor max index must be"
00289                  " >= minindex\n"
00290             << "minindex = " << ncl << " maxindex = " << nch <<endl;
00291          ad_exit(1);
00292     }
00293 */
00294     if ((va = arr_new(itemp)) == 0)
00295     {
00296       cerr << " Error trying to allocate memory for dvar_vector\n";
00297       ad_exit(1);
00298     }
00299     else
00300     {
00301       if ( (shape=new vector_shapex(ncl,nch,va)) == NULL)
00302       {
00303         cerr << "Error trying to allocate memory for dvar_vector\n";
00304         ad_exit(1);
00305       }
00306       link_ptr=* (arr_link **) va;
00307       va -= indexmin();
00308       // if ( ((int)va) %8) cerr << "Array is not QWORD alligned" << endl;
00309 #ifdef DIAG
00310       myheapcheck("Leaving dvar_vector::allocate(ncl,nch)");
00311 #endif
00312     }
00313   }
00314 }
00315 
00320    void dvar_vector::allocate(const ad_integer& ncl,const ad_integer& nch)
00321    {
00322      allocate(int(ncl),int(nch));
00323    }