Search in sources :

Example 1 with SolverEvent

use of cbit.vcell.solver.server.SolverEvent 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);
    }
}
Also used : MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) SimulationTask(cbit.vcell.messaging.server.SimulationTask) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) TempSimulation(cbit.vcell.solver.TempSimulation) SimulationContext(cbit.vcell.mapping.SimulationContext) XMLStreamException(javax.xml.stream.XMLStreamException) ThriftDataAccessException(org.vcell.vcellij.api.ThriftDataAccessException) SbmlException(org.vcell.sbml.SbmlException) SBMLException(org.sbml.jsbml.SBMLException) XmlParseException(cbit.vcell.xml.XmlParseException) SolverException(cbit.vcell.solver.SolverException) TException(org.apache.thrift.TException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) SolverEvent(cbit.vcell.solver.server.SolverEvent) Simulation(cbit.vcell.solver.Simulation) TempSimulation(cbit.vcell.solver.TempSimulation) Random(java.util.Random) SolverListener(cbit.vcell.solver.server.SolverListener) File(java.io.File) SimulationJob(cbit.vcell.solver.SimulationJob) ThriftDataAccessException(org.vcell.vcellij.api.ThriftDataAccessException) DataAccessException(org.vcell.util.DataAccessException) SimulationInfo(org.vcell.vcellij.api.SimulationInfo)

Example 2 with SolverEvent

use of cbit.vcell.solver.server.SolverEvent 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);
        }
    }
}
Also used : SolverListener(cbit.vcell.solver.server.SolverListener) SolverEvent(cbit.vcell.solver.server.SolverEvent)

Example 3 with SolverEvent

use of cbit.vcell.solver.server.SolverEvent 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);
        }
    }
}
Also used : SolverListener(cbit.vcell.solver.server.SolverListener) SolverEvent(cbit.vcell.solver.server.SolverEvent)

Example 4 with SolverEvent

use of cbit.vcell.solver.server.SolverEvent 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);
    }
}
Also used : SimulationTask(cbit.vcell.messaging.server.SimulationTask) VCMessagingException(cbit.vcell.message.VCMessagingException) FileNotFoundException(java.io.FileNotFoundException) ObjectName(javax.management.ObjectName) SolverEvent(cbit.vcell.solver.server.SolverEvent) VCellServiceMXBeanImpl(cbit.vcell.message.server.jmx.VCellServiceMXBeanImpl) SimulationMessage(cbit.vcell.solver.server.SimulationMessage) SolverListener(cbit.vcell.solver.server.SolverListener) VCMessagingException(cbit.vcell.message.VCMessagingException) File(java.io.File) HTCSolver(cbit.vcell.solvers.HTCSolver) MBeanServer(javax.management.MBeanServer)

Example 5 with SolverEvent

use of cbit.vcell.solver.server.SolverEvent in project vcell by virtualcell.

the class ClientSimManager method runQuickSimulation.

