Search in sources :

Example 21 with Constant

use of cbit.vcell.math.Constant in project vcell by virtualcell.

the class ITextWriter method writeSimulation.

// container can be a chapter or a section of a chapter.
protected void writeSimulation(Section container, Simulation sim) throws DocumentException {
    if (sim == null) {
        return;
    }
    Section simSection = container.addSection(sim.getName(), container.numberDepth() + 1);
    writeMetadata(simSection, sim.getName(), sim.getDescription(), null, "Simulation ");
    // add overriden params
    Table overParamTable = null;
    MathOverrides mo = sim.getMathOverrides();
    if (mo != null) {
        String[] constants = mo.getOverridenConstantNames();
        for (int i = 0; i < constants.length; i++) {
            String actualStr = "", defStr = "";
            Expression tempExp = mo.getDefaultExpression(constants[i]);
            if (tempExp != null) {
                defStr = tempExp.infix();
            }
            if (mo.isScan(constants[i])) {
                actualStr = mo.getConstantArraySpec(constants[i]).toString();
            } else {
                tempExp = mo.getActualExpression(constants[i], 0);
                if (tempExp != null) {
                    actualStr = tempExp.infix();
                }
            }
            if (overParamTable == null) {
                overParamTable = getTable(3, 75, 1, 3, 3);
                overParamTable.setAlignment(Table.ALIGN_LEFT);
                overParamTable.addCell(createCell("Overriden Parameters", getBold(DEF_HEADER_FONT_SIZE), 3, 1, Element.ALIGN_CENTER, true));
                overParamTable.addCell(createHeaderCell("Name", getBold(), 1));
                overParamTable.addCell(createHeaderCell("Actual Value", getBold(), 1));
                overParamTable.addCell(createHeaderCell("Default Value", getBold(), 1));
            }
            overParamTable.addCell(createCell(constants[i], getFont()));
            overParamTable.addCell(createCell(actualStr, getFont()));
            overParamTable.addCell(createCell(defStr, getFont()));
        }
    }
    if (overParamTable != null) {
        simSection.add(overParamTable);
    }
    // add spatial details
    // sim.isSpatial();
    Table meshTable = null;
    MeshSpecification mesh = sim.getMeshSpecification();
    if (mesh != null) {
        Geometry geom = mesh.getGeometry();
        Extent extent = geom.getExtent();
        String extentStr = "(" + extent.getX() + ", " + extent.getY() + ", " + extent.getZ() + ")";
        ISize meshSize = mesh.getSamplingSize();
        String meshSizeStr = "(" + meshSize.getX() + ", " + meshSize.getY() + ", " + meshSize.getZ() + ")";
        meshTable = getTable(2, 75, 1, 3, 3);
        meshTable.setAlignment(Table.ALIGN_LEFT);
        meshTable.addCell(createCell("Geometry Setting", getBold(DEF_HEADER_FONT_SIZE), 2, 1, Element.ALIGN_CENTER, true));
        meshTable.addCell(createCell("Geometry Size (um)", getFont()));
        meshTable.addCell(createCell(extentStr, getFont()));
        meshTable.addCell(createCell("Mesh Size (elements)", getFont()));
        meshTable.addCell(createCell(meshSizeStr, getFont()));
    }
    if (meshTable != null) {
        simSection.add(meshTable);
    }
    // write advanced sim settings
    Table simAdvTable = null;
    SolverTaskDescription solverDesc = sim.getSolverTaskDescription();
    if (solverDesc != null) {
        String solverName = solverDesc.getSolverDescription().getDisplayLabel();
        simAdvTable = getTable(2, 75, 1, 3, 3);
        simAdvTable.setAlignment(Table.ALIGN_LEFT);
        simAdvTable.addCell(createCell("Advanced Settings", getBold(DEF_HEADER_FONT_SIZE), 2, 1, Element.ALIGN_CENTER, true));
        simAdvTable.addCell(createCell("Solver Name", getFont()));
        simAdvTable.addCell(createCell(solverName, getFont()));
        simAdvTable.addCell(createCell("Time Bounds - Starting", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeBounds().getStartingTime(), getFont()));
        simAdvTable.addCell(createCell("Time Bounds - Ending", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeBounds().getEndingTime(), getFont()));
        simAdvTable.addCell(createCell("Time Step - Min", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeStep().getMinimumTimeStep(), getFont()));
        simAdvTable.addCell(createCell("Time Step - Default", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeStep().getDefaultTimeStep(), getFont()));
        simAdvTable.addCell(createCell("Time Step - Max", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeStep().getMaximumTimeStep(), getFont()));
        ErrorTolerance et = solverDesc.getErrorTolerance();
        if (et != null) {
            simAdvTable.addCell(createCell("Error Tolerance - Absolute", getFont()));
            simAdvTable.addCell(createCell("" + et.getAbsoluteErrorTolerance(), getFont()));
            simAdvTable.addCell(createCell("Error Tolerance - Relative", getFont()));
            simAdvTable.addCell(createCell("" + et.getRelativeErrorTolerance(), getFont()));
        }
        OutputTimeSpec ots = solverDesc.getOutputTimeSpec();
        if (ots.isDefault()) {
            simAdvTable.addCell(createCell("Keep Every", getFont()));
            simAdvTable.addCell(createCell("" + ((DefaultOutputTimeSpec) ots).getKeepEvery(), getFont()));
            simAdvTable.addCell(createCell("Keep At Most", getFont()));
            simAdvTable.addCell(createCell("" + ((DefaultOutputTimeSpec) ots).getKeepAtMost(), getFont()));
        } else if (ots.isUniform()) {
            simAdvTable.addCell(createCell("Output Time Step", getFont()));
            simAdvTable.addCell(createCell("" + ((UniformOutputTimeSpec) ots).getOutputTimeStep(), getFont()));
        } else if (ots.isExplicit()) {
            simAdvTable.addCell(createCell("Output Time Points", getFont()));
            simAdvTable.addCell(createCell("" + ((ExplicitOutputTimeSpec) ots).toCommaSeperatedOneLineOfString(), getFont()));
        }
        simAdvTable.addCell(createCell("Use Symbolic Jacobian (T/F)", getFont()));
        simAdvTable.addCell(createCell((solverDesc.getUseSymbolicJacobian() ? " T " : " F "), getFont()));
        Constant sp = solverDesc.getSensitivityParameter();
        if (sp != null) {
            simAdvTable.addCell(createCell("Sensitivity Analysis Param", getFont()));
            simAdvTable.addCell(createCell(sp.getName(), getFont()));
        }
    }
    if (simAdvTable != null) {
        simSection.add(simAdvTable);
    }
}
Also used : Table(com.lowagie.text.Table) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) Extent(org.vcell.util.Extent) ISize(org.vcell.util.ISize) Constant(cbit.vcell.math.Constant) Section(com.lowagie.text.Section) MeshSpecification(cbit.vcell.solver.MeshSpecification) Geometry(cbit.vcell.geometry.Geometry) MathOverrides(cbit.vcell.solver.MathOverrides) DefaultOutputTimeSpec(cbit.vcell.solver.DefaultOutputTimeSpec) ExplicitOutputTimeSpec(cbit.vcell.solver.ExplicitOutputTimeSpec) OutputTimeSpec(cbit.vcell.solver.OutputTimeSpec) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) Expression(cbit.vcell.parser.Expression) ErrorTolerance(cbit.vcell.solver.ErrorTolerance) SolverTaskDescription(cbit.vcell.solver.SolverTaskDescription)

