Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "heat_equation.h"
- #include "ui_heat_equation.h"
- #include <QImage>
- #include <vector>
- #include <iostream>
- using namespace std;
- #define MAXN 555
- #define remax(a,b) a=max(a,b)
- #define remin(a,b) a=min(a,b)
- int N; //Количество узлов
- vector<double> T(MAXN);//Температура в момент времени t в узле i
- vector<double> alpha(MAXN);
- vector<double> beta(MAXN);
- vector<pair<double , vector<double>>> res; //<время , вектор температур>
- double ai , bi , ci , fi;
- int last = 0; //Индекс времени, которое нужно нарисовать
- double lambda; //Коэфициент теплопроводности материала
- double ro; //Плотность материала
- double c; //Теплоемкость материала
- double h , tau; //h - шаг сетки (по пространственной координате), tau - шаг сетки (по времени)
- double t , t_end , L , T0 , T1 , Tl;
- //t - текущее время
- //t_end - конечное время
- //L - толщина пластины
- //T0 - начальная температур пластины
- //T1 - температура на границе (при х = 0)
- //Tl - температура на границе (при х = L)
- pair<double , double> ext = {DBL_MAX , DBL_MIN};//Минимальная и максимальная температуры
- Heat_equation::Heat_equation(QWidget *parent) : QMainWindow(parent), ui(new Ui::Heat_equation){
- ui->setupUi(this);
- ui->doubleSpinBox->setMaximum(DBL_MAX);
- ui->doubleSpinBox_2->setMaximum(DBL_MAX);
- ui->doubleSpinBox_3->setMaximum(DBL_MAX);
- N = 500;
- lambda = 46;
- ro = 7800;
- c = 460;
- T0 = 20;
- Tl = 300;
- T1 = 100;
- t_end = 60;
- L = 0.1;
- ui->doubleSpinBox->setValue(lambda);
- ui->doubleSpinBox_2->setValue(ro);
- ui->doubleSpinBox_3->setValue(c);
- ui->doubleSpinBox_4->setValue(L);
- ui->doubleSpinBox_5->setValue(T1);
- ui->doubleSpinBox_6->setValue(Tl);
- ui->doubleSpinBox_7->setValue(t_end);
- ui->doubleSpinBox_8->setValue(T0);
- tau = t_end / 500.0;
- h = L / (N - 1.0);
- remin(ext.first , T0);
- remax(ext.second , T0);
- remin(ext.first , Tl);
- remax(ext.second , Tl);
- for(double & tmp : T){
- tmp = T0;
- }
- t = 0;
- while(t < t_end){
- next_step();
- }
- repaint();
- }
- void Heat_equation::next_step(){
- t += tau;
- alpha[1] = 0;
- beta[1] = T1;
- for(int i = 2 ; i < N ; ++i){
- ai = lambda / (h * h);
- bi = (2.0 * lambda / (h * h)) + (ro * c / tau);
- ci = ai;
- fi = -ro * c * T[i] / tau;
- // cout << ai << " " << bi << " " << ci << " " << fi << endl;
- alpha[i] = ai / (bi - ci * alpha[i - 1]);
- beta[i] = (ci * beta[i - 1] - fi) / (bi - ci * alpha[i - 1]);
- }
- T[N] = Tl;
- for(int i = N - 1 ; i >= 1 ; --i){
- T[i] = alpha[i] * T[i + 1] + beta[i];
- // cout << alpha[i] << " " << beta[i] << endl;
- remin(ext.first , T[i]);
- remax(ext.second , T[i]);
- }
- pair<double,vector<double>> p = {t , T};
- res.push_back(p);
- }
- void Heat_equation::paintEvent(QPaintEvent *event){
- Q_UNUSED(event);
- QPainter painter(this);
- QImage img(QSize(N , res.size()) , QImage::Format_ARGB32);
- QImage grad("D://Qt_projects//Ira_4m_3//grad.jpg");
- double d = ext.second - ext.first; //Разность максимальной и минимальной температуры
- for(last = 0 ; last < res.size() ; ++last){
- for(int i = 1 ; i <= N ; ++i){
- double cur = res[last].second[i];
- double pr = cur - ext.first;
- int ind = pr / (d / 100.0);
- remax(ind , 1);
- remin(ind , 99);
- ind = ((double)grad.width() / 100.0) * ind;
- QColor cur_col = grad.pixelColor(ind , 1);
- img.setPixelColor(i - 1 , res.size() - last - 1, cur_col);
- }
- for(double i = 1 ; i <= 10 ; ++i){
- int ind = (N - 1) * (i / 10);
- cout << res[last].second[ind] << " ";
- }
- cout << endl;
- }
- painter.drawImage(50 , 50 , img);
- }
- Heat_equation::~Heat_equation()
- {
- delete ui;
- }
- void Heat_equation::on_pushButton_clicked()
- {
- lambda = ui->doubleSpinBox->value();
- ro = ui->doubleSpinBox_2->value();
- c = ui->doubleSpinBox_3->value();
- L = ui->doubleSpinBox_4->value();
- T1 = ui->doubleSpinBox_5->value();
- Tl = ui->doubleSpinBox_6->value();
- t_end = ui->doubleSpinBox_7->value();
- T0 = ui->doubleSpinBox_8->value();
- tau = t_end / 500.0;
- h = L / (N - 1.0);
- ext = {DBL_MAX , DBL_MIN};
- remin(ext.first , T0);
- remax(ext.second , T0);
- remin(ext.first , Tl);
- remax(ext.second , Tl);
- for(double & tmp : T){
- tmp = T0;
- }
- t = 0;
- res.clear();
- while(t < t_end){
- next_step();
- }
- repaint();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement