00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00105 ColumnDataWriter writer(directoryName,baseResultsFilename,cleanDirectory);
00106
00107 int time_var_id = writer.DefineUnlimitedDimension("Time",timeUnits);
00108
00109
00110
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_