Search in sources :

Example 1 with ParticleObservable

use of cbit.vcell.math.ParticleObservable in project vcell by virtualcell.

the class RulebasedMathMapping method refreshMathDescription.

/**
 * This method was created in VisualAge.
 */
@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 transform 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("rule-based particle math mapping not implemented for spatial geometry - dimension >= 1");
    }
    // 
    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 reactionRules that are not "excluded"
    // 
    ArrayList<ReactionRule> rrList = new ArrayList<ReactionRule>();
    for (ReactionRuleSpec reactionRuleSpec : simContext.getReactionContext().getReactionRuleSpecs()) {
        if (!reactionRuleSpec.isExcluded()) {
            rrList.add(reactionRuleSpec.getReactionRule());
        }
    }
    // 
    for (ReactionRule reactionRule : rrList) {
        UnresolvedParameter[] unresolvedParameters = reactionRule.getKineticLaw().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() + "', " + reactionRule.getDisplayType() + " '" + reactionRule.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 (ReactionRule reactionRule : rrList) {
        // if (reactionRule.getKineticLaw() instanceof LumpedKinetics){
        // throw new RuntimeException("Lumped Kinetics not yet supported for RuleBased Modeling");
        // }
        LocalParameter[] parameters = reactionRule.getKineticLaw().getLocalParameters();
        for (LocalParameter parameter : parameters) {
            // 
            if ((parameter.getRole() == RbmKineticLawParameterType.RuleRate)) {
                continue;
            }
            // 
            if (!reactionRule.isReversible() && parameter.getRole() == RbmKineticLawParameterType.MassActionReverseRate) {
                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);
    // 
    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);
    // 
    // define all molecules and unique species patterns (add molecules to mathDesc and speciesPatterns to varHash).
    // 
    HashMap<SpeciesPattern, VolumeParticleSpeciesPattern> speciesPatternMap = addSpeciesPatterns(domain, rrList);
    HashSet<VolumeParticleSpeciesPattern> uniqueParticleSpeciesPatterns = new HashSet<>(speciesPatternMap.values());
    for (VolumeParticleSpeciesPattern volumeParticleSpeciesPattern : uniqueParticleSpeciesPatterns) {
        varHash.addVariable(volumeParticleSpeciesPattern);
    }
    // 
    // define observables (those explicitly declared and those corresponding to seed species.
    // 
    List<ParticleObservable> observables = addObservables(geometryClass, domain, speciesPatternMap);
    for (ParticleObservable particleObservable : observables) {
        varHash.addVariable(particleObservable);
    }
    try {
        addParticleJumpProcesses(varHash, geometryClass, subDomain, speciesPatternMap);
    } catch (PropertyVetoException e1) {
        e1.printStackTrace();
        throw new MappingException(e1.getMessage(), e1);
    }
    // 
    for (int i = 0; i < fieldMathMappingParameters.length; i++) {
        if (fieldMathMappingParameters[i] instanceof UnitFactorParameter || fieldMathMappingParameters[i] instanceof ObservableConcentrationParameter) {
            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());
    // 
    for (SpeciesContext sc : model.getSpeciesContexts()) {
        if (!sc.hasSpeciesPattern()) {
            throw new MappingException("species " + sc.getName() + " has no molecular pattern");
        }
        VolumeParticleSpeciesPattern volumeParticleSpeciesPattern = speciesPatternMap.get(sc.getSpeciesPattern());
        ArrayList<ParticleInitialCondition> particleInitialConditions = new ArrayList<ParticleProperties.ParticleInitialCondition>();
        // initial conditions from scs
        SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(sc);
        Parameter initialCountParameter = scs.getInitialCountParameter();
        Expression e = getIdentifierSubstitutions(new Expression(initialCountParameter, getNameScope()), initialCountParameter.getUnitDefinition(), geometryClass);
        particleInitialConditions.add(new ParticleInitialConditionCount(e, new Expression(0.0), new Expression(0.0), new Expression(0.0)));
        ParticleProperties particleProperies = new ParticleProperties(volumeParticleSpeciesPattern, new Expression(0.0), new Expression(0.0), new Expression(0.0), new Expression(0.0), particleInitialConditions);
        subDomain.addParticleProperties(particleProperies);
    }
    // 
    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 ObservableConcentrationParameter) {
            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());
    }
}
Also used : MathDescription(cbit.vcell.math.MathDescription) ArrayList(java.util.ArrayList) SpeciesContext(cbit.vcell.model.SpeciesContext) ExpressionException(cbit.vcell.parser.ExpressionException) PropertyVetoException(java.beans.PropertyVetoException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) SubVolume(cbit.vcell.geometry.SubVolume) HashSet(java.util.HashSet) ModelException(cbit.vcell.model.ModelException) VolumeParticleSpeciesPattern(cbit.vcell.math.VolumeParticleSpeciesPattern) PropertyVetoException(java.beans.PropertyVetoException) ModelParameter(cbit.vcell.model.Model.ModelParameter) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) ParticleInitialCondition(cbit.vcell.math.ParticleProperties.ParticleInitialCondition) ParticleProperties(cbit.vcell.math.ParticleProperties) VolumeParticleObservable(cbit.vcell.math.VolumeParticleObservable) ParticleObservable(cbit.vcell.math.ParticleObservable) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) Domain(cbit.vcell.math.Variable.Domain) GeometryClass(cbit.vcell.geometry.GeometryClass) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) Variable(cbit.vcell.math.Variable) VariableHash(cbit.vcell.math.VariableHash) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant) Constant(cbit.vcell.math.Constant) UnresolvedParameter(cbit.vcell.mapping.ParameterContext.UnresolvedParameter) VolumeParticleSpeciesPattern(cbit.vcell.math.VolumeParticleSpeciesPattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern) Structure(cbit.vcell.model.Structure) StochVolVariable(cbit.vcell.math.StochVolVariable) ReactionRule(cbit.vcell.model.ReactionRule) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) Expression(cbit.vcell.parser.Expression) Model(cbit.vcell.model.Model) Parameter(cbit.vcell.model.Parameter) UnresolvedParameter(cbit.vcell.mapping.ParameterContext.UnresolvedParameter) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) ModelParameter(cbit.vcell.model.Model.ModelParameter) ParticleInitialConditionCount(cbit.vcell.math.ParticleProperties.ParticleInitialConditionCount)

