use of cbit.vcell.math.Variable in project vcell by virtualcell.
the class FastSystemAnalyzer method refreshFastVarList.
/**
* @return java.util.Vector
*/
private void refreshFastVarList() throws MathException, ExpressionException {
fastVarList.clear();
//
// get list of unique (VolVariables and MemVariables) in fastRate expressions
//
Enumeration<FastRate> fastRatesEnum = fastSystem.getFastRates();
while (fastRatesEnum.hasMoreElements()) {
FastRate fr = fastRatesEnum.nextElement();
Expression exp = fr.getFunction();
Enumeration<Variable> enum1 = MathUtilities.getRequiredVariables(exp, this);
while (enum1.hasMoreElements()) {
Variable var = enum1.nextElement();
if (var instanceof VolVariable || var instanceof MemVariable) {
if (!fastVarList.contains(var)) {
fastVarList.addElement(var);
// System.out.println("FastSystemImplicit.refreshFastVarList(), FAST RATE VARIABLE: "+var.getName());
}
}
}
}
//
// get list of all variables used in invariant expressions that are not used in fast rates
//
Enumeration<FastInvariant> fastInvariantsEnum = fastSystem.getFastInvariants();
while (fastInvariantsEnum.hasMoreElements()) {
FastInvariant fi = (FastInvariant) fastInvariantsEnum.nextElement();
Expression exp = fi.getFunction();
// System.out.println("FastSystemImplicit.refreshFastVarList(), ORIGINAL FAST INVARIANT: "+exp);
Enumeration<Variable> enum1 = MathUtilities.getRequiredVariables(exp, this);
while (enum1.hasMoreElements()) {
Variable var = enum1.nextElement();
if (var instanceof VolVariable || var instanceof MemVariable) {
if (!fastVarList.contains(var)) {
fastVarList.addElement(var);
}
}
}
}
//
// verify that there are N equations (rates+invariants) and N unknowns (fastVariables)
//
int numBoundFunctions = fastSystem.getNumFastInvariants() + fastSystem.getNumFastRates();
if (fastVarList.size() != numBoundFunctions) {
throw new MathException("FastSystem.checkDimension(), there are " + fastVarList.size() + " variables and " + numBoundFunctions + " FastInvariant's & FastRates");
}
}
use of cbit.vcell.math.Variable in project vcell by virtualcell.
the class XmlReader method readParameters.
private void readParameters(List<Element> parameterElements, ParameterContext parameterContext, HashMap<String, ParameterRoleEnum> roleHash, ParameterRoleEnum userDefinedRole, HashSet<String> xmlRolesTagsToIgnore, Model model) throws XmlParseException {
String contextName = parameterContext.getNameScope().getName();
try {
//
// prepopulate varHash with reserved symbols
//
VariableHash varHash = new VariableHash();
addResevedSymbols(varHash, model);
//
for (Element xmlParam : parameterElements) {
String parsedParamName = unMangle(xmlParam.getAttributeValue(XMLTags.NameAttrTag));
String parsedRoleString = xmlParam.getAttributeValue(XMLTags.ParamRoleAttrTag);
String parsedExpressionString = xmlParam.getText();
//
if (xmlRolesTagsToIgnore.contains(parsedRoleString)) {
varHash.removeVariable(parsedParamName);
continue;
}
Expression paramExp = null;
if (parsedExpressionString.trim().length() > 0) {
paramExp = unMangleExpression(parsedExpressionString);
}
if (varHash.getVariable(parsedParamName) == null) {
Domain domain = null;
varHash.addVariable(new Function(parsedParamName, paramExp, domain));
} else {
if (model.getReservedSymbolByName(parsedParamName) != null) {
varHash.removeVariable(parsedParamName);
Domain domain = null;
varHash.addVariable(new Function(parsedParamName, paramExp, domain));
}
}
//
// get the parameter for this xml role string
//
ParameterRoleEnum paramRole = roleHash.get(parsedRoleString);
if (paramRole == null) {
throw new XmlParseException("parameter '" + parsedParamName + "' has unexpected role '" + parsedRoleString + "' in '" + contextName + "'");
}
//
if (paramRole != userDefinedRole) {
LocalParameter paramWithSameRole = parameterContext.getLocalParameterFromRole(paramRole);
if (paramWithSameRole == null) {
throw new XmlParseException("can't find parameter with role '" + parsedRoleString + "' in '" + contextName + "'");
}
//
if (!paramWithSameRole.getName().equals(parsedParamName)) {
//
// first rename other parameters with same name
//
LocalParameter paramWithSameNameButDifferentRole = parameterContext.getLocalParameterFromName(parsedParamName);
if (paramWithSameNameButDifferentRole != null) {
//
// find available name
//
int n = 0;
String newName = parsedParamName + "_" + n++;
while (parameterContext.getEntry(newName) != null) {
newName = parsedParamName + "_" + n++;
}
parameterContext.renameLocalParameter(parsedParamName, newName);
}
//
// then rename parameter with correct role
//
parameterContext.renameLocalParameter(paramWithSameRole.getName(), parsedParamName);
}
}
}
//
// create unresolved parameters for all unresolved symbols
//
String unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
while (unresolvedSymbol != null) {
try {
Domain domain = null;
// will turn into an UnresolvedParameter.
varHash.addVariable(new Function(unresolvedSymbol, new Expression(0.0), domain));
} catch (MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException(e.getMessage());
}
parameterContext.addUnresolvedParameter(unresolvedSymbol);
unresolvedSymbol = varHash.getFirstUnresolvedSymbol();
}
//
// in topological order, add parameters to model (getting units also).
// note that all pre-defined parameters already have the correct names
// here we set expressions on pre-defined parameters and add user-defined parameters
//
Variable[] sortedVariables = varHash.getTopologicallyReorderedVariables();
ModelUnitSystem modelUnitSystem = model.getUnitSystem();
for (int i = sortedVariables.length - 1; i >= 0; i--) {
if (sortedVariables[i] instanceof Function) {
Function paramFunction = (Function) sortedVariables[i];
Element xmlParam = null;
for (int j = 0; j < parameterElements.size(); j++) {
Element tempParam = (Element) parameterElements.get(j);
if (paramFunction.getName().equals(unMangle(tempParam.getAttributeValue(XMLTags.NameAttrTag)))) {
xmlParam = tempParam;
break;
}
}
if (xmlParam == null) {
// must have been an unresolved parameter
continue;
}
String symbol = xmlParam.getAttributeValue(XMLTags.VCUnitDefinitionAttrTag);
VCUnitDefinition unit = null;
if (symbol != null) {
unit = modelUnitSystem.getInstance(symbol);
}
LocalParameter tempParam = parameterContext.getLocalParameterFromName(paramFunction.getName());
if (tempParam == null) {
tempParam = parameterContext.addLocalParameter(paramFunction.getName(), new Expression(0.0), userDefinedRole, unit, userDefinedRole.getDescription());
parameterContext.setParameterValue(tempParam, paramFunction.getExpression(), true);
} else {
if (tempParam.getExpression() != null) {
// if the expression is null, it should remain null.
parameterContext.setParameterValue(tempParam, paramFunction.getExpression(), true);
}
tempParam.setUnitDefinition(unit);
}
}
}
} catch (PropertyVetoException | ExpressionException | MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException("Exception while setting parameters for '" + contextName + "': " + e.getMessage(), e);
}
}
use of cbit.vcell.math.Variable in project vcell by virtualcell.
the class XmlReader method getParticleJumpProcess.
private ParticleJumpProcess getParticleJumpProcess(Element param, MathDescription md) throws XmlParseException {
// name
String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
ProcessSymmetryFactor processSymmetryFactor = null;
Attribute symmetryFactorAttr = param.getAttribute(XMLTags.ProcessSymmetryFactorAttrTag);
if (symmetryFactorAttr != null) {
processSymmetryFactor = new ProcessSymmetryFactor(Double.parseDouble(symmetryFactorAttr.getValue()));
}
// selected particle
List<ParticleVariable> varList = new ArrayList<ParticleVariable>();
Iterator<Element> iterator = param.getChildren(XMLTags.SelectedParticleTag, vcNamespace).iterator();
while (iterator.hasNext()) {
Element tempelement = (Element) iterator.next();
String varname = unMangle(tempelement.getAttributeValue(XMLTags.NameAttrTag));
Variable var = md.getVariable(varname);
if (!(var instanceof ParticleVariable)) {
throw new XmlParseException("Not a ParticleVariable in ParticleJumpProcess.");
}
varList.add((ParticleVariable) var);
}
// probability rate
JumpProcessRateDefinition jprd = null;
// for old models
Element pb = param.getChild(XMLTags.ParticleProbabilityRateTag, vcNamespace);
if (pb != null) {
Expression exp = unMangleExpression(pb.getText());
jprd = new MacroscopicRateConstant(exp);
} else // for new models
{
pb = param.getChild(XMLTags.MacroscopicRateConstantTag, vcNamespace);
if (// jump process rate defined by macroscopic rate constant
pb != null) {
Expression exp = unMangleExpression(pb.getText());
jprd = new MacroscopicRateConstant(exp);
} else // jump process rate defined by binding radius
{
pb = param.getChild(XMLTags.InteractionRadiusTag, vcNamespace);
if (pb != null) {
Expression exp = unMangleExpression(pb.getText());
jprd = new InteractionRadius(exp);
}
}
}
// add actions
List<Action> actionList = new ArrayList<Action>();
iterator = param.getChildren(XMLTags.ActionTag, vcNamespace).iterator();
while (iterator.hasNext()) {
Element tempelement = (Element) iterator.next();
try {
actionList.add(getAction(tempelement, md));
} catch (MathException e) {
e.printStackTrace();
throw new XmlParseException(e);
} catch (ExpressionException e) {
e.printStackTrace();
throw new XmlParseException(e);
}
}
ParticleJumpProcess jump = new ParticleJumpProcess(name, varList, jprd, actionList, processSymmetryFactor);
return jump;
}
use of cbit.vcell.math.Variable in project vcell by virtualcell.
the class XmlReader method getAction.
/**
* This method returns a Action object from a XML element.
* Creation date: (7/24/2006 5:56:36 PM)
* @return cbit.vcell.math.Action
* @param param org.jdom.Element
* @exception cbit.vcell.xml.XmlParseException The exception description.
*/
private Action getAction(Element param, MathDescription md) throws XmlParseException, MathException, ExpressionException {
// retrieve values
String operation = unMangle(param.getAttributeValue(XMLTags.OperationAttrTag));
String operand = param.getText();
Expression exp = null;
if (operand != null && operand.length() != 0) {
exp = unMangleExpression(operand);
}
String name = unMangle(param.getAttributeValue(XMLTags.VarNameAttrTag));
Variable var = md.getVariable(name);
if (var == null) {
throw new MathFormatException("variable " + name + " not defined");
}
if (!(var instanceof StochVolVariable) && !(var instanceof ParticleVariable)) {
throw new MathFormatException("variable " + name + " not a Stochastic Volume Variable");
}
try {
Action action = new Action(var, operation, exp);
return action;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of cbit.vcell.math.Variable in project vcell by virtualcell.
the class FiniteVolumeFileWriter method writeFieldData.
/**
* # Field Data
* FIELD_DATA_BEGIN
* #id, name, varname, time filename
* 0 _VCell_FieldData_0 FRAP_binding_ALPHA rfB 0.1 \\users\\fgao\\SimID_22489731_0_FRAP_binding_ALPHA_rfB_0_1.fdat
* FIELD_DATA_END
* @throws FileNotFoundException
* @throws ExpressionException
* @throws DataAccessException
*/
private void writeFieldData() throws FileNotFoundException, ExpressionException, DataAccessException {
FieldDataIdentifierSpec[] fieldDataIDSpecs = simTask.getSimulationJob().getFieldDataIdentifierSpecs();
if (fieldDataIDSpecs == null || fieldDataIDSpecs.length == 0) {
return;
}
String secondarySimDataDir = PropertyLoader.getProperty(PropertyLoader.secondarySimDataDirInternalProperty, null);
DataSetControllerImpl dsci = new DataSetControllerImpl(null, workingDirectory.getParentFile(), secondarySimDataDir == null ? null : new File(secondarySimDataDir));
printWriter.println("# Field Data");
printWriter.println("FIELD_DATA_BEGIN");
printWriter.println("#id, type, new name, name, varname, time, filename");
FieldFunctionArguments psfFieldFunc = null;
Variable var = simTask.getSimulationJob().getSimulationSymbolTable().getVariable(Simulation.PSF_FUNCTION_NAME);
if (var != null) {
FieldFunctionArguments[] ffas = FieldUtilities.getFieldFunctionArguments(var.getExpression());
if (ffas == null || ffas.length == 0) {
throw new DataAccessException("Point Spread Function " + Simulation.PSF_FUNCTION_NAME + " can only be a single field function.");
} else {
Expression newexp = new Expression(ffas[0].infix());
if (!var.getExpression().compareEqual(newexp)) {
throw new DataAccessException("Point Spread Function " + Simulation.PSF_FUNCTION_NAME + " can only be a single field function.");
}
psfFieldFunc = ffas[0];
}
}
int index = 0;
HashSet<FieldDataIdentifierSpec> uniqueFieldDataIDSpecs = new HashSet<FieldDataIdentifierSpec>();
uniqueFieldDataNSet = new HashSet<FieldDataNumerics>();
for (int i = 0; i < fieldDataIDSpecs.length; i++) {
if (!uniqueFieldDataIDSpecs.contains(fieldDataIDSpecs[i])) {
FieldFunctionArguments ffa = fieldDataIDSpecs[i].getFieldFuncArgs();
File newResampledFieldDataFile = new File(workingDirectory, SimulationData.createCanonicalResampleFileName((VCSimulationDataIdentifier) simTask.getSimulationJob().getVCDataIdentifier(), fieldDataIDSpecs[i].getFieldFuncArgs()));
uniqueFieldDataIDSpecs.add(fieldDataIDSpecs[i]);
VariableType varType = fieldDataIDSpecs[i].getFieldFuncArgs().getVariableType();
SimDataBlock simDataBlock = dsci.getSimDataBlock(null, fieldDataIDSpecs[i].getExternalDataIdentifier(), fieldDataIDSpecs[i].getFieldFuncArgs().getVariableName(), fieldDataIDSpecs[i].getFieldFuncArgs().getTime().evaluateConstant());
VariableType dataVarType = simDataBlock.getVariableType();
if (varType.equals(VariableType.UNKNOWN)) {
varType = dataVarType;
} else if (!varType.equals(dataVarType)) {
throw new IllegalArgumentException("field function variable type (" + varType.getTypeName() + ") doesn't match real variable type (" + dataVarType.getTypeName() + ")");
}
if (psfFieldFunc != null && psfFieldFunc.equals(ffa)) {
psfFieldIndex = index;
}
String fieldDataID = "_VCell_FieldData_" + index;
printWriter.println(index + " " + varType.getTypeName() + " " + fieldDataID + " " + ffa.getFieldName() + " " + ffa.getVariableName() + " " + ffa.getTime().infix() + " " + newResampledFieldDataFile);
uniqueFieldDataNSet.add(new FieldDataNumerics(SimulationData.createCanonicalFieldFunctionSyntax(ffa.getFieldName(), ffa.getVariableName(), ffa.getTime().evaluateConstant(), ffa.getVariableType().getTypeName()), fieldDataID));
index++;
}
}
if (psfFieldIndex >= 0) {
printWriter.println("PSF_FIELD_DATA_INDEX " + psfFieldIndex);
}
printWriter.println("FIELD_DATA_END");
printWriter.println();
}
Aggregations