Search in sources :

Example 61 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class FiniteVolumeFileWriter method generateRandomNumbers.

private double[] generateRandomNumbers(RandomVariable rv, int numRandomNumbers) throws ExpressionException {
    VariableDomain variableDomain = (rv instanceof VolumeRandomVariable) ? VariableDomain.VARIABLEDOMAIN_VOLUME : VariableDomain.VARIABLEDOMAIN_MEMBRANE;
    Expression seedExpr = subsituteExpression(rv.getSeed(), variableDomain);
    if (!seedExpr.isNumeric()) {
        throw new ExpressionException("Seed for RandomVariable '" + rv.getName() + " is not Constant!");
    }
    int seed = (int) rv.getSeed().evaluateConstant();
    Distribution distribution = rv.getDistribution();
    double[] randomNumbers = new double[numRandomNumbers];
    Random random = new Random(seed);
    if (distribution instanceof UniformDistribution) {
        UniformDistribution ud = (UniformDistribution) distribution;
        Expression minFlattened = subsituteExpression(ud.getMinimum(), variableDomain);
        Expression maxFlattened = subsituteExpression(ud.getMaximum(), variableDomain);
        if (!minFlattened.isNumeric()) {
            throw new ExpressionException("For RandomVariable '" + rv.getName() + "', minimum for UniformDistribution is not Constant!");
        }
        if (!maxFlattened.isNumeric()) {
            throw new ExpressionException("For RandomVariable '" + rv.getName() + "', maximum for UniformDistribution is not Constant!");
        }
        double minVal = minFlattened.evaluateConstant();
        double maxVal = maxFlattened.evaluateConstant();
        for (int i = 0; i < numRandomNumbers; i++) {
            double r = random.nextDouble();
            randomNumbers[i] = (maxVal - minVal) * r + minVal;
        }
    } else if (distribution instanceof GaussianDistribution) {
        GaussianDistribution gd = (GaussianDistribution) distribution;
        Expression meanFlattened = subsituteExpression(gd.getMean(), variableDomain);
        Expression sdFlattened = subsituteExpression(gd.getStandardDeviation(), variableDomain);
        if (!meanFlattened.isNumeric()) {
            throw new ExpressionException("For RandomVariable '" + rv.getName() + "', mean for GaussianDistribution is not Constant!");
        }
        if (!sdFlattened.isNumeric()) {
            throw new ExpressionException("For RandomVariable '" + rv.getName() + "', standard deviation for GaussianDistribution is not Constant!");
        }
        double muVal = meanFlattened.evaluateConstant();
        double sigmaVal = sdFlattened.evaluateConstant();
        for (int i = 0; i < numRandomNumbers; i++) {
            double r = random.nextGaussian();
            randomNumbers[i] = sigmaVal * r + muVal;
        }
    }
    return randomNumbers;
}
Also used : VariableDomain(cbit.vcell.math.VariableType.VariableDomain) Random(java.util.Random) GaussianDistribution(cbit.vcell.math.GaussianDistribution) Expression(cbit.vcell.parser.Expression) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) UniformDistribution(cbit.vcell.math.UniformDistribution) GaussianDistribution(cbit.vcell.math.GaussianDistribution) Distribution(cbit.vcell.math.Distribution) UniformDistribution(cbit.vcell.math.UniformDistribution) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 62 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class FiniteVolumeFileWriter method convertAnalyticGeometryToRvachevFunction.

public static Expression[] convertAnalyticGeometryToRvachevFunction(GeometrySpec geoSpec) throws ExpressionException {
    SubVolume[] subVolumes = geoSpec.getSubVolumes();
    int numSubVolumes = subVolumes.length;
    Expression[] newExps = new Expression[numSubVolumes];
    if (numSubVolumes == 1) {
        newExps[0] = new Expression(-1.0);
    } else {
        for (int i = 0; i < numSubVolumes - 1; i++) {
            if (!(subVolumes[i] instanceof AnalyticSubVolume)) {
                throw new RuntimeException("Subdomain " + i + " is not a analytic subdomain.");
            }
            AnalyticSubVolume subvolume = (AnalyticSubVolume) subVolumes[i];
            try {
                newExps[i] = RvachevFunctionUtils.convertToRvachevFunction(subvolume.getExpression());
                if (newExps[numSubVolumes - 1] == null) {
                    newExps[numSubVolumes - 1] = Expression.negate(newExps[i]);
                } else {
                    newExps[numSubVolumes - 1] = Expression.max(newExps[numSubVolumes - 1], Expression.negate(newExps[i]));
                }
            } catch (ExpressionException ex) {
                throw new ExpressionException("Domain " + subvolume.getName() + ": " + ex.getMessage());
            }
        }
        for (int i = 1; i < numSubVolumes - 1; i++) {
            for (int j = 0; j < i; j++) {
                newExps[i] = Expression.max(newExps[i], Expression.negate(newExps[j]));
            }
        }
    }
    return newExps;
}
Also used : Expression(cbit.vcell.parser.Expression) SubVolume(cbit.vcell.geometry.SubVolume) AnalyticSubVolume(cbit.vcell.geometry.AnalyticSubVolume) AnalyticSubVolume(cbit.vcell.geometry.AnalyticSubVolume) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 63 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class DataSetControllerImpl method getSimDataBlock.

