Search in sources :

Example 1 with UniformDistribution

use of cbit.vcell.math.UniformDistribution 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 2 with UniformDistribution

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

the class Xmlproducer method getXML.

private Element getXML(RandomVariable var) {
    Element randomVariableElement = null;
    if (var instanceof VolumeRandomVariable) {
        randomVariableElement = new Element(XMLTags.VolumeRandomVariableTag);
    } else {
        randomVariableElement = new Element(XMLTags.MembraneRandomVariableTag);
    }
    randomVariableElement.setAttribute(XMLTags.NameAttrTag, mangle(var.getName()));
    Element seedElement = new Element(XMLTags.RandomVariableSeedTag);
    seedElement.addContent(mangleExpression(var.getSeed()));
    randomVariableElement.addContent(seedElement);
    if (var.getDomain() != null) {
        randomVariableElement.setAttribute(XMLTags.DomainAttrTag, mangle(var.getDomain().getName()));
    }
    Element distElement = null;
    if (var.getDistribution() instanceof UniformDistribution) {
        distElement = getXML((UniformDistribution) var.getDistribution());
    } else if (var.getDistribution() instanceof GaussianDistribution) {
        distElement = getXML((GaussianDistribution) var.getDistribution());
    }
    randomVariableElement.addContent(distElement);
    return randomVariableElement;
}
Also used : GaussianDistribution(cbit.vcell.math.GaussianDistribution) Element(org.jdom.Element) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) UniformDistribution(cbit.vcell.math.UniformDistribution)

Example 3 with UniformDistribution

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

the class XmlReader method getUniformDistribution.

private UniformDistribution getUniformDistribution(Element distElement) {
    Element element = distElement.getChild(XMLTags.UniformDistributionMinimumTag, vcNamespace);
    Expression low = unMangleExpression(element.getText());
    element = distElement.getChild(XMLTags.UniformDistributionMaximumTag, vcNamespace);
    Expression high = unMangleExpression(element.getText());
    return new UniformDistribution(low, high);
}
Also used : Expression(cbit.vcell.parser.Expression) Element(org.jdom.Element) UniformDistribution(cbit.vcell.math.UniformDistribution)

Aggregations

UniformDistribution (cbit.vcell.math.UniformDistribution)3 GaussianDistribution (cbit.vcell.math.GaussianDistribution)2 VolumeRandomVariable (cbit.vcell.math.VolumeRandomVariable)2 Expression (cbit.vcell.parser.Expression)2 Element (org.jdom.Element)2 Distribution (cbit.vcell.math.Distribution)1 VariableDomain (cbit.vcell.math.VariableType.VariableDomain)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 Random (java.util.Random)1