Chaste Release::3.1
CombinedOdeSystem.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 "CombinedOdeSystem.hpp"
00038 #include "CombinedOdeSystemInformation.hpp"
00039 
00040 
00041 
00042 CombinedOdeSystem::CombinedOdeSystem(std::vector<AbstractOdeSystem*> odeSystems)
00043     : AbstractOdeSystem(0) // will be set properly below
00044 {
00045     mOdeSystems = odeSystems;
00046     for (unsigned i=0; i<odeSystems.size(); i++)
00047     {
00048         mNumberOfStateVariables += odeSystems[i]->GetNumberOfStateVariables();
00049     }
00050     mpSystemInfo = CombinedOdeSystemInformation::Instance(odeSystems);
00051     ResetToInitialConditions();
00052 
00053     // Set up working memory
00054     unsigned num_systems = odeSystems.size();
00055     mWorkingStateVars.resize(num_systems);
00056     mWorkingDerivs.resize(num_systems);
00057     unsigned offset = 0;
00058     for (unsigned i=0; i<num_systems; i++)
00059     {
00060         unsigned num_vars = odeSystems[i]->GetNumberOfStateVariables();
00061         mWorkingStateVars[i].resize(num_vars);
00062         mWorkingDerivs[i].resize(num_vars);
00063         mOffsets.push_back(offset);
00064         offset += num_vars;
00065     }
00066 }
00067 
00068 
00069 void CombinedOdeSystem::Configure(
00070         const std::map<unsigned, unsigned>& rVariableParameterMap,
00071         AbstractOdeSystem* pVariableOdeSystem,
00072         AbstractOdeSystem* pParameterOdeSystem)
00073 {
00074     struct VariableParameterMap new_map;
00075     new_map.theMap = rVariableParameterMap;
00076     unsigned var_system_index = 0;
00077     while (var_system_index < mOdeSystems.size() && mOdeSystems[var_system_index] != pVariableOdeSystem)
00078     {
00079         ++var_system_index;
00080     }
00081     new_map.pVariableOdeSystemIndex = var_system_index;
00082     new_map.pParameterOdeSystem = pParameterOdeSystem;
00083     mVariableParameterMaps.push_back(new_map);
00084 }
00085 
00086 
00087 void CombinedOdeSystem::EvaluateYDerivatives(
00088         double time,
00089         const std::vector<double>& rY,
00090         std::vector<double>& rDY)
00091 {
00092     // Copy rY to subsystems
00093     for (unsigned i=0; i<mOdeSystems.size(); i++)
00094     {
00095         unsigned offset = mOffsets[i];
00096         for (unsigned j=0; j<mOdeSystems[i]->GetNumberOfStateVariables(); j++)
00097         {
00098             mWorkingStateVars[i][j] = rY[offset + j];
00099         }
00100     }
00101 
00102     // Set parameter values
00103     for (unsigned i=0; i<mVariableParameterMaps.size(); i++)
00104     {
00105         std::map<unsigned, unsigned>& r_var_param_map = mVariableParameterMaps[i].theMap;
00106         // Iterate through map
00107         for (std::map<unsigned, unsigned>::iterator iter = r_var_param_map.begin();
00108              iter != r_var_param_map.end();
00109              ++iter)
00110         {
00111             double value = mWorkingStateVars[mVariableParameterMaps[i].pVariableOdeSystemIndex][iter->first];
00112             mVariableParameterMaps[i].pParameterOdeSystem->SetParameter(iter->second, value);
00113         }
00114     }
00115 
00116     // Call EvaluateYDerivatives on subsystems
00117     for (unsigned i=0; i<mOdeSystems.size(); i++)
00118     {
00119         mOdeSystems[i]->EvaluateYDerivatives(time, mWorkingStateVars[i], mWorkingDerivs[i]);
00120     }
00121 
00122     // Copy derivatives to rDY
00123     for (unsigned i=0; i<mOdeSystems.size(); i++)
00124     {
00125         unsigned offset = mOffsets[i];
00126         for (unsigned j=0; j<mOdeSystems[i]->GetNumberOfStateVariables(); j++)
00127         {
00128             rDY[offset + j] = mWorkingDerivs[i][j];
00129         }
00130     }
00131 }