use of cbit.vcell.math.MembraneRegionVariable 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.MembraneRegionVariable 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.MembraneRegionVariable 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;
}
use of cbit.vcell.math.MembraneRegionVariable in project vcell by virtualcell.
the class Xmlproducer method getXML.
/**
* This method returns a XML representation of a MathDescription object.
* Creation date: (3/2/2001 10:57:25 AM)
* @return Element
* @param mathdes cbit.vcell.math.MathDescription
*/
Element getXML(MathDescription mathdes) throws XmlParseException {
Element math = new Element(XMLTags.MathDescriptionTag);
// Add attributes
math.setAttribute(XMLTags.NameAttrTag, mangle(mathdes.getName()));
// Add annotation
if (mathdes.getDescription() != null && mathdes.getDescription().length() > 0) {
Element annotationElem = new Element(XMLTags.AnnotationTag);
annotationElem.setText(mangle(mathdes.getDescription()));
math.addContent(annotationElem);
}
List<ParticleMolecularType> particleMolecularTypes = mathdes.getParticleMolecularTypes();
for (ParticleMolecularType particleMolecularType : particleMolecularTypes) {
math.addContent(getXML(particleMolecularType));
}
// Add Constant subelements
Enumeration<Variable> enum1 = mathdes.getVariables();
/*java.util.Iterator k;
try {
VariableHash varHash = new VariableHash();
while (enum1.hasMoreElements())
varHash.addVariable((Variable)enum1.nextElement());
Variable vars [] = varHash.getReorderedVariables();
k = new ArrayList(java.util.Arrays.asList(vars)).iterator();
} catch (cbit.vcell.mapping.MappingException e) {
e.printStackTrace();
return null;
}*/
while (enum1.hasMoreElements()) {
Variable var = enum1.nextElement();
Element element = null;
if (var instanceof Constant) {
element = getXML((Constant) var);
} else if (var instanceof FilamentRegionVariable) {
element = getXML((FilamentRegionVariable) var);
} else if (var instanceof FilamentVariable) {
element = getXML((FilamentVariable) var);
} else if (var instanceof PointVariable) {
element = getXML((PointVariable) var);
} else if (var instanceof Function) {
element = getXML((Function) var);
} else if (var instanceof RandomVariable) {
element = getXML((RandomVariable) var);
} else if (var instanceof InsideVariable) {
// *** for internal use! Ignore it ***
continue;
} else if (var instanceof MembraneRegionVariable) {
element = getXML((MembraneRegionVariable) var);
} else if (var instanceof MemVariable) {
element = getXML((MemVariable) var);
} else if (var instanceof OutsideVariable) {
// *** for internal use! Ignore it ****
continue;
} else if (var instanceof VolumeRegionVariable) {
element = getXML((VolumeRegionVariable) var);
} else if (var instanceof VolVariable) {
element = getXML((VolVariable) var);
} else if (var instanceof StochVolVariable) {
// added for stochastic volumn variables
element = getXML((StochVolVariable) var);
} else if (var instanceof ParticleVariable) {
element = getXML((ParticleVariable) var);
} else if (var instanceof ParticleObservable) {
element = getXML((ParticleObservable) var);
} else {
throw new XmlParseException("An unknown variable type " + var.getClass().getName() + " was found when parsing the mathdescription " + mathdes.getName() + "!");
}
transcribeComments(var, element);
math.addContent(element);
}
// this was moved to the simspec!
/* buffer.append("\n");
if (geometry != null){
buffer.append(geometry.getXML());
}
buffer.append("\n");*/
// Add subdomains
Enumeration<SubDomain> enum2 = mathdes.getSubDomains();
while (enum2.hasMoreElements()) {
SubDomain subDomain = enum2.nextElement();
math.addContent(getXML(subDomain));
}
// Add Metadata (Version) if there is!
if (mathdes.getVersion() != null) {
math.addContent(getXML(mathdes.getVersion(), mathdes));
}
Iterator<Event> iter = mathdes.getEvents();
while (iter.hasNext()) {
math.addContent(getXML(iter.next()));
}
PostProcessingBlock postProcessingBlock = mathdes.getPostProcessingBlock();
if (postProcessingBlock.getNumDataGenerators() > 0) {
math.addContent(getXML(postProcessingBlock));
}
return math;
}
use of cbit.vcell.math.MembraneRegionVariable in project vcell by virtualcell.
the class ModelOptimizationSpec method removeUncoupledParameters.
public void removeUncoupledParameters() {
try {
localIssueList.clear();
MathMapping mathMapping = getSimulationContext().createNewMathMapping();
MathDescription mathDesc = mathMapping.getMathDescription();
MathSystemHash mathSystemHash = fromMath(mathDesc);
Graph graph = mathSystemHash.getDependencyGraph(mathSystemHash.getSymbols());
Tree[] spanningTrees = graph.getSpanningForest();
//
for (int i = 0; i < spanningTrees.length; i++) {
Node[] treeNodes = spanningTrees[i].getNodes();
boolean bHasStateVariables = false;
for (int j = 0; j < treeNodes.length; j++) {
Node node = treeNodes[j];
Variable var = mathDesc.getVariable(node.getName());
if (var instanceof VolVariable || var instanceof MemVariable || var instanceof FilamentVariable || var instanceof VolumeRegionVariable || var instanceof MembraneRegionVariable || var instanceof FilamentRegionVariable) {
bHasStateVariables = true;
break;
}
}
if (!bHasStateVariables) {
spanningTrees = (Tree[]) BeanUtils.removeElement(spanningTrees, spanningTrees[i]);
i--;
}
}
//
// remove parameters not mapped to a surviving tree (not coupled to any state variables
//
ArrayList<ParameterMappingSpec> paramMappingSpecsList = new ArrayList<ParameterMappingSpec>();
paramMappingSpecsList.addAll(Arrays.asList(fieldParameterMappingSpecs));
for (int i = 0; i < paramMappingSpecsList.size(); i++) {
Parameter parameter = paramMappingSpecsList.get(i).getModelParameter();
String mathName = mathMapping.getMathSymbolMapping().getVariable(parameter).getName();
boolean bFoundInTree = false;
for (int j = 0; j < spanningTrees.length; j++) {
Node node = spanningTrees[j].getNode(mathName);
if (node != null) {
bFoundInTree = true;
}
}
if (!bFoundInTree) {
paramMappingSpecsList.remove(i);
i--;
}
}
ParameterMappingSpec[] parameterMappingSpecs = new ParameterMappingSpec[paramMappingSpecsList.size()];
paramMappingSpecsList.toArray(parameterMappingSpecs);
setParameterMappingSpecs(parameterMappingSpecs);
} catch (Exception e) {
e.printStackTrace(System.out);
localIssueList.add(new Issue(this, localIssueContext, IssueCategory.ParameterEstimationGeneralWarning, e.getMessage(), Issue.SEVERITY_WARNING));
// throw new RuntimeException(e.getMessage());
}
}
Aggregations