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 #ifndef VECTORHELPERFUNCTIONS_HPP_
00030 #define VECTORHELPERFUNCTIONS_HPP_
00031
00040 #include <cassert>
00041 #include <vector>
00042
00043 #ifdef CHASTE_CVODE
00044
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
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
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
00418 realtype* p_src = NV_DATA_S(rSrc);
00419 if (p_src == &(rDest[0])) return;
00420
00421 long size = NV_LENGTH_S(rSrc);
00422 rDest.resize(size);
00423
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
00440 realtype* p_dest = NV_DATA_S(rDest);
00441 if (p_dest == &(rSrc[0])) return;
00442
00443
00444 long size = NV_LENGTH_S(rDest);
00445 assert(size == (long)rSrc.size());
00446
00447
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
00470
00471 #endif