OdeSolution.cpp

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 
00030 #include "OdeSolution.hpp"
00031 
00032 unsigned OdeSolution::GetNumberOfTimeSteps()
00033 {
00034     return mNumberOfTimeSteps;
00035 }
00036 
00037 void OdeSolution::SetNumberOfTimeSteps(unsigned numTimeSteps)
00038 {
00039     mNumberOfTimeSteps = numTimeSteps;
00040     mTimes.reserve(numTimeSteps+1);
00041     mSolutions.reserve(numTimeSteps);
00042 }
00043 
00044 std::vector<double> OdeSolution::GetVariableAtIndex(unsigned index)
00045 {
00046     std::vector<double> answer;
00047     answer.reserve(mSolutions.size());
00048     for (unsigned i=0; i<mSolutions.size(); i++)
00049     {
00050         answer.push_back(mSolutions[i][index]);
00051     }
00052     return answer;
00053 }
00054 
00055 std::vector<double>& OdeSolution::rGetTimes()
00056 {
00057     return mTimes;
00058 }
00059 
00060 std::vector<std::vector<double> >& OdeSolution::rGetSolutions()
00061 {
00062     return mSolutions;
00063 }
00064 
00065 void OdeSolution::WriteToFile(std::string directoryName,
00066                               std::string baseResultsFilename,
00067                               AbstractOdeSystem* pOdeSystem,
00068                               std::string timeUnits,
00069                               unsigned stepsPerRow,
00070                               bool cleanDirectory)
00071 {
00072     assert(stepsPerRow > 0);
00073     assert(mTimes.size() > 0);
00074     assert(mTimes.size() == mSolutions.size());
00075 
00076     // Write data to a file using ColumnDataWriter
00077     ColumnDataWriter writer(directoryName, baseResultsFilename, cleanDirectory);
00078 
00079     int time_var_id = writer.DefineUnlimitedDimension("Time", timeUnits);
00080 
00081     // Either: the ODE system should have no names&units defined, or it should
00082     // the same number as the number of solutions per timestep.
00083     assert( pOdeSystem->rGetVariableNames().size()==0 ||
00084             (pOdeSystem->rGetVariableNames().size()==mSolutions[0].size()) );
00085 
00086     unsigned num_vars = mSolutions[0].size();
00087 
00088     std::vector<int> var_ids;
00089     var_ids.reserve(num_vars);
00090     if (pOdeSystem->rGetVariableNames().size() > 0)
00091     {
00092         for (unsigned i=0; i<num_vars; i++)
00093         {
00094             var_ids.push_back(writer.DefineVariable(pOdeSystem->rGetVariableNames()[i],
00095                                                     pOdeSystem->rGetVariableUnits()[i]));
00096         }
00097     }
00098     else
00099     {
00100         for (unsigned i=0; i<num_vars; i++)
00101         {
00102             std::stringstream string_stream;
00103             string_stream << "var_" << i;
00104             var_ids.push_back(writer.DefineVariable(string_stream.str(), ""));
00105         }
00106     }
00107 
00108     writer.EndDefineMode();
00109 
00110     for (unsigned i=0; i<mSolutions.size(); i+=stepsPerRow)
00111     {
00112         writer.PutVariable(time_var_id, mTimes[i]);
00113         for (unsigned j=0; j<var_ids.size(); j++)
00114         {
00115             writer.PutVariable(var_ids[j], mSolutions[i][j]);
00116         }
00117         writer.AdvanceAlongUnlimitedDimension();
00118     }
00119     writer.Close();
00120 }

Generated on Tue Aug 4 16:10:24 2009 for Chaste by  doxygen 1.5.5