CombinedOdeSystem.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 
00030 #include "CombinedOdeSystem.hpp"
00031 #include "CombinedOdeSystemInformation.hpp"
00032 
00033 
00034 
00035 CombinedOdeSystem::CombinedOdeSystem(std::vector<AbstractOdeSystem*> odeSystems)
00036     : AbstractOdeSystem(0) // will be set properly below
00037 {
00038     mOdeSystems = odeSystems;
00039     for (unsigned i=0; i<odeSystems.size(); i++)
00040     {
00041         mNumberOfStateVariables += odeSystems[i]->GetNumberOfStateVariables();
00042     }
00043     mpSystemInfo = CombinedOdeSystemInformation::Instance(odeSystems);
00044     ResetToInitialConditions();
00045 
00046     // Set up working memory
00047     unsigned num_systems = odeSystems.size();
00048     mWorkingStateVars.resize(num_systems);
00049     mWorkingDerivs.resize(num_systems);
00050     unsigned offset = 0;
00051     for (unsigned i=0; i<num_systems; i++)
00052     {
00053         unsigned num_vars = odeSystems[i]->GetNumberOfStateVariables();
00054         mWorkingStateVars[i].resize(num_vars);
00055         mWorkingDerivs[i].resize(num_vars);
00056         mOffsets.push_back(offset);
00057         offset += num_vars;
00058     }
00059 }
00060 
00061 
00062 void CombinedOdeSystem::Configure(
00063         const std::map<unsigned, unsigned>& rVariableParameterMap,
00064         AbstractOdeSystem* pVariableOdeSystem,
00065         AbstractOdeSystem* pParameterOdeSystem)
00066 {
00067     struct VariableParameterMap new_map;
00068     new_map.theMap = rVariableParameterMap;
00069     unsigned var_system_index = 0;
00070     while (var_system_index < mOdeSystems.size() && mOdeSystems[var_system_index] != pVariableOdeSystem)
00071     {
00072         ++var_system_index;
00073     }
00074     new_map.pVariableOdeSystemIndex = var_system_index;
00075     new_map.pParameterOdeSystem = pParameterOdeSystem;
00076     mVariableParameterMaps.push_back(new_map);
00077 }
00078 
00079 
00080 void CombinedOdeSystem::EvaluateYDerivatives(
00081         double time,
00082         const std::vector<double>& rY,
00083         std::vector<double>& rDY)
00084 {
00085     // Copy rY to subsystems
00086     for (unsigned i=0; i<mOdeSystems.size(); i++)
00087     {
00088         unsigned offset = mOffsets[i];
00089         for (unsigned j=0; j<mOdeSystems[i]->GetNumberOfStateVariables(); j++)
00090         {
00091             mWorkingStateVars[i][j] = rY[offset + j];
00092         }
00093     }
00094 
00095     // Set parameter values
00096     for (unsigned i=0; i<mVariableParameterMaps.size(); i++)
00097     {
00098         std::map<unsigned, unsigned>& r_var_param_map = mVariableParameterMaps[i].theMap;
00099         // Iterate through map
00100         for (std::map<unsigned, unsigned>::iterator iter = r_var_param_map.begin();
00101              iter != r_var_param_map.end();
00102              ++iter)
00103         {
00104             double value = mWorkingStateVars[mVariableParameterMaps[i].pVariableOdeSystemIndex][iter->first];
00105             mVariableParameterMaps[i].pParameterOdeSystem->SetParameter(iter->second, value);
00106         }
00107     }
00108 
00109     // Call EvaluateYDerivatives on subsystems
00110     for (unsigned i=0; i<mOdeSystems.size(); i++)
00111     {
00112         mOdeSystems[i]->EvaluateYDerivatives(time, mWorkingStateVars[i], mWorkingDerivs[i]);
00113     }
00114 
00115     // Copy derivatives to rDY
00116     for (unsigned i=0; i<mOdeSystems.size(); i++)
00117     {
00118         unsigned offset = mOffsets[i];
00119         for (unsigned j=0; j<mOdeSystems[i]->GetNumberOfStateVariables(); j++)
00120         {
00121             rDY[offset + j] = mWorkingDerivs[i][j];
00122         }
00123     }
00124 }
Generated on Thu Dec 22 13:00:16 2011 for Chaste by  doxygen 1.6.3