Search in sources :

Example 1 with SmoldynSolver

use of org.vcell.solver.smoldyn.SmoldynSolver 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);
    }
    // Let user tell how many simultaneous processes to run if this is local paramscan
    final int[] simultaneousSimsSetting = new int[] { 1 };
    if (originalSimulation.getScanCount() > 1) {
        try {
            String simultaneousSims = DialogUtils.showInputDialog0(getDocumentWindowManager().getComponent(), "Local multi-scan simulation, enter maximum simulataneous sims to run at once", "1");
            simultaneousSimsSetting[0] = Integer.parseInt(simultaneousSims);
        } catch (UtilCancelException e) {
            return;
        }
    }
    // ----------- 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);
            simulation.setSimulationOwner(originalSimulation.getSimulationOwner());
            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) {
                        String simulationMessage = solverStatus.getSimulationMessage().getDisplayMessage();
                        String translatedMessage = solver.translateSimulationMessage(simulationMessage);
                        if (translatedMessage.startsWith(BeanUtils.FD_EXP_MESSG)) {
                            throw new RuntimeException("Sims with FieldData can only be run remotely (cannot use QuickRun).\n" + translatedMessage);
                        } else {
                            throw new RuntimeException(translatedMessage);
                        }
                    }
                    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);
    }
    AsynchClientTask runOthers = new AsynchClientTask("running scans", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            if (getClientTaskStatusSupport().isInterrupted()) {
                throw UserCancelException.CANCEL_GENERIC;
            }
            Simulation[] sims = (Simulation[]) hashTable.get("simsArray");
            Simulation simulation = sims[0];
            // Run param scans to generate data for scansnum > 0
            if (simulation.getScanCount() > 1) {
                // Start master thread so clientdispatcher modal dialog can end
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // Flag (set by dataviewer being closed) so sim scan threads know if they should stop
                        final boolean[] bWinCloseHolder = new boolean[] { false };
                        // Add close listener to dataviewer to end all scans and exit
                        final SimulationWindow haveSimulationWindow = getDocumentWindowManager().haveSimulationWindow(simulation.getSimulationInfo().getAuthoritativeVCSimulationIdentifier());
                        final Window window = (Window) BeanUtils.findTypeParentOfComponent(haveSimulationWindow.getDataViewer(), Window.class);
                        window.addWindowListener(new WindowAdapter() {

                            @Override
                            public void windowClosing(WindowEvent e) {
                                // TODO Auto-generated method stub
                                super.windowClosing(e);
                                bWinCloseHolder[0] = true;
                            }
                        });
                        // First sim scan (0) is done already before viewer is shown
                        ((SimResultsViewer) haveSimulationWindow.getDataViewer()).setLocalScanProgress(1);
                        // Counter of how many simultaneous param scan threads are running (decremented when scan thread finishes)
                        final int[] currentlyRunningCountHolder = new int[] { 0 };
                        // Run other scans starting at 1 (scan 0 is already done if we got here)
                        for (int i = 1; i < simulation.getScanCount(); i++) {
                            // Check if we can start another new param scan thread
                            while (currentlyRunningCountHolder[0] >= simultaneousSimsSetting[0]) {
                                try {
                                    Thread.sleep(50);
                                    // Check if user closed the viewer window, no need to continue
                                    if (bWinCloseHolder[0]) {
                                        return;
                                    }
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            final int scanNum = i;
                            // increment the concurrent running counter
                            currentlyRunningCountHolder[0] += 1;
                            // Start new param scan thread
                            new Thread(new Runnable() {

                                @Override
                                public void run() {
                                    try {
                                        SimulationTask simTask = new SimulationTask(new SimulationJob(simulation, scanNum, null), 0);
                                        Solver solver = createQuickRunSolver(localSimDataDir, simTask);
                                        solver.startSolver();
                                        while (true) {
                                            SolverStatus solverStatus = solver.getSolverStatus();
                                            // System.out.println("ScanNum="+scanNum+" "+solverStatus);
                                            try {
                                                Thread.sleep(100);
                                            } catch (InterruptedException e) {
                                            }
                                            // Stop if user closed the dataviewer window
                                            if (bWinCloseHolder[0]) {
                                                solver.stopSolver();
                                                break;
                                            }
                                            if (solverStatus != null) {
                                                if (solverStatus.getStatus() == SolverStatus.SOLVER_ABORTED) {
                                                    String simulationMessage = solverStatus.getSimulationMessage().getDisplayMessage();
                                                    String translatedMessage = solver.translateSimulationMessage(simulationMessage);
                                                    if (translatedMessage.startsWith(BeanUtils.FD_EXP_MESSG)) {
                                                        throw new RuntimeException("Sims with FieldData can only be run remotely (cannot use QuickRun).\n" + translatedMessage);
                                                    } else {
                                                        throw new RuntimeException(translatedMessage);
                                                    }
                                                }
                                                if (solverStatus.getStatus() != SolverStatus.SOLVER_STARTING && solverStatus.getStatus() != SolverStatus.SOLVER_READY && solverStatus.getStatus() != SolverStatus.SOLVER_RUNNING) {
                                                    break;
                                                }
                                            }
                                        }
                                    // SolverStatus solverStatus = solver.getSolverStatus();
                                    // System.out.println("ScanNum="+scanNum+" "+"FinalStatus="+solverStatus);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    } finally {
                                        // decrement the concurrent running counter
                                        currentlyRunningCountHolder[0] -= 1;
                                        // Set progress on dataviewer
                                        if (((SimResultsViewer) haveSimulationWindow.getDataViewer()).getLocalScanProgress() < (scanNum + 1)) {
                                            ((SimResultsViewer) haveSimulationWindow.getDataViewer()).setLocalScanProgress(scanNum + 1);
                                        }
                                    }
                                }
                            }).start();
                        }
                    }
                }).start();
            }
        }
    };
    taskList.add(runOthers);
    // ------- 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) WindowAdapter(java.awt.event.WindowAdapter) 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) Window(java.awt.Window) SimulationWindow(cbit.vcell.client.desktop.simulation.SimulationWindow) ChildWindow(cbit.vcell.client.ChildWindowManager.ChildWindow) UtilCancelException(org.vcell.util.UtilCancelException) Hashtable(java.util.Hashtable) TempSimulation(cbit.vcell.solver.TempSimulation) SimulationContext(cbit.vcell.mapping.SimulationContext) OutputContext(cbit.vcell.simdata.OutputContext) SimResultsViewer(cbit.vcell.client.data.SimResultsViewer) RemoteProxyException(cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) UtilCancelException(org.vcell.util.UtilCancelException) FileNotFoundException(java.io.FileNotFoundException) SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException) UserCancelException(org.vcell.util.UserCancelException) SolverEvent(cbit.vcell.solver.server.SolverEvent) TempSimulation(cbit.vcell.solver.TempSimulation) Simulation(cbit.vcell.solver.Simulation) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) WindowEvent(java.awt.event.WindowEvent) SmoldynSolver(org.vcell.solver.smoldyn.SmoldynSolver) SimulationWindow(cbit.vcell.client.desktop.simulation.SimulationWindow) EventObject(java.util.EventObject) File(java.io.File)

Aggregations

ChildWindow (cbit.vcell.client.ChildWindowManager.ChildWindow)1 SimResultsViewer (cbit.vcell.client.data.SimResultsViewer)1 SimulationWindow (cbit.vcell.client.desktop.simulation.SimulationWindow)1 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)1 FieldDataIdentifierSpec (cbit.vcell.field.FieldDataIdentifierSpec)1 SimulationContext (cbit.vcell.mapping.SimulationContext)1 RemoteProxyException (cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException)1 SimulationTask (cbit.vcell.messaging.server.SimulationTask)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 OutputContext (cbit.vcell.simdata.OutputContext)1 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)1 DataProcessingInstructions (cbit.vcell.solver.DataProcessingInstructions)1 Simulation (cbit.vcell.solver.Simulation)1 SimulationJob (cbit.vcell.solver.SimulationJob)1 SimulationOwner (cbit.vcell.solver.SimulationOwner)1 SolverException (cbit.vcell.solver.SolverException)1 TempSimulation (cbit.vcell.solver.TempSimulation)1 Solver (cbit.vcell.solver.server.Solver)1 SolverEvent (cbit.vcell.solver.server.SolverEvent)1 SolverListener (cbit.vcell.solver.server.SolverListener)1