Search in sources :

Example 46 with Function

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

the class SimulationSymbolTable method getLocalFunction.

/**
 * Insert the method's description here.
 * Creation date: (6/6/2001 7:52:15 PM)
 * @return cbit.vcell.math.Function
 * @param functionName java.lang.String
 */
private Function getLocalFunction(Function referenceFunction) throws ExpressionException {
    if (localVariableHash == null) {
        localVariableHash = new HashMap<String, Variable>();
    }
    Variable var = localVariableHash.get(referenceFunction.getName());
    if (var instanceof Function) {
        Function localFunction = (Function) var;
        if (localFunction.compareEqual(referenceFunction)) {
            // localFunction.bind(this); // update bindings to latest mathOverrides
            return localFunction;
        }
    } else if (var != null) {
        throw new RuntimeException("Variable " + var + " expected to be a Function");
    }
    // 
    // if local Function not found, create new one, bind it to the Simulation (which ensures MathOverrides), and add to list
    // 
    Function newLocalFunction = new Function(referenceFunction.getName(), referenceFunction.getExpression(), referenceFunction.getDomain());
    // newLocalFunction.bind(this);
    localVariableHash.put(newLocalFunction.getName(), newLocalFunction);
    return newLocalFunction;
}
Also used : Function(cbit.vcell.math.Function) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) ReservedVariable(cbit.vcell.math.ReservedVariable) PointVariable(cbit.vcell.math.PointVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) InsideVariable(cbit.vcell.math.InsideVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) FilamentVariable(cbit.vcell.math.FilamentVariable) VolVariable(cbit.vcell.math.VolVariable)

Example 47 with Function

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

the class SimulationSymbolTable method getVariables.

/**
 * Insert the method's description here.
 * Creation date: (5/25/01 11:34:08 AM)
 * @return cbit.vcell.math.Variable[]
 */
public Variable[] getVariables() {
    Vector<Variable> varList = new Vector<Variable>();
    // 
    // get all variables from MathDescription, but replace MathOverrides
    // 
    Enumeration<Variable> enum1 = simulation.getMathDescription().getVariables();
    while (enum1.hasMoreElements()) {
        Variable mathDescriptionVar = enum1.nextElement();
        // 
        if (mathDescriptionVar instanceof Constant) {
            try {
                Constant overriddenConstant = getLocalConstant((Constant) mathDescriptionVar);
                varList.addElement(overriddenConstant);
            } catch (ExpressionException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("local Constant " + mathDescriptionVar.getName() + " not found for Simulation");
            }
        // 
        // replace all Functions with local Functions that are bound to this Simulation
        // 
        } else if (mathDescriptionVar instanceof Function) {
            try {
                Function overriddenFunction = getLocalFunction((Function) mathDescriptionVar);
                varList.addElement(overriddenFunction);
            } catch (ExpressionException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("local Function " + mathDescriptionVar.getName() + " not found for Simulation");
            }
        // 
        // pass all other Variables through
        // 
        } else {
            varList.addElement(mathDescriptionVar);
        }
    }
    Variable[] variables = (Variable[]) BeanUtils.getArray(varList, Variable.class);
    return variables;
}
Also used : Function(cbit.vcell.math.Function) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) ReservedVariable(cbit.vcell.math.ReservedVariable) PointVariable(cbit.vcell.math.PointVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) InsideVariable(cbit.vcell.math.InsideVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) FilamentVariable(cbit.vcell.math.FilamentVariable) VolVariable(cbit.vcell.math.VolVariable) Constant(cbit.vcell.math.Constant) Vector(java.util.Vector) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 48 with Function

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

the class SundialsSolver method createFunctionList.

public Vector<AnnotatedFunction> createFunctionList() {
    // 
    // add appropriate Function columns to result set
    // 
    Vector<AnnotatedFunction> funcList = super.createFunctionList();
    SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
    if (getSensitivityParameter() != null) {
        try {
            AnnotatedFunction saf = new AnnotatedFunction(getSensitivityParameter().getName(), new Expression(getSensitivityParameter().getConstantValue()), getSensitivityParameter().getDomain(), "", VariableType.NONSPATIAL, FunctionCategory.PREDEFINED);
            if (!funcList.contains(saf)) {
                funcList.add(saf);
            }
            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) {
                        AnnotatedFunction af = new AnnotatedFunction(depSensFnName, depSensFnExpr.flatten(), variables[i].getDomain(), "", VariableType.NONSPATIAL, FunctionCategory.PREDEFINED);
                        funcList.add(af);
                    }
                }
            }
        } 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 funcList;
}
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) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 49 with Function

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

