use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class SimulationSymbolTable method createAnnotatedFunctionsList.
public Vector<AnnotatedFunction> createAnnotatedFunctionsList(MathDescription mathDescription) throws InconsistentDomainException {
// Get the list of (volVariables) in the simulation. Needed to determine 'type' of functions
boolean bSpatial = getSimulation().isSpatial();
String[] variableNames = null;
VariableType[] variableTypes = null;
if (bSpatial) {
Variable[] allVariables = getVariables();
Vector<Variable> varVector = new Vector<Variable>();
for (int i = 0; i < allVariables.length; i++) {
if ((allVariables[i] instanceof VolVariable) || (allVariables[i] instanceof VolumeRegionVariable) || (allVariables[i] instanceof MemVariable) || (allVariables[i] instanceof MembraneRegionVariable) || (allVariables[i] instanceof FilamentVariable) || (allVariables[i] instanceof FilamentRegionVariable) || (allVariables[i] instanceof PointVariable) || (allVariables[i] instanceof ParticleVariable) || (allVariables[i] instanceof InsideVariable) || (allVariables[i] instanceof OutsideVariable)) {
varVector.addElement(allVariables[i]);
} else if (allVariables[i] instanceof Constant || (allVariables[i] instanceof Function)) {
} else {
System.err.println("SimulationSymbolTable.createAnnotatedFunctionsList() found unexpected variable type " + allVariables[i].getClass().getSimpleName() + " in spatial simulation");
}
}
variableNames = new String[varVector.size()];
for (int i = 0; i < variableNames.length; i++) {
variableNames[i] = varVector.get(i).getName();
}
// Lookup table for variableType for each variable in 'variables' array.
variableTypes = new VariableType[variableNames.length];
for (int i = 0; i < variableNames.length; i++) {
variableTypes[i] = VariableType.getVariableType(varVector.get(i));
}
}
//
// Bind and substitute functions to simulation before storing them in the '.functions' file
//
Function[] functions = getFunctions();
Vector<AnnotatedFunction> annotatedFunctionVector = new Vector<AnnotatedFunction>();
for (int i = 0; i < functions.length; i++) {
if (isFunctionSaved(functions[i])) {
String errString = "";
VariableType funcType = null;
try {
Expression substitutedExp = substituteFunctions(functions[i].getExpression());
substitutedExp.bindExpression(this);
functions[i].setExpression(substitutedExp.flatten());
} catch (MathException e) {
e.printStackTrace(System.out);
errString = errString + ", " + e.getMessage();
// throw new RuntimeException(e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
errString = errString + ", " + e.getMessage();
// throw new RuntimeException(e.getMessage());
}
//
// get function's data type from the types of it's identifiers
//
funcType = bSpatial ? getFunctionVariableType(functions[i], mathDescription, variableNames, variableTypes, bSpatial) : VariableType.NONSPATIAL;
AnnotatedFunction annotatedFunc = new AnnotatedFunction(functions[i].getName(), functions[i].getExpression(), functions[i].getDomain(), errString, funcType, FunctionCategory.PREDEFINED);
annotatedFunctionVector.addElement(annotatedFunc);
}
}
return annotatedFunctionVector;
}
use of cbit.vcell.parser.ExpressionException 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());
}
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class AdamsMoultonFiveSolver method step.
/**
* Integrate over time step using the forward Euler method (1st order explicit)
* results must be stored in NumVectors-1 = vector(4);
* t is the current time
* h is the time step
*/
protected void step(double t, double h) throws SolverException {
try {
double[] oldValues = getValueVector(0);
double[] newValues = getValueVector(1);
//
// update time
oldValues[getTimeIndex()] = t;
newValues[getTimeIndex()] = t + h;
// Predict
for (int i = 0; i < getStateVariableCount(); i++) {
int I = getVariableIndex(i);
newValues[I] = oldValues[I] + h * (55.0 * f[3][I] - 59.0 * f[2][I] + 37.0 * f[1][I] - 9.0 * f[0][I]) / 24.0;
}
// Evaluate
for (int i = 0; i < getStateVariableCount(); i++) {
f[4][getVariableIndex(i)] = evaluate(newValues, i);
}
// Correct
for (int i = 0; i < getStateVariableCount(); i++) {
int I = getVariableIndex(i);
newValues[I] = oldValues[I] + h * (9.0 * f[4][I] + 19.0 * f[3][I] - 5.0 * f[2][I] + 1.0 * f[1][I]) / 24.0;
}
} catch (ExpressionException expressionException) {
throw new SolverException(expressionException.getMessage());
}
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class DefaultODESolver method initialize.
/**
* This method was created by a SmartGuide.
* @exception SolverException The exception description.
*/
protected void initialize() throws SolverException {
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
Simulation sim = simSymbolTable.getSimulation();
try {
// create a fast system if necessary
fieldFastAlgebraicSystem = null;
if (getSubDomain().getFastSystem() != null) {
fieldFastAlgebraicSystem = new FastAlgebraicSystem(new FastSystemAnalyzer(getSubDomain().getFastSystem(), simSymbolTable));
}
// refreshIdentifiers();
fieldIdentifiers = createIdentifiers();
fieldSensVariables = createSensitivityVariables();
// refreshStateVariables();
fieldStateVariables = createStateVariables();
//
// allocate ValueVectors object
fieldValueVectors = new ValueVectors(getValueVectorCount(), fieldIdentifiers.size());
// initialize indexes of variables
fieldVariableIndexes = new int[getStateVariableCount()];
for (int i = 0; i < getStateVariableCount(); i++) {
fieldVariableIndexes[i] = getStateVariable(i).getVariable().getIndex();
}
// initialize constants
double[] initialValues = getValueVector(0);
for (int i = 0; i < fieldIdentifiers.size(); i++) {
if (fieldIdentifiers.elementAt(i) instanceof Constant) {
Constant constant = (Constant) fieldIdentifiers.elementAt(i);
constant.bind(simSymbolTable);
if (constant.isConstant()) {
// constant.getValue();
initialValues[constant.getIndex()] = constant.getExpression().evaluateConstant();
} else {
throw new SolverException("cannot evaluate constant '" + constant.getName() + "' = " + constant.getExpression());
}
}
}
// initialize variables
for (int i = 0; i < getStateVariableCount(); i++) {
initialValues[getVariableIndex(i)] = getStateVariable(i).evaluateIC(initialValues);
}
fieldODESolverResultSet = createODESolverResultSet();
// reset - in the ** default ** solvers we don't pick up from where we left off, we can override that behaviour in integrate() if ever necessary
fieldCurrentTime = sim.getSolverTaskDescription().getTimeBounds().getStartingTime();
} catch (ExpressionException expressionException) {
expressionException.printStackTrace(System.out);
throw new SolverException(expressionException.getMessage());
} catch (MathException mathException) {
mathException.printStackTrace(System.out);
throw new SolverException(mathException.getMessage());
}
}
use of cbit.vcell.parser.ExpressionException 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;
}
Aggregations