VectorHelperFunctions.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef VECTORHELPERFUNCTIONS_HPP_
00037 #define VECTORHELPERFUNCTIONS_HPP_
00038
00047 #include <cassert>
00048 #include <vector>
00049
00050 #ifdef CHASTE_CVODE
00051
00052 #include <nvector/nvector_serial.h>
00053 #endif
00054
00065 template<typename VECTOR>
00066 inline double GetVectorComponent(const VECTOR& rVec, unsigned index);
00067
00078 template<typename VECTOR>
00079 inline void SetVectorComponent(VECTOR& rVec, unsigned index, double value);
00080
00090 template<typename VECTOR>
00091 inline unsigned GetVectorSize(const VECTOR& rVec);
00092
00101 template<typename VECTOR>
00102 inline void InitialiseEmptyVector(VECTOR& rVec);
00103
00114 template<typename VECTOR>
00115 inline void CreateVectorIfEmpty(VECTOR& rVec, unsigned size);
00116
00122 template<typename VECTOR>
00123 inline VECTOR CreateEmptyVector();
00124
00131 template<typename VECTOR>
00132 inline bool IsEmptyVector(VECTOR& rVec);
00133
00142 template<typename VECTOR>
00143 inline void DeleteVector(VECTOR& rVec);
00144
00154 template<typename VECTOR>
00155 inline VECTOR CopyVector(VECTOR& rVec);
00156
00166 template<typename VECTOR>
00167 inline void CopyToStdVector(const VECTOR& rSrc, std::vector<double>& rDest);
00168
00178 template<typename VECTOR>
00179 inline void CopyFromStdVector(const std::vector<double>& rSrc, VECTOR& rDest);
00180
00181
00182
00189 template<>
00190 inline double GetVectorComponent(const std::vector<double>& rVec, unsigned index)
00191 {
00192 assert(index < rVec.size());
00193 return rVec[index];
00194 }
00195
00202 template<>
00203 inline void SetVectorComponent(std::vector<double>& rVec, unsigned index, double value)
00204 {
00205 assert(index < rVec.size());
00206 rVec[index] = value;
00207 }
00208
00214 template<>
00215 inline unsigned GetVectorSize(const std::vector<double>& rVec)
00216 {
00217 return rVec.size();
00218 }
00219
00224 template<>
00225 inline void InitialiseEmptyVector(std::vector<double>& rVec)
00226 {
00227 }
00228
00234 template<>
00235 inline void CreateVectorIfEmpty(std::vector<double>& rVec, unsigned size)
00236 {
00237 if (rVec.empty())
00238 {
00239 rVec.resize(size);
00240 }
00241 }
00242
00247 template<>
00248 inline std::vector<double> CreateEmptyVector()
00249 {
00250 return std::vector<double>();
00251 }
00252
00258 template<>
00259 inline bool IsEmptyVector(std::vector<double>& rVec)
00260 {
00261 return rVec.empty();
00262 }
00263
00268 template<>
00269 inline void DeleteVector(std::vector<double>& rVec)
00270 {
00271 }
00272
00278 template<>
00279 inline std::vector<double> CopyVector(std::vector<double>& rVec)
00280 {
00281 return rVec;
00282 }
00283
00289 template<>
00290 inline void CopyToStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest)
00291 {
00292 rDest = rSrc;
00293 }
00294
00300 template<>
00301 inline void CopyFromStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest)
00302 {
00303 rDest = rSrc;
00304 }
00305
00306
00307
00308 #ifdef CHASTE_CVODE
00309
00316 template<>
00317 inline double GetVectorComponent(const N_Vector& rVec, unsigned index)
00318 {
00319 assert(rVec != NULL);
00320 return NV_Ith_S(rVec, index);
00321 }
00322
00329 template<>
00330 inline void SetVectorComponent(N_Vector& rVec, unsigned index, double value)
00331 {
00332 assert(rVec != NULL);
00333 NV_Ith_S(rVec, index) = value;
00334 }
00335
00341 template<>
00342 inline unsigned GetVectorSize(const N_Vector& rVec)
00343 {
00344 assert(rVec != NULL);
00345 return NV_LENGTH_S(rVec);
00346 }
00347
00352 template<>
00353 inline void InitialiseEmptyVector(N_Vector& rVec)
00354 {
00355 rVec = NULL;
00356 }
00357
00363 template<>
00364 inline void CreateVectorIfEmpty(N_Vector& rVec, unsigned size)
00365 {
00366 if (rVec == NULL)
00367 {
00368 rVec = N_VNew_Serial(size);
00369 }
00370 }
00371
00376 template<>
00377 inline N_Vector CreateEmptyVector()
00378 {
00379 return NULL;
00380 }
00381
00387 template<>
00388 inline bool IsEmptyVector(N_Vector& rVec)
00389 {
00390 return rVec == NULL;
00391 }
00392
00397 template<>
00398 inline void DeleteVector(N_Vector& rVec)
00399 {
00400 if (rVec)
00401 {
00402 rVec->ops->nvdestroy(rVec);
00403 rVec = NULL;
00404 }
00405 }
00406
00412 template<>
00413 inline N_Vector CopyVector(N_Vector& rVec)
00414 {
00415 N_Vector copy = NULL;
00416 if (rVec)
00417 {
00418 copy = N_VClone(rVec);
00419 unsigned size = NV_LENGTH_S(rVec);
00420 for (unsigned i=0; i<size; i++)
00421 {
00422 NV_Ith_S(copy, i) = NV_Ith_S(rVec, i);
00423 }
00424 }
00425 return copy;
00426 }
00427
00434 template<>
00435 inline void CopyToStdVector(const N_Vector& rSrc, std::vector<double>& rDest)
00436 {
00437
00438 realtype* p_src = NV_DATA_S(rSrc);
00439 if (!rDest.empty() && p_src == &(rDest[0])) return;
00440
00441 long size = NV_LENGTH_S(rSrc);
00442 rDest.resize(size);
00443
00444 for (long i=0; i<size; i++)
00445 {
00446 rDest[i] = p_src[i];
00447 }
00448 }
00449
00456 template<>
00457 inline void CopyFromStdVector(const std::vector<double>& rSrc, N_Vector& rDest)
00458 {
00459
00460 realtype* p_dest = NV_DATA_S(rDest);
00461 if (p_dest == &(rSrc[0])) return;
00462
00463
00464 long size = NV_LENGTH_S(rDest);
00465 assert(size == (long)rSrc.size());
00466
00467
00468 for (long i=0; i<size; i++)
00469 {
00470 p_dest[i] = rSrc[i];
00471 }
00472 }
00473
00480 inline std::vector<double> MakeStdVec(N_Vector v)
00481 {
00482 std::vector<double> sv;
00483 CopyToStdVector(v, sv);
00484 return sv;
00485 }
00486
00493 inline N_Vector MakeNVector(const std::vector<double>& rSrc)
00494 {
00495 N_Vector nv = NULL;
00496 CreateVectorIfEmpty(nv, rSrc.size());
00497 CopyFromStdVector(rSrc,nv);
00498 return nv;
00499 }
00500
00501 #endif // CHASTE_CVODE
00502
00503
00504
00505 #endif