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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations