Search in sources :

Example 46 with VCUnitDefinition

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

the class Kinetics method writeTokensWithReplacingProxyParams.

/**
 * writeTokensWithReplacingProxyParams : same functionality as writeTokens, but it replaces original proxyParameters with
 * corresponding new proxyparameters.
 *
 * @param pw
 */
public final String writeTokensWithReplacingProxyParams(Hashtable<String, Expression> parmExprHash) {
    // 
    // old format (version 1) (still supported for reading)
    // 
    // Kinetics GeneralCurrentKinetics {
    // Parameter a 10;
    // Parameter b 3;
    // Parameter c d/2;
    // Parameter d 5;
    // CurrentDensity a+b/c;
    // }
    // 
    // 
    // new format (version 2) (deprecated, incompatible with Version 1, still supported for reading)
    // 
    // Kinetics GeneralCurrentKinetics {
    // CurrentDensity 'currentDensity'
    // Parameter currentDensity a+b/c;
    // Parameter a 10;
    // Parameter b 3;
    // Parameter c d/2;
    // Parameter d 5;
    // }
    // 
    // latest format (version 3), which is backward compatable with version 1
    // ParameterVCMLTokens (such as "CurrentDensity") have simple expressions
    // that always consist of only the requiredIdentifier (e.g.  currentDensity; )
    // 
    // Kinetics GeneralCurrentKinetics {
    // Parameter currentDensity a+b/c;
    // Parameter a 10;
    // Parameter b 3;
    // Parameter c d/2;
    // Parameter d 5;
    // CurrentDensity currentDensity;
    // }
    // 
    // 
    java.io.StringWriter stringWriter = new java.io.StringWriter();
    java.io.PrintWriter pw = new java.io.PrintWriter(stringWriter);
    pw.println("\t\t" + VCMODL.Kinetics + " " + getKineticsDescription().getVCMLKineticsName() + " " + VCMODL.BeginBlock + " ");
    KineticsParameter[] parameters = getKineticsParameters();
    if (parameters != null) {
        for (int i = 0; i < parameters.length; i++) {
            KineticsParameter parm = parameters[i];
            Expression paramExpr = parmExprHash.get(parm.getName());
            VCUnitDefinition unit = parm.getUnitDefinition();
            if (unit == null) {
                pw.println("\t\t\t" + VCMODL.Parameter + " " + parm.getName() + " " + paramExpr.infix() + ";");
            } else {
                pw.println("\t\t\t" + VCMODL.Parameter + " " + parm.getName() + " " + paramExpr.infix() + ";" + " [" + unit.getSymbol() + "]");
            }
        }
    }
    // 
    for (int i = 0; i < parameters.length; i++) {
        KineticsParameter parm = parameters[i];
        if (parm.getRole() != ROLE_UserDefined) {
            pw.println("\t\t\t" + RoleTags[parm.getRole()] + " " + parm.getName() + ";");
        }
    }
    pw.println("\t\t" + VCMODL.EndBlock + " ");
    pw.flush();
    pw.close();
    return stringWriter.getBuffer().toString();
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression)

Example 47 with VCUnitDefinition

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

the class LumpedKinetics method getLumpedInwardCurrent.

protected Expression getLumpedInwardCurrent() throws ExpressionException, PropertyVetoException {
    // 
    // lumped kinetics always have same units (molecules/s) or (pA) whether used in a FluxReaction or a SimpleReaction
    // 
    // CurrentMagnitude = reactionRate * valence * F * unitFactor
    // 
    // for FluxReaction
    // 
    Model model = getReactionStep().getModel();
    Expression F = getSymbolExpression(model.getFARADAY_CONSTANT());
    Expression lumpedReactionRate = getSymbolExpression(getLumpedReactionRateParameter());
    Expression z = getSymbolExpression(getChargeValenceParameter());
    Expression tempInwardCurrentExpression = Expression.mult(z, F, lumpedReactionRate);
    VCUnitDefinition tempRateUnit = getLumpedReactionRateParameter().getUnitDefinition().multiplyBy(model.getFARADAY_CONSTANT().getUnitDefinition()).multiplyBy(getChargeValenceParameter().getUnitDefinition());
    VCUnitDefinition desiredUnit = getLumpedCurrentParameter().getUnitDefinition();
    Expression unitFactorExp = getElectricalUnitFactor(desiredUnit.divideBy(tempRateUnit));
    if (unitFactorExp != null) {
        tempInwardCurrentExpression = Expression.mult(tempInwardCurrentExpression, unitFactorExp);
    }
    if (hasOutwardFlux()) {
        tempInwardCurrentExpression = Expression.negate(tempInwardCurrentExpression);
    }
    return tempInwardCurrentExpression;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression)

