Search in sources :

Example 36 with MathException

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

the class SundialsSolver method createStateVariables.

/*
	This method was created in Visual Age
*/
private StateVariable[] createStateVariables() throws MathException, ExpressionException {
    Vector<StateVariable> stateVariables = new Vector<StateVariable>();
    // get Ode's from MathDescription and create ODEStateVariables
    SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
    MathDescription mathDescription = simSymbolTable.getSimulation().getMathDescription();
    Enumeration<Equation> enum1 = ((SubDomain) mathDescription.getSubDomains().nextElement()).getEquations();
    while (enum1.hasMoreElements()) {
        Equation equation = (Equation) enum1.nextElement();
        if (equation instanceof OdeEquation) {
            stateVariables.addElement(new ODEStateVariable((OdeEquation) equation, simSymbolTable));
        } else {
            throw new MathException("encountered non-ode equation, unsupported");
        }
    }
    // Get sensitivity variables
    Variable[] variables = simSymbolTable.getVariables();
    Vector<SensVariable> sensVariables = new Vector<SensVariable>();
    if (getSensitivityParameter() != null) {
        for (int i = 0; i < variables.length; i++) {
            if (variables[i] instanceof VolVariable) {
                VolVariable volVariable = (VolVariable) variables[i];
                SensVariable sv = new SensVariable(volVariable, getSensitivityParameter());
                sensVariables.addElement(sv);
            }
        }
    }
    if (rateSensitivity == null) {
        rateSensitivity = new RateSensitivity(mathDescription, mathDescription.getSubDomains().nextElement());
    }
    if (jacobian == null) {
        jacobian = new Jacobian(mathDescription, mathDescription.getSubDomains().nextElement());
    }
    for (int v = 0; v < sensVariables.size(); v++) {
        stateVariables.addElement(new SensStateVariable((SensVariable) sensVariables.elementAt(v), rateSensitivity, jacobian, sensVariables, simSymbolTable));
    }
    if (stateVariables.size() == 0) {
        throw new MathException("there are no equations defined");
    }
    StateVariable[] stateVars = (StateVariable[]) BeanUtils.getArray(stateVariables, StateVariable.class);
    return (stateVars);
}
Also used : Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) MathDescription(cbit.vcell.math.MathDescription) VolVariable(cbit.vcell.math.VolVariable) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) OdeEquation(cbit.vcell.math.OdeEquation) Equation(cbit.vcell.math.Equation) SubDomain(cbit.vcell.math.SubDomain) OdeEquation(cbit.vcell.math.OdeEquation) MathException(cbit.vcell.math.MathException) Vector(java.util.Vector)

Example 37 with MathException

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

the class SundialsSolver method getODESolverResultSet.

/**
 * This method was created in VisualAge.
 * @return double[]
 * @param vectorIndex int
 */
public ODESolverResultSet getODESolverResultSet() {
    // 
    // read .ida file
    // 
    ODESolverResultSet odeSolverResultSet = getStateVariableResultSet();
    if (odeSolverResultSet == null) {
        return null;
    }
    // 
    // add appropriate Function columns to result set
    // 
    SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
    Function[] functions = simSymbolTable.getFunctions();
    for (int i = 0; i < functions.length; i++) {
        if (SimulationSymbolTable.isFunctionSaved(functions[i])) {
            Expression exp1 = new Expression(functions[i].getExpression());
            try {
                exp1 = simSymbolTable.substituteFunctions(exp1);
            } catch (MathException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("Substitute function failed on function " + functions[i].getName() + " " + e.getMessage());
            } catch (ExpressionException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("Substitute function failed on function " + functions[i].getName() + " " + e.getMessage());
            }
            try {
                FunctionColumnDescription cd = new FunctionColumnDescription(exp1.flatten(), functions[i].getName(), null, functions[i].getName(), false);
                odeSolverResultSet.addFunctionColumn(cd);
            } catch (ExpressionException e) {
                e.printStackTrace(System.out);
            }
        }
    }
    if (getSensitivityParameter() != null) {
        try {
            if (odeSolverResultSet.findColumn(getSensitivityParameter().getName()) == -1) {
                FunctionColumnDescription fcd = new FunctionColumnDescription(new Expression(getSensitivityParameter().getConstantValue()), getSensitivityParameter().getName(), null, getSensitivityParameter().getName(), false);
                odeSolverResultSet.addFunctionColumn(fcd);
            }
            Variable[] variables = simSymbolTable.getVariables();
            StateVariable[] stateVars = createStateVariables();
            for (int i = 0; i < variables.length; i++) {
                if (variables[i] instanceof Function && SimulationSymbolTable.isFunctionSaved((Function) variables[i])) {
                    Function depSensFunction = (Function) variables[i];
                    Expression depSensFnExpr = new Expression(depSensFunction.getExpression());
                    depSensFnExpr = simSymbolTable.substituteFunctions(depSensFnExpr);
                    depSensFnExpr = getFunctionSensitivity(depSensFnExpr, getSensitivityParameter(), stateVars);
                    // depSensFnExpr = depSensFnExpr.flatten(); 	// already bound and flattened in getFunctionSensitivity, no need here.....
                    String depSensFnName = new String("sens_" + depSensFunction.getName() + "_wrt_" + getSensitivityParameter().getName());
                    if (depSensFunction != null) {
                        FunctionColumnDescription cd = new FunctionColumnDescription(depSensFnExpr.flatten(), depSensFnName, getSensitivityParameter().getName(), depSensFnName, false);
                        odeSolverResultSet.addFunctionColumn(cd);
                    }
                }
            }
        } catch (MathException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error adding function to resultSet: " + e.getMessage());
        } catch (ExpressionException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error adding function to resultSet: " + e.getMessage());
        }
    }
    return odeSolverResultSet;
}
Also used : Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) ExpressionException(cbit.vcell.parser.ExpressionException) Function(cbit.vcell.math.Function) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 38 with MathException

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

