ADMB Documentation  11.5.3197
 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 {
00145   allocate();
00146 }
00147 
00152 void make_indvar_list(const dvar_vector& t)
00153 {
00154   if (!gradient_structure::instances)
00155   {
00156     return;
00157   }
00158   if ((unsigned int)(t.indexmax()-t.indexmin()+1)
00159     > gradient_structure::MAX_NVAR_OFFSET)
00160   {
00161    if (ad_printf)
00162    {
00163      (*ad_printf)("Current maximum number of independent variables is %d\n",
00164         gradient_structure::MAX_NVAR_OFFSET);
00165      (*ad_printf)("  You need to increase the global variable "
00166      "MAX_NVAR_OFFSET to %d\n",t.indexmax()-t.indexmin()+1);
00167      (*ad_printf)("  This can be done by putting the line\n"
00168          "    gradient_structure::set_MAX_NVAR_OFFSET(%d);\n",
00169         t.indexmax()-t.indexmin()+1);
00170      (*ad_printf)("  before the declaration of the gradient_structure object.\n"
00171         " or the command line option -mno %d\n",
00172         t.indexmax()-t.indexmin()+1);
00173    }
00174    else
00175    {
00176      cerr << "Current maximum number of independent variables is "
00177           << gradient_structure::MAX_NVAR_OFFSET << "\n"
00178           <<  "  You need to increase the global variable MAX_NVAR_OFFSET to "
00179           << (t.indexmax()-t.indexmin()+1) << "\n"
00180           << "  This can be done by putting the line\n"
00181           << "    'gradient_structure::set_MAX_NVAR_OFFSET("
00182           << (t.indexmax()-t.indexmin()+1) << ");'\n"
00183           << "  before the declaration of the gradient_structure object.\n"
00184           << " or use the -mno 1149 command line option in AD Model Builder\n";
00185    }
00186    ad_exit(21);
00187   }
00188 
00189   for (int i=t.indexmin(); i<=t.indexmax(); i++)
00190   {
00191     unsigned int tmp = (unsigned int)(i - t.indexmin());
00192     gradient_structure::INDVAR_LIST->put_address(tmp,&(t.va[i].x));
00193   }
00194   gradient_structure::NVAR=t.indexmax()-t.indexmin()+1;
00195 }
00196 
00201 void copy_status(const ostream& _s, const dvar_vector& v)
00202    {
00203      ostream& s= (ostream&) _s;
00204      s << " copy_flag ";
00205      s <<"\n";
00206    }
00207 
00211 void dvar_vector::allocate()
00212 {
00213   va = NULL;
00214   link_ptr = NULL;
00215   shape = NULL;
00216   index_min = 0;
00217   index_max = -1;
00218 }
00222 void dvar_vector::allocate(const dvector& v1)
00223 {
00224   allocate(v1.indexmin(), v1.indexmax());
00225 }
00229 void dvar_vector::allocate(const dvar_vector& v1)
00230 {
00231   allocate(v1.indexmin(), v1.indexmax());
00232 }
00233 
00238 void dvar_vector::allocatec(const dvar_vector& t)
00239    {
00240      if (!(*this))
00241      {
00242        if (t.shape)
00243        {
00244          shape=t.shape;
00245          (shape->ncopies)++;
00246        }
00247        else
00248        {
00249          //cerr << "Making a copy of an unallocated dvar_vector"<<endl;
00250        }
00251        link_ptr=t.link_ptr;
00252        index_min=t.index_min;
00253        index_max=t.index_max;
00254        va = t.va;
00255      }
00256      else
00257      {
00258        cerr << "Trying to alocate to an already allocated dvar_vector" << endl;
00259      }
00260    }
00261 
00265 void dvar_vector::allocate(int ncl, int nch)
00266 {
00267   if (ncl > nch)
00268   {
00269     allocate();
00270   }
00271   else
00272   {
00273     index_min=ncl;
00274     index_max=nch;
00275 #ifndef OPT_LIB
00276     assert(nch >= ncl);
00277 #endif
00278     unsigned int itemp = (unsigned int)(nch - ncl + 1);
00279 /*
00280     if (itemp<=0)
00281     {
00282          cerr << "Error in dvar_vector constructor max index must be"
00283                  " >= minindex\n"
00284             << "minindex = " << ncl << " maxindex = " << nch <<endl;
00285          ad_exit(1);
00286     }
00287 */
00288     if ((va = arr_new(itemp)) == 0)
00289     {
00290       cerr << " Error trying to allocate memory for dvar_vector\n";
00291       ad_exit(1);
00292     }
00293     else
00294     {
00295       if ( (shape=new vector_shapex(ncl,nch,va)) == NULL)
00296       {
00297         cerr << "Error trying to allocate memory for dvar_vector\n";
00298         ad_exit(1);
00299       }
00300       link_ptr=* (arr_link **) va;
00301       va -= indexmin();
00302       // if ( ((int)va) %8) cerr << "Array is not QWORD alligned" << endl;
00303 #ifdef DIAG
00304       myheapcheck("Leaving dvar_vector::allocate(ncl,nch)");
00305 #endif
00306     }
00307   }
00308 }
00309 
00314    void dvar_vector::allocate(const ad_integer& ncl,const ad_integer& nch)
00315    {
00316      allocate(int(ncl),int(nch));
00317    }