use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class MathOverrides method isValid.
/**
* Insert the method's description here.
* Creation date: (5/4/01 11:44:22 AM)
* @return boolean
*/
public boolean isValid(MathDescription mathDescription) {
//
// returns true if MathOverrides has exactly the same list of Constants as MathDescription
//
//
// look for Constants from MathDescription not present in Overrides
//
Enumeration<Constant> enumeration = mathDescription.getConstants();
java.util.HashSet<String> mathDescriptionHash = new java.util.HashSet<String>();
while (enumeration.hasMoreElements()) {
Constant constant = enumeration.nextElement();
mathDescriptionHash.add(constant.getName());
if (!getOverridesHash().containsKey(constant.getName())) {
return false;
}
}
//
// look for Constants from Overrides not present in MathDescription
//
Enumeration<String> mathOverrideNamesEnum = getOverridesHash().keys();
while (mathOverrideNamesEnum.hasMoreElements()) {
String name = mathOverrideNamesEnum.nextElement();
if (!mathDescriptionHash.contains(name)) {
return false;
}
}
return true;
}
use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class OutputFunctionContext method getEntries.
// public abstract void validateNamingConflicts(String symbolDescription, Class<?> newSymbolClass, String newSymbolName, PropertyChangeEvent e) throws PropertyVetoException ;
public void getEntries(Map<String, SymbolTableEntry> entryMap) {
// add all valid entries (variables) from mathdescription
MathDescription mathDescription = simulationOwner.getMathDescription();
if (mathDescription != null) {
Enumeration<Variable> varEnum = mathDescription.getVariables();
while (varEnum.hasMoreElements()) {
Variable var = varEnum.nextElement();
if (!(var instanceof PseudoConstant) && !(var instanceof Constant)) {
entryMap.put(var.getName(), var);
}
}
for (DataGenerator dataGenerator : mathDescription.getPostProcessingBlock().getDataGeneratorList()) {
entryMap.put(dataGenerator.getName(), dataGenerator);
}
}
entryMap.put(ReservedVariable.TIME.getName(), ReservedVariable.TIME);
int dimension = mathDescription.getGeometry().getDimension();
if (dimension > 0) {
entryMap.put(ReservedVariable.X.getName(), ReservedVariable.X);
if (dimension > 1) {
entryMap.put(ReservedVariable.Y.getName(), ReservedVariable.Y);
if (dimension > 2) {
entryMap.put(ReservedVariable.Z.getName(), ReservedVariable.Z);
}
}
}
// then add list of output functions.
for (SymbolTableEntry ste : outputFunctionsList) {
entryMap.put(ste.getName(), ste);
}
}
use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class MathTestingUtilities method constructOdesForSensitivity.
//
// This method is used to solve for sensitivity of variables to a given parameter.
// The mathDescription and the sensitivity parameter are passed as arguments.
// New variables and ODEs are constructed according to the rule listed below and are added to the mathDescription.
// The method returns the modified mathDescription.
//
public static MathDescription constructOdesForSensitivity(MathDescription mathDesc, Constant sensParam) throws ExpressionException, MathException, MappingException {
//
// For each ODE :
//
// dX/dt = F(X, P)
//
// (where P is the sensitivity parameter)
//
// we create two other ODEs :
//
// dX_1/dt = F(X_1, P_1) and
//
// dX_2/dt = F(X_2, P_2)
//
// where P_1 = P + epsilon, and
// P_2 = P - epsilon.
//
// We keep the initial conditions for both the new ODEs to be the same,
// i.e., X_1_init = X_2_init.
//
// Then, solving for X_1 & X_2, sensitivity of X wrt P can be computed as :
//
// dX = (X_1 - X_2)
// -- ----------- .
// dP (P_1 - P_2)
//
//
// REMOVE PRINTS AFTER CHECKING !!!
System.out.println(" \n\n------------ Old Math Description -----------------");
System.out.println(mathDesc.getVCML_database());
if (mathDesc.getGeometry().getDimension() > 0) {
throw new RuntimeException("Suppport for Spatial systems not yet implemented.");
}
VariableHash varHash = new VariableHash();
Enumeration<Variable> enumVar = mathDesc.getVariables();
while (enumVar.hasMoreElements()) {
varHash.addVariable(enumVar.nextElement());
}
//
// Get 2 values of senstivity parameter (P + epsilon) & (P - epsilon)
//
Constant epsilon = new Constant("epsilon", new Expression(sensParam.getConstantValue() * 1e-3));
Constant sensParam1 = new Constant(sensParam.getName() + "_1", new Expression(sensParam.getConstantValue() + epsilon.getConstantValue()));
Constant sensParam2 = new Constant(sensParam.getName() + "_2", new Expression(sensParam.getConstantValue() - epsilon.getConstantValue()));
//
// Iterate through each subdomain (only 1 in compartmental case), and each equation in the subdomain
//
Enumeration<SubDomain> subDomainEnum = mathDesc.getSubDomains();
//
// Create a vector of equations to store the 2 equations for each ODE variable in the subdomain.
// Later, add it to the equations list in the subdomain.
//
Vector<Equation> equnsVector = new Vector<Equation>();
Vector<Variable> varsVector = new Vector<Variable>();
Vector<Variable> var1s = new Vector<Variable>();
Vector<Variable> var2s = new Vector<Variable>();
while (subDomainEnum.hasMoreElements()) {
SubDomain subDomain = subDomainEnum.nextElement();
Enumeration<Equation> equationEnum = subDomain.getEquations();
Domain domain = new Domain(subDomain);
while (equationEnum.hasMoreElements()) {
Equation equation = equationEnum.nextElement();
if (equation instanceof OdeEquation) {
OdeEquation odeEquation = (OdeEquation) equation;
// Similar to substituteWithExactSolutions, to bind and substitute functions in the ODE
Expression substitutedRateExp = substituteFunctions(odeEquation.getRateExpression(), mathDesc);
String varName = odeEquation.getVariable().getName();
VolVariable var = new VolVariable(varName, domain);
varsVector.addElement(var);
//
// Create the variable var1, and get the initExpr and rateExpr from the original ODE.
// Substitute the new vars (var1 and param1) in the old initExpr and rateExpr and create a new ODE
//
String varName1 = new String("__" + varName + "_1");
Expression initExpr1 = odeEquation.getInitialExpression();
Expression rateExpr1 = new Expression(substitutedRateExp);
rateExpr1.substituteInPlace(new Expression(varName), new Expression(varName1));
rateExpr1.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam1.getName()));
VolVariable var1 = new VolVariable(varName1, domain);
var1s.addElement(var1);
OdeEquation odeEqun1 = new OdeEquation(var1, initExpr1, rateExpr1);
equnsVector.addElement(odeEqun1);
//
// Create the variable var2, and get the initExpr and rateExpr from the original ODE.
// Substitute the new vars (var2 and param2) in the old initExpr and rateExpr and create a new ODE
//
String varName2 = new String("__" + varName + "_2");
Expression initExpr2 = odeEquation.getInitialExpression();
Expression rateExpr2 = new Expression(substitutedRateExp);
rateExpr2.substituteInPlace(new Expression(varName), new Expression(varName2));
rateExpr2.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam2.getName()));
VolVariable var2 = new VolVariable(varName2, domain);
var2s.addElement(var2);
OdeEquation odeEqun2 = new OdeEquation(var2, initExpr2, rateExpr2);
equnsVector.addElement(odeEqun2);
//
// Create a function for the sensitivity function expression (X1-X2)/(P1-P2), and save in varHash
//
Expression diffVar = Expression.add(new Expression(var1.getName()), Expression.negate(new Expression(var2.getName())));
Expression diffParam = Expression.add(new Expression(sensParam1.getName()), Expression.negate(new Expression(sensParam2.getName())));
Expression sensitivityExpr = Expression.mult(diffVar, Expression.invert(diffParam));
Function sens_Func = new Function("__sens" + varName + "_wrt_" + sensParam.getName(), sensitivityExpr, domain);
varHash.addVariable(epsilon);
varHash.addVariable(sensParam1);
varHash.addVariable(sensParam2);
varHash.addVariable(var1);
varHash.addVariable(var2);
varHash.addVariable(sens_Func);
} else {
// sensitivity not implemented for PDEs or other equation types.
throw new RuntimeException("SolverTest.constructedExactMath(): equation type " + equation.getClass().getName() + " not yet implemented");
}
}
//
// Need to substitute the new variables in the new ODEs.
// i.e., if Rate Expr for ODE_1 for variable X_1 contains variable Y, variable Z, etc.
// Rate Expr is already substituted with X_1, but it also needs substitute Y with Y_1, Z with Z_1, etc.
// So get the volume variables, from the vectors for vars, var_1s and var_2s
// Substitute the rate expressions for the newly added ODEs in equnsVector.
//
Variable[] vars = (Variable[]) BeanUtils.getArray(varsVector, Variable.class);
Variable[] var_1s = (Variable[]) BeanUtils.getArray(var1s, Variable.class);
Variable[] var_2s = (Variable[]) BeanUtils.getArray(var2s, Variable.class);
Vector<Equation> newEqunsVector = new Vector<Equation>();
for (int i = 0; i < equnsVector.size(); i++) {
Equation equn = equnsVector.elementAt(i);
Expression initEx = equn.getInitialExpression();
Expression rateEx = equn.getRateExpression();
for (int j = 0; j < vars.length; j++) {
if (equn.getVariable().getName().endsWith("_1")) {
rateEx.substituteInPlace(new Expression(vars[j].getName()), new Expression(var_1s[j].getName()));
} else if (equn.getVariable().getName().endsWith("_2")) {
rateEx.substituteInPlace(new Expression(vars[j].getName()), new Expression(var_2s[j].getName()));
}
}
OdeEquation odeEqun = new OdeEquation(equn.getVariable(), initEx, rateEx);
newEqunsVector.addElement(odeEqun);
}
//
for (int i = 0; i < newEqunsVector.size(); i++) {
mathDesc.getSubDomain(subDomain.getName()).addEquation((Equation) newEqunsVector.elementAt(i));
}
//
// FAST SYSTEM
// If the subdomain has a fast system, create a new fast system by substituting the high-low variables/parameters
// in the expressions for the fastInvariants and fastRates and adding them to the fast system.
//
Vector<FastInvariant> invarsVector = new Vector<FastInvariant>();
Vector<FastRate> ratesVector = new Vector<FastRate>();
Enumeration<FastInvariant> fastInvarsEnum = null;
Enumeration<FastRate> fastRatesEnum = null;
// Get the fast invariants and fast rates in the system.
FastSystem fastSystem = subDomain.getFastSystem();
if (fastSystem != null) {
fastInvarsEnum = fastSystem.getFastInvariants();
fastRatesEnum = fastSystem.getFastRates();
//
while (fastInvarsEnum.hasMoreElements()) {
FastInvariant fastInvar = fastInvarsEnum.nextElement();
Expression fastInvarExpr = fastInvar.getFunction();
fastInvarExpr = MathUtilities.substituteFunctions(fastInvarExpr, mathDesc);
Expression fastInvarExpr1 = new Expression(fastInvarExpr);
Expression fastInvarExpr2 = new Expression(fastInvarExpr);
for (int i = 0; i < vars.length; i++) {
fastInvarExpr1.substituteInPlace(new Expression(vars[i].getName()), new Expression(var_1s[i].getName()));
fastInvarExpr2.substituteInPlace(new Expression(vars[i].getName()), new Expression(var_2s[i].getName()));
}
fastInvarExpr1.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam1.getName()));
FastInvariant fastInvar1 = new FastInvariant(fastInvarExpr1);
invarsVector.addElement(fastInvar1);
fastInvarExpr2.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam2.getName()));
FastInvariant fastInvar2 = new FastInvariant(fastInvarExpr2);
invarsVector.addElement(fastInvar2);
}
// Add the newly created fast invariants to the existing list of fast invariants in the fast system.
for (int i = 0; i < invarsVector.size(); i++) {
FastInvariant inVar = (FastInvariant) invarsVector.elementAt(i);
fastSystem.addFastInvariant(inVar);
}
//
while (fastRatesEnum.hasMoreElements()) {
FastRate fastRate = fastRatesEnum.nextElement();
Expression fastRateExpr = fastRate.getFunction();
fastRateExpr = MathUtilities.substituteFunctions(fastRateExpr, mathDesc);
Expression fastRateExpr1 = new Expression(fastRateExpr);
Expression fastRateExpr2 = new Expression(fastRateExpr);
for (int i = 0; i < vars.length; i++) {
fastRateExpr1.substituteInPlace(new Expression(vars[i].getName()), new Expression(var_1s[i].getName()));
fastRateExpr2.substituteInPlace(new Expression(vars[i].getName()), new Expression(var_2s[i].getName()));
}
fastRateExpr1.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam1.getName()));
FastRate fastRate1 = new FastRate(fastRateExpr1);
ratesVector.addElement(fastRate1);
fastRateExpr2.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam2.getName()));
FastRate fastRate2 = new FastRate(fastRateExpr2);
ratesVector.addElement(fastRate2);
}
// Add the newly created fast rates to the existing list of fast rates in the fast system.
for (int i = 0; i < ratesVector.size(); i++) {
FastRate rate = (FastRate) ratesVector.elementAt(i);
fastSystem.addFastRate(rate);
}
}
}
// Reset all variables in mathDesc.
mathDesc.setAllVariables(varHash.getAlphabeticallyOrderedVariables());
// REMOVE PRINTS AFTER CHECKING
System.out.println(" \n\n------------ New Math Description -----------------");
System.out.println(mathDesc.getVCML_database());
return mathDesc;
}
use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class VCellCopyPasteHelper method chooseApplyPaste.
/**
* Insert the method's description here.
* Creation date: (7/10/2006 11:05:19 AM)
*/
public static void chooseApplyPaste(Component requester, String[] pasteDetails, MathOverrides mathOverrides, String[] changingMathOverridesNames, java.util.Vector<?> newMathOverridesValuesV) {
if (pasteDetails.length != changingMathOverridesNames.length || changingMathOverridesNames.length != newMathOverridesValuesV.size()) {
throw new IllegalArgumentException(VCellCopyPasteHelper.class.getName() + ".chooseApplyPaste(...) arguments must have unequal lengths");
}
// Only present things that will actually change
boolean bAtLeatOneDifferent = false;
boolean[] bEnableDisplay = new boolean[changingMathOverridesNames.length];
for (int i = 0; i < changingMathOverridesNames.length; i += 1) {
Object newValue = newMathOverridesValuesV.elementAt(i);
if (newValue instanceof Expression) {
bEnableDisplay[i] = !Compare.isEqualOrNull(mathOverrides.getActualExpression(changingMathOverridesNames[i], 0), ((Expression) newValue));
} else if (newValue instanceof ConstantArraySpec) {
bEnableDisplay[i] = !Compare.isEqualOrNull(mathOverrides.getConstantArraySpec(changingMathOverridesNames[i]), (ConstantArraySpec) newValue);
} else {
PopupGenerator.showErrorDialog(requester, "Unexpected MathOverride type=" + newValue.getClass().getName() + "\nPaste Failed, nothing changed.");
return;
}
bAtLeatOneDifferent = bAtLeatOneDifferent || bEnableDisplay[i];
}
if (!bAtLeatOneDifferent) {
PopupGenerator.showInfoDialog(requester, "All valid paste values are equal to the destination values.\nNo paste needed.");
return;
}
boolean[] bChoices = showChoices(requester, pasteDetails, bEnableDisplay);
if (bChoices != null) {
StringBuffer statusMessages = new StringBuffer();
boolean bFailure = false;
for (int i = 0; i < changingMathOverridesNames.length; i += 1) {
try {
if (bChoices[i]) {
if (newMathOverridesValuesV.elementAt(i) instanceof Expression) {
mathOverrides.putConstant(new Constant(changingMathOverridesNames[i], (Expression) newMathOverridesValuesV.elementAt(i)));
} else if (newMathOverridesValuesV.elementAt(i) instanceof ConstantArraySpec) {
mathOverrides.putConstantArraySpec((ConstantArraySpec) newMathOverridesValuesV.elementAt(i));
}
}
statusMessages.append("(OK) " + pasteDetails + "\n");
} catch (Exception e) {
bFailure = true;
statusMessages.append("(Failed) " + pasteDetails + " " + e.getMessage() + " " + e.getClass().getName() + "\n");
}
}
if (bFailure) {
PopupGenerator.showErrorDialog(requester, "Paste Results:\n" + statusMessages.toString());
}
}
}
use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class SimResultsViewer method initialize.
/**
* Insert the method's description here.
* Creation date: (10/17/2005 11:37:52 PM)
* @exception org.vcell.util.DataAccessException The exception description.
*/
private void initialize() throws DataAccessException {
// create main viewer for jobIndex 0 and wire it up
if (isODEData) {
setMainViewer(createODEDataViewer());
} else {
setMainViewer(createPDEDataViewer());
}
java.beans.PropertyChangeListener pcl = new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
if (evt.getSource() == SimResultsViewer.this && (evt.getPropertyName().equals("dataViewerManager"))) {
try {
getMainViewer().setDataViewerManager(getDataViewerManager());
} catch (java.beans.PropertyVetoException exc) {
exc.printStackTrace();
}
}
if (evt.getSource() == SimResultsViewer.this && (evt.getPropertyName().equals("simulationModelInfo"))) {
getMainViewer().setSimulationModelInfo(getSimulationModelInfo());
}
}
};
addPropertyChangeListener(pcl);
// if necessarry, create parameter choices panel and wire it up
if (getSimulation().getScanCount() > 1) {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(5, 0));
panel.setBorder(BorderFactory.createEtchedBorder());
JLabel label = new JLabel("<html><b>Choose Parameter Values</b></html>");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBorder(BorderFactory.createEmptyBorder(2, 2, 0, 2));
panel.add(label, BorderLayout.NORTH);
String[] scanParams = getSimulation().getMathOverrides().getScannedConstantNames();
Arrays.sort(scanParams);
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.X_AXIS));
for (int i = 0; i < scanParams.length; i++) {
Constant[] scanConstants = getSimulation().getMathOverrides().getConstantArraySpec(scanParams[i]).getConstants();
String[][] values = new String[scanConstants.length][1];
for (int j = 0; j < scanConstants.length; j++) {
values[j][0] = scanConstants[j].getExpression().infix();
}
class ScanChoicesTableModel extends javax.swing.table.AbstractTableModel {
String[] columnNames;
Object[][] rowData;
ScanChoicesTableModel(Object[][] argData, String[] argNames) {
columnNames = argNames;
rowData = argData;
}
public String getColumnName(int column) {
return columnNames[column].toString();
}
public int getRowCount() {
return rowData.length;
}
public int getColumnCount() {
return columnNames.length;
}
public Object getValueAt(int row, int col) {
return rowData[row][col];
}
public boolean isCellEditable(int row, int column) {
return false;
}
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value;
fireTableCellUpdated(row, col);
}
}
;
ScanChoicesTableModel tm = new ScanChoicesTableModel(values, new String[] { scanParams[i] });
final JTable table = new JTable(tm);
choicesHash.put(scanParams[i], table);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().setSelectionInterval(0, 0);
final ListSelectionListener[] nextListSelectionListener = new ListSelectionListener[1];
nextListSelectionListener[0] = new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
DefaultListSelectionModel list = (DefaultListSelectionModel) e.getSource();
int selected = list.getAnchorSelectionIndex();
final int previous = (selected == e.getFirstIndex() ? e.getLastIndex() : e.getFirstIndex());
ListReset listReset = new ListReset() {
@Override
public void reset(VCDataIdentifier myVcDataIdentifier) {
if (myVcDataIdentifier instanceof VCSimulationDataIdentifier) {
int paramScanIndex = ((VCSimulationDataIdentifier) myVcDataIdentifier).getJobIndex();
table.getSelectionModel().removeListSelectionListener(nextListSelectionListener[0]);
try {
table.setRowSelectionInterval(paramScanIndex, paramScanIndex);
} finally {
table.getSelectionModel().addListSelectionListener(nextListSelectionListener[0]);
}
} else {
table.setRowSelectionInterval(previous, previous);
}
}
};
updateScanParamChoices("SimResultsViewer set paramScan index=" + getSelectedParamScanJobIndex(), listReset);
}
}
};
table.getSelectionModel().addListSelectionListener(nextListSelectionListener[0]);
JScrollPane scr = new JScrollPane(table);
JPanel p = new JPanel();
scr.setPreferredSize(new java.awt.Dimension(100, Math.min(150, table.getPreferredSize().height + table.getTableHeader().getPreferredSize().height + 5)));
p.setLayout(new java.awt.BorderLayout());
p.add(scr, java.awt.BorderLayout.CENTER);
p.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
tablePanel.add(p);
}
panel.add(tablePanel, BorderLayout.CENTER);
if (isODEData) {
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton button = new JButton("Time Plot with Multiple Parameter Value Sets");
buttonPanel.add(button);
panel.add(buttonPanel, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainViewer.showTimePlotMultipleScans(dataManager);
}
});
} else {
pdeDataViewer.setSimNameSimDataID(new ExportSpecs.SimNameSimDataID(getSimulation().getName(), getSimulation().getSimulationInfo().getAuthoritativeVCSimulationIdentifier(), SimResultsViewer.getParamScanInfo(getSimulation(), getSelectedParamScanJobIndex())));
}
setParamChoicesPanel(panel);
}
// put things together
setLayout(new java.awt.BorderLayout());
add(getMainViewer(), java.awt.BorderLayout.CENTER);
if (getSimulation().getScanCount() > 1) {
add(getParamChoicesPanel(), java.awt.BorderLayout.SOUTH);
}
}
Aggregations