Example 2 with ParticleObservable

use of cbit.vcell.math.ParticleObservable in project vcell by virtualcell.

the class NFsimXMLWriter method getListOfObservables.

private static Element getListOfObservables(MathDescription mathDesc) throws SolverException {
    Element listOfObservablesElement = new Element("ListOfObservables");
    int observableIndex = 0;
    Enumeration<Variable> enum1 = mathDesc.getVariables();
    while (enum1.hasMoreElements()) {
        Variable var = enum1.nextElement();
        if (var instanceof ParticleObservable) {
            ParticleObservable particleObservable = (ParticleObservable) var;
            Element observableElement = new Element("Observable");
            String observableId = "O" + (observableIndex + 1);
            observableElement.setAttribute("id", observableId);
            observableElement.setAttribute("name", particleObservable.getName());
            observableElement.setAttribute("type", particleObservable.getType().getText());
            Element listOfPatterns = getParticleSpeciesPatternList(observableId, particleObservable);
            observableElement.addContent(listOfPatterns);
            listOfObservablesElement.addContent(observableElement);
            observableIndex++;
        }
    }
    Format format = Format.getPrettyFormat();
    XMLOutputter outp = new XMLOutputter(format);
    String sOurs = outp.outputString(listOfObservablesElement);
    return listOfObservablesElement;
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) Variable(cbit.vcell.math.Variable) Format(org.jdom.output.Format) Element(org.jdom.Element) ParticleObservable(cbit.vcell.math.ParticleObservable)

Example 3 with ParticleObservable

use of cbit.vcell.math.ParticleObservable in project vcell by virtualcell.

the class NFsimXMLWriter method getListOfFunctions.

