Search in sources :

Example 1 with ODEStateVariable

use of cbit.vcell.solver.ode.ODEStateVariable in project vcell by virtualcell.

the class AbstractSolver method getFunctionSensitivity.

/**
 * This method was created by a SmartGuide.
 * @return double[]
 * @param identifier java.lang.String
 */
public Expression getFunctionSensitivity(Expression funcExpr, Constant constant, StateVariable[] stateVariables) throws ExpressionException {
    if (stateVariables == null || stateVariables.length == 0) {
        return null;
    }
    // this uses the chain rule
    // d F(x)   del F(x)        del F(x)   del xi
    // ------ = -------- + Sum (-------- * ------)
    // d k      del k      i    del xi    del k
    // explicit     via state variables
    // dependence
    // 
    // collect the explicit term
    // 
    SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
    Expression sensFuncExp = funcExpr.differentiate(constant.getName());
    sensFuncExp.bindExpression(simSymbolTable);
    sensFuncExp = sensFuncExp.flatten();
    for (int i = 0; i < stateVariables.length; i++) {
        if (stateVariables[i] instanceof ODEStateVariable) {
            ODEStateVariable sv = (ODEStateVariable) stateVariables[i];
            VolVariable volVar = (VolVariable) sv.getVariable();
            // 
            // get corresponding sensitivity state variable
            // 
            SensStateVariable sensStateVariable = null;
            for (int j = 0; j < stateVariables.length; j++) {
                if (stateVariables[j] instanceof SensStateVariable && ((SensVariable) ((SensStateVariable) stateVariables[j]).getVariable()).getVolVariable().getName().equals(volVar.getName()) && ((SensStateVariable) stateVariables[j]).getParameter().getName().equals(constant.getName())) {
                    sensStateVariable = (SensStateVariable) stateVariables[j];
                }
            }
            if (sensStateVariable == null) {
                System.out.println("sens of " + volVar.getName() + " wrt " + constant.getName() + " is not found");
                return null;
            }
            // 
            // get coefficient of proportionality   (e.g.  A = total + b*B + c*C ... gives dA/dK = b*dB/dK + c*dC/dK)
            // 
            Expression coeffExpression = funcExpr.differentiate(volVar.getName());
            coeffExpression.bindExpression(simSymbolTable);
            coeffExpression = MathUtilities.substituteFunctions(coeffExpression, simSymbolTable);
            coeffExpression.bindExpression(simSymbolTable);
            coeffExpression = coeffExpression.flatten();
            sensFuncExp = Expression.add(sensFuncExp, Expression.mult(coeffExpression, new Expression(sensStateVariable.getVariable().getName())));
        }
    }
    return new Expression(sensFuncExp);
}
Also used : Expression(cbit.vcell.parser.Expression) VolVariable(cbit.vcell.math.VolVariable) ODEStateVariable(cbit.vcell.solver.ode.ODEStateVariable) SensStateVariable(cbit.vcell.solver.ode.SensStateVariable) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) SensVariable(cbit.vcell.solver.ode.SensVariable)

Aggregations

VolVariable (cbit.vcell.math.VolVariable)1 Expression (cbit.vcell.parser.Expression)1 SimulationSymbolTable (cbit.vcell.solver.SimulationSymbolTable)1 ODEStateVariable (cbit.vcell.solver.ode.ODEStateVariable)1 SensStateVariable (cbit.vcell.solver.ode.SensStateVariable)1 SensVariable (cbit.vcell.solver.ode.SensVariable)1