Chaste Release::3.1
SteadyStateRunner.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "SteadyStateRunner.hpp"
00037 
00038 #ifdef CHASTE_CVODE
00039 
00040 void SteadyStateRunner::RunToSteadyStateImplementation()
00041 {
00042     // Get necessary things from stimulus current
00043     boost::shared_ptr<RegularStimulus> p_reg_stim =
00044                          boost::static_pointer_cast<RegularStimulus>(mpModel->GetStimulusFunction());
00045     const double pacing_cycle_length = p_reg_stim->GetPeriod(); //ms
00046     double maximum_time_step = p_reg_stim->GetDuration();  // ms
00047 
00048     // Set up vectors to monitor progress
00049     std::vector<double> old_state_vars;
00050     std::vector<double> new_state_vars;
00051     CopyToStdVector(mpModel->rGetStateVariables(),old_state_vars);
00052 
00053     mpModel->SetMaxSteps(1e5); // Per pace.
00054 
00055     bool rough_solution = true;
00056     //mpModel->SetTolerances(1e-4,1e-6);
00057 
00058     // Use optimisations from #1912 - since we don't interfere with the model between Solve() calls don't reset.
00059     mpModel->SetAutoReset(false);
00060 
00061     const unsigned num_paces_to_analyse = (mTwoPaceScan) ? 2u : 1u;
00062     const unsigned max_evaluations = (unsigned)(mMaxNumPaces/num_paces_to_analyse); // Only allow 10000 paces at the moment.
00063     for (unsigned i=0; i<max_evaluations; i++)
00064     {
00065         // Look at two paces in the hope of detecting alternans and still saying we're steady-ish.
00066         mpModel->Solve((pacing_cycle_length)*(double)(i), (pacing_cycle_length)*(double)(i+num_paces_to_analyse), maximum_time_step);
00067         this->mNumEvaluations += num_paces_to_analyse;
00068         CopyToStdVector(mpModel->rGetStateVariables(),new_state_vars);
00069 
00070         // Calculate the change in the norm of the state variables
00071         double temp = 0;
00072         for (unsigned i=0; i<old_state_vars.size(); i++)
00073         {
00074             temp += fabs(new_state_vars[i] - old_state_vars[i]);
00075         }
00076 
00077         if (rough_solution && temp < 1e-4)
00078         {
00079             mpModel->SetTolerances(1e-6,1e-8);
00080             rough_solution = false;
00081         }
00082 
00083         if (temp < 1e-6)
00084         {
00085             break;
00086         }
00087 
00088         old_state_vars = new_state_vars;
00089     }
00090 
00091     // Since we don't know what we are going to do with it next re-set it on every solve call.
00092     mpModel->SetAutoReset(true);
00093 }
00094 
00095 #endif // CHASTE_CVODE
00096