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