rdf.cpp 5.7 KB

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