Search in sources :

Example 6 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class NFsimXMLWriter method getListOfFunctions.

private static Element getListOfFunctions(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
    Element listOfParametersElement = new Element("ListOfFunctions");
    for (Variable var : simulationSymbolTable.getVariables()) {
        Double value = null;
        if (var instanceof Constant || var instanceof Function) {
            Expression valExpression = var.getExpression();
            Expression substitutedValExpr = null;
            try {
                substitutedValExpr = simulationSymbolTable.substituteFunctions(valExpression);
            } catch (Exception e) {
                e.printStackTrace(System.out);
                throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
            }
            try {
                value = substitutedValExpr.evaluateConstant();
            } catch (ExpressionException e) {
                System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
            }
            Element functionElement = new Element("Function");
            functionElement.setAttribute("id", var.getName());
            if (value != null) {
                // parameter, see getListOfParameters() above
                continue;
            } else {
                Element listOfReferencesElement = new Element("ListOfReferences");
                String[] references = valExpression.getSymbols();
                for (int i = 0; i < references.length; i++) {
                    String reference = references[i];
                    Element referenceElement = new Element("Reference");
                    referenceElement.setAttribute("name", reference);
                    Variable referenceVariable = simulationSymbolTable.getVariable(reference);
                    Double referenceValue = null;
                    Expression referenceExpression = referenceVariable.getExpression();
                    Expression substitutedReferenceExpression = null;
                    if (referenceExpression != null) {
                        try {
                            substitutedReferenceExpression = simulationSymbolTable.substituteFunctions(referenceExpression);
                        } catch (Exception e) {
                            e.printStackTrace(System.out);
                            throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
                        }
                        try {
                            referenceValue = substitutedReferenceExpression.evaluateConstant();
                        } catch (ExpressionException e) {
                            System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
                        }
                    }
                    if (referenceVariable instanceof ParticleObservable) {
                        referenceElement.setAttribute("type", "Observable");
                    } else if (referenceVariable instanceof Function) {
                        if (referenceValue != null) {
                            referenceElement.setAttribute("type", "ConstantExpression");
                        } else {
                            referenceElement.setAttribute("type", "Function");
                        }
                    } else {
                        // constant
                        referenceElement.setAttribute("type", "ConstantExpression");
                    }
                    listOfReferencesElement.addContent(referenceElement);
                }
                functionElement.addContent(listOfReferencesElement);
                Element expressionElement = new Element("Expression");
                String functionExpression = valExpression.infix();
                expressionElement.setText(functionExpression);
                functionElement.addContent(expressionElement);
            }
            listOfParametersElement.addContent(functionElement);
        }
    }
    return listOfParametersElement;
}
Also used : Variable(cbit.vcell.math.Variable) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant) Constant(cbit.vcell.math.Constant) Element(org.jdom.Element) SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) ExpressionException(cbit.vcell.parser.ExpressionException) Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) SolverException(cbit.vcell.solver.SolverException) ParticleObservable(cbit.vcell.math.ParticleObservable)

Example 7 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class NFsimXMLWriter method getListOfSpecies.