the class CVodeFileWriter method writeEquations.

/**
 * Insert the method's description here.
 * Creation date: (3/8/00 10:31:52 PM)
 */
protected String writeEquations(HashMap<Discontinuity, String> discontinuityNameMap) throws MathException, ExpressionException {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < getStateVariableCount(); i++) {
        StateVariable stateVar = getStateVariable(i);
        Expression rateExpr = new Expression(stateVar.getRateExpression());
        Expression initExpr = new Expression(stateVar.getInitialRateExpression());
        initExpr = MathUtilities.substituteFunctions(initExpr, varsSymbolTable).flatten();
        initExpr.substituteInPlace(new Expression("t"), new Expression(0.0));
        String[] symbols0 = initExpr.getSymbols();
        if (symbols0 != null) {
            for (String symbol : symbols0) {
                SymbolTableEntry ste = initExpr.getSymbolBinding(symbol);
                if (!ste.equals(ReservedVariable.X) && !ste.equals(ReservedVariable.Y) && !ste.equals(ReservedVariable.Z)) {
                    throw new MathException("Variables are not allowed in initial condition.\nInitial condition of variable:" + stateVar.getVariable().getName() + " has variable(" + symbol + ") in expression.");
                }
            }
        }
        rateExpr = MathUtilities.substituteFunctions(rateExpr, varsSymbolTable).flatten();
        Vector<Discontinuity> v = rateExpr.getDiscontinuities();
        for (Discontinuity od : v) {
            od = getSubsitutedAndFlattened(od, varsSymbolTable);
            String dname = discontinuityNameMap.get(od);
            if (dname == null) {
                dname = ROOT_VARIABLE_PREFIX + discontinuityNameMap.size();
                discontinuityNameMap.put(od, dname);
            }
            rateExpr.substituteInPlace(od.getDiscontinuityExp(), new Expression("(" + dname + "==1)"));
        }
        sb.append("ODE " + stateVar.getVariable().getName() + " INIT " + initExpr.flatten().infix() + ";\n\t RATE " + rateExpr.flatten().infix() + ";\n");
    }
    return sb.toString();
}
Also used : Discontinuity(cbit.vcell.parser.Discontinuity) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException)

Example 39 with MathException

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

the class AdamsMoultonFiveSolver method integrate.

/**
 * This method was created by a SmartGuide.
 *  THIS HAS NOT BEEN UPDATED LIKE ODEIntegrator.integrate () and
 *  RungeKuttaFehlbergIntegrator.integrate()...
 */
