CellMLLoader.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "CellMLLoader.hpp"
00037 
00038 #include <algorithm>
00039 #include "EulerIvpOdeSolver.hpp"
00040 
00041 CellMLLoader::CellMLLoader(const FileFinder& rCellMLFile, const OutputFileHandler& rOutputFileHandler, const std::vector<std::string>& rOptions)
00042     : mCellMLFile(rCellMLFile),
00043       mOutputFileHandler(rOutputFileHandler),
00044       mOptions(rOptions),
00045       mpConverter(new CellMLToSharedLibraryConverter(true)), // Builds using just the 'heart' component
00046       mUseCvode(boost::logic::indeterminate)
00047 {
00048 }
00049 
00050 AbstractCardiacCellInterface* CellMLLoader::LoadCellMLFile(bool makeCvodeCell)
00051 {
00052     std::string model_name = mCellMLFile.GetLeafNameNoExtension();
00053     FileFinder copied_model = mOutputFileHandler.FindFile(mCellMLFile.GetLeafName());
00054 
00055     // If this is the first call, set up to do the conversion
00056     if (boost::logic::indeterminate(mUseCvode))
00057     {
00058         mUseCvode = makeCvodeCell;
00059 
00060         // Remove or add the "--cvode" option to get the desired cell type
00061         std::vector<std::string>::iterator it = std::find(mOptions.begin(), mOptions.end(), "--cvode");
00062         if (!makeCvodeCell && it != mOptions.end())
00063         {
00064             mOptions.erase(it);
00065         }
00066         if (makeCvodeCell && it == mOptions.end())
00067         {
00068             mOptions.push_back("--cvode");
00069         }
00070 
00071         // Create an options file and put it in the output directory with the CellML file
00072         mpConverter->CreateOptionsFile(mOutputFileHandler, model_name, mOptions);
00073         mOutputFileHandler.CopyFileTo(mCellMLFile);
00074         // Copy a .out file also if present
00075         FileFinder out_file(model_name + ".out", mCellMLFile);
00076         if (out_file.Exists())
00077         {
00078             mOutputFileHandler.CopyFileTo(out_file);
00079         }
00080     }
00081     // If however we've made a cell before, check that we're making the same type this time
00082     else if (makeCvodeCell != mUseCvode)
00083     {
00084         EXCEPTION("You cannot call both LoadCvodeCell and LoadCardiacCell on the same CellMLLoader.");
00085     }
00086 
00087     // Convert the CellML to a shared library (no-op if shared library exists)
00088     DynamicCellModelLoaderPtr p_loader = mpConverter->Convert(copied_model);
00089 
00090     // Use the shared library to load a concrete cell
00091     boost::shared_ptr<AbstractStimulusFunction> p_stimulus;
00092     boost::shared_ptr<EulerIvpOdeSolver> p_solver;
00093     if (!makeCvodeCell)
00094     {
00095         // Put in a Forward Euler solver as a default for normal cells
00096         p_solver.reset(new EulerIvpOdeSolver);
00097     }
00098     AbstractCardiacCellInterface* p_loaded_cell = p_loader->CreateCell(p_solver, p_stimulus);
00099 
00100     // If the CellML file has a default stimulus we may as well use it.
00101     if (p_loaded_cell->HasCellMLDefaultStimulus())
00102     {
00103         p_loaded_cell->UseCellMLDefaultStimulus();
00104     }
00105     return p_loaded_cell;
00106 }
00107 
00108 boost::shared_ptr<AbstractCardiacCell> CellMLLoader::LoadCardiacCell(void)
00109 {
00110     AbstractCardiacCellInterface* p_loaded_cell = LoadCellMLFile(false);
00111     boost::shared_ptr<AbstractCardiacCell> p_model(dynamic_cast<AbstractCardiacCell*>(p_loaded_cell));
00112     return p_model;
00113 }
00114 
00115 #ifdef CHASTE_CVODE
00116 boost::shared_ptr<AbstractCvodeCell> CellMLLoader::LoadCvodeCell(void)
00117 {
00118     AbstractCardiacCellInterface* p_loaded_cell = LoadCellMLFile(true);
00119     boost::shared_ptr<AbstractCvodeCell> p_model(dynamic_cast<AbstractCvodeCell*>(p_loaded_cell));
00120     return p_model;
00121 }
00122 #endif
00123 
00124 

Generated by  doxygen 1.6.2