Chaste Release::3.1
CombinedOdeSystemInformation.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 
00037 #include "CombinedOdeSystemInformation.hpp"
00038 
00039 #include <cassert>
00040 
00041 boost::shared_ptr<CombinedOdeSystemInformation> CombinedOdeSystemInformation::Instance(const std::vector<AbstractOdeSystem*>& rSubsystems)
00042 {
00043     // Get the information for the subsystems
00044     std::vector<boost::shared_ptr<const AbstractOdeSystemInformation> > info_vec;
00045     info_vec.reserve(rSubsystems.size());
00046     for (unsigned i=0; i<rSubsystems.size(); i++)
00047     {
00048         info_vec.push_back(rSubsystems[i]->GetSystemInformation());
00049     }
00050 
00051     boost::shared_ptr<CombinedOdeSystemInformation> p_inst;
00052 
00053     // Search to see if we have an information object for this sequence of
00054     // subsystems already.
00055     for (unsigned i=0; i<msInstances.size(); i++)
00056     {
00057         if (info_vec.size() == msInstances[i].subsystemInformation.size())
00058         {
00059             bool equal = true;
00060             for (unsigned j=0; j<info_vec.size(); j++)
00061             {
00062                 if (msInstances[i].subsystemInformation[j] != info_vec[j])
00063                 {
00064                     equal = false;
00065                     break;
00066                 }
00067             }
00068             if (equal)
00069             {
00070                 p_inst = msInstances[i].pInfoInstance;
00071                 break;
00072             }
00073         }
00074     }
00075 
00076     // Create a new object if needed
00077     if (!p_inst)
00078     {
00079         p_inst.reset(new CombinedOdeSystemInformation(info_vec));
00080         struct InstancePointers inst;
00081         inst.subsystemInformation = info_vec;
00082         inst.pInfoInstance = p_inst;
00083         msInstances.push_back(inst);
00084     }
00085 
00086     return p_inst;
00087 }
00088 
00089 CombinedOdeSystemInformation::CombinedOdeSystemInformation(const std::vector<boost::shared_ptr<const AbstractOdeSystemInformation> >& rSubsystemInfo)
00090 {
00091     // Figure out our size
00092     unsigned total_system_size = 0u;
00093     for (unsigned i=0; i<rSubsystemInfo.size(); i++)
00094     {
00095         total_system_size += rSubsystemInfo[i]->rGetStateVariableNames().size();
00096     }
00097     mVariableNames.reserve(total_system_size);
00098     mVariableUnits.reserve(total_system_size);
00099     mInitialConditions.reserve(total_system_size);
00100 
00101     // Set up our info from the subsystems
00102     for (unsigned i=0; i<rSubsystemInfo.size(); i++)
00103     {
00104         std::vector<double> inits = rSubsystemInfo[i]->GetInitialConditions();
00105         const std::vector<std::string>& names = rSubsystemInfo[i]->rGetStateVariableNames();
00106         const std::vector<std::string>& units = rSubsystemInfo[i]->rGetStateVariableUnits();
00107         unsigned system_size = names.size();
00108         assert(inits.size() == system_size);
00109         assert(units.size() == system_size);
00110 
00111         for (unsigned j=0; j<system_size; j++)
00112         {
00113             mVariableNames.push_back(names[j]);
00114             mVariableUnits.push_back(units[j]);
00115             mInitialConditions.push_back(inits[j]);
00116         }
00117     }
00118 
00119     mInitialised = true;
00120 }
00121 
00122 #define COVERAGE_IGNORE
00123 void CombinedOdeSystemInformation::Initialise()
00124 {
00125     // does nothing; work done in constructor
00126     // but we need the method because it is pure in our base class
00127 }
00128 
00130 std::vector<struct CombinedOdeSystemInformation::InstancePointers> CombinedOdeSystemInformation::msInstances;
00131 #undef COVERAGE_IGNORE