use of cbit.vcell.math.FunctionColumnDescription in project vcell by virtualcell.
the class ParameterEstimationTaskSimulatorIDA method getOdeSolverResultSet.
private ODESolverResultSet getOdeSolverResultSet(ParameterEstimationTask parameterEstimationTask, OptimizationSpec optSpec, OptimizationResultSet optResultSet) throws Exception {
if (optResultSet == null) {
return null;
}
String[] parameterNames = optResultSet.getOptSolverResultSet().getParameterNames();
double[] bestEstimates = optResultSet.getOptSolverResultSet().getBestEstimates();
// if we don't have parameter names or best estimates, return null. if we have them, we can run a simulation and generate a solution
if (parameterNames == null || parameterNames.length == 0 || bestEstimates == null || bestEstimates.length == 0) {
return null;
}
// check if we have solution or not, if not, generate a solution since we have the best estimates
if (optResultSet.getSolutionNames() == null) {
RowColumnResultSet rcResultSet = getRowColumnRestultSetByBestEstimations(parameterEstimationTask, parameterNames, bestEstimates);
optResultSet.setSolutionFromRowColumnResultSet(rcResultSet);
}
String[] solutionNames = optResultSet.getSolutionNames();
if (solutionNames != null && solutionNames.length > 0) {
ODESolverResultSet odeSolverResultSet = new ODESolverResultSet();
// add data column descriptions
for (int i = 0; i < solutionNames.length; i++) {
odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(solutionNames[i]));
}
//
// add row data
//
int numRows = optResultSet.getSolutionValues(0).length;
for (int i = 0; i < numRows; i++) {
odeSolverResultSet.addRow(optResultSet.getSolutionRow(i));
}
//
// make temporary simulation (with overrides for parameter values)
//
MathDescription mathDesc = parameterEstimationTask.getSimulationContext().getMathDescription();
Simulation simulation = new Simulation(mathDesc);
SimulationSymbolTable simSymbolTable = new SimulationSymbolTable(simulation, 0);
//
for (int i = 0; i < optSpec.getParameters().length; i++) {
cbit.vcell.opt.Parameter parameter = optSpec.getParameters()[i];
simulation.getMathOverrides().putConstant(new Constant(parameter.getName(), new Expression(parameter.getInitialGuess())));
}
//
for (int i = 0; i < parameterNames.length; i++) {
simulation.getMathOverrides().putConstant(new Constant(parameterNames[i], new Expression(optResultSet.getOptSolverResultSet().getBestEstimates()[i])));
}
//
// add functions (evaluating them at optimal parameter)
//
Vector<AnnotatedFunction> annotatedFunctions = simSymbolTable.createAnnotatedFunctionsList(mathDesc);
for (AnnotatedFunction f : annotatedFunctions) {
Expression funcExp = f.getExpression();
for (int j = 0; j < parameterNames.length; j++) {
funcExp.substituteInPlace(new Expression(parameterNames[j]), new Expression(optResultSet.getOptSolverResultSet().getBestEstimates()[j]));
}
odeSolverResultSet.addFunctionColumn(new FunctionColumnDescription(funcExp, f.getName(), null, f.getName(), false));
}
return odeSolverResultSet;
} else {
return null;
}
}
use of cbit.vcell.math.FunctionColumnDescription in project vcell by virtualcell.
the class ODESolverPlotSpecificationPanel method initConnections.
/**
* Initializes connections
* @exception java.lang.Exception The exception description.
*/
@SuppressWarnings({ "serial", "unchecked" })
private void initConnections() throws java.lang.Exception {
// user code begin {1}
// user code end
getFilterPanel().addPropertyChangeListener(ivjEventHandler);
getYAxisChoice().addListSelectionListener(ivjEventHandler);
this.addPropertyChangeListener(ivjEventHandler);
getXAxisComboBox_frm().addItemListener(ivjEventHandler);
getLogSensCheckbox().addActionListener(ivjEventHandler);
getSensitivityParameterSlider().addChangeListener(ivjEventHandler);
connPtoP1SetTarget();
connPtoP3SetTarget();
getYAxisChoice().setCellRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
ODEDataInterface mdi = getMyDataInterface();
if (mdi == null) {
return this;
}
String varName = (String) value;
ColumnDescription cd = null;
try {
cd = mdi.getColumnDescription(varName);
} catch (ObjectNotFoundException e) {
e.printStackTrace();
}
if (cd instanceof FunctionColumnDescription && ((FunctionColumnDescription) cd).getIsUserDefined()) {
if (function_icon == null) {
function_icon = new ImageIcon(getClass().getResource("/icons/function_icon.png"));
}
setIcon(function_icon);
}
if (mdi.getDataSymbolMetadataResolver() != null && mdi.getDataSymbolMetadataResolver().getDataSymbolMetadata(varName) != null) {
DataSymbolMetadata dsm = mdi.getDataSymbolMetadataResolver().getDataSymbolMetadata(varName);
String tooltipString = dsm.tooltipString;
if (tooltipString == null) {
tooltipString = varName;
}
setToolTipText(tooltipString);
}
return this;
}
});
}
use of cbit.vcell.math.FunctionColumnDescription 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.FunctionColumnDescription 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.FunctionColumnDescription 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);
}
Aggregations