Chaste Commit::f2ff7ee04e70ac9d06c57344df8d017dbb12b97b
CellMLLoader.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#include "CellMLLoader.hpp"
37
38#include <algorithm>
39#include "EulerIvpOdeSolver.hpp"
40
41CellMLLoader::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 // Chaste codegen doesn't use options files or outfiles, instead options are set directly here
72 mpConverter->SetOptions(mOptions);
74 }
75 // If however we've made a cell before, check that we're making the same type this time
76 else if (makeCvodeCell != mUseCvode)
77 {
78 EXCEPTION("You cannot call both LoadCvodeCell and LoadCardiacCell on the same CellMLLoader.");
79 }
80
81 // Convert the CellML to a shared library (no-op if shared library exists)
82 DynamicCellModelLoaderPtr p_loader = mpConverter->Convert(copied_model);
83
84 // Use the shared library to load a concrete cell
85 boost::shared_ptr<AbstractStimulusFunction> p_stimulus;
86 boost::shared_ptr<EulerIvpOdeSolver> p_solver;
87 if (!makeCvodeCell)
88 {
89 // Put in a Forward Euler solver as a default for normal cells
90 p_solver.reset(new EulerIvpOdeSolver);
91 }
92 AbstractCardiacCellInterface* p_loaded_cell = p_loader->CreateCell(p_solver, p_stimulus);
93
94 // If the CellML file has a default stimulus we may as well use it.
95 if (p_loaded_cell->HasCellMLDefaultStimulus())
96 {
97 p_loaded_cell->UseCellMLDefaultStimulus();
98 }
99 return p_loaded_cell;
100}
101
102boost::shared_ptr<AbstractCardiacCell> CellMLLoader::LoadCardiacCell(void)
103{
104 AbstractCardiacCellInterface* p_loaded_cell = LoadCellMLFile(false);
105 boost::shared_ptr<AbstractCardiacCell> p_model(dynamic_cast<AbstractCardiacCell*>(p_loaded_cell));
106 return p_model;
107}
108
109#ifdef CHASTE_CVODE
110boost::shared_ptr<AbstractCvodeCell> CellMLLoader::LoadCvodeCell(void)
111{
112 AbstractCardiacCellInterface* p_loaded_cell = LoadCellMLFile(true);
113 boost::shared_ptr<AbstractCvodeCell> p_model(dynamic_cast<AbstractCvodeCell*>(p_loaded_cell));
114 return p_model;
115}
116#endif
117
118
#define EXCEPTION(message)
virtual boost::shared_ptr< RegularStimulus > UseCellMLDefaultStimulus()
std::vector< std::string > mOptions
FileFinder mCellMLFile
boost::shared_ptr< AbstractCvodeCell > LoadCvodeCell(void)
boost::shared_ptr< AbstractCardiacCell > LoadCardiacCell(void)
boost::logic::tribool mUseCvode
AbstractCardiacCellInterface * LoadCellMLFile(bool makeCvodeCell)
CellMLLoader(const FileFinder &rCellMLFile, const OutputFileHandler &rOutputFileHandler, const std::vector< std::string > &rOptions)
boost::shared_ptr< CellMLToSharedLibraryConverter > mpConverter
OutputFileHandler mOutputFileHandler
std::string GetLeafNameNoExtension() const
std::string GetLeafName() const
FileFinder CopyFileTo(const FileFinder &rSourceFile) const
FileFinder FindFile(std::string leafName) const