text: /*
  
  Copyright (c) 2005-2022, University of Oxford.
  All rights reserved.
  
  University of Oxford means the Chancellor, Masters and Scholars of the
  University of Oxford, having an administrative office at Wellington
  Square, Oxford OX1 2JD, UK.
  
  This file is part of Chaste.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.
   * Neither the name of the University of Oxford nor the names of its
     contributors may be used to endorse or promote products derived from this
     software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
  */
  
  #include "AbstractOneStepIvpOdeSolver.hpp"
  #include "TimeStepper.hpp"
  #include "Exception.hpp"
  #include <cmath>
  
  OdeSolution AbstractOneStepIvpOdeSolver::Solve(AbstractOdeSystem* pOdeSystem,
                                                 std::vector<double>& rYValues,
                                                 double startTime,
                                                 double endTime,
                                                 double timeStep,
                                                 double timeSampling)
  {
      assert(rYValues.size()==pOdeSystem->GetNumberOfStateVariables());
      assert(endTime > startTime);
      assert(timeStep > 0.0);
      assert(timeSampling >= timeStep);
  
      mStoppingEventOccurred = false;
      if (pOdeSystem->CalculateStoppingEvent(startTime, rYValues) == true)
      {
          EXCEPTION("(Solve with sampling) Stopping event is true for initial condition");
      }
      TimeStepper stepper(startTime, endTime, timeSampling);
  
      // setup solutions if output is required
      OdeSolution solutions;
      solutions.SetNumberOfTimeSteps(stepper.EstimateTimeSteps());
      solutions.rGetSolutions().push_back(rYValues);
      solutions.rGetTimes().push_back(startTime);
      solutions.SetOdeSystemInformation(pOdeSystem->GetSystemInformation());
      solutions.SetSolverName( GetIdentifier() );
  
      mWorkingMemory.resize(rYValues.size());
  
      // Solve the ODE system
      while ( !stepper.IsTimeAtEnd() && !mStoppingEventOccurred )
      {
          InternalSolve(pOdeSystem, rYValues, mWorkingMemory, stepper.GetTime(), stepper.GetNextTime(), timeStep);
          stepper.AdvanceOneTimeStep();
          // write current solution into solutions
          solutions.rGetSolutions().push_back(rYValues);
          // Push back new time into the time solution vector
          if (mStoppingEventOccurred)
          {
              solutions.rGetTimes().push_back(mStoppingTime);
          }
          else
          {
              solutions.rGetTimes().push_back(stepper.GetTime());
          }
      }
  
      // stepper.EstimateTimeSteps may have been an overestimate...
      solutions.SetNumberOfTimeSteps(stepper.GetTotalTimeStepsTaken());
      return solutions;
  }
  
  void AbstractOneStepIvpOdeSolver::Solve(AbstractOdeSystem* pOdeSystem,
                                          std::vector<double>& rYValues,
                                          double startTime,
                                          double endTime,
                                          double timeStep)
  {
      assert(rYValues.size()==pOdeSystem->GetNumberOfStateVariables());
      assert(endTime > startTime);
      assert(timeStep > 0.0);
  
      mStoppingEventOccurred = false;
      if (pOdeSystem->CalculateStoppingEvent(startTime, rYValues) == true)
      {
          EXCEPTION("(Solve without sampling) Stopping event is true for initial condition");
      }
  
      // Perhaps resize working memory
      mWorkingMemory.resize(rYValues.size());
      // And solve...
      InternalSolve(pOdeSystem, rYValues, mWorkingMemory, startTime, endTime, timeStep);
  }
  
  void AbstractOneStepIvpOdeSolver::InternalSolve(AbstractOdeSystem* pOdeSystem,
                                                  std::vector<double>& rYValues,
                                                  std::vector<double>& rWorkingMemory,
                                                  double startTime,
                                                  double endTime,
                                                  double timeStep)
  {
      TimeStepper stepper(startTime, endTime, timeStep);
      // Solve the ODE system
  
      // Which of our vectors holds the current solution?
      // If this is true, it's in rYValues, otherwise it's in rWorkingMemory.
      bool curr_is_curr = false;
  
      // should never get here if this bool has been set to true;
      assert(!mStoppingEventOccurred);
      while ( !stepper.IsTimeAtEnd() && !mStoppingEventOccurred )
      {
          curr_is_curr = !curr_is_curr;
          // Function that calls the appropriate one-step solver
          CalculateNextYValue(pOdeSystem,
                              stepper.GetNextTimeStep(),
                              stepper.GetTime(),
                              curr_is_curr ? rYValues : rWorkingMemory,
                              curr_is_curr ? rWorkingMemory : rYValues);
          stepper.AdvanceOneTimeStep();
          if (pOdeSystem->CalculateStoppingEvent(stepper.GetTime(),
                                                  curr_is_curr ? rWorkingMemory : rYValues) == true)
          {
              mStoppingTime = stepper.GetTime();
              mStoppingEventOccurred = true;
          }
      }
      // Final answer must be in rYValues
      if (curr_is_curr)
      {
          rYValues.assign(rWorkingMemory.begin(), rWorkingMemory.end());
      }
  }
  