use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.
the class MathTestingUtilities method getConstructedResultSet.
/**
* Insert the method's description here.
* Creation date: (1/17/2003 3:47:43 PM)
* @return cbit.vcell.solver.ode.ODESolverResultSet
* @param sim cbit.vcell.solver.Simulation
*/
public static ODESolverResultSet getConstructedResultSet(MathDescription mathDesc, double[] time) throws Exception {
if (mathDesc.getGeometry().getDimension() != 0) {
throw new RuntimeException("can only handle non-spatial simulations.");
}
Simulation sim = new Simulation(mathDesc);
SimulationSymbolTable simSymbolTable = new SimulationSymbolTable(sim, 0);
ODESolverResultSet resultSet = new ODESolverResultSet();
resultSet.addDataColumn(new ODESolverResultSetColumnDescription("t"));
for (int i = 0; i < time.length; i++) {
resultSet.addRow(new double[] { time[i] });
}
java.util.Enumeration<SubDomain> subDomainEnum = mathDesc.getSubDomains();
String errorString = "Variable(s) : ";
while (subDomainEnum.hasMoreElements()) {
SubDomain subDomain = subDomainEnum.nextElement();
java.util.Enumeration<Equation> enumEquations = subDomain.getEquations();
while (enumEquations.hasMoreElements()) {
Equation equation = enumEquations.nextElement();
Expression constructedSolution = equation.getExactSolution();
if (constructedSolution != null) {
constructedSolution = new Expression(constructedSolution);
constructedSolution.bindExpression(simSymbolTable);
constructedSolution = simSymbolTable.substituteFunctions(constructedSolution);
constructedSolution = constructedSolution.flatten();
resultSet.addFunctionColumn(new FunctionColumnDescription(constructedSolution, equation.getVariable().getName(), null, equation.getVariable().getName(), false));
} else {
errorString = errorString + equation.getVariable().getName() + ", ";
}
}
}
if (!errorString.equals("Variable(s) : ")) {
throw new RuntimeException(errorString + " don't have a constructed solution");
}
return resultSet;
}
use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.
the class MathTestingUtilities method getExactResultSet.
/**
* Insert the method's description here.
* Creation date: (1/17/2003 3:47:43 PM)
* @return cbit.vcell.solver.ode.ODESolverResultSet
* @param sim cbit.vcell.solver.Simulation
*/
public static ODESolverResultSet getExactResultSet(MathDescription mathDesc, double[] time, Constant sensitivityParam) throws Exception {
if (mathDesc.getGeometry().getDimension() != 0) {
throw new RuntimeException("can only handle non-spatial simulations.");
}
Simulation sim = new Simulation(mathDesc);
SimulationSymbolTable simSymbolTable = new SimulationSymbolTable(sim, 0);
ODESolverResultSet resultSet = new ODESolverResultSet();
resultSet.addDataColumn(new ODESolverResultSetColumnDescription("t"));
for (int i = 0; i < time.length; i++) {
resultSet.addRow(new double[] { time[i] });
}
java.util.Enumeration<SubDomain> subDomainEnum = mathDesc.getSubDomains();
while (subDomainEnum.hasMoreElements()) {
SubDomain subDomain = subDomainEnum.nextElement();
java.util.Enumeration<Equation> enumEquations = subDomain.getEquations();
while (enumEquations.hasMoreElements()) {
Equation equation = enumEquations.nextElement();
Expression exactSolution = equation.getExactSolution();
if (exactSolution != null) {
exactSolution = new Expression(exactSolution);
exactSolution.bindExpression(simSymbolTable);
exactSolution = simSymbolTable.substituteFunctions(exactSolution);
exactSolution.bindExpression(simSymbolTable);
exactSolution = exactSolution.flatten();
resultSet.addFunctionColumn(new FunctionColumnDescription(exactSolution, equation.getVariable().getName(), null, equation.getVariable().getName(), false));
if (sensitivityParam != null) {
exactSolution = equation.getExactSolution();
Expression exactSensitivity = new Expression(exactSolution);
exactSensitivity.bindExpression(simSymbolTable);
exactSensitivity = simSymbolTable.substituteFunctions(exactSensitivity);
exactSensitivity.bindExpression(simSymbolTable);
exactSensitivity = exactSensitivity.differentiate(sensitivityParam.getName());
exactSensitivity = exactSensitivity.flatten();
VolVariable volVar = (VolVariable) equation.getVariable();
String sensName = SensVariable.getSensName(volVar, sensitivityParam);
resultSet.addFunctionColumn(new FunctionColumnDescription(exactSensitivity, sensName, null, sensName, false));
}
} else {
throw new RuntimeException("variable " + equation.getVariable().getName() + " doesn't have an exact solution");
}
}
}
return resultSet;
}
use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.
the class DefaultODESolver method createODESolverResultSet.
/**
*/
private ODESolverResultSet createODESolverResultSet() throws ExpressionException {
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
//
// create symbol table for binding expression
//
String[] symbols = new String[fieldIdentifiers.size()];
for (int i = 0; i < symbols.length; i++) {
symbols[i] = ((Variable) fieldIdentifiers.elementAt(i)).getName();
}
// Initialize the ResultSet...
ODESolverResultSet odeSolverResultSet = new ODESolverResultSet();
odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(ReservedVariable.TIME.getName()));
for (int i = 0; i < getStateVariableCount(); i++) {
StateVariable stateVariable = getStateVariable(i);
if (stateVariable instanceof SensStateVariable) {
SensStateVariable sensStateVariable = (SensStateVariable) stateVariable;
odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(sensStateVariable.getVariable().getName(), sensStateVariable.getParameter().getName(), sensStateVariable.getVariable().getName()));
} else {
odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(stateVariable.getVariable().getName()));
}
}
Variable[] variables = simSymbolTable.getVariables();
for (int i = 0; i < variables.length; i++) {
if (variables[i] instanceof Function && SimulationSymbolTable.isFunctionSaved((Function) variables[i])) {
Function function = (Function) variables[i];
Expression exp1 = new Expression(function.getExpression());
try {
exp1 = simSymbolTable.substituteFunctions(exp1);
} catch (MathException e) {
e.printStackTrace(System.out);
throw new RuntimeException("Substitute function failed on function " + function.getName() + " " + e.getMessage());
}
odeSolverResultSet.addFunctionColumn(new FunctionColumnDescription(exp1.flatten(), function.getName(), null, function.getName(), false));
}
}
//
if (getSensitivityParameter() != null) {
if (odeSolverResultSet.findColumn(getSensitivityParameter().getName()) == -1) {
FunctionColumnDescription fcd = new FunctionColumnDescription(new Expression(getSensitivityParameter().getConstantValue()), getSensitivityParameter().getName(), null, getSensitivityParameter().getName(), false);
odeSolverResultSet.addFunctionColumn(fcd);
}
StateVariable[] stateVars = (StateVariable[]) org.vcell.util.BeanUtils.getArray(fieldStateVariables, StateVariable.class);
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());
try {
depSensFnExpr = simSymbolTable.substituteFunctions(depSensFnExpr);
} catch (MathException e) {
e.printStackTrace(System.out);
throw new RuntimeException("Substitute function failed on function " + depSensFunction.getName() + " " + e.getMessage());
}
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);
}
}
}
}
return (odeSolverResultSet);
}
use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.
the class ODESimData method readIn.
/**
* JMW : This really should be synchronized...
*/
public void readIn(DataInputStream input) throws IOException {
formatID = input.readUTF();
if (formatID.equals(SIMPLE_ODE_DATA_FORMAT_ID)) {
this.mathName = input.readUTF();
// read data from old format file
double saveInterval = input.readDouble();
int savedNumber = input.readInt();
int variableNumber = input.readInt();
String[] variableNames = new String[variableNumber];
double[][] dataValues = new double[savedNumber][variableNumber];
for (int i = 0; i < variableNumber; i++) {
int flag = input.readInt();
variableNames[i] = input.readUTF();
for (int j = 0; j < savedNumber; j++) {
dataValues[j][i] = input.readDouble();
}
}
// now put data in new data structure
int rowCount = savedNumber;
int columnCount = variableNumber + 1;
addDataColumn(new ODESolverResultSetColumnDescription("t", "t"));
for (int c = 1; c < columnCount; c++) {
addDataColumn(new ODESolverResultSetColumnDescription(variableNames[c - 1], variableNames[c - 1]));
}
double[] values = new double[columnCount];
for (int c = 0; c < columnCount; c++) values[c] = 0.0;
for (int r = 0; r < rowCount; r++) {
values[0] = r * saveInterval / 1000.0;
addRow(values);
}
for (int c = 1; c < columnCount; c++) {
for (int r = 0; r < rowCount; r++) {
setValue(r, c, dataValues[r][c - 1]);
}
}
} else if (formatID.equals(GENERIC_ODE_DATA_FORMAT_ID)) {
this.mathName = input.readUTF();
int rowCount = input.readInt();
int columnCount = input.readInt();
for (int c = 0; c < columnCount; c++) {
String columnName = input.readUTF();
String columnDisplayName = input.readUTF();
addDataColumn(new ODESolverResultSetColumnDescription(columnName, columnDisplayName));
}
double[] values = new double[columnCount];
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < columnCount; c++) {
values[c] = input.readDouble();
}
addRow(values);
}
} else if (formatID.equals(COMPACT_ODE_DATA_FORMAT_ID)) {
this.mathName = input.readUTF();
int rowCount = input.readInt();
int columnCount = input.readInt();
for (int c = 0; c < columnCount; c++) {
String columnName = input.readUTF();
String columnDisplayName = input.readUTF();
String columnParameterName = input.readUTF();
if (columnParameterName.equals("null")) {
columnParameterName = null;
}
addDataColumn(new ODESolverResultSetColumnDescription(columnName, columnParameterName, columnDisplayName));
}
double[] values = new double[columnCount];
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < columnCount; c++) {
values[c] = input.readDouble();
}
addRow(values);
}
try {
int functionCount = input.readInt();
for (int c = 0; c < functionCount; c++) {
String columnName = input.readUTF();
String columnDisplayName = input.readUTF();
String columnParameterName = input.readUTF();
if (columnParameterName.equals("null")) {
columnParameterName = null;
}
String expressionString = input.readUTF();
try {
Expression expression = new Expression(expressionString);
addFunctionColumn(new FunctionColumnDescription(expression, columnName, columnParameterName, columnDisplayName, false));
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
System.out.println("ODESimData.readIn(): unable to bind expression '" + expressionString + "'");
} catch (ExpressionException e) {
e.printStackTrace(System.out);
System.out.println("ODESimData.readIn(): unable to parse expression '" + expressionString + "'");
}
}
} catch (EOFException e) {
}
} else {
throw new IOException("DataInputStream is wrong format '" + formatID + "'");
}
}
use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.
the class ODESimData method readIDADataFile.
public static ODESimData readIDADataFile(VCDataIdentifier vcdId, File dataFile, int keepMost, File functionsFile) throws DataAccessException {
// read ida file
System.out.println("reading ida file : " + dataFile);
ODESimData odeSimData = new ODESimData();
odeSimData.formatID = IDA_DATA_FORMAT_ID;
odeSimData.mathName = vcdId.getID();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile)));
// Read header
String line = bufferedReader.readLine();
if (line == null) {
// throw exception
return null;
}
StringTokenizer st = new StringTokenizer(line, ":");
while (st.hasMoreTokens()) {
odeSimData.addDataColumn(new ODESolverResultSetColumnDescription(st.nextToken()));
}
// Read data
while ((line = bufferedReader.readLine()) != null) {
st = new StringTokenizer(line);
double[] values = new double[odeSimData.getDataColumnCount()];
int count = 0;
while (st.hasMoreTokens()) {
values[count++] = Double.valueOf(st.nextToken()).doubleValue();
}
if (count == odeSimData.getDataColumnCount()) {
odeSimData.addRow(values);
} else {
break;
}
}
//
} catch (Exception e) {
e.printStackTrace(System.out);
return null;
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
if (!odeSimData.getColumnDescriptions(0).getName().equals(SimDataConstants.HISTOGRAM_INDEX_NAME)) {
Vector<AnnotatedFunction> funcList;
try {
funcList = FunctionFileGenerator.readFunctionsFile(functionsFile, vcdId.getID());
for (AnnotatedFunction func : funcList) {
try {
Expression expression = new Expression(func.getExpression());
odeSimData.addFunctionColumn(new FunctionColumnDescription(expression, func.getName(), null, func.getName(), false));
} catch (ExpressionException e) {
throw new RuntimeException("Could not add function " + func.getName() + " to annotatedFunctionList");
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace(System.out);
throw new DataAccessException(e1.getMessage());
} catch (IOException e1) {
e1.printStackTrace(System.out);
throw new DataAccessException(e1.getMessage());
}
}
if (keepMost > 0) {
odeSimData.trimRows(keepMost);
}
return odeSimData;
}
Aggregations