Search in sources :

Example 21 with Equation

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

the class MathDescPanel method refreshEquations.

/**
 * This method was created by a SmartGuide.
 */
private void refreshEquations() throws ExpressionException {
    TreePath selectionPath = getselectionModel1().getSelectionPath();
    if (selectionPath != null) {
        BioModelNode node = (BioModelNode) selectionPath.getLastPathComponent();
        Object[] objectPath = node.getUserObjectPath();
        Object lastPathObject = objectPath[objectPath.length - 1];
        // for non-stochastic
        Expression[] expArray = null;
        // for stochastic
        String[] expString = null;
        getExpressionCanvas1().setStrings(null);
        getExpressionCanvas1().setExpressions(null);
        if (lastPathObject instanceof Function) {
            Function function = (Function) lastPathObject;
            Expression exp = function.getExpression();
            if (getJRadioButtonValue().isSelected()) {
                exp.bindExpression(getMathDescription());
            } else {
                exp.bindExpression(null);
            }
            exp = exp.flatten();
            // stochastic variable initial value must be an int, therefore we must make it here.
            if (getMathDescription().isNonSpatialStoch() && function.getName().startsWith(DiffEquMathMapping.MATH_FUNC_SUFFIX_SPECIES_INIT_CONC_UNIT_PREFIX)) {
                try {
                    double value = exp.evaluateConstant();
                    expString = new String[] { function.getName() + " = " + Math.round(value) };
                    getExpressionCanvas1().setStrings(expString);
                } catch (Exception e) {
                    expArray = new Expression[] { Expression.assign(new Expression(function.getName()), exp) };
                    getExpressionCanvas1().setExpressions(expArray);
                }
            } else {
                expArray = new Expression[] { Expression.assign(new Expression(function.getName()), exp) };
                getExpressionCanvas1().setExpressions(expArray);
            }
        } else if (lastPathObject instanceof Constant) {
            Constant constant = (Constant) lastPathObject;
            Expression exp = constant.getExpression();
            if (getJRadioButtonValue().isSelected()) {
                exp.bindExpression(getMathDescription());
            } else {
                exp.bindExpression(null);
            }
            exp = exp.flatten();
            // stochastic variable initial value must be an int, therefore we must make it here.
            if (getMathDescription().isNonSpatialStoch() && constant.getName().startsWith(DiffEquMathMapping.MATH_FUNC_SUFFIX_SPECIES_INIT_CONC_UNIT_PREFIX)) {
                try {
                    double value = exp.evaluateConstant();
                    expString = new String[] { constant.getName() + " = " + Math.round(value) };
                    getExpressionCanvas1().setStrings(expString);
                } catch (Exception e) {
                    expArray = new Expression[] { Expression.assign(new Expression(constant.getName()), exp) };
                    getExpressionCanvas1().setExpressions(expArray);
                }
            } else {
                expArray = new Expression[] { Expression.assign(new Expression(constant.getName()), exp) };
                getExpressionCanvas1().setExpressions(expArray);
            }
        } else if (lastPathObject instanceof Equation) {
            Equation equ = (Equation) lastPathObject;
            Enumeration<Expression> enum_equ = equ.getTotalExpressions();
            Vector<Expression> expList = new Vector<Expression>();
            while (enum_equ.hasMoreElements()) {
                Expression exp = new Expression(enum_equ.nextElement());
                if (getJRadioButtonValue().isSelected()) {
                    exp.bindExpression(getMathDescription());
                } else {
                    exp.bindExpression(null);
                }
                expList.addElement(exp.flatten());
            }
            expArray = new Expression[expList.size()];
            expList.copyInto(expArray);
            getExpressionCanvas1().setExpressions(expArray);
        } else if (// added Sept 29, 2006
        lastPathObject instanceof VarIniCondition) {
            VarIniCondition varIni = (VarIniCondition) lastPathObject;
            expString = new String[] { varIni.toString() };
            getExpressionCanvas1().setStrings(expString);
        } else if (// added Sept 29, 2006
        lastPathObject instanceof Action) {
            Action action = (Action) lastPathObject;
            expString = new String[] { action.toString() };
            getExpressionCanvas1().setStrings(expString);
        } else if (// added Sept 29, 2006
        lastPathObject instanceof String) {
            // to check if the string is the probability string
            int index1 = ((String) lastPathObject).indexOf("probability_rate");
            int index2 = ((String) lastPathObject).indexOf("=");
            if (// has probability eqution,but don't calculate prob string, since it is not a constant value.
            (index1 == 0) && ((index2 + 2) < ((String) lastPathObject).length())) {
                expString = new String[] { (String) lastPathObject };
                getExpressionCanvas1().setStrings(expString);
            }
        }
    } else {
        getExpressionCanvas1().setExpressions(null);
        getExpressionCanvas1().setStrings(null);
        getExpressionCanvas1().repaint();
    }
// Variable var = getMathDescription().getVariable(varName);
// SubDomain subDomain = getMathDescription().getSubDomain(subDomainName);
// Equation equ = null;
// if (var != null && subDomain != null){
// equ = subDomain.getEquation(var);
// }
// if (equ == null){
// if (subDomain instanceof MembraneSubDomain && var instanceof VolVariable){
// equ = ((MembraneSubDomain)subDomain).getJumpCondition((VolVariable)var);
// }
// }
// if (equ == null){
// Expression exp = null;
// String title = "hello";
// if (var instanceof Constant || var instanceof Function){
// exp = var.getExpression();
// title = "Expression for "+var.getName();
// }
// if (getJRadioButtonValue().isSelected()){
// exp.bindExpression(getMathDescription());
// }else{
// exp.bindExpression(null);
// }
// getExpressionCanvas1().setExpression(exp.flatten());
// getEquationTitleLabel().setText(title);
// }else{
// Enumeration enum_equ = equ.getTotalExpressions();
// Vector expList = new Vector();
// while (enum_equ.hasMoreElements()){
// Expression exp = new Expression((Expression)enum_equ.nextElement());
// if (getJRadioButtonValue().isSelected()){
// exp.bindExpression(getMathDescription());
// }else{
// exp.bindExpression(null);
// }
// expList.addElement(exp.flatten());
// }
// Expression expArray[] = new Expression[expList.size()];
// expList.copyInto(expArray);
// getExpressionCanvas1().setExpressions(expArray);
// getEquationTitleLabel().setText("Equations for "+var.getName()+" within "+subDomain.getName());
// }
// } catch (java.lang.Throwable ivjExc) {
// handleException(ivjExc);
// getEquationTitleLabel().setText("Equations for "+varName+" within "+subDomainName+" *** "+ivjExc.getMessage());
// }
// }
// }else{
// getEquationTitleLabel().setText("no variable selected");
// getExpressionCanvas1().setExpression((Expression)null);
// }
}
Also used : VarIniCondition(cbit.vcell.math.VarIniCondition) Action(cbit.vcell.math.Action) Constant(cbit.vcell.math.Constant) Equation(cbit.vcell.math.Equation) BioModelNode(cbit.vcell.desktop.BioModelNode) ExpressionException(cbit.vcell.parser.ExpressionException) Function(cbit.vcell.math.Function) TreePath(javax.swing.tree.TreePath) Expression(cbit.vcell.parser.Expression) Vector(java.util.Vector)