Example 22 with Constant

use of cbit.vcell.math.Constant 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 23 with Constant

use of cbit.vcell.math.Constant 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 24 with Constant

use of cbit.vcell.math.Constant in project vcell by virtualcell.

the class SolverTaskDescription method setSensitivityParameter.

/**
 * Sets the sensitivityParameter property (cbit.vcell.math.Constant) value.
 * @param sensitivityParameter The new value for the property.
 * @see #getSensitivityParameter
 */
public void setSensitivityParameter(Constant sensitivityParameter) throws java.beans.PropertyVetoException {
    if (!Matchable.areEqual(fieldSensitivityParameter, sensitivityParameter)) {
        Constant oldValue = fieldSensitivityParameter;
        fireVetoableChange("sensitivityParameter", oldValue, sensitivityParameter);
        fieldSensitivityParameter = sensitivityParameter;
        firePropertyChange("sensitivityParameter", oldValue, sensitivityParameter);
    }
}
Also used : Constant(cbit.vcell.math.Constant)

Example 25 with Constant

use of cbit.vcell.math.Constant in project vcell by virtualcell.

the class DefaultODESolver method initialize.

/**
 * This method was created by a SmartGuide.
 * @exception SolverException The exception description.
 */
protected void initialize() throws SolverException {
    SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
    Simulation sim = simSymbolTable.getSimulation();
    try {
        // create a fast system if necessary
        fieldFastAlgebraicSystem = null;
        if (getSubDomain().getFastSystem() != null) {
            fieldFastAlgebraicSystem = new FastAlgebraicSystem(new FastSystemAnalyzer(getSubDomain().getFastSystem(), simSymbolTable));
        }
        // refreshIdentifiers();
        fieldIdentifiers = createIdentifiers();
        fieldSensVariables = createSensitivityVariables();
        // refreshStateVariables();
        fieldStateVariables = createStateVariables();
        // 
        // allocate ValueVectors object
        fieldValueVectors = new ValueVectors(getValueVectorCount(), fieldIdentifiers.size());
        // initialize indexes of variables
        fieldVariableIndexes = new int[getStateVariableCount()];
        for (int i = 0; i < getStateVariableCount(); i++) {
            fieldVariableIndexes[i] = getStateVariable(i).getVariable().getIndex();
        }
        // initialize constants
        double[] initialValues = getValueVector(0);
        for (int i = 0; i < fieldIdentifiers.size(); i++) {
            if (fieldIdentifiers.elementAt(i) instanceof Constant) {
                Constant constant = (Constant) fieldIdentifiers.elementAt(i);
                constant.bind(simSymbolTable);
                if (constant.isConstant()) {
                    // constant.getValue();
                    initialValues[constant.getIndex()] = constant.getExpression().evaluateConstant();
                } else {
                    throw new SolverException("cannot evaluate constant '" + constant.getName() + "' = " + constant.getExpression());
                }
            }
        }
        // initialize variables
        for (int i = 0; i < getStateVariableCount(); i++) {
            initialValues[getVariableIndex(i)] = getStateVariable(i).evaluateIC(initialValues);
        }
        fieldODESolverResultSet = createODESolverResultSet();
        // reset - in the ** default ** solvers we don't pick up from where we left off, we can override that behaviour in integrate() if ever necessary
        fieldCurrentTime = sim.getSolverTaskDescription().getTimeBounds().getStartingTime();
    } catch (ExpressionException expressionException) {
        expressionException.printStackTrace(System.out);
        throw new SolverException(expressionException.getMessage());
    } catch (MathException mathException) {
        mathException.printStackTrace(System.out);
        throw new SolverException(mathException.getMessage());
    }
}
Also used : FastSystemAnalyzer(cbit.vcell.mapping.FastSystemAnalyzer) Simulation(cbit.vcell.solver.Simulation) Constant(cbit.vcell.math.Constant) PseudoConstant(cbit.vcell.math.PseudoConstant) MathException(cbit.vcell.math.MathException) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException)

Aggregations

Constant (cbit.vcell.math.Constant)69 Expression (cbit.vcell.parser.Expression)43 Variable (cbit.vcell.math.Variable)31 Function (cbit.vcell.math.Function)24 MathDescription (cbit.vcell.math.MathDescription)22 VolVariable (cbit.vcell.math.VolVariable)21 ExpressionException (cbit.vcell.parser.ExpressionException)21 MathException (cbit.vcell.math.MathException)15 Vector (java.util.Vector)15 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)14 MemVariable (cbit.vcell.math.MemVariable)14 MembraneRegionVariable (cbit.vcell.math.MembraneRegionVariable)13 SubDomain (cbit.vcell.math.SubDomain)13 MacroscopicRateConstant (cbit.vcell.math.MacroscopicRateConstant)12 VolumeRegionVariable (cbit.vcell.math.VolumeRegionVariable)12 ReservedVariable (cbit.vcell.math.ReservedVariable)11 FilamentVariable (cbit.vcell.math.FilamentVariable)10 InsideVariable (cbit.vcell.math.InsideVariable)10 OutsideVariable (cbit.vcell.math.OutsideVariable)10 FilamentRegionVariable (cbit.vcell.math.FilamentRegionVariable)9