Advertisement
kss

1.cpp

kss
Oct 26th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <iostream>
  2. //#include "math.h"
  3. #include <vector>
  4. #include <fstream>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. void readFile();    // Read data from file
  10. void calculate();
  11. int sign(double);   // signed function
  12.  
  13. vector<double> m, Eg, Ec, dSO, En;
  14.  
  15. int main() {
  16. // read data from file
  17.  
  18.     readFile();
  19.  
  20. // reference point of time
  21.  
  22.     clock_t begin = clock();
  23.  
  24. // the function which shoud be improved
  25.  
  26.     calculate();
  27.  
  28. // end of time
  29. clock_t end = clock();
  30.  
  31. // spent time (sec)
  32. double ec_time = double(end - begin) / CLOCKS_PER_SEC;
  33.  
  34. cout << "time: " << ec_time << "\n";
  35.  
  36. cout << En[0] << "\n"
  37.      << En[1] << "\n"
  38.      << En[2] << "\n"
  39.      << En[3] << "\n"
  40.      << En[4] << "\n";
  41. }
  42.  
  43. int sign (double Value){
  44.     int sValue=-1;
  45.     if (Value > 0) sValue=1;
  46.     return sValue;
  47. }
  48.  
  49. void readFile() {
  50.  
  51.     ifstream infile;
  52.             infile.open ("1.txt");
  53.     double a, b, c, d;
  54.     m.clear (); Eg.clear (); Ec.clear (); dSO.clear ();
  55.     while (infile >> a >> b >> c >> d)
  56.     {
  57.      m.push_back(a);
  58.      Ec.push_back(b);
  59.      Eg.push_back(c);
  60.      dSO.push_back(d/3+c);
  61.     }
  62.     infile.close();
  63. }
  64.  
  65. void calculate() {
  66.     double pi=3.141592653589793;
  67.     double nm=1e-9;
  68.     double h=6.626068e-34/(2*pi);
  69.     double e=1.60217646e-19;
  70.     double dz=0.0212306; // the step between points
  71.     double D_const = dz*dz*nm*nm/(h*h); // some const
  72.     int setWFs=50; // the number of required zero values
  73.     double x1, x2; // the values of "new_val" between different E
  74.     double dE=e*1e-5; // the step of E
  75.  
  76. const size_t num_poi=m.size (); // the number of z-points
  77. vector<double> m_E(num_poi); // the vector of m which dependent from E
  78. double E=3.74983e-19; // min(Ec); the minimum value of Ec
  79.  
  80. //calculation of vector m(E) at start point min(Ec)
  81. for (size_t i = 0; i < num_poi; ++i)  m_E[i]=m[i]*(1-(Ec[i]-E)/dSO[i]);
  82.  
  83. // initial values of new_val at start z-points
  84. double new_val=0, prev_val = 1.0, prev_2_val = 0.0;
  85.  
  86. // tha calculation of new_val at last z-point
  87. for (size_t i = 2; i < num_poi; ++i) {
  88.     new_val =
  89.          (2*D_const*(Ec[i]-E)+
  90.           2/(m_E[i]+m_E[i-1])+
  91.           2/(m_E[i-1]+m_E[i-2]))*(m_E[i]+m_E[i-1])*prev_val/2+
  92.             (m_E[i]+m_E[i-1])*(-prev_2_val)/((m_E[i-1]+m_E[i-2]));
  93.                     prev_2_val = prev_val;
  94.                     prev_val = new_val;
  95. }
  96.  
  97. // next step of E
  98. E+=dE;
  99.  
  100. // clear information about found zero-points
  101. En.clear ();
  102.  
  103. // similar calculation until En.size!=setWFs
  104. do {
  105. x1=new_val;
  106.  
  107.     for (size_t i = 0; i < num_poi; ++i)  m_E[i]=m[i]*(1-(Ec[i]-E)/dSO[i]);
  108.  
  109.   prev_val = 1.0, prev_2_val = 0.0;
  110.                   for (size_t i = 2; i !=num_poi; ++i) {
  111.   new_val =
  112.             (2*D_const*(Ec[i]-E)+
  113.              2/(m_E[i]+m_E[i-1])+
  114.              2/(m_E[i-1]+m_E[i-2]))*(m_E[i]+m_E[i-1])*prev_val/2+
  115.              (m_E[i]+m_E[i-1])*(-prev_2_val)/((m_E[i-1]+m_E[i-2]));
  116.                           prev_2_val = prev_val;
  117.                           prev_val = new_val;
  118.              }
  119.  
  120. x2=new_val;
  121. // if sign(x) isn't changed -> next E = E + dE;
  122.         if (sign(x1)==sign(x2)) E+=dE;
  123. // if sign(x) is chenged -> write value of E and -> next E = E +dE
  124.         else {
  125.  
  126.             En.push_back (E-dE-x1*dE/(x2-x1));
  127.             E+=dE;
  128.             cout << "Found\t" << En.size () << "\tof\t" << setWFs << "\n";
  129.            
  130.         }
  131.  
  132. } while (En.size()<setWFs);
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement