Search in sources :

Example 1 with Distribution

use of cbit.vcell.math.Distribution 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 Distribution

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

the class XmlReader method getRandomVariable.

private RandomVariable getRandomVariable(Element param) throws XmlParseException {
    // get attributes
    String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
    Element element = param.getChild(XMLTags.RandomVariableSeedTag, vcNamespace);
    Expression seed = null;
    if (element != null) {
        seed = unMangleExpression(element.getText());
    }
    Distribution dist = null;
    element = param.getChild(XMLTags.UniformDistributionTag, vcNamespace);
    if (element != null) {
        dist = getUniformDistribution(element);
    }
    element = param.getChild(XMLTags.GaussianDistributionTag, vcNamespace);
    if (element != null) {
        dist = getGaussianDistribution(element);
    }
    String domainStr = unMangle(param.getAttributeValue(XMLTags.DomainAttrTag));
    Domain domain = null;
    if (domainStr != null) {
        domain = new Domain(domainStr);
    }
    RandomVariable var = null;
    if (param.getName().equals(XMLTags.VolumeRandomVariableTag)) {
        var = new VolumeRandomVariable(name, seed, dist, domain);
    } else if (param.getName().equals(XMLTags.MembraneRandomVariableTag)) {
        var = new MembraneRandomVariable(name, seed, dist, domain);
    } else {
        throw new XmlParseException(param.getName() + " is not supported!");
    }
    transcribeComments(param, var);
    return var;
}
Also used : Expression(cbit.vcell.parser.Expression) Element(org.jdom.Element) UniformDistribution(cbit.vcell.math.UniformDistribution) Distribution(cbit.vcell.math.Distribution) GaussianDistribution(cbit.vcell.math.GaussianDistribution) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) Domain(cbit.vcell.math.Variable.Domain)

Aggregations

Distribution (cbit.vcell.math.Distribution)2 GaussianDistribution (cbit.vcell.math.GaussianDistribution)2 UniformDistribution (cbit.vcell.math.UniformDistribution)2 VolumeRandomVariable (cbit.vcell.math.VolumeRandomVariable)2 Expression (cbit.vcell.parser.Expression)2 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)1 FilamentSubDomain (cbit.vcell.math.FilamentSubDomain)1 MembraneRandomVariable (cbit.vcell.math.MembraneRandomVariable)1 MembraneSubDomain (cbit.vcell.math.MembraneSubDomain)1 PointSubDomain (cbit.vcell.math.PointSubDomain)1 RandomVariable (cbit.vcell.math.RandomVariable)1 Domain (cbit.vcell.math.Variable.Domain)1 VariableDomain (cbit.vcell.math.VariableType.VariableDomain)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 Random (java.util.Random)1 Element (org.jdom.Element)1