OdeSolution.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 
00034 #ifndef _ODESOLUTION_HPP_
00035 #define _ODESOLUTION_HPP_
00036 
00037 #include <vector>
00038 #include <fstream>
00039 #include <cassert>
00040 #include "ColumnDataWriter.hpp"
00041 #include "AbstractOdeSystem.hpp"
00042 #include <sstream>
00043 
00044 class OdeSolution
00045 {
00046 private:
00047     unsigned mNumberOfTimeSteps;    
00049     std::vector<double> mTimes; 
00050     std::vector<std::vector<double> > mSolutions;  
00052 public:
00053 
00054     unsigned GetNumberOfTimeSteps(void)
00055     {
00056         return mNumberOfTimeSteps;
00057     }
00058 
00059     void SetNumberOfTimeSteps(unsigned num_timesteps)
00060     {
00061         mNumberOfTimeSteps = num_timesteps;
00062         mTimes.reserve(num_timesteps+1);
00063         mSolutions.reserve(num_timesteps+1);
00064     }
00065 
00066     std::vector<double> GetVariableAtIndex(unsigned index)
00067     {
00068         std::vector<double> answer;
00069         answer.reserve(mSolutions.size());
00070         for (unsigned i=0; i<mSolutions.size(); i++)
00071         {
00072             answer.push_back(mSolutions[i][index]);
00073         }
00074         return answer;
00075     }
00076 
00077     std::vector<double>& rGetTimes()
00078     {
00079         return mTimes;
00080     }
00081 
00082     std::vector<std::vector<double> >& rGetSolutions()
00083     {
00084         return mSolutions;
00085     }
00086 
00087 
00093     void WriteToFile(std::string directoryName,
00094                      std::string baseResultsFilename,
00095                      AbstractOdeSystem* pOdeSystem,
00096                      std::string timeUnits,
00097                      unsigned stepPerRow = 1,
00098                      bool cleanDirectory = true)
00099     {
00100         assert(stepPerRow > 0);
00101         assert(mTimes.size()>0);
00102         assert(mTimes.size()==mSolutions.size());
00103 
00104         // Write data to a file using ColumnDataWriter
00105         ColumnDataWriter writer(directoryName,baseResultsFilename,cleanDirectory);
00106 
00107         int time_var_id = writer.DefineUnlimitedDimension("Time",timeUnits);
00108 
00109         // Either: the ODE system should have no names&units defined, or it should
00110         // the same number as the number of solutions per timestep.
00111         assert( pOdeSystem->rGetVariableNames().size()==0 ||
00112                 (pOdeSystem->rGetVariableNames().size()==mSolutions[0].size()) );
00113 
00114         unsigned num_vars = mSolutions[0].size();
00115 
00116         std::vector<int> var_ids;
00117         var_ids.reserve(num_vars);
00118         if(pOdeSystem->rGetVariableNames().size() > 0)
00119         {
00120             for (unsigned i=0; i<num_vars; i++)
00121             {
00122                 var_ids.push_back(writer.DefineVariable(pOdeSystem->rGetVariableNames()[i],
00123                                                         pOdeSystem->rGetVariableUnits()[i]));
00124             }
00125         }
00126         else
00127         {
00128             for (unsigned i=0; i<num_vars; i++)
00129             {
00130                 std::stringstream string_stream;
00131                 string_stream << "var_"<< i;
00132                 var_ids.push_back(writer.DefineVariable(string_stream.str(),""));
00133             }
00134         }
00135 
00136         writer.EndDefineMode();
00137 
00138         for (unsigned i=0; i<mSolutions.size(); i+=stepPerRow)
00139         {
00140             writer.PutVariable(time_var_id, mTimes[i]);
00141             for (unsigned j=0; j<var_ids.size(); j++)
00142             {
00143                 writer.PutVariable(var_ids[j], mSolutions[i][j]);
00144             }
00145             writer.AdvanceAlongUnlimitedDimension();
00146         }
00147         writer.Close();
00148     }
00149 };
00150 
00151 #endif //_ODESOLUTION_HPP_

Generated on Wed Mar 18 12:51:56 2009 for Chaste by  doxygen 1.5.5