SteadyStateRunner.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, 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     /*
00056      * This was an idea about solving roughly to start with and then refining later,
00057      * but it overrides anything set by the user and is therefore a bit dangerous.
00058      * It also didn't seem to make very much difference...
00059      */
00060     //bool rough_solution = true;
00061     //mpModel->SetTolerances(1e-4,1e-6);
00062 
00063     // Use optimisations from #1912 - since we don't interfere with the model between Solve() calls don't reset.
00064     mpModel->SetMinimalReset(true);
00065 
00066     const unsigned num_paces_to_analyse = (mTwoPaceScan) ? 2u : 1u;
00067 
00068     for (unsigned i=0; i<mMaxNumPaces; i=i+num_paces_to_analyse)
00069     {
00070         // Look at two paces in the hope of detecting alternans and still saying we're steady-ish.
00071         mpModel->Solve((pacing_cycle_length)*(double)(i), (pacing_cycle_length)*(double)(i+num_paces_to_analyse), maximum_time_step);
00072         this->mNumEvaluations += num_paces_to_analyse;
00073         CopyToStdVector(mpModel->rGetStateVariables(),new_state_vars);
00074 
00075         // Calculate the change in the norm of the state variables
00076         double temp = 0;
00077         for (unsigned j=0; j<old_state_vars.size(); j++)
00078         {
00079             temp += fabs(new_state_vars[j] - old_state_vars[j]);
00080         }
00081 
00082 //        if (rough_solution && temp < 1e-4)
00083 //        {
00084 //            mpModel->SetTolerances(1e-6,1e-8);
00085 //            rough_solution = false;
00086 //        }
00087 
00088         if (temp < 1e-6)
00089         {
00090             break;
00091         }
00092 
00093         old_state_vars = new_state_vars;
00094     }
00095 
00096     // Since we don't know what we are going to do with it next re-set it on every solve call.
00097     mpModel->SetMinimalReset(false);
00098 }
00099 
00100 #endif // CHASTE_CVODE
00101 

Generated by  doxygen 1.6.2