Object-oriented Scientific Computing Library: Version 0.910
ode_iv_table.h
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006-2012, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 #ifndef O2SCL_ODE_IV_TABLE_H
00024 #define O2SCL_ODE_IV_TABLE_H
00025 
00026 #include <o2scl/ovector_tlate.h>
00027 #include <o2scl/omatrix_tlate.h>
00028 #include <o2scl/adapt_step.h>
00029 #include <o2scl/gsl_astep.h>
00030 #include <o2scl/ode_iv_solve.h>
00031 
00032 #ifndef DOXYGENP
00033 namespace o2scl {
00034 #endif
00035 
00036   /** \brief Solve an initial-value ODE problem and store
00037       the result in a \ref table object
00038       
00039       This class is experimental.
00040   */
00041   template<class func_t=ode_funct<>, 
00042     class vec_t=ovector_base, class alloc_vec_t=ovector, 
00043     class alloc_t=ovector_alloc> class ode_iv_table : 
00044   public ode_iv_solve<func_t,vec_t,alloc_vec_t,alloc_t> {
00045     
00046     public:
00047 
00048     /// Desc
00049     int solve_grid_table(size_t n, vec_t &ystart, table &t, std::string x_col, 
00050                          std::string y_prefix, std::string dydx_prefix, 
00051                          std::string yerr_prefix, func_t &derivs) {
00052       std::string cname;
00053 
00054       size_t n_sol=t.get_nlines();
00055       double x0=t.get(x_col,0);
00056       double x1=t.get(x_col,n_sol-1);
00057       double h=t.get(x_col,1)-x0;
00058 
00059       // Allocate vectors and matrices for solve_grid()
00060       alloc_vec_t x_sol;
00061       this->ao.allocate(x_sol,n_sol);
00062       omatrix ysol(n_sol,n), dydx_sol(n_sol,n), yerr_sol(n_sol,n);
00063 
00064       // Copy over x_grid from table
00065       ovector_base &ob=t.get_column(x_col);
00066       vector_copy(n_sol,x_col,x_sol);
00067 
00068       // Perform solution
00069       this->template solve_grid<omatrix,omatrix_row>
00070       (x0,x1,h,n,ystart,n_sol,x_sol,ysol,dydx_sol,yerr_sol,derivs);
00071 
00072       // Pointers to columns in table
00073       std::vector<ovector_base *> yt, dyt, errt;
00074 
00075       // Create new columns if necessary and get pointers
00076       for(size_t i=0;i<n;i++) {
00077 
00078         cname=y_prefix+itos(i);
00079         if (!t.is_column(cname)) t.new_column(cname);
00080         yt.push_back(&t.get_column(cname));
00081 
00082         cname=dydx_prefix+itos(i);
00083         if (!t.is_column(cname)) t.new_column(cname);
00084         dyt.push_back(&t.get_column(cname));
00085 
00086         cname=yerr_prefix+itos(i);
00087         if (!t.is_column(cname)) t.new_column(cname);
00088         errt.push_back(&t.get_column(cname));
00089 
00090         // Now copy data over
00091         for(size_t j=0;j<n_sol;j++) {
00092           (*yt[i])[j]=ysol[j][i];
00093           (*dyt[i])[j]=dydx_sol[j][i];
00094           (*errt[i])[j]=yerr_sol[j][i];
00095         }
00096 
00097       }
00098 
00099       return 0;
00100     }
00101 
00102     /// Desc
00103     int solve_store_table(double x0, double x1, double h, size_t n, 
00104                           vec_t &ystart, size_t &n_sol, table &t, 
00105                           std::string x_col, std::string y_prefix,
00106                           std::string dydx_prefix, std::string yerr_prefix, 
00107                           func_t &derivs) {
00108 
00109       std::string cname;
00110       if (t.get_nlines()<n_sol) t.set_nlines(n_sol);
00111       
00112       // Allocate vectors and matrices for solve_store()
00113       alloc_vec_t x_sol;
00114       this->ao.allocate(x_sol,n_sol);
00115       omatrix ysol(n_sol,n), dydx_sol(n_sol,n), yerr_sol(n_sol,n);
00116 
00117       // Perform solution
00118       this->template solve_store<omatrix,omatrix_row>
00119       (x0,x1,h,n,ystart,n_sol,x_sol,ysol,dydx_sol,yerr_sol,derivs);
00120 
00121       // Pointers to columns in table
00122       std::vector<ovector_base *> yt, dyt, errt;
00123 
00124       if (!t.is_column(x_col)) t.new_column(x_col);
00125       ovector_base &x_vec=t.get_column(x_col);
00126 
00127       // Create new columns if necessary and get pointers
00128       for(size_t i=0;i<n;i++) {
00129 
00130         cname=y_prefix+itos(i);
00131         if (!t.is_column(cname)) t.new_column(cname);
00132         yt.push_back(&t.get_column(cname));
00133 
00134         cname=dydx_prefix+itos(i);
00135         if (!t.is_column(cname)) t.new_column(cname);
00136         dyt.push_back(&t.get_column(cname));
00137 
00138         cname=yerr_prefix+itos(i);
00139         if (!t.is_column(cname)) t.new_column(cname);
00140         errt.push_back(&t.get_column(cname));
00141 
00142         // Now copy data over
00143         for(size_t j=0;j<n_sol;j++) {
00144           if (i==0) x_vec[j]=x_sol[j];
00145           (*yt[i])[j]=ysol[j][i];
00146           (*dyt[i])[j]=dydx_sol[j][i];
00147           (*errt[i])[j]=yerr_sol[j][i];
00148         }
00149 
00150       }
00151 
00152       return 0;
00153     }
00154     
00155   };
00156 
00157 #ifndef DOXYGENP
00158 }
00159 #endif
00160 
00161 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).

Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads.