Chaste Release::3.1
CellMLLoader.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, 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     }
00075     // If however we've made a cell before, check that we're making the same type this time
00076     else if (makeCvodeCell != mUseCvode)
00077     {
00078         EXCEPTION("You cannot call both LoadCvodeCell and LoadCardiacCell on the same CellMLLoader.");
00079     }
00080 
00081     // Convert the CellML to a shared library (no-op if shared library exists)
00082     DynamicCellModelLoaderPtr p_loader = mpConverter->Convert(copied_model);
00083 
00084     // Use the shared library to load a concrete cell
00085     boost::shared_ptr<AbstractStimulusFunction> p_stimulus;
00086     boost::shared_ptr<EulerIvpOdeSolver> p_solver;
00087     if (!makeCvodeCell)
00088     {
00089         // Put in a Forward Euler solver as a default for normal cells
00090         p_solver.reset(new EulerIvpOdeSolver);
00091     }
00092     AbstractCardiacCellInterface* p_loaded_cell = p_loader->CreateCell(p_solver, p_stimulus);
00093 
00094     // If the CellML file has a default stimulus we may as well use it.
00095     if (p_loaded_cell->HasCellMLDefaultStimulus())
00096     {
00097         p_loaded_cell->UseCellMLDefaultStimulus();
00098     }
00099     return p_loaded_cell;
00100 }
00101 
00102 boost::shared_ptr<AbstractCardiacCell> CellMLLoader::LoadCardiacCell(void)
00103 {
00104     AbstractCardiacCellInterface* p_loaded_cell = LoadCellMLFile(false);
00105     boost::shared_ptr<AbstractCardiacCell> p_model(dynamic_cast<AbstractCardiacCell*>(p_loaded_cell));
00106     return p_model;
00107 }
00108 
00109 #ifdef CHASTE_CVODE
00110 boost::shared_ptr<AbstractCvodeCell> CellMLLoader::LoadCvodeCell(void)
00111 {
00112     AbstractCardiacCellInterface* p_loaded_cell = LoadCellMLFile(true);
00113     boost::shared_ptr<AbstractCvodeCell> p_model(dynamic_cast<AbstractCvodeCell*>(p_loaded_cell));
00114     return p_model;
00115 }
00116 #endif
00117 
00118