Chaste Release::3.1
|
00001 /* 00002 00003 Copyright (c) 2005-2012, University of Oxford. 00004 All rights reserved. 00005 00006 University of Oxford means the Chancellor, Masters and Scholars of the 00007 University of Oxford, having an administrative office at Wellington 00008 Square, Oxford OX1 2JD, UK. 00009 00010 This file is part of Chaste. 00011 00012 Redistribution and use in source and binary forms, with or without 00013 modification, are permitted provided that the following conditions are met: 00014 * Redistributions of source code must retain the above copyright notice, 00015 this list of conditions and the following disclaimer. 00016 * Redistributions in binary form must reproduce the above copyright notice, 00017 this list of conditions and the following disclaimer in the documentation 00018 and/or other materials provided with the distribution. 00019 * Neither the name of the University of Oxford nor the names of its 00020 contributors may be used to endorse or promote products derived from this 00021 software without specific prior written permission. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 00034 */ 00035 00036 #ifndef VECTORHELPERFUNCTIONS_HPP_ 00037 #define VECTORHELPERFUNCTIONS_HPP_ 00038 00047 #include <cassert> 00048 #include <vector> 00049 00050 #ifdef CHASTE_CVODE 00051 // CVODE headers 00052 #include <nvector/nvector_serial.h> 00053 #endif 00054 00064 template<typename VECTOR> 00065 inline double GetVectorComponent(const VECTOR& rVec, unsigned index); 00066 00077 template<typename VECTOR> 00078 inline void SetVectorComponent(VECTOR& rVec, unsigned index, double value); 00079 00089 template<typename VECTOR> 00090 inline unsigned GetVectorSize(const VECTOR& rVec); 00091 00100 template<typename VECTOR> 00101 inline void InitialiseEmptyVector(VECTOR& rVec); 00102 00113 template<typename VECTOR> 00114 inline void CreateVectorIfEmpty(VECTOR& rVec, unsigned size); 00115 00121 template<typename VECTOR> 00122 inline VECTOR CreateEmptyVector(); 00123 00129 template<typename VECTOR> 00130 inline bool IsEmptyVector(VECTOR& rVec); 00131 00140 template<typename VECTOR> 00141 inline void DeleteVector(VECTOR& rVec); 00142 00151 template<typename VECTOR> 00152 inline VECTOR CopyVector(VECTOR& rVec); 00153 00163 template<typename VECTOR> 00164 inline void CopyToStdVector(const VECTOR& rSrc, std::vector<double>& rDest); 00165 00175 template<typename VECTOR> 00176 inline void CopyFromStdVector(const std::vector<double>& rSrc, VECTOR& rDest); 00177 00178 // Specialisations for std::vector<double> 00179 00185 template<> 00186 inline double GetVectorComponent(const std::vector<double>& rVec, unsigned index) 00187 { 00188 assert(index < rVec.size()); 00189 return rVec[index]; 00190 } 00191 00198 template<> 00199 inline void SetVectorComponent(std::vector<double>& rVec, unsigned index, double value) 00200 { 00201 assert(index < rVec.size()); 00202 rVec[index] = value; 00203 } 00204 00209 template<> 00210 inline unsigned GetVectorSize(const std::vector<double>& rVec) 00211 { 00212 return rVec.size(); 00213 } 00214 00219 template<> 00220 inline void InitialiseEmptyVector(std::vector<double>& rVec) 00221 { 00222 } 00223 00229 template<> 00230 inline void CreateVectorIfEmpty(std::vector<double>& rVec, unsigned size) 00231 { 00232 if (rVec.empty()) 00233 { 00234 rVec.resize(size); 00235 } 00236 } 00237 00241 template<> 00242 inline std::vector<double> CreateEmptyVector() 00243 { 00244 return std::vector<double>(); 00245 } 00246 00251 template<> 00252 inline bool IsEmptyVector(std::vector<double>& rVec) 00253 { 00254 return rVec.empty(); 00255 } 00256 00261 template<> 00262 inline void DeleteVector(std::vector<double>& rVec) 00263 { 00264 } 00265 00270 template<> 00271 inline std::vector<double> CopyVector(std::vector<double>& rVec) 00272 { 00273 return rVec; 00274 } 00275 00281 template<> 00282 inline void CopyToStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest) 00283 { 00284 rDest = rSrc; 00285 } 00286 00292 template<> 00293 inline void CopyFromStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest) 00294 { 00295 rDest = rSrc; 00296 } 00297 00298 // Specialisations for N_Vector 00299 00300 #ifdef CHASTE_CVODE 00301 00307 template<> 00308 inline double GetVectorComponent(const N_Vector& rVec, unsigned index) 00309 { 00310 assert(rVec != NULL); 00311 return NV_Ith_S(rVec, index); 00312 } 00313 00320 template<> 00321 inline void SetVectorComponent(N_Vector& rVec, unsigned index, double value) 00322 { 00323 assert(rVec != NULL); 00324 NV_Ith_S(rVec, index) = value; 00325 } 00326 00331 template<> 00332 inline unsigned GetVectorSize(const N_Vector& rVec) 00333 { 00334 assert(rVec != NULL); 00335 return NV_LENGTH_S(rVec); 00336 } 00337 00342 template<> 00343 inline void InitialiseEmptyVector(N_Vector& rVec) 00344 { 00345 rVec = NULL; 00346 } 00347 00353 template<> 00354 inline void CreateVectorIfEmpty(N_Vector& rVec, unsigned size) 00355 { 00356 if (rVec == NULL) 00357 { 00358 rVec = N_VNew_Serial(size); 00359 } 00360 } 00361 00365 template<> 00366 inline N_Vector CreateEmptyVector() 00367 { 00368 return NULL; 00369 } 00370 00375 template<> 00376 inline bool IsEmptyVector(N_Vector& rVec) 00377 { 00378 return rVec == NULL; 00379 } 00380 00385 template<> 00386 inline void DeleteVector(N_Vector& rVec) 00387 { 00388 if (rVec) 00389 { 00390 rVec->ops->nvdestroy(rVec); 00391 rVec = NULL; 00392 } 00393 } 00394 00399 template<> 00400 inline N_Vector CopyVector(N_Vector& rVec) 00401 { 00402 N_Vector copy = NULL; 00403 if (rVec) 00404 { 00405 copy = N_VClone(rVec); 00406 unsigned size = NV_LENGTH_S(rVec); 00407 for (unsigned i=0; i<size; i++) 00408 { 00409 NV_Ith_S(copy, i) = NV_Ith_S(rVec, i); 00410 } 00411 } 00412 return copy; 00413 } 00414 00421 template<> 00422 inline void CopyToStdVector(const N_Vector& rSrc, std::vector<double>& rDest) 00423 { 00424 // Check for no-op 00425 realtype* p_src = NV_DATA_S(rSrc); 00426 if (p_src == &(rDest[0])) return; 00427 // Set dest size 00428 long size = NV_LENGTH_S(rSrc); 00429 rDest.resize(size); 00430 // Copy data 00431 for (long i=0; i<size; i++) 00432 { 00433 rDest[i] = p_src[i]; 00434 } 00435 } 00436 00443 template<> 00444 inline void CopyFromStdVector(const std::vector<double>& rSrc, N_Vector& rDest) 00445 { 00446 // Check for no-op 00447 realtype* p_dest = NV_DATA_S(rDest); 00448 if (p_dest == &(rSrc[0])) return; 00449 00450 // Check dest size 00451 long size = NV_LENGTH_S(rDest); 00452 assert(size == (long)rSrc.size()); 00453 00454 // Copy data 00455 for (long i=0; i<size; i++) 00456 { 00457 p_dest[i] = rSrc[i]; 00458 } 00459 } 00460 00467 inline std::vector<double> MakeStdVec(N_Vector v) 00468 { 00469 std::vector<double> sv; 00470 CopyToStdVector(v, sv); 00471 return sv; 00472 } 00473 00480 inline N_Vector MakeNVector(const std::vector<double>& rSrc) 00481 { 00482 N_Vector nv = NULL; 00483 CreateVectorIfEmpty(nv, rSrc.size()); 00484 CopyFromStdVector(rSrc,nv); 00485 return nv; 00486 } 00487 00488 #endif // CHASTE_CVODE 00489 00490 // End of helper functions 00491 00492 #endif /*VECTORHELPERFUNCTIONS_HPP_*/