Example 22 with Equation

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

the class MathDescriptionTreeModel method createBaseTree.

/**
 * Insert the method's description here.
 * Creation date: (11/28/00 1:06:51 PM)
 * @return cbit.vcell.desktop.BioModelNode
 * @param docManager cbit.vcell.clientdb.DocumentManager
 */
private BioModelNode createBaseTree() {
    if (getMathDescription() == null) {
        return new BioModelNode(" ", false);
    }
    // 
    // make root node
    // 
    BioModelNode rootNode = new BioModelNode("math description", true);
    // 
    // create subTree of Constants
    // 
    BioModelNode constantRootNode = new BioModelNode("constants", true);
    Enumeration<Constant> enum1 = getMathDescription().getConstants();
    while (enum1.hasMoreElements()) {
        Constant constant = enum1.nextElement();
        BioModelNode constantNode = new BioModelNode(constant, false);
        constantRootNode.add(constantNode);
    }
    if (constantRootNode.getChildCount() > 0) {
        rootNode.add(constantRootNode);
    }
    // 
    // create subTree of Functions
    // 
    BioModelNode functionRootNode = new BioModelNode("functions", true);
    Enumeration<Function> enum2 = getMathDescription().getFunctions();
    while (enum2.hasMoreElements()) {
        Function function = enum2.nextElement();
        BioModelNode functionNode = new BioModelNode(function, false);
        functionRootNode.add(functionNode);
    }
    if (functionRootNode.getChildCount() > 0) {
        rootNode.add(functionRootNode);
    }
    // 
    // create subTree of VolumeSubDomains and MembraneSubDomains
    // 
    BioModelNode volumeRootNode = new BioModelNode("volume domains", true);
    BioModelNode membraneRootNode = new BioModelNode("membrane domains", true);
    Enumeration<SubDomain> enum3 = getMathDescription().getSubDomains();
    while (enum3.hasMoreElements()) {
        SubDomain subDomain = enum3.nextElement();
        if (subDomain instanceof cbit.vcell.math.CompartmentSubDomain) {
            CompartmentSubDomain volumeSubDomain = (CompartmentSubDomain) subDomain;
            BioModelNode volumeSubDomainNode = new BioModelNode(volumeSubDomain, true);
            if (// stochastic subtree
            getMathDescription().isNonSpatialStoch()) {
                // add stoch variable initial conditions
                BioModelNode varIniConditionNode = new BioModelNode("variable_initial_conditions", true);
                for (VarIniCondition varIni : volumeSubDomain.getVarIniConditions()) {
                    BioModelNode varIniNode = new BioModelNode(varIni, false);
                    varIniConditionNode.add(varIniNode);
                }
                volumeSubDomainNode.add(varIniConditionNode);
                // add jump processes
                for (JumpProcess jp : volumeSubDomain.getJumpProcesses()) {
                    BioModelNode jpNode = new BioModelNode(jp, true);
                    // add probability rate.
                    String probRate = "P_" + jp.getName();
                    BioModelNode prNode = new BioModelNode("probability_rate = " + probRate, false);
                    jpNode.add(prNode);
                    // add Actions
                    Enumeration<Action> actions = Collections.enumeration(jp.getActions());
                    while (actions.hasMoreElements()) {
                        Action action = actions.nextElement();
                        BioModelNode actionNode = new BioModelNode(action, false);
                        jpNode.add(actionNode);
                    }
                    volumeSubDomainNode.add(jpNode);
                }
            } else // non-stochastic subtree
            {
                // 
                // add equation children
                // 
                Enumeration<Equation> eqnEnum = volumeSubDomain.getEquations();
                while (eqnEnum.hasMoreElements()) {
                    Equation equation = eqnEnum.nextElement();
                    BioModelNode equationNode = new BioModelNode(equation, false);
                    volumeSubDomainNode.add(equationNode);
                }
                // 
                // add fast system
                // 
                FastSystem fastSystem = volumeSubDomain.getFastSystem();
                if (fastSystem != null) {
                    BioModelNode fsNode = new BioModelNode(fastSystem, true);
                    Enumeration<FastInvariant> enumFI = fastSystem.getFastInvariants();
                    while (enumFI.hasMoreElements()) {
                        FastInvariant fi = enumFI.nextElement();
                        fsNode.add(new BioModelNode(fi, false));
                    }
                    Enumeration<FastRate> enumFR = fastSystem.getFastRates();
                    while (enumFR.hasMoreElements()) {
                        FastRate fr = enumFR.nextElement();
                        fsNode.add(new BioModelNode(fr, false));
                    }
                    volumeSubDomainNode.add(fsNode);
                }
            }
            volumeRootNode.add(volumeSubDomainNode);
        } else if (subDomain instanceof MembraneSubDomain) {
            MembraneSubDomain membraneSubDomain = (MembraneSubDomain) subDomain;
            BioModelNode membraneSubDomainNode = new BioModelNode(membraneSubDomain, true);
            // 
            // add equation children
            // 
            Enumeration<Equation> eqnEnum = membraneSubDomain.getEquations();
            while (eqnEnum.hasMoreElements()) {
                Equation equation = eqnEnum.nextElement();
                BioModelNode equationNode = new BioModelNode(equation, false);
                membraneSubDomainNode.add(equationNode);
            }
            // 
            // add jump condition children
            // 
            Enumeration<JumpCondition> jcEnum = membraneSubDomain.getJumpConditions();
            while (jcEnum.hasMoreElements()) {
                JumpCondition jumpCondition = jcEnum.nextElement();
                BioModelNode jcNode = new BioModelNode(jumpCondition, false);
                membraneSubDomainNode.add(jcNode);
            }
            // 
            // add fast system
            // 
            FastSystem fastSystem = membraneSubDomain.getFastSystem();
            if (fastSystem != null) {
                BioModelNode fsNode = new BioModelNode(fastSystem, true);
                Enumeration<FastInvariant> enumFI = fastSystem.getFastInvariants();
                while (enumFI.hasMoreElements()) {
                    FastInvariant fi = enumFI.nextElement();
                    fsNode.add(new BioModelNode(fi, false));
                }
                Enumeration<FastRate> enumFR = fastSystem.getFastRates();
                while (enumFR.hasMoreElements()) {
                    FastRate fr = enumFR.nextElement();
                    fsNode.add(new BioModelNode(fr, false));
                }
                membraneSubDomainNode.add(fsNode);
            }
            membraneRootNode.add(membraneSubDomainNode);
        }
    }
    if (volumeRootNode.getChildCount() > 0) {
        rootNode.add(volumeRootNode);
    }
    if (membraneRootNode.getChildCount() > 0) {
        rootNode.add(membraneRootNode);
    }
    return rootNode;
}
Also used : JumpCondition(cbit.vcell.math.JumpCondition) VarIniCondition(cbit.vcell.math.VarIniCondition) Action(cbit.vcell.math.Action) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Enumeration(java.util.Enumeration) Constant(cbit.vcell.math.Constant) Equation(cbit.vcell.math.Equation) FastRate(cbit.vcell.math.FastRate) BioModelNode(cbit.vcell.desktop.BioModelNode) FastInvariant(cbit.vcell.math.FastInvariant) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Function(cbit.vcell.math.Function) FastSystem(cbit.vcell.math.FastSystem) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) JumpProcess(cbit.vcell.math.JumpProcess)

