Search in sources :

Example 16 with Function

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);
}
Also used : Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) Domain(cbit.vcell.math.Variable.Domain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Vector(java.util.Vector)

Example 17 with Function

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);
}
Also used : Function(cbit.vcell.math.Function) Table(com.lowagie.text.Table) Expression(cbit.vcell.parser.Expression) Constant(cbit.vcell.math.Constant) Section(com.lowagie.text.Section)

Example 18 with Function

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);
}
Also used : Constant(cbit.vcell.math.Constant) Dimension(java.awt.Dimension) Section(com.lowagie.text.Section) BufferedImage(java.awt.image.BufferedImage) ExpressionException(cbit.vcell.parser.ExpressionException) DocumentException(com.lowagie.text.DocumentException) ExpressionException(cbit.vcell.parser.ExpressionException) Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) SimulationContext(cbit.vcell.mapping.SimulationContext) GeometryContext(cbit.vcell.mapping.GeometryContext) ReactionContext(cbit.vcell.mapping.ReactionContext)

Example 19 with Function

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;
}
Also used : MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) ReservedVariable(cbit.vcell.math.ReservedVariable) PointVariable(cbit.vcell.math.PointVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) InsideVariable(cbit.vcell.math.InsideVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) FilamentVariable(cbit.vcell.math.FilamentVariable) VolVariable(cbit.vcell.math.VolVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) Constant(cbit.vcell.math.Constant) InsideVariable(cbit.vcell.math.InsideVariable) ExpressionException(cbit.vcell.parser.ExpressionException) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) Function(cbit.vcell.math.Function) MemVariable(cbit.vcell.math.MemVariable) Vector(java.util.Vector) VariableType(cbit.vcell.math.VariableType) VolVariable(cbit.vcell.math.VolVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) FilamentVariable(cbit.vcell.math.FilamentVariable) PointVariable(cbit.vcell.math.PointVariable) OutsideVariable(cbit.vcell.math.OutsideVariable)

Example 20 with Function

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);
}
Also used : ReservedVariable(cbit.vcell.math.ReservedVariable) Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Aggregations

Function (cbit.vcell.math.Function)52 Expression (cbit.vcell.parser.Expression)42 Variable (cbit.vcell.math.Variable)32 ExpressionException (cbit.vcell.parser.ExpressionException)26 Constant (cbit.vcell.math.Constant)24 VolVariable (cbit.vcell.math.VolVariable)22 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)18 MathException (cbit.vcell.math.MathException)16 MathDescription (cbit.vcell.math.MathDescription)15 InsideVariable (cbit.vcell.math.InsideVariable)13 MemVariable (cbit.vcell.math.MemVariable)13 OutsideVariable (cbit.vcell.math.OutsideVariable)13 ReservedVariable (cbit.vcell.math.ReservedVariable)13 Domain (cbit.vcell.math.Variable.Domain)13 MembraneRegionVariable (cbit.vcell.math.MembraneRegionVariable)12 SubDomain (cbit.vcell.math.SubDomain)12 VolumeRegionVariable (cbit.vcell.math.VolumeRegionVariable)12 Vector (java.util.Vector)12 Element (org.jdom.Element)11 SubVolume (cbit.vcell.geometry.SubVolume)9