protected void integrate() throws SolverException, UserStopException, IOException {
    try {
        SolverTaskDescription taskDescription = simTask.getSimulation().getSolverTaskDescription();
        double timeStep = taskDescription.getTimeStep().getDefaultTimeStep();
        fieldCurrentTime = taskDescription.getTimeBounds().getStartingTime();
        // before computation begins, settle fast equilibrium
        if (getFastAlgebraicSystem() != null) {
            fieldValueVectors.copyValues(0, 1);
            getFastAlgebraicSystem().initVars(getValueVector(0), getValueVector(1));
            getFastAlgebraicSystem().solveSystem(getValueVector(0), getValueVector(1));
            fieldValueVectors.copyValues(1, 0);
        }
        // check for failure
        check(getValueVector(0));
        // Evaluate
        for (int i = 0; i < getStateVariableCount(); i++) {
            f[0][getVariableIndex(i)] = evaluate(getValueVector(0), i);
        }
        // check for failure
        check(getValueVector(0));
        updateResultSet();
        // 
        int iteration = 0;
        while (fieldCurrentTime < taskDescription.getTimeBounds().getEndingTime()) {
            checkForUserStop();
            if (iteration < 3) {
                // Take Runge-Kutta step...
                prep(fieldCurrentTime, timeStep);
            } else {
                // Take Adams-Moulton step...
                step(fieldCurrentTime, timeStep);
            }
            // update (old = new)
            fieldValueVectors.copyValuesDown();
            // compute fast system
            if (getFastAlgebraicSystem() != null) {
                fieldValueVectors.copyValues(0, 1);
                getFastAlgebraicSystem().initVars(getValueVector(0), getValueVector(1));
                getFastAlgebraicSystem().solveSystem(getValueVector(0), getValueVector(1));
                fieldValueVectors.copyValues(1, 0);
            }
            // check for failure
            check(getValueVector(0));
            if (iteration < 3) {
                for (int i = 0; i < getStateVariableCount(); i++) {
                    f[iteration + 1][getVariableIndex(i)] = evaluate(getValueVector(0), i);
                }
                // check for failure
                check(f[iteration + 1]);
            } else {
                // Evaluate
                for (int i = 0; i < getStateVariableCount(); i++) {
                    f[4][getVariableIndex(i)] = evaluate(getValueVector(0), i);
                }
                // check for failure
                check(f[4]);
                shiftWorkArrays();
            }
            // fieldCurrentTime += timeStep;
            iteration++;
            fieldCurrentTime = taskDescription.getTimeBounds().getStartingTime() + iteration * timeStep;
            // store results if it coincides with a save interval
            if (taskDescription.getOutputTimeSpec().isDefault()) {
                int keepEvery = ((DefaultOutputTimeSpec) taskDescription.getOutputTimeSpec()).getKeepEvery();
                if ((iteration % keepEvery) == 0)
                    updateResultSet();
            }
        }
        // store last time point
        if (taskDescription.getOutputTimeSpec().isDefault()) {
            int keepEvery = ((DefaultOutputTimeSpec) taskDescription.getOutputTimeSpec()).getKeepEvery();
            if ((iteration % keepEvery) == 0)
                updateResultSet();
        }
    } catch (ExpressionException expressionException) {
        throw new SolverException(expressionException.getMessage());
    } catch (MathException mathException) {
        throw new SolverException(mathException.getMessage());
    }
}
Also used : MathException(cbit.vcell.math.MathException) SolverTaskDescription(cbit.vcell.solver.SolverTaskDescription) SolverException(cbit.vcell.solver.SolverException) DefaultOutputTimeSpec(cbit.vcell.solver.DefaultOutputTimeSpec) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 40 with MathException

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

the class XmlReader method getVarIniCount.

/**
 * This method return a VarIniCondition object from a XML element.
 * Creation date: (7/24/2006 5:26:05 PM)
 * @return cbit.vcell.math.VarIniCondition
 * @param param org.jdom.Element
 * @exception cbit.vcell.xml.XmlParseException The exception description.
 */
private VarIniCondition getVarIniCount(Element param, MathDescription md) throws XmlParseException, MathException, ExpressionException {
    // retrieve values
    Expression exp = unMangleExpression(param.getText());
    String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
    Variable var = md.getVariable(name);
    if (var == null) {
        throw new MathFormatException("variable " + name + " not defined");
    }
    if (!(var instanceof StochVolVariable)) {
        throw new MathFormatException("variable " + name + " not a Stochastic Volume Variable");
    }
    try {
        VarIniCondition varIni = new VarIniCount(var, exp);
        return varIni;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : VarIniCondition(cbit.vcell.math.VarIniCondition) FilamentVariable(cbit.vcell.math.FilamentVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) PointVariable(cbit.vcell.math.PointVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) Expression(cbit.vcell.parser.Expression) MathFormatException(cbit.vcell.math.MathFormatException) VarIniCount(cbit.vcell.math.VarIniCount) StochVolVariable(cbit.vcell.math.StochVolVariable) GeometryException(cbit.vcell.geometry.GeometryException) MathFormatException(cbit.vcell.math.MathFormatException) MappingException(cbit.vcell.mapping.MappingException) PropertyVetoException(java.beans.PropertyVetoException) ImageException(cbit.image.ImageException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ModelException(cbit.vcell.model.ModelException) DataConversionException(org.jdom.DataConversionException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException)

Aggregations

MathException (cbit.vcell.math.MathException)81 ExpressionException (cbit.vcell.parser.ExpressionException)49 Expression (cbit.vcell.parser.Expression)41 Variable (cbit.vcell.math.Variable)34 VolVariable (cbit.vcell.math.VolVariable)27 PropertyVetoException (java.beans.PropertyVetoException)20 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)19 MathDescription (cbit.vcell.math.MathDescription)18 MemVariable (cbit.vcell.math.MemVariable)18 DataAccessException (org.vcell.util.DataAccessException)17 Vector (java.util.Vector)16 Element (org.jdom.Element)16 IOException (java.io.IOException)15 Function (cbit.vcell.math.Function)14 ArrayList (java.util.ArrayList)14 MappingException (cbit.vcell.mapping.MappingException)13 SimulationSymbolTable (cbit.vcell.solver.SimulationSymbolTable)13 Constant (cbit.vcell.math.Constant)12 MembraneParticleVariable (cbit.vcell.math.MembraneParticleVariable)12 ParticleVariable (cbit.vcell.math.ParticleVariable)12