private static Element getListOfFunctions(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
    Element listOfParametersElement = new Element("ListOfFunctions");
    for (Variable var : simulationSymbolTable.getVariables()) {
        Double value = null;
        if (var instanceof Constant || var instanceof Function) {
            Expression valExpression = var.getExpression();
            Expression substitutedValExpr = null;
            try {
                substitutedValExpr = simulationSymbolTable.substituteFunctions(valExpression);
            } catch (Exception e) {
                e.printStackTrace(System.out);
                throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
            }
            try {
                value = substitutedValExpr.evaluateConstant();
            } catch (ExpressionException e) {
                System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
            }
            Element functionElement = new Element("Function");
            functionElement.setAttribute("id", var.getName());
            if (value != null) {
                // parameter, see getListOfParameters() above
                continue;
            } else {
                Element listOfReferencesElement = new Element("ListOfReferences");
                String[] references = valExpression.getSymbols();
                for (int i = 0; i < references.length; i++) {
                    String reference = references[i];
                    Element referenceElement = new Element("Reference");
                    referenceElement.setAttribute("name", reference);
                    Variable referenceVariable = simulationSymbolTable.getVariable(reference);
                    Double referenceValue = null;
                    Expression referenceExpression = referenceVariable.getExpression();
                    Expression substitutedReferenceExpression = null;
                    if (referenceExpression != null) {
                        try {
                            substitutedReferenceExpression = simulationSymbolTable.substituteFunctions(referenceExpression);
                        } catch (Exception e) {
                            e.printStackTrace(System.out);
                            throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
                        }
                        try {
                            referenceValue = substitutedReferenceExpression.evaluateConstant();
                        } catch (ExpressionException e) {
                            System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
                        }
                    }
                    if (referenceVariable instanceof ParticleObservable) {
                        referenceElement.setAttribute("type", "Observable");
                    } else if (referenceVariable instanceof Function) {
                        if (referenceValue != null) {
                            referenceElement.setAttribute("type", "ConstantExpression");
                        } else {
                            referenceElement.setAttribute("type", "Function");
                        }
                    } else {
                        // constant
                        referenceElement.setAttribute("type", "ConstantExpression");
                    }
                    listOfReferencesElement.addContent(referenceElement);
                }
                functionElement.addContent(listOfReferencesElement);
                Element expressionElement = new Element("Expression");
                String functionExpression = valExpression.infix();
                expressionElement.setText(functionExpression);
                functionElement.addContent(expressionElement);
            }
            listOfParametersElement.addContent(functionElement);
        }
    }
    return listOfParametersElement;
}
Also used : Variable(cbit.vcell.math.Variable) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant) Constant(cbit.vcell.math.Constant) Element(org.jdom.Element) SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) ExpressionException(cbit.vcell.parser.ExpressionException) Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) SolverException(cbit.vcell.solver.SolverException) ParticleObservable(cbit.vcell.math.ParticleObservable)

Example 4 with ParticleObservable

use of cbit.vcell.math.ParticleObservable 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;
}
Also used : FilamentVariable(cbit.vcell.math.FilamentVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) PointVariable(cbit.vcell.math.PointVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant) Constant(cbit.vcell.math.Constant) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) Element(org.jdom.Element) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) InsideVariable(cbit.vcell.math.InsideVariable) PostProcessingBlock(cbit.vcell.math.PostProcessingBlock) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) Function(cbit.vcell.math.Function) MemVariable(cbit.vcell.math.MemVariable) ParticleMolecularType(cbit.vcell.math.ParticleMolecularType) StochVolVariable(cbit.vcell.math.StochVolVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) VolVariable(cbit.vcell.math.VolVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) FilamentVariable(cbit.vcell.math.FilamentVariable) Event(cbit.vcell.math.Event) BioEvent(cbit.vcell.mapping.BioEvent) PointVariable(cbit.vcell.math.PointVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) VolumeParticleObservable(cbit.vcell.math.VolumeParticleObservable) ParticleObservable(cbit.vcell.math.ParticleObservable)

Example 5 with ParticleObservable

use of cbit.vcell.math.ParticleObservable in project vcell by virtualcell.

the class RulebasedMathMapping method addObservables.

