Package microsim.space
Class DoubleDiffuseSpace
- java.lang.Object
-
- microsim.space.AbstractSpace<java.lang.Double>
-
- microsim.space.DoubleSpace
-
- microsim.space.DoubleDiffuseSpace
-
- All Implemented Interfaces:
EventListener
public class DoubleDiffuseSpace extends DoubleSpace implements EventListener
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 DblDiffuseTorusGrid space see the heatBugs example. See
diffuse
for a brief explanation of the diffusion algorithm. Note that this space doesn't seem to work correctly when run with hotspot 1.01- Version:
- $Revision: 1.2 $ $Date: 2004/08/11 09:05:30 $
- Author:
- Nick Collier
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DoubleDiffuseSpace.Verbs
-
Field Summary
Fields Modifier and Type Field Description static int
EVENT_DIFFUSE
static long
MAX
static int
MOORE
static int
VON_NEUMANN
-
Constructor Summary
Constructors Constructor Description DoubleDiffuseSpace(int xSize, int ySize)
Constructs a DblDiffuseTorusGrid space with the specificed dimensionsDoubleDiffuseSpace(int xSize, int ySize, double diffusionConstant, double evaporationRate)
Constructs a DblDiffuseTorusGrid space with the specified diffusion constant, evaporation rate, and dimensions
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.double[]
getMatrix()
Return the matrix of values representing the grid.double[]
getMooreNeighbors(int x, int y)
Gets the Moore neighbors of the specified coordinate.double[]
getMooreNeighbors(int x, int y, int xExtent, int yExtent)
Gets the extended Moore neighbors of 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
onEvent(java.lang.Enum<?> type)
void
print(double[] mtr)
void
setDiffusionConstant(double diffusionConstant)
Sets the diffusion constant for this DblDiffuseTorusGrid spacevoid
setEvaporationRate(double rate)
Sets the evaporation rate for this DblDiffuseTorusGrid spacevoid
update()
Copies the writeLattice to the readLattice-
Methods inherited from class microsim.space.DoubleSpace
add, clear, copyGridTo, copyGridTo, countObjectsAt, get, getDbl, max, mean, min, multiply, resetTo, set, setDbl, size, sum, swapPositions, variance
-
Methods inherited from class microsim.space.AbstractSpace
boundX, boundY, equals, getMooreNeighborsPositions, getVonNeumannNeighborsPositions, getXSize, getYSize, reflectX, reflectY, torusX, torusY, toString
-
-
-
-
Field Detail
-
EVENT_DIFFUSE
public static final int EVENT_DIFFUSE
- See Also:
- Constant Field Values
-
VON_NEUMANN
public static final int VON_NEUMANN
- See Also:
- Constant Field Values
-
MOORE
public static final int MOORE
- See Also:
- Constant Field Values
-
MAX
public static final long MAX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DoubleDiffuseSpace
public DoubleDiffuseSpace(int xSize, int ySize)
Constructs a DblDiffuseTorusGrid space with the specificed dimensions- Parameters:
xSize
- size of the x dimensionySize
- size of the y dimension
-
DoubleDiffuseSpace
public DoubleDiffuseSpace(int xSize, int ySize, double diffusionConstant, double evaporationRate)
Constructs a DblDiffuseTorusGrid space with the specified diffusion constant, evaporation rate, and dimensions- Parameters:
diffusionConstant
- the diffusion constantevaporationRate
- the evaporation ratexSize
- size of the x dimensionySize
- size of the y dimension
-
-
Method Detail
-
setDiffusionConstant
public void setDiffusionConstant(double diffusionConstant)
Sets the diffusion constant for this DblDiffuseTorusGrid space
-
setEvaporationRate
public void setEvaporationRate(double rate)
Sets the evaporation rate for this DblDiffuseTorusGrid 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
-
print
public void print(double[] mtr)
-
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 objecty
- 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 objecty
- the y coordinate of the objectxExtent
- the extension of the neighborhood in the x directionyExtent
- 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 objecty
- 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 objecty
- the y coordinate of the objectxExtent
- the extension of the neighborhood in the x directionyExtent
- 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 coordinatey
- the y origin coordinaterange
- the range out from the coordinate to searchincludeOrigin
- include object at origin in searchneighborhoodType
- the type of neighborhood to search. Can be one of Discrete2DSpace.VON_NEUMANN or Discrete2DSpace.MOORE.- Returns:
- the Objects determined to be the maximum.
-
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 coordinatey
- the y origin coordinaterange
- the range out from the coordinate to searchincludeOrigin
- include object at origin in searchneighborhoodType
- the type of neighborhood to search. Can be one of Discrete2DSpace.VON_NEUMANN or Discrete2DSpace.MOORE.- Returns:
- the Objects determined to be the maximum.
-
getMatrix
public double[] getMatrix()
Description copied from class:DoubleSpace
Return the matrix of values representing the grid.- Overrides:
getMatrix
in classDoubleSpace
- Returns:
- A matrix of double with the same dimensions of the grid.
-
onEvent
public void onEvent(java.lang.Enum<?> type)
- Specified by:
onEvent
in interfaceEventListener
-
-