private static Element getListOfSpecies(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
    CompartmentSubDomain compartmentSubDomain = (CompartmentSubDomain) mathDesc.getSubDomains().nextElement();
    // 
    // NFsim expects a list of seed species.  These are concrete species patterns that have a "ParticleProperties" element defined (for initial conditions).
    // 
    Element listOfSpeciesElement = new Element("ListOfSpecies");
    for (int speciesIndex = 0; speciesIndex < compartmentSubDomain.getParticleProperties().size(); speciesIndex++) {
        // seedSpecies.getSpeciesPattern().resolveBonds();
        ParticleProperties particleProperties = compartmentSubDomain.getParticleProperties().get(speciesIndex);
        Element speciesElement = new Element("Species");
        String speciesID = "S" + (speciesIndex + 1);
        ParticleSpeciesPattern seedSpecies = (ParticleSpeciesPattern) particleProperties.getVariable();
        speciesElement.setAttribute("id", speciesID);
        speciesElement.setAttribute("name", seedSpecies.getName());
        List<ParticleInitialCondition> particleInitialConditions = particleProperties.getParticleInitialConditions();
        if (particleInitialConditions.size() != 1) {
            throw new SolverException("multiple particle initial conditions not expected for " + ParticleSpeciesPattern.class.getSimpleName() + " " + seedSpecies.getName());
        }
        // the initial conditions must be a count in math (ParticleInitialConditionCount)
        if (!(particleInitialConditions.get(0) instanceof ParticleInitialConditionCount)) {
            throw new SolverException("expecting initial count for " + ParticleSpeciesPattern.class.getSimpleName() + " " + seedSpecies.getName());
        }
        ParticleInitialConditionCount initialCount = (ParticleInitialConditionCount) particleInitialConditions.get(0);
        try {
            double value = evaluateConstant(initialCount.getCount(), simulationSymbolTable);
            Integer maxMoleculesPerType = simulationSymbolTable.getSimulation().getSolverTaskDescription().getNFSimSimulationOptions().getMaxMoleculesPerType();
            if (maxMoleculesPerType == null) {
                maxMoleculesPerType = NFsimSimulationOptions.DefaultMaxMoleculesPerSpecies;
            }
            if (maxMoleculesPerType.doubleValue() < value) {
                String eMessage = "The Initial count for Species '" + seedSpecies.getName() + "' is " + BigDecimal.valueOf(value).toBigInteger();
                eMessage += ", which is higher than the limit of " + maxMoleculesPerType + ".\n";
                eMessage += "Please do one of the following: \n- reduce the Initial Condition value for this Species or reduce the compartment size\n";
                eMessage += "- increase the maximal number of Molecules per Molecular Type in the Advanced Solver Options panel.";
                throw new RuntimeException(eMessage);
            }
            speciesElement.setAttribute("concentration", Double.toString(value));
        } catch (ExpressionException | MathException e) {
            e.printStackTrace();
            throw new SolverException("error processing initial count of " + ParticleSpeciesPattern.class.getSimpleName() + " " + seedSpecies.getName() + ": " + e.getMessage());
        }
        HashMap<Bond, BondSites> bondSiteMapping = new HashMap<Bond, BondSites>();
        Element listOfMoleculesElement = getListOfMolecules(speciesID, seedSpecies, bondSiteMapping);
        speciesElement.addContent(listOfMoleculesElement);
        if (bondSiteMapping.size() > 0) {
            Element listOfBondsElement = getListOfBonds(bondSiteMapping);
            speciesElement.addContent(listOfBondsElement);
        }
        listOfSpeciesElement.addContent(speciesElement);
    }
    return listOfSpeciesElement;
}
Also used : HashMap(java.util.HashMap) Element(org.jdom.Element) ExpressionException(cbit.vcell.parser.ExpressionException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) ParticleInitialCondition(cbit.vcell.math.ParticleProperties.ParticleInitialCondition) MathException(cbit.vcell.math.MathException) ParticleProperties(cbit.vcell.math.ParticleProperties) ParticleSpeciesPattern(cbit.vcell.math.ParticleSpeciesPattern) VolumeParticleSpeciesPattern(cbit.vcell.math.VolumeParticleSpeciesPattern) SolverException(cbit.vcell.solver.SolverException) ParticleInitialConditionCount(cbit.vcell.math.ParticleProperties.ParticleInitialConditionCount)

Example 8 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class NFsimXMLWriter method getListOfParameters.

private static Element getListOfParameters(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
    Element listOfParametersElement = new Element("ListOfParameters");
    for (Variable var : simulationSymbolTable.getVariables()) {
        Double value = null;
        if (var instanceof Constant || var instanceof Function) {
            Expression valExpression = var.getExpression();
            Expression substitutedValExpr = null;
            try {
                substitutedValExpr = simulationSymbolTable.substituteFunctions(valExpression);
            } catch (Exception e) {
                e.printStackTrace(System.out);
                throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
            }
            try {
                value = substitutedValExpr.evaluateConstant();
            } catch (ExpressionException e) {
                System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
            }
            Element parameterElement = new Element("Parameter");
            parameterElement.setAttribute("id", var.getName());
            if (value != null) {
                parameterElement.setAttribute("type", "Constant");
                parameterElement.setAttribute("value", value.toString());
            } else {
                // function, see getListOfFunctions() below
                continue;
            }
            listOfParametersElement.addContent(parameterElement);
        }
    }
    return listOfParametersElement;
}
Also used : Function(cbit.vcell.math.Function) Variable(cbit.vcell.math.Variable) Expression(cbit.vcell.parser.Expression) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant) Constant(cbit.vcell.math.Constant) Element(org.jdom.Element) SolverException(cbit.vcell.solver.SolverException) SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 9 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class SmoldynFileWriter method writeReactions.

