Search in sources :

Example 71 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class SimulationContext method getUnitInfo.

/**
 * SimulationContext constructor.
 * This constructor differs with the previous one with one more boolean input parameter, which specifies whether
 * the new application is a stochastic application or not.
 */
// @Deprecated
// public SimulationContext(Model model, Geometry geometry, boolean bStoch, boolean bRuleBased) throws PropertyVetoException {
// this(model,geometry,null,null,legacyType(bStoch, bRuleBased));
// }
@Override
public UnitInfo getUnitInfo() throws UnsupportedOperationException {
    if (unitInfo != null) {
        return unitInfo;
    }
    if (bioModel == null) {
        throw new UnsupportedOperationException("bioModel not set yet");
    }
    Model m = bioModel.getModel();
    ModelUnitSystem unitSys = m.getUnitSystem();
    VCUnitDefinition tu = unitSys.getTimeUnit();
    String sym = tu.getSymbol();
    unitInfo = new UnitInfo(sym);
    return unitInfo;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Model(cbit.vcell.model.Model) BioModel(cbit.vcell.biomodel.BioModel) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 72 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class SpeciesContextSpec method convertConcentrationToParticles.

public Expression convertConcentrationToParticles(Expression iniConcentration) throws ExpressionException, MappingException {
    Expression iniParticlesExpr = null;
    Structure structure = getSpeciesContext().getStructure();
    double structSize = computeStructureSize();
    if (structure instanceof Membrane) {
        // particles = iniConcentration(molecules/um2)*size(um2)
        try {
            iniParticlesExpr = new Expression((Math.round(iniConcentration.evaluateConstant() * structSize)));
        } catch (ExpressionException e) {
            iniParticlesExpr = Expression.mult(iniConcentration, new Expression(structSize)).flatten();
        }
    } else {
        // convert concentration(particles/volume) to number of particles
        // particles = [iniConcentration(uM)*size(um3)]/KMOLE
        // @Note : 'kMole' variable here is used only as a var name, it does not represent the previously known ReservedSymbol KMOLE.
        ModelUnitSystem modelUnitSystem = getSimulationContext().getModel().getUnitSystem();
        VCUnitDefinition volSubstanceToStochastic = modelUnitSystem.getStochasticSubstanceUnit().divideBy(modelUnitSystem.getVolumeSubstanceUnit());
        double volSubstanceToStochasticScale = volSubstanceToStochastic.getDimensionlessScale().doubleValue();
        try {
            iniParticlesExpr = new Expression((Math.round(iniConcentration.evaluateConstant() * structSize * volSubstanceToStochasticScale)));
        } catch (ExpressionException e) {
            Expression numeratorExpr = Expression.mult(iniConcentration, new Expression(structSize));
            Expression exp = new Expression(volSubstanceToStochasticScale);
            iniParticlesExpr = Expression.mult(numeratorExpr, exp).flatten();
        }
    }
    return iniParticlesExpr;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) ExpressionException(cbit.vcell.parser.ExpressionException) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 73 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class StochMathMapping method getProbabilityRate.

/**
 * Get probability expression for the specific elementary reaction.
 * Input: ReactionStep, the reaction. isForwardDirection, if the elementary reaction is forward from the reactionstep.
 * Output: Expression. the probability expression.
 * Creation date: (9/14/2006 3:22:58 PM)
 * @throws ExpressionException
 */
private Expression getProbabilityRate(ReactionStep reactionStep, Expression rateConstantExpr, boolean isForwardDirection) throws MappingException, ExpressionException, ModelException {
    // the structure where reaction happens
    StructureMapping sm = getSimulationContext().getGeometryContext().getStructureMapping(reactionStep.getStructure());
    Model model = getSimulationContext().getModel();
    Expression reactionStructureSize = new Expression(sm.getStructure().getStructureSize(), getNameScope());
    VCUnitDefinition reactionSubstanceUnit = model.getUnitSystem().getSubstanceUnit(reactionStep.getStructure());
    VCUnitDefinition stochasticSubstanceUnit = model.getUnitSystem().getStochasticSubstanceUnit();
    Expression reactionSubstanceUnitFactor = getUnitFactor(stochasticSubstanceUnit.divideBy(reactionSubstanceUnit));
    Expression factorExpr = Expression.mult(reactionStructureSize, reactionSubstanceUnitFactor);
    // Using the MassActionFunction to write out the math description
    MassActionSolver.MassActionFunction maFunc = null;
    Kinetics kinetics = reactionStep.getKinetics();
    if (kinetics.getKineticsDescription().equals(KineticsDescription.MassAction) || kinetics.getKineticsDescription().equals(KineticsDescription.General) || kinetics.getKineticsDescription().equals(KineticsDescription.GeneralPermeability)) {
        Expression rateExp = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate).getExpression();
        Parameter forwardRateParameter = null;
        Parameter reverseRateParameter = null;
        if (kinetics.getKineticsDescription().equals(KineticsDescription.MassAction)) {
            forwardRateParameter = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_KForward);
            reverseRateParameter = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_KReverse);
        } else if (kinetics.getKineticsDescription().equals(KineticsDescription.GeneralPermeability)) {
            forwardRateParameter = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_Permeability);
            reverseRateParameter = kinetics.getKineticsParameterFromRole(Kinetics.ROLE_Permeability);
        }
        maFunc = MassActionSolver.solveMassAction(forwardRateParameter, reverseRateParameter, rateExp, reactionStep);
        if (maFunc.getForwardRate() == null && maFunc.getReverseRate() == null) {
            throw new MappingException("Cannot generate stochastic math mapping for the reaction:" + reactionStep.getName() + "\nLooking for the rate function according to the form of k1*Reactant1^Stoir1*Reactant2^Stoir2...-k2*Product1^Stoip1*Product2^Stoip2.");
        }
    } else {
        throw new MappingException("Cannot generate stochastic math mapping for the reaction:" + reactionStep.getName() + "\n. Unsupported kinetic type " + kinetics.getKineticsDescription().getName());
    }
    List<ReactionParticipant> reacPart;
    if (isForwardDirection) {
        reacPart = maFunc.getReactants();
        System.out.println("forward reaction rate (coefficient?) is " + maFunc.getForwardRate().infix());
    } else {
        reacPart = maFunc.getProducts();
        System.out.println("reverse reaction rate (coefficient?) is " + maFunc.getReverseRate().infix());
    }
    Expression rxnProbabilityExpr = null;
    for (int i = 0; i < reacPart.size(); i++) {
        VCUnitDefinition speciesSubstanceUnit = model.getUnitSystem().getSubstanceUnit(reacPart.get(i).getStructure());
        Expression speciesUnitFactor = getUnitFactor(speciesSubstanceUnit.divideBy(stochasticSubstanceUnit));
        int stoichiometry = 0;
        stoichiometry = reacPart.get(i).getStoichiometry();
        // ******the following part is to form the s*(s-1)(s-2)..(s-stoi+1).portion of the probability rate.
        Expression speciesStructureSize = new Expression(reacPart.get(i).getStructure().getStructureSize(), getNameScope());
        Expression speciesFactor = Expression.div(speciesUnitFactor, speciesStructureSize);
        // s*(s-1)(s-2)..(s-stoi+1)
        SpeciesCountParameter spCountParam = getSpeciesCountParameter(reacPart.get(i).getSpeciesContext());
        Expression spCount_exp = new Expression(spCountParam, getNameScope());
        // species from uM to No. of Particles, form s*(s-1)*(s-2)
        Expression speciesFactorial = new Expression(spCount_exp);
        for (int j = 1; j < stoichiometry; j++) {
            speciesFactorial = Expression.mult(speciesFactorial, Expression.add(spCount_exp, new Expression(-j)));
        }
        // update total factor with speceies factor
        if (stoichiometry == 1) {
            factorExpr = Expression.mult(factorExpr, speciesFactor);
        } else if (stoichiometry > 1) {
            // rxnProbExpr * (structSize^stoichiometry)
            factorExpr = Expression.mult(factorExpr, Expression.power(speciesFactor, new Expression(stoichiometry)));
        }
        if (rxnProbabilityExpr == null) {
            rxnProbabilityExpr = new Expression(speciesFactorial);
        } else {
            // for more than one reactant
            rxnProbabilityExpr = Expression.mult(rxnProbabilityExpr, speciesFactorial);
        }
    }
    // Now construct the probability expression.
    Expression probExp = null;
    if (rateConstantExpr == null) {
        throw new MappingException("Can not find reaction rate constant in reaction: " + reactionStep.getName());
    } else if (rxnProbabilityExpr == null) {
        probExp = new Expression(rateConstantExpr);
    } else if ((rateConstantExpr != null) && (rxnProbabilityExpr != null)) {
        probExp = Expression.mult(rateConstantExpr, rxnProbabilityExpr);
    }
    // simplify the factor
    RationalExp factorRatExp = RationalExpUtils.getRationalExp(factorExpr);
    factorExpr = new Expression(factorRatExp.infixString());
    factorExpr.bindExpression(this);
    // get probability rate with converting factor
    probExp = Expression.mult(probExp, factorExpr);
    probExp = probExp.flatten();
    return probExp;
}
Also used : RationalExp(cbit.vcell.matrix.RationalExp) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) 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) MassActionSolver(cbit.vcell.model.MassActionSolver) Kinetics(cbit.vcell.model.Kinetics) LumpedKinetics(cbit.vcell.model.LumpedKinetics) ReactionParticipant(cbit.vcell.model.ReactionParticipant)

