use of cbit.vcell.messaging.server.SimulationTask in project vcell by virtualcell.
the class RunFakeSimOp method runFVSolverStandalone.
private static void runFVSolverStandalone(File simulationDataDir, Simulation sim, ClientTaskStatusSupport progressListener) throws Exception {
int jobIndex = 0;
SimulationTask simTask = new SimulationTask(new SimulationJob(sim, jobIndex, null), 0);
SolverUtilities.prepareSolverExecutable(sim.getSolverTaskDescription().getSolverDescription());
FVSolverStandalone fvSolver = new FVSolverStandalone(simTask, simulationDataDir, false);
fvSolver.startSolver();
// fvSolver.runSolver();
SolverStatus status = fvSolver.getSolverStatus();
while (status.getStatus() != SolverStatus.SOLVER_FINISHED && status.getStatus() != SolverStatus.SOLVER_ABORTED && status.getStatus() != SolverStatus.SOLVER_STOPPED) {
if (progressListener != null) {
progressListener.setProgress((int) (fvSolver.getProgress() * 100));
if (progressListener.isInterrupted()) {
fvSolver.stopSolver();
throw UserCancelException.CANCEL_GENERIC;
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace(System.out);
// catch interrupted exception and ignore it, otherwise it will popup a dialog in user interface saying"sleep interrupted"
}
status = fvSolver.getSolverStatus();
}
if (status.getStatus() != SolverStatus.SOLVER_FINISHED) {
throw new Exception("Sover did not finish normally." + status);
}
}
use of cbit.vcell.messaging.server.SimulationTask in project vcell by virtualcell.
the class StochtestRunService method runsolver.
private static void runsolver(Simulation newSimulation, File baseDirectory, int numRuns, TimeSeriesMultitrialData timeSeriesMultitrialData) {
Simulation versSimulation = null;
File destDir = null;
boolean bTimeout = false;
// int progress = 1;
for (int trialIndex = 0; trialIndex < numRuns; trialIndex++) {
System.out.println("\n=====================================\n\nStarting trial " + (trialIndex + 1) + " of " + numRuns + "\n\n==============================\n");
long startTime = System.currentTimeMillis();
// }
try {
versSimulation = new TempSimulation(newSimulation, false);
// printout(ruleBasedTestDir.getAbsolutePath());
destDir = new File(baseDirectory, timeSeriesMultitrialData.datasetName);
SimulationTask simTask = new SimulationTask(new SimulationJob(versSimulation, 0, null), 0);
Solver solver = ClientSimManager.createQuickRunSolver(destDir, simTask);
solver.startSolver();
while (true) {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (System.currentTimeMillis() - startTime > 30 * 1000) {
// timeout after 30 seconds .. otherwise multiple runs will take forever
bTimeout = true;
solver.stopSolver();
throw new RuntimeException("timed out");
}
SolverStatus solverStatus = solver.getSolverStatus();
if (solverStatus != null) {
if (solverStatus.getStatus() == SolverStatus.SOLVER_ABORTED) {
throw new RuntimeException(solverStatus.getSimulationMessage().getDisplayMessage());
}
if (solverStatus.getStatus() != SolverStatus.SOLVER_STARTING && solverStatus.getStatus() != SolverStatus.SOLVER_READY && solverStatus.getStatus() != SolverStatus.SOLVER_RUNNING) {
break;
}
}
}
SimulationData simData = new SimulationData(simTask.getSimulationJob().getVCDataIdentifier(), destDir, null, null);
ODEDataBlock odeDataBlock = simData.getODEDataBlock();
ODESimData odeSimData = odeDataBlock.getODESimData();
timeSeriesMultitrialData.addDataSet(odeSimData, trialIndex);
} catch (Exception e) {
e.printStackTrace();
File file = new File(baseDirectory, Simulation.createSimulationID(versSimulation.getKey()) + "_solverExc.txt");
StochtestFileUtils.writeMessageTofile(file, e.getMessage());
if (bTimeout) {
throw new RuntimeException("timed out");
} else {
throw new RuntimeException("solver failed : " + e.getMessage(), e);
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
StochtestFileUtils.clearDir(destDir);
}
// printout("\n");
}
use of cbit.vcell.messaging.server.SimulationTask in project vcell by virtualcell.
the class SbmlVcmlConverter method solveSimulation.
/**
* solves the simulation that is passed in throught the simulation job.
*/
private static void solveSimulation(SimulationJob simJob, String filePathName, SimSpec argSimSpec) /* Hashtable argSpUnitsHash, double argTimeFactor, Model.ReservedSymbol kMoleSymbol*/
{
ODESolverResultSet odeSolverResultSet = null;
try {
/* // Generate .idaInput string
File idaInputFile = new File(filePathName.replace(".vcml", ".idaInput"));
PrintWriter idaPW = new java.io.PrintWriter(idaInputFile);
IDAFileWriter idaFileWriter = new IDAFileWriter(idaPW, simJob);
idaFileWriter.write();
idaPW.close();
// use the SundialsStandaloneSolver
File idaOutputFile = new File(filePathName.replace(".vcml", ".ida"));
Executable executable = new Executable(new String[]{"SundialsSolverStandalone", idaInputFile.getAbsolutePath(), idaOutputFile.getAbsolutePath()});
executable.start();
*/
// Generate .cvodeInput string
File cvodeInputFile = new File(filePathName.replace(".vcml", ".cvodeInput"));
PrintWriter cvodePW = new java.io.PrintWriter(cvodeInputFile);
SimulationTask simTask = new SimulationTask(simJob, 0);
CVodeFileWriter cvodeFileWriter = new CVodeFileWriter(cvodePW, simTask);
cvodeFileWriter.write();
cvodePW.close();
// use the cvodeStandalone solver
// use the SundialsStandaloneSolver
File cvodeOutputFile = new File(filePathName.replace(".vcml", ".ida"));
/*
Executable executable = new Executable(new String[]{"d:/workspace/VCell_5.3_VCell/SundialsSolverStandalone_x64", cvodeInputFile.getAbsolutePath(), cvodeOutputFile.getAbsolutePath()});
executable.start();
*/
// use the cvodeStandalone solver
// use the SundialsStandaloneSolver
File[] exes = SolverUtilities.getExes(SolverDescription.CombinedSundials);
assert exes.length == 1 : "one and only one smoldyn solver expected";
File sundialsExe = exes[0];
Executable executable = new Executable(new String[] { sundialsExe.getAbsolutePath(), cvodeInputFile.getAbsolutePath(), cvodeOutputFile.getAbsolutePath() });
executable.start();
// get the result
odeSolverResultSet = VCellSBMLSolver.getODESolverResultSet(simJob, cvodeOutputFile.getPath());
} catch (Exception e) {
e.printStackTrace(System.out);
throw new RuntimeException("Error running NativeIDA solver : " + e.getMessage());
}
// now write out the results into CSV file
try {
if (odeSolverResultSet != null) {
File csvFile = new File(filePathName.replace(".vcml", ".csv"));
PrintStream outputStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(csvFile)));
outputStream.print("time");
for (int i = 0; i < argSimSpec.getVarsList().length; i++) {
outputStream.print("," + argSimSpec.getVarsList()[i]);
}
outputStream.println();
// extract data for time and species
double[][] data = new double[argSimSpec.getVarsList().length + 1][];
int column = odeSolverResultSet.findColumn("t");
data[0] = odeSolverResultSet.extractColumn(column);
int origDataLength = data[0].length;
for (int i = 0; i < argSimSpec.getVarsList().length; i++) {
column = odeSolverResultSet.findColumn(argSimSpec.getVarsList()[i]);
if (column == -1) {
Variable var = simJob.getSimulationSymbolTable().getVariable(argSimSpec.getVarsList()[i]);
data[i + 1] = new double[data[0].length];
if (var instanceof cbit.vcell.math.Constant) {
double value = ((cbit.vcell.math.Constant) var).getExpression().evaluateConstant();
for (int j = 0; j < data[i + 1].length; j++) {
data[i + 1][j] = value;
}
} else {
throw new RuntimeException("Did not find " + argSimSpec.getVarsList()[i] + " in simulation");
}
} else {
data[i + 1] = odeSolverResultSet.extractColumn(column);
}
}
double endTime = (simJob.getSimulation().getSolverTaskDescription().getTimeBounds().getEndingTime());
// for each time, print row
int index = 0;
double[] sampleTimes = new double[numTimeSteps + 1];
for (int i = 0; i <= numTimeSteps; i++) {
sampleTimes[i] = endTime * i / numTimeSteps;
}
for (int i = 0; i < sampleTimes.length; i++) {
// find largest index whose time is not past sample time
while (true) {
// if already at last time point, then if it equals the sampleTime, we're done if it doesn't then we don't have this time point.
if (index == odeSolverResultSet.getRowCount() - 1) {
if (data[0][index] == sampleTimes[i]) {
break;
} else {
throw new RuntimeException("sampleTime does not match at last time point");
}
}
// haven't gotten to last time point yet, stop when next time step is past sampleTime.
if (data[0][index + 1] > sampleTimes[i]) {
break;
}
// sampleTime must be later in our data list.
index++;
}
// if (data[0][index] == sampleTimes[i]) {
if (Math.abs(data[0][index] - sampleTimes[i]) < 1e-12) {
// if timeFactor is not 1.0, time is not in seconds (mins or hrs); if timeFactor is 60, divide sampleTime/60; if it is 3600, divide sampleTime/3600.
// if (argTimeFactor != 1.0) {
// outputStream.print(data[0][index]/argTimeFactor);
// } else {
outputStream.print(data[0][index]);
// }
for (int j = 0; j < argSimSpec.getVarsList().length; j++) {
// SBMLImporter.SBVCConcentrationUnits spConcUnits = (SBMLImporter.SBVCConcentrationUnits)argSpUnitsHash.get(argSimSpec.getVarsList()[j]);
// if (spConcUnits != null) {
// VCUnitDefinition sbunits = spConcUnits.getSBConcentrationUnits();
// VCUnitDefinition vcunits = spConcUnits.getVCConcentrationUnits();
// SBMLUnitParameter unitFactor = SBMLUtils.getConcUnitFactor("spConcParam", vcunits, sbunits, kMoleSymbol);
// outputStream.print("," + data[j + 1][index] * unitFactor.getExpression().evaluateConstant()); //earlier, hack unitfactor = 0.000001
// earlier, hack unitfactor = 0.000001
outputStream.print("," + data[j + 1][index]);
}
// }
outputStream.println();
} else {
// if data[0][index] < sampleTime, must interpolate
double fraction = (sampleTimes[i] - data[0][index]) / (data[0][index + 1] - data[0][index]);
// if argTimeFactor is not 1.0, time is not in seconds (mins or hrs); if argTimeFactor is 60, divide sampleTime/60; if it is 3600, divide sampleTime/3600.
// if (argTimeFactor != 1.0) {
// outputStream.print(sampleTimes[i]/argTimeFactor);
// } else {
outputStream.print(sampleTimes[i]);
// }
for (int j = 0; j < argSimSpec.getVarsList().length; j++) {
double interpolatedValue = 0.0;
double[] speciesVals = null;
double[] times = null;
// Currently using 2nd order interpolation
if (index == 0) {
// can only do 1st order interpolation
times = new double[] { data[0][index], data[0][index + 1] };
speciesVals = new double[] { data[j + 1][index], data[j + 1][index + 1] };
interpolatedValue = MathTestingUtilities.taylorInterpolation(sampleTimes[i], times, speciesVals);
} else {
if (index >= 1 && index <= origDataLength - 3) {
double val_1 = Math.abs(sampleTimes[i] - data[0][index - 1]);
double val_2 = Math.abs(sampleTimes[i] - data[0][index + 2]);
if (val_1 < val_2) {
times = new double[] { data[0][index - 1], data[0][index], data[0][index + 1] };
speciesVals = new double[] { data[j + 1][index - 1], data[j + 1][index], data[j + 1][index + 1] };
} else {
times = new double[] { data[0][index], data[0][index + 1], data[0][index + 2] };
speciesVals = new double[] { data[j + 1][index], data[j + 1][index + 1], data[j + 1][index + 2] };
}
interpolatedValue = MathTestingUtilities.taylorInterpolation(sampleTimes[i], times, speciesVals);
} else {
times = new double[] { data[0][index - 1], data[0][index], data[0][index + 1] };
speciesVals = new double[] { data[j + 1][index - 1], data[j + 1][index], data[j + 1][index + 1] };
interpolatedValue = MathTestingUtilities.taylorInterpolation(sampleTimes[i], times, speciesVals);
}
}
// end if-else - calc of interpolated value
// interpolatedValue = interpolatedValue * unitFactor.getExpression().evaluateConstant();
outputStream.print("," + interpolatedValue);
}
// end for - sp values for each time point (row)
outputStream.println();
}
}
outputStream.close();
} else {
throw new RuntimeException("Result set was null, could not write to CSV file");
}
} catch (Exception e) {
e.printStackTrace(System.out);
throw new RuntimeException("Errors saving results to CSV file" + e.getMessage());
}
}
use of cbit.vcell.messaging.server.SimulationTask in project vcell by virtualcell.
the class SimulationControllerImpl method onSimJobQueue_SimulationTask.
private void onSimJobQueue_SimulationTask(VCMessage vcMessage) {
SimulationTask simTask = null;
try {
SimulationTaskMessage simTaskMessage = new SimulationTaskMessage(vcMessage);
simTask = simTaskMessage.getSimulationTask();
LocalSolverController solverController = getOrCreateSolverController(simTask);
// can only start after updating the database is done
solverController.startSimulationJob();
} catch (Exception e) {
lg.error(e.getMessage(), e);
KeyValue simKey = simTask.getSimKey();
VCSimulationIdentifier vcSimID = simTask.getSimulationJob().getVCDataIdentifier().getVcSimID();
int jobIndex = simTask.getSimulationJob().getJobIndex();
int taskID = simTask.getTaskID();
SimulationJobStatus newJobStatus = new SimulationJobStatus(VCellServerID.getSystemServerID(), vcSimID, jobIndex, null, SchedulerStatus.FAILED, taskID, SimulationMessage.jobFailed(e.getMessage()), null, null);
SimulationJobStatusEvent event = new SimulationJobStatusEvent(this, Simulation.createSimulationID(simKey), newJobStatus, null, null, vcSimID.getOwner().getName());
fireSimulationJobStatusEvent(event);
}
}
use of cbit.vcell.messaging.server.SimulationTask in project vcell by virtualcell.
the class HtcSimulationWorker method initQueueConsumer.
private void initQueueConsumer() {
this.sharedMessageProducer = vcMessagingService.createProducerSession();
QueueListener queueListener = new QueueListener() {
@Override
public void onQueueMessage(VCMessage vcMessage, VCMessageSession session) throws RollbackException {
SimulationTask simTask = null;
try {
SimulationTaskMessage simTaskMessage = new SimulationTaskMessage(vcMessage);
simTask = simTaskMessage.getSimulationTask();
if (lg.isInfoEnabled()) {
lg.info("onQueueMessage() run simulation key=" + simTask.getSimKey() + ", job=" + simTask.getSimulationJobID() + ", task=" + simTask.getTaskID() + " for user " + simTask.getUserName());
}
PostProcessingChores rd = choresFor(simTask);
HtcProxy clonedHtcProxy = htcProxy.cloneThreadsafe();
if (lg.isInfoEnabled()) {
lg.info("onQueueMessage() submit job: simulation key=" + simTask.getSimKey() + ", job=" + simTask.getSimulationJobID() + ", task=" + simTask.getTaskID() + " for user " + simTask.getUserName());
}
HtcJobID pbsId = submit2PBS(simTask, clonedHtcProxy, rd);
if (lg.isInfoEnabled()) {
lg.info("onQueueMessage() sending 'accepted' message for job: simulation key=" + simTask.getSimKey() + ", job=" + simTask.getSimulationJobID() + ", task=" + simTask.getTaskID() + " for user " + simTask.getUserName());
}
synchronized (sharedMessageProducer) {
WorkerEventMessage.sendAccepted(sharedMessageProducer, HtcSimulationWorker.class.getName(), simTask, ManageUtils.getHostName(), pbsId);
}
if (lg.isInfoEnabled()) {
lg.info("onQueueMessage() sent 'accepted' message for job: simulation key=" + simTask.getSimKey() + ", job=" + simTask.getSimulationJobID() + ", task=" + simTask.getTaskID() + " for user " + simTask.getUserName());
}
} catch (Exception e) {
lg.error(e.getMessage(), e);
if (simTask != null) {
try {
lg.error("failed to process simTask request: " + e.getMessage() + " for simulation key=" + simTask.getSimKey() + ", job=" + simTask.getSimulationJobID() + ", task=" + simTask.getTaskID() + " for user " + simTask.getUserName(), e);
synchronized (sharedMessageProducer) {
WorkerEventMessage.sendFailed(sharedMessageProducer, HtcSimulationWorker.class.getName(), simTask, ManageUtils.getHostName(), SimulationMessage.jobFailed(e.getMessage()));
}
lg.error("sent 'failed' message for simulation key=" + simTask.getSimKey() + ", job=" + simTask.getSimulationJobID() + ", task=" + simTask.getTaskID() + " for user " + simTask.getUserName(), e);
} catch (VCMessagingException e1) {
lg.error(e1.getMessage(), e);
}
} else {
lg.error("failed to process simTask request: " + e.getMessage(), e);
}
}
}
};
int numHtcworkerThreads = Integer.parseInt(PropertyLoader.getProperty(PropertyLoader.htcworkerThreadsProperty, "5"));
this.pooledQueueConsumer = new VCPooledQueueConsumer(queueListener, numHtcworkerThreads, sharedMessageProducer);
this.pooledQueueConsumer.initThreadPool();
VCellQueue queue = VCellQueue.SimJobQueue;
VCMessageSelector selector = vcMessagingService.createSelector(getJobSelector());
String threadName = "SimJob Queue Consumer";
queueConsumer = new VCQueueConsumer(queue, pooledQueueConsumer, selector, threadName, MessageConstants.PREFETCH_LIMIT_SIM_JOB_HTC);
vcMessagingService.addMessageConsumer(queueConsumer);
}
Aggregations