Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
SteadyStateRunner.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "SteadyStateRunner.hpp"
37#include "ZeroStimulus.hpp"
38
39#ifdef CHASTE_CVODE
40
42{
43 // Get necessary things from stimulus current
44 boost::shared_ptr<RegularStimulus> p_reg_stim = boost::static_pointer_cast<RegularStimulus>(mpModel->GetStimulusFunction());
45 boost::shared_ptr<ZeroStimulus> p_zero_stim(new ZeroStimulus);
46 const double pacing_cycle_length = p_reg_stim->GetPeriod(); // ms
47 double stimulus_duration = p_reg_stim->GetDuration(); // ms
48 double stimulus_start_time = p_reg_stim->GetStartTime(); // ms
49 double stimulus_end_time = stimulus_start_time + stimulus_duration;
50 double maximum_time_step = pacing_cycle_length; // ms
51
52 bool force_reset_setting = mpModel->GetForceReset();
53 bool minimal_reset_setting = mpModel->GetMinimalReset();
54
55 mpModel->SetMaxSteps(1e5); // Per pace (or part of pace with stimulus start/stop).
56 mpModel->SetForceReset(true); // Best way to deal with discontinuities in the RHS according to CVODE
57
58 // Set up vectors to monitor progress
59 std::vector<double> old_state_vars;
60 std::vector<double> new_state_vars;
61 CopyToStdVector(mpModel->rGetStateVariables(), old_state_vars);
62
63 const unsigned num_paces_to_analyse = (mTwoPaceScan) ? 2u : 1u;
64 for (unsigned i = 0; i < mMaxNumPaces; i = i + num_paces_to_analyse)
65 {
66 // Look at two paces in the hope of detecting alternans and still saying we're steady-ish.
67 for (unsigned j = 0; j < num_paces_to_analyse; j++)
68 {
69 // Pre-stimulus (can skip if stimulus is applied at t=0)
70 if (stimulus_start_time > 0)
71 {
72 mpModel->SetMinimalReset(true); // We are just carrying on from last stop (not on stimulus) so don't need a solver reset.
73 mpModel->SetStimulusFunction(p_zero_stim);
74 mpModel->Solve((pacing_cycle_length) * (double)(i + j), (pacing_cycle_length) * (double)(i + j) + stimulus_start_time, maximum_time_step);
75 mpModel->SetForceReset(true); // Reset every solve call after this (next two) to deal with discontinuities in stimulus.
76 }
77
78 mpModel->SetStimulusFunction(p_reg_stim); // RegularStimulus applies at stimulus_start_time<=t<=stimulus_end_time (includes bounds).
79 mpModel->Solve((pacing_cycle_length) * (double)(i + j) + stimulus_start_time, (pacing_cycle_length) * (double)(i + j) + stimulus_end_time, maximum_time_step);
80
81 // Post-stimulus
82 mpModel->SetStimulusFunction(p_zero_stim); // added this because the RegularStimulus behaviour is to apply the stimulus at t=stimulus_end_time, which might confuse matters.
83 mpModel->Solve((pacing_cycle_length) * (double)(i + j) + stimulus_end_time, (pacing_cycle_length) * (double)(i + j + 1), maximum_time_step);
84 }
85
86 this->mNumEvaluations += num_paces_to_analyse;
87 CopyToStdVector(mpModel->rGetStateVariables(), new_state_vars);
88
89 // Calculate the change in the norm of the state variables
90 double temp = 0;
91 for (unsigned j = 0; j < old_state_vars.size(); j++)
92 {
93 temp += fabs(new_state_vars[j] - old_state_vars[j]);
94 }
95
96 if (temp < 1e-6)
97 {
98 break; // say we are converged enough to steady state to stop here.
99 }
100
101 old_state_vars = new_state_vars;
102 }
103
104 // Reset stimulus to normal
105 mpModel->SetStimulusFunction(p_reg_stim);
106 mpModel->SetForceReset(force_reset_setting);
107 mpModel->SetMinimalReset(minimal_reset_setting);
108}
109
110#endif // CHASTE_CVODE
void CopyToStdVector(const VECTOR &rSrc, std::vector< double > &rDest)
boost::shared_ptr< AbstractCvodeCell > mpModel
virtual void RunToSteadyStateImplementation()