Example 23 with Equation

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

the class ApplicationConstraintsGenerator method steadyStateFromApplication.

/**
 * Insert the method's description here.
 * Creation date: (6/26/01 8:25:55 AM)
 * @return cbit.vcell.constraints.ConstraintContainerImpl
 */
public static ConstraintContainerImpl steadyStateFromApplication(SimulationContext simContext, double tolerance) {
    try {
        ConstraintContainerImpl ccImpl = new ConstraintContainerImpl();
        // ====================
        // add physical limits
        // ====================
        // 
        // no negative concentrations
        // 
        cbit.vcell.model.Model model = simContext.getModel();
        cbit.vcell.model.SpeciesContext[] speciesContexts = model.getSpeciesContexts();
        for (int i = 0; i < speciesContexts.length; i++) {
            ccImpl.addSimpleBound(new SimpleBounds(speciesContexts[i].getName(), new RealInterval(0, Double.POSITIVE_INFINITY), AbstractConstraint.PHYSICAL_LIMIT, "non-negative concentration"));
        }
        for (int i = 0; i < speciesContexts.length; i++) {
            SpeciesContextSpecParameter initParam = (simContext.getReactionContext().getSpeciesContextSpec(speciesContexts[i])).getInitialConditionParameter();
            if (initParam != null) {
                double initialValue = initParam.getExpression().evaluateConstant();
                double lowInitialValue = Math.min(initialValue / tolerance, initialValue * tolerance);
                double highInitialValue = Math.max(initialValue / tolerance, initialValue * tolerance);
                ccImpl.addSimpleBound(new SimpleBounds(speciesContexts[i].getName(), new RealInterval(lowInitialValue, highInitialValue), AbstractConstraint.MODELING_ASSUMPTION, "close to specified \"initialCondition\""));
            }
        }
        // =========================
        // add modeling assumptions
        // =========================
        // 
        // mass action forward and reverse rates should be non-negative
        // 
        cbit.vcell.model.ReactionStep[] reactionSteps = model.getReactionSteps();
        for (int i = 0; i < reactionSteps.length; i++) {
            Kinetics kinetics = reactionSteps[i].getKinetics();
            if (kinetics instanceof MassActionKinetics) {
                Expression forwardRateConstraintExp = new Expression(((MassActionKinetics) kinetics).getForwardRateParameter().getExpression().infix() + ">=0");
                forwardRateConstraintExp = getSteadyStateExpression(forwardRateConstraintExp);
                if (!forwardRateConstraintExp.compareEqual(new Expression(1.0))) {
                    ccImpl.addGeneralConstraint(new GeneralConstraint(forwardRateConstraintExp, AbstractConstraint.MODELING_ASSUMPTION, "non-negative forward rate"));
                }
                Expression reverseRateConstraintExp = new Expression(((MassActionKinetics) kinetics).getReverseRateParameter().getExpression().infix() + ">=0");
                reverseRateConstraintExp = getSteadyStateExpression(reverseRateConstraintExp);
                if (!reverseRateConstraintExp.compareEqual(new Expression(1.0))) {
                    ccImpl.addGeneralConstraint(new GeneralConstraint(reverseRateConstraintExp, AbstractConstraint.MODELING_ASSUMPTION, "non-negative reverse rate"));
                }
            }
            KineticsParameter authoritativeParameter = kinetics.getAuthoritativeParameter();
            Expression kineticRateConstraintExp = new Expression(authoritativeParameter.getName() + "==" + authoritativeParameter.getExpression().infix());
            kineticRateConstraintExp = getSteadyStateExpression(kineticRateConstraintExp);
            if (!kineticRateConstraintExp.compareEqual(new Expression(1.0))) {
                ccImpl.addGeneralConstraint(new GeneralConstraint(kineticRateConstraintExp, AbstractConstraint.MODELING_ASSUMPTION, "definition"));
            }
        }
        // 
        try {
            simContext.setMathDescription(simContext.createNewMathMapping().getMathDescription());
        } catch (Throwable e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("cannot create mathDescription");
        }
        MathDescription mathDesc = simContext.getMathDescription();
        if (mathDesc.getGeometry().getDimension() > 0) {
            throw new RuntimeException("spatial simulations not yet supported");
        }
        CompartmentSubDomain subDomain = (CompartmentSubDomain) mathDesc.getSubDomains().nextElement();
        java.util.Enumeration<Equation> enumEquations = subDomain.getEquations();
        while (enumEquations.hasMoreElements()) {
            Equation equation = (Equation) enumEquations.nextElement();
            Expression rateConstraintExp = new Expression(equation.getRateExpression().infix() + "==0");
            rateConstraintExp = getSteadyStateExpression(rateConstraintExp);
            if (!rateConstraintExp.compareEqual(new Expression(1.0))) {
                // not a trivial constraint (always true)
                ccImpl.addGeneralConstraint(new GeneralConstraint(rateConstraintExp, AbstractConstraint.PHYSICAL_LIMIT, "definition of steady state"));
            }
        }
        // 
        for (int i = 0; i < reactionSteps.length; i++) {
            Kinetics kinetics = reactionSteps[i].getKinetics();
            Kinetics.KineticsParameter[] parameters = kinetics.getKineticsParameters();
            for (int j = 0; j < parameters.length; j++) {
                Expression exp = parameters[j].getExpression();
                if (exp.getSymbols() == null || exp.getSymbols().length == 0) {
                    // 
                    try {
                        double constantValue = exp.evaluateConstant();
                        double lowValue = Math.min(constantValue / tolerance, constantValue * tolerance);
                        double highValue = Math.max(constantValue / tolerance, constantValue * tolerance);
                        RealInterval interval = new RealInterval(lowValue, highValue);
                        ccImpl.addSimpleBound(new SimpleBounds(parameters[j].getName(), interval, AbstractConstraint.MODELING_ASSUMPTION, "parameter close to model default"));
                    } catch (cbit.vcell.parser.ExpressionException e) {
                        System.out.println("error evaluating parameter " + parameters[j].getName() + " in reaction step " + reactionSteps[i].getName());
                    }
                } else {
                    Expression parameterDefinitionExp = new Expression(parameters[j].getName() + "==" + parameters[j].getExpression().infix());
                    ccImpl.addGeneralConstraint(new GeneralConstraint(getSteadyStateExpression(parameterDefinitionExp), AbstractConstraint.MODELING_ASSUMPTION, "parameter definition"));
                }
            }
        }
        ccImpl.addSimpleBound(new SimpleBounds(model.getFARADAY_CONSTANT().getName(), new RealInterval(model.getFARADAY_CONSTANT().getExpression().evaluateConstant()), AbstractConstraint.PHYSICAL_LIMIT, "Faraday's constant"));
        ccImpl.addSimpleBound(new SimpleBounds(model.getTEMPERATURE().getName(), new RealInterval(300), AbstractConstraint.PHYSICAL_LIMIT, "Absolute Temperature Kelvin"));
        ccImpl.addSimpleBound(new SimpleBounds(model.getGAS_CONSTANT().getName(), new RealInterval(model.getGAS_CONSTANT().getExpression().evaluateConstant()), AbstractConstraint.PHYSICAL_LIMIT, "ideal gas constant"));
        ccImpl.addSimpleBound(new SimpleBounds(model.getKMILLIVOLTS().getName(), new RealInterval(model.getKMILLIVOLTS().getExpression().evaluateConstant()), AbstractConstraint.PHYSICAL_LIMIT, "ideal gas constant"));
        // 
        // add K_fluxs
        // 
        java.util.Enumeration<Variable> enumVars = mathDesc.getVariables();
        while (enumVars.hasMoreElements()) {
            Variable var = (Variable) enumVars.nextElement();
            if (var.getName().startsWith("Kflux_") && var instanceof Function) {
                Expression kfluxExp = new Expression(((Function) var).getExpression());
                kfluxExp.bindExpression(mathDesc);
                kfluxExp = MathUtilities.substituteFunctions(kfluxExp, mathDesc);
                kfluxExp = kfluxExp.flatten();
                ccImpl.addSimpleBound(new SimpleBounds(var.getName(), new RealInterval(kfluxExp.evaluateConstant()), AbstractConstraint.MODELING_ASSUMPTION, "flux conversion factor"));
            }
        }
        return ccImpl;
    } catch (cbit.vcell.parser.ExpressionException e) {
        e.printStackTrace(System.out);
        return null;
    } catch (java.beans.PropertyVetoException e) {
        e.printStackTrace(System.out);
        return null;
    }
}
Also used : Variable(cbit.vcell.math.Variable) SimpleBounds(cbit.vcell.constraints.SimpleBounds) MathDescription(cbit.vcell.math.MathDescription) GeneralConstraint(cbit.vcell.constraints.GeneralConstraint) RealInterval(net.sourceforge.interval.ia_math.RealInterval) Function(cbit.vcell.math.Function) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) ConstraintContainerImpl(cbit.vcell.constraints.ConstraintContainerImpl) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter) Equation(cbit.vcell.math.Equation) AbstractConstraint(cbit.vcell.constraints.AbstractConstraint) GeneralConstraint(cbit.vcell.constraints.GeneralConstraint) Expression(cbit.vcell.parser.Expression) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) MassActionKinetics(cbit.vcell.model.MassActionKinetics) MassActionKinetics(cbit.vcell.model.MassActionKinetics) Kinetics(cbit.vcell.model.Kinetics)

