package edu.colorado.csdms.heat;

import java.util.List;

/**
 * A solver for the 2D heat equation on a uniform grid.
 */
public class Solve2D {

  /**
   * Solve the 2D heat equation on a uniform grid.
   * 
   * @param temperature a 2D array of temperatures
   * @param shape the dimensions of the grid, in (rows, cols)
   * @param spacing the distance between rows and columns, respectively
   * @param alpha parameter in heat equation
   * @param timeStep current time step
   * @return the temperatures after timeStep
   */
  public static double[][] solve(double[][] temperature, List<Integer> shape,
      List<Double> spacing, Double alpha, Double timeStep) {

    Integer topRow = shape.get(0) - 1;
    Integer topCol = shape.get(1) - 1;
    Double dx2 = Math.pow(spacing.get(1), 2.0);
    Double dy2 = Math.pow(spacing.get(0), 2.0);
    Double c = alpha * timeStep / (dx2 + dy2);
    double[][] newTemperature = new double[shape.get(0)][shape.get(1)];
    
    for (int i = 1; i < topRow; i++) {
      for (int j = 1; j < topCol; j++) {
        Double rowOp = dx2 * (temperature[i][j - 1] + temperature[i][j + 1]);
        Double colOp = dy2 * (temperature[i - 1][j] + temperature[i + 1][j]);
        newTemperature[i][j] =
            c * (rowOp + colOp - 2.0 * (dx2 + dy2) * temperature[i][j]);
      }
    }

    for (int i = 0; i < shape.get(0); i++) {
      newTemperature[i][0] = 0.0;
      newTemperature[i][topCol] = 0.0;
    }

    for (int j = 0; j < shape.get(1); j++) {
      newTemperature[0][j] = 0.0;
      newTemperature[topRow][j] = 0.0;
    }

    for (int i = 1; i < topRow; i++) {
      for (int j = 1; j < topCol; j++) {
        newTemperature[i][j] += temperature[i][j];
      }
    }
    
    return newTemperature;
  }
}

@@ -27,16 +27,69 @@
 .heat;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0A
 import j
@@ -733,16 +733,21 @@
 r topRow
+Index
  = shape
@@ -751,17 +751,17 @@
 ape.get(
-0
+1
 ) - 1;%0A 
@@ -777,16 +777,21 @@
 r topCol
+Index
  = shape
@@ -787,33 +787,33 @@
 dex = shape.get(
-1
+0
 ) - 1;%0A    Doubl
@@ -841,17 +841,17 @@
 ing.get(
-1
+0
 ), 2.0);
@@ -881,33 +881,33 @@
 pow(spacing.get(
-0
+1
 ), 2.0);%0A    Dou
@@ -999,17 +999,17 @@
 ape.get(
-0
+1
 )%5D%5Bshape
@@ -1013,17 +1013,17 @@
 ape.get(
-1
+0
 )%5D;%0A    
@@ -1045,32 +1045,37 @@
  = 1; i %3C topRow
+Index
 ; i++) %7B%0A      f
@@ -1091,32 +1091,37 @@
  = 1; j %3C topCol
+Index
 ; j++) %7B%0A       
@@ -1405,16 +1405,16 @@
 int 
-i
+j
  = 0; 
-i
+j
  %3C s
@@ -1426,17 +1426,17 @@
 get(0); 
-i
+j
 ++) %7B%0A  
@@ -1458,12 +1458,12 @@
 ure%5B
-i%5D%5B0
+0%5D%5Bj
 %5D = 
@@ -1492,17 +1492,22 @@
 ure%5B
-i%5D%5BtopCol
+topRowIndex%5D%5Bj
 %5D = 
@@ -1527,32 +1527,32 @@
    for (int 
-j
+i
  = 0; 
-j
+i
  %3C shape.get
@@ -1556,17 +1556,17 @@
 get(1); 
-j
+i
 ++) %7B%0A  
@@ -1576,36 +1576,36 @@
  newTemperature%5B
-0%5D%5Bj
+i%5D%5B0
 %5D = 0.0;%0A      n
@@ -1622,17 +1622,22 @@
 ure%5B
-topRow%5D%5Bj
+i%5D%5BtopColIndex
 %5D = 
@@ -1678,16 +1678,21 @@
 %3C topRow
+Index
 ; i++) %7B
@@ -1724,16 +1724,21 @@
 %3C topCol
+Index
 ; j++) %7B

Fix error with indexing temperature array