private void writeReactions() throws ExpressionException, MathException {
    printWriter.println("# reactions");
    Enumeration<SubDomain> subdomains = mathDesc.getSubDomains();
    while (subdomains.hasMoreElements()) {
        SubDomain subdomain = subdomains.nextElement();
        for (ParticleJumpProcess pjp : subdomain.getParticleJumpProcesses()) {
            ArrayList<Variable> reactants = new ArrayList<Variable>();
            ArrayList<Variable> products = new ArrayList<Variable>();
            for (Action a : pjp.getActions().toArray(new Action[pjp.getActions().size()])) {
                if (a.getOperation().equals(Action.ACTION_CREATE)) {
                    products.add(a.getVar());
                } else if (a.getOperation().equals(Action.ACTION_DESTROY)) {
                    reactants.add(a.getVar());
                }
            }
            Expression rateDefinition = null;
            JumpProcessRateDefinition jprd = pjp.getParticleRateDefinition();
            if (jprd instanceof MacroscopicRateConstant) {
                rateDefinition = subsituteFlatten(((MacroscopicRateConstant) jprd).getExpression());
            } else if (jprd instanceof InteractionRadius) {
                rateDefinition = subsituteFlatten(((InteractionRadius) jprd).getExpression());
            } else {
                new RuntimeException("The jump process rate definition is not supported");
            }
            if (rateDefinition.isZero()) {
                continue;
            }
            if (mathDesc.isSpatialHybrid()) {
                String[] symbols = rateDefinition.getSymbols();
                if (symbols != null) {
                    if (subdomain instanceof MembraneSubDomain) {
                        rateDefinition = new Expression(FiniteVolumeFileWriter.replaceVolumeVariable(getSimulationTask(), (MembraneSubDomain) subdomain, rateDefinition));
                    }
                }
            } else {
                try {
                    rateDefinition.evaluateConstant();
                } catch (ExpressionException ex) {
                    throw new ExpressionException("reaction rate for jump process " + pjp.getName() + " is not a constant. Constants are required for all reaction rates.");
                }
            }
            // Smoldyn takes maximum 2nd order reaction.
            if (reactants.size() > 2) {
                throw new MathException("VCell spatial stochastic models support up to 2nd order reactions. \n" + "The reaction:" + pjp.getName() + " has more than 2 reactants.");
            }
            if (products.size() > 2) {
                throw new MathException("VCell spatial stochastic models support up to 2nd order reactions. \n" + "The reaction:" + pjp.getName() + " has more than 2 products.");
            }
            String rateDefinitionStr = simulation.getMathDescription().isSpatialHybrid() ? rateDefinition.infix() + ";" : rateDefinition.evaluateConstant() + "";
            if (subdomain instanceof CompartmentSubDomain) {
                // 0th order reaction, product limited to one and we'll let the reaction know where it happens
                if (reactants.size() == 0 && products.size() == 1) {
                    printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_cmpt + " " + subdomain.getName() + " " + pjp.getName() + " ");
                } else {
                    printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction + " " + /* + subdomain.getName() + " "*/
                    pjp.getName() + " ");
                }
                writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
            } else if (subdomain instanceof MembraneSubDomain) {
                // 0th order reaction, product limited to one and it can be on mem or in vol
                if (reactants.size() == 0 && products.size() == 1) {
                    printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
                    writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
                } else // consuming of a species to nothing, limited to one reactant
                if (reactants.size() == 1 && products.size() == 0) {
                    if (// consuming a mem species in mem reaction
                    getMembraneVariableCount(reactants) == 1) {
                        printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
                        writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
                    } else // it equals to adsorption, species A from vol adsorbed to mem as again species A, and then we kill the speceis A on mem.
                    if (getVolumeVariableCount(reactants) == 1) {
                        writeRateTransitionCommand(reactants, products, subdomain, rateDefinitionStr);
                        String speciesName = reactants.get(0).getName();
                        String killMolCmd = "cmd " + SmoldynVCellMapper.SmoldynKeyword.E + " " + SmoldynVCellMapper.SmoldynKeyword.killmol + " " + speciesName + "(" + SmoldynKeyword.up + ")";
                        killMolCommands.add(killMolCmd);
                    }
                } else // Use rate command for any membrane reactions with 1 reactant and 1 product
                if ((reactants.size() == 1) && (products.size() == 1)) {
                    // Membrane reaction (1 react to 1 product).
                    if (getMembraneVariableCount(products) == 1 && getMembraneVariableCount(reactants) == 1) {
                        printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
                        writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
                    } else // Other single molecular reactions
                    {
                        writeRateTransitionCommand(reactants, products, subdomain, rateDefinitionStr);
                    }
                } else // membrane reactions which are not one to one, or 0th order, or consuming species
                {
                    if (// membrane reaction has one membrane bound reactant
                    (getMembraneVariableCount(reactants) == 1)) {
                        printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
                        writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
                    } else if (// bimolecular membrane reaction
                    getMembraneVariableCount(reactants) == 2) {
                        if (jprd instanceof InteractionRadius) {
                            printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
                            writeReactionByInteractionRadius(reactants, products, subdomain, rateDefinitionStr, pjp.getName());
                        } else {
                            // throw new MathException("Error with reaction: " + pjp.getName() + ".\nVCell Spatial stochastic modeling requires macroscopic or microscopic kinetics for bimolecular membrane reactions.");
                            printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
                            writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
                        }
                    } else if (getMembraneVariableCount(reactants) == 0) {
                        throw new MathException("Error with reaction: " + pjp.getName() + ".\nIn VCell spatial stochastic modeling, the membrane reaction requires at least one membrane bound reactant.");
                    }
                }
            }
        }
    }
    printWriter.println();
}
Also used : Action(cbit.vcell.math.Action) JumpProcessRateDefinition(cbit.vcell.math.JumpProcessRateDefinition) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) ReservedVariable(cbit.vcell.math.ReservedVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) Variable(cbit.vcell.math.Variable) InteractionRadius(cbit.vcell.math.InteractionRadius) ParticleJumpProcess(cbit.vcell.math.ParticleJumpProcess) ArrayList(java.util.ArrayList) ExpressionException(cbit.vcell.parser.ExpressionException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant)

