OdeSolution.cpp

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

Generated by  doxygen 1.6.2