/**
 * This method was created by a SmartGuide.
 * @return double[]
 * @param varName java.lang.String
 * @param time double
 */
public SimDataBlock getSimDataBlock(OutputContext outputContext, VCDataIdentifier vcdID, String varName, double time) throws DataAccessException {
    VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ")  <<ENTER>>");
    try {
        // 
        // check if already cached for non-function variables
        // 
        VCData simData = getVCData(vcdID);
        VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ") got VCData");
        long dataBlockTimeStamp = simData.getDataBlockTimeStamp(PDE_DATA, time);
        VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ") got dataBlockTimeStamp");
        PDEDataInfo pdeDataInfo = new PDEDataInfo(vcdID.getOwner(), vcdID.getID(), varName, time, dataBlockTimeStamp);
        SimDataBlock simDataBlock = null;
        AnnotatedFunction function = getFunction(outputContext, vcdID, varName);
        VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ") got function");
        if (function == null) {
            simDataBlock = (cacheTable0 != null ? cacheTable0.get(pdeDataInfo) : null);
            if (simDataBlock == null) {
                simDataBlock = simData.getSimDataBlock(outputContext, varName, time);
                if (simDataBlock != null && dataCachingEnabled) {
                    // cacheTable.put(pdeDataInfo,simDataBlock);
                    if (cacheTable0 != null) {
                        try {
                            cacheTable0.put(pdeDataInfo, simDataBlock);
                        } catch (CacheException e) {
                            // if can't cache the data, it is ok
                            e.printStackTrace();
                        }
                    }
                }
            }
        } else {
            if (simData instanceof SimulationData) {
                function = ((SimulationData) simData).simplifyFunction(function);
            }
            VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ") evaluating function");
            simDataBlock = evaluateFunction(outputContext, vcdID, simData, function, time);
        }
        CartesianMesh mesh = getMesh(vcdID);
        if (mesh != null && mesh.isChomboMesh()) {
            for (int i = 0; i < simDataBlock.getData().length; i++) {
                if (simDataBlock.getData()[i] == 1.23456789e300) {
                    simDataBlock.getData()[i] = Double.NaN;
                }
            }
        }
        if (simDataBlock != null) {
            VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ")  <<EXIT-simDataBlock not null>>");
            return simDataBlock;
        } else {
            String msg = "failure reading " + varName + " at t=" + time + " for " + vcdID.getOwner().getName() + "'s " + vcdID.getID();
            if (lg.isWarnEnabled())
                lg.warn("DataSetControllerImpl.getDataBlockValues(): " + msg);
            VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ")  <<EXIT-Exception>>");
            throw new DataAccessException(msg);
        }
    } catch (MathException e) {
        lg.error(e.getMessage(), e);
        VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ")  <<EXIT-Exception>>");
        throw new DataAccessException(e.getMessage());
    } catch (IOException e) {
        lg.error(e.getMessage(), e);
        VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ")  <<EXIT-Exception>>");
        throw new DataAccessException(e.getMessage());
    } catch (ExpressionException e) {
        lg.error(e.getMessage(), e);
        VCMongoMessage.sendTrace("DataSetControllerImpl.getSimDataBlock(" + varName + ", " + time + ")  <<EXIT-Exception>>");
        throw new DataAccessException(e.getMessage());
    }
}
Also used : CacheException(org.vcell.util.CacheException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) CartesianMesh(cbit.vcell.solvers.CartesianMesh) MathException(cbit.vcell.math.MathException) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 64 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class ITextWriter method writeSubDomainsEquationsAsImages.