Example 48 with VCUnitDefinition

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

the class Model method gatherIssues.

/**
 * Insert the method's description here.
 * Creation date: (5/12/2004 10:38:12 PM)
 * @param issueList java.util.Vector
 */
public void gatherIssues(IssueContext issueContext, List<Issue> issueList) {
    issueContext = issueContext.newChildContext(ContextType.Model, this);
    // 
    try {
        for (ModelParameter modelParameter : fieldModelParameters) {
            Expression exp = modelParameter.getExpression();
            String[] symbols = exp.getSymbols();
            if (symbols != null) {
                String issueMsgPrefix = "Global parameter '" + modelParameter.getName() + "' ";
                for (int j = 0; j < symbols.length; j++) {
                    SymbolTableEntry ste = exp.getSymbolBinding(symbols[j]);
                    if (ste == null) {
                        issueList.add(new Issue(modelParameter, issueContext, IssueCategory.ModelParameterExpressionError, issueMsgPrefix + "references undefined symbol '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
                    } else if (ste instanceof SpeciesContext) {
                        if (!contains((SpeciesContext) ste)) {
                            issueList.add(new Issue(modelParameter, issueContext, IssueCategory.ModelParameterExpressionError, issueMsgPrefix + "references undefined species '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
                        }
                    } else if (ste instanceof ModelParameter) {
                        if (!contains((ModelParameter) ste)) {
                            issueList.add(new Issue(modelParameter, issueContext, IssueCategory.ModelParameterExpressionError, issueMsgPrefix + "references undefined global parameter '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
                        }
                    }
                }
            }
        }
        // 
        for (int i = 0; i < fieldModelParameters.length; i++) {
            try {
                VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(getUnitSystem());
                VCUnitDefinition paramUnitDef = fieldModelParameters[i].getUnitDefinition();
                VCUnitDefinition expUnitDef = unitEvaluator.getUnitDefinition(fieldModelParameters[i].getExpression());
                if (paramUnitDef == null) {
                    issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "defined unit is null", Issue.SEVERITY_WARNING));
                } else if (expUnitDef == null) {
                    issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "computed unit is null", Issue.SEVERITY_WARNING));
                } else if (paramUnitDef.isTBD()) {
                    issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "unit is undefined (" + unitSystem.getInstance_TBD().getSymbol() + ")", Issue.SEVERITY_WARNING));
                } else if (!paramUnitDef.isEquivalent(expUnitDef) && !expUnitDef.isTBD()) {
                    issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "unit mismatch, computed = [" + expUnitDef.getSymbol() + "]", Issue.SEVERITY_WARNING));
                }
            } catch (VCUnitException e) {
                issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "units inconsistent: " + e.getMessage(), Issue.SEVERITY_WARNING));
            } catch (ExpressionException e) {
                issueList.add(new Issue(fieldModelParameters[i], issueContext, IssueCategory.Units, "units inconsistent: " + e.getMessage(), Issue.SEVERITY_WARNING));
            }
        }
    } catch (Throwable e) {
        e.printStackTrace(System.out);
        issueList.add(new Issue(this, issueContext, IssueCategory.Units, "unexpected exception: " + e.getMessage(), Issue.SEVERITY_WARNING));
    }
    // 
    for (Structure struct : fieldStructures) {
        struct.gatherIssues(issueContext, issueList);
    }
    // 
    for (int i = 0; i < fieldReactionSteps.length; i++) {
        fieldReactionSteps[i].gatherIssues(issueContext, issueList);
    }
    // 
    // get issues from species contexts (species patterns)
    // 
    int seedSpeciesCount = 0;
    for (int i = 0; i < fieldSpeciesContexts.length; i++) {
        if (fieldSpeciesContexts[i].hasSpeciesPattern()) {
            seedSpeciesCount++;
        }
        fieldSpeciesContexts[i].gatherIssues(issueContext, issueList);
    }
    if (seedSpeciesCount == 0 && !rbmModelContainer.getReactionRuleList().isEmpty()) {
        String msg = "At least one seed species is required.";
        issueList.add(new Issue(this, issueContext, IssueCategory.Identifiers, msg, Issue.Severity.WARNING));
    }
    // 
    // get issues for symbol name clashes (specifically structures with same voltage names or structure names)
    // 
    HashSet<SymbolTableEntry> steHashSet = new HashSet<SymbolTableEntry>();
    gatherLocalEntries(steHashSet);
    Iterator<SymbolTableEntry> iter = steHashSet.iterator();
    Hashtable<String, SymbolTableEntry> symbolHashtable = new Hashtable<String, SymbolTableEntry>();
    while (iter.hasNext()) {
        SymbolTableEntry ste = iter.next();
        SymbolTableEntry existingSTE = symbolHashtable.get(ste.getName());
        if (existingSTE != null) {
            issueList.add(new Issue(this, issueContext, IssueCategory.Identifiers, "model symbol \"" + ste.getName() + "\" is used within \"" + ste.getNameScope().getName() + "\" and \"" + existingSTE.getNameScope().getName() + "\"", Issue.SEVERITY_ERROR));
        } else {
            symbolHashtable.put(ste.getName(), ste);
        }
    }
    // 
    // gather issues for electrical topology (unspecified +ve or -ve features, or +ve feature == -ve feature
    // 
    getElectricalTopology().gatherIssues(issueContext, issueList);
    // 
    if (rbmModelContainer == null) {
        issueList.add(new Issue(this, issueContext, IssueCategory.InternalError, "Rbm Model Container is null", Issue.SEVERITY_WARNING));
    } else {
        rbmModelContainer.gatherIssues(issueContext, issueList);
    }
}
Also used : Issue(org.vcell.util.Issue) Hashtable(java.util.Hashtable) ExpressionException(cbit.vcell.parser.ExpressionException) VCUnitException(cbit.vcell.units.VCUnitException) VCUnitEvaluator(cbit.vcell.parser.VCUnitEvaluator) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) HashSet(java.util.HashSet)

