Advertisement
osipyonok

Ira 4m lab3 cpp

Apr 24th, 2017
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #include "heat_equation.h"
  2. #include "ui_heat_equation.h"
  3. #include <QImage>
  4. #include <vector>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. #define MAXN 555
  10.  
  11. #define remax(a,b) a=max(a,b)
  12. #define remin(a,b) a=min(a,b)
  13.  
  14. int N; //Количество узлов
  15.  
  16. vector<double> T(MAXN);//Температура в момент времени t в узле i
  17. vector<double> alpha(MAXN);
  18. vector<double> beta(MAXN);
  19.  
  20. vector<pair<double , vector<double>>> res; //<время , вектор температур>
  21.  
  22. double ai , bi , ci , fi;
  23. int last = 0; //Индекс времени, которое нужно нарисовать
  24. double lambda; //Коэфициент теплопроводности материала
  25. double ro; //Плотность материала
  26. double c; //Теплоемкость материала
  27. double h , tau; //h - шаг сетки (по пространственной координате), tau - шаг сетки (по времени)
  28. double t , t_end , L , T0 , T1 , Tl;
  29. //t - текущее время
  30. //t_end - конечное время
  31. //L - толщина пластины
  32. //T0 - начальная температур пластины
  33. //T1 - температура на границе (при х = 0)
  34. //Tl - температура на границе (при х = L)
  35.  
  36. pair<double , double> ext = {DBL_MAX , DBL_MIN};//Минимальная и максимальная температуры
  37.  
  38. Heat_equation::Heat_equation(QWidget *parent) : QMainWindow(parent), ui(new Ui::Heat_equation){
  39.     ui->setupUi(this);  
  40.     ui->doubleSpinBox->setMaximum(DBL_MAX);
  41.     ui->doubleSpinBox_2->setMaximum(DBL_MAX);
  42.     ui->doubleSpinBox_3->setMaximum(DBL_MAX);
  43.  
  44.     N = 500;
  45.     lambda = 46;
  46.     ro = 7800;
  47.     c = 460;
  48.     T0 = 20;
  49.     Tl = 300;
  50.     T1 = 100;
  51.     t_end = 60;
  52.     L = 0.1;
  53.  
  54.     ui->doubleSpinBox->setValue(lambda);
  55.     ui->doubleSpinBox_2->setValue(ro);
  56.     ui->doubleSpinBox_3->setValue(c);
  57.     ui->doubleSpinBox_4->setValue(L);
  58.     ui->doubleSpinBox_5->setValue(T1);
  59.     ui->doubleSpinBox_6->setValue(Tl);
  60.     ui->doubleSpinBox_7->setValue(t_end);
  61.     ui->doubleSpinBox_8->setValue(T0);
  62.  
  63.     tau = t_end / 500.0;
  64.     h = L / (N - 1.0);
  65.  
  66.     remin(ext.first , T0);
  67.     remax(ext.second , T0);
  68.  
  69.     remin(ext.first , Tl);
  70.     remax(ext.second , Tl);
  71.  
  72.     for(double & tmp : T){
  73.         tmp = T0;
  74.     }
  75.  
  76.     t = 0;
  77.  
  78.     while(t < t_end){
  79.         next_step();
  80.     }
  81.  
  82.     repaint();
  83. }
  84.  
  85. void Heat_equation::next_step(){
  86.     t += tau;
  87.  
  88.     alpha[1] = 0;
  89.     beta[1] = T1;
  90.  
  91.     for(int i = 2 ; i < N ; ++i){
  92.  
  93.         ai = lambda / (h * h);
  94.         bi = (2.0 * lambda / (h * h)) + (ro * c / tau);
  95.         ci = ai;
  96.         fi = -ro * c * T[i] / tau;
  97.      //      cout << ai << " " << bi << " " << ci << " " << fi << endl;
  98.         alpha[i] = ai / (bi - ci * alpha[i - 1]);
  99.         beta[i] = (ci * beta[i - 1] - fi) / (bi - ci * alpha[i - 1]);
  100.     }
  101.  
  102.     T[N] = Tl;
  103.  
  104.     for(int i = N - 1 ; i >= 1 ; --i){
  105.         T[i] = alpha[i] * T[i + 1] + beta[i];
  106.      //   cout << alpha[i] << " " << beta[i] << endl;
  107.         remin(ext.first , T[i]);
  108.         remax(ext.second , T[i]);
  109.     }
  110.     pair<double,vector<double>> p = {t , T};
  111.  
  112.     res.push_back(p);
  113. }
  114.  
  115.  
  116. void Heat_equation::paintEvent(QPaintEvent *event){
  117.     Q_UNUSED(event);
  118.     QPainter painter(this);
  119.  
  120.     QImage img(QSize(N , res.size()) , QImage::Format_ARGB32);
  121.     QImage grad("D://Qt_projects//Ira_4m_3//grad.jpg");
  122.  
  123.     double d = ext.second - ext.first; //Разность максимальной и минимальной температуры
  124.  
  125.     for(last = 0 ; last < res.size() ; ++last){
  126.         for(int i = 1 ; i <= N ; ++i){
  127.             double cur = res[last].second[i];
  128.  
  129.             double pr = cur - ext.first;
  130.  
  131.             int ind = pr / (d / 100.0);
  132.             remax(ind , 1);
  133.             remin(ind , 99);
  134.  
  135.             ind = ((double)grad.width() / 100.0) * ind;
  136.  
  137.             QColor cur_col = grad.pixelColor(ind , 1);
  138.  
  139.             img.setPixelColor(i - 1 , res.size() - last - 1, cur_col);
  140.         }
  141.         for(double i = 1 ; i <= 10 ; ++i){
  142.             int ind = (N - 1) * (i / 10);
  143.             cout << res[last].second[ind] << " ";
  144.         }
  145.         cout << endl;
  146.     }
  147.  
  148.  
  149.     painter.drawImage(50 , 50 , img);
  150.  
  151. }
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. Heat_equation::~Heat_equation()
  162. {
  163.     delete ui;
  164. }
  165.  
  166. void Heat_equation::on_pushButton_clicked()
  167. {
  168.     lambda = ui->doubleSpinBox->value();
  169.     ro = ui->doubleSpinBox_2->value();
  170.     c = ui->doubleSpinBox_3->value();
  171.     L = ui->doubleSpinBox_4->value();
  172.     T1 = ui->doubleSpinBox_5->value();
  173.     Tl = ui->doubleSpinBox_6->value();
  174.     t_end = ui->doubleSpinBox_7->value();
  175.     T0 = ui->doubleSpinBox_8->value();
  176.  
  177.     tau = t_end / 500.0;
  178.     h = L / (N - 1.0);
  179.     ext = {DBL_MAX , DBL_MIN};
  180.  
  181.     remin(ext.first , T0);
  182.     remax(ext.second , T0);
  183.  
  184.     remin(ext.first , Tl);
  185.     remax(ext.second , Tl);
  186.  
  187.     for(double & tmp : T){
  188.         tmp = T0;
  189.     }
  190.  
  191.     t = 0;
  192.  
  193.     res.clear();
  194.  
  195.     while(t < t_end){
  196.         next_step();
  197.     }
  198.  
  199.     repaint();
  200.  
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement