use of cbit.vcell.solver.ode.ODESolverResultSet 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.solver.ode.ODESolverResultSet 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.solver.ode.ODESolverResultSet in project vcell by virtualcell.
the class SbmlVcmlConverter method solveSimulation.
/**
* solves the simulation that is passed in throught the simulation job.
*/
private static void solveSimulation(SimulationJob simJob, String filePathName, SimSpec argSimSpec) /* Hashtable argSpUnitsHash, double argTimeFactor, Model.ReservedSymbol kMoleSymbol*/
{
ODESolverResultSet odeSolverResultSet = null;
try {
/* // Generate .idaInput string
File idaInputFile = new File(filePathName.replace(".vcml", ".idaInput"));
PrintWriter idaPW = new java.io.PrintWriter(idaInputFile);
IDAFileWriter idaFileWriter = new IDAFileWriter(idaPW, simJob);
idaFileWriter.write();
idaPW.close();
// use the SundialsStandaloneSolver
File idaOutputFile = new File(filePathName.replace(".vcml", ".ida"));
Executable executable = new Executable(new String[]{"SundialsSolverStandalone", idaInputFile.getAbsolutePath(), idaOutputFile.getAbsolutePath()});
executable.start();
*/
// Generate .cvodeInput string
File cvodeInputFile = new File(filePathName.replace(".vcml", ".cvodeInput"));
PrintWriter cvodePW = new java.io.PrintWriter(cvodeInputFile);
SimulationTask simTask = new SimulationTask(simJob, 0);
CVodeFileWriter cvodeFileWriter = new CVodeFileWriter(cvodePW, simTask);
cvodeFileWriter.write();
cvodePW.close();
// use the cvodeStandalone solver
// use the SundialsStandaloneSolver
File cvodeOutputFile = new File(filePathName.replace(".vcml", ".ida"));
/*
Executable executable = new Executable(new String[]{"d:/workspace/VCell_5.3_VCell/SundialsSolverStandalone_x64", cvodeInputFile.getAbsolutePath(), cvodeOutputFile.getAbsolutePath()});
executable.start();
*/
// use the cvodeStandalone solver
// use the SundialsStandaloneSolver
File[] exes = SolverUtilities.getExes(SolverDescription.CombinedSundials);
assert exes.length == 1 : "one and only one smoldyn solver expected";
File sundialsExe = exes[0];
Executable executable = new Executable(new String[] { sundialsExe.getAbsolutePath(), cvodeInputFile.getAbsolutePath(), cvodeOutputFile.getAbsolutePath() });
executable.start();
// get the result
odeSolverResultSet = VCellSBMLSolver.getODESolverResultSet(simJob, cvodeOutputFile.getPath());
} catch (Exception e) {
e.printStackTrace(System.out);
throw new RuntimeException("Error running NativeIDA solver : " + e.getMessage());
}
// now write out the results into CSV file
try {
if (odeSolverResultSet != null) {
File csvFile = new File(filePathName.replace(".vcml", ".csv"));
PrintStream outputStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(csvFile)));
outputStream.print("time");
for (int i = 0; i < argSimSpec.getVarsList().length; i++) {
outputStream.print("," + argSimSpec.getVarsList()[i]);
}
outputStream.println();
// extract data for time and species
double[][] data = new double[argSimSpec.getVarsList().length + 1][];
int column = odeSolverResultSet.findColumn("t");
data[0] = odeSolverResultSet.extractColumn(column);
int origDataLength = data[0].length;
for (int i = 0; i < argSimSpec.getVarsList().length; i++) {
column = odeSolverResultSet.findColumn(argSimSpec.getVarsList()[i]);
if (column == -1) {
Variable var = simJob.getSimulationSymbolTable().getVariable(argSimSpec.getVarsList()[i]);
data[i + 1] = new double[data[0].length];
if (var instanceof cbit.vcell.math.Constant) {
double value = ((cbit.vcell.math.Constant) var).getExpression().evaluateConstant();
for (int j = 0; j < data[i + 1].length; j++) {
data[i + 1][j] = value;
}
} else {
throw new RuntimeException("Did not find " + argSimSpec.getVarsList()[i] + " in simulation");
}
} else {
data[i + 1] = odeSolverResultSet.extractColumn(column);
}
}
double endTime = (simJob.getSimulation().getSolverTaskDescription().getTimeBounds().getEndingTime());
// for each time, print row
int index = 0;
double[] sampleTimes = new double[numTimeSteps + 1];
for (int i = 0; i <= numTimeSteps; i++) {
sampleTimes[i] = endTime * i / numTimeSteps;
}
for (int i = 0; i < sampleTimes.length; i++) {
// find largest index whose time is not past sample time
while (true) {
// if already at last time point, then if it equals the sampleTime, we're done if it doesn't then we don't have this time point.
if (index == odeSolverResultSet.getRowCount() - 1) {
if (data[0][index] == sampleTimes[i]) {
break;
} else {
throw new RuntimeException("sampleTime does not match at last time point");
}
}
// haven't gotten to last time point yet, stop when next time step is past sampleTime.
if (data[0][index + 1] > sampleTimes[i]) {
break;
}
// sampleTime must be later in our data list.
index++;
}
// if (data[0][index] == sampleTimes[i]) {
if (Math.abs(data[0][index] - sampleTimes[i]) < 1e-12) {
// if timeFactor is not 1.0, time is not in seconds (mins or hrs); if timeFactor is 60, divide sampleTime/60; if it is 3600, divide sampleTime/3600.
// if (argTimeFactor != 1.0) {
// outputStream.print(data[0][index]/argTimeFactor);
// } else {
outputStream.print(data[0][index]);
// }
for (int j = 0; j < argSimSpec.getVarsList().length; j++) {
// SBMLImporter.SBVCConcentrationUnits spConcUnits = (SBMLImporter.SBVCConcentrationUnits)argSpUnitsHash.get(argSimSpec.getVarsList()[j]);
// if (spConcUnits != null) {
// VCUnitDefinition sbunits = spConcUnits.getSBConcentrationUnits();
// VCUnitDefinition vcunits = spConcUnits.getVCConcentrationUnits();
// SBMLUnitParameter unitFactor = SBMLUtils.getConcUnitFactor("spConcParam", vcunits, sbunits, kMoleSymbol);
// outputStream.print("," + data[j + 1][index] * unitFactor.getExpression().evaluateConstant()); //earlier, hack unitfactor = 0.000001
// earlier, hack unitfactor = 0.000001
outputStream.print("," + data[j + 1][index]);
}
// }
outputStream.println();
} else {
// if data[0][index] < sampleTime, must interpolate
double fraction = (sampleTimes[i] - data[0][index]) / (data[0][index + 1] - data[0][index]);
// if argTimeFactor is not 1.0, time is not in seconds (mins or hrs); if argTimeFactor is 60, divide sampleTime/60; if it is 3600, divide sampleTime/3600.
// if (argTimeFactor != 1.0) {
// outputStream.print(sampleTimes[i]/argTimeFactor);
// } else {
outputStream.print(sampleTimes[i]);
// }
for (int j = 0; j < argSimSpec.getVarsList().length; j++) {
double interpolatedValue = 0.0;
double[] speciesVals = null;
double[] times = null;
// Currently using 2nd order interpolation
if (index == 0) {
// can only do 1st order interpolation
times = new double[] { data[0][index], data[0][index + 1] };
speciesVals = new double[] { data[j + 1][index], data[j + 1][index + 1] };
interpolatedValue = MathTestingUtilities.taylorInterpolation(sampleTimes[i], times, speciesVals);
} else {
if (index >= 1 && index <= origDataLength - 3) {
double val_1 = Math.abs(sampleTimes[i] - data[0][index - 1]);
double val_2 = Math.abs(sampleTimes[i] - data[0][index + 2]);
if (val_1 < val_2) {
times = new double[] { data[0][index - 1], data[0][index], data[0][index + 1] };
speciesVals = new double[] { data[j + 1][index - 1], data[j + 1][index], data[j + 1][index + 1] };
} else {
times = new double[] { data[0][index], data[0][index + 1], data[0][index + 2] };
speciesVals = new double[] { data[j + 1][index], data[j + 1][index + 1], data[j + 1][index + 2] };
}
interpolatedValue = MathTestingUtilities.taylorInterpolation(sampleTimes[i], times, speciesVals);
} else {
times = new double[] { data[0][index - 1], data[0][index], data[0][index + 1] };
speciesVals = new double[] { data[j + 1][index - 1], data[j + 1][index], data[j + 1][index + 1] };
interpolatedValue = MathTestingUtilities.taylorInterpolation(sampleTimes[i], times, speciesVals);
}
}
// end if-else - calc of interpolated value
// interpolatedValue = interpolatedValue * unitFactor.getExpression().evaluateConstant();
outputStream.print("," + interpolatedValue);
}
// end for - sp values for each time point (row)
outputStream.println();
}
}
outputStream.close();
} else {
throw new RuntimeException("Result set was null, could not write to CSV file");
}
} catch (Exception e) {
e.printStackTrace(System.out);
throw new RuntimeException("Errors saving results to CSV file" + e.getMessage());
}
}
use of cbit.vcell.solver.ode.ODESolverResultSet in project vcell by virtualcell.
the class BNGDataPlotListModel method setOdeSolverResultSet.
/**
* Sets the dataSource property (cbit.vcell.modelopt.gui.DataSource) value.
* @param dataSource The new value for the property.
* @see #getDataSource
*/
public void setOdeSolverResultSet(ODESolverResultSet odeSolverResultSet) {
ODESolverResultSet oldValue = fieldOdeSolverResultSet;
fieldOdeSolverResultSet = odeSolverResultSet;
firePropertyChange("odeSolverResultSet", oldValue, odeSolverResultSet);
}
use of cbit.vcell.solver.ode.ODESolverResultSet in project vcell by virtualcell.
the class BNGOutputPanel method setTextArea.
/**
* Comment
*/
private void setTextArea(javax.swing.event.ListSelectionEvent listSelectionEvent) {
int listSelectionIndex = (int) getOutputFormatsList().getSelectedIndex();
if (listSelectionIndex == -1) {
return;
}
if (((String) getOutputFormatsList().getSelectedValue()).endsWith("dat")) {
((java.awt.CardLayout) getOutputsTextPanel().getLayout()).show(getOutputsTextPanel(), getbngDataPlotPanel().getName());
String fileContentStr = getbngOutput1().getBNGFileContent(listSelectionIndex);
// Read the data from the cdat/gdat file contents to create a data source for the bngDataPlotPane
ODESolverResultSet odeSolverResultSet = getOdeSolverResultSet(fileContentStr);
getbngDataPlotPanel().setOdeSolverResultSet(odeSolverResultSet);
getbngDataPlotPanel().selectAll();
} else {
((java.awt.CardLayout) getOutputsTextPanel().getLayout()).show(getOutputsTextPanel(), getOutputTextScrollPane().getName());
String fileContentStr = getbngOutput1().getBNGFileContent(listSelectionIndex);
getOutputTextArea().setText(fileContentStr);
}
}
Aggregations