Search in sources :

Example 11 with Domain

use of cbit.vcell.math.Variable.Domain in project vcell by virtualcell.

the class CellQuanVCTranslator method processVariables.

protected void processVariables() {
    @SuppressWarnings("unchecked") List<Element> compElementList = sRoot.getChildren(CELLMLTags.COMPONENT, sNamespace);
    Iterator<Element> compElementIterator = compElementList.iterator();
    Element comp, var, varRef, temp;
    String compName, varName, publicIn, privateIn, mangledName;
    while (compElementIterator.hasNext()) {
        comp = compElementIterator.next();
        compName = comp.getAttributeValue(CELLMLTags.name, sAttNamespace);
        @SuppressWarnings("unchecked") List<Element> varElementList = comp.getChildren(CELLMLTags.VARIABLE, sNamespace);
        Iterator<Element> varElementIterator = varElementList.iterator();
        // clear const and volVars vectors for each component
        while (varElementIterator.hasNext()) {
            var = varElementIterator.next();
            publicIn = var.getAttributeValue(CELLMLTags.public_interface, sAttNamespace);
            privateIn = var.getAttributeValue(CELLMLTags.private_interface, sAttNamespace);
            if ((publicIn != null && publicIn.equals(CELLMLTags.inInterface)) || (privateIn != null && privateIn.equals(CELLMLTags.inInterface)))
                continue;
            varName = var.getAttributeValue(CELLMLTags.name, sAttNamespace);
            mangledName = nm.getMangledName(compName, varName);
            String uniqueVarName = getUniqueParamName(mangledName);
            System.out.println("Vars: " + compName + "." + varName + " " + mangledName + " " + uniqueVarName);
            // declare any differential free variables as VolumeVariable's
            temp = getMathElementIdentifier(comp, varName, MathMLTags.DIFFERENTIAL);
            try {
                if (temp != null) {
                    // formula ignored at this point
                    Domain domain = null;
                    VolVariable volVar = new VolVariable(mangledName, domain);
                    varsHash.addVariable(volVar);
                    continue;
                }
                // any LHS variables of an eq operator are functions
                temp = getMathElementIdentifier(comp, varName, MathMLTags.IDENTIFIER);
                if (temp != null) {
                    Function fn = addFunction(temp, comp, mangledName);
                    varsHash.addVariable(fn);
                    continue;
                }
                // Handle delta variables as a special case function
                varRef = getMatchingVarRef(comp, varName);
                if (varRef != null) {
                    // 2 functions with same name?
                    Function rateFn = addRateFunction(varRef, compName, uniqueVarName);
                    varsHash.addVariable(rateFn);
                    continue;
                }
                // check if it is the time variable, if not add as a constant. No handling for a specific value for the time var.
                if (!isTimeVar(varName)) {
                    Constant constant = new Constant(uniqueVarName, new Expression(getInitial(comp, varName)));
                    varsHash.addVariable(constant);
                }
            } catch (Exception e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("Error adding variable to variablesHash : " + e.getMessage());
            }
        }
    // end iteration for VARIABLES
    }
    // add the variables from varsHash to mathDescription
    try {
        mathDescription.setAllVariables(varsHash.getAlphabeticallyOrderedVariables());
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("Error adding variables to mathDescription : " + e.getMessage());
    }
}
Also used : Function(cbit.vcell.math.Function) VolVariable(cbit.vcell.math.VolVariable) Expression(cbit.vcell.parser.Expression) Constant(cbit.vcell.math.Constant) Element(org.jdom.Element) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) Domain(cbit.vcell.math.Variable.Domain) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 12 with Domain

use of cbit.vcell.math.Variable.Domain in project vcell by virtualcell.

the class CellQuanVCTranslator method addFunction.

private Function addFunction(Element temp, Element comp, String mangledName) {
    String expStr = null;
    Expression exp = null;
    Element parent = (Element) temp.getParent();
    Element sibling = parent.getChild(MathMLTags.APPLY, mathns);
    if (sibling == null) {
        // check if its value is assigned to another variable (i.e. A = B)
        @SuppressWarnings("unchecked") ArrayList<Element> list = new ArrayList<Element>(parent.getChildren(MathMLTags.IDENTIFIER, mathns));
        if (list.size() == 2) {
            expStr = (list.get(1)).getTextTrim();
        }
        if (expStr == null || expStr.length() == 0) {
            expStr = parent.getChildText(MathMLTags.CONSTANT, mathns);
        }
        if (expStr == null || expStr.length() == 0) {
            // check if 'piecewise'
            sibling = parent.getChild(MathMLTags.PIECEWISE, mathns);
        }
        if (expStr != null) {
            try {
                exp = new Expression(expStr);
                exp = processMathExp(comp, exp);
                nl.mangleString(expStr);
            } catch (ExpressionException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException(e.getMessage());
            }
        }
    }
    if (sibling != null) {
        Element trimmedMath = new Element(CELLMLTags.MATH, mathns).addContent(sibling.detach());
        fixMathMLBug(trimmedMath);
        try {
            exp = (new ExpressionMathMLParser(null)).fromMathML(trimmedMath);
        } catch (ExpressionException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException(e.getMessage());
        }
        exp = processMathExp(comp, exp);
        expStr = exp.infix();
        nl.mangleString(expStr);
    }
    Domain domain = null;
    return new Function(mangledName, exp, domain);
}
Also used : Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ExpressionMathMLParser(cbit.vcell.parser.ExpressionMathMLParser) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) Domain(cbit.vcell.math.Variable.Domain) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 13 with Domain

use of cbit.vcell.math.Variable.Domain 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 14 with Domain

use of cbit.vcell.math.Variable.Domain in project vcell by virtualcell.

the class VariableHashTest method main.

/**
 * Starts the application.
 * @param args an array of command-line arguments
 */
public static void main(java.lang.String[] args) {
    try {
        VariableHash hash = new VariableHash();
        hash.addVariable(new Constant("D", new Expression("4")));
        hash.addVariable(new Constant("E", new Expression("D")));
        hash.addVariable(new Constant("B", new Expression("C")));
        hash.addVariable(new Constant("F", new Expression("A+B+C+E")));
        hash.addVariable(new Constant("C", new Expression("D+5")));
        hash.addVariable(new Constant("A", new Expression("B+C")));
        Domain domain = null;
        hash.addVariable(new VolVariable("V1", domain));
        hash.addVariable(new VolVariable("V2", domain));
        hash.addVariable(new Function("B1", new Expression("C1+V3"), domain));
        hash.addVariable(new Function("F1", new Expression("A+B+C+E/V1+V2-C1+B1"), domain));
        hash.addVariable(new Function("C1", new Expression("D+5+B1"), domain));
        hash.addVariable(new VolVariable("V3", domain));
        // //
        // // unsorted list
        // //
        // Variable unsortedVars[] = hash.getTopologicallyReorderedVariables();
        // for (int i = 0; i < unsortedVars.length; i++){
        // System.out.println(unsortedVars[i]);
        // }
        // 
        // Topologically sorted list
        // 
        Variable[] sortedVars = hash.getTopologicallyReorderedVariables();
        for (int i = 0; i < sortedVars.length; i++) {
            System.out.println(sortedVars[i]);
        }
        // 
        // Alphabetically sorted list
        // 
        sortedVars = hash.getAlphabeticallyOrderedVariables();
        for (int i = 0; i < sortedVars.length; i++) {
            System.out.println(sortedVars[i]);
        }
    } catch (Throwable e) {
        e.printStackTrace(System.out);
    }
}
Also used : Function(cbit.vcell.math.Function) Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) Expression(cbit.vcell.parser.Expression) VolVariable(cbit.vcell.math.VolVariable) VariableHash(cbit.vcell.math.VariableHash) Constant(cbit.vcell.math.Constant) Domain(cbit.vcell.math.Variable.Domain)

Example 15 with Domain

use of cbit.vcell.math.Variable.Domain in project vcell by virtualcell.

the class DiffEquMathMapping method refreshVariables.

/**
 * This method was created in VisualAge.
 */
