use of cbit.vcell.solver.server.SolverListener in project vcell by virtualcell.
the class SimulationServiceImpl method computeModel.
private SimulationInfo computeModel(BioModel bioModel, SimulationSpec simSpec, ClientTaskStatusSupport statusCallback) {
try {
SimulationContext simContext = bioModel.getSimulationContext(0);
MathMappingCallback callback = new MathMappingCallbackTaskAdapter(statusCallback);
Simulation newsim = simContext.addNewSimulation(SimulationOwner.DEFAULT_SIM_NAME_PREFIX, callback, NetworkGenerationRequirements.AllowTruncatedStandardTimeout);
SimulationInfo simulationInfo = new SimulationInfo();
simulationInfo.setId(Math.abs(new Random().nextInt(1000000)));
// ----------- run simulation(s)
final File localSimDataDir = ResourceUtil.getLocalSimDir(User.tempUser.getName());
Simulation simulation = new TempSimulation(newsim, false);
final SimulationServiceContext simServiceContext = new SimulationServiceContext();
simServiceContext.simInfo = simulationInfo;
simServiceContext.simState = SimulationState.running;
simServiceContext.simTask = new SimulationTask(new SimulationJob(simulation, 0, null), 0);
simServiceContext.vcDataIdentifier = simServiceContext.simTask.getSimulationJob().getVCDataIdentifier();
simServiceContext.solver = createQuickRunSolver(localSimDataDir, simServiceContext.simTask);
simServiceContext.localSimDataDir = localSimDataDir;
if (simServiceContext.solver == null) {
throw new RuntimeException("null solver");
}
sims.put(simulationInfo.id, simServiceContext);
simServiceContext.solver.addSolverListener(new SolverListener() {
public void solverStopped(SolverEvent event) {
simServiceContext.simState = SimulationState.failed;
System.err.println("Simulation stopped");
}
public void solverStarting(SolverEvent event) {
simServiceContext.simState = SimulationState.running;
updateStatus(event);
}
public void solverProgress(SolverEvent event) {
simServiceContext.simState = SimulationState.running;
updateStatus(event);
}
public void solverPrinted(SolverEvent event) {
simServiceContext.simState = SimulationState.running;
}
public void solverFinished(SolverEvent event) {
try {
getDataSetController(simServiceContext).getDataSetTimes(simServiceContext.vcDataIdentifier);
simServiceContext.simState = SimulationState.done;
} catch (DataAccessException e) {
simServiceContext.simState = SimulationState.failed;
e.printStackTrace();
}
updateStatus(event);
}
public void solverAborted(SolverEvent event) {
simServiceContext.simState = SimulationState.failed;
System.err.println(event.getSimulationMessage().getDisplayMessage());
}
private void updateStatus(SolverEvent event) {
if (statusCallback == null)
return;
statusCallback.setMessage(event.getSimulationMessage().getDisplayMessage());
statusCallback.setProgress((int) (event.getProgress() * 100));
}
});
simServiceContext.solver.startSolver();
return simServiceContext.simInfo;
} catch (Exception e) {
e.printStackTrace(System.out);
// remember the exceptiopn ... fail the status ... save the error message
return new SimulationInfo().setId(1);
}
}
use of cbit.vcell.solver.server.SolverListener in project vcell by virtualcell.
the class SolverPreprocessor method main.
public static void main(java.lang.String[] args) {
if (args.length < 2) {
System.out.print(SolverPreprocessor.class.getName() + " ");
System.out.println(Arrays.toString(args));
System.out.println("Missing arguments: " + SolverPreprocessor.class.getName() + " [simulationTaskFile] [userdir] <parallel dir> ");
System.exit(1);
}
File parallelDirectory = null;
if (args.length >= 3) {
parallelDirectory = new File(args[2]);
if (!parallelDirectory.exists()) {
parallelDirectory.mkdirs();
}
if (!parallelDirectory.isDirectory() || !parallelDirectory.canWrite()) {
throw new IllegalArgumentException(parallelDirectory.getAbsolutePath() + " is not a writeable directory");
}
}
Logging.init();
try {
PropertyLoader.loadProperties();
//
File simulationFile = new File(args[0]);
final SimulationTask simTask = XmlHelper.XMLToSimTask(FileUtils.readFileToString(simulationFile));
if (parallelDirectory != null) {
// simulation task needs to be written to the "parallel directory" (a temporary directory) here (it is local to the cluster).
FileUtils.copyFile(simulationFile, new File(parallelDirectory, simulationFile.getName()));
}
File userDirectory = new File(args[1]);
final String hostName = null;
VCMongoMessage.serviceStartup(ServiceName.solverPreprocessor, Integer.valueOf(simTask.getSimKey().toString()), args);
//
// JMX registration
//
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
mbs.registerMBean(new VCellServiceMXBeanImpl(), new ObjectName(VCellServiceMXBean.jmxObjectName));
final HTCSolver htcSolver = new HTCSolver(simTask, userDirectory, parallelDirectory) {
public void startSolver() {
try {
super.initialize();
} catch (Exception e) {
e.printStackTrace();
SimulationMessage simMessage = SimulationMessage.jobFailed(e.getMessage());
try {
sendFailureAndExit(this, simTask, hostName, simMessage);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public void stopSolver() {
}
public double getCurrentTime() {
return 0;
}
public double getProgress() {
return 0;
}
};
SolverListener solverListener = new SolverListener() {
public void solverStopped(SolverEvent event) {
VCMongoMessage.sendSolverEvent(event);
try {
sendFailureAndExit(htcSolver, simTask, hostName, event.getSimulationMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
public void solverStarting(SolverEvent event) {
VCMongoMessage.sendSolverEvent(event);
}
public void solverProgress(SolverEvent event) {
VCMongoMessage.sendSolverEvent(event);
}
public void solverPrinted(SolverEvent event) {
VCMongoMessage.sendSolverEvent(event);
}
public void solverFinished(SolverEvent event) {
VCMongoMessage.sendSolverEvent(event);
}
public void solverAborted(SolverEvent event) {
VCMongoMessage.sendSolverEvent(event);
try {
sendFailureAndExit(htcSolver, simTask, hostName, event.getSimulationMessage());
} catch (VCMessagingException e) {
e.printStackTrace();
}
}
};
htcSolver.addSolverListener(solverListener);
htcSolver.startSolver();
VCMongoMessage.sendInfo("preprocessor done");
exitWithCode(0);
} catch (Throwable e) {
lg.error(e.getMessage(), e);
exitWithCode(-1);
}
}
use of cbit.vcell.solver.server.SolverListener in project vcell by virtualcell.
the class SimulationServiceImpl method computeModel.
private SimulationInfo computeModel(VCDocument vcDoc, SimulationSpec simSpec, ClientTaskStatusSupport statusCallback) throws Exception {
// try {
Simulation newsim = null;
if (vcDoc instanceof BioModel) {
SimulationContext simContext = ((BioModel) vcDoc).getSimulationContext(0);
MathMappingCallback callback = new MathMappingCallbackTaskAdapter(statusCallback);
newsim = simContext.addNewSimulation(SimulationOwner.DEFAULT_SIM_NAME_PREFIX, callback, NetworkGenerationRequirements.AllowTruncatedStandardTimeout);
} else if (vcDoc instanceof MathModel) {
newsim = ((MathModel) vcDoc).addNewSimulation(SimulationOwner.DEFAULT_SIM_NAME_PREFIX);
} else {
throw new IllegalArgumentException("computeModel expecting BioModel or MathModel but got " + vcDoc.getClass().getName());
}
GeometrySpec geometrySpec = newsim.getMathDescription().getGeometry().getGeometrySpec();
VCImage image = geometrySpec.getImage();
if (image != null) {
newsim.getMeshSpecification().setSamplingSize(new ISize(image.getNumX(), image.getNumY(), image.getNumZ()));
}
// newsim.getMeshSpecification().setSamplingSize(new ISize(simContext.getGeometry().getGeometrySpec().getImage().getNumX(), simContext.getGeometry().getGeometrySpec().getImage().getNumY(), simContext.getGeometry().getGeometrySpec().getImage().getNumZ()));
newsim.getSolverTaskDescription().setTimeBounds(new TimeBounds(0, simSpec.getTotalTime()));
newsim.getSolverTaskDescription().setOutputTimeSpec(new UniformOutputTimeSpec(simSpec.getOutputTimeStep()));
SimulationInfo simulationInfo = new SimulationInfo(Math.abs(new Random().nextInt(1000000)));
// ----------- run simulation(s)
final File localSimDataDir = ResourceUtil.getLocalSimDir(User.tempUser.getName());
Simulation simulation = new TempSimulation(newsim, false);
final SimulationServiceContext simServiceContext = new SimulationServiceContext();
simServiceContext.simInfo = simulationInfo;
simServiceContext.simState = SimulationState.running;
simServiceContext.simTask = new SimulationTask(new SimulationJob(simulation, 0, null), 0);
simServiceContext.vcDataIdentifier = simServiceContext.simTask.getSimulationJob().getVCDataIdentifier();
simServiceContext.solver = createQuickRunSolver(localSimDataDir, simServiceContext.simTask);
simServiceContext.localSimDataDir = localSimDataDir;
if (simServiceContext.solver == null) {
throw new RuntimeException("null solver");
}
sims.put(simulationInfo.getLocalId(), simServiceContext);
simServiceContext.solver.addSolverListener(new SolverListener() {
public void solverStopped(SolverEvent event) {
simServiceContext.simState = SimulationState.failed;
System.err.println("Simulation stopped");
}
public void solverStarting(SolverEvent event) {
simServiceContext.simState = SimulationState.running;
updateStatus(event);
}
public void solverProgress(SolverEvent event) {
simServiceContext.simState = SimulationState.running;
updateStatus(event);
}
public void solverPrinted(SolverEvent event) {
simServiceContext.simState = SimulationState.running;
}
public void solverFinished(SolverEvent event) {
try {
getDataSetController(simServiceContext).getDataSetTimes(simServiceContext.vcDataIdentifier);
simServiceContext.simState = SimulationState.done;
} catch (DataAccessException e) {
simServiceContext.simState = SimulationState.failed;
e.printStackTrace();
}
updateStatus(event);
}
public void solverAborted(SolverEvent event) {
simServiceContext.simState = SimulationState.failed;
System.err.println(event.getSimulationMessage().getDisplayMessage());
}
private void updateStatus(SolverEvent event) {
if (statusCallback == null)
return;
statusCallback.setMessage(event.getSimulationMessage().getDisplayMessage());
statusCallback.setProgress((int) (event.getProgress() * 100));
}
});
if (true) {
// debug
if (vcDoc instanceof BioModel) {
XmlUtil.writeXMLStringToFile(XmlHelper.bioModelToXML(((BioModel) vcDoc)), "C:\\Users\\frm\\vcGititImageJWorkspace\\vcell\\imagej_BM_true.xml", true);
} else if (vcDoc instanceof MathModel) {
XmlUtil.writeXMLStringToFile(XmlHelper.mathModelToXML(((MathModel) vcDoc)), "C:\\Users\\frm\\vcGititImageJWorkspace\\vcell\\imagej_MM_true.xml", true);
}
}
simServiceContext.solver.startSolver();
return simServiceContext.simInfo;
// } catch (Exception e){
// e.printStackTrace(System.out);
// // remember the exceptiopn ... fail the status ... save the error message
// return new SimulationInfo();
// }
}
use of cbit.vcell.solver.server.SolverListener in project vcell by virtualcell.
the class AbstractSolver method fireSolverStarting.
/**
* Method to support listener events.
*/
protected void fireSolverStarting(SimulationMessage message) {
// Create event
SolverEvent event = new SolverEvent(this, SolverEvent.SOLVER_STARTING, message, 0, 0, message.getHtcJobId());
VCMongoMessage.sendSolverEvent(event);
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == SolverListener.class) {
((SolverListener) listeners[i + 1]).solverStarting(event);
}
}
}
use of cbit.vcell.solver.server.SolverListener in project vcell by virtualcell.
the class AbstractSolver method fireSolverFinished.
/**
* Method to support listener events.
*/
protected void fireSolverFinished() {
// Create event
SolverEvent event = new SolverEvent(this, SolverEvent.SOLVER_FINISHED, SimulationMessage.MESSAGE_SOLVEREVENT_FINISHED, getProgress(), getCurrentTime(), null);
VCMongoMessage.sendSolverEvent(event);
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == SolverListener.class) {
((SolverListener) listeners[i + 1]).solverFinished(event);
}
}
}
Aggregations