use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class RasterExporter method makeVTKImageData.
public ExportOutput[] makeVTKImageData(OutputContext outputContext, JobRequest jobRequest, User user, DataServerImpl dataServerImpl, ExportSpecs exportSpecs, FileDataContainerManager fileDataContainerManager) throws Exception {
String simID = exportSpecs.getVCDataIdentifier().getID();
VCDataIdentifier vcdID = exportSpecs.getVCDataIdentifier();
VariableSpecs variableSpecs = exportSpecs.getVariableSpecs();
TimeSpecs timeSpecs = exportSpecs.getTimeSpecs();
cbit.vcell.solvers.CartesianMesh mesh = dataServerImpl.getMesh(user, vcdID);
Vector<ExportOutput> exportOutV = new Vector<ExportOutput>();
for (int j = timeSpecs.getBeginTimeIndex(); j <= timeSpecs.getEndTimeIndex(); j++) {
exportServiceImpl.fireExportProgress(jobRequest.getJobID(), vcdID, "VTKIMG", (double) (j - timeSpecs.getBeginTimeIndex()) / (double) (timeSpecs.getEndTimeIndex() - timeSpecs.getBeginTimeIndex() + 1));
StringBuffer sb = new StringBuffer();
sb.append("# vtk DataFile Version 2.0" + "\n");
sb.append("Simulation " + vcdID.toString() + "\n");
sb.append("ASCII" + "\n");
sb.append("DATASET STRUCTURED_POINTS" + "\n");
sb.append("DIMENSIONS " + mesh.getSizeX() + " " + mesh.getSizeY() + " " + mesh.getSizeZ() + "\n");
sb.append("SPACING " + mesh.getExtent().getX() + " " + mesh.getExtent().getY() + " " + mesh.getExtent().getZ() + "\n");
sb.append("ORIGIN " + mesh.getOrigin().getX() + " " + mesh.getOrigin().getY() + " " + mesh.getOrigin().getZ() + "\n");
sb.append("POINT_DATA " + mesh.getNumVolumeElements() + "\n");
// write volume region ids
sb.append("SCALARS " + "regionID" + " double 1" + "\n");
sb.append("LOOKUP_TABLE default" + "\n");
int yzSize = mesh.getSizeY() * mesh.getSizeZ();
int index = 0;
for (int yz = 0; yz < yzSize; yz++) {
for (int x = 0; x < mesh.getSizeX(); x++) {
sb.append((x != 0 ? " " : "") + mesh.getVolumeRegionIndex(index));
index++;
}
sb.append("\n");
}
sb.append("\n");
for (int k = 0; k < variableSpecs.getVariableNames().length; k++) {
SimDataBlock simDataBlock = dataServerImpl.getSimDataBlock(outputContext, user, vcdID, variableSpecs.getVariableNames()[k], timeSpecs.getAllTimes()[j]);
if (simDataBlock.getVariableType().equals(VariableType.VOLUME)) {
sb.append("SCALARS " + variableSpecs.getVariableNames()[k] + " double 1" + "\n");
sb.append("LOOKUP_TABLE default" + "\n");
double[] volumeData = simDataBlock.getData();
index = 0;
for (int yz = 0; yz < yzSize; yz++) {
for (int x = 0; x < mesh.getSizeX(); x++) {
sb.append((x != 0 ? " " : "") + volumeData[index]);
index++;
}
sb.append("\n");
}
ExportOutput exportOut = new ExportOutput(true, ".vtk", simID.toString(), "_vol_" + j, fileDataContainerManager);
fileDataContainerManager.append(exportOut.getFileDataContainerID(), sb.toString());
exportOutV.add(exportOut);
} else {
throw new RuntimeException("VTK Image format only for volume data");
}
}
}
ExportOutput[] exportOutputArr = exportOutV.toArray(new ExportOutput[0]);
return exportOutputArr;
}
use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class MathTestingUtilities method comparePDEResultsWithExact.
/**
* Insert the method's description here.
* Creation date: (8/20/2003 12:58:10 PM)
*/
public static SimulationComparisonSummary comparePDEResultsWithExact(SimulationSymbolTable simSymbolTable, PDEDataManager dataManager, String type, double absErrorThreshold, double relErrorThreshold) throws DataAccessException, ExpressionException {
java.util.Hashtable<String, DataErrorSummary> tempVarHash = new java.util.Hashtable<String, DataErrorSummary>();
double[] timeArray = dataManager.getDataSetTimes();
Variable[] vars = simSymbolTable.getVariables();
CartesianMesh mesh = dataManager.getMesh();
MathDescription mathDesc = simSymbolTable.getSimulation().getMathDescription();
// Get volumeSubdomains from mathDesc/mesh and store in lookupTable
int numVol = mesh.getSizeX() * mesh.getSizeY() * mesh.getSizeZ();
CompartmentSubDomain[] volSubDomainLookup = new CompartmentSubDomain[numVol];
for (int i = 0; i < numVol; i++) {
int subVolumeIndex = mesh.getSubVolumeFromVolumeIndex(i);
SubVolume subVolume = mathDesc.getGeometry().getGeometrySpec().getSubVolume(subVolumeIndex);
CompartmentSubDomain compSubDomain = mathDesc.getCompartmentSubDomain(subVolume.getName());
volSubDomainLookup[i] = compSubDomain;
}
// Get membraneSubdomains from mathDesc/mesh and store in lookupTable
int numMem = mesh.getMembraneElements().length;
MembraneSubDomain[] memSubDomainLookup = new MembraneSubDomain[numMem];
for (int i = 0; i < numMem; i++) {
int insideVolIndex = mesh.getMembraneElements()[i].getInsideVolumeIndex();
int outsideVolIndex = mesh.getMembraneElements()[i].getOutsideVolumeIndex();
MembraneSubDomain memSubDomain = mathDesc.getMembraneSubDomain(volSubDomainLookup[insideVolIndex], volSubDomainLookup[outsideVolIndex]);
memSubDomainLookup[i] = memSubDomain;
}
double[] valueArray = new double[4];
SimpleSymbolTable symbolTable = new SimpleSymbolTable(new String[] { "t", "x", "y", "z" });
int tIndex = symbolTable.getEntry("t").getIndex();
int xIndex = symbolTable.getEntry("x").getIndex();
int yIndex = symbolTable.getEntry("y").getIndex();
int zIndex = symbolTable.getEntry("z").getIndex();
SimulationComparisonSummary simComparisonSummary = new SimulationComparisonSummary();
String hashKey = new String("");
long dataLength = 0;
// for each var, do the following :
for (int i = 0; i < vars.length; i++) {
if (vars[i] instanceof VolVariable || vars[i] instanceof MemVariable || vars[i] instanceof FilamentVariable || vars[i] instanceof VolumeRegionVariable || vars[i] instanceof MembraneRegionVariable || vars[i] instanceof FilamentRegionVariable) {
// for each time in timeArray,
for (int j = 0; j < timeArray.length; j++) {
if (type.equals(TestCaseNew.EXACT_STEADY)) {
if (j != (timeArray.length - 1)) {
continue;
}
}
// get data block from varName, data from datablock
SimDataBlock simDataBlock = dataManager.getSimDataBlock(vars[i].getName(), timeArray[j]);
double[] data = simDataBlock.getData();
dataLength = data.length;
SubDomain subDomain = null;
Coordinate subDomainCoord = null;
// for each point in data block ...
for (int k = 0; k < dataLength; k++) {
// Get subdomain from mesh (from the lookupTable), get coordinates (x,y,z) from mesh, evaluate EXACT SOLN at that coord
if (vars[i] instanceof VolVariable) {
subDomain = volSubDomainLookup[k];
subDomainCoord = mesh.getCoordinateFromVolumeIndex(k);
} else if (vars[i] instanceof MemVariable) {
subDomain = memSubDomainLookup[k];
subDomainCoord = mesh.getCoordinateFromMembraneIndex(k);
} else {
throw new RuntimeException("Var " + vars[i].getName() + " not supported yet!");
}
hashKey = vars[i].getName() + ":" + subDomain.getName();
DataErrorSummary tempVar = (DataErrorSummary) tempVarHash.get(hashKey);
if (tempVar == null) {
Expression exp = new Expression(subDomain.getEquation(vars[i]).getExactSolution());
exp.bindExpression(simSymbolTable);
exp = MathUtilities.substituteFunctions(exp, simSymbolTable);
exp = exp.flatten();
exp.bindExpression(symbolTable);
tempVar = new DataErrorSummary(exp);
tempVarHash.put(hashKey, tempVar);
}
// time
valueArray[tIndex] = timeArray[j];
// x
valueArray[xIndex] = subDomainCoord.getX();
// y
valueArray[yIndex] = subDomainCoord.getY();
// z
valueArray[zIndex] = subDomainCoord.getZ();
// EXACT soln at coord subDomainCoord
double value = tempVar.getExactExp().evaluateVector(valueArray);
tempVar.addDataValues(value, data[k], timeArray[j], k, absErrorThreshold, relErrorThreshold);
}
// end for (k)
}
// end for (j)
}
// end - if (var)
}
// end for (i)
Enumeration<String> enumKeys = tempVarHash.keys();
while (enumKeys.hasMoreElements()) {
String key = enumKeys.nextElement();
DataErrorSummary tempVarSummary = tempVarHash.get(key);
simComparisonSummary.addVariableComparisonSummary(new VariableComparisonSummary(key, tempVarSummary.getMinRef(), tempVarSummary.getMaxRef(), tempVarSummary.getMaxAbsoluteError(), tempVarSummary.getMaxRelativeError(), tempVarSummary.getL2Norm(), tempVarSummary.getTimeAtMaxAbsoluteError(), tempVarSummary.getIndexAtMaxAbsoluteError(), tempVarSummary.getTimeAtMaxRelativeError(), tempVarSummary.getIndexAtMaxRelativeError()));
}
return simComparisonSummary;
}
use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class FiniteVolumeFileWriter method writeFieldData.
/**
* # Field Data
* FIELD_DATA_BEGIN
* #id, name, varname, time filename
* 0 _VCell_FieldData_0 FRAP_binding_ALPHA rfB 0.1 \\users\\fgao\\SimID_22489731_0_FRAP_binding_ALPHA_rfB_0_1.fdat
* FIELD_DATA_END
* @throws FileNotFoundException
* @throws ExpressionException
* @throws DataAccessException
*/
private void writeFieldData() throws FileNotFoundException, ExpressionException, DataAccessException {
FieldDataIdentifierSpec[] fieldDataIDSpecs = simTask.getSimulationJob().getFieldDataIdentifierSpecs();
if (fieldDataIDSpecs == null || fieldDataIDSpecs.length == 0) {
return;
}
String secondarySimDataDir = PropertyLoader.getProperty(PropertyLoader.secondarySimDataDirInternalProperty, null);
DataSetControllerImpl dsci = new DataSetControllerImpl(null, workingDirectory.getParentFile(), secondarySimDataDir == null ? null : new File(secondarySimDataDir));
printWriter.println("# Field Data");
printWriter.println("FIELD_DATA_BEGIN");
printWriter.println("#id, type, new name, name, varname, time, filename");
FieldFunctionArguments psfFieldFunc = null;
Variable var = simTask.getSimulationJob().getSimulationSymbolTable().getVariable(Simulation.PSF_FUNCTION_NAME);
if (var != null) {
FieldFunctionArguments[] ffas = FieldUtilities.getFieldFunctionArguments(var.getExpression());
if (ffas == null || ffas.length == 0) {
throw new DataAccessException("Point Spread Function " + Simulation.PSF_FUNCTION_NAME + " can only be a single field function.");
} else {
Expression newexp = new Expression(ffas[0].infix());
if (!var.getExpression().compareEqual(newexp)) {
throw new DataAccessException("Point Spread Function " + Simulation.PSF_FUNCTION_NAME + " can only be a single field function.");
}
psfFieldFunc = ffas[0];
}
}
int index = 0;
HashSet<FieldDataIdentifierSpec> uniqueFieldDataIDSpecs = new HashSet<FieldDataIdentifierSpec>();
uniqueFieldDataNSet = new HashSet<FieldDataNumerics>();
for (int i = 0; i < fieldDataIDSpecs.length; i++) {
if (!uniqueFieldDataIDSpecs.contains(fieldDataIDSpecs[i])) {
FieldFunctionArguments ffa = fieldDataIDSpecs[i].getFieldFuncArgs();
File newResampledFieldDataFile = new File(workingDirectory, SimulationData.createCanonicalResampleFileName((VCSimulationDataIdentifier) simTask.getSimulationJob().getVCDataIdentifier(), fieldDataIDSpecs[i].getFieldFuncArgs()));
uniqueFieldDataIDSpecs.add(fieldDataIDSpecs[i]);
VariableType varType = fieldDataIDSpecs[i].getFieldFuncArgs().getVariableType();
SimDataBlock simDataBlock = dsci.getSimDataBlock(null, fieldDataIDSpecs[i].getExternalDataIdentifier(), fieldDataIDSpecs[i].getFieldFuncArgs().getVariableName(), fieldDataIDSpecs[i].getFieldFuncArgs().getTime().evaluateConstant());
VariableType dataVarType = simDataBlock.getVariableType();
if (varType.equals(VariableType.UNKNOWN)) {
varType = dataVarType;
} else if (!varType.equals(dataVarType)) {
throw new IllegalArgumentException("field function variable type (" + varType.getTypeName() + ") doesn't match real variable type (" + dataVarType.getTypeName() + ")");
}
if (psfFieldFunc != null && psfFieldFunc.equals(ffa)) {
psfFieldIndex = index;
}
String fieldDataID = "_VCell_FieldData_" + index;
printWriter.println(index + " " + varType.getTypeName() + " " + fieldDataID + " " + ffa.getFieldName() + " " + ffa.getVariableName() + " " + ffa.getTime().infix() + " " + newResampledFieldDataFile);
uniqueFieldDataNSet.add(new FieldDataNumerics(SimulationData.createCanonicalFieldFunctionSyntax(ffa.getFieldName(), ffa.getVariableName(), ffa.getTime().evaluateConstant(), ffa.getVariableType().getTypeName()), fieldDataID));
index++;
}
}
if (psfFieldIndex >= 0) {
printWriter.println("PSF_FIELD_DATA_INDEX " + psfFieldIndex);
}
printWriter.println("FIELD_DATA_END");
printWriter.println();
}
use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class RunSimulation2DOp method runRefSimulation.
public ImageTimeSeries<FloatImage> runRefSimulation(LocalWorkspace localWorkspace, Simulation simulation, String varName, ClientTaskStatusSupport progressListener) throws Exception {
User owner = LocalWorkspace.getDefaultOwner();
KeyValue simKey = LocalWorkspace.createNewKeyValue();
runFVSolverStandalone(new File(localWorkspace.getDefaultSimDataDirectory()), simulation, progressListener);
Extent extent = simulation.getMathDescription().getGeometry().getExtent();
Origin origin = simulation.getMathDescription().getGeometry().getOrigin();
VCDataIdentifier vcDataIdentifier = new VCSimulationDataIdentifier(new VCSimulationIdentifier(simKey, owner), 0);
CartesianMesh mesh = localWorkspace.getDataSetControllerImpl().getMesh(vcDataIdentifier);
ISize isize = new ISize(mesh.getSizeX(), mesh.getSizeY(), mesh.getSizeZ());
double[] dataTimes = localWorkspace.getDataSetControllerImpl().getDataSetTimes(vcDataIdentifier);
FloatImage[] solutionImages = new FloatImage[dataTimes.length];
for (int i = 0; i < dataTimes.length; i++) {
SimDataBlock simDataBlock = localWorkspace.getDataSetControllerImpl().getSimDataBlock(null, vcDataIdentifier, varName, dataTimes[i]);
double[] doubleData = simDataBlock.getData();
float[] floatPixels = new float[doubleData.length];
for (int j = 0; j < doubleData.length; j++) {
floatPixels[j] = (float) doubleData[j];
}
solutionImages[i] = new FloatImage(floatPixels, origin, extent, isize.getX(), isize.getY(), isize.getZ());
}
ImageTimeSeries<FloatImage> solution = new ImageTimeSeries<FloatImage>(FloatImage.class, solutionImages, dataTimes, 1);
return solution;
}
use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class RunRefSimulationOp method runRefSimulation.
private static ImageTimeSeries<FloatImage> runRefSimulation(LocalWorkspace localWorkspace, ROI cellROI, double timeStepVal, TimeBounds timeBounds, FloatImage initRefConc, double baseDiffusionRate, ClientTaskStatusSupport progressListener) throws Exception {
String INITCONC_EXTDATA_NAME = "initConc";
String INITCONC_EXTDATA_VARNAME = "conc";
String VAR_NAME = "species";
User owner = LocalWorkspace.getDefaultOwner();
KeyValue simKey = LocalWorkspace.createNewKeyValue();
ExternalDataInfo initialConcentrationExtData = createNewExternalDataInfo(localWorkspace, INITCONC_EXTDATA_NAME);
Extent extent = initRefConc.getExtent();
Origin origin = initRefConc.getOrigin();
ISize isize = new ISize(initRefConc.getNumX(), initRefConc.getNumY(), initRefConc.getNumZ());
saveExternalData(initRefConc, INITCONC_EXTDATA_VARNAME, initialConcentrationExtData.getExternalDataIdentifier(), localWorkspace);
FieldFunctionArguments initConditionFFA = new FieldFunctionArguments(INITCONC_EXTDATA_NAME, INITCONC_EXTDATA_VARNAME, new Expression(0.0), VariableType.VOLUME);
BioModel bioModel = createRefSimBioModel(simKey, owner, origin, extent, cellROI, timeStepVal, timeBounds, VAR_NAME, new Expression(initConditionFFA.infix()), baseDiffusionRate);
if (progressListener != null) {
progressListener.setMessage("Running Reference Simulation...");
}
// run simulation
runFVSolverStandalone(new File(localWorkspace.getDefaultSimDataDirectory()), bioModel.getSimulation(0), initialConcentrationExtData.getExternalDataIdentifier(), progressListener, true);
VCDataIdentifier vcDataIdentifier = new VCSimulationDataIdentifier(new VCSimulationIdentifier(simKey, owner), 0);
double[] dataTimes = localWorkspace.getDataSetControllerImpl().getDataSetTimes(vcDataIdentifier);
FloatImage[] solutionImages = new FloatImage[dataTimes.length];
for (int i = 0; i < dataTimes.length; i++) {
SimDataBlock simDataBlock = localWorkspace.getDataSetControllerImpl().getSimDataBlock(null, vcDataIdentifier, VAR_NAME, dataTimes[i]);
double[] doubleData = simDataBlock.getData();
float[] floatPixels = new float[doubleData.length];
for (int j = 0; j < doubleData.length; j++) {
floatPixels[j] = (float) doubleData[j];
}
solutionImages[i] = new FloatImage(floatPixels, origin, extent, isize.getX(), isize.getY(), isize.getZ());
}
ImageTimeSeries<FloatImage> solution = new ImageTimeSeries<FloatImage>(FloatImage.class, solutionImages, dataTimes, 1);
return solution;
}
Aggregations