Chaste Commit::29c3c4ab2bee00ddd311a035a1e522c03cbe928a
SteadyStateRunner.cpp
1/*
2
3Copyright (c) 2005-2026, 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
40{
41 // Get necessary things from stimulus current
42 boost::shared_ptr<RegularStimulus> p_reg_stim = boost::static_pointer_cast<RegularStimulus>(mpModel->GetStimulusFunction());
43 boost::shared_ptr<ZeroStimulus> p_zero_stim(new ZeroStimulus);
44 const double pacing_cycle_length = p_reg_stim->GetPeriod(); // ms
45 double stimulus_duration = p_reg_stim->GetDuration(); // ms
46 double stimulus_start_time = p_reg_stim->GetStartTime(); // ms
47 double stimulus_end_time = stimulus_start_time + stimulus_duration;
48 double maximum_time_step = pacing_cycle_length; // ms
49
50 bool force_reset_setting = mpModel->GetForceReset();
51 bool minimal_reset_setting = mpModel->GetMinimalReset();
52
53 mpModel->SetMaxSteps(1e5); // Per pace (or part of pace with stimulus start/stop).
54 mpModel->SetForceReset(true); // Best way to deal with discontinuities in the RHS according to CVODE
55
56 // Set up vectors to monitor progress
57 std::vector<double> old_state_vars;
58 std::vector<double> new_state_vars;
59 CopyToStdVector(mpModel->rGetStateVariables(), old_state_vars);
60
61 const unsigned num_paces_to_analyse = (mTwoPaceScan) ? 2u : 1u;
62 for (unsigned i = 0; i < mMaxNumPaces; i = i + num_paces_to_analyse)
63 {
64 // Look at two paces in the hope of detecting alternans and still saying we're steady-ish.
65 for (unsigned j = 0; j < num_paces_to_analyse; j++)
66 {
67 // Pre-stimulus (can skip if stimulus is applied at t=0)
68 if (stimulus_start_time > 0)
69 {
70 mpModel->SetMinimalReset(true); // We are just carrying on from last stop (not on stimulus) so don't need a solver reset.
71 mpModel->SetStimulusFunction(p_zero_stim);
72 mpModel->Solve((pacing_cycle_length) * (double)(i + j), (pacing_cycle_length) * (double)(i + j) + stimulus_start_time, maximum_time_step);
73 mpModel->SetForceReset(true); // Reset every solve call after this (next two) to deal with discontinuities in stimulus.
74 }
75
76 mpModel->SetStimulusFunction(p_reg_stim); // RegularStimulus applies at stimulus_start_time<=t<=stimulus_end_time (includes bounds).
77 mpModel->Solve((pacing_cycle_length) * (double)(i + j) + stimulus_start_time, (pacing_cycle_length) * (double)(i + j) + stimulus_end_time, maximum_time_step);
78
79 // Post-stimulus
80 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.
81 mpModel->Solve((pacing_cycle_length) * (double)(i + j) + stimulus_end_time, (pacing_cycle_length) * (double)(i + j + 1), maximum_time_step);
82 }
83
84 this->mNumEvaluations += num_paces_to_analyse;
85 CopyToStdVector(mpModel->rGetStateVariables(), new_state_vars);
86
87 // Calculate the change in the norm of the state variables
88 double temp = 0;
89 for (unsigned j = 0; j < old_state_vars.size(); j++)
90 {
91 temp += fabs(new_state_vars[j] - old_state_vars[j]);
92 }
93
94 if (temp < 1e-6)
95 {
96 break; // say we are converged enough to steady state to stop here.
97 }
98
99 old_state_vars = new_state_vars;
100 }
101
102 // Reset stimulus to normal
103 mpModel->SetStimulusFunction(p_reg_stim);
104 mpModel->SetForceReset(force_reset_setting);
105 mpModel->SetMinimalReset(minimal_reset_setting);
106}
void CopyToStdVector(const VECTOR &rSrc, std::vector< double > &rDest)
boost::shared_ptr< AbstractCvodeCell > mpModel
virtual void RunToSteadyStateImplementation()