Search in sources :

Example 66 with Constant

use of cbit.vcell.math.Constant 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 67 with Constant

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

the class SensStateVariable method init.

/**
 * This method was created in VisualAge.
 * @param rateSensitivity cbit.vcell.math.RateSensitivity
 * @param jacobian cbit.vcell.math.Jacobian
 */
private void init(RateSensitivity rateSensitivity, Jacobian jacobian, SymbolTable symbolTable, Vector<SensVariable> sensVarArray) throws MathException, ExpressionException {
    // 
    // order sensVars according to the jacobian (rate index)
    // 
    sensVars = (Vector<SensVariable>) sensVarArray.clone();
    for (int i = 0; i < sensVarArray.size(); i++) {
        SensVariable sensVar = sensVarArray.elementAt(i);
        int index = jacobian.getRateIndex(sensVar.getVolVariable());
        sensVars.setElementAt(sensVar, index);
    }
    // 
    // given the system
    // 
    // 
    // d Ci
    // ------- = Fi(C1..Cn,P1..Pm)
    // d t
    // 
    // where:  C's are variables and P's are parameters
    // 
    // jacobian.show();
    VolVariable var = ((SensVariable) variable).getVolVariable();
    Constant parameter = ((SensVariable) variable).getParameter();
    // 
    // get list of jacobian expressions for this var (Ci)
    // 
    // d Fi
    // ------   for all j
    // d Cj
    // 
    optimizedJacobianExps = new Expression[jacobian.getNumRates()];
    int currIndex = jacobian.getRateIndex(var);
    for (int i = 0; i < optimizedJacobianExps.length; i++) {
        Expression exp = jacobian.getJexp(currIndex, i);
        exp.bindExpression(symbolTable);
        optimizedJacobianExps[i] = exp.flatten();
    }
    // 
    // get rate sensitivity for this variable (Ci) and the parameter under investigation (Pj)
    // 
    // d Fi
    // ------
    // d Pj
    // 
    // 
    Expression exp = rateSensitivity.getCPexp(var, parameter.getName());
    exp.bindExpression(symbolTable);
    optimizedRateSensExp = exp.flatten();
}
Also used : VolVariable(cbit.vcell.math.VolVariable) Expression(cbit.vcell.parser.Expression) Constant(cbit.vcell.math.Constant)

Example 68 with Constant

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

the class ModelOptimizationMapping method computeOptimizationSpec.

/**
 * Insert the method's description here.
 * Creation date: (8/22/2005 9:26:52 AM)
 * @return cbit.vcell.opt.OptimizationSpec
 * @param modelOptimizationSpec cbit.vcell.modelopt.ModelOptimizationSpec
 */
