use of cbit.vcell.math.ParameterVariable in project vcell by virtualcell.
the class FiniteVolumeFileWriter method write.
/**
* Insert the method's description here.
* Creation date: (5/9/2005 2:52:48 PM)
* @throws MathException
* @throws ExpressionException
* @throws DataAccessException
* @throws IOException
*/
public void write(String[] parameterNames) throws Exception {
Variable[] originalVars = null;
Simulation simulation = simTask.getSimulation();
MathDescription mathDesc = simulation.getMathDescription();
if (bChomboSolver) {
writeJMSParamters();
writeSimulationParamters();
writeModelDescription();
writeChomboSpec();
writeVariables();
writePostProcessingBlock();
writeCompartments();
writeMembranes();
} else {
if (simTask.getSimulation().isSerialParameterScan()) {
originalVars = (Variable[]) BeanUtils.getArray(mathDesc.getVariables(), Variable.class);
Variable[] allVars = (Variable[]) BeanUtils.getArray(mathDesc.getVariables(), Variable.class);
MathOverrides mathOverrides = simulation.getMathOverrides();
String[] scanParameters = mathOverrides.getOverridenConstantNames();
for (int i = 0; i < scanParameters.length; i++) {
String scanParameter = scanParameters[i];
Variable mathVariable = mathDesc.getVariable(scanParameter);
//
if (mathVariable instanceof Constant) {
Constant origConstant = (Constant) mathVariable;
for (int j = 0; j < allVars.length; j++) {
if (allVars[j].equals(origConstant)) {
allVars[j] = new ParameterVariable(origConstant.getName());
break;
}
}
}
}
mathDesc.setAllVariables(allVars);
}
writeJMSParamters();
writeSimulationParamters();
writeModelDescription();
writeMeshFile();
writeVariables();
if (mathDesc.isSpatialHybrid()) {
writeSmoldyn();
}
writeParameters(parameterNames);
writeSerialParameterScans();
writeFieldData();
writePostProcessingBlock();
writeCompartments();
writeMembranes();
if (originalVars != null) {
mathDesc.setAllVariables(originalVars);
}
}
}
use of cbit.vcell.math.ParameterVariable 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.ParameterVariable 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;
}
Aggregations