Example 49 with VCUnitDefinition

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

the class ModelUnitSystem method createUnitSystem.

private static ModelUnitSystem createUnitSystem(VCUnitDefinition volumeSubstanceUnit, VCUnitDefinition membraneSubstanceUnit, VCUnitDefinition lumpedReactionSubstanceUnit, VCUnitDefinition volumeUnit, VCUnitDefinition areaUnit, VCUnitDefinition lengthUnit, VCUnitDefinition timeUnit) {
    ModelUnitSystem modelUnitSystem = new ModelUnitSystem();
    VCUnitDefinition mole = modelUnitSystem.getInstance("mole");
    VCUnitDefinition molecules = modelUnitSystem.getInstance("molecules");
    if (volumeSubstanceUnit == null || (!volumeSubstanceUnit.isCompatible(mole) && !volumeSubstanceUnit.isCompatible(molecules) && !volumeSubstanceUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS()))) {
        throw new RuntimeException("volume substance unit [" + volumeSubstanceUnit + "] must be compatible with mole or molecules");
    }
    if (membraneSubstanceUnit == null || (!membraneSubstanceUnit.isCompatible(mole) && !membraneSubstanceUnit.isCompatible(molecules) && !membraneSubstanceUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS()))) {
        throw new RuntimeException("membrane substance unit [" + membraneSubstanceUnit + "] must be compatible with mole or molecules");
    }
    if (lumpedReactionSubstanceUnit == null || (!lumpedReactionSubstanceUnit.isCompatible(mole) && !lumpedReactionSubstanceUnit.isCompatible(molecules) && !lumpedReactionSubstanceUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS()))) {
        throw new RuntimeException("lumped reaction substance unit [" + lumpedReactionSubstanceUnit + "] must be compatible with mole or molecules");
    }
    if (lengthUnit == null || (!lengthUnit.isCompatible(modelUnitSystem.getInstance("m")) && !lengthUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS()))) {
        throw new RuntimeException("length unit [" + lengthUnit + "] must be compatible with meter");
    }
    if (areaUnit == null || (!areaUnit.isCompatible(modelUnitSystem.getInstance("m2")) && !areaUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS()))) {
        throw new RuntimeException("area unit [" + areaUnit + "] must be compatible with meter^2");
    }
    if (volumeUnit == null || (!volumeUnit.isCompatible(modelUnitSystem.getInstance("m3")) && !volumeUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS()))) {
        throw new RuntimeException("volume unit [" + volumeUnit + "] must be compatible with meter^3");
    }
    // if vol unit is dimensionless, area and length should also be dimensionless.
    if (volumeUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS())) {
        if (!lengthUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS()) && !areaUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS())) {
            throw new RuntimeException("If volume unit is dimensionless, length and area units should also be dimensionless.");
        }
    }
    if (timeUnit == null || (!timeUnit.isCompatible(modelUnitSystem.getInstance("s")) && !timeUnit.isCompatible(modelUnitSystem.getInstance_DIMENSIONLESS()))) {
        throw new RuntimeException("time unit [" + timeUnit + "] must be compatible with seconds");
    }
    modelUnitSystem.UNITSYMBOL_lengthUnit = lengthUnit.getSymbol();
    modelUnitSystem.UNITSYMBOL_areaUnit = areaUnit.getSymbol();
    modelUnitSystem.UNITSYMBOL_volumeUnit = volumeUnit.getSymbol();
    modelUnitSystem.UNITSYMBOL_timeUnit = timeUnit.getSymbol();
    modelUnitSystem.UNITSYMBOL_volumeSubstanceUnit = volumeSubstanceUnit.getSymbol();
    modelUnitSystem.UNITSYMBOL_membraneSubstanceUnit = membraneSubstanceUnit.getSymbol();
    modelUnitSystem.UNITSYMBOL_lumpedReactionSubstanceUnit = lumpedReactionSubstanceUnit.getSymbol();
    return modelUnitSystem;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition)

Example 50 with VCUnitDefinition

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

the class StochMathMapping_4_8 method getExpressionConcToAmt.

/**
 * getExpressionConcToAmt : converts the concentration expression ('concExpr') to an expression of the number of particles.
 * 		If argument 'speciesContext' is on a membrane, particlesExpr = concExpr * size_of_Mem. If 'speciesContext' is in
 * 		feature, particlesExpr = (concExpr * size_of_Feature)/KMOLE.
 * @param concExpr
 * @param speciesContext
 * @return
 * @throws MappingException
 * @throws ExpressionException
 */
Expression getExpressionConcToAmt(Expression concExpr, SpeciesContext speciesContext) throws MappingException, ExpressionException {
    Expression exp = Expression.mult(concExpr, new Expression(speciesContext.getStructure().getStructureSize(), getNameScope()));
    ModelUnitSystem unitSystem = getSimulationContext().getModel().getUnitSystem();
    VCUnitDefinition substanceUnit = unitSystem.getSubstanceUnit(speciesContext.getStructure());
    Expression unitFactor = getUnitFactor(unitSystem.getStochasticSubstanceUnit().divideBy(substanceUnit));
    Expression particlesExpr = Expression.mult(exp, unitFactor);
    return particlesExpr;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) 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