Example 24 with Equation

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

the class FiniteVolumeFileWriter method writeCompartment_VarContext.

/**
 * Insert the method's description here.
 * Creation date: (5/9/2005 2:52:48 PM)
 * @throws ExpressionException
 */
private void writeCompartment_VarContext(CompartmentSubDomain volSubDomain) throws ExpressionException {
    Simulation simulation = simTask.getSimulation();
    // 
    // get list of volVariables participating in PDEs (anywhere).
    // 
    Vector<VolVariable> pdeVolVariableList = new Vector<VolVariable>();
    Variable[] variables = simTask.getSimulationJob().getSimulationSymbolTable().getVariables();
    for (int i = 0; i < variables.length; i++) {
        if (variables[i] instanceof VolVariable && simulation.getMathDescription().isPDE((VolVariable) variables[i])) {
            pdeVolVariableList.add((VolVariable) variables[i]);
        }
    }
    Enumeration<Equation> enum_equ = volSubDomain.getEquations();
    while (enum_equ.hasMoreElements()) {
        Equation equation = enum_equ.nextElement();
        // for chombo solver, only write equations for variables that are defined in this compartment
        if (!bChomboSolver || equation.getVariable().getDomain().getName().equals(volSubDomain.getName())) {
            if (equation instanceof VolumeRegionEquation) {
                writeCompartmentRegion_VarContext_Equation(volSubDomain, (VolumeRegionEquation) equation);
            } else if (equation instanceof MeasureEquation) {
                throw new RuntimeException("Measure Equation " + equation.getClass().getSimpleName() + " not yet supported in FiniteVolume solvers");
            } else {
                writeCompartment_VarContext_Equation(volSubDomain, equation);
            }
        }
        if (equation instanceof PdeEquation) {
            pdeVolVariableList.remove(equation.getVariable());
        }
    }
    // 
    if (!bChomboSolver) {
        for (int i = 0; i < pdeVolVariableList.size(); i++) {
            VolVariable volVar = pdeVolVariableList.elementAt(i);
            boolean bSteady = simulation.getMathDescription().isPdeSteady(volVar);
            PdeEquation dummyPdeEquation = new PdeEquation(volVar, bSteady, new Expression(0.0), new Expression(0.0), new Expression(0.0));
            writeCompartment_VarContext_Equation(volSubDomain, dummyPdeEquation);
        }
    }
}
Also used : FilamentVariable(cbit.vcell.math.FilamentVariable) VolVariable(cbit.vcell.math.VolVariable) ReservedVariable(cbit.vcell.math.ReservedVariable) ParameterVariable(cbit.vcell.math.ParameterVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) MemVariable(cbit.vcell.math.MemVariable) Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) MeasureEquation(cbit.vcell.math.MeasureEquation) PdeEquation(cbit.vcell.math.PdeEquation) VolumeRegionEquation(cbit.vcell.math.VolumeRegionEquation) MembraneRegionEquation(cbit.vcell.math.MembraneRegionEquation) Equation(cbit.vcell.math.Equation) PdeEquation(cbit.vcell.math.PdeEquation) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) VolumeRegionEquation(cbit.vcell.math.VolumeRegionEquation) MeasureEquation(cbit.vcell.math.MeasureEquation) Vector(java.util.Vector)

