use of cbit.vcell.math.StochVolVariable in project vcell by virtualcell.
the class XmlReader method getStochVolVariable.
/**
* This method returns a Stochasitc volumn variable from a XML element.
* Creation date: (7/24/2006 5:05:51 PM)
* @return cbit.vcell.math.StochVolVariable
* @param param org.jdom.Element
*/
private StochVolVariable getStochVolVariable(Element param) {
String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
// -- create new StochVolVariable object
StochVolVariable stochVar = new StochVolVariable(name);
transcribeComments(param, stochVar);
return stochVar;
}
use of cbit.vcell.math.StochVolVariable 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.StochVolVariable in project vcell by virtualcell.
the class XmlReader method getVarIniCount.
/**
* This method return a VarIniCondition object from a XML element.
* Creation date: (7/24/2006 5:26:05 PM)
* @return cbit.vcell.math.VarIniCondition
* @param param org.jdom.Element
* @exception cbit.vcell.xml.XmlParseException The exception description.
*/
private VarIniCondition getVarIniCount(Element param, MathDescription md) throws XmlParseException, MathException, ExpressionException {
// retrieve values
Expression exp = unMangleExpression(param.getText());
String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
Variable var = md.getVariable(name);
if (var == null) {
throw new MathFormatException("variable " + name + " not defined");
}
if (!(var instanceof StochVolVariable)) {
throw new MathFormatException("variable " + name + " not a Stochastic Volume Variable");
}
try {
VarIniCondition varIni = new VarIniCount(var, exp);
return varIni;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of cbit.vcell.math.StochVolVariable 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.StochVolVariable in project vcell by virtualcell.
the class StochMathMapping method refreshMathDescription.
/**
* set up a math description based on current simulationContext.
*/
@Override
protected void refreshMathDescription() throws MappingException, MatrixException, MathException, ExpressionException, ModelException {
// use local variable instead of using getter all the time.
SimulationContext simContext = getSimulationContext();
GeometryClass geometryClass = simContext.getGeometry().getGeometrySpec().getSubVolumes()[0];
Domain domain = new Domain(geometryClass);
// local structure mapping list
StructureMapping[] structureMappings = simContext.getGeometryContext().getStructureMappings();
// We have to check if all the reactions are able to tranform to stochastic jump processes before generating the math.
String stochChkMsg = simContext.getModel().isValidForStochApp();
if (!(stochChkMsg.equals(""))) {
throw new ModelException("Problem updating math description: " + simContext.getName() + "\n" + stochChkMsg);
}
simContext.checkValidity();
//
if (simContext.getGeometry().getDimension() > 0) {
throw new MappingException("nonspatial stochastic math mapping requires 0-dimensional geometry");
}
//
for (int i = 0; i < structureMappings.length; i++) {
if (structureMappings[i] instanceof MembraneMapping) {
if (((MembraneMapping) structureMappings[i]).getCalculateVoltage()) {
throw new MappingException("electric potential not yet supported for particle models");
}
}
}
//
// fail if any events
//
BioEvent[] bioEvents = simContext.getBioEvents();
if (bioEvents != null && bioEvents.length > 0) {
throw new MappingException("events not yet supported for particle-based models");
}
//
// verify that all structures are mapped to subvolumes and all subvolumes are mapped to a structure
//
Structure[] structures = simContext.getGeometryContext().getModel().getStructures();
for (int i = 0; i < structures.length; i++) {
StructureMapping sm = simContext.getGeometryContext().getStructureMapping(structures[i]);
if (sm == null || (sm instanceof FeatureMapping && ((FeatureMapping) sm).getGeometryClass() == null)) {
throw new MappingException("model structure '" + structures[i].getName() + "' not mapped to a geometry subVolume");
}
if (sm != null && (sm instanceof MembraneMapping) && ((MembraneMapping) sm).getVolumeFractionParameter() != null) {
Expression volFractExp = ((MembraneMapping) sm).getVolumeFractionParameter().getExpression();
try {
if (volFractExp != null) {
double volFract = volFractExp.evaluateConstant();
if (volFract >= 1.0) {
throw new MappingException("model structure '" + (getSimulationContext().getModel().getStructureTopology().getInsideFeature(((MembraneMapping) sm).getMembrane()).getName() + "' has volume fraction >= 1.0"));
}
}
} catch (ExpressionException e) {
e.printStackTrace(System.out);
}
}
}
SubVolume[] subVolumes = simContext.getGeometryContext().getGeometry().getGeometrySpec().getSubVolumes();
for (int i = 0; i < subVolumes.length; i++) {
Structure[] mappedStructures = simContext.getGeometryContext().getStructuresFromGeometryClass(subVolumes[i]);
if (mappedStructures == null || mappedStructures.length == 0) {
throw new MappingException("geometry subVolume '" + subVolumes[i].getName() + "' not mapped from a model structure");
}
}
//
// gather only those reactionSteps that are not "excluded"
//
ReactionSpec[] reactionSpecs = simContext.getReactionContext().getReactionSpecs();
Vector<ReactionStep> rsList = new Vector<ReactionStep>();
for (int i = 0; i < reactionSpecs.length; i++) {
if (!reactionSpecs[i].isExcluded()) {
rsList.add(reactionSpecs[i].getReactionStep());
}
}
//
for (ReactionStep reactionStep : rsList) {
Kinetics.UnresolvedParameter[] unresolvedParameters = reactionStep.getKinetics().getUnresolvedParameters();
if (unresolvedParameters != null && unresolvedParameters.length > 0) {
StringBuffer buffer = new StringBuffer();
for (int j = 0; j < unresolvedParameters.length; j++) {
if (j > 0) {
buffer.append(", ");
}
buffer.append(unresolvedParameters[j].getName());
}
throw new MappingException("In Application '" + simContext.getName() + "', " + reactionStep.getDisplayType() + " '" + reactionStep.getName() + "' contains unresolved identifier(s): " + buffer);
}
}
//
// create new MathDescription (based on simContext's previous MathDescription if possible)
//
MathDescription oldMathDesc = simContext.getMathDescription();
mathDesc = null;
if (oldMathDesc != null) {
if (oldMathDesc.getVersion() != null) {
mathDesc = new MathDescription(oldMathDesc.getVersion());
} else {
mathDesc = new MathDescription(oldMathDesc.getName());
}
} else {
mathDesc = new MathDescription(simContext.getName() + "_generated");
}
//
// temporarily place all variables in a hashtable (before binding) and discarding duplicates
//
VariableHash varHash = new VariableHash();
//
// conversion factors
//
Model model = simContext.getModel();
varHash.addVariable(new Constant(getMathSymbol(model.getKMOLE(), null), getIdentifierSubstitutions(model.getKMOLE().getExpression(), model.getKMOLE().getUnitDefinition(), null)));
varHash.addVariable(new Constant(getMathSymbol(model.getN_PMOLE(), null), getIdentifierSubstitutions(model.getN_PMOLE().getExpression(), model.getN_PMOLE().getUnitDefinition(), null)));
varHash.addVariable(new Constant(getMathSymbol(model.getPI_CONSTANT(), null), getIdentifierSubstitutions(model.getPI_CONSTANT().getExpression(), model.getPI_CONSTANT().getUnitDefinition(), null)));
varHash.addVariable(new Constant(getMathSymbol(model.getFARADAY_CONSTANT(), null), getIdentifierSubstitutions(model.getFARADAY_CONSTANT().getExpression(), model.getFARADAY_CONSTANT().getUnitDefinition(), null)));
varHash.addVariable(new Constant(getMathSymbol(model.getFARADAY_CONSTANT_NMOLE(), null), getIdentifierSubstitutions(model.getFARADAY_CONSTANT_NMOLE().getExpression(), model.getFARADAY_CONSTANT_NMOLE().getUnitDefinition(), null)));
varHash.addVariable(new Constant(getMathSymbol(model.getGAS_CONSTANT(), null), getIdentifierSubstitutions(model.getGAS_CONSTANT().getExpression(), model.getGAS_CONSTANT().getUnitDefinition(), null)));
varHash.addVariable(new Constant(getMathSymbol(model.getTEMPERATURE(), null), getIdentifierSubstitutions(new Expression(simContext.getTemperatureKelvin()), model.getTEMPERATURE().getUnitDefinition(), null)));
Enumeration<SpeciesContextMapping> enum1 = getSpeciesContextMappings();
while (enum1.hasMoreElements()) {
SpeciesContextMapping scm = enum1.nextElement();
if (scm.getVariable() instanceof StochVolVariable) {
varHash.addVariable(scm.getVariable());
}
}
// deals with model parameters
ModelParameter[] modelParameters = simContext.getModel().getModelParameters();
for (int j = 0; j < modelParameters.length; j++) {
Expression expr = getSubstitutedExpr(modelParameters[j].getExpression(), true, false);
expr = getIdentifierSubstitutions(expr, modelParameters[j].getUnitDefinition(), geometryClass);
varHash.addVariable(newFunctionOrConstant(getMathSymbol(modelParameters[j], geometryClass), expr, geometryClass));
}
// added July 2009, ElectricalStimulusParameter electric mapping tab
ElectricalStimulus[] elecStimulus = simContext.getElectricalStimuli();
if (elecStimulus.length > 0) {
throw new MappingException("Modles with electrophysiology are not supported for stochastic applications.");
}
for (int j = 0; j < structureMappings.length; j++) {
if (structureMappings[j] instanceof MembraneMapping) {
MembraneMapping memMapping = (MembraneMapping) structureMappings[j];
Parameter initialVoltageParm = memMapping.getInitialVoltageParameter();
try {
Expression exp = initialVoltageParm.getExpression();
exp.evaluateConstant();
varHash.addVariable(newFunctionOrConstant(getMathSymbol(memMapping.getMembrane().getMembraneVoltage(), memMapping.getGeometryClass()), getIdentifierSubstitutions(memMapping.getInitialVoltageParameter().getExpression(), memMapping.getInitialVoltageParameter().getUnitDefinition(), memMapping.getGeometryClass()), memMapping.getGeometryClass()));
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new MappingException("Membrane initial voltage: " + initialVoltageParm.getName() + " cannot be evaluated as constant.");
}
}
}
//
for (ReactionStep rs : rsList) {
if (rs.getKinetics() instanceof LumpedKinetics) {
throw new RuntimeException("Lumped Kinetics not yet supported for Stochastic Math Generation");
}
Kinetics.KineticsParameter[] parameters = rs.getKinetics().getKineticsParameters();
for (KineticsParameter parameter : parameters) {
//
if ((parameter.getRole() == Kinetics.ROLE_CurrentDensity) && (parameter.getExpression() == null || parameter.getExpression().isZero())) {
continue;
}
//
// don't add rate, we'll do it later when creating the jump processes
//
// if (parameter.getRole() == Kinetics.ROLE_ReactionRate) {
// continue;
// }
//
// don't add mass action reverse parameter if irreversible
//
// if (!rs.isReversible() && parameters[i].getRole() == Kinetics.ROLE_KReverse){
// continue;
// }
Expression expr = getSubstitutedExpr(parameter.getExpression(), true, false);
varHash.addVariable(newFunctionOrConstant(getMathSymbol(parameter, geometryClass), getIdentifierSubstitutions(expr, parameter.getUnitDefinition(), geometryClass), geometryClass));
}
}
// the parameter "Size" is already put into mathsymbolmapping in refreshSpeciesContextMapping()
for (int i = 0; i < structureMappings.length; i++) {
StructureMapping sm = structureMappings[i];
StructureMapping.StructureMappingParameter parm = sm.getParameterFromRole(StructureMapping.ROLE_Size);
if (parm.getExpression() != null) {
try {
double value = parm.getExpression().evaluateConstant();
varHash.addVariable(new Constant(getMathSymbol(parm, sm.getGeometryClass()), new Expression(value)));
} catch (ExpressionException e) {
// varHash.addVariable(new Function(getMathSymbol0(parm,sm),getIdentifierSubstitutions(parm.getExpression(),parm.getUnitDefinition(),sm)));
e.printStackTrace(System.out);
throw new MappingException("Size of structure:" + sm.getNameScope().getName() + " cannot be evaluated as constant.");
}
}
}
SpeciesContextSpec[] speciesContextSpecs = getSimulationContext().getReactionContext().getSpeciesContextSpecs();
addInitialConditions(domain, speciesContextSpecs, varHash);
//
// constant species (either function or constant)
//
enum1 = getSpeciesContextMappings();
while (enum1.hasMoreElements()) {
SpeciesContextMapping scm = (SpeciesContextMapping) enum1.nextElement();
if (scm.getVariable() instanceof Constant) {
varHash.addVariable(scm.getVariable());
}
}
//
if (simContext.getGeometryContext().getGeometry() != null) {
try {
mathDesc.setGeometry(simContext.getGeometryContext().getGeometry());
} catch (java.beans.PropertyVetoException e) {
e.printStackTrace(System.out);
throw new MappingException("failure setting geometry " + e.getMessage());
}
} else {
throw new MappingException("Geometry must be defined in Application " + simContext.getName());
}
//
// create subDomains
//
SubVolume subVolume = simContext.getGeometry().getGeometrySpec().getSubVolumes()[0];
SubDomain subDomain = new CompartmentSubDomain(subVolume.getName(), 0);
mathDesc.addSubDomain(subDomain);
//
// functions: species which is not a variable, but has dependency expression
//
enum1 = getSpeciesContextMappings();
while (enum1.hasMoreElements()) {
SpeciesContextMapping scm = (SpeciesContextMapping) enum1.nextElement();
if (scm.getVariable() == null && scm.getDependencyExpression() != null) {
StructureMapping sm = simContext.getGeometryContext().getStructureMapping(scm.getSpeciesContext().getStructure());
Expression exp = scm.getDependencyExpression();
exp.bindExpression(this);
SpeciesCountParameter spCountParam = getSpeciesCountParameter(scm.getSpeciesContext());
varHash.addVariable(new Function(getMathSymbol(spCountParam, sm.getGeometryClass()), getIdentifierSubstitutions(exp, spCountParam.getUnitDefinition(), sm.getGeometryClass()), domain));
}
}
addJumpProcesses(varHash, geometryClass, subDomain);
//
for (int i = 0; i < fieldMathMappingParameters.length; i++) {
if (fieldMathMappingParameters[i] instanceof UnitFactorParameter) {
varHash.addVariable(newFunctionOrConstant(getMathSymbol(fieldMathMappingParameters[i], geometryClass), getIdentifierSubstitutions(fieldMathMappingParameters[i].getExpression(), fieldMathMappingParameters[i].getUnitDefinition(), geometryClass), fieldMathMappingParameters[i].getGeometryClass()));
}
}
//
// set Variables to MathDescription all at once with the order resolved by "VariableHash"
//
mathDesc.setAllVariables(varHash.getAlphabeticallyOrderedVariables());
//
// set up variable initial conditions in subDomain
//
SpeciesContextSpec[] scSpecs = simContext.getReactionContext().getSpeciesContextSpecs();
for (int i = 0; i < speciesContextSpecs.length; i++) {
// get stochastic variable by name
SpeciesCountParameter spCountParam = getSpeciesCountParameter(speciesContextSpecs[i].getSpeciesContext());
StructureMapping sm = simContext.getGeometryContext().getStructureMapping(speciesContextSpecs[i].getSpeciesContext().getStructure());
String varName = getMathSymbol(spCountParam, sm.getGeometryClass());
StochVolVariable var = (StochVolVariable) mathDesc.getVariable(varName);
// stochastic use initial number of particles
SpeciesContextSpec.SpeciesContextSpecParameter initParm = scSpecs[i].getInitialCountParameter();
// stochastic variables initial expression.
if (initParm != null) {
VarIniCondition varIni = null;
if (!scSpecs[i].isConstant() && getSimulationContext().isRandomizeInitCondition()) {
varIni = new VarIniPoissonExpectedCount(var, new Expression(getMathSymbol(initParm, sm.getGeometryClass())));
} else {
varIni = new VarIniCount(var, new Expression(getMathSymbol(initParm, sm.getGeometryClass())));
}
subDomain.addVarIniCondition(varIni);
}
}
//
for (int i = 0; i < fieldMathMappingParameters.length; i++) {
if (fieldMathMappingParameters[i] instanceof UnitFactorParameter) {
Variable variable = newFunctionOrConstant(getMathSymbol(fieldMathMappingParameters[i], geometryClass), getIdentifierSubstitutions(fieldMathMappingParameters[i].getExpression(), fieldMathMappingParameters[i].getUnitDefinition(), geometryClass), fieldMathMappingParameters[i].getGeometryClass());
if (mathDesc.getVariable(variable.getName()) == null) {
mathDesc.addVariable(variable);
}
}
if (fieldMathMappingParameters[i] instanceof ObservableCountParameter) {
Variable variable = newFunctionOrConstant(getMathSymbol(fieldMathMappingParameters[i], geometryClass), getIdentifierSubstitutions(fieldMathMappingParameters[i].getExpression(), fieldMathMappingParameters[i].getUnitDefinition(), geometryClass), fieldMathMappingParameters[i].getGeometryClass());
if (mathDesc.getVariable(variable.getName()) == null) {
mathDesc.addVariable(variable);
}
}
}
if (!mathDesc.isValid()) {
System.out.println(mathDesc.getVCML_database());
throw new MappingException("generated an invalid mathDescription: " + mathDesc.getWarning());
}
}
Aggregations