Example 10 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class SmoldynFileWriter method writeDrifts.

private void writeDrifts() throws ExpressionBindingException, ExpressionException, MathException {
    // writer diffusion properties
    printWriter.println("# drift properties");
    Enumeration<SubDomain> subDomainEnumeration = mathDesc.getSubDomains();
    while (subDomainEnumeration.hasMoreElements()) {
        SubDomain subDomain = subDomainEnumeration.nextElement();
        List<ParticleProperties> particlePropertiesList = subDomain.getParticleProperties();
        for (ParticleProperties pp : particlePropertiesList) {
            String variableName = null;
            if (subDomain instanceof MembraneSubDomain) {
                variableName = getVariableName(pp.getVariable(), subDomain);
            } else {
                variableName = getVariableName(pp.getVariable(), null);
            }
            try {
                double driftX = 0.0;
                if (pp.getDriftX() != null) {
                    driftX = subsituteFlattenToConstant(pp.getDriftX());
                }
                double driftY = 0.0;
                if (pp.getDriftY() != null) {
                    driftY = subsituteFlattenToConstant(pp.getDriftY());
                }
                double driftZ = 0.0;
                if (pp.getDriftZ() != null) {
                    driftZ = subsituteFlattenToConstant(pp.getDriftZ());
                }
                printWriter.println(SmoldynVCellMapper.SmoldynKeyword.drift + " " + variableName + " " + driftX + " " + driftY + " " + driftZ);
            } catch (NotAConstantException ex) {
                throw new ExpressionException("diffusion coefficient for variable " + variableName + " is not a constant. Constants are required for all diffusion coefficients");
            }
        }
    }
    printWriter.println();
}
Also used : CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) ParticleProperties(cbit.vcell.math.ParticleProperties) ExpressionException(cbit.vcell.parser.ExpressionException)

Aggregations

ExpressionException (cbit.vcell.parser.ExpressionException)199 Expression (cbit.vcell.parser.Expression)138 MathException (cbit.vcell.math.MathException)58 PropertyVetoException (java.beans.PropertyVetoException)51 DataAccessException (org.vcell.util.DataAccessException)34 ArrayList (java.util.ArrayList)32 Variable (cbit.vcell.math.Variable)30 IOException (java.io.IOException)29 Element (org.jdom.Element)26 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)25 MappingException (cbit.vcell.mapping.MappingException)24 Function (cbit.vcell.math.Function)24 Vector (java.util.Vector)24 ModelException (cbit.vcell.model.ModelException)23 SolverException (cbit.vcell.solver.SolverException)23 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)22 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)21 Constant (cbit.vcell.math.Constant)20 MathDescription (cbit.vcell.math.MathDescription)19 Structure (cbit.vcell.model.Structure)18