Chaste  Release::2017.1
VectorHelperFunctions.hpp
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifndef VECTORHELPERFUNCTIONS_HPP_
37 #define VECTORHELPERFUNCTIONS_HPP_
38 
47 #include <cassert>
48 #include <vector>
49 
50 #ifdef CHASTE_CVODE
51 // CVODE headers
52 #include <nvector/nvector_serial.h>
53 #endif
54 
65 template<typename VECTOR>
66 inline double GetVectorComponent(const VECTOR& rVec, unsigned index);
67 
78 template<typename VECTOR>
79 inline void SetVectorComponent(VECTOR& rVec, unsigned index, double value);
80 
90 template<typename VECTOR>
91 inline unsigned GetVectorSize(const VECTOR& rVec);
92 
101 template<typename VECTOR>
102 inline void InitialiseEmptyVector(VECTOR& rVec);
103 
114 template<typename VECTOR>
115 inline void CreateVectorIfEmpty(VECTOR& rVec, unsigned size);
116 
122 template<typename VECTOR>
123 inline VECTOR CreateEmptyVector();
124 
131 template<typename VECTOR>
132 inline bool IsEmptyVector(VECTOR& rVec);
133 
142 template<typename VECTOR>
143 inline void DeleteVector(VECTOR& rVec);
144 
154 template<typename VECTOR>
155 inline VECTOR CopyVector(VECTOR& rVec);
156 
166 template<typename VECTOR>
167 inline void CopyToStdVector(const VECTOR& rSrc, std::vector<double>& rDest);
168 
178 template<typename VECTOR>
179 inline void CopyFromStdVector(const std::vector<double>& rSrc, VECTOR& rDest);
180 
181 // Specialisations for std::vector<double>
182 
189 template<>
190 inline double GetVectorComponent(const std::vector<double>& rVec, unsigned index)
191 {
192  assert(index < rVec.size());
193  return rVec[index];
194 }
195 
202 template<>
203 inline void SetVectorComponent(std::vector<double>& rVec, unsigned index, double value)
204 {
205  assert(index < rVec.size());
206  rVec[index] = value;
207 }
208 
214 template<>
215 inline unsigned GetVectorSize(const std::vector<double>& rVec)
216 {
217  return rVec.size();
218 }
219 
224 template<>
225 inline void InitialiseEmptyVector(std::vector<double>& rVec)
226 {
227 }
228 
234 template<>
235 inline void CreateVectorIfEmpty(std::vector<double>& rVec, unsigned size)
236 {
237  if (rVec.empty())
238  {
239  rVec.resize(size);
240  }
241 }
242 
247 template<>
248 inline std::vector<double> CreateEmptyVector()
249 {
250  return std::vector<double>();
251 }
252 
258 template<>
259 inline bool IsEmptyVector(std::vector<double>& rVec)
260 {
261  return rVec.empty();
262 }
263 
268 template<>
269 inline void DeleteVector(std::vector<double>& rVec)
270 {
271 }
272 
278 template<>
279 inline std::vector<double> CopyVector(std::vector<double>& rVec)
280 {
281  return rVec;
282 }
283 
289 template<>
290 inline void CopyToStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest)
291 {
292  rDest = rSrc;
293 }
294 
300 template<>
301 inline void CopyFromStdVector(const std::vector<double>& rSrc, std::vector<double>& rDest)
302 {
303  rDest = rSrc;
304 }
305 
306 // Specialisations for N_Vector
307 
308 #ifdef CHASTE_CVODE
309 
316 template<>
317 inline double GetVectorComponent(const N_Vector& rVec, unsigned index)
318 {
319  assert(rVec != nullptr);
320  return NV_Ith_S(rVec, index);
321 }
322 
329 template<>
330 inline void SetVectorComponent(N_Vector& rVec, unsigned index, double value)
331 {
332  assert(rVec != nullptr);
333  NV_Ith_S(rVec, index) = value;
334 }
335 
341 template<>
342 inline unsigned GetVectorSize(const N_Vector& rVec)
343 {
344  assert(rVec != nullptr);
345  return NV_LENGTH_S(rVec);
346 }
347 
352 template<>
353 inline void InitialiseEmptyVector(N_Vector& rVec)
354 {
355  rVec = nullptr;
356 }
357 
363 template<>
364 inline void CreateVectorIfEmpty(N_Vector& rVec, unsigned size)
365 {
366  if (rVec == nullptr)
367  {
368  rVec = N_VNew_Serial(size);
369  }
370 }
371 
376 template<>
378 {
379  return nullptr;
380 }
381 
387 template<>
388 inline bool IsEmptyVector(N_Vector& rVec)
389 {
390  return rVec == nullptr;
391 }
392 
397 template<>
398 inline void DeleteVector(N_Vector& rVec)
399 {
400  if (rVec)
401  {
402  rVec->ops->nvdestroy(rVec);
403  rVec = nullptr;
404  }
405 }
406 
412 template<>
414 {
415  N_Vector copy = nullptr;
416  if (rVec)
417  {
418  copy = N_VClone(rVec);
419  unsigned size = NV_LENGTH_S(rVec);
420  for (unsigned i=0; i<size; i++)
421  {
422  NV_Ith_S(copy, i) = NV_Ith_S(rVec, i);
423  }
424  }
425  return copy;
426 }
427 
434 template<>
435 inline void CopyToStdVector(const N_Vector& rSrc, std::vector<double>& rDest)
436 {
437  // Check for no-op
438  realtype* p_src = NV_DATA_S(rSrc);
439  if (!rDest.empty() && p_src == &(rDest[0])) return;
440  // Set dest size
441  long size = NV_LENGTH_S(rSrc);
442  rDest.resize(size);
443  // Copy data
444  for (long i=0; i<size; i++)
445  {
446  rDest[i] = p_src[i];
447  }
448 }
449 
456 template<>
457 inline void CopyFromStdVector(const std::vector<double>& rSrc, N_Vector& rDest)
458 {
459  // Check for no-op
460  realtype* p_dest = NV_DATA_S(rDest);
461  if (p_dest == &(rSrc[0])) return;
462 
463  // Check dest size
464  long size = NV_LENGTH_S(rDest);
465  assert(size == (long)rSrc.size());
466 
467  // Copy data
468  for (long i=0; i<size; i++)
469  {
470  p_dest[i] = rSrc[i];
471  }
472 }
473 
480 inline std::vector<double> MakeStdVec(N_Vector v)
481 {
482  std::vector<double> sv;
483  CopyToStdVector(v, sv);
484  return sv;
485 }
486 
493 inline N_Vector MakeNVector(const std::vector<double>& rSrc)
494 {
495  N_Vector nv = nullptr;
496  CreateVectorIfEmpty(nv, rSrc.size());
497  CopyFromStdVector(rSrc,nv);
498  return nv;
499 }
500 
501 #endif // CHASTE_CVODE
502 
503 // End of helper functions
504 
505 #endif /*VECTORHELPERFUNCTIONS_HPP_*/
std::vector< double > MakeStdVec(N_Vector v)
VECTOR CreateEmptyVector()
VECTOR CopyVector(VECTOR &rVec)
N_Vector MakeNVector(const std::vector< double > &rSrc)
void InitialiseEmptyVector(VECTOR &rVec)
void CreateVectorIfEmpty(VECTOR &rVec, unsigned size)
void SetVectorComponent(VECTOR &rVec, unsigned index, double value)
void CopyFromStdVector(const std::vector< double > &rSrc, VECTOR &rDest)
void CopyToStdVector(const VECTOR &rSrc, std::vector< double > &rDest)
double GetVectorComponent(const VECTOR &rVec, unsigned index)
void DeleteVector(VECTOR &rVec)
unsigned GetVectorSize(const VECTOR &rVec)
bool IsEmptyVector(VECTOR &rVec)