rdf.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright (c) 2021 NVIDIA Corporation. All rights reserved.
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <cstdio>
  8. #include <iomanip>
  9. #include "dcdread.h"
  10. #include <assert.h>
  11. #include <Kokkos_Core.hpp>
  12. #include <nvToolsExt.h>
  13. int l_round(float num);
  14. typedef Kokkos::View<double *> view_type_double;
  15. typedef Kokkos::View<unsigned long long int *> view_type_long;
  16. typedef view_type_double::HostMirror host_view_type_double;
  17. typedef view_type_long::HostMirror host_view_type_long;
  18. void pair_gpu(view_type_double d_x, view_type_double d_y, view_type_double d_z,
  19. view_type_long d_g2, int numatm, int nconf,
  20. const double xbox, const double ybox, const double zbox,
  21. int d_bin);
  22. int main(int argc, char *argv[])
  23. {
  24. Kokkos::initialize(argc, argv);
  25. {
  26. printf("Default Kokkos execution space %s\n",
  27. typeid(Kokkos::DefaultExecutionSpace).name());
  28. double xbox, ybox, zbox;
  29. int nbin;
  30. int numatm, nconf, inconf;
  31. string file;
  32. ///////////////////////////////////////////////////////////////
  33. inconf = 10;
  34. nbin = 2000;
  35. file = "../input/alk.traj.dcd";
  36. ///////////////////////////////////////
  37. std::ifstream infile;
  38. infile.open(file.c_str());
  39. if (!infile)
  40. {
  41. cout << "file " << file.c_str() << " not found\n";
  42. return 1;
  43. }
  44. assert(infile);
  45. ofstream pairfile, stwo;
  46. pairfile.open("RDF.dat");
  47. stwo.open("Pair_entropy.dat");
  48. /////////////////////////////////////////////////////////
  49. dcdreadhead(&numatm, &nconf, infile);
  50. cout << "Dcd file has " << numatm << " atoms and " << nconf << " frames" << endl;
  51. if (inconf > nconf)
  52. cout << "nconf is reset to " << nconf << endl;
  53. else
  54. {
  55. nconf = inconf;
  56. }
  57. cout << "Calculating RDF for " << nconf << " frames" << endl;
  58. ////////////////////////////////////////////////////////
  59. view_type_double x("x", nconf * numatm);
  60. view_type_double y("y", nconf * numatm);
  61. view_type_double z("z", nconf * numatm);
  62. view_type_long g2("g2", nbin);
  63. host_view_type_double h_x = Kokkos::create_mirror_view(x);
  64. host_view_type_double h_y = Kokkos::create_mirror_view(y);
  65. host_view_type_double h_z = Kokkos::create_mirror_view(z);
  66. host_view_type_long h_g2 = Kokkos::create_mirror_view(g2);
  67. /////////reading cordinates//////////////////////////////////////////////
  68. nvtxRangePush("Read_File");
  69. double ax[numatm], ay[numatm], az[numatm];
  70. for (int i = 0; i < nconf; i++)
  71. {
  72. dcdreadframe(ax, ay, az, infile, numatm, xbox, ybox, zbox);
  73. for (int j = 0; j < numatm; j++)
  74. {
  75. h_x(i * numatm + j) = ax[j];
  76. h_y(i * numatm + j) = ay[j];
  77. h_z(i * numatm + j) = az[j];
  78. }
  79. }
  80. for (int i = 0; i < nbin; i++)
  81. h_g2(0) = 0;
  82. nvtxRangePop(); //pop for Reading file
  83. cout << "Reading of input file is completed" << endl;
  84. nvtxRangePush("Pair_Calculation");
  85. Kokkos::deep_copy(x, h_x);
  86. Kokkos::deep_copy(y, h_y);
  87. Kokkos::deep_copy(z, h_z);
  88. Kokkos::deep_copy(g2, h_g2);
  89. //////////////////////////////////////////////////////////////////////////
  90. pair_gpu(x, y, z, g2, numatm, nconf, xbox, ybox, zbox, nbin);
  91. Kokkos::deep_copy(h_g2, g2);
  92. nvtxRangePop(); //Pop for Pair Calculation
  93. double pi = acos(-1.0l);
  94. double rho = (numatm) / (xbox * ybox * zbox);
  95. double norm = (4.0l * pi * rho) / 3.0l;
  96. double rl, ru, nideal;
  97. double t_g2[nbin];
  98. double r, gr, lngr, lngrbond, s2 = 0.0l, s2bond = 0.0l;
  99. double box = min(xbox, ybox);
  100. box = min(box, zbox);
  101. double del = box / (2.0l * nbin);
  102. nvtxRangePush("Entropy_Calculation");
  103. for (int i = 0; i < nbin; i++)
  104. {
  105. rl = (i)*del;
  106. ru = rl + del;
  107. nideal = norm * (ru * ru * ru - rl * rl * rl);
  108. t_g2[i] = (double)h_g2(i) / ((double)nconf * (double)numatm * nideal);
  109. r = (i)*del;
  110. pairfile << (i + 0.5l) * del << " " << t_g2[i] << endl;
  111. if (r < 2.0l)
  112. {
  113. gr = 0.0l;
  114. }
  115. else
  116. {
  117. gr = t_g2[i];
  118. }
  119. if (gr < 1e-5)
  120. {
  121. lngr = 0.0l;
  122. }
  123. else
  124. {
  125. lngr = log(gr);
  126. }
  127. if (t_g2[i] < 1e-6)
  128. {
  129. lngrbond = 0.0l;
  130. }
  131. else
  132. {
  133. lngrbond = log(t_g2[i]);
  134. }
  135. s2 = s2 - 2.0l * pi * rho * ((gr * lngr) - gr + 1.0l) * del * r * r;
  136. s2bond = s2bond - 2.0l * pi * rho * ((t_g2[i] * lngrbond) - t_g2[i] + 1.0l) * del * r * r;
  137. }
  138. nvtxRangePop(); //Pop for Entropy Calculation
  139. stwo << "s2 value is " << s2 << endl;
  140. stwo << "s2bond value is " << s2bond << endl;
  141. cout << "#Freeing Host memory" << endl;
  142. cout << "#Number of atoms processed: " << numatm << endl
  143. << endl;
  144. cout << "#Number of confs processed: " << nconf << endl
  145. << endl;
  146. } // Kokkos Initialize ends here
  147. Kokkos::finalize();
  148. return 0;
  149. }
  150. int l_round(float num)
  151. {
  152. return num < 0 ? num - 0.5 : num + 0.5;
  153. }
  154. void pair_gpu(view_type_double d_x, view_type_double d_y, view_type_double d_z,
  155. view_type_long d_g2, int numatm, int nconf,
  156. const double xbox, const double ybox, const double zbox,
  157. int d_bin)
  158. {
  159. printf("\n %d %d ", nconf, numatm);
  160. for (int frame = 0; frame < nconf; frame++)
  161. {
  162. printf("\n %d ", frame);
  163. Kokkos::parallel_for((numatm * numatm), KOKKOS_LAMBDA(const int index) {
  164. int id1 = index / numatm;
  165. int id2 = index % numatm;
  166. double r, cut, dx, dy, dz;
  167. int ig2;
  168. double box;
  169. int myround;
  170. float num;
  171. box = min(xbox, ybox);
  172. box = min(box, zbox);
  173. double del = box / (2.0 * d_bin);
  174. cut = box * 0.5;
  175. dx = d_x(frame * numatm + id1) - d_x(frame * numatm + id2);
  176. dy = d_y(frame * numatm + id1) - d_y(frame * numatm + id2);
  177. dz = d_z(frame * numatm + id1) - d_z(frame * numatm + id2);
  178. num = dx / xbox;
  179. myround = num < 0 ? num - 0.5 : num + 0.5;
  180. dx = dx - xbox * myround;
  181. num = dy / ybox;
  182. myround = num < 0 ? num - 0.5 : num + 0.5;
  183. dy = dy - ybox * myround;
  184. num = dz / zbox;
  185. myround = num < 0 ? num - 0.5 : num + 0.5;
  186. dz = dz - zbox * myround;
  187. r = sqrtf(dx * dx + dy * dy + dz * dz);
  188. if (r < cut)
  189. {
  190. ig2 = (int)(r / del);
  191. Kokkos::atomic_increment(&d_g2(ig2));
  192. }
  193. });
  194. }
  195. }