use of cbit.vcell.math.VolumeRandomVariable 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.VolumeRandomVariable 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;
}
use of cbit.vcell.math.VolumeRandomVariable in project vcell by virtualcell.
the class FiniteVolumeFileWriter method writeVariables.
/**
*# Variables : type name unit time_dependent_flag advection_flag solve_whole_mesh_flag solve_regions
*VARIABLE_BEGIN
*VOLUME_ODE rB uM
*VOLUME_PDE rf uM false false
*VOLUME_PDE r uM false false
*VOLUME_ODE rfB uM
*VARIABLE_END
* @throws MathException
* @throws ExpressionException
* @throws IOException
*/
private void writeVariables() throws MathException, ExpressionException, IOException {
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
printWriter.println("# Variables : type name domain time_dependent_flag advection_flag grad_flag solve_whole_mesh_flag solve_regions");
printWriter.println(FVInputFileKeyword.VARIABLE_BEGIN);
MathDescription mathDesc = simSymbolTable.getSimulation().getMathDescription();
Variable[] vars = simSymbolTable.getVariables();
ArrayList<RandomVariable> rvList = new ArrayList<RandomVariable>();
for (int i = 0; i < vars.length; i++) {
String varName = vars[i].getName();
String domainName = vars[i].getDomain() == null ? null : vars[i].getDomain().getName();
if (vars[i] instanceof VolumeRandomVariable || vars[i] instanceof MembraneRandomVariable) {
rvList.add((RandomVariable) vars[i]);
} else if (vars[i] instanceof VolVariable) {
if (bChomboSolver && domainName == null) {
throw new MathException(simTask.getSimulation().getSolverTaskDescription().getSolverDescription().getDisplayLabel() + " requires that every variable is defined in a single domain");
}
VolVariable volVar = (VolVariable) vars[i];
if (mathDesc.isPDE(volVar)) {
boolean hasTimeVaryingDiffusionOrAdvection = simSymbolTable.hasTimeVaryingDiffusionOrAdvection(volVar);
final boolean hasVelocity = mathDesc.hasVelocity(volVar);
final boolean hasGradient = mathDesc.hasGradient(volVar);
if (mathDesc.isPdeSteady(volVar)) {
printWriter.print("VOLUME_PDE_STEADY ");
} else {
printWriter.print("VOLUME_PDE ");
}
printWriter.print(varName + " " + domainName + " " + hasTimeVaryingDiffusionOrAdvection + " " + hasVelocity + " " + hasGradient);
} else {
printWriter.print("VOLUME_ODE " + varName + " " + domainName);
}
if (domainName == null) {
Vector<SubDomain> listOfSubDomains = new Vector<SubDomain>();
int totalNumCompartments = 0;
Enumeration<SubDomain> subDomainEnum = mathDesc.getSubDomains();
while (subDomainEnum.hasMoreElements()) {
SubDomain subDomain = subDomainEnum.nextElement();
if (subDomain instanceof CompartmentSubDomain) {
CompartmentSubDomain compartmentSubDomain = (CompartmentSubDomain) subDomain;
totalNumCompartments++;
Equation varEquation = subDomain.getEquation(vars[i]);
if (varEquation != null) {
if (!(varEquation instanceof PdeEquation) || !((PdeEquation) varEquation).isDummy(simSymbolTable, compartmentSubDomain)) {
listOfSubDomains.add(compartmentSubDomain);
}
}
}
}
if ((totalNumCompartments == listOfSubDomains.size()) || (listOfSubDomains.size() == 0 && simTask.getSimulation().getSolverTaskDescription().getSolverDescription().equals(SolverDescription.SundialsPDE))) {
printWriter.print(" true");
} else {
printWriter.print(" false");
for (int j = 0; j < listOfSubDomains.size(); j++) {
CompartmentSubDomain compartmentSubDomain = (CompartmentSubDomain) listOfSubDomains.elementAt(j);
printWriter.print(" " + compartmentSubDomain.getName());
}
}
printWriter.println();
} else {
printWriter.println(" false " + domainName);
}
} else if (vars[i] instanceof VolumeParticleVariable) {
printWriter.println(FVInputFileKeyword.VOLUME_PARTICLE + " " + varName + " " + domainName);
} else if (vars[i] instanceof MembraneParticleVariable) {
printWriter.println(FVInputFileKeyword.MEMBRANE_PARTICLE + " " + varName + " " + domainName);
} else if (vars[i] instanceof VolumeRegionVariable) {
printWriter.println("VOLUME_REGION " + varName + " " + domainName);
} else if (vars[i] instanceof MemVariable) {
if (bChomboSolver && domainName == null) {
throw new MathException(simTask.getSimulation().getSolverTaskDescription().getSolverDescription().getDisplayLabel() + " requires that every variable is defined in a single domain");
}
MemVariable memVar = (MemVariable) vars[i];
if (mathDesc.isPDE(memVar)) {
printWriter.println("MEMBRANE_PDE " + varName + " " + domainName + " " + simSymbolTable.hasTimeVaryingDiffusionOrAdvection(memVar));
} else {
printWriter.println("MEMBRANE_ODE " + varName + " " + domainName);
}
} else if (vars[i] instanceof MembraneRegionVariable) {
printWriter.println("MEMBRANE_REGION " + varName + " " + domainName);
} else if (vars[i] instanceof FilamentVariable) {
throw new RuntimeException("Filament application not supported yet");
}
}
int numRandomVariables = rvList.size();
if (numRandomVariables > 0) {
ISize samplingSize = simTask.getSimulation().getMeshSpecification().getSamplingSize();
String[] varNameArr = new String[numRandomVariables];
VariableType[] varTypeArr = new VariableType[numRandomVariables];
double[][] dataArr = new double[numRandomVariables][];
for (int i = 0; i < numRandomVariables; i++) {
RandomVariable rv = rvList.get(i);
varNameArr[i] = rv.getName();
int numRandomNumbers = 0;
if (rv instanceof VolumeRandomVariable) {
printWriter.print("VOLUME_RANDOM");
varTypeArr[i] = VariableType.VOLUME;
numRandomNumbers = samplingSize.getXYZ();
} else if (rv instanceof MembraneRandomVariable) {
printWriter.print("MEMBRANE_RANDOM");
varTypeArr[i] = VariableType.MEMBRANE;
numRandomNumbers = resampledGeometry.getGeometrySurfaceDescription().getSurfaceCollection().getTotalPolygonCount();
} else {
throw new RuntimeException("Unknown RandomVariable type");
}
printWriter.println(" " + varNameArr[i]);
dataArr[i] = generateRandomNumbers(rv, numRandomNumbers);
}
File rvFile = new File(workingDirectory, simTask.getSimulationJobID() + RANDOM_VARIABLE_FILE_EXTENSION);
DataSet.writeNew(rvFile, varNameArr, varTypeArr, samplingSize, dataArr);
}
printWriter.println(FVInputFileKeyword.VARIABLE_END);
printWriter.println();
}
use of cbit.vcell.math.VolumeRandomVariable 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