Chaste Commit::fa89f2b838c1edb21a1eaec92ee3a2eacc9255dd
AbstractPurkinjeCellFactory.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
37#include "AbstractPurkinjeCellFactory.hpp"
38#include "PurkinjeVentricularJunctionStimulus.hpp"
39#include "MultiStimulus.hpp"
40#include "HeartConfig.hpp"
41#include "Warnings.hpp"
42
43template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
45 : AbstractCardiacCellFactory<ELEMENT_DIM,SPACE_DIM>(),
46 mpMixedDimensionMesh(NULL)
47{
48}
49
50template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
52{
53 std::string pvj_file_name;
54 bool file_specified = true;
55
56 try
57 {
58 // HeartConfig::Instance()->GetMeshName() will throw an exception if no mesh name is defined
59 pvj_file_name = HeartConfig::Instance()->GetMeshName() + ".pvj";
60 }
61 catch(Exception&)
62 {
63 file_specified = false;
64 }
65
66 FileFinder junction_file(pvj_file_name, RelativeTo::AbsoluteOrCwd);
67 if (!file_specified || !junction_file.Exists() )
68 {
69 // In this case we expect the user to specify PVJ programmatically, so return immediately.
70 WARNING("No Purkinje-Ventricular junction (.pvj) file found. Junctions must be specified manually.");
71 return;
72 }
73
74 std::ifstream junction_stream(junction_file.GetAbsolutePath().c_str());
75
76 // Under normal circumstances, if the file exists pvj then it ought to readable
77 assert(junction_stream.good());
78 //if (!junction_stream.good())
79 //{ // file couldn't be opened
80 // EXCEPTION("Couldn't open data file: " << junction_file.GetAbsolutePath());
81 //}
82
83 // Reads in file defining nodes and resistance (separated by space)
84 while (junction_stream.good())
85 {
86 std::string this_line;
87 getline(junction_stream, this_line);
88
89 if (this_line=="" || this_line=="\r")
90 {
91 if (junction_stream.eof())
92 { // If the blank line is the last line carry on OK.
93 break;
94 }
95 else
96 {
97 EXCEPTION("No data found on line in file: " << junction_file.GetAbsolutePath());
98 }
99 }
100 std::stringstream line(this_line);
101
102 unsigned node_id;
103 line >> node_id;
104 double resistance;
105 line >> resistance;
106
107 if (mpMixedDimensionMesh->rGetNodePermutation().size() != 0) //Do we have a permuted mesh?
108 {
109 unsigned mapped_node_id = mpMixedDimensionMesh->rGetNodePermutation()[node_id];
110
111 mJunctionMap[mapped_node_id] = resistance;
112 }
113 else
114 {
115 mJunctionMap[node_id] = resistance;
116 }
117 }
118}
119
120template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
122 AbstractCardiacCellInterface* pPurkinjeCell,
123 AbstractCardiacCellInterface* pCardiacCell,
124 double resistance)
125{
126 // Figure out the effective resistance for this mesh, in kOhm.cm^3
127 if (pNode) // Should always be provided apart from low-level tests!
128 {
129 assert(mpMixedDimensionMesh);
130 typedef typename MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>::CableRangeAtNode CableRangeAtNode;
131 CableRangeAtNode cable_range = mpMixedDimensionMesh->GetCablesAtNode(pNode);
132 double total_cross_sectional_area = 0.0;
133 for (typename MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>::NodeCableIterator iter=cable_range.first;
134 iter != cable_range.second;
135 ++iter)
136 {
137 Element<1u,SPACE_DIM>* p_cable = iter->second;
138 double cable_radius = p_cable->GetAttribute();
139 total_cross_sectional_area += M_PI*cable_radius*cable_radius;
140 }
141 resistance *= total_cross_sectional_area / HeartConfig::Instance()->GetPurkinjeSurfaceAreaToVolumeRatio();
142 }
143 // Create the junction stimuli, and associate them with the cells
144 boost::shared_ptr<PurkinjeVentricularJunctionStimulus> p_pvj_ventricular_stim(new PurkinjeVentricularJunctionStimulus(resistance));
145 boost::shared_ptr<PurkinjeVentricularJunctionStimulus> p_pvj_purkinje_stim(new PurkinjeVentricularJunctionStimulus(resistance));
146 p_pvj_purkinje_stim->SetAppliedToPurkinjeCellModel();
147 p_pvj_ventricular_stim->SetVentricularCellModel(pCardiacCell);
148 p_pvj_ventricular_stim->SetPurkinjeCellModel(pPurkinjeCell);
149 p_pvj_purkinje_stim->SetVentricularCellModel(pCardiacCell);
150 p_pvj_purkinje_stim->SetPurkinjeCellModel(pPurkinjeCell);
151
152 // Create new combined stimuli which add the junction stimuli to those already in the cells
153 boost::shared_ptr<MultiStimulus> p_multi_stim_ventricular(new MultiStimulus);
154 p_multi_stim_ventricular->AddStimulus(p_pvj_ventricular_stim);
155 p_multi_stim_ventricular->AddStimulus(pCardiacCell->GetStimulusFunction());
156 pCardiacCell->SetStimulusFunction(p_multi_stim_ventricular);
157
158 boost::shared_ptr<MultiStimulus> p_multi_stim_purkinje(new MultiStimulus);
159 p_multi_stim_purkinje->AddStimulus(p_pvj_purkinje_stim);
160 p_multi_stim_purkinje->AddStimulus(pPurkinjeCell->GetStimulusFunction());
161 pPurkinjeCell->SetStimulusFunction(p_multi_stim_purkinje);
162}
163
164template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
166{
167 mpMixedDimensionMesh = dynamic_cast<MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>*>(pMesh);
168 if (mpMixedDimensionMesh == NULL)
169 {
170 EXCEPTION("AbstractPurkinjeCellFactory must take a MixedDimensionMesh");
171 }
172 mLocalPurkinjeNodes.clear();
173 for (typename MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>::CableElementIterator iter = mpMixedDimensionMesh->GetCableElementIteratorBegin();
174 iter != mpMixedDimensionMesh->GetCableElementIteratorEnd();
175 ++iter)
176 {
177 mLocalPurkinjeNodes.insert((*iter)->GetNodeGlobalIndex(0u));
178 mLocalPurkinjeNodes.insert((*iter)->GetNodeGlobalIndex(1u));
179 }
181
182 ReadJunctionsFile();
183}
184
185template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
187 Node<SPACE_DIM>* pNode,
188 AbstractCardiacCellInterface* pCardiacCell)
189{
190 unsigned node_index = pNode->GetIndex();
191 if (mLocalPurkinjeNodes.count(node_index)>0)
192 {
193 return CreatePurkinjeCellForTissueNode(pNode, pCardiacCell);
194 }
195 else
196 {
197 return new FakeBathCell(this->mpSolver, this->mpZeroStimulus);
198 }
199}
200
201template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
203{
204 if (mpMixedDimensionMesh == NULL)
205 {
206 EXCEPTION("The mixed dimension mesh object has not been set in the cell factory");
207 }
208 return mpMixedDimensionMesh;
209}
210
211template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
213 AbstractCardiacCellInterface* pPurkinjeCell,
214 AbstractCardiacCellInterface* pCardiacCell)
215{
216 std::map<unsigned, double>::iterator iter = mJunctionMap.find(pNode->GetIndex());
217 if (iter != mJunctionMap.end())
218 {
219 CreateJunction(pNode, pPurkinjeCell, pCardiacCell, iter->second);
220 }
221}
222
223// Explicit instantiation
#define EXCEPTION(message)
virtual void SetMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
boost::shared_ptr< AbstractStimulusFunction > GetStimulusFunction()
void SetStimulusFunction(boost::shared_ptr< AbstractStimulusFunction > pStimulus)
void CreateJunctionFromFile(const Node< SPACE_DIM > *pNode, AbstractCardiacCellInterface *pPurkinjeCell, AbstractCardiacCellInterface *pCardiacCell)
AbstractCardiacCellInterface * CreatePurkinjeCellForNode(Node< SPACE_DIM > *pNode, AbstractCardiacCellInterface *pCardiacCell)
void SetMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
MixedDimensionMesh< ELEMENT_DIM, SPACE_DIM > * GetMixedDimensionMesh()
void CreateJunction(const Node< SPACE_DIM > *pNode, AbstractCardiacCellInterface *pPurkinjeCell, AbstractCardiacCellInterface *pCardiacCell, double resistance)
std::string GetAbsolutePath() const
bool Exists() const
std::string GetMeshName() const
double GetPurkinjeSurfaceAreaToVolumeRatio()
static HeartConfig * Instance()
std::vector< Element< 1, SPACE_DIM > * >::const_iterator CableElementIterator
std::pair< NodeCableIterator, NodeCableIterator > CableRangeAtNode
std::multimap< constNode< SPACE_DIM > *, Element< 1u, SPACE_DIM > * >::iterator NodeCableIterator
Definition Node.hpp:59
unsigned GetIndex() const
Definition Node.cpp:158