Chaste  Release::2017.1
CellMLLoader.cpp
1 /*
2 
3 Copyright (c) 2005-2017, 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 #include "CellMLLoader.hpp"
37 
38 #include <algorithm>
39 #include "EulerIvpOdeSolver.hpp"
40 
41 CellMLLoader::CellMLLoader(const FileFinder& rCellMLFile, const OutputFileHandler& rOutputFileHandler, const std::vector<std::string>& rOptions)
42  : mCellMLFile(rCellMLFile),
43  mOutputFileHandler(rOutputFileHandler),
44  mOptions(rOptions),
45  mpConverter(new CellMLToSharedLibraryConverter(true)), // Builds using just the 'heart' component
46  mUseCvode(boost::logic::indeterminate)
47 {
48 }
49 
51 {
52  std::string model_name = mCellMLFile.GetLeafNameNoExtension();
54 
55  // If this is the first call, set up to do the conversion
56  if (boost::logic::indeterminate(mUseCvode))
57  {
58  mUseCvode = makeCvodeCell;
59 
60  // Remove or add the "--cvode" option to get the desired cell type
61  std::vector<std::string>::iterator it = std::find(mOptions.begin(), mOptions.end(), "--cvode");
62  if (!makeCvodeCell && it != mOptions.end())
63  {
64  mOptions.erase(it);
65  }
66  if (makeCvodeCell && it == mOptions.end())
67  {
68  mOptions.push_back("--cvode");
69  }
70 
71  // Create an options file and put it in the output directory with the CellML file
72  mpConverter->CreateOptionsFile(mOutputFileHandler, model_name, mOptions);
74  // Copy a .out file also if present
75  FileFinder out_file(model_name + ".out", mCellMLFile);
76  if (out_file.Exists())
77  {
79  }
80  }
81  // If however we've made a cell before, check that we're making the same type this time
82  else if (makeCvodeCell != mUseCvode)
83  {
84  EXCEPTION("You cannot call both LoadCvodeCell and LoadCardiacCell on the same CellMLLoader.");
85  }
86 
87  // Convert the CellML to a shared library (no-op if shared library exists)
88  DynamicCellModelLoaderPtr p_loader = mpConverter->Convert(copied_model);
89 
90  // Use the shared library to load a concrete cell
91  boost::shared_ptr<AbstractStimulusFunction> p_stimulus;
92  boost::shared_ptr<EulerIvpOdeSolver> p_solver;
93  if (!makeCvodeCell)
94  {
95  // Put in a Forward Euler solver as a default for normal cells
96  p_solver.reset(new EulerIvpOdeSolver);
97  }
98  AbstractCardiacCellInterface* p_loaded_cell = p_loader->CreateCell(p_solver, p_stimulus);
99 
100  // If the CellML file has a default stimulus we may as well use it.
101  if (p_loaded_cell->HasCellMLDefaultStimulus())
102  {
103  p_loaded_cell->UseCellMLDefaultStimulus();
104  }
105  return p_loaded_cell;
106 }
107 
108 boost::shared_ptr<AbstractCardiacCell> CellMLLoader::LoadCardiacCell(void)
109 {
110  AbstractCardiacCellInterface* p_loaded_cell = LoadCellMLFile(false);
111  boost::shared_ptr<AbstractCardiacCell> p_model(dynamic_cast<AbstractCardiacCell*>(p_loaded_cell));
112  return p_model;
113 }
114 
115 #ifdef CHASTE_CVODE
116 boost::shared_ptr<AbstractCvodeCell> CellMLLoader::LoadCvodeCell(void)
117 {
118  AbstractCardiacCellInterface* p_loaded_cell = LoadCellMLFile(true);
119  boost::shared_ptr<AbstractCvodeCell> p_model(dynamic_cast<AbstractCvodeCell*>(p_loaded_cell));
120  return p_model;
121 }
122 #endif
123 
124 
OutputFileHandler mOutputFileHandler
std::vector< std::string > mOptions
boost::shared_ptr< AbstractCvodeCell > LoadCvodeCell(void)
FileFinder CopyFileTo(const FileFinder &rSourceFile) const
#define EXCEPTION(message)
Definition: Exception.hpp:143
FileFinder FindFile(std::string leafName) const
CellMLLoader(const FileFinder &rCellMLFile, const OutputFileHandler &rOutputFileHandler, const std::vector< std::string > &rOptions)
virtual boost::shared_ptr< RegularStimulus > UseCellMLDefaultStimulus()
boost::shared_ptr< CellMLToSharedLibraryConverter > mpConverter
AbstractCardiacCellInterface * LoadCellMLFile(bool makeCvodeCell)
bool Exists() const
Definition: FileFinder.cpp:180
std::string GetLeafNameNoExtension() const
Definition: FileFinder.cpp:242
boost::shared_ptr< AbstractCardiacCell > LoadCardiacCell(void)
boost::logic::tribool mUseCvode
std::string GetLeafName() const
Definition: FileFinder.cpp:237
FileFinder mCellMLFile