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
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
00077 ColumnDataWriter writer(directoryName, baseResultsFilename, cleanDirectory);
00078
00079 int time_var_id = writer.DefineUnlimitedDimension("Time", timeUnits);
00080
00081
00082
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 }