/**************************************************** Sets a row of cells to a given value. User can specify which row, and the beginning and ending cell to be set on that row. ****************************************************/ package initialconditions; import java.lang.*; import java.io.*; import main.BetterTokenizer; import main.Cell; import main.GeneralInput; public class RowIC extends InitialCondition { /** The row whose value should be set. */ int pos=-1; /** The first column in the row whose value should be set. -1 indicates set from start (default). */ int startCol = -1; /** The last column in the row whose value should be set. -1 indicates set until end (default). */ int endCol = -1; public final boolean noiseAware = true; public RowIC() { super() ; } public RowIC(String n, float iv, int p) { super(); node = n; initValue = iv; pos = p; } public void setModel(Cell [] cells) { int index = pos * Cell.arrayWidth; int start = 0, end = Cell.arrayWidth - 1; if(startCol != -1) start = startCol; if(endCol != -1) end = endCol; for(int i = start; i <= end; i++) { float val = initValue; if(noiseLevel > 0.0f) { val += noiseLevel * (float)rng.nextGaussian(); if(val < 0.0f) val = 0.0f; } try { cells[i + index].setInitialValue(node, val); } catch(Exception e) { System.out.println(e.toString()); return; } } } public void setFrom(Cell [] cells, boolean takePresentValue) throws Exception { int index = pos * Cell.arrayWidth; int start = 0, end = Cell.arrayWidth - 1; if(startCol != -1) start = startCol; if(endCol != -1) end = endCol; if(!takePresentValue) { initValue = cells[index + start].getInitialValue(node); for(int i = start + 1; i <= end; i++) { if(initValue != cells[index + i].getInitialValue(node)) { throw new UnsuitableICException(); } } } else { initValue = cells[index].nodePeek(node); for(int i = start + 1; i <= end; i++) { if(initValue != cells[index + i].nodePeek(node)) { throw new UnsuitableICException(); } } } } public void print() { System.out.println("Row of " + node + " = " + initValue + " in position " + pos + "; "); } protected void loadParameter(String info, BetterTokenizer tokenizer) throws Exception { if(info.equals("Row") || info.equals("YPos")) { GeneralInput.nextToken(tokenizer); pos = (int)tokenizer.nval; } else if(info.equals("StartCol")) { GeneralInput.nextToken(tokenizer); startCol = (int)tokenizer.nval; } else if(info.equals("EndCol")) { GeneralInput.nextToken(tokenizer); endCol = (int)tokenizer.nval; } else super.loadParameter(info, tokenizer); } public void toString(PrintWriter pw, String indent) { super.toString(pw, indent); pw.println(indent + "&Row\t" + pos); if(startCol != -1) pw.println(indent + "&StartCol\t" + startCol); if(endCol != -1) pw.println(indent + "&EndCol\t" + endCol); } }