use of cbit.vcell.math.Function in project vcell by virtualcell.
the class MathTestingUtilities method getOutwardNormalFromInsideOutsideFunction.
/**
* Insert the method's description here.
* Creation date: (1/23/2003 10:30:23 PM)
* @return cbit.vcell.parser.Expression
* @param analyticSubDomainExp cbit.vcell.parser.Expression
*/
public static Function[] getOutwardNormalFromInsideOutsideFunction(Expression insideOutsideFunction, String baseName) throws ExpressionException, MappingException {
java.util.Vector<Function> varList = new java.util.Vector<Function>();
Expression F = new Expression(insideOutsideFunction);
F.bindExpression(null);
F = F.flatten();
if (insideOutsideFunction.isRelational() || insideOutsideFunction.isLogical()) {
throw new RuntimeException("expecting smooth function for analytic function");
}
String F_name = baseName + "_F";
String F_dx_name = baseName + "_F_dx";
Expression F_dx = F.differentiate("x").flatten();
String F_dy_name = baseName + "_F_dy";
Expression F_dy = F.differentiate("y").flatten();
String F_dz_name = baseName + "_F_dz";
Expression F_dz = F.differentiate("z").flatten();
String normalLengthName = baseName + "_F_d_length";
Expression normalLength = new Expression("sqrt(pow(" + F_dx_name + ",2)+pow(" + F_dy_name + ",2)+pow(" + F_dz_name + ",2))");
String distanceToSurfaceName = baseName + "_distance";
Expression distanceToSurface = new Expression("abs(" + F_name + ")/" + normalLengthName);
String normalXName = baseName + "_Nx";
Expression normalX = new Expression(F_dx_name + "/" + normalLengthName);
String normalYName = baseName + "_Ny";
Expression normalY = new Expression(F_dy_name + "/" + normalLengthName);
String normalZName = baseName + "_Nz";
Expression normalZ = new Expression(F_dz_name + "/" + normalLengthName);
Domain domain = null;
varList.add(new Function(F_name, F, domain));
varList.add(new Function(F_dx_name, F_dx, domain));
varList.add(new Function(F_dy_name, F_dy, domain));
varList.add(new Function(F_dz_name, F_dz, domain));
varList.add(new Function(normalLengthName, normalLength, domain));
varList.add(new Function(normalXName, normalX, domain));
varList.add(new Function(normalYName, normalY, domain));
varList.add(new Function(normalZName, normalZ, domain));
varList.add(new Function(distanceToSurfaceName, distanceToSurface, domain));
return (Function[]) BeanUtils.getArray(varList, Function.class);
}
use of cbit.vcell.math.Function in project vcell by virtualcell.
the class ITextWriter method writeMathDescAsText.
// container can be a chapter or a section of a chapter.
// MathDescription.description ignored.
protected void writeMathDescAsText(Section container, MathDescription mathDesc) throws DocumentException {
if (mathDesc == null) {
return;
}
Section mathDescSection = container.addSection("Math Description: " + mathDesc.getName(), container.depth() + 1);
Section mathDescSubSection = null;
Table expTable = null;
int[] widths = { 2, 8 };
// add Constants
Enumeration<Constant> constantsList = mathDesc.getConstants();
while (constantsList.hasMoreElements()) {
Constant constant = constantsList.nextElement();
Expression exp = constant.getExpression();
if (expTable == null) {
expTable = getTable(2, 100, 1, 2, 2);
expTable.addCell(createHeaderCell("Constant Name", getBold(), 1));
expTable.addCell(createHeaderCell("Expression", getBold(), 1));
expTable.setWidths(widths);
expTable.endHeaders();
}
// widths[0] = Math.max(constant.getName().length(), widths[0]);
// widths[1] = Math.max(exp.infix().length(), widths[1]);
expTable.addCell(createCell(constant.getName(), getFont()));
expTable.addCell(createCell(exp.infix(), getFont()));
}
// expTable.setWidths(widths); breaks the contents of the cell, also, widths[1] = widths[1]/widths[0], widths[0] = 1
if (expTable != null) {
mathDescSubSection = mathDescSection.addSection("Constants", mathDescSection.depth() + 1);
mathDescSubSection.add(expTable);
expTable = null;
}
mathDescSubSection = null;
// add functions
Enumeration<Function> functionsList = mathDesc.getFunctions();
while (functionsList.hasMoreElements()) {
Function function = functionsList.nextElement();
Expression exp = function.getExpression();
if (expTable == null) {
expTable = getTable(2, 100, 1, 2, 2);
expTable.addCell(createHeaderCell("Function Name", getBold(), 1));
expTable.addCell(createHeaderCell("Expression", getBold(), 1));
expTable.endHeaders();
expTable.setWidths(widths);
}
expTable.addCell(createCell(function.getName(), getFont()));
expTable.addCell(createCell(exp.infix(), getFont()));
}
if (expTable != null) {
mathDescSubSection = mathDescSection.addSection("Functions", mathDescSection.depth() + 1);
mathDescSubSection.add(expTable);
}
writeSubDomainsEquationsAsText(mathDescSection, mathDesc);
}
use of cbit.vcell.math.Function in project vcell by virtualcell.
the class ITextWriter method writeMathDescAsImages.
// container can be a chapter or a section of a chapter.
// MathDescription.description ignored.
// currently not used.
protected void writeMathDescAsImages(Section container, MathDescription mathDesc) throws DocumentException {
if (mathDesc == null) {
return;
}
Section mathDescSection = container.addSection("Math Description: " + mathDesc.getName(), container.depth() + 1);
Section mathDescSubSection = null;
Expression[] expArray = null;
BufferedImage dummy = new BufferedImage(500, 50, BufferedImage.TYPE_3BYTE_BGR);
int scale = 1;
int viewableWidth = (int) (document.getPageSize().width() - document.leftMargin() - document.rightMargin());
// add Constants
Enumeration<Constant> constantsList = mathDesc.getConstants();
while (constantsList.hasMoreElements()) {
Constant constant = constantsList.nextElement();
Expression exp = constant.getExpression();
try {
expArray = new Expression[] { Expression.assign(new Expression(constant.getName()), exp.flatten()) };
} catch (ExpressionException ee) {
System.err.println("Unable to process constant " + constant.getName() + " for publishing");
ee.printStackTrace();
continue;
}
try {
Dimension dim = ExpressionCanvas.getExpressionImageSize(expArray, (Graphics2D) dummy.getGraphics());
BufferedImage bufferedImage = new BufferedImage((int) dim.getWidth() * scale, (int) dim.getHeight() * scale, BufferedImage.TYPE_3BYTE_BGR);
ExpressionCanvas.getExpressionAsImage(expArray, bufferedImage, scale);
com.lowagie.text.Image expImage = com.lowagie.text.Image.getInstance(bufferedImage, null);
expImage.setAlignment(com.lowagie.text.Image.ALIGN_LEFT);
if (mathDescSubSection == null) {
mathDescSubSection = mathDescSection.addSection("Constants", mathDescSection.depth() + 1);
}
if (viewableWidth < Math.floor(expImage.scaledWidth())) {
expImage.scaleToFit(viewableWidth, expImage.plainHeight());
System.out.println("Constant After scaling: " + expImage.scaledWidth());
}
mathDescSubSection.add(expImage);
} catch (Exception e) {
System.err.println("Unable to add structure mapping image to report.");
e.printStackTrace();
}
}
mathDescSubSection = null;
// add functions
Enumeration<Function> functionsList = mathDesc.getFunctions();
while (functionsList.hasMoreElements()) {
Function function = functionsList.nextElement();
Expression exp = function.getExpression();
try {
expArray = new Expression[] { Expression.assign(new Expression(function.getName()), exp.flatten()) };
} catch (ExpressionException ee) {
System.err.println("Unable to process function " + function.getName() + " for publishing");
ee.printStackTrace();
continue;
}
try {
Dimension dim = ExpressionCanvas.getExpressionImageSize(expArray, (Graphics2D) dummy.getGraphics());
BufferedImage bufferedImage = new BufferedImage((int) dim.getWidth() * scale, (int) dim.getHeight() * scale, BufferedImage.TYPE_3BYTE_BGR);
ExpressionCanvas.getExpressionAsImage(expArray, bufferedImage, scale);
com.lowagie.text.Image expImage = com.lowagie.text.Image.getInstance(bufferedImage, null);
expImage.setAlignment(com.lowagie.text.Image.ALIGN_LEFT);
if (mathDescSubSection == null) {
mathDescSubSection = mathDescSection.addSection("Functions", mathDescSection.depth() + 1);
}
if (viewableWidth < Math.floor(expImage.scaledWidth())) {
expImage.scaleToFit(viewableWidth, expImage.height());
System.out.println("Function After scaling: " + expImage.scaledWidth());
}
mathDescSubSection.add(expImage);
} catch (Exception e) {
System.err.println("Unable to add structure mapping image to report.");
e.printStackTrace();
}
}
writeSubDomainsEquationsAsImages(mathDescSection, mathDesc);
}
use of cbit.vcell.math.Function in project vcell by virtualcell.
the class SimulationSymbolTable method createAnnotatedFunctionsList.
public Vector<AnnotatedFunction> createAnnotatedFunctionsList(MathDescription mathDescription) throws InconsistentDomainException {
// Get the list of (volVariables) in the simulation. Needed to determine 'type' of functions
boolean bSpatial = getSimulation().isSpatial();
String[] variableNames = null;
VariableType[] variableTypes = null;
if (bSpatial) {
Variable[] allVariables = getVariables();
Vector<Variable> varVector = new Vector<Variable>();
for (int i = 0; i < allVariables.length; i++) {
if ((allVariables[i] instanceof VolVariable) || (allVariables[i] instanceof VolumeRegionVariable) || (allVariables[i] instanceof MemVariable) || (allVariables[i] instanceof MembraneRegionVariable) || (allVariables[i] instanceof FilamentVariable) || (allVariables[i] instanceof FilamentRegionVariable) || (allVariables[i] instanceof PointVariable) || (allVariables[i] instanceof ParticleVariable) || (allVariables[i] instanceof InsideVariable) || (allVariables[i] instanceof OutsideVariable)) {
varVector.addElement(allVariables[i]);
} else if (allVariables[i] instanceof Constant || (allVariables[i] instanceof Function)) {
} else {
System.err.println("SimulationSymbolTable.createAnnotatedFunctionsList() found unexpected variable type " + allVariables[i].getClass().getSimpleName() + " in spatial simulation");
}
}
variableNames = new String[varVector.size()];
for (int i = 0; i < variableNames.length; i++) {
variableNames[i] = varVector.get(i).getName();
}
// Lookup table for variableType for each variable in 'variables' array.
variableTypes = new VariableType[variableNames.length];
for (int i = 0; i < variableNames.length; i++) {
variableTypes[i] = VariableType.getVariableType(varVector.get(i));
}
}
//
// Bind and substitute functions to simulation before storing them in the '.functions' file
//
Function[] functions = getFunctions();
Vector<AnnotatedFunction> annotatedFunctionVector = new Vector<AnnotatedFunction>();
for (int i = 0; i < functions.length; i++) {
if (isFunctionSaved(functions[i])) {
String errString = "";
VariableType funcType = null;
try {
Expression substitutedExp = substituteFunctions(functions[i].getExpression());
substitutedExp.bindExpression(this);
functions[i].setExpression(substitutedExp.flatten());
} catch (MathException e) {
e.printStackTrace(System.out);
errString = errString + ", " + e.getMessage();
// throw new RuntimeException(e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
errString = errString + ", " + e.getMessage();
// throw new RuntimeException(e.getMessage());
}
//
// get function's data type from the types of it's identifiers
//
funcType = bSpatial ? getFunctionVariableType(functions[i], mathDescription, variableNames, variableTypes, bSpatial) : VariableType.NONSPATIAL;
AnnotatedFunction annotatedFunc = new AnnotatedFunction(functions[i].getName(), functions[i].getExpression(), functions[i].getDomain(), errString, funcType, FunctionCategory.PREDEFINED);
annotatedFunctionVector.addElement(annotatedFunc);
}
}
return annotatedFunctionVector;
}
use of cbit.vcell.math.Function 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