package initialconditions; import java.lang.*; import java.io.*; import main.BetterTokenizer; import main.Cell; import main.GeneralInput; import main.Globals; import java.util.Random; public class InitialCondition extends Object { String node; float initValue=0f; float lowerBound=0f, upperBound=1.0f; float noiseLevel=0.0f; Random rng; public final boolean noiseAware = false; // in fact, it is, but as a base class we set the default here public InitialCondition() { rng = Globals.rng; } public InitialCondition(String n, float iv) { node = n; initValue = iv; rng = Globals.rng; } public void setModel(Cell [] cells) { for(int i = 0; i < cells.length; i++) { float val = initValue; if(noiseLevel > 0.0f) { val += noiseLevel * (float)rng.nextGaussian(); if(val < 0.0f) val = 0.0f; } try { cells[i].setInitialValue(node, val); } catch(Exception e) { System.out.println(e.toString()); return; } } } public void setFrom(Cell [] cells, boolean takePresentValue) throws Exception { if(!takePresentValue) { initValue = cells[0].getInitialValue(node); for(int i = 1; i < cells.length; i++) { if(initValue != cells[i].getInitialValue(node)) { throw new UnsuitableICException(); } } } else { initValue = cells[0].nodePeek(node); for(int i = 1; i < cells.length; i++) { if(initValue != cells[i].nodePeek(node)) { throw new UnsuitableICException(); } } } } public void changeValue(float newValue) { initValue = newValue; } public float getValue() { return initValue; } public void setValue(float value) { initValue = value; } public float getLowerBound() { return lowerBound; } public float getUpperBound() { return upperBound; } public String getNodeName() { return node; } public void print() { System.out.println(node + "=" + initValue + "; "); } public void loadParameters(BetterTokenizer tokenizer) throws Exception { GeneralInput.indent++; String info = GeneralInput.findNextIDToken(tokenizer); while(!info.equals("endIC")) { loadParameter(info, tokenizer); info = GeneralInput.findNextIDToken(tokenizer); } GeneralInput.indent--; } // Override this function for each type of init condition. You can still call this one to load node protected void loadParameter(String info, BetterTokenizer tokenizer) throws Exception { if(info.equals("Node")) { GeneralInput.nextToken(tokenizer); node = tokenizer.sval; } else if(info.equals("Value")) { GeneralInput.nextToken(tokenizer); initValue = (float)tokenizer.nval; } else if(info.equals("LowerBound")) { GeneralInput.nextToken(tokenizer); lowerBound = (float)tokenizer.nval; } else if(info.equals("UpperBound")) { GeneralInput.nextToken(tokenizer); upperBound = (float)tokenizer.nval; } else if(info.equals("NoiseLevel")) { GeneralInput.nextToken(tokenizer); noiseLevel = (float)tokenizer.nval; } } public void toString(PrintWriter pw, String indent) { pw.println(indent + "&Node\t" + node); pw.println(indent + "&Value\t" + initValue); pw.println(indent + "&LowerBound\t" + lowerBound); pw.println(indent + "&UpperBound\t" + upperBound); pw.println(indent + "&NoiseLevel\t" + noiseLevel); } }