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

Generated on Mon Nov 1 12:35:24 2010 for Chaste by  doxygen 1.5.5