use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class OdeFileWriter method createSymbolTable.
/**
* OdeFileCoder constructor comment.
* @throws Exception
*/
private void createSymbolTable() throws Exception {
//
// Create symbol table for binding sensitivity variable expressions. (Cannot bind to simulation,
// since it does not have the sensitivity variables corresponding to the volume variables).
//
varsSymbolTable = new VariableSymbolTable();
// SymbolTableEntry.index doesn't matter ... just code generating binding by var names not index.
varsSymbolTable.addVar(ReservedVariable.TIME);
int count = 0;
Variable[] variables = simTask.getSimulationJob().getSimulationSymbolTable().getVariables();
for (int i = 0; i < variables.length; i++) {
if (variables[i] instanceof VolVariable) {
VolVariable vVar = (VolVariable) variables[i];
vVar.setIndex(count);
varsSymbolTable.addVar(vVar);
count++;
} else if (variables[i] instanceof Function) {
Function func = (Function) variables[i];
func.setIndex(count);
varsSymbolTable.addVar(func);
count++;
} else if (variables[i] instanceof Constant) {
Constant constant = (Constant) variables[i];
constant.setIndex(count);
varsSymbolTable.addVar(constant);
count++;
} else if (variables[i] instanceof ParameterVariable) {
ParameterVariable param = (ParameterVariable) variables[i];
param.setIndex(count);
varsSymbolTable.addVar(param);
count++;
}
}
// Get the vector of sensVariables, needed for creating SensStateVariables
Vector<SensStateVariable> sensVars = new Vector<SensStateVariable>();
for (int i = 0; i < getStateVariableCount(); i++) {
if (simTask.getSimulation().getSolverTaskDescription().getSensitivityParameter() != null) {
if (getStateVariable(i) instanceof SensStateVariable) {
sensVars.addElement((SensStateVariable) getStateVariable(i));
}
}
}
for (int j = count; j < (count + sensVars.size()); j++) {
SensVariable sVar = (SensVariable) (sensVars.elementAt(j - count).getVariable());
sVar.setIndex(j);
varsSymbolTable.addVar(sVar);
}
}
use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class SensStateVariable method init.
/**
* This method was created in VisualAge.
* @param rateSensitivity cbit.vcell.math.RateSensitivity
* @param jacobian cbit.vcell.math.Jacobian
*/
private void init(RateSensitivity rateSensitivity, Jacobian jacobian, SymbolTable symbolTable, Vector<SensVariable> sensVarArray) throws MathException, ExpressionException {
//
// order sensVars according to the jacobian (rate index)
//
sensVars = (Vector<SensVariable>) sensVarArray.clone();
for (int i = 0; i < sensVarArray.size(); i++) {
SensVariable sensVar = sensVarArray.elementAt(i);
int index = jacobian.getRateIndex(sensVar.getVolVariable());
sensVars.setElementAt(sensVar, index);
}
//
// given the system
//
//
// d Ci
// ------- = Fi(C1..Cn,P1..Pm)
// d t
//
// where: C's are variables and P's are parameters
//
// jacobian.show();
VolVariable var = ((SensVariable) variable).getVolVariable();
Constant parameter = ((SensVariable) variable).getParameter();
//
// get list of jacobian expressions for this var (Ci)
//
// d Fi
// ------ for all j
// d Cj
//
optimizedJacobianExps = new Expression[jacobian.getNumRates()];
int currIndex = jacobian.getRateIndex(var);
for (int i = 0; i < optimizedJacobianExps.length; i++) {
Expression exp = jacobian.getJexp(currIndex, i);
exp.bindExpression(symbolTable);
optimizedJacobianExps[i] = exp.flatten();
}
//
// get rate sensitivity for this variable (Ci) and the parameter under investigation (Pj)
//
// d Fi
// ------
// d Pj
//
//
Expression exp = rateSensitivity.getCPexp(var, parameter.getName());
exp.bindExpression(symbolTable);
optimizedRateSensExp = exp.flatten();
}
use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class ModelOptimizationMapping method computeOptimizationSpec.
/**
* Insert the method's description here.
* Creation date: (8/22/2005 9:26:52 AM)
* @return cbit.vcell.opt.OptimizationSpec
* @param modelOptimizationSpec cbit.vcell.modelopt.ModelOptimizationSpec
*/
MathSymbolMapping computeOptimizationSpec() throws MathException, MappingException {
if (getModelOptimizationSpec().getReferenceData() == null) {
System.out.println("no referenced data defined");
return null;
}
OptimizationSpec optSpec = new OptimizationSpec();
optSpec.setComputeProfileDistributions(modelOptimizationSpec.isComputeProfileDistributions());
parameterMappings = null;
//
// get original MathDescription (later to be substituted for local/global parameters).
//
SimulationContext simContext = modelOptimizationSpec.getSimulationContext();
MathMapping mathMapping = simContext.createNewMathMapping();
MathDescription origMathDesc = null;
mathSymbolMapping = null;
try {
origMathDesc = mathMapping.getMathDescription();
mathSymbolMapping = mathMapping.getMathSymbolMapping();
} catch (MatrixException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ModelException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
// create objective function (mathDesc and data)
//
ReferenceData referenceData = getRemappedReferenceData(mathMapping);
if (referenceData == null) {
throw new RuntimeException("no referenced data defined");
}
//
// get parameter mappings
//
ParameterMappingSpec[] parameterMappingSpecs = modelOptimizationSpec.getParameterMappingSpecs();
Vector<ParameterMapping> parameterMappingList = new Vector<ParameterMapping>();
Variable[] allVars = (Variable[]) BeanUtils.getArray(origMathDesc.getVariables(), Variable.class);
for (int i = 0; i < parameterMappingSpecs.length; i++) {
cbit.vcell.model.Parameter modelParameter = parameterMappingSpecs[i].getModelParameter();
String mathSymbol = null;
Variable mathVariable = null;
if (mathSymbolMapping != null) {
Variable variable = mathSymbolMapping.getVariable(modelParameter);
if (variable != null) {
mathSymbol = variable.getName();
}
if (mathSymbol != null) {
mathVariable = origMathDesc.getVariable(mathSymbol);
}
}
if (mathVariable != null) {
if (parameterMappingSpecs[i].isSelected()) {
if (parameterMappingSpecs[i].getHigh() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getCurrent() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is smaller than its lower bound.");
}
if (parameterMappingSpecs[i].getCurrent() > parameterMappingSpecs[i].getHigh()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getLow() < 0) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All lower bounds must not be negative.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getLow())) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Lower bounds must not be infinity.");
}
if (parameterMappingSpecs[i].getHigh() <= 0) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All upper bounds must be positive.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getHigh())) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Upper bounds must not be infinity.");
}
}
double low = parameterMappingSpecs[i].isSelected() && parameterMappingSpecs[i].getLow() == 0 ? 1e-8 : parameterMappingSpecs[i].getLow();
double high = parameterMappingSpecs[i].getHigh();
double scale = Math.abs(parameterMappingSpecs[i].getCurrent()) < 1.0E-10 ? 1.0 : Math.abs(parameterMappingSpecs[i].getCurrent());
double current = parameterMappingSpecs[i].getCurrent();
low = Math.min(low, current);
high = Math.max(high, current);
Parameter optParameter = new Parameter(mathSymbol, low, high, scale, current);
ParameterMapping parameterMapping = new ParameterMapping(modelParameter, optParameter, mathVariable);
//
if (mathVariable instanceof Constant) {
Constant origConstant = (Constant) mathVariable;
for (int j = 0; j < allVars.length; j++) {
if (allVars[j].equals(origConstant)) {
if (parameterMappingSpecs[i].isSelected()) {
allVars[j] = new ParameterVariable(origConstant.getName());
} else {
allVars[j] = new Constant(origConstant.getName(), new Expression(optParameter.getInitialGuess()));
}
break;
}
}
}
//
if (parameterMappingSpecs[i].isSelected()) {
parameterMappingList.add(parameterMapping);
}
}
}
parameterMappings = (ParameterMapping[]) BeanUtils.getArray(parameterMappingList, ParameterMapping.class);
try {
origMathDesc.setAllVariables(allVars);
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
for (int i = 0; i < parameterMappings.length; i++) {
optSpec.addParameter(parameterMappings[i].getOptParameter());
}
Vector<Issue> issueList = new Vector<Issue>();
IssueContext issueContext = new IssueContext();
optSpec.gatherIssues(issueContext, issueList);
for (int i = 0; i < issueList.size(); i++) {
Issue issue = issueList.elementAt(i);
if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
throw new RuntimeException(issue.getMessage());
}
}
//
//
//
optimizationSpec = optSpec;
return mathSymbolMapping;
}
use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class NFsimXMLWriter method isFunction.
private static boolean isFunction(String candidate, MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
Element listOfParametersElement = new Element("ListOfFunctions");
for (Variable var : simulationSymbolTable.getVariables()) {
Double value = null;
if (var instanceof Constant || var instanceof Function) {
Expression valExpression = var.getExpression();
Expression substitutedValExpr = null;
try {
substitutedValExpr = simulationSymbolTable.substituteFunctions(valExpression);
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
}
try {
value = substitutedValExpr.evaluateConstant();
} catch (ExpressionException e) {
System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
}
if (value != null) {
// parameter, see getListOfParameters() above
continue;
} else {
String current = var.getName();
if (candidate.equals(current)) {
return true;
}
}
}
}
return false;
}
Aggregations