use of cbit.vcell.math.VariableType.VariableDomain 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.VariableType.VariableDomain in project vcell by virtualcell.
the class DataSetControllerImpl method getVtuVarInfos.
public VtuVarInfo[] getVtuVarInfos(ComsolSimFiles comsolFiles, OutputContext outputContext, VCDataIdentifier vcdataID) throws DataAccessException {
try {
DataIdentifier[] dataIdentifiers = getDataIdentifiers(outputContext, vcdataID);
if (dataIdentifiers == null) {
return null;
}
ArrayList<VtuVarInfo> vtuVarInfos = new ArrayList<VtuVarInfo>();
for (DataIdentifier di : dataIdentifiers) {
String name = di.getName();
String displayName = di.getDisplayName();
if (di.getDomain() != null) {
System.err.println("DataSetControllerImpl.getVtuVarInfos(comsol): need to support proper domain names now");
}
String domainName = "domain";
VariableDomain variableDomain = null;
VariableType variableType = di.getVariableType();
if (variableType.equals(VariableType.VOLUME) || variableType.equals(VariableType.VOLUME_REGION)) {
variableDomain = VariableDomain.VARIABLEDOMAIN_VOLUME;
} else if (variableType.equals(VariableType.MEMBRANE) || variableType.equals(VariableType.MEMBRANE_REGION)) {
variableDomain = VariableDomain.VARIABLEDOMAIN_MEMBRANE;
} else if (variableType.equals(VariableType.POINT_VARIABLE)) {
variableDomain = VariableDomain.VARIABLEDOMAIN_POINT;
} else if (variableType.equals(VariableType.CONTOUR) || variableType.equals(VariableType.CONTOUR_REGION)) {
variableDomain = VariableDomain.VARIABLEDOMAIN_CONTOUR;
} else if (variableType.equals(VariableType.NONSPATIAL)) {
variableDomain = VariableDomain.VARIABLEDOMAIN_UNKNOWN;
} else if (variableType.equals(VariableType.POSTPROCESSING)) {
variableDomain = VariableDomain.VARIABLEDOMAIN_POSTPROCESSING;
} else {
System.err.print("skipping var " + di + ", unsupported data type");
}
String functionExpression = null;
boolean bMeshVariable = false;
if (name.toUpperCase().contains("SIZE")) {
System.err.println("Skipping Moving Boundary variable '" + name + "' because it is a size ... change later");
continue;
}
vtuVarInfos.add(new VtuVarInfo(name, displayName, domainName, variableDomain, functionExpression, DataType.PointData, bMeshVariable));
}
return vtuVarInfos.toArray(new VtuVarInfo[0]);
} catch (Exception e) {
lg.error(e.getMessage(), e);
throw new DataAccessException("failed to retrieve VTK variable list: " + e.getMessage(), e);
}
}
use of cbit.vcell.math.VariableType.VariableDomain in project vcell by virtualcell.
the class OutputFunctionContext method computeFunctionTypeWRTExpression.
// check if the new expression is valid for outputFunction of functionType
public VariableType computeFunctionTypeWRTExpression(AnnotatedFunction outputFunction, Expression exp) throws ExpressionException, InconsistentDomainException {
MathDescription mathDescription = getSimulationOwner().getMathDescription();
boolean bSpatial = getSimulationOwner().getGeometry().getDimension() > 0;
if (!bSpatial) {
return VariableType.NONSPATIAL;
}
Expression newexp = new Expression(exp);
// making sure that output function is not direct function of constant.
newexp.bindExpression(this);
// here use math description as symbol table because we allow
// new expression itself to be function of constant.
newexp = MathUtilities.substituteFunctions(newexp, this).flatten();
String[] symbols = newexp.getSymbols();
VariableType functionType = outputFunction.getFunctionType();
String funcName = outputFunction.getName();
Domain funcDomain = outputFunction.getDomain();
VariableType[] varTypes = null;
if (symbols != null && symbols.length > 0) {
// making sure that new expression is defined in the same domain
varTypes = new VariableType[symbols.length];
for (int i = 0; i < symbols.length; i++) {
if (ReservedMathSymbolEntries.getReservedVariableEntry(symbols[i]) != null) {
varTypes[i] = functionType;
} else {
Variable var = mathDescription.getVariable(symbols[i]);
if (var == null) {
var = mathDescription.getPostProcessingBlock().getDataGenerator(symbols[i]);
}
varTypes[i] = VariableType.getVariableType(var);
if (funcDomain != null) {
if (var.getDomain() == null) {
// OK
continue;
}
GeometryClass funcGeoClass = simulationOwner.getGeometry().getGeometryClass(funcDomain.getName());
GeometryClass varGeoClass = simulationOwner.getGeometry().getGeometryClass(var.getDomain().getName());
if (varGeoClass instanceof SubVolume && funcGeoClass instanceof SurfaceClass) {
// seems ok if membrane refereces volume
if (!((SurfaceClass) funcGeoClass).isAdjacentTo((SubVolume) varGeoClass)) {
// but has to be adjacent
String errMsg = "'" + funcName + "' defined on Membrane '" + funcDomain.getName() + "' directly or indirectly references " + " variable '" + symbols[i] + "' defined on Volume '" + var.getDomain().getName() + " which is not adjacent to Membrane '" + funcDomain.getName() + "'.";
throw new ExpressionException(errMsg);
}
} else if (!var.getDomain().compareEqual(funcDomain)) {
String errMsg = "'" + funcName + "' defined on '" + funcDomain.getName() + "' directly or indirectly references " + " variable '" + symbols[i] + "' defined on '" + var.getDomain().getName() + ".";
throw new ExpressionException(errMsg);
}
}
}
}
}
// if there are no variables (like built in function, vcRegionArea), check with flattened expression to find out the variable type of the new expression
VariableDomain functionVariableDomain = functionType.getVariableDomain();
Function flattenedFunction = new Function(funcName, newexp, funcDomain);
flattenedFunction.bind(this);
VariableType newVarType = SimulationSymbolTable.getFunctionVariableType(flattenedFunction, getSimulationOwner().getMathDescription(), symbols, varTypes, bSpatial);
if (!newVarType.getVariableDomain().equals(functionVariableDomain)) {
String errMsg = "The expression for '" + funcName + "' includes at least one " + newVarType.getVariableDomain().getName() + " variable. Please make sure that only " + functionVariableDomain.getName() + " variables are " + "referenced in " + functionVariableDomain.getName() + " output functions.";
throw new ExpressionException(errMsg);
}
return newVarType;
}
use of cbit.vcell.math.VariableType.VariableDomain in project vcell by virtualcell.
the class VCellProxyHandler method getDataSetFileOfVariableAtTimeIndex.
@Override
public String getDataSetFileOfVariableAtTimeIndex(SimulationDataSetRef simulationDataSetRef, VariableInfo var, int timeIndex) throws ThriftDataAccessException {
try {
if (var.isMeshVar) {
return getEmptyMeshFile(simulationDataSetRef, var.domainName, timeIndex).getAbsolutePath();
}
File meshFileForVariableAndTime = getPopulatedMeshFileLocation(simulationDataSetRef, var, timeIndex);
if (meshFileForVariableAndTime.exists()) {
return meshFileForVariableAndTime.getAbsolutePath();
}
//
// get data from server for this variable, domain, time
//
VtkManager vtkManager = vcellClientDataService.getVtkManager(simulationDataSetRef);
VariableDomain variableDomainType = VariableDomain.VARIABLEDOMAIN_UNKNOWN;
if (var.variableDomainType == DomainType.MEMBRANE) {
variableDomainType = VariableDomain.VARIABLEDOMAIN_MEMBRANE;
} else if (var.variableDomainType == DomainType.VOLUME) {
variableDomainType = VariableDomain.VARIABLEDOMAIN_VOLUME;
}
org.vcell.vis.io.VtuVarInfo.DataType dataType = org.vcell.vis.io.VtuVarInfo.DataType.CellData;
if (var.isSetDataType() && var.dataType == DataType.CELLDATA) {
dataType = org.vcell.vis.io.VtuVarInfo.DataType.CellData;
} else if (var.isSetDataType() && var.dataType == DataType.POINTDATA) {
dataType = org.vcell.vis.io.VtuVarInfo.DataType.PointData;
}
VtuVarInfo vtuVarInfo = new VtuVarInfo(var.getVariableVtuName(), var.getVariableDisplayName(), var.getDomainName(), variableDomainType, var.getExpressionString(), dataType, var.isMeshVar);
List<Double> times = getTimePoints(simulationDataSetRef);
double time = (double) times.get(timeIndex);
double[] data = vtkManager.getVtuMeshData(vtuVarInfo, time);
//
// get empty mesh file for this domain (getEmptyMeshFile() will ensure that the file exists or create it).
//
File emptyMeshFile = getEmptyMeshFile(simulationDataSetRef, var.getDomainName(), timeIndex);
if (var.getDataType() == DataType.CELLDATA) {
VisMeshUtils.writeCellDataToVtu(emptyMeshFile, var.getVariableVtuName(), data, meshFileForVariableAndTime);
} else if (var.getDataType() == DataType.POINTDATA) {
VisMeshUtils.writePointDataToVtu(emptyMeshFile, var.getVariableVtuName(), data, meshFileForVariableAndTime);
}
return meshFileForVariableAndTime.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
throw new ThriftDataAccessException("failed to retrieve data file for variable " + var.getVariableVtuName() + " at time index " + timeIndex);
}
}
use of cbit.vcell.math.VariableType.VariableDomain in project vcell by virtualcell.
the class FiniteVolumeFileWriter method writeFastSystem.
/*private void writeDataProcessor() throws DataAccessException, IOException, MathException, DivideByZeroException, ExpressionException {
Simulation simulation = simTask.getSimulation();
DataProcessingInstructions dpi = simulation.getDataProcessingInstructions();
if (dpi == null) {
printWriter.println("DATA_PROCESSOR_BEGIN " + DataProcessingInstructions.ROI_TIME_SERIES);
printWriter.println("DATA_PROCESSOR_END");
printWriter.println();
return;
}
FieldDataIdentifierSpec fdis = dpi.getSampleImageFieldData(simulation.getVersion().getOwner());
if (fdis == null) {
throw new DataAccessException("Can't find sample image in data processing instructions");
}
String secondarySimDataDir = PropertyLoader.getProperty(PropertyLoader.secondarySimDataDirProperty, null);
DataSetControllerImpl dsci = new DataSetControllerImpl(new NullSessionLog(),null,userDirectory.getParentFile(),secondarySimDataDir == null ? null : new File(secondarySimDataDir));
CartesianMesh origMesh = dsci.getMesh(fdis.getExternalDataIdentifier());
SimDataBlock simDataBlock = dsci.getSimDataBlock(null,fdis.getExternalDataIdentifier(), fdis.getFieldFuncArgs().getVariableName(), fdis.getFieldFuncArgs().getTime().evaluateConstant());
VariableType varType = fdis.getFieldFuncArgs().getVariableType();
VariableType dataVarType = simDataBlock.getVariableType();
if (!varType.equals(VariableType.UNKNOWN) && !varType.equals(dataVarType)) {
throw new IllegalArgumentException("field function variable type (" + varType.getTypeName() + ") doesn't match real variable type (" + dataVarType.getTypeName() + ")");
}
double[] origData = simDataBlock.getData();
String filename = SimulationJob.createSimulationJobID(Simulation.createSimulationID(simulation.getKey()), simulationJob.getJobIndex()) + FieldDataIdentifierSpec.getDefaultFieldDataFileNameForSimulation(fdis.getFieldFuncArgs());
File fdatFile = new File(userDirectory, filename);
DataSet.writeNew(fdatFile,
new String[] {fdis.getFieldFuncArgs().getVariableName()},
new VariableType[]{simDataBlock.getVariableType()},
new ISize(origMesh.getSizeX(),origMesh.getSizeY(),origMesh.getSizeZ()),
new double[][]{origData});
printWriter.println("DATA_PROCESSOR_BEGIN " + dpi.getScriptName());
printWriter.println(dpi.getScriptInput());
printWriter.println("SampleImageFile " + fdis.getFieldFuncArgs().getVariableName() + " " + fdis.getFieldFuncArgs().getTime().infix() + " " + fdatFile);
printWriter.println("DATA_PROCESSOR_END");
printWriter.println();
}
*/
/**
*# fast system dimension num_dependents
*FAST_SYSTEM_BEGIN 2 2
*INDEPENDENT_VARIALBES rf r
*DEPENDENT_VARIALBES rB rfB
*
*PSEUDO_CONSTANT_BEGIN
*__C0 (rfB + rf);
*__C1 (r + rB);
*PSEUDO_CONSTANT_END
*
*FAST_RATE_BEGIN
* - ((0.02 * ( - ( - r + __C1) - ( - rf + __C0) + (20.0 * ((x > -3.0) && (x < 3.0) && (y > -5.0) && (y < 5.0))) + _VCell_FieldData_0) * rf) - (0.1 * ( - rf + __C0)));
*((0.02 * r * ( - ( - r + __C1) - ( - rf + __C0) + (20.0 * ((x > -3.0) && (x < 3.0) && (y > -5.0) && (y < 5.0))) + _VCell_FieldData_0)) - (0.1 * ( - r + __C1)));
*FAST_RATE_END
*
*FAST_DEPENDENCY_BEGIN
*rB ( - r + __C1);
*rfB ( - rf + __C0);
*FAST_DEPENDENCY_END
*
*JACOBIAN_BEGIN
* - (0.1 + (0.02 * (1.0 + (0.0 * ((x > -3.0) && (x < 3.0) && (y > -5.0) && (y < 5.0)))) * rf) + (0.02 * ( - ( - r + __C1) - ( - rf + __C0) + (20.0 * ((x > -3.0) && (x < 3.0) && (y > -5.0) && (y < 5.0))) + _VCell_FieldData_0)));
* - (0.02 * (1.0 + (0.0 * ((x > -3.0) && (x < 3.0) && (y > -5.0) && (y < 5.0)))) * rf);
*(0.02 * r * (1.0 + (0.0 * ((x > -3.0) && (x < 3.0) && (y > -5.0) && (y < 5.0)))));
*(0.1 + (0.02 * ( - ( - r + __C1) - ( - rf + __C0) + (20.0 * ((x > -3.0) && (x < 3.0) && (y > -5.0) && (y < 5.0))) + _VCell_FieldData_0)) + (0.02 * r * (1.0 + (0.0 * ((x > -3.0) && (x < 3.0) && (y > -5.0) && (y < 5.0))))));
*JACOBIAN_END
*
*FAST_SYSTEM_END
* @throws ExpressionException
* @throws MathException
*/
private void writeFastSystem(SubDomain subDomain) throws MathException, ExpressionException {
VariableDomain variableDomain = (subDomain instanceof CompartmentSubDomain) ? VariableDomain.VARIABLEDOMAIN_VOLUME : VariableDomain.VARIABLEDOMAIN_MEMBRANE;
FastSystem fastSystem = subDomain.getFastSystem();
if (fastSystem == null) {
return;
}
FastSystemAnalyzer fs_analyzer = new FastSystemAnalyzer(fastSystem, simTask.getSimulationJob().getSimulationSymbolTable());
int numIndep = fs_analyzer.getNumIndependentVariables();
int numDep = fs_analyzer.getNumDependentVariables();
int numPseudo = fs_analyzer.getNumPseudoConstants();
printWriter.println("# fast system dimension num_dependents");
printWriter.println("FAST_SYSTEM_BEGIN " + numIndep + " " + numDep);
if (numIndep != 0) {
printWriter.print("INDEPENDENT_VARIALBES ");
Enumeration<Variable> enum1 = fs_analyzer.getIndependentVariables();
while (enum1.hasMoreElements()) {
Variable var = enum1.nextElement();
printWriter.print(var.getName() + " ");
}
printWriter.println();
}
if (numDep != 0) {
printWriter.print("DEPENDENT_VARIALBES ");
Enumeration<Variable> enum1 = fs_analyzer.getDependentVariables();
while (enum1.hasMoreElements()) {
Variable var = enum1.nextElement();
printWriter.print(var.getName() + " ");
}
printWriter.println();
}
printWriter.println();
if (numPseudo != 0) {
printWriter.println("PSEUDO_CONSTANT_BEGIN");
Enumeration<PseudoConstant> enum1 = fs_analyzer.getPseudoConstants();
while (enum1.hasMoreElements()) {
PseudoConstant pc = enum1.nextElement();
printWriter.println(pc.getName() + " " + subsituteExpression(pc.getPseudoExpression(), fs_analyzer, variableDomain).infix() + ";");
}
printWriter.println("PSEUDO_CONSTANT_END");
printWriter.println();
}
if (numIndep != 0) {
printWriter.println("FAST_RATE_BEGIN");
Enumeration<Expression> enum1 = fs_analyzer.getFastRateExpressions();
while (enum1.hasMoreElements()) {
Expression exp = enum1.nextElement();
printWriter.println(subsituteExpression(exp, fs_analyzer, variableDomain).infix() + ";");
}
printWriter.println("FAST_RATE_END");
printWriter.println();
}
if (numDep != 0) {
printWriter.println("FAST_DEPENDENCY_BEGIN");
Enumeration<Expression> enum_exp = fs_analyzer.getDependencyExps();
Enumeration<Variable> enum_var = fs_analyzer.getDependentVariables();
while (enum_exp.hasMoreElements()) {
Expression exp = enum_exp.nextElement();
Variable depVar = enum_var.nextElement();
printWriter.println(depVar.getName() + " " + subsituteExpression(exp, fs_analyzer, variableDomain).infix() + ";");
}
printWriter.println("FAST_DEPENDENCY_END");
printWriter.println();
}
if (numIndep != 0) {
printWriter.println("JACOBIAN_BEGIN");
Enumeration<Expression> enum_fre = fs_analyzer.getFastRateExpressions();
while (enum_fre.hasMoreElements()) {
Expression fre = enum_fre.nextElement();
Enumeration<Variable> enum_var = fs_analyzer.getIndependentVariables();
while (enum_var.hasMoreElements()) {
Variable var = enum_var.nextElement();
Expression exp = subsituteExpression(fre, fs_analyzer, variableDomain).flatten();
Expression differential = exp.differentiate(var.getName());
printWriter.println(subsituteExpression(differential, fs_analyzer, variableDomain).infix() + ";");
}
}
printWriter.println("JACOBIAN_END");
printWriter.println();
}
printWriter.println("FAST_SYSTEM_END");
printWriter.println();
}
Aggregations