the class OdeFileWriter method createSymbolTable.

/**
 * OdeFileCoder constructor comment.
 * @throws Exception
 */
private void createSymbolTable() throws Exception {
    // 
    // Create symbol table for binding sensitivity variable expressions. (Cannot bind to simulation,
    // since it does not have the sensitivity variables corresponding to the volume variables).
    // 
    varsSymbolTable = new VariableSymbolTable();
    // SymbolTableEntry.index doesn't matter ... just code generating binding by var names not index.
    varsSymbolTable.addVar(ReservedVariable.TIME);
    int count = 0;
    Variable[] variables = simTask.getSimulationJob().getSimulationSymbolTable().getVariables();
    for (int i = 0; i < variables.length; i++) {
        if (variables[i] instanceof VolVariable) {
            VolVariable vVar = (VolVariable) variables[i];
            vVar.setIndex(count);
            varsSymbolTable.addVar(vVar);
            count++;
        } else if (variables[i] instanceof Function) {
            Function func = (Function) variables[i];
            func.setIndex(count);
            varsSymbolTable.addVar(func);
            count++;
        } else if (variables[i] instanceof Constant) {
            Constant constant = (Constant) variables[i];
            constant.setIndex(count);
            varsSymbolTable.addVar(constant);
            count++;
        } else if (variables[i] instanceof ParameterVariable) {
            ParameterVariable param = (ParameterVariable) variables[i];
            param.setIndex(count);
            varsSymbolTable.addVar(param);
            count++;
        }
    }
    // Get the vector of sensVariables, needed for creating SensStateVariables
    Vector<SensStateVariable> sensVars = new Vector<SensStateVariable>();
    for (int i = 0; i < getStateVariableCount(); i++) {
        if (simTask.getSimulation().getSolverTaskDescription().getSensitivityParameter() != null) {
            if (getStateVariable(i) instanceof SensStateVariable) {
                sensVars.addElement((SensStateVariable) getStateVariable(i));
            }
        }
    }
    for (int j = count; j < (count + sensVars.size()); j++) {
        SensVariable sVar = (SensVariable) (sensVars.elementAt(j - count).getVariable());
        sVar.setIndex(j);
        varsSymbolTable.addVar(sVar);
    }
}
Also used : Function(cbit.vcell.math.Function) ReservedVariable(cbit.vcell.math.ReservedVariable) ParameterVariable(cbit.vcell.math.ParameterVariable) Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) VolVariable(cbit.vcell.math.VolVariable) Constant(cbit.vcell.math.Constant) VariableSymbolTable(cbit.vcell.parser.VariableSymbolTable) ParameterVariable(cbit.vcell.math.ParameterVariable) Vector(java.util.Vector)

Example 50 with Function

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

the class NFsimXMLWriter method isFunction.

private static boolean isFunction(String candidate, 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");
            }
            if (value != null) {
                // parameter, see getListOfParameters() above
                continue;
            } else {
                String current = var.getName();
                if (candidate.equals(current)) {
                    return true;
                }
            }
        }
    }
    return false;
}
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)

Aggregations

Function (cbit.vcell.math.Function)52 Expression (cbit.vcell.parser.Expression)42 Variable (cbit.vcell.math.Variable)32 ExpressionException (cbit.vcell.parser.ExpressionException)26 Constant (cbit.vcell.math.Constant)24 VolVariable (cbit.vcell.math.VolVariable)22 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)18 MathException (cbit.vcell.math.MathException)16 MathDescription (cbit.vcell.math.MathDescription)15 InsideVariable (cbit.vcell.math.InsideVariable)13 MemVariable (cbit.vcell.math.MemVariable)13 OutsideVariable (cbit.vcell.math.OutsideVariable)13 ReservedVariable (cbit.vcell.math.ReservedVariable)13 Domain (cbit.vcell.math.Variable.Domain)13 MembraneRegionVariable (cbit.vcell.math.MembraneRegionVariable)12 SubDomain (cbit.vcell.math.SubDomain)12 VolumeRegionVariable (cbit.vcell.math.VolumeRegionVariable)12 Vector (java.util.Vector)12 Element (org.jdom.Element)11 SubVolume (cbit.vcell.geometry.SubVolume)9