public void runQuickSimulation(final Simulation originalSimulation, ViewerType viewerType) {
    Collection<AsynchClientTask> taskList = new ArrayList<AsynchClientTask>();
    final SimulationOwner simulationOwner = simWorkspace.getSimulationOwner();
    // ----------- update math if it is from biomodel (simulationContext)
    if (simulationOwner instanceof SimulationContext) {
        Collection<AsynchClientTask> ut = ClientRequestManager.updateMath(documentWindowManager.getComponent(), ((SimulationContext) simulationOwner), false, NetworkGenerationRequirements.ComputeFullStandardTimeout);
        taskList.addAll(ut);
    }
    // ----------- run simulation(s)
    final File localSimDataDir = ResourceUtil.getLocalSimDir(User.tempUser.getName());
    AsynchClientTask runSimTask = new AsynchClientTask("running simulation", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            Simulation simulation = new TempSimulation(originalSimulation, false);
            SimulationTask simTask = new SimulationTask(new SimulationJob(simulation, 0, null), 0);
            Solver solver = createQuickRunSolver(localSimDataDir, simTask);
            if (solver == null) {
                throw new RuntimeException("null solver");
            }
            // check if spatial stochastic simulation (smoldyn solver) has data processing instructions with field data - need to access server for field data, so cannot do local simulation run.
            if (solver instanceof SmoldynSolver) {
                DataProcessingInstructions dpi = simulation.getDataProcessingInstructions();
                if (dpi != null) {
                    FieldDataIdentifierSpec fdis = dpi.getSampleImageFieldData(simulation.getVersion().getOwner());
                    if (fdis != null) {
                        throw new RuntimeException("Spatial Stochastic simulation '" + simulation.getName() + "' (Smoldyn solver) with field data (in data processing instructions) cannot be run locally at this time since field data needs to be retrieved from the VCell server.");
                    }
                }
            }
            solver.addSolverListener(new SolverListener() {

                public void solverStopped(SolverEvent event) {
                    getClientTaskStatusSupport().setMessage(event.getSimulationMessage().getDisplayMessage());
                }

                public void solverStarting(SolverEvent event) {
                    String displayMessage = event.getSimulationMessage().getDisplayMessage();
                    System.out.println(displayMessage);
                    getClientTaskStatusSupport().setMessage(displayMessage);
                    if (displayMessage.equals(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING_INIT.getDisplayMessage())) {
                        getClientTaskStatusSupport().setProgress(75);
                    } else if (displayMessage.equals(SimulationMessage.MESSAGE_SOLVER_RUNNING_INPUT_FILE.getDisplayMessage())) {
                        getClientTaskStatusSupport().setProgress(90);
                    }
                }

                public void solverProgress(SolverEvent event) {
                    getClientTaskStatusSupport().setMessage("Running...");
                    int progress = (int) (event.getProgress() * 100);
                    getClientTaskStatusSupport().setProgress(progress);
                }

                public void solverPrinted(SolverEvent event) {
                    getClientTaskStatusSupport().setMessage("Running...");
                }

                public void solverFinished(SolverEvent event) {
                    getClientTaskStatusSupport().setMessage(event.getSimulationMessage().getDisplayMessage());
                }

                public void solverAborted(SolverEvent event) {
                    getClientTaskStatusSupport().setMessage(event.getSimulationMessage().getDisplayMessage());
                }
            });
            solver.startSolver();
            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                }
                if (getClientTaskStatusSupport().isInterrupted()) {
                    solver.stopSolver();
                    throw UserCancelException.CANCEL_GENERIC;
                }
                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;
                    }
                }
            }
            ArrayList<AnnotatedFunction> outputFunctionsList = getSimWorkspace().getSimulationOwner().getOutputFunctionContext().getOutputFunctionsList();
            OutputContext outputContext = new OutputContext(outputFunctionsList.toArray(new AnnotatedFunction[outputFunctionsList.size()]));
            Simulation[] simsArray = new Simulation[] { simulation };
            hashTable.put("outputContext", outputContext);
            hashTable.put("simsArray", simsArray);
        }
    };
    taskList.add(runSimTask);
    // --------- add tasks from showSimResults : retrieve data, display results
    AsynchClientTask[] showResultsTask = showSimulationResults0(true, viewerType);
    for (AsynchClientTask task : showResultsTask) {
        taskList.add(task);
    }
    // ------- dispatch
    AsynchClientTask[] taskArray = new AsynchClientTask[taskList.size()];
    taskList.toArray(taskArray);
    ClientTaskDispatcher.dispatch(documentWindowManager.getComponent(), new Hashtable<String, Object>(), taskArray, true, true, null);
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) SmoldynSolver(org.vcell.solver.smoldyn.SmoldynSolver) Solver(cbit.vcell.solver.server.Solver) SimulationTask(cbit.vcell.messaging.server.SimulationTask) ArrayList(java.util.ArrayList) SimulationOwner(cbit.vcell.solver.SimulationOwner) DataProcessingInstructions(cbit.vcell.solver.DataProcessingInstructions) SolverListener(cbit.vcell.solver.server.SolverListener) SolverStatus(cbit.vcell.solver.server.SolverStatus) SimulationJob(cbit.vcell.solver.SimulationJob) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) Hashtable(java.util.Hashtable) TempSimulation(cbit.vcell.solver.TempSimulation) SimulationContext(cbit.vcell.mapping.SimulationContext) OutputContext(cbit.vcell.simdata.OutputContext) SolverEvent(cbit.vcell.solver.server.SolverEvent) Simulation(cbit.vcell.solver.Simulation) TempSimulation(cbit.vcell.solver.TempSimulation) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) SmoldynSolver(org.vcell.solver.smoldyn.SmoldynSolver) EventObject(java.util.EventObject) File(java.io.File)

Aggregations

SolverEvent (cbit.vcell.solver.server.SolverEvent)10 SolverListener (cbit.vcell.solver.server.SolverListener)10 File (java.io.File)4 SimulationTask (cbit.vcell.messaging.server.SimulationTask)3 SimulationContext (cbit.vcell.mapping.SimulationContext)2 Simulation (cbit.vcell.solver.Simulation)2 SimulationJob (cbit.vcell.solver.SimulationJob)2 TempSimulation (cbit.vcell.solver.TempSimulation)2 SolverStatus (cbit.vcell.solver.server.SolverStatus)2 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)1 FieldDataIdentifierSpec (cbit.vcell.field.FieldDataIdentifierSpec)1 MathMappingCallbackTaskAdapter (cbit.vcell.mapping.MathMappingCallbackTaskAdapter)1 MathMappingCallback (cbit.vcell.mapping.SimulationContext.MathMappingCallback)1 VCMessagingException (cbit.vcell.message.VCMessagingException)1 VCellServiceMXBeanImpl (cbit.vcell.message.server.jmx.VCellServiceMXBeanImpl)1 OutputContext (cbit.vcell.simdata.OutputContext)1 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)1 DataProcessingInstructions (cbit.vcell.solver.DataProcessingInstructions)1 SimulationOwner (cbit.vcell.solver.SimulationOwner)1 SolverException (cbit.vcell.solver.SolverException)1