Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dane do txt
- 12 100 1200 25 300 100
- 0 5 6 1 0 0 1 0 1 1 0 1 0 0
- 1 6 7 2 0 1 1 1 1 2 0 2 0 0
- 2 7 8 3 0 2 1 2 1 3 0 3 0 0
- 3 8 9 4 0 3 1 3 1 4 0 4 1 2
- 5 10 11 6 1 0 2 0 2 1 1 1 0 0
- 6 11 12 7 1 1 2 1 2 2 1 2 0 0
- 7 12 13 8 1 2 2 2 2 3 1 3 0 0
- 8 13 14 9 1 3 2 3 2 4 1 4 1 2
- 10 15 16 11 2 0 3 0 3 1 2 1 0 0
- 11 16 17 12 2 1 3 1 3 2 2 2 0 0
- 15 18 19 16 3 0 4 0 4 1 3 1 2 1
- 16 19 20 17 3 1 4 1 4 2 3 2 2 1
- #include <iostream>
- #include <iomanip>
- #include <math.h>
- #include <fstream>
- using namespace std;
- const double eps = 1e-12; // stala przyblizenia zera potrzebna do obliczenia ukladu rownan metoda Gaussa
- int licznik = 0;
- float t0, tinf, k, alfa, q;
- float ksi[4][2] = {
- { -0.5773502692, -0.5773502692 },
- { 0.5773502692, -0.5773502692 },
- { 0.5773502692, 0.5773502692 },
- { -0.5773502692, 0.5773502692 }
- };
- float N(int no, float ksi, float ni){
- switch (no)
- {
- case 1:
- return (1 - ksi)*(1 - ni) / 4;
- case 2:
- return (1 + ksi)*(1 - ni) / 4;
- case 3:
- return (1 + ksi)*(1 + ni) / 4;
- case 4:
- return (1 - ksi)*(1 + ni) / 4;
- default:
- break;
- }
- }
- float NKsi(int no, float ni){
- switch (no)
- {
- case 1:
- return (-1)*(1 - ni) / 4;
- case 2:
- return (1 - ni) / 4;
- case 3:
- return (1 + ni) / 4;
- case 4:
- return (-1)*(1 + ni) / 4;
- default:
- break;
- }
- }
- float Nni(int no, float ksi){
- switch (no)
- {
- case 1:
- return (1 - ksi)*(-1) / 4;
- case 2:
- return (1 + ksi)*(-1) / 4;
- case 3:
- return (1 + ksi) / 4;
- case 4:
- return (1 - ksi) / 4;
- default:
- break;
- }
- }
- float Rp(float ksi, float ni, float x1, float x2, float x3, float x4){ //Funkcja przejscia elementu lokalnego do globalnego
- return N(1, ksi, ni)*x1 + N(2, ksi, ni)*x2 + N(3, ksi, ni)*x3 + N(4, ksi, ni)*x4;
- }
- float J00(float x1, float x2, float x3, float x4, float ni){
- return (NKsi(1, ni)*x1 + NKsi(2, ni)*x2 + NKsi(3, ni)*x3 + NKsi(4, ni)*x4)*1.0f;
- }
- float J01(float y1, float y2, float y3, float y4, float ni){
- return (NKsi(1, ni)*y1 + NKsi(2, ni)*y2 + NKsi(3, ni)*y3 + NKsi(4, ni)*y4)*1.0f;
- }
- float J10(int x1, int x2, int x3, int x4, float ksi){
- return (Nni(1, ksi)*x1 + Nni(2, ksi)*x2 + Nni(3, ksi)*x3 + Nni(4, ksi)*x4)*1.0f;
- }
- float J11(float y1, float y2, float y3, float y4, float ksi){
- return (Nni(1, ksi)*y1 + Nni(2, ksi)*y2 + Nni(3, ksi)*y3 + Nni(4, ksi)*y4)*1.0f;
- }
- float detJfun(float x1, float x2, float x3, float x4){
- return (x1*x4) - (x2*x3);
- }
- float detJminusFun(float x1, float x2, float x3, float x4, float det){
- return (x1*x4) - (x2*x3);
- }
- class ElementSkonczony
- {
- public:
- float x1, x2, x3, x4;
- float y1, y2, y3, y4; //położenie węzłów elementu skonczonego w ukladzie globalnym
- float t01, t02, t03, t04; //temperatura poczatkowa w poszczególnych węzłach C
- float K; //Wspolczynnik przewodzenia ciapla W/m*C
- int NOP[4]; //NOP1, NOP2, NOP3, NOP4- wspolczynniki w odniesieniu do macierzy globalnej
- float K_l[4][4]; //K_l- lokalna macierz wspolczynnikow ukladu rownan
- float F_l[4]; //F_l- lokalny wektor prawej czesci ukladu
- float J[4][4];
- float detJ[4];
- float Jminus[4][4];
- float detJminus[4];
- float pochodnaLokalna[4][2];
- int war = 0;
- int warPow = 0;
- //float JElem[2][2];
- ElementSkonczony(int NOP1n, int NOP2n, int NOP3n, int NOP4n, float x1n, float y1n, float x2n, float y2n, float x3n, float y3n, float x4n, float y4n, float t0, int warn, int warPown){ //Konstruktor elementu skonczonego
- x1 = x1n;
- x2 = x2n;
- x3 = x3n;
- x4 = x4n;
- y1 = y1n;
- y2 = y2n;
- y3 = y3n;
- y4 = y4n;
- NOP[0] = NOP1n;
- NOP[1] = NOP2n;
- NOP[2] = NOP3n;
- NOP[3] = NOP4n;
- this->war = warn;
- this->warPow = warPown;
- t01 = t02 = t03 = t04 = t0;
- for (int i = 0; i<4; ++i)
- for (int j = 0; j<4; ++j){
- K_l[i][j] = 0;
- }
- }
- void jakobiMatrix(){
- J[0][0] = J00(x1, x2, x3, x4, -0.577);
- J[0][1] = J01(y1, y2, y3, y4, -0.577);
- J[0][2] = J10(x1, x2, x3, x4, -0.577);
- J[0][3] = J11(y1, y2, y3, y4, -0.577);
- this->detJ[0] = detJfun(J[0][0], J[0][1], J[0][2], J[0][3]);
- Jminus[0][0] = J[0][3] / detJ[0];
- Jminus[0][1] = -J[0][1] / detJ[0];
- Jminus[0][2] = -J[0][2] / detJ[0];
- Jminus[0][3] = J[0][0] / detJ[0];
- J[1][0] = J00(x1, x2, x3, x4, -0.577);
- J[1][1] = J01(y1, y2, y3, y4, 0.577);
- J[1][2] = J10(x1, x2, x3, x4, -0.577);
- J[1][3] = J11(y1, y2, y3, y4, 0.577);
- this->detJ[1] = detJfun(J[1][0], J[1][1], J[1][2], J[1][3]);
- Jminus[1][0] = J[1][3] / detJ[1];
- Jminus[1][1] = -J[1][1] / detJ[1];
- Jminus[1][2] = -J[1][2] / detJ[1];
- Jminus[1][3] = J[1][0] / detJ[1];
- J[2][0] = J00(x1, x2, x3, x4, 0.577);
- J[2][1] = J01(y1, y2, y3, y4, 0.577);
- J[2][2] = J10(x1, x2, x3, x4, 0.577);
- J[2][3] = J11(y1, y2, y3, y4, 0.577);
- this->detJ[2] = detJfun(J[2][0], J[2][1], J[2][2], J[2][3]);
- Jminus[2][0] = J[2][3] / detJ[2];
- Jminus[2][1] = -J[2][1] / detJ[2];
- Jminus[2][2] = -J[2][2] / detJ[2];
- Jminus[2][3] = J[2][0] / detJ[2];
- J[3][0] = J00(x1, x2, x3, x4, 0.577);
- J[3][1] = J01(y1, y2, y3, y4, -0.577);
- J[3][2] = J10(x1, x2, x3, x4, 0.577);
- J[3][3] = J11(y1, y2, y3, y4, -0.577);
- this->detJ[3] = detJfun(J[3][0], J[3][1], J[3][2], J[3][3]);
- Jminus[3][0] = J[3][3] / detJ[3];
- Jminus[3][1] = -J[3][1] / detJ[3];
- Jminus[3][2] = -J[3][2] / detJ[3];
- Jminus[3][3] = J[3][0] / detJ[3];
- }
- void liczPochodne(){
- for (int j = 0; j<4; j++){
- for (int i = 0; i<4; i++){//uzupelnianie macierzy lokalnej
- for (int l = 0; l<4; l++){
- float dndxl = NKsi(i + 1, ksi[j][1]) * Jminus[0][j] + Nni(i + 1, ksi[j][0]) * Jminus[j][1];
- float dndyl = NKsi(i + 1, ksi[j][1]) * Jminus[2][j] + Nni(i + 1, ksi[j][0]) * Jminus[j][3];
- float dndxk = NKsi(i + 1, ksi[j][1]) * Jminus[0][j] + Nni(i + 1, ksi[j][0]) * Jminus[j][1];
- float dndyk = NKsi(i + 1, ksi[j][1]) * Jminus[2][j] + Nni(i + 1, ksi[j][0]) * Jminus[j][3];
- K_l[i][l] += k*detJ[i] * (dndxl*dndxk + dndyl*dndyk);
- }
- }
- }
- if (war == 1){ // dodajemy strumien ciepla q
- K_l[warPow][warPow] += q / 2;
- K_l[warPow + 1][warPow + 1] += q / 2;
- cout << q << " ";
- }
- else if (war == 2){ //dodajemy wymniane ciepla alfa
- K_l[warPow][warPow] += 2.0 / 6.0*alfa;
- K_l[warPow + 1][warPow + 1] += 2.0 / 6.0*alfa;
- K_l[warPow + 1][warPow] += 1.0 / 6.0*alfa;
- K_l[warPow][warPow + 1] += 1.0 / 6.0*alfa;
- F_l[warPow] = 1 / 2 * alfa*tinf;
- F_l[warPow + 1] = 1 / 2 * alfa*tinf;
- cout << " " << alfa;
- }
- }
- void macierzLokalnaWyswietl(){
- cout << endl;
- for (int i = 0; i<4; ++i){
- for (int j = 0; j<4; ++j){
- cout << J[i][j] << " ";
- if (j % 2 == 1) cout << endl;
- }
- cout << endl;
- }
- cout << x1 << " " << x2 << " " << x3 << " " << x4 << endl;
- cout << y1 << " " << y2 << " " << y3 << " " << y4 << endl;
- for (int i = 0; i<4; ++i){
- for (int j = 0; j<4; ++j){
- cout << K_l[i][j] << " ";
- }
- cout << endl;
- }
- }
- };
- bool gauss(int n, float ** AB, float * T)
- {
- int i, j, k;
- float m, s;
- //Eliminacja wspolczynnikow
- for (i = 0; i < n - 1; i++){
- for (j = i + 1; j < n; j++){
- if (fabs(AB[i][i]) < eps) return false;
- m = -AB[j][i] / AB[i][i]; // obliczamy mnoznik
- for (k = i + 1; k <= n; k++){
- AB[j][k] += m * AB[i][j];
- }
- }
- }
- //Wyliczanie niewiadomych
- for (i = n - 1; i >= 0; i--){
- s = AB[i][n];
- for (j = n - 1; j >= i + 1; j--){
- s -= AB[i][j] * T[j];
- }
- if (fabs(AB[i][i]) < eps) return false;
- T[i] = s / AB[i][i];
- }
- return true;
- }
- int main(){
- int NOP1n, NOP2n, NOP3n, NOP4n;
- int ne, nh;
- float x1n, x2n, x3n, x4n, y1n, y2n, y3n, y4n;
- int war, warPow;
- fstream plik;
- plik.open("dane.txt", ios::in); //Otwieranie pliku z danymi
- if (!plik.good())
- return false;
- plik >> ne >> t0 >> tinf >> k >> alfa >> q,
- nh = ne + 1;
- ElementSkonczony** tabElementowSkonczonych = new ElementSkonczony*[ne]; //Tworzymy tablice elementow skonczonych
- for (int i = 0; i<ne; i++){
- plik >> NOP1n >> NOP2n >> NOP3n >> NOP4n >> x1n >> y1n >> x2n >> y2n >> x3n >> y3n >> x4n >> y4n >> war >> warPow;
- tabElementowSkonczonych[i] = new ElementSkonczony(NOP1n, NOP2n, NOP3n, NOP4n, x1n, y1n, x2n, y2n, x3n, y3n, x4n, y4n, t0, war, warPow);
- }
- for (int i = 0; i<ne; i++){
- tabElementowSkonczonych[i]->jakobiMatrix();
- }
- for (int i = 0; i<ne; i++){
- tabElementowSkonczonych[i]->liczPochodne();
- }
- nh = 21;
- float **AB = new float*[nh]; //Macierz AB- pomocnicza macierz do obliczenia ukladu rownan metoda Gaussa
- float** KG = new float*[nh]; //Macierz KG- globalna macierz wspolczynnikow uklady rownan
- float* FG = new float[nh]; //F- wektor prawej czesci ukladu rownan
- float* T = new float[nh]; //T- wektor niewiadomych temperatur
- for (int i = 0; i<nh; i++)
- AB[i] = new float[nh + 1];
- for (int i = 0; i<nh; i++){
- T[i] = t0;
- KG[i] = new float[nh];
- }
- for (int i = 0; i<nh; i++)
- {
- for (int j = 0; j<nh; j++)
- KG[i][j] = 0;
- FG[i] = 0;
- T[i] = 0;
- }
- for (int i = 0; i<12; ++i){
- for (int k = 0; k<4; k++){ // uzupelnianie macierzy globalnej
- for (int l = 0; l<4; l++){
- KG[tabElementowSkonczonych[i]->NOP[k]][tabElementowSkonczonych[i]->NOP[l]] += tabElementowSkonczonych[i]->K_l[k][l];
- }
- FG[tabElementowSkonczonych[i]->NOP[k]] += tabElementowSkonczonych[i]->F_l[k];
- }
- }
- for (int i = 0; i < nh; i++){
- for (int j = 0; j < nh; j++)
- AB[i][j] = KG[i][j];
- }
- for (int i = 0; i < nh; i++){
- AB[i][nh] = FG[i];
- }
- if (gauss(nh, AB, T)){ //Wywolanie funkcji obliczajacej uklad rownan metoda Gaussa
- cout << endl << endl;
- cout << "Niewiadome temperatury w wezlach wynosza : " << endl << endl;
- for (int i = 0; i < nh; i++){
- cout << "t" << i + 1 << " = " << setw(9) << T[i] << endl;
- }
- }
- else{
- cout << "DZIELNIK ZERO\n";
- cout << endl;
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement