uchicago.src.sim.space
Class Diffuse2D

java.lang.Object
  extended by uchicago.src.sim.space.Diffuse2D
All Implemented Interfaces:
Discrete2DSpace, Torus
Direct Known Subclasses:
Diffuse2DHexagonal, HeatSpace

public class Diffuse2D
extends java.lang.Object
implements Discrete2DSpace, Torus

Discrete 2nd order approximation of 2d diffusion with evaporation. Essentialy a java implementation of Diffuse2d in the Swarm simulation toolkit. Toroidal in shape and works with number values. This space simulates concurency through the use of a read and write matrix. Any writes to the space, write to the write matrix, and any reads to the read matrix. The diffuse() method then diffuses the write matrix and copies the new values into the read matrix.

For an example of a Diffuse2d space see the heatBugs example. See diffuse for a brief explanation of the diffusion algorithm.

Version:
$Revision: 1.5 $ $Date: 2004/11/03 19:50:57 $
Author:
Nick Collier

Field Summary
protected  double diffCon
           
protected  double evapRate
           
static long MAX
           
static long MIN
           
protected  int nextX
           
protected  int nextY
           
protected  int prevX
           
protected  int prevY
           
protected  DoubleMatrix readMatrix
           
protected  DoubleMatrix writeMatrix
           
protected  int x
           
protected  int xSize
           
protected  int y
           
protected  int ySize
           
 
Fields inherited from interface uchicago.src.sim.space.Discrete2DSpace
MOORE, VON_NEUMANN
 
Constructor Summary
Diffuse2D(double diffusionConstant, double evaporationRate, int xSize, int ySize)
          Constructs a Diffuse2d space with the specified diffusion constant, evaporation rate, and dimensions
Diffuse2D(int xSize, int ySize)
          Constructs a Diffuse2d space with the specificed dimensions
 
Method Summary
protected  double[] compareMax(double[] array)
           
protected  double[] compareMin(double[] array)
           
 void diffuse()
          Runs the diffusion with the current rates and values.
 double[] findMaximum(int x, int y, int range, boolean includeOrigin, int neighborhoodType)
          Finds the maximum grid cell value within a specified range from the specified origin coordinate.
 double[] findMinimum(int x, int y, int range, boolean includeOrigin, int neighborhoodType)
          Finds the minimum grid cell value within a specified range from the specified origin coordinate.
 BaseMatrix getMatrix()
          Gets the matrix collection class that contains all the values.
 double[] getMooreNeighbors(int x, int y)
          Gets the Moore neighbors of the specified coordinate. doubles are returned by row starting with the "NW corner" and ending with the "SE corner."
 double[] getMooreNeighbors(int x, int y, int xExtent, int yExtent)
          Gets the extended Moore neighbors of the specified coordinate.
 java.lang.Object getObjectAt(int x, int y)
          Gets the object (a Long) at the specified coordinate.
 java.awt.Dimension getSize()
          Gets the dimension of the space
 int getSizeX()
          Gets the size of the x dimension
 int getSizeY()
          Gets the size of the y dimension
 double getValueAt(int x, int y)
          Gets the value at the specified coordinate.
 double[] getVonNeumannNeighbors(int x, int y)
          Gets the von Neumann neighbors of the specified coordinate.
 double[] getVonNeumannNeighbors(int x, int y, int xExtent, int yExtent)
          Gets the extended von Neumann neighbors of the specified coordinate.
 void putObjectAt(int x, int y, java.lang.Object object)
          Puts the specified Object at the specified coordinate.
 void putValueAt(int x, int y, double value)
          Puts the specified value at the specified coordinate.
 void setDiffusionConstant(double diffusionConstant)
          Sets the diffusion constant for this Diffuse2d space
 void setEvaporationRate(double rate)
          Sets the evaporation rate for this Diffuse2d space
 void update()
          Copies the writeLattice to the readLattice
protected  int xnorm(int x)
          Normalize the x value to the toroidal coordinates
protected  int ynorm(int y)
          Normalize the y value to the toroidal coordinates
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX

public static final long MAX
See Also:
Constant Field Values

MIN

public static final long MIN
See Also:
Constant Field Values

diffCon

protected double diffCon

evapRate

protected double evapRate

readMatrix

protected DoubleMatrix readMatrix

writeMatrix

protected DoubleMatrix writeMatrix

xSize

protected int xSize

ySize

protected int ySize

x

protected int x

prevX

protected int prevX

nextX

protected int nextX

y

protected int y

prevY

protected int prevY

nextY

protected int nextY
Constructor Detail

Diffuse2D

public Diffuse2D(int xSize,
                 int ySize)
Constructs a Diffuse2d space with the specificed dimensions

Parameters:
xSize - size of the x dimension
ySize - size of the y dimension

Diffuse2D

public Diffuse2D(double diffusionConstant,
                 double evaporationRate,
                 int xSize,
                 int ySize)
Constructs a Diffuse2d space with the specified diffusion constant, evaporation rate, and dimensions

Parameters:
diffusionConstant - the diffusion constant
evaporationRate - the evaporation rate
xSize - size of the x dimension
ySize - size of the y dimension
Method Detail

setDiffusionConstant

public void setDiffusionConstant(double diffusionConstant)
Sets the diffusion constant for this Diffuse2d space


setEvaporationRate

public void setEvaporationRate(double rate)
Sets the evaporation rate for this Diffuse2d space


diffuse

