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;
}
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;
}
Aggregations