use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class NFsimXMLWriter method getListOfFunctions.
private static Element getListOfFunctions(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");
}
Element functionElement = new Element("Function");
functionElement.setAttribute("id", var.getName());
if (value != null) {
// parameter, see getListOfParameters() above
continue;
} else {
Element listOfReferencesElement = new Element("ListOfReferences");
String[] references = valExpression.getSymbols();
for (int i = 0; i < references.length; i++) {
String reference = references[i];
Element referenceElement = new Element("Reference");
referenceElement.setAttribute("name", reference);
Variable referenceVariable = simulationSymbolTable.getVariable(reference);
Double referenceValue = null;
Expression referenceExpression = referenceVariable.getExpression();
Expression substitutedReferenceExpression = null;
if (referenceExpression != null) {
try {
substitutedReferenceExpression = simulationSymbolTable.substituteFunctions(referenceExpression);
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
}
try {
referenceValue = substitutedReferenceExpression.evaluateConstant();
} catch (ExpressionException e) {
System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
}
}
if (referenceVariable instanceof ParticleObservable) {
referenceElement.setAttribute("type", "Observable");
} else if (referenceVariable instanceof Function) {
if (referenceValue != null) {
referenceElement.setAttribute("type", "ConstantExpression");
} else {
referenceElement.setAttribute("type", "Function");
}
} else {
// constant
referenceElement.setAttribute("type", "ConstantExpression");
}
listOfReferencesElement.addContent(referenceElement);
}
functionElement.addContent(listOfReferencesElement);
Element expressionElement = new Element("Expression");
String functionExpression = valExpression.infix();
expressionElement.setText(functionExpression);
functionElement.addContent(expressionElement);
}
listOfParametersElement.addContent(functionElement);
}
}
return listOfParametersElement;
}
use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class NFsimXMLWriter method getListOfParameters.
private static Element getListOfParameters(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
Element listOfParametersElement = new Element("ListOfParameters");
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");
}
Element parameterElement = new Element("Parameter");
parameterElement.setAttribute("id", var.getName());
if (value != null) {
parameterElement.setAttribute("type", "Constant");
parameterElement.setAttribute("value", value.toString());
} else {
// function, see getListOfFunctions() below
continue;
}
listOfParametersElement.addContent(parameterElement);
}
}
return listOfParametersElement;
}
use of cbit.vcell.math.Constant in project vcell by virtualcell.
the class ParameterEstimationTaskSimulatorIDA method getRowColumnRestultSetByBestEstimations.
public RowColumnResultSet getRowColumnRestultSetByBestEstimations(ParameterEstimationTask parameterEstimationTask, String[] paramNames, double[] paramValues) throws Exception {
// create a temp simulation based on math description
KeyValue key = new KeyValue("" + Math.abs(new Random().nextLong()));
SimulationVersion dummyVersion = new SimulationVersion(key, "name", new User("temp", new KeyValue("1")), null, null, null, null, null, null, null);
Simulation simulation = new Simulation(dummyVersion, parameterEstimationTask.getSimulationContext().getMathDescription());
ReferenceData refData = parameterEstimationTask.getModelOptimizationSpec().getReferenceData();
double[] times = refData.getDataByColumn(0);
double endTime = times[times.length - 1];
ExplicitOutputTimeSpec exTimeSpec = new ExplicitOutputTimeSpec(times);
// set simulation ending time and output interval
simulation.getSolverTaskDescription().setTimeBounds(new TimeBounds(0, endTime));
simulation.getSolverTaskDescription().setOutputTimeSpec(exTimeSpec);
// set parameters as math overrides
MathOverrides mathOverrides = simulation.getMathOverrides();
for (int i = 0; i < paramNames.length; i++) {
mathOverrides.putConstant(new Constant(paramNames[i], new Expression(paramValues[i])));
}
SimulationTask simTask = new SimulationTask(new SimulationJob(simulation, 0, null), 0);
IDASolverStandalone idaSolver = new IDASolverStandalone(simTask, ResourceUtil.getLocalSimDir("temp"), false);
// startSolver();
idaSolver.runSolver();
Thread.sleep(1000);
long startTimeMS = System.currentTimeMillis();
while (idaSolver.getSolverStatus().isRunning() && System.currentTimeMillis() < (startTimeMS + 10000L)) {
Thread.sleep(500);
}
ODESolverResultSet resultset = idaSolver.getODESolverResultSet();
return resultset;
}
use of cbit.vcell.math.Constant 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.Constant in project vcell by virtualcell.
the class Xmlproducer method getXML.
/**
* This method returns a XML representation of a MathDescription object.
* Creation date: (3/2/2001 10:57:25 AM)
* @return Element
* @param mathdes cbit.vcell.math.MathDescription
*/
Element getXML(MathDescription mathdes) throws XmlParseException {
Element math = new Element(XMLTags.MathDescriptionTag);
// Add attributes
math.setAttribute(XMLTags.NameAttrTag, mangle(mathdes.getName()));
// Add annotation
if (mathdes.getDescription() != null && mathdes.getDescription().length() > 0) {
Element annotationElem = new Element(XMLTags.AnnotationTag);
annotationElem.setText(mangle(mathdes.getDescription()));
math.addContent(annotationElem);
}
List<ParticleMolecularType> particleMolecularTypes = mathdes.getParticleMolecularTypes();
for (ParticleMolecularType particleMolecularType : particleMolecularTypes) {
math.addContent(getXML(particleMolecularType));
}
// Add Constant subelements
Enumeration<Variable> enum1 = mathdes.getVariables();
/*java.util.Iterator k;
try {
VariableHash varHash = new VariableHash();
while (enum1.hasMoreElements())
varHash.addVariable((Variable)enum1.nextElement());
Variable vars [] = varHash.getReorderedVariables();
k = new ArrayList(java.util.Arrays.asList(vars)).iterator();
} catch (cbit.vcell.mapping.MappingException e) {
e.printStackTrace();
return null;
}*/
while (enum1.hasMoreElements()) {
Variable var = enum1.nextElement();
Element element = null;
if (var instanceof Constant) {
element = getXML((Constant) var);
} else if (var instanceof FilamentRegionVariable) {
element = getXML((FilamentRegionVariable) var);
} else if (var instanceof FilamentVariable) {
element = getXML((FilamentVariable) var);
} else if (var instanceof PointVariable) {
element = getXML((PointVariable) var);
} else if (var instanceof Function) {
element = getXML((Function) var);
} else if (var instanceof RandomVariable) {
element = getXML((RandomVariable) var);
} else if (var instanceof InsideVariable) {
// *** for internal use! Ignore it ***
continue;
} else if (var instanceof MembraneRegionVariable) {
element = getXML((MembraneRegionVariable) var);
} else if (var instanceof MemVariable) {
element = getXML((MemVariable) var);
} else if (var instanceof OutsideVariable) {
// *** for internal use! Ignore it ****
continue;
} else if (var instanceof VolumeRegionVariable) {
element = getXML((VolumeRegionVariable) var);
} else if (var instanceof VolVariable) {
element = getXML((VolVariable) var);
} else if (var instanceof StochVolVariable) {
// added for stochastic volumn variables
element = getXML((StochVolVariable) var);
} else if (var instanceof ParticleVariable) {
element = getXML((ParticleVariable) var);
} else if (var instanceof ParticleObservable) {
element = getXML((ParticleObservable) var);
} else {
throw new XmlParseException("An unknown variable type " + var.getClass().getName() + " was found when parsing the mathdescription " + mathdes.getName() + "!");
}
transcribeComments(var, element);
math.addContent(element);
}
// this was moved to the simspec!
/* buffer.append("\n");
if (geometry != null){
buffer.append(geometry.getXML());
}
buffer.append("\n");*/
// Add subdomains
Enumeration<SubDomain> enum2 = mathdes.getSubDomains();
while (enum2.hasMoreElements()) {
SubDomain subDomain = enum2.nextElement();
math.addContent(getXML(subDomain));
}
// Add Metadata (Version) if there is!
if (mathdes.getVersion() != null) {
math.addContent(getXML(mathdes.getVersion(), mathdes));
}
Iterator<Event> iter = mathdes.getEvents();
while (iter.hasNext()) {
math.addContent(getXML(iter.next()));
}
PostProcessingBlock postProcessingBlock = mathdes.getPostProcessingBlock();
if (postProcessingBlock.getNumDataGenerators() > 0) {
math.addContent(getXML(postProcessingBlock));
}
return math;
}
Aggregations