AbstractOneStepIvpOdeSolver.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 #include "AbstractOneStepIvpOdeSolver.hpp"
00030 #include "TimeStepper.hpp"
00031 #include <cmath>
00032 
00033 const double smidge = 1e-10;
00034 
00035 OdeSolution AbstractOneStepIvpOdeSolver::Solve(AbstractOdeSystem* pOdeSystem,
00036                                                std::vector<double>& rYValues,
00037                                                double startTime,
00038                                                double endTime,
00039                                                double timeStep,
00040                                                double timeSampling)
00041 {
00042     assert(rYValues.size()==pOdeSystem->GetNumberOfStateVariables());
00043     assert(endTime > startTime);
00044     assert(timeStep > 0.0);
00045     assert(timeSampling >= timeStep);
00046 
00047     mStoppingEventOccurred = false;
00048     if ( pOdeSystem->CalculateStoppingEvent(startTime, rYValues) == true )
00049     {
00050         EXCEPTION("(Solve with sampling) Stopping event is true for initial condition");
00051     }
00052     TimeStepper stepper(startTime, endTime, timeSampling);
00053 
00054     // setup solutions if output is required
00055     OdeSolution solutions;
00056     solutions.SetNumberOfTimeSteps(stepper.EstimateTimeSteps());
00057     solutions.rGetSolutions().push_back(rYValues);
00058     solutions.rGetTimes().push_back(startTime);
00059 
00060     mWorkingMemory.resize(rYValues.size());
00061 
00062     // Solve the ODE system
00063     while ( !stepper.IsTimeAtEnd() && !mStoppingEventOccurred )
00064     {
00065         InternalSolve(pOdeSystem, rYValues, mWorkingMemory, stepper.GetTime(), stepper.GetNextTime(), timeStep);
00066         stepper.AdvanceOneTimeStep();
00067         // write current solution into solutions
00068         solutions.rGetSolutions().push_back(rYValues);
00069         // Push back new time into the time solution vector
00070         if ( mStoppingEventOccurred )
00071         {
00072             solutions.rGetTimes().push_back(mStoppingTime);
00073         }
00074         else
00075         {
00076             solutions.rGetTimes().push_back(stepper.GetTime());
00077         }
00078     }
00079 
00080     // stepper.EstimateTimeSteps may have been an overestimate...
00081     solutions.SetNumberOfTimeSteps(stepper.GetTimeStepsElapsed());
00082     return solutions;
00083 }
00084 
00085 void AbstractOneStepIvpOdeSolver::Solve(AbstractOdeSystem* pOdeSystem,
00086                                         std::vector<double>& rYValues,
00087                                         double startTime,
00088                                         double endTime,
00089                                         double timeStep)
00090 {
00091     assert(rYValues.size()==pOdeSystem->GetNumberOfStateVariables());
00092     assert(endTime > startTime);
00093     assert(timeStep > 0.0);
00094 
00095     mStoppingEventOccurred = false;
00096     if ( pOdeSystem->CalculateStoppingEvent(startTime, rYValues) == true )
00097     {
00098         EXCEPTION("(Solve without sampling) Stopping event is true for initial condition");
00099     }
00100 
00101     // Perhaps resize working memory
00102     mWorkingMemory.resize(rYValues.size());
00103     // And solve...
00104     InternalSolve(pOdeSystem, rYValues, mWorkingMemory, startTime, endTime, timeStep);
00105 }
00106 
00107 void AbstractOneStepIvpOdeSolver::InternalSolve(AbstractOdeSystem* pOdeSystem,
00108                                                 std::vector<double>& rYValues,
00109                                                 std::vector<double>& rWorkingMemory,
00110                                                 double startTime,
00111                                                 double endTime,
00112                                                 double timeStep)
00113 {
00114     TimeStepper stepper(startTime, endTime, timeStep);
00115     // Solve the ODE system
00116 
00117     // Which of our vectors holds the current solution?
00118     // If this is true, it's in rYValues, otherwise it's in rWorkingMemory.
00119     bool curr_is_curr = false;
00120 
00121     // should never get here if this bool has been set to true;
00122     assert(!mStoppingEventOccurred);
00123     while ( !stepper.IsTimeAtEnd() && !mStoppingEventOccurred )
00124     {
00125         curr_is_curr = not curr_is_curr;
00126         // Function that calls the appropriate one-step solver
00127         CalculateNextYValue(pOdeSystem,
00128                             stepper.GetNextTimeStep(),
00129                             stepper.GetTime(),
00130                             curr_is_curr ? rYValues : rWorkingMemory,
00131                             curr_is_curr ? rWorkingMemory : rYValues);
00132         stepper.AdvanceOneTimeStep();
00133         if ( pOdeSystem->CalculateStoppingEvent(stepper.GetTime(),
00134                                                 curr_is_curr ? rWorkingMemory : rYValues) == true )
00135         {
00136             mStoppingTime = stepper.GetTime();
00137             mStoppingEventOccurred = true;
00138         }
00139     }
00140     // Final answer must be in rYValues
00141     if (curr_is_curr)
00142     {
00143         rYValues.assign(rWorkingMemory.begin(), rWorkingMemory.end());
00144     }
00145 }

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