Search in sources :

Example 6 with KineticsParameter

use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.

the class SBPAXKineticsExtractor method setKForwardParam.

private static void setKForwardParam(ReactionStep reaction, SBOParam kForward, Kinetics kinetics) throws ExpressionException, PropertyVetoException {
    if (kForward != null) {
        KineticsParameter fwdParam = ((MassActionKinetics) kinetics).getForwardRateParameter();
        kinetics.setParameterValue(fwdParam, new Expression(kForward.getNumber()));
        String unitF = SBOParam.formatUnit(kForward.getUnit());
        VCUnitDefinition fwdUnit = reaction.getModel().getUnitSystem().getInstance(unitF);
        fwdParam.setUnitDefinition(fwdUnit);
    }
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) Expression(cbit.vcell.parser.Expression) MassActionKinetics(cbit.vcell.model.MassActionKinetics)

Example 7 with KineticsParameter

use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.

the class SBPAXKineticsExtractor method setKReverseParam.

private static void setKReverseParam(ReactionStep reaction, SBOParam kReverse, Kinetics kinetics) throws ExpressionException, PropertyVetoException {
    if (kReverse != null) {
        KineticsParameter revParam = ((MassActionKinetics) kinetics).getReverseRateParameter();
        kinetics.setParameterValue(revParam, new Expression(kReverse.getNumber()));
        String unitR = SBOParam.formatUnit(kReverse.getUnit());
        VCUnitDefinition revUnit = reaction.getModel().getUnitSystem().getInstance(unitR);
        revParam.setUnitDefinition(revUnit);
    }
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) Expression(cbit.vcell.parser.Expression) MassActionKinetics(cbit.vcell.model.MassActionKinetics)

Example 8 with KineticsParameter

use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.

the class BioModelParametersTableModel method propertyChange.