private List<ParticleObservable> addObservables(GeometryClass geometryClass, Domain domain, HashMap<SpeciesPattern, VolumeParticleSpeciesPattern> speciesPatternMap) throws MappingException, MathException {
    ArrayList<ParticleObservable> observables = new ArrayList<>();
    // 
    for (MathMappingParameter mathMappingParameter : getMathMappingParameters()) {
        if (mathMappingParameter instanceof ObservableCountParameter) {
            ObservableCountParameter observableCountParameter = (ObservableCountParameter) mathMappingParameter;
            RbmObservable rbmObservable = observableCountParameter.getObservable();
            ParticleObservable.ObservableType particleObservableType = null;
            if (rbmObservable.getType() == RbmObservable.ObservableType.Molecules) {
                particleObservableType = ParticleObservable.ObservableType.Molecules;
            } else {
                particleObservableType = ParticleObservable.ObservableType.Species;
            }
            ParticleObservable particleObservable = new VolumeParticleObservable(getMathSymbol(observableCountParameter, geometryClass), domain, particleObservableType);
            switch(rbmObservable.getSequence()) {
                case Multimolecular:
                    {
                        particleObservable.setSequence(Sequence.Multimolecular);
                        break;
                    }
                case PolymerLengthEqual:
                    {
                        particleObservable.setSequence(Sequence.PolymerLengthEqual);
                        particleObservable.setQuantity(rbmObservable.getSequenceLength());
                        break;
                    }
                case PolymerLengthGreater:
                    {
                        particleObservable.setSequence(Sequence.PolymerLengthGreater);
                        particleObservable.setQuantity(rbmObservable.getSequenceLength());
                        break;
                    }
                default:
                    {
                        throw new RuntimeException("unexpected sequence " + rbmObservable.getSequence());
                    }
            }
            for (SpeciesPattern speciesPattern : rbmObservable.getSpeciesPatternList()) {
                VolumeParticleSpeciesPattern vpsp = speciesPatternMap.get(speciesPattern);
                particleObservable.addParticleSpeciesPattern(vpsp);
            }
            observables.add(particleObservable);
        }
        if (mathMappingParameter instanceof SpeciesCountParameter) {
            SpeciesCountParameter speciesCountParameter = (SpeciesCountParameter) mathMappingParameter;
            ParticleObservable.ObservableType particleObservableType = ParticleObservable.ObservableType.Species;
            ParticleObservable particleObservable = new VolumeParticleObservable(getMathSymbol(speciesCountParameter, geometryClass), domain, particleObservableType);
            particleObservable.setSequence(Sequence.Multimolecular);
            SpeciesPattern speciesPattern = speciesCountParameter.getSpeciesContext().getSpeciesPattern();
            VolumeParticleSpeciesPattern vpsp = speciesPatternMap.get(speciesPattern);
            particleObservable.addParticleSpeciesPattern(vpsp);
            observables.add(particleObservable);
        }
    }
    return observables;
}
Also used : RbmObservable(cbit.vcell.model.RbmObservable) ArrayList(java.util.ArrayList) VolumeParticleSpeciesPattern(cbit.vcell.math.VolumeParticleSpeciesPattern) VolumeParticleObservable(cbit.vcell.math.VolumeParticleObservable) ParticleObservable(cbit.vcell.math.ParticleObservable) VolumeParticleObservable(cbit.vcell.math.VolumeParticleObservable) VolumeParticleSpeciesPattern(cbit.vcell.math.VolumeParticleSpeciesPattern) SpeciesPattern(org.vcell.model.rbm.SpeciesPattern)

Aggregations

ParticleObservable (cbit.vcell.math.ParticleObservable)5 Variable (cbit.vcell.math.Variable)4 Constant (cbit.vcell.math.Constant)3 MacroscopicRateConstant (cbit.vcell.math.MacroscopicRateConstant)3 VolumeParticleObservable (cbit.vcell.math.VolumeParticleObservable)3 Element (org.jdom.Element)3 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)2 Function (cbit.vcell.math.Function)2 ParticleVariable (cbit.vcell.math.ParticleVariable)2 StochVolVariable (cbit.vcell.math.StochVolVariable)2 SubDomain (cbit.vcell.math.SubDomain)2 VolumeParticleVariable (cbit.vcell.math.VolumeParticleVariable)2 Expression (cbit.vcell.parser.Expression)2 ExpressionException (cbit.vcell.parser.ExpressionException)2 GeometryClass (cbit.vcell.geometry.GeometryClass)1 SubVolume (cbit.vcell.geometry.SubVolume)1 BioEvent (cbit.vcell.mapping.BioEvent)1 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)1 UnresolvedParameter (cbit.vcell.mapping.ParameterContext.UnresolvedParameter)1 Event (cbit.vcell.math.Event)1