use of cbit.vcell.geometry.SubVolume in project vcell by virtualcell.
the class FiniteVolumeFileWriter method writeChomboSpec.
private void writeChomboSpec() throws ExpressionException, SolverException, PropertyVetoException, ClassNotFoundException, IOException, GeometryException, ImageException {
if (!bChomboSolver) {
return;
}
GeometrySpec geometrySpec = resampledGeometry.getGeometrySpec();
int dimension = geometrySpec.getDimension();
if (dimension == 1) {
throw new SolverException(simTask.getSimulation().getSolverTaskDescription().getSolverDescription().getDisplayLabel() + " is only supported for simulations with 2D or 3D geometry.");
}
Simulation simulation = getSimulationTask().getSimulation();
SolverTaskDescription solverTaskDescription = simulation.getSolverTaskDescription();
ChomboSolverSpec chomboSolverSpec = solverTaskDescription.getChomboSolverSpec();
printWriter.println(FVInputFileKeyword.CHOMBO_SPEC_BEGIN);
printWriter.println(FVInputFileKeyword.DIMENSION + " " + geometrySpec.getDimension());
Extent extent = geometrySpec.getExtent();
Origin origin = geometrySpec.getOrigin();
ISize isize = simulation.getMeshSpecification().getSamplingSize();
switch(geometrySpec.getDimension()) {
case 2:
printWriter.println(FVInputFileKeyword.MESH_SIZE + " " + isize.getX() + " " + isize.getY());
printWriter.println(FVInputFileKeyword.DOMAIN_SIZE + " " + extent.getX() + " " + extent.getY());
printWriter.println(FVInputFileKeyword.DOMAIN_ORIGIN + " " + origin.getX() + " " + origin.getY());
break;
case 3:
printWriter.println(FVInputFileKeyword.MESH_SIZE + " " + isize.getX() + " " + isize.getY() + " " + isize.getZ());
printWriter.println(FVInputFileKeyword.DOMAIN_SIZE + " " + extent.getX() + " " + extent.getY() + " " + extent.getZ());
printWriter.println(FVInputFileKeyword.DOMAIN_ORIGIN + " " + origin.getX() + " " + origin.getY() + " " + origin.getZ());
break;
}
List<CompartmentSubDomain> featureList = new ArrayList<CompartmentSubDomain>();
Enumeration<SubDomain> enum1 = simulation.getMathDescription().getSubDomains();
while (enum1.hasMoreElements()) {
SubDomain sd = enum1.nextElement();
if (sd instanceof CompartmentSubDomain) {
featureList.add((CompartmentSubDomain) sd);
}
}
int numFeatures = featureList.size();
CompartmentSubDomain[] features = featureList.toArray(new CompartmentSubDomain[0]);
int[] phases = new int[numFeatures];
Arrays.fill(phases, -1);
phases[numFeatures - 1] = 0;
int[] numAssigned = new int[] { 1 };
assignPhases(features, numFeatures - 1, phases, numAssigned);
Map<String, Integer> subDomainPhaseMap = new HashMap<String, Integer>();
for (int i = 0; i < phases.length; ++i) {
if (phases[i] == -1) {
throw new SolverException("Failed to assign a phase to CompartmentSubdomain '" + features[i].getName() + "'. It might be caused by too coarsh a mesh.");
}
subDomainPhaseMap.put(features[i].getName(), phases[i]);
}
SubVolume[] subVolumes = geometrySpec.getSubVolumes();
if (geometrySpec.hasImage()) {
Geometry geometry = (Geometry) BeanUtils.cloneSerializable(simulation.getMathDescription().getGeometry());
Geometry simGeometry = geometry;
VCImage img = geometry.getGeometrySpec().getImage();
int factor = Math.max(Math.max(img.getNumX(), img.getNumY()), img.getNumZ()) < 512 ? 2 : 1;
ISize distanceMapMeshSize = new ISize(img.getNumX() * factor, img.getNumY() * factor, img.getNumZ() * factor);
Vect3d deltaX = null;
boolean bCellCentered = false;
double dx = 0.5;
double dy = 0.5;
double dz = 0.5;
int Nx = distanceMapMeshSize.getX();
int Ny = distanceMapMeshSize.getY();
int Nz = distanceMapMeshSize.getZ();
if (dimension == 2) {
// pad the 2D image with itself in order to obtain a 3D image used to compute the distance map
// because the distance map algorithm is 3D only (using distance to triangles)
byte[] oldPixels = img.getPixels();
byte[] newPixels = new byte[oldPixels.length * 3];
System.arraycopy(oldPixels, 0, newPixels, 0, oldPixels.length);
System.arraycopy(oldPixels, 0, newPixels, oldPixels.length, oldPixels.length);
System.arraycopy(oldPixels, 0, newPixels, oldPixels.length * 2, oldPixels.length);
double distX = geometry.getExtent().getX() / img.getNumX();
double distY = geometry.getExtent().getY() / img.getNumY();
// we set the distance on the z axis to something that makes sense
double distZ = Math.max(distX, distY);
Extent newExtent = new Extent(geometry.getExtent().getX(), geometry.getExtent().getY(), distZ * 3);
VCImage newImage = new VCImageUncompressed(null, newPixels, newExtent, img.getNumX(), img.getNumY(), 3);
// copy the pixel classes too
ArrayList<VCPixelClass> newPixelClasses = new ArrayList<VCPixelClass>();
for (VCPixelClass origPixelClass : geometry.getGeometrySpec().getImage().getPixelClasses()) {
SubVolume origSubvolume = geometry.getGeometrySpec().getImageSubVolumeFromPixelValue(origPixelClass.getPixel());
newPixelClasses.add(new VCPixelClass(null, origSubvolume.getName(), origPixelClass.getPixel()));
}
newImage.setPixelClasses(newPixelClasses.toArray(new VCPixelClass[newPixelClasses.size()]));
simGeometry = new Geometry(geometry, newImage);
Nz = 3;
}
GeometrySpec simGeometrySpec = simGeometry.getGeometrySpec();
Extent simExtent = simGeometrySpec.getExtent();
dx = simExtent.getX() / (Nx - 1);
dy = simExtent.getY() / (Ny - 1);
dz = simExtent.getZ() / (Nz - 1);
if (Math.abs(dx - dy) > 0.1 * Math.max(dx, dy)) {
dx = Math.min(dx, dy);
dy = dx;
Nx = (int) (simExtent.getX() / dx + 1);
Ny = (int) (simExtent.getY() / dx + 1);
if (dimension == 3) {
dz = dx;
Nz = (int) (simExtent.getZ() / dx + 1);
}
}
deltaX = new Vect3d(dx, dy, dz);
// one more point in each direction
distanceMapMeshSize = new ISize(Nx + 1, Ny + 1, Nz + 1);
Extent distanceMapExtent = new Extent(simExtent.getX() + dx, simExtent.getY() + dy, simExtent.getZ() + dz);
simGeometrySpec.setExtent(distanceMapExtent);
GeometrySurfaceDescription geoSurfaceDesc = simGeometry.getGeometrySurfaceDescription();
geoSurfaceDesc.setVolumeSampleSize(distanceMapMeshSize);
geoSurfaceDesc.updateAll();
VCImage vcImage = RayCaster.sampleGeometry(simGeometry, distanceMapMeshSize, bCellCentered);
SubvolumeSignedDistanceMap[] distanceMaps = DistanceMapGenerator.computeDistanceMaps(simGeometry, vcImage, bCellCentered);
if (dimension == 2) {
distanceMaps = DistanceMapGenerator.extractMiddleSlice(distanceMaps);
}
printWriter.println(FVInputFileKeyword.SUBDOMAINS + " " + simGeometrySpec.getNumSubVolumes() + " " + FVInputFileKeyword.DISTANCE_MAP);
for (int i = 0; i < subVolumes.length; i++) {
File distanceMapFile = new File(workingDirectory, getSimulationTask().getSimulationJobID() + "_" + subVolumes[i].getName() + DISTANCE_MAP_FILE_EXTENSION);
writeDistanceMapFile(deltaX, distanceMaps[i], distanceMapFile);
int phase = subDomainPhaseMap.get(subVolumes[i].getName());
printWriter.println(subVolumes[i].getName() + " " + phase + " " + distanceMapFile.getAbsolutePath());
}
} else {
printWriter.println(FVInputFileKeyword.SUBDOMAINS + " " + geometrySpec.getNumSubVolumes());
Expression[] rvachevExps = convertAnalyticGeometryToRvachevFunction(geometrySpec);
for (int i = 0; i < subVolumes.length; i++) {
if (subVolumes[i] instanceof AnalyticSubVolume) {
String name = subVolumes[i].getName();
int phase = subDomainPhaseMap.get(name);
printWriter.println(name + " " + phase + " ");
printWriter.println(FVInputFileKeyword.IF + " " + rvachevExps[i].infix() + ";");
printWriter.println(FVInputFileKeyword.USER + " " + ((AnalyticSubVolume) subVolumes[i]).getExpression().infix() + ";");
}
}
}
printWriter.println(FVInputFileKeyword.MAX_BOX_SIZE + " " + chomboSolverSpec.getMaxBoxSize());
printWriter.println(FVInputFileKeyword.FILL_RATIO + " " + chomboSolverSpec.getFillRatio());
printWriter.println(FVInputFileKeyword.RELATIVE_TOLERANCE + " " + simulation.getSolverTaskDescription().getErrorTolerance().getRelativeErrorTolerance());
printWriter.println(FVInputFileKeyword.SAVE_VCELL_OUTPUT + " " + chomboSolverSpec.isSaveVCellOutput());
printWriter.println(FVInputFileKeyword.SAVE_CHOMBO_OUTPUT + " " + chomboSolverSpec.isSaveChomboOutput());
printWriter.println(FVInputFileKeyword.ACTIVATE_FEATURE_UNDER_DEVELOPMENT + " " + chomboSolverSpec.isActivateFeatureUnderDevelopment());
printWriter.println(FVInputFileKeyword.SMALL_VOLFRAC_THRESHOLD + " " + chomboSolverSpec.getSmallVolfracThreshold());
printWriter.println(FVInputFileKeyword.BLOCK_FACTOR + " " + chomboSolverSpec.getBlockFactor());
printWriter.println(FVInputFileKeyword.TAGS_GROW + " " + chomboSolverSpec.getTagsGrow());
// Refinement
int numLevels = chomboSolverSpec.getNumRefinementLevels();
// Refinements #Levels ratio 1, ratio 2, etc
printWriter.print(FVInputFileKeyword.REFINEMENTS + " " + (numLevels + 1));
List<Integer> ratios = chomboSolverSpec.getRefineRatioList();
for (int i : ratios) {
printWriter.print(" " + i);
}
// write last refinement ratio, fake
printWriter.println(" 2");
// membrane rois
List<RefinementRoi> memRios = chomboSolverSpec.getMembraneRefinementRois();
printWriter.println(FVInputFileKeyword.REFINEMENT_ROIS + " " + RoiType.Membrane + " " + memRios.size());
for (RefinementRoi roi : memRios) {
if (roi.getRoiExpression() == null) {
throw new SolverException("ROI expression cannot be null");
}
// level tagsGrow ROIexpression
printWriter.println(roi.getLevel() + " " + roi.getRoiExpression().infix() + ";");
}
List<RefinementRoi> volRios = chomboSolverSpec.getVolumeRefinementRois();
printWriter.println(FVInputFileKeyword.REFINEMENT_ROIS + " " + RoiType.Volume + " " + volRios.size());
for (RefinementRoi roi : volRios) {
if (roi.getRoiExpression() == null) {
throw new SolverException("ROI expression cannot be null");
}
printWriter.println(roi.getLevel() + " " + roi.getRoiExpression().infix() + ";");
}
printWriter.println(FVInputFileKeyword.VIEW_LEVEL + " " + chomboSolverSpec.getViewLevel());
printWriter.println(FVInputFileKeyword.CHOMBO_SPEC_END);
printWriter.println();
}
use of cbit.vcell.geometry.SubVolume in project vcell by virtualcell.
the class OutputFunctionContext method getAutoCompleteSymbolFilter.
public AutoCompleteSymbolFilter getAutoCompleteSymbolFilter(final Domain functionDomain) {
AutoCompleteSymbolFilter stef = new AutoCompleteSymbolFilter() {
public boolean accept(SymbolTableEntry ste) {
if (simulationOwner.getGeometry().getDimension() > 0) {
if (functionDomain == null) {
return true;
}
if (ste.getName().endsWith(InsideVariable.INSIDE_VARIABLE_SUFFIX) || ste.getName().endsWith(OutsideVariable.OUTSIDE_VARIABLE_SUFFIX)) {
return false;
}
if (ste instanceof ReservedVariable) {
return true;
}
if (ste instanceof AnnotatedFunction) {
return functionDomain.compareEqual(((AnnotatedFunction) ste).getDomain());
}
if (ste instanceof Variable) {
Variable var = (Variable) ste;
if (var.getDomain() == null) {
return true;
}
GeometryClass gc = simulationOwner.getGeometry().getGeometryClass(functionDomain.getName());
GeometryClass vargc = simulationOwner.getGeometry().getGeometryClass(var.getDomain().getName());
if (gc instanceof SurfaceClass && vargc instanceof SubVolume) {
if (((SurfaceClass) gc).isAdjacentTo((SubVolume) vargc)) {
return true;
} else {
return false;
}
} else {
return var.getDomain().compareEqual(functionDomain);
}
}
}
return true;
}
public boolean acceptFunction(String funcName) {
return true;
}
};
return stef;
}
use of cbit.vcell.geometry.SubVolume 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.geometry.SubVolume in project vcell by virtualcell.
the class GeometrySubVolumePanel method geometrySubVolumePanel_Initialize.
/**
* Comment
*/
private void geometrySubVolumePanel_Initialize() {
getScrollPaneTable().setDefaultRenderer(SubVolume.class, new GeometrySubVolumeTableCellRenderer());
getScrollPaneTable().setDefaultEditor(SubVolume.class, new DefaultCellEditor(new JTextField()) {
private int lastRow = -1;
private int lastCol = -1;
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
lastRow = row;
lastCol = column;
delegate.setValue(((SubVolume) value).getName());
return editorComponent;
}
public final boolean stopCellEditing() {
//
try {
String name = (String) delegate.getCellEditorValue();
while (true) {
if (name.equals(TokenMangler.fixTokenStrict(name))) {
break;
}
name = DialogUtils.showInputDialog0(getComponent(), "Subdomain name " + name + " has illegal characters." + "\nProvide new value.", name);
}
// VALIDATE_OK, delegate gets New Good value
delegate.setValue(name);
} catch (UtilCancelException e) {
// delegate gets Last Good value
delegate.setValue(((SubVolume) getScrollPaneTable().getValueAt(lastRow, lastCol)).getName());
} catch (Throwable e) {
// Delegate keeps UNVALIDATED value
}
return super.stopCellEditing();
}
});
getScrollPaneTable().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
refreshButtons();
}
use of cbit.vcell.geometry.SubVolume in project vcell by virtualcell.
the class GeometrySubVolumeTableCellRenderer method getTableCellRendererComponent.
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value instanceof SubVolume) {
SubVolume subVolume = (SubVolume) value;
java.awt.Color handleColor = new java.awt.Color(colormap[subVolume.getHandle()]);
setIcon(new ColorIcon(15, 15, handleColor));
setText(subVolume.getName());
}
return this;
}
Aggregations