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
00117 template<typename VECTOR>
00118 inline void DeleteVector(VECTOR& rVec);
00119
00128 template<typename VECTOR>
00129 inline VECTOR CopyVector(VECTOR& rVec);
00130
00140 template<typename VECTOR>
00141 inline void CopyToStdVector(const VECTOR& rSrc, std::vector<double>& rDest);
00142
00152 template<typename VECTOR>
00153 inline void CopyFromStdVector(const std::vector<double>& rSrc, VECTOR& rDest);
00154
00155
00156
00157
00158
00159
00165 template<>
00166 inline double GetVectorComponent(const std::vector<double>& rVec, unsigned index)
00167 {
00168 assert(index < rVec.size());
00169 return rVec[index];
00170 }
00171
00178 template<>
00179 inline void SetVectorComponent(std::vector<double>& rVec, unsigned index, double value)
00180 {
00181 assert(index < rVec.size());
00182 rVec[index] = value;
00183 }
00184
00189 template<>
00190 inline unsigned GetVectorSize(const std::vector<double>& rVec)
00191 {
00192 return rVec.size();
00193 }
00194
00199 template<>
00200 inline void InitialiseEmptyVector(std::vector<double>& rVec)
00201 {
00202 }
00203
00209 template<>
00210 inline void CreateVectorIfEmpty(std::vector<double>& rVec, unsigned size)
00211 {
00212 if (rVec.empty())
00213 {
00214 rVec.resize(size);
00215 }
00216 }
00217
00222 template<>
00223 inline void DeleteVector(std::vector<double>& rVec)
00224 {
00225 }
00226
00231 template<>
00232 inline std::vector<double> CopyVector(std::vector<double>& rVec)
00233 {
00234 return rVec;
00235 }
00236
00242 template<>
00243 inline void CopyToStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest)
00244 {
00245 rDest = rSrc;
00246 }
00247
00253 template<>
00254 inline void CopyFromStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest)
00255 {
00256 rDest = rSrc;
00257 }
00258
00259
00260
00261
00262
00263
00264 #ifdef CHASTE_CVODE
00265
00271 template<>
00272 inline double GetVectorComponent(const N_Vector& rVec, unsigned index)
00273 {
00274 assert(rVec != NULL);
00275 return NV_Ith_S(rVec, index);
00276 }
00277
00284 template<>
00285 inline void SetVectorComponent(N_Vector& rVec, unsigned index, double value)
00286 {
00287 assert(rVec != NULL);
00288 NV_Ith_S(rVec, index) = value;
00289 }
00290
00295 template<>
00296 inline unsigned GetVectorSize(const N_Vector& rVec)
00297 {
00298 assert(rVec != NULL);
00299 return NV_LENGTH_S(rVec);
00300 }
00301
00306 template<>
00307 inline void InitialiseEmptyVector(N_Vector& rVec)
00308 {
00309 rVec = NULL;
00310 }
00311
00317 template<>
00318 inline void CreateVectorIfEmpty(N_Vector& rVec, unsigned size)
00319 {
00320 if (rVec == NULL)
00321 {
00322 rVec = N_VNew_Serial(size);
00323 }
00324 }
00325
00330 template<>
00331 inline void DeleteVector(N_Vector& rVec)
00332 {
00333 if (rVec)
00334 {
00335 rVec->ops->nvdestroy(rVec);
00336 rVec = NULL;
00337 }
00338 }
00339
00344 template<>
00345 inline N_Vector CopyVector(N_Vector& rVec)
00346 {
00347 N_Vector copy = NULL;
00348 if (rVec)
00349 {
00350 copy = N_VClone(rVec);
00351 unsigned size = NV_LENGTH_S(rVec);
00352 for (unsigned i=0; i<size; i++)
00353 {
00354 NV_Ith_S(copy, i) = NV_Ith_S(rVec, i);
00355 }
00356 }
00357 return copy;
00358 }
00359
00366 template<>
00367 inline void CopyToStdVector(const N_Vector& rSrc, std::vector<double>& rDest)
00368 {
00369
00370 realtype* p_src = NV_DATA_S(rSrc);
00371 if (p_src == &(rDest[0])) return;
00372
00373 long size = NV_LENGTH_S(rSrc);
00374 rDest.resize(size);
00375
00376 for (long i=0; i<size; i++)
00377 {
00378 rDest[i] = p_src[i];
00379 }
00380 }
00381
00388 template<>
00389 inline void CopyFromStdVector(const std::vector<double>& rSrc, N_Vector& rDest)
00390 {
00391
00392 realtype* p_dest = NV_DATA_S(rDest);
00393 if (p_dest == &(rSrc[0])) return;
00394
00395 long size = NV_LENGTH_S(rDest);
00396 assert(size == (long)rSrc.size());
00397
00398 for (long i=0; i<size; i++)
00399 {
00400 p_dest[i] = rSrc[i];
00401 }
00402 }
00403 #endif // CHASTE_CVODE
00404
00405
00406
00407
00408
00409
00410 #endif