Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
AbstractPurkinjeCellFactory.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
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 if (!junction_stream.good())
77 { // file couldn't be opened
78 EXCEPTION("Couldn't open data file: " << junction_file.GetAbsolutePath());
79 }
80
81 // Reads in file defining nodes and resistance (separated by space)
82 while (junction_stream.good())
83 {
84 std::string this_line;
85 getline(junction_stream, this_line);
86
87 if (this_line=="" || this_line=="\r")
88 {
89 if (junction_stream.eof())
90 { // If the blank line is the last line carry on OK.
91 break;
92 }
93 else
94 {
95 EXCEPTION("No data found on line in file: " << junction_file.GetAbsolutePath());
96 }
97 }
98 std::stringstream line(this_line);
99
100 unsigned node_id;
101 line >> node_id;
102 double resistance;
103 line >> resistance;
104
105 if (mpMixedDimensionMesh->rGetNodePermutation().size() != 0) //Do we have a permuted mesh?
106 {
107 unsigned mapped_node_id = mpMixedDimensionMesh->rGetNodePermutation()[node_id];
108
109 mJunctionMap[mapped_node_id] = resistance;
110 }
111 else
112 {
113 mJunctionMap[node_id] = resistance;
114 }
115 }
116}
117
118template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
120 AbstractCardiacCellInterface* pPurkinjeCell,
121 AbstractCardiacCellInterface* pCardiacCell,
122 double resistance)
123{
124 // Figure out the effective resistance for this mesh, in kOhm.cm^3
125 if (pNode) // Should always be provided apart from low-level tests!
126 {
127 assert(mpMixedDimensionMesh);
128 typedef typename MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>::CableRangeAtNode CableRangeAtNode;
129 CableRangeAtNode cable_range = mpMixedDimensionMesh->GetCablesAtNode(pNode);
130 double total_cross_sectional_area = 0.0;
131 for (typename MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>::NodeCableIterator iter=cable_range.first;
132 iter != cable_range.second;
133 ++iter)
134 {
135 Element<1u,SPACE_DIM>* p_cable = iter->second;
136 double cable_radius = p_cable->GetAttribute();
137 total_cross_sectional_area += M_PI*cable_radius*cable_radius;
138 }
139 resistance *= total_cross_sectional_area / HeartConfig::Instance()->GetPurkinjeSurfaceAreaToVolumeRatio();
140 }
141 // Create the junction stimuli, and associate them with the cells
142 boost::shared_ptr<PurkinjeVentricularJunctionStimulus> p_pvj_ventricular_stim(new PurkinjeVentricularJunctionStimulus(resistance));
143 boost::shared_ptr<PurkinjeVentricularJunctionStimulus> p_pvj_purkinje_stim(new PurkinjeVentricularJunctionStimulus(resistance));
144 p_pvj_purkinje_stim->SetAppliedToPurkinjeCellModel();
145 p_pvj_ventricular_stim->SetVentricularCellModel(pCardiacCell);
146 p_pvj_ventricular_stim->SetPurkinjeCellModel(pPurkinjeCell);
147 p_pvj_purkinje_stim->SetVentricularCellModel(pCardiacCell);
148 p_pvj_purkinje_stim->SetPurkinjeCellModel(pPurkinjeCell);
149
150 // Create new combined stimuli which add the junction stimuli to those already in the cells
151 boost::shared_ptr<MultiStimulus> p_multi_stim_ventricular(new MultiStimulus);
152 p_multi_stim_ventricular->AddStimulus(p_pvj_ventricular_stim);
153 p_multi_stim_ventricular->AddStimulus(pCardiacCell->GetStimulusFunction());
154 pCardiacCell->SetStimulusFunction(p_multi_stim_ventricular);
155
156 boost::shared_ptr<MultiStimulus> p_multi_stim_purkinje(new MultiStimulus);
157 p_multi_stim_purkinje->AddStimulus(p_pvj_purkinje_stim);
158 p_multi_stim_purkinje->AddStimulus(pPurkinjeCell->GetStimulusFunction());
159 pPurkinjeCell->SetStimulusFunction(p_multi_stim_purkinje);
160}
161
162template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
164{
165 mpMixedDimensionMesh = dynamic_cast<MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>*>(pMesh);
166 if (mpMixedDimensionMesh == NULL)
167 {
168 EXCEPTION("AbstractPurkinjeCellFactory must take a MixedDimensionMesh");
169 }
170 mLocalPurkinjeNodes.clear();
171 for (typename MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>::CableElementIterator iter = mpMixedDimensionMesh->GetCableElementIteratorBegin();
172 iter != mpMixedDimensionMesh->GetCableElementIteratorEnd();
173 ++iter)
174 {
175 mLocalPurkinjeNodes.insert((*iter)->GetNodeGlobalIndex(0u));
176 mLocalPurkinjeNodes.insert((*iter)->GetNodeGlobalIndex(1u));
177 }
179
180 ReadJunctionsFile();
181}
182
183template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
185 Node<SPACE_DIM>* pNode,
186 AbstractCardiacCellInterface* pCardiacCell)
187{
188 unsigned node_index = pNode->GetIndex();
189 if (mLocalPurkinjeNodes.count(node_index)>0)
190 {
191 return CreatePurkinjeCellForTissueNode(pNode, pCardiacCell);
192 }
193 else
194 {
195 return new FakeBathCell(this->mpSolver, this->mpZeroStimulus);
196 }
197}
198
199template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
201{
202 if (mpMixedDimensionMesh == NULL)
203 {
204 EXCEPTION("The mixed dimension mesh object has not been set in the cell factory");
205 }
206 return mpMixedDimensionMesh;
207}
208
209template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
211 AbstractCardiacCellInterface* pPurkinjeCell,
212 AbstractCardiacCellInterface* pCardiacCell)
213{
214 std::map<unsigned, double>::iterator iter = mJunctionMap.find(pNode->GetIndex());
215 if (iter != mJunctionMap.end())
216 {
217 CreateJunction(pNode, pPurkinjeCell, pCardiacCell, iter->second);
218 }
219}
220
221// 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