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
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
00131 realtype* p_src = NV_DATA_S(src);
00132 if (p_src == &(rDest[0])) return;
00133
00134 long size = NV_LENGTH_S(src);
00135 rDest.resize(size);
00136
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
00154 realtype* p_dest = NV_DATA_S(dest);
00155 if (p_dest == &(rSrc[0])) return;
00156
00157 long size = NV_LENGTH_S(dest);
00158 assert(size == (long)rSrc.size());
00159
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
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
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
00325
00326
00327
00328 #endif