Example 74 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class CurrentClampElectricalDevice method initializeParameters.

private void initializeParameters() throws ExpressionException {
    ElectricalDevice.ElectricalDeviceParameter[] parameters = new ElectricalDevice.ElectricalDeviceParameter[3];
    // 
    // set the transmembrane current (total current, if necessary derive it from the current density).
    // 
    ElectricalDeviceParameter transMembraneCurrent = null;
    ModelUnitSystem modelUnitSystem = mathMapping.getSimulationContext().getModel().getUnitSystem();
    VCUnitDefinition currentUnit = modelUnitSystem.getCurrentUnit();
    if (currentClampStimulus instanceof TotalCurrentClampStimulus) {
        TotalCurrentClampStimulus stimulus = (TotalCurrentClampStimulus) currentClampStimulus;
        LocalParameter currentParameter = stimulus.getCurrentParameter();
        transMembraneCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TransmembraneCurrent], new Expression(currentParameter.getExpression()), ROLE_TransmembraneCurrent, currentUnit);
    } else if (currentClampStimulus instanceof CurrentDensityClampStimulus) {
        CurrentDensityClampStimulus stimulus = (CurrentDensityClampStimulus) currentClampStimulus;
        LocalParameter currentDensityParameter = stimulus.getCurrentDensityParameter();
        // 
        // here we have to determine the expression for current (from current density).
        // 
        Feature feature1 = currentClampStimulus.getElectrode().getFeature();
        Feature feature2 = mathMapping.getSimulationContext().getGroundElectrode().getFeature();
        Membrane membrane = null;
        StructureTopology structTopology = mathMapping.getSimulationContext().getModel().getStructureTopology();
        if (structTopology.getParentStructure(feature1) != null && structTopology.getOutsideFeature((Membrane) structTopology.getParentStructure(feature1)) == feature2) {
            membrane = ((Membrane) structTopology.getParentStructure(feature1));
        } else if (structTopology.getParentStructure(feature2) != null && structTopology.getOutsideFeature((Membrane) structTopology.getParentStructure(feature2)) == feature1) {
            membrane = ((Membrane) structTopology.getParentStructure(feature2));
        }
        if (membrane == null) {
            throw new RuntimeException("current clamp based on current density crosses multiple membranes, unable to " + "determine single membrane to convert current density into current in Application '" + mathMapping.getSimulationContext().getName() + "'.");
        }
        MembraneMapping membraneMapping = (MembraneMapping) mathMapping.getSimulationContext().getGeometryContext().getStructureMapping(membrane);
        StructureMappingParameter sizeParameter = membraneMapping.getSizeParameter();
        Expression area = null;
        if (mathMapping.getSimulationContext().getGeometry().getDimension() == 0 && (sizeParameter.getExpression() == null || sizeParameter.getExpression().isZero())) {
            area = membraneMapping.getNullSizeParameterValue();
        } else {
            area = new Expression(sizeParameter, mathMapping.getNameScope());
        }
        transMembraneCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TransmembraneCurrent], Expression.mult(new Expression(currentDensityParameter.getExpression()), area), ROLE_TransmembraneCurrent, currentUnit);
    } else {
        throw new RuntimeException("unexpected current clamp stimulus type : " + currentClampStimulus.getClass().getName());
    }
    ElectricalDeviceParameter totalCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TotalCurrent], new Expression(transMembraneCurrent, getNameScope()), ROLE_TotalCurrent, currentUnit);
    ElectricalDeviceParameter voltage = new ElectricalDeviceParameter(DefaultNames[ROLE_Voltage], null, ROLE_Voltage, modelUnitSystem.getVoltageUnit());
    parameters[0] = totalCurrent;
    parameters[1] = transMembraneCurrent;
    parameters[2] = voltage;
    // 
    // add any user-defined parameters
    // 
    LocalParameter[] stimulusParameters = currentClampStimulus.getLocalParameters();
    for (int i = 0; stimulusParameters != null && i < stimulusParameters.length; i++) {
        if (stimulusParameters[i].getRole() == ElectricalStimulus.ElectricalStimulusParameterType.UserDefined) {
            ElectricalDeviceParameter newParam = new ElectricalDeviceParameter(stimulusParameters[i].getName(), new Expression(stimulusParameters[i].getExpression()), ROLE_UserDefined, stimulusParameters[i].getUnitDefinition());
            parameters = (ElectricalDeviceParameter[]) BeanUtils.addElement(parameters, newParam);
        }
    }
    setParameters(parameters);
}
Also used : MembraneMapping(cbit.vcell.mapping.MembraneMapping) StructureTopology(cbit.vcell.model.Model.StructureTopology) CurrentDensityClampStimulus(cbit.vcell.mapping.CurrentDensityClampStimulus) StructureMappingParameter(cbit.vcell.mapping.StructureMapping.StructureMappingParameter) TotalCurrentClampStimulus(cbit.vcell.mapping.TotalCurrentClampStimulus) Feature(cbit.vcell.model.Feature) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Membrane(cbit.vcell.model.Membrane) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 75 with VCUnitDefinition