@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
    super.propertyChange(evt);
    if (evt.getSource() instanceof EditableSymbolTableEntry) {
        int changeRow = getRowIndex((EditableSymbolTableEntry) evt.getSource());
        if (changeRow >= 0) {
            fireTableRowsUpdated(changeRow, changeRow);
        }
    } else {
        String propertyName = evt.getPropertyName();
        if (evt.getSource() == bioModel.getModel()) {
            if (propertyName.equals(Model.PROPERTY_NAME_MODEL_PARAMETERS)) {
                ModelParameter[] oldValue = (ModelParameter[]) evt.getOldValue();
                if (oldValue != null) {
                    for (EditableSymbolTableEntry parameter : oldValue) {
                        parameter.removePropertyChangeListener(this);
                    }
                }
                ModelParameter[] newValue = (ModelParameter[]) evt.getNewValue();
                if (newValue != null) {
                    for (EditableSymbolTableEntry parameter : newValue) {
                        parameter.addPropertyChangeListener(this);
                    }
                }
                refreshData();
            } else if (propertyName.equals(Model.PROPERTY_NAME_SPECIES_CONTEXTS)) {
                SpeciesContext[] oldValue = (SpeciesContext[]) evt.getOldValue();
                if (oldValue != null) {
                    for (SpeciesContext sc : oldValue) {
                        sc.removePropertyChangeListener(this);
                    }
                }
                SpeciesContext[] newValue = (SpeciesContext[]) evt.getNewValue();
                if (newValue != null) {
                    for (SpeciesContext sc : newValue) {
                        sc.addPropertyChangeListener(this);
                    }
                }
                refreshData();
            } else if (propertyName.equals(Model.PROPERTY_NAME_REACTION_STEPS)) {
                ReactionStep[] oldValue = (ReactionStep[]) evt.getOldValue();
                if (oldValue != null) {
                    for (ReactionStep reactionStep : oldValue) {
                        reactionStep.removePropertyChangeListener(this);
                        reactionStep.getKinetics().removePropertyChangeListener(this);
                        for (KineticsParameter kineticsEditableSymbolTableEntry : reactionStep.getKinetics().getKineticsParameters()) {
                            kineticsEditableSymbolTableEntry.removePropertyChangeListener(this);
                        }
                        for (ProxyParameter proxyEditableSymbolTableEntry : reactionStep.getKinetics().getProxyParameters()) {
                            proxyEditableSymbolTableEntry.removePropertyChangeListener(this);
                        }
                        for (UnresolvedParameter unresolvedEditableSymbolTableEntry : reactionStep.getKinetics().getUnresolvedParameters()) {
                            unresolvedEditableSymbolTableEntry.removePropertyChangeListener(this);
                        }
                    }
                }
                ReactionStep[] newValue = (ReactionStep[]) evt.getNewValue();
                if (newValue != null) {
                    for (ReactionStep reactionStep : newValue) {
                        reactionStep.addPropertyChangeListener(this);
                        reactionStep.getKinetics().addPropertyChangeListener(this);
                        for (KineticsParameter kineticsEditableSymbolTableEntry : reactionStep.getKinetics().getKineticsParameters()) {
                            kineticsEditableSymbolTableEntry.addPropertyChangeListener(this);
                        }
                        for (ProxyParameter proxyEditableSymbolTableEntry : reactionStep.getKinetics().getProxyParameters()) {
                            proxyEditableSymbolTableEntry.addPropertyChangeListener(this);
                        }
                        for (UnresolvedParameter unresolvedEditableSymbolTableEntry : reactionStep.getKinetics().getUnresolvedParameters()) {
                            unresolvedEditableSymbolTableEntry.addPropertyChangeListener(this);
                        }
                    }
                }
                refreshData();
            } else if (evt.getPropertyName().equals(RbmModelContainer.PROPERTY_NAME_REACTION_RULE_LIST)) {
                List<ReactionRule> oldValue = (List<ReactionRule>) evt.getOldValue();
                if (oldValue != null) {
                    for (ReactionRule rs : oldValue) {
                        rs.removePropertyChangeListener(this);
                    }
                }
                List<ReactionRule> newValue = (List<ReactionRule>) evt.getNewValue();
                if (newValue != null) {
                    for (ReactionRule rs : newValue) {
                        rs.addPropertyChangeListener(this);
                    }
                }
                refreshData();
            }
        } else if (evt.getSource() == bioModel) {
            if (propertyName.equals(BioModel.PROPERTY_NAME_SIMULATION_CONTEXTS)) {
                SimulationContext[] oldValue = (SimulationContext[]) evt.getOldValue();
                for (SimulationContext simulationContext : oldValue) {
                    simulationContext.removePropertyChangeListener(this);
                    simulationContext.getGeometryContext().removePropertyChangeListener(this);
                    for (StructureMapping mapping : simulationContext.getGeometryContext().getStructureMappings()) {
                        mapping.removePropertyChangeListener(this);
                        for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
                            parameter.removePropertyChangeListener(this);
                        }
                    }
                    simulationContext.getReactionContext().removePropertyChangeListener(this);
                    for (SpeciesContextSpec spec : simulationContext.getReactionContext().getSpeciesContextSpecs()) {
                        spec.removePropertyChangeListener(this);
                        for (EditableSymbolTableEntry parameter : spec.getParameters()) {
                            parameter.removePropertyChangeListener(this);
                        }
                    }
                    for (ElectricalStimulus elect : simulationContext.getElectricalStimuli()) {
                        elect.removePropertyChangeListener(this);
                        for (EditableSymbolTableEntry parameter : elect.getParameters()) {
                            parameter.removePropertyChangeListener(this);
                        }
                    }
                    for (SpatialObject spatialObject : simulationContext.getSpatialObjects()) {
                        spatialObject.removePropertyChangeListener(this);
                    }
                    for (SpatialProcess spatialProcess : simulationContext.getSpatialProcesses()) {
                        spatialProcess.removePropertyChangeListener(this);
                        for (LocalParameter p : spatialProcess.getParameters()) {
                            p.removePropertyChangeListener(this);
                        }
                    }
                    for (SimulationContextParameter p : simulationContext.getSimulationContextParameters()) {
                        p.removePropertyChangeListener(this);
                    }
                }
                SimulationContext[] newValue = (SimulationContext[]) evt.getNewValue();
                for (SimulationContext simulationContext : newValue) {
                    simulationContext.addPropertyChangeListener(this);
                    simulationContext.getGeometryContext().addPropertyChangeListener(this);
                    for (StructureMapping mapping : simulationContext.getGeometryContext().getStructureMappings()) {
                        mapping.addPropertyChangeListener(this);
                        for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
                            parameter.addPropertyChangeListener(this);
                        }
                    }
                    simulationContext.getReactionContext().addPropertyChangeListener(this);
                    for (SpeciesContextSpec spec : simulationContext.getReactionContext().getSpeciesContextSpecs()) {
                        spec.addPropertyChangeListener(this);
                        for (EditableSymbolTableEntry parameter : spec.getParameters()) {
                            parameter.addPropertyChangeListener(this);
                        }
                    }
                    for (ElectricalStimulus elect : simulationContext.getElectricalStimuli()) {
                        elect.addPropertyChangeListener(this);
                        for (EditableSymbolTableEntry parameter : elect.getParameters()) {
                            parameter.addPropertyChangeListener(this);
                        }
                    }
                    for (SpatialObject spatialObject : simulationContext.getSpatialObjects()) {
                        spatialObject.addPropertyChangeListener(this);
                    }
                    for (SpatialProcess spatialProcess : simulationContext.getSpatialProcesses()) {
                        spatialProcess.addPropertyChangeListener(this);
                        for (LocalParameter p : spatialProcess.getParameters()) {
                            p.addPropertyChangeListener(this);
                        }
                    }
                    for (SimulationContextParameter p : simulationContext.getSimulationContextParameters()) {
                        p.addPropertyChangeListener(this);
                    }
                }
                refreshData();
            }
        } else if (evt.getSource() instanceof GeometryContext && evt.getPropertyName().equals(GeometryContext.PROPERTY_STRUCTURE_MAPPINGS)) {
            StructureMapping[] oldValue = (StructureMapping[]) evt.getOldValue();
            if (oldValue != null) {
                for (StructureMapping mapping : oldValue) {
                    mapping.removePropertyChangeListener(this);
                    for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
                        parameter.removePropertyChangeListener(this);
                    }
                }
            }
            StructureMapping[] newValue = (StructureMapping[]) evt.getNewValue();
            if (newValue != null) {
                for (StructureMapping mapping : newValue) {
                    mapping.addPropertyChangeListener(this);
                    for (EditableSymbolTableEntry parameter : mapping.getParameters()) {
                        parameter.addPropertyChangeListener(this);
                    }
                }
            }
            refreshData();
        } else if (evt.getSource() instanceof ReactionStep && (evt.getPropertyName().equals(ReactionStep.PROPERTY_NAME_KINETICS))) {
            Kinetics oldValue = (Kinetics) evt.getOldValue();
            if (oldValue != null) {
                oldValue.removePropertyChangeListener(this);
                for (KineticsParameter kineticsEditableSymbolTableEntry : oldValue.getKineticsParameters()) {
                    kineticsEditableSymbolTableEntry.removePropertyChangeListener(this);
                }
                for (ProxyParameter proxyEditableSymbolTableEntry : oldValue.getProxyParameters()) {
                    proxyEditableSymbolTableEntry.removePropertyChangeListener(this);
                }
                for (UnresolvedParameter unresolvedEditableSymbolTableEntry : oldValue.getUnresolvedParameters()) {
                    unresolvedEditableSymbolTableEntry.removePropertyChangeListener(this);
                }
            }
            Kinetics newValue = (Kinetics) evt.getNewValue();
            if (newValue != null) {
                newValue.addPropertyChangeListener(this);
                for (KineticsParameter kineticsEditableSymbolTableEntry : newValue.getKineticsParameters()) {
                    kineticsEditableSymbolTableEntry.addPropertyChangeListener(this);
                }
                for (ProxyParameter proxyEditableSymbolTableEntry : newValue.getProxyParameters()) {
                    proxyEditableSymbolTableEntry.addPropertyChangeListener(this);
                }
                for (UnresolvedParameter unresolvedEditableSymbolTableEntry : newValue.getUnresolvedParameters()) {
                    unresolvedEditableSymbolTableEntry.addPropertyChangeListener(this);
                }
            }
            refreshData();
        } else if (evt.getSource() instanceof SimulationContext && evt.getPropertyName().equals(SimulationContext.PROPERTY_NAME_SPATIALPROCESSES)) {
            SpatialProcess[] oldValue = (SpatialProcess[]) evt.getOldValue();
            if (oldValue != null) {
                for (SpatialProcess process : oldValue) {
                    process.removePropertyChangeListener(this);
                    for (EditableSymbolTableEntry parameter : process.getParameters()) {
                        parameter.removePropertyChangeListener(this);
                    }
                }
            }
            SpatialProcess[] newValue = (SpatialProcess[]) evt.getNewValue();
            if (newValue != null) {
                for (SpatialProcess process : newValue) {
                    process.addPropertyChangeListener(this);
                    for (EditableSymbolTableEntry parameter : process.getParameters()) {
                        parameter.addPropertyChangeListener(this);
                    }
                }
            }
            refreshData();
        } else if (evt.getSource() instanceof SimulationContext && evt.getPropertyName().equals(SimulationContext.PROPERTY_NAME_SPATIALOBJECTS)) {
            SpatialObject[] oldValue = (SpatialObject[]) evt.getOldValue();
            if (oldValue != null) {
                for (SpatialObject spatialObject : oldValue) {
                    spatialObject.removePropertyChangeListener(this);
                }
            }
            SpatialObject[] newValue = (SpatialObject[]) evt.getNewValue();
            if (newValue != null) {
                for (SpatialObject spatialObject : newValue) {
                    spatialObject.addPropertyChangeListener(this);
                }
            }
            refreshData();
        } else if (evt.getSource() instanceof SpatialObject && evt.getPropertyName().equals(SpatialObject.PROPERTY_NAME_QUANTITYCATEGORIESENABLED)) {
            refreshData();
        } else if (evt.getSource() instanceof SimulationContext && evt.getPropertyName().equals(SimulationContext.PROPERTY_NAME_SIMULATIONCONTEXTPARAMETERS)) {
            SimulationContextParameter[] oldValue = (SimulationContextParameter[]) evt.getOldValue();
            if (oldValue != null) {
                for (SimulationContextParameter param : oldValue) {
                    param.removePropertyChangeListener(this);
                }
            }
            SimulationContextParameter[] newValue = (SimulationContextParameter[]) evt.getNewValue();
            if (newValue != null) {
                for (SimulationContextParameter param : newValue) {
                    param.addPropertyChangeListener(this);
                }
            }
            refreshData();
        } else if (evt.getSource() instanceof Kinetics && (evt.getPropertyName().equals(Kinetics.PROPERTY_NAME_KINETICS_PARAMETERS))) {
            EditableSymbolTableEntry[] oldValue = (EditableSymbolTableEntry[]) evt.getOldValue();
            if (oldValue != null) {
                for (int i = 0; i < oldValue.length; i++) {
                    oldValue[i].removePropertyChangeListener(this);
                }
            }
            EditableSymbolTableEntry[] newValue = (EditableSymbolTableEntry[]) evt.getNewValue();
            if (newValue != null) {
                for (int i = 0; i < newValue.length; i++) {
                    newValue[i].addPropertyChangeListener(this);
                }
            }
            refreshData();
        // } else if(evt.getSource() instanceof ReactionRuleEmbedded) {
        // ReactionRuleEmbedded reactionRule = (ReactionRuleEmbedded) evt.getSource();
        // int changeRow = getRowIndex(reactionRule);
        // if (changeRow >= 0) {
        // fireTableRowsUpdated(changeRow, changeRow);
        // }
        }
    }
}
Also used : UnresolvedParameter(cbit.vcell.model.Kinetics.UnresolvedParameter) SpeciesContext(cbit.vcell.model.SpeciesContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) StructureMapping(cbit.vcell.mapping.StructureMapping) SpatialObject(cbit.vcell.mapping.spatial.SpatialObject) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) SpatialProcess(cbit.vcell.mapping.spatial.processes.SpatialProcess) ArrayList(java.util.ArrayList) List(java.util.List) GeometryContext(cbit.vcell.mapping.GeometryContext) ReactionRule(cbit.vcell.model.ReactionRule) SimulationContext(cbit.vcell.mapping.SimulationContext) SimulationContextParameter(cbit.vcell.mapping.SimulationContext.SimulationContextParameter) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) ModelParameter(cbit.vcell.model.Model.ModelParameter) ElectricalStimulus(cbit.vcell.mapping.ElectricalStimulus) ProxyParameter(cbit.vcell.model.ProxyParameter) ReactionStep(cbit.vcell.model.ReactionStep) Kinetics(cbit.vcell.model.Kinetics) EditableSymbolTableEntry(cbit.vcell.model.EditableSymbolTableEntry)

Example 9 with KineticsParameter

use of cbit.vcell.model.Kinetics.KineticsParameter 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());
    }
}
Also used : VarIniCondition(cbit.vcell.math.VarIniCondition) LumpedKinetics(cbit.vcell.model.LumpedKinetics) MathDescription(cbit.vcell.math.MathDescription) ExpressionException(cbit.vcell.parser.ExpressionException) PropertyVetoException(java.beans.PropertyVetoException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) SubVolume(cbit.vcell.geometry.SubVolume) Vector(java.util.Vector) ModelException(cbit.vcell.model.ModelException) ModelParameter(cbit.vcell.model.Model.ModelParameter) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) ReactionStep(cbit.vcell.model.ReactionStep) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) Domain(cbit.vcell.math.Variable.Domain) GeometryClass(cbit.vcell.geometry.GeometryClass) StochVolVariable(cbit.vcell.math.StochVolVariable) Variable(cbit.vcell.math.Variable) VariableHash(cbit.vcell.math.VariableHash) Constant(cbit.vcell.math.Constant) VarIniPoissonExpectedCount(cbit.vcell.math.VarIniPoissonExpectedCount) Function(cbit.vcell.math.Function) Structure(cbit.vcell.model.Structure) StochVolVariable(cbit.vcell.math.StochVolVariable) VarIniCount(cbit.vcell.math.VarIniCount) Expression(cbit.vcell.parser.Expression) Model(cbit.vcell.model.Model) Parameter(cbit.vcell.model.Parameter) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) ModelParameter(cbit.vcell.model.Model.ModelParameter)

