use of cbit.vcell.solver.SolverException 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.solver.SolverException in project vcell by virtualcell.
the class AdamsMoultonFiveSolver method integrate.
/**
* This method was created by a SmartGuide.
* THIS HAS NOT BEEN UPDATED LIKE ODEIntegrator.integrate () and
* RungeKuttaFehlbergIntegrator.integrate()...
*/
protected void integrate() throws SolverException, UserStopException, IOException {
try {
SolverTaskDescription taskDescription = simTask.getSimulation().getSolverTaskDescription();
double timeStep = taskDescription.getTimeStep().getDefaultTimeStep();
fieldCurrentTime = taskDescription.getTimeBounds().getStartingTime();
// before computation begins, settle fast equilibrium
if (getFastAlgebraicSystem() != null) {
fieldValueVectors.copyValues(0, 1);
getFastAlgebraicSystem().initVars(getValueVector(0), getValueVector(1));
getFastAlgebraicSystem().solveSystem(getValueVector(0), getValueVector(1));
fieldValueVectors.copyValues(1, 0);
}
// check for failure
check(getValueVector(0));
// Evaluate
for (int i = 0; i < getStateVariableCount(); i++) {
f[0][getVariableIndex(i)] = evaluate(getValueVector(0), i);
}
// check for failure
check(getValueVector(0));
updateResultSet();
//
int iteration = 0;
while (fieldCurrentTime < taskDescription.getTimeBounds().getEndingTime()) {
checkForUserStop();
if (iteration < 3) {
// Take Runge-Kutta step...
prep(fieldCurrentTime, timeStep);
} else {
// Take Adams-Moulton step...
step(fieldCurrentTime, timeStep);
}
// update (old = new)
fieldValueVectors.copyValuesDown();
// compute fast system
if (getFastAlgebraicSystem() != null) {
fieldValueVectors.copyValues(0, 1);
getFastAlgebraicSystem().initVars(getValueVector(0), getValueVector(1));
getFastAlgebraicSystem().solveSystem(getValueVector(0), getValueVector(1));
fieldValueVectors.copyValues(1, 0);
}
// check for failure
check(getValueVector(0));
if (iteration < 3) {
for (int i = 0; i < getStateVariableCount(); i++) {
f[iteration + 1][getVariableIndex(i)] = evaluate(getValueVector(0), i);
}
// check for failure
check(f[iteration + 1]);
} else {
// Evaluate
for (int i = 0; i < getStateVariableCount(); i++) {
f[4][getVariableIndex(i)] = evaluate(getValueVector(0), i);
}
// check for failure
check(f[4]);
shiftWorkArrays();
}
// fieldCurrentTime += timeStep;
iteration++;
fieldCurrentTime = taskDescription.getTimeBounds().getStartingTime() + iteration * timeStep;
// store results if it coincides with a save interval
if (taskDescription.getOutputTimeSpec().isDefault()) {
int keepEvery = ((DefaultOutputTimeSpec) taskDescription.getOutputTimeSpec()).getKeepEvery();
if ((iteration % keepEvery) == 0)
updateResultSet();
}
}
// store last time point
if (taskDescription.getOutputTimeSpec().isDefault()) {
int keepEvery = ((DefaultOutputTimeSpec) taskDescription.getOutputTimeSpec()).getKeepEvery();
if ((iteration % keepEvery) == 0)
updateResultSet();
}
} catch (ExpressionException expressionException) {
throw new SolverException(expressionException.getMessage());
} catch (MathException mathException) {
throw new SolverException(mathException.getMessage());
}
}
use of cbit.vcell.solver.SolverException in project vcell by virtualcell.
the class AdamsMoultonFiveSolver method step.
/**
* Integrate over time step using the forward Euler method (1st order explicit)
* results must be stored in NumVectors-1 = vector(4);
* t is the current time
* h is the time step
*/
protected void step(double t, double h) throws SolverException {
try {
double[] oldValues = getValueVector(0);
double[] newValues = getValueVector(1);
//
// update time
oldValues[getTimeIndex()] = t;
newValues[getTimeIndex()] = t + h;
// Predict
for (int i = 0; i < getStateVariableCount(); i++) {
int I = getVariableIndex(i);
newValues[I] = oldValues[I] + h * (55.0 * f[3][I] - 59.0 * f[2][I] + 37.0 * f[1][I] - 9.0 * f[0][I]) / 24.0;
}
// Evaluate
for (int i = 0; i < getStateVariableCount(); i++) {
f[4][getVariableIndex(i)] = evaluate(newValues, i);
}
// Correct
for (int i = 0; i < getStateVariableCount(); i++) {
int I = getVariableIndex(i);
newValues[I] = oldValues[I] + h * (9.0 * f[4][I] + 19.0 * f[3][I] - 5.0 * f[2][I] + 1.0 * f[1][I]) / 24.0;
}
} catch (ExpressionException expressionException) {
throw new SolverException(expressionException.getMessage());
}
}
use of cbit.vcell.solver.SolverException in project vcell by virtualcell.
the class CVodeSolverStandalone method initialize.
/**
* This method takes the place of the old runUnsteady()...
*/
protected void initialize() throws SolverException {
fireSolverStarting(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING_INIT);
super.initialize();
String inputFilename = getInputFilename();
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_RUNNING, SimulationMessage.MESSAGE_SOLVER_RUNNING_INPUT_FILE));
fireSolverStarting(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING_INPUT_FILE);
PrintWriter pw = null;
try {
pw = new java.io.PrintWriter(inputFilename);
CVodeFileWriter cvodeFileWriter = new CVodeFileWriter(pw, simTask, bMessaging);
cvodeFileWriter.write();
} catch (Exception e) {
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, SimulationMessage.solverAborted("CVODE solver could not generate input file: " + e.getMessage())));
e.printStackTrace(System.out);
throw new SolverException("CVODE solver could not generate input file: " + e.getMessage());
} finally {
if (pw != null) {
pw.close();
}
}
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_RUNNING, SimulationMessage.MESSAGE_SOLVER_RUNNING_START));
setMathExecutable(new MathExecutable(getMathExecutableCommand(), getSaveDirectory()));
}
use of cbit.vcell.solver.SolverException in project vcell by virtualcell.
the class AbstractJavaSolver method runSolver.
/**
* Insert the method's description here.
* Creation date: (6/26/2001 3:08:31 PM)
*/
public void runSolver() {
try {
fieldRunning = true;
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_STARTING, SimulationMessage.MESSAGE_SOLVER_STARTING_INIT));
fireSolverStarting(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING_INIT);
initialize();
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_RUNNING, SimulationMessage.MESSAGE_SOLVER_RUNNING_START));
fireSolverProgress(getProgress());
integrate();
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_FINISHED, SimulationMessage.MESSAGE_SOLVER_FINISHED));
fireSolverFinished();
} catch (SolverException integratorException) {
lg.error(integratorException.getMessage(), integratorException);
SimulationMessage simulationMessage = SimulationMessage.solverAborted(integratorException.getMessage());
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, simulationMessage));
fireSolverAborted(simulationMessage);
} catch (IOException ioException) {
lg.error(ioException.getMessage(), ioException);
SimulationMessage simulationMessage = SimulationMessage.solverAborted(ioException.getMessage());
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, simulationMessage));
fireSolverAborted(simulationMessage);
} catch (UserStopException userStopException) {
lg.error(userStopException.getMessage(), userStopException);
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_STOPPED, SimulationMessage.solverStopped(userStopException.getMessage())));
fireSolverStopped();
} catch (Exception e) {
lg.error("AbstractJavaSolver.runSolver() : Caught Throwable instead of SolverException -- THIS EXCEPTION SHOULD NOT HAPPEN!", e);
SimulationMessage simulationMessage = SimulationMessage.solverAborted(e.getMessage());
setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, simulationMessage));
fireSolverAborted(simulationMessage);
} finally {
fieldRunning = false;
}
}
Aggregations