Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <iostream>
- #include <vector>
- #include <QPicture>
- #include <QImage>
- #include <QTimer>
- using namespace std;
- const int PIX_SIZE = 10;
- const int MAXN = 111;
- vector<vector<vector<double>>> res;
- int cur = 0;
- int nx, ny;
- double ai, bi, ci, fi;
- double a, lambda, ro, c;
- double hx, hy, tau, t_end, time;
- double t0, l, h, th, tc;
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- vector<vector<double>> T(MAXN , vector<double>(MAXN));
- vector<double> alfa(MAXN), beta(MAXN);
- nx = ny = 30;
- t_end = 60;
- l = h = 2.5;
- lambda = 2.3e-5;
- ro = 7.85;
- c = 0.00045;
- th = 30;
- tc = 80;
- t0 = 5;
- hx = l / (nx - 1);
- hy = h / (ny - 1);
- a = lambda / (ro * c);
- tau = t_end / 100.0;
- for (int i = 1; i <= nx; ++i)
- for (int j = 1; j <= ny; ++j)
- T[i][j] = t0;
- res.push_back(T);
- time = 0.0;
- while(time < t_end){
- time += tau;
- for (int j = 1; j <= ny; ++j) {
- alfa[1] = 0.0;
- beta[1] = th;
- for (int i = 2; i <= nx - 1; ++i) {
- ai = lambda / (hx * hx);
- bi = 2.0 * lambda / (hx * hx) + ro * c / tau;
- ci = lambda / (hx * hx);
- fi = -ro * c * T[i][j] / tau;
- alfa[i] = ai / (bi - ci * alfa[i - 1]);
- beta[i] = (ci * beta[i - 1] - fi) / (bi - ci * alfa[i - 1]);
- }
- T[nx][j] = tc;
- for (int i = nx - 1; i >= 1; --i) {
- T[i][j] = alfa[i] * T[i + 1][j] + beta[i];
- }
- }
- for (int i = 2; i <= nx - 1; ++i) {
- alfa[1] = 2.0 * a * tau / (2.0 * a * tau + hy * hy);
- beta[1] = hy * hy * T[i][1] / (2.0 * a * tau + hy * hy);
- for (int j = 2; j <= ny - 1; ++j) {
- ai = lambda / (hy * hy);
- bi = 2.0 * lambda / (hy * hy) + ro * c / tau;
- ci = lambda / (hy * hy);
- fi = -ro * c * T[i][j] / tau;
- alfa[j] = ai / (bi - ci * alfa[j - 1]);
- beta[j] = (ci * beta[j - 1] - fi) / (bi - ci * alfa[j - 1]);
- }
- T[i][ny] = (2.0 * a * tau * beta[ny - 1] + (hy * hy) * T[i][ny]) / (2.0 * a * tau * (1.0 - alfa[ny - 1]) + (hy * hy));
- for (int j = ny - 1; j >= 1; --j) {
- T[i][j] = alfa[j] * T[i][j + 1] + beta[j];
- }
- }
- res.push_back(T);
- }
- ui->tableWidget->setRowCount(10);
- ui->tableWidget->setColumnCount(10);
- for(int i = 0 ; i < ui->tableWidget->rowCount() ; ++i)
- ui->tableWidget->setRowHeight(i , 20);
- for(int i = 0 ; i < ui->tableWidget->columnCount() ; ++i)
- ui->tableWidget->setColumnWidth(i , 60);
- ui->tableWidget->resize(QSize(60 * 10.5 - 5 , 20 * 11 + 5));
- for(int i = 0 ; i < ui->tableWidget->rowCount() ; ++i)
- for(int j = 0 ; j < ui->tableWidget->columnCount() ; ++j){
- QTableWidgetItem * item = new QTableWidgetItem(QString::number(res[0][(i + 1) * nx / 10][(j + 1) * ny / 10]));
- ui->tableWidget->setItem(i , j , item);
- }
- }
- void MainWindow::update(){
- if(cur > t_end){
- trm->stop();
- return;
- }
- repaint();
- QString str = "Time: "; str += QString::number(cur); str += " s.";
- ui->label->setText(str);
- ++cur;
- cur = min(cur , (int)res.size() - 1);
- }
- void MainWindow::paintEvent(QPaintEvent *event){
- Q_UNUSED(event);
- QPainter painter(this);
- painter.setPen(QPen(Qt::black , 1 , Qt::SolidLine , Qt::FlatCap));
- painter.setBrush(QBrush(Qt::red , Qt::SolidPattern));
- QImage img(QSize(nx * PIX_SIZE , ny * PIX_SIZE) , QImage::Format_ARGB32);
- QImage grad("D:\\Qt_projects\\4m_lab3\\grad.jpg");
- for(int i = 1 ; i <= nx ; ++i){
- for(int j = 1 ; j <= ny ; ++j){
- int el = res[cur][i][j];
- QColor col;
- int mx = max(th , max(tc , t0));
- int mn = min(th , min(tc , t0));
- int mc = mx - mn;
- int cl = el - mn;
- int ind = (double)cl / ((double)mc / 100.0);
- if(ind <= 0)ind = 3;
- if(ind >= 100)ind = 98;
- ind = ((double)grad.width() / 100.0) * (double)ind;
- col = grad.pixelColor(ind , 20);
- for(int x = PIX_SIZE * (i - 1) ; x < PIX_SIZE * i ; ++x){
- for(int y = PIX_SIZE * (j - 1) ; y < PIX_SIZE * j ; ++y){
- img.setPixelColor(x , y , col);
- }
- }
- }
- }
- painter.drawImage(50,50,img);
- for(int i = 0 ; i < ui->tableWidget->rowCount() ; ++i)
- for(int j = 0 ; j < ui->tableWidget->columnCount() ; ++j){
- QTableWidgetItem * item = new QTableWidgetItem(QString::number(res[cur][(i + 1) * nx / 10.3][(j + 1) * ny / 10]));
- ui->tableWidget->setItem(j , i , item);
- }
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- if(trm != NULL){
- delete trm;
- }
- }
- void MainWindow::on_pushButton_2_clicked()
- {
- cur = 0;
- trm = new QTimer(this);
- connect(trm , SIGNAL(timeout()) , this , SLOT(update()));
- trm->start(250);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement