Chaste  Release::3.4
AbstractPurkinjeCellFactory.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 
37 #include "AbstractPurkinjeCellFactory.hpp"
38 #include "PurkinjeVentricularJunctionStimulus.hpp"
39 #include "MultiStimulus.hpp"
40 #include "HeartConfig.hpp"
41 #include "Warnings.hpp"
42 
43 
44 
45 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
47  : AbstractCardiacCellFactory<ELEMENT_DIM,SPACE_DIM>(),
48  mpMixedDimensionMesh(NULL)
49 {
50 }
51 
52 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
54 {
55  std::string pvj_file_name;
56  bool file_specified = true;
57 
58  try
59  {
60  // HeartConfig::Instance()->GetMeshName() will throw an exception if no mesh name is defined
61  pvj_file_name = HeartConfig::Instance()->GetMeshName() + ".pvj";
62  }
63  catch(Exception&)
64  {
65  file_specified = false;
66  }
67 
68  FileFinder junction_file(pvj_file_name, RelativeTo::AbsoluteOrCwd);
69  if (!file_specified || !junction_file.Exists() )
70  {
71  // In this case we expect the user to specify PVJ programmatically, so return immediately.
72  WARNING("No Purkinje-Ventricular junction (.pvj) file found. Junctions must be specified manually.");
73  return;
74  }
75 
76  std::ifstream junction_stream(junction_file.GetAbsolutePath().c_str());
77 
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 
120 template<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 
164 
165 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
167 {
168  mpMixedDimensionMesh = dynamic_cast<MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>*>(pMesh);
169  if (mpMixedDimensionMesh == NULL)
170  {
171  EXCEPTION("AbstractPurkinjeCellFactory must take a MixedDimensionMesh");
172  }
173  mLocalPurkinjeNodes.clear();
174  for (typename MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>::CableElementIterator iter = mpMixedDimensionMesh->GetCableElementIteratorBegin();
175  iter != mpMixedDimensionMesh->GetCableElementIteratorEnd();
176  ++iter)
177  {
178  mLocalPurkinjeNodes.insert((*iter)->GetNodeGlobalIndex(0u));
179  mLocalPurkinjeNodes.insert((*iter)->GetNodeGlobalIndex(1u));
180  }
182 
183  ReadJunctionsFile();
184 }
185 
186 
187 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
189  Node<SPACE_DIM>* pNode,
190  AbstractCardiacCellInterface* pCardiacCell)
191 {
192  unsigned node_index = pNode->GetIndex();
193  if (mLocalPurkinjeNodes.count(node_index)>0)
194  {
195  return CreatePurkinjeCellForTissueNode(pNode, pCardiacCell);
196  }
197  else
198  {
199  return new FakeBathCell(this->mpSolver, this->mpZeroStimulus);
200  }
201 }
202 
203 
204 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
206 {
207  if (mpMixedDimensionMesh == NULL)
208  {
209  EXCEPTION("The mixed dimension mesh object has not been set in the cell factory");
210  }
211  return mpMixedDimensionMesh;
212 }
213 
214 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
216  AbstractCardiacCellInterface* pPurkinjeCell,
217  AbstractCardiacCellInterface* pCardiacCell)
218 {
219  std::map<unsigned, double>::iterator iter = mJunctionMap.find(pNode->GetIndex());
220  if(iter != mJunctionMap.end())
221  {
222  CreateJunction(pNode, pPurkinjeCell, pCardiacCell, iter->second);
223  }
224 }
225 
227 // Explicit instantiation
229 
230 template class AbstractPurkinjeCellFactory<1,1>;
231 template class AbstractPurkinjeCellFactory<2,2>;
232 template class AbstractPurkinjeCellFactory<3,3>;
233 template class AbstractPurkinjeCellFactory<1,2>;
234 template class AbstractPurkinjeCellFactory<1,3>;
double GetPurkinjeSurfaceAreaToVolumeRatio()
AbstractCardiacCellInterface * CreatePurkinjeCellForNode(Node< SPACE_DIM > *pNode, AbstractCardiacCellInterface *pCardiacCell)
Definition: Node.hpp:58
std::pair< NodeCableIterator, NodeCableIterator > CableRangeAtNode
std::string GetAbsolutePath() const
Definition: FileFinder.cpp:221
#define EXCEPTION(message)
Definition: Exception.hpp:143
MixedDimensionMesh< ELEMENT_DIM, SPACE_DIM > * GetMixedDimensionMesh()
void SetStimulusFunction(boost::shared_ptr< AbstractStimulusFunction > pStimulus)
std::string GetMeshName() const
std::multimap< const Node< SPACE_DIM > *, Element< 1u, SPACE_DIM > * >::iterator NodeCableIterator
void CreateJunctionFromFile(const Node< SPACE_DIM > *pNode, AbstractCardiacCellInterface *pPurkinjeCell, AbstractCardiacCellInterface *pCardiacCell)
bool Exists() const
Definition: FileFinder.cpp:180
void SetMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
virtual void SetMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
boost::shared_ptr< AbstractStimulusFunction > GetStimulusFunction()
unsigned GetIndex() const
Definition: Node.cpp:159
void CreateJunction(const Node< SPACE_DIM > *pNode, AbstractCardiacCellInterface *pPurkinjeCell, AbstractCardiacCellInterface *pCardiacCell, double resistance)
static HeartConfig * Instance()
std::vector< Element< 1, SPACE_DIM > * >::const_iterator CableElementIterator