Example 25 with Equation

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

the class FiniteVolumeFileWriter method writeMembrane_jumpConditions.

/**
 *JUMP_CONDITION_BEGIN r
 *INFLUX 0.0;
 *OUTFLUX 0.0;
 *JUMP_CONDITION_END
 * @throws ExpressionException
 * @throws MathException
 */
private void writeMembrane_jumpConditions(MembraneSubDomain msd) throws ExpressionException, MathException {
    Enumeration<JumpCondition> enum1 = msd.getJumpConditions();
    // equations for boundaryValues for inner compartment subdomain
    CompartmentSubDomain innerCompSubDomain = msd.getInsideCompartment();
    Enumeration<Equation> innercompSubDomEqnsEnum = innerCompSubDomain.getEquations();
    while (innercompSubDomEqnsEnum.hasMoreElements()) {
        Equation eqn = innercompSubDomEqnsEnum.nextElement();
        if (eqn instanceof PdeEquation) {
            PdeEquation pdeEqn = (PdeEquation) eqn;
            BoundaryConditionValue boundaryValue = pdeEqn.getBoundaryConditionValue(msd.getName());
            if (boundaryValue != null) {
                // check if the type of BoundaryConditionSpec for this membraneSubDomain (msd) in the (inner) compartmentSubDomain is Flux; if not, it cannot be handled.
                BoundaryConditionSpec bcs = innerCompSubDomain.getBoundaryConditionSpec(msd.getName());
                if (bcs == null) {
                    throw new MathException("No Boundary type specified for '" + msd.getName() + "' in '" + innerCompSubDomain.getName() + "'.");
                }
                if (bcs != null && !bcs.getBoundaryConditionType().compareEqual(BoundaryConditionType.getNEUMANN()) && !bChomboSolver) {
                    throw new MathException("Boundary type '" + bcs.getBoundaryConditionType().boundaryTypeStringValue() + "' for compartmentSubDomain '" + innerCompSubDomain.getName() + "' not handled by the chosen solver. Expecting boundary condition of type 'Flux'.");
                }
                if (pdeEqn.getVariable().getDomain() == null || pdeEqn.getVariable().getDomain().getName().equals(msd.getInsideCompartment().getName())) {
                    printWriter.println("JUMP_CONDITION_BEGIN " + pdeEqn.getVariable().getName());
                    Expression flux = subsituteExpression(boundaryValue.getBoundaryConditionExpression(), VariableDomain.VARIABLEDOMAIN_MEMBRANE);
                    String infix = replaceVolumeVariable(getSimulationTask(), msd, flux);
                    printWriter.println(bcs.getBoundaryConditionType().boundaryTypeStringValue().toUpperCase() + " " + msd.getInsideCompartment().getName() + " " + infix + ";");
                    printWriter.println("JUMP_CONDITION_END");
                    printWriter.println();
                }
            }
        }
    }
    // equations for boundaryValues for outer compartment subdomain
    CompartmentSubDomain outerCompSubDomain = msd.getOutsideCompartment();
    Enumeration<Equation> outerCompSubDomEqnsEnum = outerCompSubDomain.getEquations();
    while (outerCompSubDomEqnsEnum.hasMoreElements()) {
        Equation eqn = outerCompSubDomEqnsEnum.nextElement();
        if (eqn instanceof PdeEquation) {
            PdeEquation pdeEqn = (PdeEquation) eqn;
            BoundaryConditionValue boundaryValue = pdeEqn.getBoundaryConditionValue(msd.getName());
            if (boundaryValue != null) {
                // check if the type of BoundaryConditionSpec for this membraneSubDomain (msd) in the (inner) compartmentSubDomain is Flux; if not, it cannot be handled.
                BoundaryConditionSpec bcs = outerCompSubDomain.getBoundaryConditionSpec(msd.getName());
                if (bcs != null && !bcs.getBoundaryConditionType().compareEqual(BoundaryConditionType.getNEUMANN()) && !bChomboSolver) {
                    throw new MathException("Boundary type '" + bcs.getBoundaryConditionType().boundaryTypeStringValue() + "' for compartmentSubDomain '" + outerCompSubDomain.getName() + "' not handled by the chosen solver. Expecting boundary condition of type 'Flux'.");
                }
                if (pdeEqn.getVariable().getDomain() == null || pdeEqn.getVariable().getDomain().getName().equals(msd.getOutsideCompartment().getName())) {
                    printWriter.println("JUMP_CONDITION_BEGIN " + pdeEqn.getVariable().getName());
                    Expression flux = subsituteExpression(boundaryValue.getBoundaryConditionExpression(), VariableDomain.VARIABLEDOMAIN_MEMBRANE);
                    String infix = replaceVolumeVariable(getSimulationTask(), msd, flux);
                    printWriter.println(bcs.getBoundaryConditionType().boundaryTypeStringValue().toUpperCase() + " " + msd.getOutsideCompartment().getName() + " " + infix + ";");
                    printWriter.println("JUMP_CONDITION_END");
                    printWriter.println();
                }
            }
        }
    }
    while (enum1.hasMoreElements()) {
        JumpCondition jc = enum1.nextElement();
        printWriter.println("JUMP_CONDITION_BEGIN " + jc.getVariable().getName());
        // influx
        if (jc.getVariable().getDomain() == null || jc.getVariable().getDomain().getName().equals(msd.getInsideCompartment().getName())) {
            Expression flux = subsituteExpression(jc.getInFluxExpression(), VariableDomain.VARIABLEDOMAIN_MEMBRANE);
            String infix = replaceVolumeVariable(getSimulationTask(), msd, flux);
            printWriter.println(BoundaryConditionType.NEUMANN_STRING.toUpperCase() + " " + msd.getInsideCompartment().getName() + " " + infix + ";");
        }
        if (jc.getVariable().getDomain() == null || jc.getVariable().getDomain().getName().equals(msd.getOutsideCompartment().getName())) {
            // outflux
            Expression flux = subsituteExpression(jc.getOutFluxExpression(), VariableDomain.VARIABLEDOMAIN_MEMBRANE);
            String infix = replaceVolumeVariable(simTask, msd, flux);
            printWriter.println(BoundaryConditionType.NEUMANN_STRING.toUpperCase() + " " + msd.getOutsideCompartment().getName() + " " + infix + ";");
        }
        printWriter.println("JUMP_CONDITION_END");
        printWriter.println();
    }
}
Also used : JumpCondition(cbit.vcell.math.JumpCondition) PdeEquation(cbit.vcell.math.PdeEquation) BoundaryConditionValue(cbit.vcell.math.PdeEquation.BoundaryConditionValue) Expression(cbit.vcell.parser.Expression) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) MathException(cbit.vcell.math.MathException) MeasureEquation(cbit.vcell.math.MeasureEquation) PdeEquation(cbit.vcell.math.PdeEquation) VolumeRegionEquation(cbit.vcell.math.VolumeRegionEquation) MembraneRegionEquation(cbit.vcell.math.MembraneRegionEquation) Equation(cbit.vcell.math.Equation) BoundaryConditionSpec(cbit.vcell.math.SubDomain.BoundaryConditionSpec)

Aggregations

Equation (cbit.vcell.math.Equation)32 PdeEquation (cbit.vcell.math.PdeEquation)21 OdeEquation (cbit.vcell.math.OdeEquation)20 SubDomain (cbit.vcell.math.SubDomain)17 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)16 Variable (cbit.vcell.math.Variable)16 Expression (cbit.vcell.parser.Expression)16 Vector (java.util.Vector)14 MembraneRegionEquation (cbit.vcell.math.MembraneRegionEquation)13 MembraneSubDomain (cbit.vcell.math.MembraneSubDomain)13 VolVariable (cbit.vcell.math.VolVariable)13 MathDescription (cbit.vcell.math.MathDescription)12 VolumeRegionEquation (cbit.vcell.math.VolumeRegionEquation)12 MathException (cbit.vcell.math.MathException)10 Constant (cbit.vcell.math.Constant)8 JumpCondition (cbit.vcell.math.JumpCondition)8 MemVariable (cbit.vcell.math.MemVariable)8 MembraneRegionVariable (cbit.vcell.math.MembraneRegionVariable)8 SimulationSymbolTable (cbit.vcell.solver.SimulationSymbolTable)8 Function (cbit.vcell.math.Function)7