ADMB Documentation  11.1.1916
 All Classes Files Functions Variables Typedefs Friends Defines
f1b2loc3.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: f1b2loc3.cpp 1709 2014-02-28 21:48:21Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <df1b2fun.h>
00012 #include "f1b2locl.h"
00013 class newadkludge;
00014 extern newadkludge * newadkl;
00015 
00016 typedef local_init_pass1_var  * PLOCAL_INIT_PASS1_VAR;
00017 local_init_pass1_var** local_init_pass1_var::list =
00018   new PLOCAL_INIT_PASS1_VAR[200];
00019 int local_init_pass1_var::num_active_parameters=0;
00020 int local_init_pass1_var::num_vars=0;
00021 
00026 void local_init_pass1_var::allocate_all(void)
00027 {
00028   num_active_parameters=local_init_pass1_var::nvarcalc_all();
00029 
00030   df1b2variable::save_adpool_pointer();
00031   adpool * tmppool=df1b2variable::pool;
00032   if (!localf1b2gradlist)
00033   {
00034     localf1b2gradlist = new df1b2_gradlist(400000U,20000U,800000U,40000U,
00035       200000U,10000U,adstring("lf1b2list1"));
00036     if (!localf1b2gradlist)
00037     {
00038       cerr << "Error allocating memory for local df1b2gradlist" << endl;
00039       ad_exit(1);
00040     }
00041   }
00042   globalf1b2gradlist=f1b2gradlist;
00043   f1b2gradlist=localf1b2gradlist;
00044 
00045   if (tmppool)
00046   {
00047     //cout << tmppool << endl;
00048     // check if current pool is the right size
00049     if (tmppool->nvar != num_active_parameters)
00050     {
00051       // check sizes of other pools
00052       int found_pool_flag=0;
00053       for (int i=0;i<df1b2variable::adpool_counter;i++)
00054       {
00055         if (df1b2variable::adpool_vector[i]->nvar == num_active_parameters)
00056         {
00057           adpool * tmp = df1b2variable::pool;
00058           df1b2variable::pool=df1b2variable::adpool_vector[i];
00059           if (!tmp->on_adpool_vector())
00060           {
00061             df1b2variable::adpool_vector[df1b2variable::adpool_counter]=tmp;
00062             df1b2variable::nvar_vector[df1b2variable::adpool_counter]=
00063               tmp->nvar;
00064             //df1b2variable::adpool_counter++;
00065             df1b2variable::increment_adpool_counter();
00066             tmp->on_adpool_vector()=1;
00067           }
00068           found_pool_flag=1;
00069           break;
00070         }
00071       }
00072       if (!found_pool_flag)
00073       {
00074         if (df1b2variable::adpool_counter>=df1b2variable::adpool_vectorsize)
00075         {
00076            cerr << "Need to increase adpool_vectorsize" << endl;
00077            ad_exit(1);
00078         }
00079         if (!df1b2variable::pool->on_adpool_vector())
00080         {
00081           df1b2variable::adpool_vector[df1b2variable::adpool_counter]=
00082             df1b2variable::pool;
00083           df1b2variable::nvar_vector[df1b2variable::adpool_counter]=
00084             df1b2variable::pool->nvar;
00085           //df1b2variable::adpool_counter++;
00086           df1b2variable::increment_adpool_counter();
00087           df1b2variable::pool->on_adpool_vector()=1;
00088         }
00089         df1b2variable::pool=new adpool();
00090         if (!df1b2variable::pool)
00091         {
00092           cerr << "Memory allocation error" << endl;
00093           ad_exit(1);
00094         }
00095 
00096         df1b2variable::nvar=num_active_parameters;
00097         df1b2variable::set_blocksize();
00098 
00099         if (!df1b2variable::pool->on_adpool_vector())
00100         {
00101           df1b2variable::adpool_vector[df1b2variable::adpool_counter]=
00102             df1b2variable::pool;
00103           df1b2variable::nvar_vector[df1b2variable::adpool_counter]=
00104             df1b2variable::pool->nvar;
00105           //df1b2variable::adpool_counter++;
00106           df1b2variable::increment_adpool_counter();
00107           df1b2variable::pool->on_adpool_vector()=1;
00108         }
00109       }
00110     }
00111   }
00112   else
00113   {
00114     df1b2variable::pool=new adpool();
00115     if (!df1b2variable::pool)
00116     {
00117       cerr << "Memory allocation error" << endl;
00118       ad_exit(1);
00119     }
00120     df1b2variable::nvar=num_active_parameters;
00121     df1b2variable::set_blocksize();
00122     df1b2variable::adpool_vector[df1b2variable::adpool_counter]=
00123       df1b2variable::pool;
00124     df1b2variable::nvar_vector[df1b2variable::adpool_counter]=
00125       df1b2variable::pool->nvar;
00126     //df1b2variable::adpool_counter++;
00127     df1b2variable::increment_adpool_counter();
00128   }
00129   df1b2variable::nvar=num_active_parameters;
00130   df1b2variable::set_blocksize();
00131 
00132   for (int i=0;i<num_vars;i++)
00133   {
00134     list[i]->allocate();
00135   }
00136 }
00137 
00142 void local_init_pass1_var::add_to_list(void)
00143 {
00144   list[num_vars++]=this;
00145 }
00146 
00151 void local_init_pass1_var::set_dot_all(void)
00152 {
00153   int ii=0;
00154   for (int i=0;i<num_vars;i++)
00155   {
00156     list[i]->set_dot(ii);
00157   }
00158 }
00159 
00164 void local_init_pass1_df1b2variable::dot_calcs(local_dep_df1b2variable& v,int j)
00165 {
00166   if (adpool_stack_pointer<1)
00167   {
00168     cerr << "this can't happen" << endl;
00169     ad_exit(1);
00170   }
00171   int global_nvar=adpool_nvar_stack[adpool_stack_pointer-1];
00172   double * xd=xudot;
00173   double * ud=v.get_u_dot();
00174   double * zd=(v.p)->get_u_dot();
00175   for (int i=0;i<global_nvar;i++)
00176   {
00177     *zd+=ud[j] * *xd;
00178     zd++;
00179     xd++;
00180   }
00181 }
00182 
00187 void local_init_pass1_df1b2variable::allocate(void)
00188 {
00189   df1b2variable::allocate();
00190   *get_u()=xu;
00191 };
00192 
00197 int local_init_pass1_var::nvarcalc_all(void)
00198 {
00199   int n=0;
00200   for (int i=0;i<num_vars;i++)
00201   {
00202     n+=list[i]->nvar_calc();
00203   }
00204   return n;
00205 }
00206 
00211 local_init_pass1_df1b2variable::local_init_pass1_df1b2variable(double _xu,
00212   double* _xudot)
00213 {
00214   xu=_xu;
00215   *get_u()=_xu;
00216   xudot=_xudot;
00217 }