sundials_nvector.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * -----------------------------------------------------------------
  3. * $Revision: 4075 $
  4. * $Date: 2014-04-24 10:46:58 -0700 (Thu, 24 Apr 2014) $
  5. * -----------------------------------------------------------------
  6. * Programmer(s): Radu Serban and Aaron Collier @ LLNL
  7. * -----------------------------------------------------------------
  8. * LLNS Copyright Start
  9. * Copyright (c) 2014, Lawrence Livermore National Security
  10. * This work was performed under the auspices of the U.S. Department
  11. * of Energy by Lawrence Livermore National Laboratory in part under
  12. * Contract W-7405-Eng-48 and in part under Contract DE-AC52-07NA27344.
  13. * Produced at the Lawrence Livermore National Laboratory.
  14. * All rights reserved.
  15. * For details, see the LICENSE file.
  16. * LLNS Copyright End
  17. * -----------------------------------------------------------------
  18. * This is the implementation file for a generic NVECTOR package.
  19. * It contains the implementation of the N_Vector operations listed
  20. * in nvector.h.
  21. * -----------------------------------------------------------------
  22. */
  23. #include <stdlib.h>
  24. #include <sundials/sundials_nvector.h>
  25. /*
  26. * -----------------------------------------------------------------
  27. * Functions in the 'ops' structure
  28. * -----------------------------------------------------------------
  29. */
  30. SUNDIALS_EXPORT N_Vector N_VClone(N_Vector w)
  31. {
  32. N_Vector v = NULL;
  33. v = w->ops->nvclone(w);
  34. return(v);
  35. }
  36. SUNDIALS_EXPORT N_Vector N_VCloneEmpty(N_Vector w)
  37. {
  38. N_Vector v = NULL;
  39. v = w->ops->nvcloneempty(w);
  40. return(v);
  41. }
  42. SUNDIALS_EXPORT void N_VDestroy(N_Vector v)
  43. {
  44. if (v==NULL) return;
  45. v->ops->nvdestroy(v);
  46. return;
  47. }
  48. SUNDIALS_EXPORT void N_VSpace(N_Vector v, long int *lrw, long int *liw)
  49. {
  50. v->ops->nvspace(v, lrw, liw);
  51. return;
  52. }
  53. SUNDIALS_EXPORT realtype *N_VGetArrayPointer(N_Vector v)
  54. {
  55. return((realtype *) v->ops->nvgetarraypointer(v));
  56. }
  57. SUNDIALS_EXPORT void N_VSetArrayPointer(realtype *v_data, N_Vector v)
  58. {
  59. v->ops->nvsetarraypointer(v_data, v);
  60. return;
  61. }
  62. SUNDIALS_EXPORT void N_VLinearSum(realtype a, N_Vector x, realtype b, N_Vector y, N_Vector z)
  63. {
  64. z->ops->nvlinearsum(a, x, b, y, z);
  65. return;
  66. }
  67. SUNDIALS_EXPORT void N_VConst(realtype c, N_Vector z)
  68. {
  69. z->ops->nvconst(c, z);
  70. return;
  71. }
  72. SUNDIALS_EXPORT void N_VProd(N_Vector x, N_Vector y, N_Vector z)
  73. {
  74. z->ops->nvprod(x, y, z);
  75. return;
  76. }
  77. SUNDIALS_EXPORT void N_VDiv(N_Vector x, N_Vector y, N_Vector z)
  78. {
  79. z->ops->nvdiv(x, y, z);
  80. return;
  81. }
  82. SUNDIALS_EXPORT void N_VScale(realtype c, N_Vector x, N_Vector z)
  83. {
  84. z->ops->nvscale(c, x, z);
  85. return;
  86. }
  87. SUNDIALS_EXPORT void N_VAbs(N_Vector x, N_Vector z)
  88. {
  89. z->ops->nvabs(x, z);
  90. return;
  91. }
  92. SUNDIALS_EXPORT void N_VInv(N_Vector x, N_Vector z)
  93. {
  94. z->ops->nvinv(x, z);
  95. return;
  96. }
  97. SUNDIALS_EXPORT void N_VAddConst(N_Vector x, realtype b, N_Vector z)
  98. {
  99. z->ops->nvaddconst(x, b, z);
  100. return;
  101. }
  102. SUNDIALS_EXPORT realtype N_VDotProd(N_Vector x, N_Vector y)
  103. {
  104. return((realtype) y->ops->nvdotprod(x, y));
  105. }
  106. SUNDIALS_EXPORT realtype N_VMaxNorm(N_Vector x)
  107. {
  108. return((realtype) x->ops->nvmaxnorm(x));
  109. }
  110. SUNDIALS_EXPORT realtype N_VWrmsNorm(N_Vector x, N_Vector w)
  111. {
  112. return((realtype) x->ops->nvwrmsnorm(x, w));
  113. }
  114. SUNDIALS_EXPORT realtype N_VWrmsNormMask(N_Vector x, N_Vector w, N_Vector id)
  115. {
  116. return((realtype) x->ops->nvwrmsnormmask(x, w, id));
  117. }
  118. SUNDIALS_EXPORT realtype N_VMin(N_Vector x)
  119. {
  120. return((realtype) x->ops->nvmin(x));
  121. }
  122. SUNDIALS_EXPORT realtype N_VWL2Norm(N_Vector x, N_Vector w)
  123. {
  124. return((realtype) x->ops->nvwl2norm(x, w));
  125. }
  126. SUNDIALS_EXPORT realtype N_VL1Norm(N_Vector x)
  127. {
  128. return((realtype) x->ops->nvl1norm(x));
  129. }
  130. SUNDIALS_EXPORT void N_VCompare(realtype c, N_Vector x, N_Vector z)
  131. {
  132. z->ops->nvcompare(c, x, z);
  133. return;
  134. }
  135. SUNDIALS_EXPORT booleantype N_VInvTest(N_Vector x, N_Vector z)
  136. {
  137. return((booleantype) z->ops->nvinvtest(x, z));
  138. }
  139. SUNDIALS_EXPORT booleantype N_VConstrMask(N_Vector c, N_Vector x, N_Vector m)
  140. {
  141. return((booleantype) x->ops->nvconstrmask(c, x, m));
  142. }
  143. SUNDIALS_EXPORT realtype N_VMinQuotient(N_Vector num, N_Vector denom)
  144. {
  145. return((realtype) num->ops->nvminquotient(num, denom));
  146. }
  147. /*
  148. * -----------------------------------------------------------------
  149. * Additional functions exported by the generic NVECTOR:
  150. * N_VCloneEmptyVectorArray
  151. * N_VCloneVectorArray
  152. * N_VDestroyVectorArray
  153. * -----------------------------------------------------------------
  154. */
  155. SUNDIALS_EXPORT N_Vector *N_VCloneEmptyVectorArray(int count, N_Vector w)
  156. {
  157. N_Vector *vs = NULL;
  158. int j;
  159. if (count <= 0) return(NULL);
  160. vs = (N_Vector *) malloc(count * sizeof(N_Vector));
  161. if(vs == NULL) return(NULL);
  162. for (j = 0; j < count; j++) {
  163. vs[j] = N_VCloneEmpty(w);
  164. if (vs[j] == NULL) {
  165. N_VDestroyVectorArray(vs, j-1);
  166. return(NULL);
  167. }
  168. }
  169. return(vs);
  170. }
  171. SUNDIALS_EXPORT N_Vector *N_VCloneVectorArray(int count, N_Vector w)
  172. {
  173. N_Vector *vs = NULL;
  174. int j;
  175. if (count <= 0) return(NULL);
  176. vs = (N_Vector *) malloc(count * sizeof(N_Vector));
  177. if(vs == NULL) return(NULL);
  178. for (j = 0; j < count; j++) {
  179. vs[j] = N_VClone(w);
  180. if (vs[j] == NULL) {
  181. N_VDestroyVectorArray(vs, j-1);
  182. return(NULL);
  183. }
  184. }
  185. return(vs);
  186. }
  187. SUNDIALS_EXPORT void N_VDestroyVectorArray(N_Vector *vs, int count)
  188. {
  189. int j;
  190. if (vs==NULL) return;
  191. for (j = 0; j < count; j++) N_VDestroy(vs[j]);
  192. free(vs); vs = NULL;
  193. return;
  194. }