use of cbit.vcell.units.VCUnitDefinition in project vcell by virtualcell.

the class PotentialMapping method getOdeRHS.

/**
 * Insert the method's description here.
 * Creation date: (4/24/2002 10:45:35 AM)
 * @return cbit.vcell.parser.Expression
 * @param capacitiveDevice cbit.vcell.mapping.potential.ElectricalDevice
 */
public Expression getOdeRHS(MembraneElectricalDevice capacitiveDevice, AbstractMathMapping mathMapping) throws ExpressionException, MappingException {
    NameScope nameScope = mathMapping.getNameScope();
    Expression transMembraneCurrent = capacitiveDevice.getParameterFromRole(ElectricalDevice.ROLE_TransmembraneCurrent).getExpression().renameBoundSymbols(nameScope);
    Expression totalCapacitance = new Expression(capacitiveDevice.getCapacitanceParameter(), nameScope);
    Expression totalCurrent = new Expression(capacitiveDevice.getTotalCurrentSymbol(), nameScope);
    ModelUnitSystem modelUnitSystem = fieldMathMapping.getSimulationContext().getModel().getUnitSystem();
    VCUnitDefinition potentialUnit = modelUnitSystem.getVoltageUnit();
    VCUnitDefinition timeUnit = modelUnitSystem.getTimeUnit();
    SymbolTableEntry capacitanceParameter = capacitiveDevice.getCapacitanceParameter();
    VCUnitDefinition unitFactor = potentialUnit.divideBy(timeUnit).multiplyBy(capacitanceParameter.getUnitDefinition()).divideBy(capacitiveDevice.getTotalCurrentSymbol().getUnitDefinition());
    Expression unitFactorExp = fieldMathMapping.getUnitFactor(unitFactor);
    Expression exp = Expression.mult(Expression.div(unitFactorExp, totalCapacitance), Expression.add(totalCurrent, Expression.negate(transMembraneCurrent)));
    // exp.bindExpression(mathMapping);
    return exp;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) NameScope(cbit.vcell.parser.NameScope) Expression(cbit.vcell.parser.Expression) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Aggregations

VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)113 Expression (cbit.vcell.parser.Expression)73 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)36 ExpressionException (cbit.vcell.parser.ExpressionException)26 PropertyVetoException (java.beans.PropertyVetoException)24 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)21 Element (org.jdom.Element)20 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)19 ArrayList (java.util.ArrayList)17 ModelParameter (cbit.vcell.model.Model.ModelParameter)16 Model (cbit.vcell.model.Model)14 Parameter (cbit.vcell.model.Parameter)14 SpeciesContext (cbit.vcell.model.SpeciesContext)13 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)13 Membrane (cbit.vcell.model.Membrane)12 Structure (cbit.vcell.model.Structure)12 StructureMapping (cbit.vcell.mapping.StructureMapping)11 MathException (cbit.vcell.math.MathException)10 ReactionStep (cbit.vcell.model.ReactionStep)10 Feature (cbit.vcell.model.Feature)9