Quantcast
Channel: GUJ - Tópicos com a tag java
Viewing all articles
Browse latest Browse all 17410

Problema da distribuição de calor - Sistemas Paralelos e Distribuidos

$
0
0

@Serafin escreveu:

Pessoal, preciso fazer um trabalho sobre o Mapa de Distribuição de calor e estou tendo algumas dificuldades.
Segue abaixo o que precisa ser feito:

"Desenvolver um programa paralelo (usando threads ou subprocessos comunicantes) que receba como entrada uma grade
inicial com valores de temperatura e execute um número de passos da equação 1 e, ao fim, apresente a matriz de saída.
A entrada pode ser feita a partir de um arquivo no seguinte formato:
2 2
0.4 2.1
-0.1 2.0
Esta entrada indica que o arquivo contém uma grade 2 2 com os valores 0:4 e 2:1 na primeira linha e 􀀀0:1 e 2:0 na
segunda. A saída pode utilizar este mesmo formato, guardando em um arquivo o resultado.
A quantidade de passos a efetuar e a quantidade de tarefas a criar podem ser solicitadas durante a execução."

Abaixo segue o que eu consegui até o momento, porém travei, não sei mais o que fazer agora hehe. Alguém tem alguma sugestão ou exemplo?
E-mail: serafinpuc@gmail.com
==============================

LeitorArquivo.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LeitorArquivo {

private FileReader fr;
private BufferedReader br;

public LeitorArquivo() {
    try {
        String caminho = new File("").getAbsolutePath();
        caminho = caminho + File.separator + "src\\TestInput";

        this.fr = new FileReader(new File(caminho));
        this.br = new BufferedReader(this.fr);

    } catch (FileNotFoundException ex) {
        System.out.println("Arquivo não encontrado.");
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public FileReader abreArquivo() {
    try {
        String caminho = new File("").getAbsolutePath();
        caminho = caminho + File.separator + "src\\TestInput";

        FileReader fr = new FileReader(new File(caminho));
        return fr;

    } catch (FileNotFoundException ex) {
        System.out.println("Arquivo não encontrado.");
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

public String lePrimeiraLinha() throws IOException {
    String strTexto;

    strTexto = this.br.readLine();

    return strTexto;
}

public String leArquivo() throws IOException {
    String strTexto = "";

    BufferedReader br = new BufferedReader(fr);
    strTexto = br.readLine();

    return strTexto;
}

public double[][] montaMatriz(int nRow, int nCol) throws IOException {
    int cntLinha = 0;
    double[][] matrix = new double[nRow][nCol];

    for (String line; (line = this.br.readLine()) != null;) {
        String[] lMatrix = line.split(" ");

        for (int k = 0; k < lMatrix.length; k++) {
            String tmp = lMatrix[k];
            try {
                matrix[cntLinha][k] = Double.parseDouble(tmp);
            } catch (NumberFormatException ne) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ne);
            }
        }
        cntLinha++;
    }
    return matrix;
}

public void imprimeMatriz(double[][] matrix, int nRow, int nCol) {
    System.out.println("\tCol 0\tCol 1\tCol 2\tCol 3\tCol 4\tCol 5\tCol 6\tCol 7\tCol 8\tCol 9\tCol 10");
    for (int i = 0; i < nRow; i++) {
        System.out.print("Lin "+ i + ":\t");
        for (int j = 0; j < nCol; j++) {
            System.out.print(matrix[i][j] + "\t");
        }
        System.out.println("");
    }
}

}

ThermalProcessor.java

import java.text.DecimalFormat;

public class ThermalProcessor {

public void calculaTemperatura(double[][] matrix) {
    int xi, yi;
    int TAMX = 11, TAMY = 11, STEPS = 1000;

    double[][] novaMatriz = new double[TAMX][TAMY];

    //Cx e Cy são fatores que indicam quão rápido o calor dissipa entre as posições da grade.
    double cx = 0.1, cy = 0.1;
    String tmpValorPos;
    double vizinhoEsq, vizinhoDir, vizinhoCima, vizinhoBaixo, centro;

    DecimalFormat formato = new DecimalFormat("#.###");
    // inicializa U0
    for (int ts = 0; ts < STEPS; ts++) {
        for (yi = 1; yi < TAMY - 1; yi++) {
            for (xi = 1; xi < TAMX - 1; xi++) {
                //Captura e guarda os valores dos vizinhos    
                vizinhoEsq = matrix[xi - 1][yi];
                vizinhoDir = matrix[xi + 1][yi];
                vizinhoCima = matrix[xi][yi + 1];
                vizinhoBaixo = matrix[xi][yi - 1];
                centro = matrix[xi][yi];

                //novaMatriz[xi][yi] = Double.parseDouble(formato.format(centro
                tmpValorPos = formato.format(centro
                        + cx * (vizinhoEsq + vizinhoDir - (2.0 * centro))
                        + cy * (vizinhoBaixo + vizinhoCima - (2.0 * centro))
                );

                tmpValorPos = tmpValorPos.replace(",", ".");
                novaMatriz[xi][yi] = Double.parseDouble(tmpValorPos);
            }
        }

        matrix = novaMatriz;
        System.out.println("Calor dissipado após passo: " + (ts + 1));
        imprimeMatriz(novaMatriz, TAMX, TAMY);
    }
}

public void imprimeMatriz(double[][] matrix, int nRow, int nCol) {
    System.out.println("\tCol 0\tCol 1\tCol 2\tCol 3\tCol 4\tCol 5\tCol 6\tCol 7\tCol 8\tCol 9\tCol 10");
    for (int i = 0; i < nRow; i++) {
        System.out.print("Lin "+ i + ":\t");
        for (int j = 0; j < nCol; j++) {
            System.out.print(matrix[i][j] + "\t");
        }
        System.out.println("");
    }
}

}

Main.java

import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {
    LeitorArquivo leitor = new LeitorArquivo();

    String[] vDef = leitor.lePrimeiraLinha().split(" ");

    int nRow = Integer.parseInt(vDef[0]);
    int nCol = Integer.parseInt(vDef[1]);

    double[][] matrix = new double[nRow][nCol];

    matrix = leitor.montaMatriz(nRow, nCol);

    leitor.imprimeMatriz(matrix, nRow, nCol);

    ThermalProcessor tp = new ThermalProcessor();
    tp.calculaTemperatura(matrix);
}

}

Aqui, o arquivo com os numeros testados até o momento.

11 11
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 40 1 1 1 1 1
1 1 1 1 30 50 10 1 1 1 1
1 1 1 1 1 20 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1

Mensagens: 1

Participantes: 1

Ler tópico completo


Viewing all articles
Browse latest Browse all 17410

Latest Images

Trending Articles

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>