use of cbit.vcell.math.VolVariable in project vcell by virtualcell.
the class MathTestingUtilities method comparePDEResultsWithExact.
/**
* Insert the method's description here.
* Creation date: (8/20/2003 12:58:10 PM)
*/
public static SimulationComparisonSummary comparePDEResultsWithExact(SimulationSymbolTable simSymbolTable, PDEDataManager dataManager, String type, double absErrorThreshold, double relErrorThreshold) throws DataAccessException, ExpressionException {
java.util.Hashtable<String, DataErrorSummary> tempVarHash = new java.util.Hashtable<String, DataErrorSummary>();
double[] timeArray = dataManager.getDataSetTimes();
Variable[] vars = simSymbolTable.getVariables();
CartesianMesh mesh = dataManager.getMesh();
MathDescription mathDesc = simSymbolTable.getSimulation().getMathDescription();
// Get volumeSubdomains from mathDesc/mesh and store in lookupTable
int numVol = mesh.getSizeX() * mesh.getSizeY() * mesh.getSizeZ();
CompartmentSubDomain[] volSubDomainLookup = new CompartmentSubDomain[numVol];
for (int i = 0; i < numVol; i++) {
int subVolumeIndex = mesh.getSubVolumeFromVolumeIndex(i);
SubVolume subVolume = mathDesc.getGeometry().getGeometrySpec().getSubVolume(subVolumeIndex);
CompartmentSubDomain compSubDomain = mathDesc.getCompartmentSubDomain(subVolume.getName());
volSubDomainLookup[i] = compSubDomain;
}
// Get membraneSubdomains from mathDesc/mesh and store in lookupTable
int numMem = mesh.getMembraneElements().length;
MembraneSubDomain[] memSubDomainLookup = new MembraneSubDomain[numMem];
for (int i = 0; i < numMem; i++) {
int insideVolIndex = mesh.getMembraneElements()[i].getInsideVolumeIndex();
int outsideVolIndex = mesh.getMembraneElements()[i].getOutsideVolumeIndex();
MembraneSubDomain memSubDomain = mathDesc.getMembraneSubDomain(volSubDomainLookup[insideVolIndex], volSubDomainLookup[outsideVolIndex]);
memSubDomainLookup[i] = memSubDomain;
}
double[] valueArray = new double[4];
SimpleSymbolTable symbolTable = new SimpleSymbolTable(new String[] { "t", "x", "y", "z" });
int tIndex = symbolTable.getEntry("t").getIndex();
int xIndex = symbolTable.getEntry("x").getIndex();
int yIndex = symbolTable.getEntry("y").getIndex();
int zIndex = symbolTable.getEntry("z").getIndex();
SimulationComparisonSummary simComparisonSummary = new SimulationComparisonSummary();
String hashKey = new String("");
long dataLength = 0;
// for each var, do the following :
for (int i = 0; i < vars.length; i++) {
if (vars[i] instanceof VolVariable || vars[i] instanceof MemVariable || vars[i] instanceof FilamentVariable || vars[i] instanceof VolumeRegionVariable || vars[i] instanceof MembraneRegionVariable || vars[i] instanceof FilamentRegionVariable) {
// for each time in timeArray,
for (int j = 0; j < timeArray.length; j++) {
if (type.equals(TestCaseNew.EXACT_STEADY)) {
if (j != (timeArray.length - 1)) {
continue;
}
}
// get data block from varName, data from datablock
SimDataBlock simDataBlock = dataManager.getSimDataBlock(vars[i].getName(), timeArray[j]);
double[] data = simDataBlock.getData();
dataLength = data.length;
SubDomain subDomain = null;
Coordinate subDomainCoord = null;
// for each point in data block ...
for (int k = 0; k < dataLength; k++) {
// Get subdomain from mesh (from the lookupTable), get coordinates (x,y,z) from mesh, evaluate EXACT SOLN at that coord
if (vars[i] instanceof VolVariable) {
subDomain = volSubDomainLookup[k];
subDomainCoord = mesh.getCoordinateFromVolumeIndex(k);
} else if (vars[i] instanceof MemVariable) {
subDomain = memSubDomainLookup[k];
subDomainCoord = mesh.getCoordinateFromMembraneIndex(k);
} else {
throw new RuntimeException("Var " + vars[i].getName() + " not supported yet!");
}
hashKey = vars[i].getName() + ":" + subDomain.getName();
DataErrorSummary tempVar = (DataErrorSummary) tempVarHash.get(hashKey);
if (tempVar == null) {
Expression exp = new Expression(subDomain.getEquation(vars[i]).getExactSolution());
exp.bindExpression(simSymbolTable);
exp = MathUtilities.substituteFunctions(exp, simSymbolTable);
exp = exp.flatten();
exp.bindExpression(symbolTable);
tempVar = new DataErrorSummary(exp);
tempVarHash.put(hashKey, tempVar);
}
// time
valueArray[tIndex] = timeArray[j];
// x
valueArray[xIndex] = subDomainCoord.getX();
// y
valueArray[yIndex] = subDomainCoord.getY();
// z
valueArray[zIndex] = subDomainCoord.getZ();
// EXACT soln at coord subDomainCoord
double value = tempVar.getExactExp().evaluateVector(valueArray);
tempVar.addDataValues(value, data[k], timeArray[j], k, absErrorThreshold, relErrorThreshold);
}
// end for (k)
}
// end for (j)
}
// end - if (var)
}
// end for (i)
Enumeration<String> enumKeys = tempVarHash.keys();
while (enumKeys.hasMoreElements()) {
String key = enumKeys.nextElement();
DataErrorSummary tempVarSummary = tempVarHash.get(key);
simComparisonSummary.addVariableComparisonSummary(new VariableComparisonSummary(key, tempVarSummary.getMinRef(), tempVarSummary.getMaxRef(), tempVarSummary.getMaxAbsoluteError(), tempVarSummary.getMaxRelativeError(), tempVarSummary.getL2Norm(), tempVarSummary.getTimeAtMaxAbsoluteError(), tempVarSummary.getIndexAtMaxAbsoluteError(), tempVarSummary.getTimeAtMaxRelativeError(), tempVarSummary.getIndexAtMaxRelativeError()));
}
return simComparisonSummary;
}
use of cbit.vcell.math.VolVariable in project vcell by virtualcell.
the class MathTestingUtilities method getExactResultSet.
/**
* Insert the method's description here.
* Creation date: (1/17/2003 3:47:43 PM)
* @return cbit.vcell.solver.ode.ODESolverResultSet
* @param sim cbit.vcell.solver.Simulation
*/
public static ODESolverResultSet getExactResultSet(MathDescription mathDesc, double[] time, Constant sensitivityParam) throws Exception {
if (mathDesc.getGeometry().getDimension() != 0) {
throw new RuntimeException("can only handle non-spatial simulations.");
}
Simulation sim = new Simulation(mathDesc);
SimulationSymbolTable simSymbolTable = new SimulationSymbolTable(sim, 0);
ODESolverResultSet resultSet = new ODESolverResultSet();
resultSet.addDataColumn(new ODESolverResultSetColumnDescription("t"));
for (int i = 0; i < time.length; i++) {
resultSet.addRow(new double[] { time[i] });
}
java.util.Enumeration<SubDomain> subDomainEnum = mathDesc.getSubDomains();
while (subDomainEnum.hasMoreElements()) {
SubDomain subDomain = subDomainEnum.nextElement();
java.util.Enumeration<Equation> enumEquations = subDomain.getEquations();
while (enumEquations.hasMoreElements()) {
Equation equation = enumEquations.nextElement();
Expression exactSolution = equation.getExactSolution();
if (exactSolution != null) {
exactSolution = new Expression(exactSolution);
exactSolution.bindExpression(simSymbolTable);
exactSolution = simSymbolTable.substituteFunctions(exactSolution);
exactSolution.bindExpression(simSymbolTable);
exactSolution = exactSolution.flatten();
resultSet.addFunctionColumn(new FunctionColumnDescription(exactSolution, equation.getVariable().getName(), null, equation.getVariable().getName(), false));
if (sensitivityParam != null) {
exactSolution = equation.getExactSolution();
Expression exactSensitivity = new Expression(exactSolution);
exactSensitivity.bindExpression(simSymbolTable);
exactSensitivity = simSymbolTable.substituteFunctions(exactSensitivity);
exactSensitivity.bindExpression(simSymbolTable);
exactSensitivity = exactSensitivity.differentiate(sensitivityParam.getName());
exactSensitivity = exactSensitivity.flatten();
VolVariable volVar = (VolVariable) equation.getVariable();
String sensName = SensVariable.getSensName(volVar, sensitivityParam);
resultSet.addFunctionColumn(new FunctionColumnDescription(exactSensitivity, sensName, null, sensName, false));
}
} else {
throw new RuntimeException("variable " + equation.getVariable().getName() + " doesn't have an exact solution");
}
}
}
return resultSet;
}
use of cbit.vcell.math.VolVariable in project vcell by virtualcell.
the class FiniteVolumeFileWriter method replaceVolumeVariable.
public static String replaceVolumeVariable(SimulationTask simTask, MembraneSubDomain msd, Expression exp) throws MathException, ExpressionException {
Expression fluxExpr = new Expression(exp);
String[] symbols = exp.getSymbols();
String flux = null;
// size function may be in the expression, they are not variables
if (symbols == null) {
flux = exp.infix();
} else {
for (String symbol : symbols) {
Variable var = simTask.getSimulationJob().getSimulationSymbolTable().getVariable(symbol);
if (var instanceof VolVariable || var instanceof VolumeRegionVariable) {
fluxExpr.substituteInPlace(new Expression(var.getName()), new Expression(var.getName() + "_" + var.getDomain().getName() + "_membrane"));
}
}
// for old models
flux = fluxExpr.infix();
flux = flux.replaceAll("_INSIDE", "_" + msd.getInsideCompartment().getName() + "_membrane");
flux = flux.replaceAll("_OUTSIDE", "_" + msd.getOutsideCompartment().getName() + "_membrane");
}
return flux;
}
use of cbit.vcell.math.VolVariable in project vcell by virtualcell.
the class SimulationSymbolTable method hasTimeVaryingDiffusionOrAdvection.
public boolean hasTimeVaryingDiffusionOrAdvection(Variable variable) throws MathException, ExpressionException {
Enumeration<SubDomain> enum1 = simulation.getMathDescription().getSubDomains();
while (enum1.hasMoreElements()) {
SubDomain subDomain = enum1.nextElement();
Equation equation = subDomain.getEquation(variable);
//
if (equation instanceof PdeEquation) {
Vector<Expression> spatialExpressionList = new Vector<Expression>();
spatialExpressionList.add(((PdeEquation) equation).getDiffusionExpression());
if (((PdeEquation) equation).getVelocityX() != null) {
spatialExpressionList.add(((PdeEquation) equation).getVelocityX());
}
if (((PdeEquation) equation).getVelocityY() != null) {
spatialExpressionList.add(((PdeEquation) equation).getVelocityY());
}
if (((PdeEquation) equation).getVelocityZ() != null) {
spatialExpressionList.add(((PdeEquation) equation).getVelocityZ());
}
for (int i = 0; i < spatialExpressionList.size(); i++) {
Expression spatialExp = spatialExpressionList.elementAt(i);
spatialExp = substituteFunctions(spatialExp);
String[] symbols = spatialExp.getSymbols();
if (symbols != null) {
for (int j = 0; j < symbols.length; j++) {
SymbolTableEntry entry = spatialExp.getSymbolBinding(symbols[j]);
if (entry instanceof ReservedVariable) {
if (((ReservedVariable) entry).isTIME()) {
return true;
}
}
if (entry instanceof VolVariable) {
return true;
}
if (entry instanceof VolumeRegionVariable) {
return true;
}
if (entry instanceof MemVariable || entry instanceof MembraneRegionVariable) {
return true;
}
}
}
}
}
}
return false;
}
use of cbit.vcell.math.VolVariable in project vcell by virtualcell.
the class SimulationSymbolTable method createAnnotatedFunctionsList.
public Vector<AnnotatedFunction> createAnnotatedFunctionsList(MathDescription mathDescription) throws InconsistentDomainException {
// Get the list of (volVariables) in the simulation. Needed to determine 'type' of functions
boolean bSpatial = getSimulation().isSpatial();
String[] variableNames = null;
VariableType[] variableTypes = null;
if (bSpatial) {
Variable[] allVariables = getVariables();
Vector<Variable> varVector = new Vector<Variable>();
for (int i = 0; i < allVariables.length; i++) {
if ((allVariables[i] instanceof VolVariable) || (allVariables[i] instanceof VolumeRegionVariable) || (allVariables[i] instanceof MemVariable) || (allVariables[i] instanceof MembraneRegionVariable) || (allVariables[i] instanceof FilamentVariable) || (allVariables[i] instanceof FilamentRegionVariable) || (allVariables[i] instanceof PointVariable) || (allVariables[i] instanceof ParticleVariable) || (allVariables[i] instanceof InsideVariable) || (allVariables[i] instanceof OutsideVariable)) {
varVector.addElement(allVariables[i]);
} else if (allVariables[i] instanceof Constant || (allVariables[i] instanceof Function)) {
} else {
System.err.println("SimulationSymbolTable.createAnnotatedFunctionsList() found unexpected variable type " + allVariables[i].getClass().getSimpleName() + " in spatial simulation");
}
}
variableNames = new String[varVector.size()];
for (int i = 0; i < variableNames.length; i++) {
variableNames[i] = varVector.get(i).getName();
}
// Lookup table for variableType for each variable in 'variables' array.
variableTypes = new VariableType[variableNames.length];
for (int i = 0; i < variableNames.length; i++) {
variableTypes[i] = VariableType.getVariableType(varVector.get(i));
}
}
//
// Bind and substitute functions to simulation before storing them in the '.functions' file
//
Function[] functions = getFunctions();
Vector<AnnotatedFunction> annotatedFunctionVector = new Vector<AnnotatedFunction>();
for (int i = 0; i < functions.length; i++) {
if (isFunctionSaved(functions[i])) {
String errString = "";
VariableType funcType = null;
try {
Expression substitutedExp = substituteFunctions(functions[i].getExpression());
substitutedExp.bindExpression(this);
functions[i].setExpression(substitutedExp.flatten());
} catch (MathException e) {
e.printStackTrace(System.out);
errString = errString + ", " + e.getMessage();
// throw new RuntimeException(e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
errString = errString + ", " + e.getMessage();
// throw new RuntimeException(e.getMessage());
}
//
// get function's data type from the types of it's identifiers
//
funcType = bSpatial ? getFunctionVariableType(functions[i], mathDescription, variableNames, variableTypes, bSpatial) : VariableType.NONSPATIAL;
AnnotatedFunction annotatedFunc = new AnnotatedFunction(functions[i].getName(), functions[i].getExpression(), functions[i].getDomain(), errString, funcType, FunctionCategory.PREDEFINED);
annotatedFunctionVector.addElement(annotatedFunc);
}
}
return annotatedFunctionVector;
}
Aggregations