// currently not used.
protected void writeSubDomainsEquationsAsImages(Section mathDescSection, MathDescription mathDesc) {
    Enumeration<SubDomain> subDomains = mathDesc.getSubDomains();
    Expression[] expArray;
    Section volDomains = mathDescSection.addSection("Volume Domains", mathDescSection.depth() + 1);
    Section memDomains = mathDescSection.addSection("Membrane Domains", mathDescSection.depth() + 1);
    // arbitrary
    int scale = 1, height = 200;
    int viewableWidth = (int) (document.getPageSize().width() - document.leftMargin() - document.rightMargin());
    BufferedImage dummy = new BufferedImage(viewableWidth, height, BufferedImage.TYPE_3BYTE_BGR);
    while (subDomains.hasMoreElements()) {
        SubDomain subDomain = subDomains.nextElement();
        Enumeration<Equation> equationsList = subDomain.getEquations();
        ArrayList<Expression> expList = new ArrayList<Expression>();
        while (equationsList.hasMoreElements()) {
            Equation equ = equationsList.nextElement();
            try {
                Enumeration<Expression> enum_equ = equ.getTotalExpressions();
                while (enum_equ.hasMoreElements()) {
                    Expression exp = new Expression(enum_equ.nextElement());
                    expList.add(exp.flatten());
                }
            } catch (ExpressionException ee) {
                System.err.println("Unable to process the equation for subdomain: " + subDomain.getName());
                ee.printStackTrace();
                continue;
            }
        }
        expArray = (Expression[]) expList.toArray(new Expression[expList.size()]);
        Section tempSection = null;
        if (subDomain instanceof CompartmentSubDomain) {
            tempSection = volDomains.addSection(subDomain.getName(), volDomains.depth() + 1);
        } else if (subDomain instanceof MembraneSubDomain) {
            tempSection = memDomains.addSection(subDomain.getName(), memDomains.depth() + 1);
        }
        try {
            Dimension dim = ExpressionCanvas.getExpressionImageSize(expArray, (Graphics2D) dummy.getGraphics());
            System.out.println("Image dim: " + dim.width + " " + dim.height);
            BufferedImage bufferedImage = new BufferedImage((int) dim.getWidth() * scale, (int) dim.getHeight() * scale, BufferedImage.TYPE_3BYTE_BGR);
            ExpressionCanvas.getExpressionAsImage(expArray, bufferedImage, scale);
            // Table imageTable = null;;
            com.lowagie.text.Image expImage = com.lowagie.text.Image.getInstance(bufferedImage, null);
            expImage.setAlignment(com.lowagie.text.Image.LEFT);
            if (viewableWidth < expImage.scaledWidth()) {
                expImage.scaleToFit(viewableWidth, expImage.height());
                System.out.println("SubDomain expresions After scaling: " + expImage.scaledWidth());
            }
            /*Cell imageCell = new Cell();
				imageCell.add(expImage);
				if (imageTable == null) {
					imageTable = getTable(1, 100, 1, 1, 0);
				}
				imageTable.setTableFitsPage(false);
 				imageTable.setCellsFitPage(false);
				imageTable.addCell(imageCell);
				imageTable.setWidth(100);
				tempSection.add(imageTable);*/
            tempSection.add(expImage);
        } catch (Exception e) {
            System.err.println("Unable to add subdomain equation image to report.");
            e.printStackTrace();
        }
    }
    if (volDomains.isEmpty()) {
        mathDescSection.remove(volDomains);
    }
    if (memDomains.isEmpty()) {
        mathDescSection.remove(memDomains);
    }
}
Also used : MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) ArrayList(java.util.ArrayList) PdeEquation(cbit.vcell.math.PdeEquation) VolumeRegionEquation(cbit.vcell.math.VolumeRegionEquation) OdeEquation(cbit.vcell.math.OdeEquation) FilamentRegionEquation(cbit.vcell.math.FilamentRegionEquation) MembraneRegionEquation(cbit.vcell.math.MembraneRegionEquation) Equation(cbit.vcell.math.Equation) 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) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Expression(cbit.vcell.parser.Expression) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SimulationContext(cbit.vcell.mapping.SimulationContext) GeometryContext(cbit.vcell.mapping.GeometryContext) ReactionContext(cbit.vcell.mapping.ReactionContext)

Example 65 with ExpressionException

use of cbit.vcell.parser.ExpressionException 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)

Aggregations

ExpressionException (cbit.vcell.parser.ExpressionException)199 Expression (cbit.vcell.parser.Expression)138 MathException (cbit.vcell.math.MathException)58 PropertyVetoException (java.beans.PropertyVetoException)51 DataAccessException (org.vcell.util.DataAccessException)34 ArrayList (java.util.ArrayList)32 Variable (cbit.vcell.math.Variable)30 IOException (java.io.IOException)29 Element (org.jdom.Element)26 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)25 MappingException (cbit.vcell.mapping.MappingException)24 Function (cbit.vcell.math.Function)24 Vector (java.util.Vector)24 ModelException (cbit.vcell.model.ModelException)23 SolverException (cbit.vcell.solver.SolverException)23 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)22 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)21 Constant (cbit.vcell.math.Constant)20 MathDescription (cbit.vcell.math.MathDescription)19 Structure (cbit.vcell.model.Structure)18