Search in sources :

Example 1 with SimulationInfo

use of org.vcell.vcellij.api.SimulationInfo 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 SimulationInfo

use of org.vcell.vcellij.api.SimulationInfo in project vcell by virtualcell.

the class SimpleClient method perform.

private static void perform(SimulationService.Client client) throws TException {
    SBMLModel model = new SBMLModel();
    model.setFilepath("filepath");
    SimulationSpec simSpec = new SimulationSpec();
    SimulationInfo simulationInfo = client.computeModel(model, simSpec);
}
Also used : SBMLModel(org.vcell.vcellij.api.SBMLModel) SimulationSpec(org.vcell.vcellij.api.SimulationSpec) SimulationInfo(org.vcell.vcellij.api.SimulationInfo)

Example 3 with SimulationInfo

use of org.vcell.vcellij.api.SimulationInfo in project vcell by virtualcell.

the class VCellService method runSimulation.

private static Task<List<Dataset>, SimulationState> runSimulation(final SimulationServiceImpl client, final VCellModel vCellModel, final SimulationSpec simSpec, final List<Species> outputSpecies, final boolean shouldCreateIndividualDatasets, final OpService opService, final DatasetService datasetService) throws IOException, XMLStreamException {
    final Task<List<Dataset>, SimulationState> task = new Task<List<Dataset>, SimulationState>() {

        @Override
        protected List<Dataset> doInBackground() throws Exception {
            setSubtask(SimulationState.notRun);
            final File sbmlSpatialFile = new File(vCellModel.getName() + ".xml");
            new SBMLWriter().write(vCellModel.getSbmlDocument(), sbmlSpatialFile);
            final SBMLModel model = new SBMLModel();
            model.setFilepath(sbmlSpatialFile.getAbsolutePath());
            final SimulationInfo simulationInfo = client.computeModel(model, simSpec);
            try {
                Thread.sleep(500);
            } catch (final InterruptedException e) {
                e.printStackTrace();
            }
            setSubtask(SimulationState.running);
            while (client.getStatus(simulationInfo).getSimState() == SimulationState.running) {
                System.out.println("waiting for simulation results");
                try {
                    Thread.sleep(500);
                } catch (final InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (client.getStatus(simulationInfo).getSimState() == SimulationState.failed) {
                setSubtask(SimulationState.failed);
                return null;
            }
            final List<Dataset> results = new ArrayList<>();
            final List<VariableInfo> vars = client.getVariableList(simulationInfo);
            final List<Double> times = client.getTimePoints(simulationInfo);
            for (final VariableInfo var : vars) {
                if (outputSpecies.stream().anyMatch(species -> species.getId().equals(var.getVariableVtuName()))) {
                    // Get data for first time point and determine dimensions
                    List<Double> data = client.getData(simulationInfo, var, 0);
                    final int[] dimensions = getDimensions(data, times);
                    final Img<DoubleType> img = opService.create().img(dimensions);
                    final RandomAccess<DoubleType> imgRA = img.randomAccess();
                    // Copy data to the ImgLib2 Img
                    for (int t = 0; t < times.size(); t++) {
                        data = client.getData(simulationInfo, var, t);
                        for (int d = 0; d < data.size(); d++) {
                            imgRA.setPosition(new int[] { d, t });
                            imgRA.get().set(data.get(d));
                        }
                    }
                    // Create ImageJ Dataset and add to results
                    final Dataset dataset = datasetService.create(img);
                    dataset.setName(var.getVariableVtuName());
                    results.add(dataset);
                }
            }
            // If desired, add all datasets with the same dimensions
            if (!shouldCreateIndividualDatasets && !results.isEmpty()) {
                // First, group datasets according to dimensions
                final List<List<Dataset>> datasetGroups = new ArrayList<>();
                final List<Dataset> initialGroup = new ArrayList<>();
                initialGroup.add(results.get(0));
                datasetGroups.add(initialGroup);
                for (int i = 1; i < results.size(); i++) {
                    final Dataset result = results.get(i);
                    for (final List<Dataset> datasetGroup : datasetGroups) {
                        final Dataset[] datasets = new Dataset[] { datasetGroup.get(0), result };
                        if (Datasets.areSameSize(datasets, 0, 1)) {
                            datasetGroup.add(result);
                        } else {
                            final List<Dataset> newGroup = new ArrayList<>();
                            newGroup.add(result);
                            datasetGroups.add(newGroup);
                        }
                    }
                }
                final List<Dataset> summedResults = new ArrayList<>();
                for (final List<Dataset> datasetGroup : datasetGroups) {
                    final Img<DoubleType> sum = opService.create().img(datasetGroup.get(0));
                    for (final Dataset dataset : datasetGroup) {
                        @SuppressWarnings("unchecked") final RandomAccessibleInterval<DoubleType> current = (Img<DoubleType>) dataset.getImgPlus().getImg();
                        opService.math().add(sum, sum, current);
                    }
                    final Dataset result = datasetService.create(sum);
                    result.setName(datasetGroup.stream().map(d -> d.getName()).collect(Collectors.joining("+")));
                    summedResults.add(result);
                }
                return summedResults;
            }
            setSubtask(SimulationState.done);
            return results;
        }
    };
    return task;
}
Also used : Task(org.vcell.imagej.common.gui.Task) SBMLModel(org.vcell.vcellij.api.SBMLModel) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Img(net.imglib2.img.Img) Dataset(net.imagej.Dataset) VariableInfo(org.vcell.vcellij.api.VariableInfo) SBMLWriter(org.sbml.jsbml.SBMLWriter) DoubleType(net.imglib2.type.numeric.real.DoubleType) SimulationState(org.vcell.vcellij.api.SimulationState) File(java.io.File) SimulationInfo(org.vcell.vcellij.api.SimulationInfo)

Aggregations

SimulationInfo (org.vcell.vcellij.api.SimulationInfo)3 File (java.io.File)2 SBMLModel (org.vcell.vcellij.api.SBMLModel)2 MathMappingCallbackTaskAdapter (cbit.vcell.mapping.MathMappingCallbackTaskAdapter)1 SimulationContext (cbit.vcell.mapping.SimulationContext)1 MathMappingCallback (cbit.vcell.mapping.SimulationContext.MathMappingCallback)1 SimulationTask (cbit.vcell.messaging.server.SimulationTask)1 Simulation (cbit.vcell.solver.Simulation)1 SimulationJob (cbit.vcell.solver.SimulationJob)1 SolverException (cbit.vcell.solver.SolverException)1 TempSimulation (cbit.vcell.solver.TempSimulation)1 SolverEvent (cbit.vcell.solver.server.SolverEvent)1 SolverListener (cbit.vcell.solver.server.SolverListener)1 XmlParseException (cbit.vcell.xml.XmlParseException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Random (java.util.Random)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Dataset (net.imagej.Dataset)1