rdf.cpp 5.4 KB

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