You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sort_RAW_File.cc 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**********************************************************
  2. This software is part of J.-S. Caux's ABACUS library.
  3. Copyright (c) J.-S. Caux.
  4. -----------------------------------------------------------
  5. File: Sort_RAW_File.cc
  6. Purpose: orders the file in form factor or energy.
  7. ***********************************************************/
  8. #include "ABACUS.h"
  9. using namespace std;
  10. namespace ABACUS {
  11. void Sort_RAW_File (const char ffsq_file[], char optionchar)
  12. {
  13. Sort_RAW_File (ffsq_file, optionchar, 'a'); // dummy whichDSF, doesn't matter
  14. }
  15. void Sort_RAW_File (const char ff_file[], char optionchar, char whichDSF)
  16. {
  17. // Sorts FF in decreasing order for 'f' option, or energies in increasing order for option 'e', and writes .dat_srt file
  18. if (!(optionchar == 'e' || optionchar == 'f')) ABACUSerror("Wrong option in Sort_FF");
  19. // Check size of threads file:
  20. struct stat statbuf;
  21. stat (ff_file, &statbuf);
  22. int filesize = statbuf.st_size;
  23. // Determine the number of entries approximately
  24. int entry_size = 2* sizeof(float) + 2*sizeof(int);
  25. //const int MAXDATA = 50000000;
  26. const int MAXDATA = filesize/entry_size + 10;
  27. DP* omega = new DP[MAXDATA];
  28. int* iK = new int[MAXDATA];
  29. DP* ff = new DP[MAXDATA];
  30. DP* ff_im = new DP[MAXDATA];
  31. DP* dev = new DP[MAXDATA];
  32. string* label = new string[MAXDATA];
  33. ifstream infile;
  34. infile.open(ff_file);
  35. if (infile.fail()) ABACUSerror("The input file was not opened successfully in Sort_RAW_File. ");
  36. stringstream outfilename;
  37. string outfilename_string;
  38. outfilename << ff_file << "_srt_" << optionchar;
  39. outfilename_string = outfilename.str();
  40. const char* outfilename_c_str = outfilename_string.c_str();
  41. ofstream outfile;
  42. outfile.open(outfilename_c_str);
  43. outfile.precision(16);
  44. int Ndata = 0;
  45. while (((infile.peek()) != EOF) && (Ndata < MAXDATA)) {
  46. infile >> omega[Ndata];
  47. infile >> iK[Ndata];
  48. if (whichDSF != 'Z') infile >> ff[Ndata];
  49. if (whichDSF == 'q') infile >> ff_im[Ndata]; // imaginary part of overlap, quench case
  50. infile >> dev[Ndata];
  51. infile >> label[Ndata];
  52. Ndata++;
  53. }
  54. infile.close();
  55. int* index = new int[Ndata];
  56. DP* ffsq = new DP[Ndata];
  57. for (int i = 0; i < Ndata; ++i) index[i] = i;
  58. for (int i = 0; i < Ndata; ++i) ffsq[i] = ff[i] * ff[i];
  59. if (optionchar == 'f') QuickSort(ffsq, index, 0, Ndata - 1);
  60. else if (optionchar == 'e') QuickSort(omega, index, 0, Ndata - 1);
  61. for (int i = 0; i < Ndata; i++) {
  62. if (i > 0) outfile << endl;
  63. if (optionchar == 'f') {
  64. outfile << omega[index[Ndata - 1 - i] ] << "\t" << iK[index[Ndata - 1 - i] ];
  65. if (whichDSF != 'Z') outfile << "\t" << ff[index[Ndata - 1 - i] ];
  66. if (whichDSF == 'q') outfile << "\t" << ff_im[index[Ndata - 1 - i] ];
  67. outfile << "\t" << dev[index[Ndata - 1 - i] ] << "\t" << label[index[Ndata - 1 - i] ];
  68. }
  69. else if (optionchar == 'e') {
  70. outfile << omega[i] << "\t" << iK[index[i] ];
  71. if (whichDSF != 'Z') outfile << "\t" << ff[index[i] ];
  72. if (whichDSF == 'q') outfile << "\t" << ff_im[index[i] ];
  73. outfile << "\t" << dev[index[i] ] << "\t" << label[index[i] ];
  74. }
  75. }
  76. outfile.close();
  77. delete[] omega;
  78. delete[] iK;
  79. delete[] ff;
  80. delete[] ff_im;
  81. delete[] dev;
  82. delete[] label;
  83. delete[] index;
  84. delete[] ffsq;
  85. return;
  86. }
  87. } // namespace ABACUS