/** * 2d box with no scalar fields of a fixed (at construction time) * width and height * @author Michael Schuresko * @version %I%, %G% * @since 1.0 */ public class SimpleScalarEnvironment implements IEnvironment { BoxBoundary m_boundary; double[] m_arrLfMin; double[] m_arrLfMax; IScalarField m_field; public SimpleScalarEnvironment() { initializeVars(100.0,100.0, false); } public SimpleScalarEnvironment(double lfWidth, double lfHeight, boolean bAddDefault) { initializeVars(lfWidth, lfHeight, bAddDefault); } public SimpleScalarEnvironment(double [] arrLfMin, double [] arrLfMax, boolean bAddDflt) { initializeVars(arrLfMin, arrLfMax, bAddDflt); } public SimpleScalarEnvironment(SimpleScalarEnvironment src) { m_field = src.m_field.makeCopy(); m_arrLfMax = MathUtils.arrDblCpy(src.m_arrLfMax, src.m_arrLfMax.length); m_arrLfMin = MathUtils.arrDblCpy(src.m_arrLfMin, src.m_arrLfMin.length); m_boundary = new BoxBoundary(m_arrLfMin, m_arrLfMax); } public void initializeVars(double lfWidth, double lfHeight, boolean bAddDefault) { m_arrLfMin = new double[2]; m_arrLfMax = new double[2]; m_arrLfMin[0] = 0.0; m_arrLfMin[1] = 0.0; m_arrLfMax[0] = lfWidth; m_arrLfMax[1] = lfHeight; Jama.Matrix matSigInv = Jama.Matrix.identity(2, 2); if(!bAddDefault) { matSigInv.timesEquals(0.0); } Jama.Matrix matCenter = new Jama.Matrix(2,1); matCenter.set(1, 0, 0.5*lfHeight); matCenter.set(0, 0, 0.25*lfWidth); ScalarFieldSumGauss field = new ScalarFieldSumGauss(2); try { field.addGaussian(1.0, matCenter, matSigInv); matCenter.set(0, 0, 0.75*lfWidth); field.addGaussian(1.0, matCenter, matSigInv); } catch(Exception e) { System.out.println("Mike, you'd better debug this"); } m_field = field; m_boundary = new BoxBoundary(m_arrLfMin, m_arrLfMax); } public void initializeVars(double [] arrLfMin, double [] arrLfMax, boolean bAddDflt) { m_arrLfMax = MathUtils.arrDblCpy(arrLfMax, arrLfMax.length); m_arrLfMin = MathUtils.arrDblCpy(arrLfMin, arrLfMin.length); Jama.Matrix matSigInv = Jama.Matrix.identity(2, 2); matSigInv.timesEquals(10.0); if(!bAddDflt) { matSigInv.timesEquals(0.0); } Jama.Matrix matCenter = new Jama.Matrix(2,1); matCenter.set(0, 0, 0.6*m_arrLfMin[0]+0.4*m_arrLfMax[0]); matCenter.set(1, 0, 0.15*m_arrLfMin[1] + 0.85*m_arrLfMax[1]); ScalarFieldSumGauss field = new ScalarFieldSumGauss(2); try { field.addGaussian(1.0, matCenter, matSigInv); matCenter.set(0, 0, 0.4*m_arrLfMin[0]+0.6*m_arrLfMax[0]); matCenter.set(1, 0, 0.85*m_arrLfMin[1] + 0.15*m_arrLfMax[1]); field.addGaussian(1.0, matCenter, matSigInv); } catch(Exception e) { System.out.println("Mike, you'd better debug this"); } m_field = field; m_boundary = new BoxBoundary(m_arrLfMin, m_arrLfMax); } /** * Gradient at a particular location for a given field * @param nWhichField field to take gradient of * @param arrLoc place at which to take gradient * @return gradient (of proper dimensionality) as a double [] */ public double [] getGradient(int nWhichField, double arrLoc[]) { if(nWhichField == 0 && m_field != null) { return this.m_field.getGradient(arrLoc); } return new double[m_arrLfMin.length]; } /** * Gradient at a particular location for a given field * @param nWhichField field to take gradient of * @param arrLoc place at which to take gradient * @param arrRslt gradient (of proper dimensionality) as a double [] */ public void getGradient(int nWhichField, double arrLoc[], double arrRslt[]) { if(nWhichField == 0 && m_field != null) { m_field.getGradient(arrLoc, arrRslt); } else { for(int i = 0; i < arrRslt.length; ++i) { arrRslt[i] = 0.0; } } } /** * dimensionality of space in which environment is embedded */ public int getDimensionality() { return 2; } /** * number of scalar fields over environment */ public int getNumScalars() { return 1; } /** * Gets a particular field value at a particular location * Invalid for this class * Figure out how to declare exceptions in interface definitions */ public double getFieldVal(int nWhichField, double arrLoc[]) { if(nWhichField == 0) { return this.m_field.getVal(arrLoc); } return 0.0; } /** * Gets all field values for a particular location as a vector * Invalid for this class * Figure out how to declare exceptions in interface definitions * @param arrLoc invalid for this class * @return null -- invalid for this class */ public double [] getFieldVals(double arrLoc[]) { double [] arrLfResult = new double[1]; arrLfResult[0] = getFieldVal(0, arrLoc); return arrLfResult; } public IEnvironment makeCopy() { return new SimpleScalarEnvironment(this); } /** * Gets boundary interface */ public IBoundary getBoundary() { return m_boundary; } }