public void diffuse()
Runs the diffusion with the current rates and values. Following the Swarm class, it is roughly newValue = evap(ownValue + diffusionConstant * (nghAvg - ownValue)) where nghAvg is the weighted average of a cells eight neighbors, and ownValue is the current value for the current cell.

Values from the readMatrix are used to calculate diffusion. This value is then written to the writeMatrix. When this has been done for every cell in the grid, the writeMatrix is copied to the readMatrix.


update

public void update()
Copies the writeLattice to the readLattice


getVonNeumannNeighbors

public double[] getVonNeumannNeighbors(int x,
                                       int y)
Gets the von Neumann neighbors of the specified coordinate. doubles are returned in west, east, north, south order. The double at x, y is not returned.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
Returns:
an array of doubles in west, east, north, south order

getVonNeumannNeighbors

public double[] getVonNeumannNeighbors(int x,
                                       int y,
                                       int xExtent,
                                       int yExtent)
Gets the extended von Neumann neighbors of the specified coordinate. The extension in the x and y direction are specified by xExtent and yExtent. doubles are returned in west, east, north, south order with the most distant object first. The double at x,y is not returned.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
xExtent - the extension of the neighborhood in the x direction
yExtent - the extension of the neighborhood in the y direction
Returns:
an array of doubles in west, east, north, south order with the most distant object first.

getMooreNeighbors

public double[] getMooreNeighbors(int x,
                                  int y)
Gets the Moore neighbors of the specified coordinate. doubles are returned by row starting with the "NW corner" and ending with the "SE corner." The double at x, y is not returned.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
Returns:
an array of doubles ordered by row starting with the "NW corner" and ending with the "SE corner."

getMooreNeighbors

public double[] getMooreNeighbors(int x,
                                  int y,
                                  int xExtent,
                                  int yExtent)
Gets the extended Moore neighbors of the specified coordinate. The extension in the x and y direction are specified by xExtent and yExtent. doubles are returned by row starting with the "NW corner" and ending with the "SE corner." The double at x,y is not returned.

Parameters:
x - the x coordinate of the object
y - the y coordinate of the object
xExtent - the extension of the neighborhood in the x direction
yExtent - the extension of the neighborhood in the y direction
Returns:
an array of doubles ordered by row starting with the "NW corner" and ending with the "SE corner."

findMaximum

public double[] findMaximum(int x,
                            int y,
                            int range,
                            boolean includeOrigin,
                            int neighborhoodType)
Finds the maximum grid cell value within a specified range from the specified origin coordinate.

Parameters:
x - the x origin coordinate
y - the y origin coordinate
range - the range out from the coordinate to search
includeOrigin - include object at origin in search
neighborhoodType - the type of neighborhood to search. Can be one of Discrete2DSpace.VON_NEUMANN or Discrete2DSpace.MOORE.
Returns:
the Objects determined to be the maximum.

compareMax

protected double[] compareMax(double[] array)

findMinimum

public double[] findMinimum(int x,
                            int y,
                            int range,
                            boolean includeOrigin,
                            int neighborhoodType)
Finds the minimum grid cell value within a specified range from the specified origin coordinate.

Parameters:
x - the x origin coordinate
y - the y origin coordinate
range - the range out from the coordinate to search
includeOrigin - include object at origin in search
neighborhoodType - the type of neighborhood to search. Can be one of Discrete2DSpace.VON_NEUMANN or Discrete2DSpace.MOORE.
Returns:
the Objects determined to be the maximum.

compareMin

protected double[] compareMin(double[] array)

getSizeX

public int getSizeX()
Gets the size of the x dimension

Specified by:
getSizeX in interface Discrete2DSpace

getSizeY

public int getSizeY()
Gets the size of the y dimension

Specified by:
getSizeY in interface Discrete2DSpace

getSize

public java.awt.Dimension getSize()
Gets the dimension of the space

Specified by:
getSize in interface Discrete2DSpace

getObjectAt

public java.lang.Object getObjectAt(int x,
                                    int y)
Gets the object (a Long) at the specified coordinate.

Specified by:
getObjectAt in interface Discrete2DSpace
Parameters:
x - the x coordinate
y - the y coordinate
Returns:
the Object at x,y

getValueAt

public double getValueAt(int x,
                         int y)
Gets the value at the specified coordinate.

Specified by:
getValueAt in interface Discrete2DSpace
Parameters:
x - the x coordinate
y - the y coordinate
Returns:
the value at x, y

putObjectAt

public void putObjectAt(int x,
                        int y,
                        java.lang.Object object)
Puts the specified Object at the specified coordinate.

Specified by:
putObjectAt in interface Discrete2DSpace
Parameters:
x - the x coordinate
y - the y coordinate
object - the object to put
Throws:
java.lang.IllegalArgumentException - if object is not an instance of Number

putValueAt

public void putValueAt(int x,
                       int y,
                       double value)
Puts the specified value at the specified coordinate.

Specified by:
putValueAt in interface Discrete2DSpace
Parameters:
x - the x coordinate
y - the y coordinate
value - the value to put

getMatrix

public BaseMatrix getMatrix()
Gets the matrix collection class that contains all the values. In this case the readMatrix is returned.

Specified by:
getMatrix in interface Discrete2DSpace
Returns:
the write matrix.

xnorm

protected int xnorm(int x)
Normalize the x value to the toroidal coordinates

Parameters:
x - the value to normalize
Returns:
the normalized value

ynorm

protected int ynorm(int y)
Normalize the y value to the toroidal coordinates

Parameters:
y - the value to normalize
Returns:
the normalized value