use of cbit.vcell.math.MathException in project vcell by virtualcell.
the class SundialsSolver method createStateVariables.
/*
This method was created in Visual Age
*/
private StateVariable[] createStateVariables() throws MathException, ExpressionException {
Vector<StateVariable> stateVariables = new Vector<StateVariable>();
// get Ode's from MathDescription and create ODEStateVariables
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
MathDescription mathDescription = simSymbolTable.getSimulation().getMathDescription();
Enumeration<Equation> enum1 = ((SubDomain) mathDescription.getSubDomains().nextElement()).getEquations();
while (enum1.hasMoreElements()) {
Equation equation = (Equation) enum1.nextElement();
if (equation instanceof OdeEquation) {
stateVariables.addElement(new ODEStateVariable((OdeEquation) equation, simSymbolTable));
} else {
throw new MathException("encountered non-ode equation, unsupported");
}
}
// Get sensitivity variables
Variable[] variables = simSymbolTable.getVariables();
Vector<SensVariable> sensVariables = new Vector<SensVariable>();
if (getSensitivityParameter() != null) {
for (int i = 0; i < variables.length; i++) {
if (variables[i] instanceof VolVariable) {
VolVariable volVariable = (VolVariable) variables[i];
SensVariable sv = new SensVariable(volVariable, getSensitivityParameter());
sensVariables.addElement(sv);
}
}
}
if (rateSensitivity == null) {
rateSensitivity = new RateSensitivity(mathDescription, mathDescription.getSubDomains().nextElement());
}
if (jacobian == null) {
jacobian = new Jacobian(mathDescription, mathDescription.getSubDomains().nextElement());
}
for (int v = 0; v < sensVariables.size(); v++) {
stateVariables.addElement(new SensStateVariable((SensVariable) sensVariables.elementAt(v), rateSensitivity, jacobian, sensVariables, simSymbolTable));
}
if (stateVariables.size() == 0) {
throw new MathException("there are no equations defined");
}
StateVariable[] stateVars = (StateVariable[]) BeanUtils.getArray(stateVariables, StateVariable.class);
return (stateVars);
}
use of cbit.vcell.math.MathException 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;
}
use of cbit.vcell.math.MathException in project vcell by virtualcell.
the class CVodeFileWriter method writeEquations.
/**
* Insert the method's description here.
* Creation date: (3/8/00 10:31:52 PM)
*/
protected String writeEquations(HashMap<Discontinuity, String> discontinuityNameMap) throws MathException, ExpressionException {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < getStateVariableCount(); i++) {
StateVariable stateVar = getStateVariable(i);
Expression rateExpr = new Expression(stateVar.getRateExpression());
Expression initExpr = new Expression(stateVar.getInitialRateExpression());
initExpr = MathUtilities.substituteFunctions(initExpr, varsSymbolTable).flatten();
initExpr.substituteInPlace(new Expression("t"), new Expression(0.0));
String[] symbols0 = initExpr.getSymbols();
if (symbols0 != null) {
for (String symbol : symbols0) {
SymbolTableEntry ste = initExpr.getSymbolBinding(symbol);
if (!ste.equals(ReservedVariable.X) && !ste.equals(ReservedVariable.Y) && !ste.equals(ReservedVariable.Z)) {
throw new MathException("Variables are not allowed in initial condition.\nInitial condition of variable:" + stateVar.getVariable().getName() + " has variable(" + symbol + ") in expression.");
}
}
}
rateExpr = MathUtilities.substituteFunctions(rateExpr, varsSymbolTable).flatten();
Vector<Discontinuity> v = rateExpr.getDiscontinuities();
for (Discontinuity od : v) {
od = getSubsitutedAndFlattened(od, varsSymbolTable);
String dname = discontinuityNameMap.get(od);
if (dname == null) {
dname = ROOT_VARIABLE_PREFIX + discontinuityNameMap.size();
discontinuityNameMap.put(od, dname);
}
rateExpr.substituteInPlace(od.getDiscontinuityExp(), new Expression("(" + dname + "==1)"));
}
sb.append("ODE " + stateVar.getVariable().getName() + " INIT " + initExpr.flatten().infix() + ";\n\t RATE " + rateExpr.flatten().infix() + ";\n");
}
return sb.toString();
}
use of cbit.vcell.math.MathException 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.math.MathException in project vcell by virtualcell.
the class XmlReader method getVarIniCount.
/**
* This method return a VarIniCondition object from a XML element.
* Creation date: (7/24/2006 5:26:05 PM)
* @return cbit.vcell.math.VarIniCondition
* @param param org.jdom.Element
* @exception cbit.vcell.xml.XmlParseException The exception description.
*/
private VarIniCondition getVarIniCount(Element param, MathDescription md) throws XmlParseException, MathException, ExpressionException {
// retrieve values
Expression exp = unMangleExpression(param.getText());
String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
Variable var = md.getVariable(name);
if (var == null) {
throw new MathFormatException("variable " + name + " not defined");
}
if (!(var instanceof StochVolVariable)) {
throw new MathFormatException("variable " + name + " not a Stochastic Volume Variable");
}
try {
VarIniCondition varIni = new VarIniCount(var, exp);
return varIni;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Aggregations