OdeSolution.cpp
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 #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
00091 ColumnDataWriter writer(directoryName, baseResultsFilename, cleanDirectory, precision);
00092
00093 if (!PetscTools::AmMaster())
00094 {
00095
00096 return;
00097 }
00098
00099 int time_var_id = writer.DefineUnlimitedDimension("Time", timeUnits);
00100
00101
00102
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 }