ADMB Documentation  11.1.2542
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_arr.cpp 1919 2014-04-22 22:02: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 //extern double * NULL_ADDRESS;
00020 //extern grad_stack  * GRAD_STACK1; //js
00021 //extern indvar_offset_list * INDVAR_LIST;
00022 //extern unsigned  MAX_NVAR_OFFSET;
00023 
00024 void * farptr_norm(void *);
00025 long int farptr_tolong(void *) ;
00026 
00031 dvar_vector& dvar_vector::shift(int min)
00032 {
00033   va += indexmin()-min;
00034   index_max=index_max-index_min+min;
00035   index_min=min;
00036   shape->shift(min);
00037   return *this;
00038 }
00039 
00044  dvar_vector::dvar_vector(const independent_variables& t)
00045  {
00046    int i;
00047    allocate(t.indexmin(),t.indexmax());
00048 
00049    for ( i=indexmin(); i<=indexmax(); i++)
00050    {
00051      va[i].x=(t.v)[i];
00052    }
00053 
00054    make_indvar_list(*this);
00055  }
00056 
00061  dvar_vector::dvar_vector(const dvector& t)
00062  {
00063    if (!t)
00064    {
00065      allocate();
00066    }
00067    else
00068    {
00069      va=NULL;
00070      allocate(t.indexmin(),t.indexmax());
00071      initialize();
00072      for (int i = indexmin(); i <= indexmax(); i++)
00073      {
00074        va[i].x=(t.v)[i];
00075      }
00076    }
00077  }
00078 
00079 
00080 //#ifdef __BORLANDC__
00081  //prevariable dvar_vector::elem(int i) { return (va+i); }
00082 //#endif
00083 
00088  dvar_vector::dvar_vector( unsigned int sz, double * x )
00089  {
00090    allocate(0,sz-1);
00091    for (unsigned int i=0; i<sz; i++)
00092    {
00093      va[i].x = x[i];
00094    }
00095  }
00096 
00101  dvar_vector::dvar_vector(int ncl,int nch)
00102  {
00103    if (ncl>nch)
00104      allocate();
00105    else
00106    {
00107      va=NULL;
00108      allocate(ncl,nch);
00109    }
00110    #ifdef SAFE_INITIALIZE
00111      initialize();
00112    #endif
00113 
00114    #ifdef DIAG
00115      cout << " Allocating dvar_vector with ptr_address\n  "
00116          << &va << "  pointing at  " << (va+indexmin()) << "\n";
00117    #endif
00118  }
00119 
00120 /*
00121  dvar_vector::dvar_vector(const ad_integer& ncl,const ad_integer& nch)
00122  {
00123    allocate(ncl,nch);
00124    #ifdef SAFE_INITIALIZE
00125      initialize();
00126    #endif
00127 
00128    #ifdef DIAG
00129      cout << " Allocating dvar_vector with ptr_address\n  "
00130          << &va << "  pointing at  " << (va+indexmin()) << "\n";
00131    #endif
00132  }
00133  */
00134 
00139  dvar_vector::dvar_vector(void)
00140  {
00141    allocate();
00142  }
00143 
00148 void make_indvar_list(const dvar_vector& t)
00149 {
00150   if (!gradient_structure::instances)
00151   {
00152     return;
00153   }
00154   if ((unsigned int)(t.indexmax()-t.indexmin()+1)
00155     > gradient_structure::MAX_NVAR_OFFSET)
00156   {
00157    if (ad_printf)
00158    {
00159      (*ad_printf)("Current maximum number of independent variables is %d\n",
00160         gradient_structure::MAX_NVAR_OFFSET);
00161      (*ad_printf)("  You need to increase the global variable "
00162      "MAX_NVAR_OFFSET to %d\n",t.indexmax()-t.indexmin()+1);
00163      (*ad_printf)("  This can be done by putting the line\n"
00164          "    gradient_structure::set_MAX_NVAR_OFFSET(%d);\n",
00165         t.indexmax()-t.indexmin()+1);
00166      (*ad_printf)("  before the declaration of the gradient_structure object.\n"
00167         " or the command line option -mno %d\n",
00168         t.indexmax()-t.indexmin()+1);
00169    }
00170    else
00171    {
00172      cerr << "Current maximum number of independent variables is "
00173           << gradient_structure::MAX_NVAR_OFFSET << "\n"
00174           <<  "  You need to increase the global variable MAX_NVAR_OFFSET to "
00175           << (t.indexmax()-t.indexmin()+1) << "\n"
00176           << "  This can be done by putting the line\n"
00177           << "    'gradient_structure::set_MAX_NVAR_OFFSET("
00178           << (t.indexmax()-t.indexmin()+1) << ");'\n"
00179           << "  before the declaration of the gradient_structure object.\n"
00180           << " or use the -mno 1149 command line option in AD Model Builder\n";
00181    }
00182    ad_exit(21);
00183   }
00184 
00185   for (int i=t.indexmin(); i<=t.indexmax(); i++)
00186   {
00187     unsigned int tmp=i-t.indexmin();
00188     gradient_structure::INDVAR_LIST->put_address(tmp,&(t.va[i].x));
00189   }
00190   gradient_structure::NVAR=t.indexmax()-t.indexmin()+1;
00191 }
00192 
00197 void copy_status(const ostream& _s, const dvar_vector& v)
00198    {
00199      ostream& s= (ostream&) _s;
00200      s << " copy_flag ";
00201      s <<"\n";
00202    }
00203 
00208    void dvar_vector::allocate(void)
00209    {
00210      shape=NULL;
00211      va=NULL;
00212    }
00213 
00218 void dvar_vector::allocate(const dvector& v1)
00219    {
00220      allocate(v1.indexmin(),v1.indexmax());
00221    }
00222 
00227 void dvar_vector::allocate(const dvar_vector& v1)
00228    {
00229      allocate(v1.indexmin(),v1.indexmax());
00230    }
00231 
00236 void dvar_vector::allocatec(const dvar_vector& t)
00237    {
00238      if (!(*this))
00239      {
00240        if (t.shape)
00241        {
00242          shape=t.shape;
00243          (shape->ncopies)++;
00244        }
00245        else
00246        {
00247          //cerr << "Making a copy of an unallocated dvar_vector"<<endl;
00248        }
00249        link_ptr=t.link_ptr;
00250        index_min=t.index_min;
00251        index_max=t.index_max;
00252        va = t.va;
00253      }
00254      else
00255      {
00256        cerr << "Trying to alocate to an already allocated dvar_vector" << endl;
00257      }
00258    }
00259 
00264    void dvar_vector::allocate(int ncl,int nch)
00265    {
00266      if (ncl>nch)
00267        allocate();
00268      else
00269      {
00270        index_min=ncl;
00271        index_max=nch;
00272        int itemp=nch-ncl+1;
00273        if (itemp<=0)
00274        {
00275          cerr << "Error in dvar_vector constructor max index must be"
00276                  " >= minindex\n"
00277             << "minindex = " << ncl << " maxindex = " << nch <<endl;
00278          ad_exit(1);
00279        }
00280        if ( (va = arr_new(itemp)) ==0)
00281        {
00282          cerr << " Error trying to allocate memory for dvar_vector\n";
00283          ad_exit(1);
00284        }
00285 
00286        if ( (shape=new vector_shapex(ncl,nch,va)) == NULL)
00287        {
00288          cerr << "Error trying to allocate memory for dvar_vector\n";
00289          ad_exit(1);
00290        }
00291 
00292        link_ptr=* (arr_link **) va;
00293        va -= indexmin();
00294        // if ( ((int)va) %8) cerr << "Array is not QWORD alligned" << endl;
00295 
00296        #ifdef DIAG
00297          myheapcheck("Leaving dvar_vector::allocate(ncl,nch)");
00298        #endif
00299      }
00300    }
00301 
00306    void dvar_vector::allocate(const ad_integer& ncl,const ad_integer& nch)
00307    {
00308      allocate(int(ncl),int(nch));
00309    }