/nemo_cvs/src/nbody/glnemo/src/particles_data.cpp

https://github.com/Milkyway-at-home/nemo · C++ · 181 lines · 134 code · 12 blank · 35 comment · 31 complexity · 5b4e83071acac2c2926fa3645e874bc3 MD5 · raw file

  1. // ============================================================================
  2. // Copyright Jean-Charles LAMBERT - 2004-2007
  3. // e-mail: Jean-Charles.Lambert@oamp.fr
  4. // address: Dynamique des galaxies
  5. // Laboratoire d'Astrophysique de Marseille
  6. // 2, place Le Verrier
  7. // 13248 Marseille Cedex 4, France
  8. // CNRS U.M.R 6110
  9. // ============================================================================
  10. // See the complete license in LICENSE and/or "http://www.cecill.info".
  11. // ============================================================================
  12. #include "particles_data.h"
  13. #include <stdlib.h>
  14. #include <assert.h>
  15. #include <iostream>
  16. #include <math.h>
  17. // ============================================================================
  18. // Constructor
  19. ParticlesData::ParticlesData()
  20. {
  21. pos = NULL;
  22. vel = NULL;
  23. vel_norm = NULL;
  24. timu = NULL;
  25. nbody = NULL;
  26. nemobits = NULL;
  27. tree_depth = NULL;
  28. coo_max[0] = coo_max[1] = coo_max[2] = 0.0;
  29. i_max[0] = i_max[1] = i_max[2] = 0;
  30. tree_size_max = 1000000;
  31. }
  32. // ============================================================================
  33. // copy Constructor
  34. const ParticlesData& ParticlesData::operator=(const ParticlesData& m)
  35. {
  36. if (m.nbody) {
  37. nbody = (int *) mallocate((char *) nbody, sizeof(int), true);
  38. *nbody = *m.nbody;
  39. tree_depth = (int *) mallocate((char *) tree_depth, sizeof(int)* (*nbody), true);
  40. if (m.pos) {
  41. pos = (float *) mallocate((char *) pos, sizeof (float) * 3 * (*nbody), true);
  42. //memcpy((float *) pos, (float *) m.pos, sizeof(float)* (*nbody) * 3);
  43. for (int i=0; i < (*nbody); i++) {
  44. pos[i*3] = m.pos[i*3];
  45. pos[i*3+1] = m.pos[i*3+1];
  46. pos[i*3+2] = m.pos[i*3+2];
  47. }
  48. }
  49. if (m.vel) {
  50. vel = (float *) mallocate((char *) vel, sizeof (float) * 3 * (*nbody), true);
  51. //memcpy((float *) vel, (float *) m.vel, sizeof(float)* (*nbody) * 3);
  52. for (int i=0; i < (*nbody); i++) {
  53. vel[i*3] = m.vel[i*3];
  54. vel[i*3+1] = m.vel[i*3+1];
  55. vel[i*3+2] = m.vel[i*3+2];
  56. }
  57. }
  58. else {
  59. if (vel)
  60. free ((float *) vel);
  61. vel = NULL;
  62. }
  63. if (m.vel_norm) {
  64. vel_norm = (float *) mallocate((char *) vel_norm, sizeof (float) * (*nbody), true);
  65. //memcpy((float *) vel_norm, (float *) m.vel_norm, sizeof(float)* (*nbody));
  66. for (int i=0; i < (*nbody); i++) {
  67. vel_norm[i] = m.vel_norm[i];
  68. }
  69. }
  70. else {
  71. if (vel_norm)
  72. free ((float *) vel_norm);
  73. vel_norm = NULL;
  74. }
  75. //memcpy((float *) coo_max, (float *) m.coo_max, sizeof(float)* 3);
  76. //memcpy((int *) i_max, (int *) m.i_max, sizeof(int)* 3);
  77. for (int i=0; i <3; i++) {
  78. coo_max[i] = m.coo_max[i];
  79. i_max[i] = m.i_max[i];
  80. }
  81. if (m.nemobits) {
  82. nemobits = (int *) mallocate((char *) nemobits, sizeof(int), true);
  83. *nemobits = *m.nemobits;
  84. }
  85. if (m.timu) {
  86. timu = (float *) mallocate((char *) timu, sizeof(float), true);
  87. *timu = *m.timu;
  88. }
  89. }
  90. return *this;
  91. }
  92. // ============================================================================
  93. // Destructor
  94. // we use the "free" C statement because io_nemo use malloc to alloc data and
  95. // you cannot "delete" a pointer allocate with malloc
  96. ParticlesData::~ParticlesData()
  97. {
  98. if (pos)
  99. free ((float *) pos);
  100. if (vel)
  101. free ((float *) vel);
  102. if (vel_norm)
  103. free ((float *) vel_norm);
  104. if (timu)
  105. free ((float *) timu);
  106. if (nbody)
  107. free ((int *) nbody);
  108. if (nemobits)
  109. free ((int *) nemobits);
  110. if (tree_depth)
  111. free ((int *) tree_depth);
  112. }
  113. // ============================================================================
  114. // ParticlesData::mallocate
  115. char * ParticlesData::mallocate(char * p, int lg, bool force)
  116. {
  117. char * ptr;
  118. if (p == NULL || force) {
  119. if (force && p ) {
  120. free ((char *) p);
  121. }
  122. ptr = (char *) malloc(sizeof(char)*lg);
  123. if (!ptr) {
  124. std::cerr << "[allocate_pointer], allocation memory error, aborted\n";
  125. exit(1);
  126. }
  127. return ptr;
  128. }
  129. else /* assume p is already allocated */
  130. return p;
  131. }
  132. // ============================================================================
  133. // ParticlesData::allocVar
  134. int ParticlesData::allocVar()
  135. {
  136. nbody = (int *) mallocate((char *) nbody , sizeof(int));
  137. //tree_depth = (int *) mallocate((char *) tree_depth, sizeof(int)* (*nbody));
  138. //tree_depth = (int *) mallocate((char *) tree_depth, sizeof(int));
  139. nemobits = (int *) mallocate((char *) nemobits, sizeof(int));
  140. timu = (float *) mallocate((char *) timu , sizeof(float));
  141. *nbody = *nemobits = -1;
  142. *timu = -1.;
  143. return 1;
  144. }
  145. // ============================================================================
  146. // ParticlesData::allocTree
  147. int ParticlesData::allocTree()
  148. {
  149. tree_depth = (int *) mallocate((char *) tree_depth, sizeof(int)* (*nbody),true);
  150. for (int i=0; i<*nbody; i++) {
  151. tree_depth[i] = 1;
  152. }
  153. return 1;
  154. }
  155. // ============================================================================
  156. // ParticlesData::computeVelNorm()
  157. void ParticlesData::computeVelNorm()
  158. {
  159. if (vel) {
  160. vel_norm = (float *) mallocate((char *) vel_norm, sizeof(float)*(*nbody),true);
  161. for (int i=0; i<(*nbody); i++) {
  162. float vx=vel[i*3];
  163. float vy=vel[i*3 + 1];
  164. float vz=vel[i*3 + 2];
  165. vel_norm[i] = sqrt(vx*vx + vy*vy + vz*vz);
  166. }
  167. }
  168. }
  169. // ============================================================================