Example 10 with KineticsParameter

use of cbit.vcell.model.Kinetics.KineticsParameter in project vcell by virtualcell.

the class StructureAnalyzer method getCorrectedRateExpression.

public Expression getCorrectedRateExpression(ReactionStep reactionStep, ReactionParticipant reactionParticipant, RateType rateType) throws Exception {
    if (reactionParticipant instanceof Catalyst) {
        throw new Exception("Catalyst " + reactionParticipant + " doesn't have a rate for this reaction");
    // return new Expression(0.0);
    }
    double stoich = reactionStep.getStoichiometry(reactionParticipant.getSpeciesContext());
    if (stoich == 0.0) {
        return new Expression(0.0);
    }
    // 
    // make distributed rate with correct stoichiometry for this participant
    // 
    VCUnitDefinition correctedReactionRateUnit = null;
    Expression distribRate = null;
    if (reactionStep.getKinetics() instanceof DistributedKinetics) {
        DistributedKinetics distributedKinetics = (DistributedKinetics) reactionStep.getKinetics();
        KineticsParameter distribReactionRateParameter = distributedKinetics.getReactionRateParameter();
        distribRate = new Expression(distribReactionRateParameter, mathMapping.getNameScope());
        correctedReactionRateUnit = distribReactionRateParameter.getUnitDefinition();
    } else if (reactionStep.getKinetics() instanceof LumpedKinetics) {
        // 
        // need to put this into concentration/time with respect to structure for reaction.
        // 
        Structure.StructureSize structureSize = reactionStep.getStructure().getStructureSize();
        LumpedKinetics lumpedKinetics = (LumpedKinetics) reactionStep.getKinetics();
        KineticsParameter lumpedReactionRateParameter = lumpedKinetics.getLumpedReactionRateParameter();
        Expression lumpedReactionRateExp = new Expression(lumpedReactionRateParameter, mathMapping.getNameScope());
        distribRate = Expression.div(lumpedReactionRateExp, new Expression(structureSize, mathMapping.getNameScope()));
        ;
        correctedReactionRateUnit = lumpedReactionRateParameter.getUnitDefinition().divideBy(structureSize.getUnitDefinition());
    }
    // correct for stoichiometry
    Expression distribRateWithStoich = distribRate;
    if (stoich != 1) {
        distribRateWithStoich = Expression.mult(new Expression(stoich), distribRateWithStoich);
    }
    // flux correction if reaction and reactionParticipant are in different compartments. (not necessarily dimensionless, use KFlux parameter).
    Expression distribRateWithStoichFlux = distribRateWithStoich;
    if (reactionStep.getStructure() != reactionParticipant.getStructure()) {
        StructureMapping reactionSM = mathMapping.getSimulationContext().getGeometryContext().getStructureMapping(reactionStep.getStructure());
        StructureMapping speciesSM = mathMapping.getSimulationContext().getGeometryContext().getStructureMapping(reactionParticipant.getStructure());
        Parameter fluxCorrectionParameter = mathMapping.getFluxCorrectionParameter(reactionSM, speciesSM);
        Expression fluxCorrection = new Expression(fluxCorrectionParameter, mathMapping.getNameScope());
        distribRateWithStoichFlux = Expression.mult(fluxCorrection, distribRateWithStoichFlux);
        correctedReactionRateUnit = correctedReactionRateUnit.multiplyBy(fluxCorrectionParameter.getUnitDefinition());
    }
    // apply unit factor for difference substance
    ModelUnitSystem unitSystem = mathMapping.getSimulationContext().getModel().getUnitSystem();
    VCUnitDefinition timeUnit = unitSystem.getTimeUnit();
    VCUnitDefinition speciesConcUnit = reactionParticipant.getSpeciesContext().getUnitDefinition();
    VCUnitDefinition speciesConcRateUnit = speciesConcUnit.divideBy(timeUnit);
    Expression unitFactor = null;
    if (rateType == RateType.ConcentrationRate) {
        unitFactor = mathMapping.getUnitFactor(speciesConcRateUnit.divideBy(correctedReactionRateUnit));
    } else if (rateType == RateType.ResolvedFluxRate) {
        unitFactor = mathMapping.getUnitFactor(speciesConcRateUnit.multiplyBy(unitSystem.getLengthUnit()).divideBy(correctedReactionRateUnit));
    }
    return Expression.mult(unitFactor, distribRateWithStoichFlux).flatten();
}
Also used : DistributedKinetics(cbit.vcell.model.DistributedKinetics) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) LumpedKinetics(cbit.vcell.model.LumpedKinetics) Expression(cbit.vcell.parser.Expression) MathMappingParameter(cbit.vcell.mapping.AbstractMathMapping.MathMappingParameter) Parameter(cbit.vcell.model.Parameter) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) Catalyst(cbit.vcell.model.Catalyst) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Aggregations

KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)46 Expression (cbit.vcell.parser.Expression)31 SpeciesContext (cbit.vcell.model.SpeciesContext)23 ModelParameter (cbit.vcell.model.Model.ModelParameter)19 ReactionStep (cbit.vcell.model.ReactionStep)18 Kinetics (cbit.vcell.model.Kinetics)15 ExpressionException (cbit.vcell.parser.ExpressionException)14 Model (cbit.vcell.model.Model)12 PropertyVetoException (java.beans.PropertyVetoException)12 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)11 MassActionKinetics (cbit.vcell.model.MassActionKinetics)10 ReactionParticipant (cbit.vcell.model.ReactionParticipant)9 SimpleReaction (cbit.vcell.model.SimpleReaction)9 Structure (cbit.vcell.model.Structure)9 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)8 SpeciesContextSpecParameter (cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter)6 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)6 ReactionRule (cbit.vcell.model.ReactionRule)6 Species (cbit.vcell.model.Species)6 Vector (java.util.Vector)6