MathSymbolMapping computeOptimizationSpec() throws MathException, MappingException {
    if (getModelOptimizationSpec().getReferenceData() == null) {
        System.out.println("no referenced data defined");
        return null;
    }
    OptimizationSpec optSpec = new OptimizationSpec();
    optSpec.setComputeProfileDistributions(modelOptimizationSpec.isComputeProfileDistributions());
    parameterMappings = null;
    // 
    // get original MathDescription (later to be substituted for local/global parameters).
    // 
    SimulationContext simContext = modelOptimizationSpec.getSimulationContext();
    MathMapping mathMapping = simContext.createNewMathMapping();
    MathDescription origMathDesc = null;
    mathSymbolMapping = null;
    try {
        origMathDesc = mathMapping.getMathDescription();
        mathSymbolMapping = mathMapping.getMathSymbolMapping();
    } catch (MatrixException e) {
        e.printStackTrace(System.out);
        throw new MappingException(e.getMessage());
    } catch (ModelException e) {
        e.printStackTrace(System.out);
        throw new MappingException(e.getMessage());
    } catch (ExpressionException e) {
        e.printStackTrace(System.out);
        throw new MathException(e.getMessage());
    }
    // 
    // create objective function (mathDesc and data)
    // 
    ReferenceData referenceData = getRemappedReferenceData(mathMapping);
    if (referenceData == null) {
        throw new RuntimeException("no referenced data defined");
    }
    // 
    // get parameter mappings
    // 
    ParameterMappingSpec[] parameterMappingSpecs = modelOptimizationSpec.getParameterMappingSpecs();
    Vector<ParameterMapping> parameterMappingList = new Vector<ParameterMapping>();
    Variable[] allVars = (Variable[]) BeanUtils.getArray(origMathDesc.getVariables(), Variable.class);
    for (int i = 0; i < parameterMappingSpecs.length; i++) {
        cbit.vcell.model.Parameter modelParameter = parameterMappingSpecs[i].getModelParameter();
        String mathSymbol = null;
        Variable mathVariable = null;
        if (mathSymbolMapping != null) {
            Variable variable = mathSymbolMapping.getVariable(modelParameter);
            if (variable != null) {
                mathSymbol = variable.getName();
            }
            if (mathSymbol != null) {
                mathVariable = origMathDesc.getVariable(mathSymbol);
            }
        }
        if (mathVariable != null) {
            if (parameterMappingSpecs[i].isSelected()) {
                if (parameterMappingSpecs[i].getHigh() < parameterMappingSpecs[i].getLow()) {
                    throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
                }
                if (parameterMappingSpecs[i].getCurrent() < parameterMappingSpecs[i].getLow()) {
                    throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is smaller than its lower bound.");
                }
                if (parameterMappingSpecs[i].getCurrent() > parameterMappingSpecs[i].getHigh()) {
                    throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
                }
                if (parameterMappingSpecs[i].getLow() < 0) {
                    throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All lower bounds must not be negative.");
                }
                if (Double.isInfinite(parameterMappingSpecs[i].getLow())) {
                    throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Lower bounds must not be infinity.");
                }
                if (parameterMappingSpecs[i].getHigh() <= 0) {
                    throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All upper bounds must be positive.");
                }
                if (Double.isInfinite(parameterMappingSpecs[i].getHigh())) {
                    throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Upper bounds must not be infinity.");
                }
            }
            double low = parameterMappingSpecs[i].isSelected() && parameterMappingSpecs[i].getLow() == 0 ? 1e-8 : parameterMappingSpecs[i].getLow();
            double high = parameterMappingSpecs[i].getHigh();
            double scale = Math.abs(parameterMappingSpecs[i].getCurrent()) < 1.0E-10 ? 1.0 : Math.abs(parameterMappingSpecs[i].getCurrent());
            double current = parameterMappingSpecs[i].getCurrent();
            low = Math.min(low, current);
            high = Math.max(high, current);
            Parameter optParameter = new Parameter(mathSymbol, low, high, scale, current);
            ParameterMapping parameterMapping = new ParameterMapping(modelParameter, optParameter, mathVariable);
            // 
            if (mathVariable instanceof Constant) {
                Constant origConstant = (Constant) mathVariable;
                for (int j = 0; j < allVars.length; j++) {
                    if (allVars[j].equals(origConstant)) {
                        if (parameterMappingSpecs[i].isSelected()) {
                            allVars[j] = new ParameterVariable(origConstant.getName());
                        } else {
                            allVars[j] = new Constant(origConstant.getName(), new Expression(optParameter.getInitialGuess()));
                        }
                        break;
                    }
                }
            }
            // 
            if (parameterMappingSpecs[i].isSelected()) {
                parameterMappingList.add(parameterMapping);
            }
        }
    }
    parameterMappings = (ParameterMapping[]) BeanUtils.getArray(parameterMappingList, ParameterMapping.class);
    try {
        origMathDesc.setAllVariables(allVars);
    } catch (ExpressionBindingException e) {
        e.printStackTrace(System.out);
        throw new MathException(e.getMessage());
    }
    // 
    for (int i = 0; i < parameterMappings.length; i++) {
        optSpec.addParameter(parameterMappings[i].getOptParameter());
    }
    Vector<Issue> issueList = new Vector<Issue>();
    IssueContext issueContext = new IssueContext();
    optSpec.gatherIssues(issueContext, issueList);
    for (int i = 0; i < issueList.size(); i++) {
        Issue issue = issueList.elementAt(i);
        if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
            throw new RuntimeException(issue.getMessage());
        }
    }
    // 
    // 
    // 
    optimizationSpec = optSpec;
    return mathSymbolMapping;
}
Also used : ParameterVariable(cbit.vcell.math.ParameterVariable) Variable(cbit.vcell.math.Variable) Issue(org.vcell.util.Issue) MathDescription(cbit.vcell.math.MathDescription) Constant(cbit.vcell.math.Constant) ParameterVariable(cbit.vcell.math.ParameterVariable) ExpressionException(cbit.vcell.parser.ExpressionException) MappingException(cbit.vcell.mapping.MappingException) MatrixException(cbit.vcell.matrix.MatrixException) IssueContext(org.vcell.util.IssueContext) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) Vector(java.util.Vector) ModelException(cbit.vcell.model.ModelException) SimulationContext(cbit.vcell.mapping.SimulationContext) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) ReferenceData(cbit.vcell.opt.ReferenceData) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) MathMapping(cbit.vcell.mapping.MathMapping) Parameter(cbit.vcell.opt.Parameter)

Example 69 with Constant

use of cbit.vcell.math.Constant 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

Constant (cbit.vcell.math.Constant)69 Expression (cbit.vcell.parser.Expression)43 Variable (cbit.vcell.math.Variable)31 Function (cbit.vcell.math.Function)24 MathDescription (cbit.vcell.math.MathDescription)22 VolVariable (cbit.vcell.math.VolVariable)21 ExpressionException (cbit.vcell.parser.ExpressionException)21 MathException (cbit.vcell.math.MathException)15 Vector (java.util.Vector)15 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)14 MemVariable (cbit.vcell.math.MemVariable)14 MembraneRegionVariable (cbit.vcell.math.MembraneRegionVariable)13 SubDomain (cbit.vcell.math.SubDomain)13 MacroscopicRateConstant (cbit.vcell.math.MacroscopicRateConstant)12 VolumeRegionVariable (cbit.vcell.math.VolumeRegionVariable)12 ReservedVariable (cbit.vcell.math.ReservedVariable)11 FilamentVariable (cbit.vcell.math.FilamentVariable)10 InsideVariable (cbit.vcell.math.InsideVariable)10 OutsideVariable (cbit.vcell.math.OutsideVariable)10 FilamentRegionVariable (cbit.vcell.math.FilamentRegionVariable)9