Chaste  Release::2017.1
SteadyStateRunner.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "SteadyStateRunner.hpp"
37 
38 #ifdef CHASTE_CVODE
39 
41 {
42  // Get necessary things from stimulus current
43  boost::shared_ptr<RegularStimulus> p_reg_stim =
44  boost::static_pointer_cast<RegularStimulus>(mpModel->GetStimulusFunction());
45  const double pacing_cycle_length = p_reg_stim->GetPeriod(); //ms
46  double maximum_time_step = p_reg_stim->GetDuration(); // ms
47 
48  // Set up vectors to monitor progress
49  std::vector<double> old_state_vars;
50  std::vector<double> new_state_vars;
51  CopyToStdVector(mpModel->rGetStateVariables(),old_state_vars);
52 
53  mpModel->SetMaxSteps(1e5); // Per pace.
54 
55  /*
56  * This was an idea about solving roughly to start with and then refining later,
57  * but it overrides anything set by the user and is therefore a bit dangerous.
58  * It also didn't seem to make very much difference...
59  */
60  //bool rough_solution = true;
61  //mpModel->SetTolerances(1e-4,1e-6);
62 
63  // Use optimisations from #1912 - since we don't interfere with the model between Solve() calls don't reset.
64  mpModel->SetMinimalReset(true);
65 
66  const unsigned num_paces_to_analyse = (mTwoPaceScan) ? 2u : 1u;
67 
68  for (unsigned i=0; i<mMaxNumPaces; i=i+num_paces_to_analyse)
69  {
70  // Look at two paces in the hope of detecting alternans and still saying we're steady-ish.
71  mpModel->Solve((pacing_cycle_length)*(double)(i), (pacing_cycle_length)*(double)(i+num_paces_to_analyse), maximum_time_step);
72  this->mNumEvaluations += num_paces_to_analyse;
73  CopyToStdVector(mpModel->rGetStateVariables(),new_state_vars);
74 
75  // Calculate the change in the norm of the state variables
76  double temp = 0;
77  for (unsigned j=0; j<old_state_vars.size(); j++)
78  {
79  temp += fabs(new_state_vars[j] - old_state_vars[j]);
80  }
81 
82 // if (rough_solution && temp < 1e-4)
83 // {
84 // mpModel->SetTolerances(1e-6,1e-8);
85 // rough_solution = false;
86 // }
87 
88  if (temp < 1e-6)
89  {
90  break;
91  }
92 
93  old_state_vars = new_state_vars;
94  }
95 
96  // Since we don't know what we are going to do with it next re-set it on every solve call.
97  mpModel->SetMinimalReset(false);
98 }
99 
100 #endif // CHASTE_CVODE
101 
boost::shared_ptr< AbstractCvodeCell > mpModel
virtual void RunToSteadyStateImplementation()
void CopyToStdVector(const VECTOR &rSrc, std::vector< double > &rDest)