private void refreshVariables() throws MappingException {
    // System.out.println("MathMapping.refreshVariables()");
    // 
    // non-constant dependent variables require a function
    // 
    Enumeration<SpeciesContextMapping> enum1 = getSpeciesContextMappings();
    while (enum1.hasMoreElements()) {
        SpeciesContextMapping scm = enum1.nextElement();
        SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(scm.getSpeciesContext());
        if (scm.getDependencyExpression() != null && !scs.isConstant()) {
            // scm.setVariable(new Function(scm.getSpeciesContext().getName(),scm.getDependencyExpression()));
            scm.setVariable(null);
        }
        if (getSimulationContext().hasEventAssignment(scs.getSpeciesContext())) {
            scm.setDependencyExpression(null);
        }
    }
    // 
    // non-constant independent variables require either a membrane or volume variable
    // 
    enum1 = getSpeciesContextMappings();
    while (enum1.hasMoreElements()) {
        SpeciesContextMapping scm = (SpeciesContextMapping) enum1.nextElement();
        SpeciesContextSpec scs = simContext.getReactionContext().getSpeciesContextSpec(scm.getSpeciesContext());
        if (scm.getDependencyExpression() == null && (!scs.isConstant() || getSimulationContext().hasEventAssignment(scs.getSpeciesContext()))) {
            StructureMapping sm = simContext.getGeometryContext().getStructureMapping(scm.getSpeciesContext().getStructure());
            Structure struct = scm.getSpeciesContext().getStructure();
            Domain domain = null;
            if (sm.getGeometryClass() != null) {
                domain = new Domain(sm.getGeometryClass());
            }
            if (struct instanceof Feature || struct instanceof Membrane) {
                if (sm.getGeometryClass() instanceof SurfaceClass) {
                    if (scs.isWellMixed()) {
                        scm.setVariable(new MembraneRegionVariable(scm.getSpeciesContext().getName(), domain));
                    } else if (getSimulationContext().isStoch() && getSimulationContext().getGeometry().getDimension() > 0 && !scs.isForceContinuous()) {
                        scm.setVariable(new MemVariable(scm.getSpeciesContext().getName() + "_conc", domain));
                    } else {
                        scm.setVariable(new MemVariable(scm.getSpeciesContext().getName(), domain));
                    }
                } else {
                    if (scs.isWellMixed()) {
                        scm.setVariable(new VolumeRegionVariable(scm.getSpeciesContext().getName(), domain));
                    } else if (getSimulationContext().isStoch() && getSimulationContext().getGeometry().getDimension() > 0 && !scs.isForceContinuous()) {
                        scm.setVariable(new VolVariable(scm.getSpeciesContext().getName() + "_conc", domain));
                    } else {
                        scm.setVariable(new VolVariable(scm.getSpeciesContext().getName(), domain));
                    }
                }
            } else {
                throw new MappingException("class " + scm.getSpeciesContext().getStructure().getClass() + " not supported");
            }
            mathSymbolMapping.put(scm.getSpeciesContext(), scm.getVariable().getName());
        }
    }
}
Also used : MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) SurfaceClass(cbit.vcell.geometry.SurfaceClass) VolVariable(cbit.vcell.math.VolVariable) Feature(cbit.vcell.model.Feature) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) MemVariable(cbit.vcell.math.MemVariable) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) Domain(cbit.vcell.math.Variable.Domain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain)

Aggregations

Domain (cbit.vcell.math.Variable.Domain)54 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)35 Expression (cbit.vcell.parser.Expression)34 MembraneSubDomain (cbit.vcell.math.MembraneSubDomain)28 PointSubDomain (cbit.vcell.math.PointSubDomain)22 FilamentSubDomain (cbit.vcell.math.FilamentSubDomain)19 VolVariable (cbit.vcell.math.VolVariable)15 ExpressionException (cbit.vcell.parser.ExpressionException)14 Function (cbit.vcell.math.Function)13 SubDomain (cbit.vcell.math.SubDomain)13 Variable (cbit.vcell.math.Variable)13 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)10 Element (org.jdom.Element)10 Constant (cbit.vcell.math.Constant)9 MemVariable (cbit.vcell.math.MemVariable)9 VariableHash (cbit.vcell.math.VariableHash)9 VariableType (cbit.vcell.math.VariableType)9 VolumeRegionVariable (cbit.vcell.math.VolumeRegionVariable)8 GeometryClass (cbit.vcell.geometry.GeometryClass)7 SubVolume (cbit.vcell.geometry.SubVolume)7