VectorHelperFunctions.hpp

Go to the documentation of this file.
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 #ifndef VECTORHELPERFUNCTIONS_HPP_
00030 #define VECTORHELPERFUNCTIONS_HPP_
00031 
00040 #include <cassert>
00041 #include <vector>
00042 
00043 #ifdef CHASTE_CVODE
00044 // CVODE headers
00045 #include <nvector/nvector_serial.h>
00046 #endif
00047 
00057 template<typename VECTOR>
00058 inline double GetVectorComponent(const VECTOR& rVec, unsigned index);
00059 
00070 template<typename VECTOR>
00071 inline void SetVectorComponent(VECTOR& rVec, unsigned index, double value);
00072 
00082 template<typename VECTOR>
00083 inline unsigned GetVectorSize(const VECTOR& rVec);
00084 
00093 template<typename VECTOR>
00094 inline void InitialiseEmptyVector(VECTOR& rVec);
00095 
00104 template<typename VECTOR>
00105 inline void DeleteVector(VECTOR& rVec);
00106 
00115 template<typename VECTOR>
00116 inline VECTOR CopyVector(VECTOR& rVec);
00117 
00118 
00119 #ifdef CHASTE_CVODE
00120 
00128 inline void CopyToStdVector(N_Vector src, std::vector<realtype>& rDest)
00129 {
00130     // Check for no-op
00131     realtype* p_src = NV_DATA_S(src);
00132     if (p_src == &(rDest[0])) return;
00133     // Set dest size
00134     long size = NV_LENGTH_S(src);
00135     rDest.resize(size);
00136     // Copy data
00137     for (long i=0; i<size; i++)
00138     {
00139         rDest[i] = p_src[i];
00140     }
00141 }
00142 
00143 
00151 inline void CopyFromStdVector(const std::vector<realtype>& rSrc, N_Vector dest)
00152 {
00153     // Check for no-op
00154     realtype* p_dest = NV_DATA_S(dest);
00155     if (p_dest == &(rSrc[0])) return;
00156     // Check dest size
00157     long size = NV_LENGTH_S(dest);
00158     assert(size == (long)rSrc.size());
00159     // Copy data
00160     for (long i=0; i<size; i++)
00161     {
00162         p_dest[i] = rSrc[i];
00163     }
00164 }
00165 
00166 #endif // CHASTE_CVODE
00167 
00168 
00169 //
00170 // Specialisations for std::vector<double>
00171 //
00172 
00178 template<>
00179 inline double GetVectorComponent(const std::vector<double>& rVec, unsigned index)
00180 {
00181     assert(index < rVec.size());
00182     return rVec[index];
00183 }
00184 
00191 template<>
00192 inline void SetVectorComponent(std::vector<double>& rVec, unsigned index, double value)
00193 {
00194     assert(index < rVec.size());
00195     rVec[index] = value;
00196 }
00197 
00202 template<>
00203 inline unsigned GetVectorSize(const std::vector<double>& rVec)
00204 {
00205     return rVec.size();
00206 }
00207 
00212 template<>
00213 inline void InitialiseEmptyVector(std::vector<double>& rVec)
00214 {
00215 }
00216 
00221 template<>
00222 inline void DeleteVector(std::vector<double>& rVec)
00223 {
00224 }
00225 
00230 template<>
00231 inline std::vector<double> CopyVector(std::vector<double>& rVec)
00232 {
00233     return rVec;
00234 }
00235 
00236 //
00237 // Specialisations for N_Vector
00238 //
00239 
00240 #ifdef CHASTE_CVODE
00241 
00247 template<>
00248 inline double GetVectorComponent(const N_Vector& rVec, unsigned index)
00249 {
00250     assert(rVec != NULL);
00251     return NV_Ith_S(rVec, index);
00252 }
00253 
00260 template<>
00261 inline void SetVectorComponent(N_Vector& rVec, unsigned index, double value)
00262 {
00263     assert(rVec != NULL);
00264     NV_Ith_S(rVec, index) = value;
00265 }
00266 
00271 template<>
00272 inline unsigned GetVectorSize(const N_Vector& rVec)
00273 {
00274     assert(rVec != NULL);
00275     return NV_LENGTH_S(rVec);
00276 }
00277 
00282 template<>
00283 inline void InitialiseEmptyVector(N_Vector& rVec)
00284 {
00285     rVec = NULL;
00286 }
00287 
00292 template<>
00293 inline void DeleteVector(N_Vector& rVec)
00294 {
00295     if (rVec)
00296     {
00297         rVec->ops->nvdestroy(rVec);
00298         rVec = NULL;
00299     }
00300 }
00301 
00306 template<>
00307 inline N_Vector CopyVector(N_Vector& rVec)
00308 {
00309     N_Vector copy = NULL;
00310     if (rVec)
00311     {
00312         copy = N_VClone(rVec);
00313         unsigned size = NV_LENGTH_S(rVec);
00314         for (unsigned i=0; i<size; i++)
00315         {
00316             NV_Ith_S(copy, i) = NV_Ith_S(rVec, i);
00317         }
00318     }
00319     return copy;
00320 }
00321 #endif // CHASTE_CVODE
00322 
00323 //
00324 // End of helper functions
00325 //
00326 
00327 
00328 #endif /*VECTORHELPERFUNCTIONS_HPP_*/

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5