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 
00106 template<typename VECTOR>
00107 inline void CreateVectorIfEmpty(VECTOR& rVec, unsigned size);
00108 
00114 template<typename VECTOR>
00115 inline VECTOR CreateEmptyVector();
00116 
00122 template<typename VECTOR>
00123 inline bool IsEmptyVector(VECTOR& rVec);
00124 
00133 template<typename VECTOR>
00134 inline void DeleteVector(VECTOR& rVec);
00135 
00144 template<typename VECTOR>
00145 inline VECTOR CopyVector(VECTOR& rVec);
00146 
00156 template<typename VECTOR>
00157 inline void CopyToStdVector(const VECTOR& rSrc, std::vector<double>& rDest);
00158 
00168 template<typename VECTOR>
00169 inline void CopyFromStdVector(const std::vector<double>& rSrc, VECTOR& rDest);
00170 
00171 // Specialisations for std::vector<double>
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 
00222 template<>
00223 inline void CreateVectorIfEmpty(std::vector<double>& rVec, unsigned size)
00224 {
00225     if (rVec.empty())
00226     {
00227         rVec.resize(size);
00228     }
00229 }
00230 
00234 template<>
00235 inline std::vector<double> CreateEmptyVector()
00236 {
00237     return std::vector<double>();
00238 }
00239 
00244 template<>
00245 inline bool IsEmptyVector(std::vector<double>& rVec)
00246 {
00247     return rVec.empty();
00248 }
00249 
00254 template<>
00255 inline void DeleteVector(std::vector<double>& rVec)
00256 {
00257 }
00258 
00263 template<>
00264 inline std::vector<double> CopyVector(std::vector<double>& rVec)
00265 {
00266     return rVec;
00267 }
00268 
00274 template<>
00275 inline void CopyToStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest)
00276 {
00277     rDest = rSrc;
00278 }
00279 
00285 template<>
00286 inline void CopyFromStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest)
00287 {
00288     rDest = rSrc;
00289 }
00290 
00291 // Specialisations for N_Vector
00292 
00293 #ifdef CHASTE_CVODE
00294 
00300 template<>
00301 inline double GetVectorComponent(const N_Vector& rVec, unsigned index)
00302 {
00303     assert(rVec != NULL);
00304     return NV_Ith_S(rVec, index);
00305 }
00306 
00313 template<>
00314 inline void SetVectorComponent(N_Vector& rVec, unsigned index, double value)
00315 {
00316     assert(rVec != NULL);
00317     NV_Ith_S(rVec, index) = value;
00318 }
00319 
00324 template<>
00325 inline unsigned GetVectorSize(const N_Vector& rVec)
00326 {
00327     assert(rVec != NULL);
00328     return NV_LENGTH_S(rVec);
00329 }
00330 
00335 template<>
00336 inline void InitialiseEmptyVector(N_Vector& rVec)
00337 {
00338     rVec = NULL;
00339 }
00340 
00346 template<>
00347 inline void CreateVectorIfEmpty(N_Vector& rVec, unsigned size)
00348 {
00349     if (rVec == NULL)
00350     {
00351         rVec = N_VNew_Serial(size);
00352     }
00353 }
00354 
00358 template<>
00359 inline N_Vector CreateEmptyVector()
00360 {
00361     return NULL;
00362 }
00363 
00368 template<>
00369 inline bool IsEmptyVector(N_Vector& rVec)
00370 {
00371     return rVec == NULL;
00372 }
00373 
00378 template<>
00379 inline void DeleteVector(N_Vector& rVec)
00380 {
00381     if (rVec)
00382     {
00383         rVec->ops->nvdestroy(rVec);
00384         rVec = NULL;
00385     }
00386 }
00387 
00392 template<>
00393 inline N_Vector CopyVector(N_Vector& rVec)
00394 {
00395     N_Vector copy = NULL;
00396     if (rVec)
00397     {
00398         copy = N_VClone(rVec);
00399         unsigned size = NV_LENGTH_S(rVec);
00400         for (unsigned i=0; i<size; i++)
00401         {
00402             NV_Ith_S(copy, i) = NV_Ith_S(rVec, i);
00403         }
00404     }
00405     return copy;
00406 }
00407 
00414 template<>
00415 inline void CopyToStdVector(const N_Vector& rSrc, std::vector<double>& rDest)
00416 {
00417     // Check for no-op
00418     realtype* p_src = NV_DATA_S(rSrc);
00419     if (p_src == &(rDest[0])) return;
00420     // Set dest size
00421     long size = NV_LENGTH_S(rSrc);
00422     rDest.resize(size);
00423     // Copy data
00424     for (long i=0; i<size; i++)
00425     {
00426         rDest[i] = p_src[i];
00427     }
00428 }
00429 
00436 template<>
00437 inline void CopyFromStdVector(const std::vector<double>& rSrc, N_Vector& rDest)
00438 {
00439     // Check for no-op
00440     realtype* p_dest = NV_DATA_S(rDest);
00441     if (p_dest == &(rSrc[0])) return;
00442 
00443     // Check dest size
00444     long size = NV_LENGTH_S(rDest);
00445     assert(size == (long)rSrc.size());
00446 
00447     // Copy data
00448     for (long i=0; i<size; i++)
00449     {
00450         p_dest[i] = rSrc[i];
00451     }
00452 }
00453 
00460 inline std::vector<double> MakeStdVec(N_Vector v)
00461 {
00462     std::vector<double> sv;
00463     CopyToStdVector(v, sv);
00464     return sv;
00465 }
00466 
00467 #endif // CHASTE_CVODE
00468 
00469 // End of helper functions
00470 
00471 #endif /*VECTORHELPERFUNCTIONS_HPP_*/
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3