Search in sources :

Example 1 with SimulationInfo

use of org.vcell.sbmlsim.api.common.SimulationInfo in project vcell by virtualcell.

the class VCellSbmlSimCLI method process_timepoints.

private void process_timepoints() throws Exception {
    Gson gson = new Gson();
    SimulationInfo simInfo = null;
    if (simhandle != null) {
        simInfo = new SimulationInfo(simhandle);
    } else {
        throw new CliException("simhandle (simulation handle) not specified");
    }
    if (outputfile == null) {
        throw new CliException("output file not specified");
    }
    TimePoints timePoints = simService.getTimePoints(simInfo);
    String timepointsJSON = gson.toJson(timePoints);
    FileUtils.write(outputfile, timepointsJSON);
}
Also used : TimePoints(org.vcell.sbmlsim.api.common.TimePoints) Gson(com.google.gson.Gson) SimulationInfo(org.vcell.sbmlsim.api.common.SimulationInfo)

Example 2 with SimulationInfo

use of org.vcell.sbmlsim.api.common.SimulationInfo in project vcell by virtualcell.

the class VCellSbmlSimCLI method process_status.

private void process_status() throws Exception {
    Gson gson = new Gson();
    SimulationInfo simInfo = null;
    if (simhandle != null) {
        simInfo = new SimulationInfo(simhandle);
    } else {
        throw new CliException("simhandle (simulation handle) not specified");
    }
    if (outputfile == null) {
        throw new CliException("output file not specified");
    }
    SimulationStatus simstatus = simService.getStatus(simInfo);
    String simstatusJSON = gson.toJson(simstatus);
    FileUtils.write(outputfile, simstatusJSON);
}
Also used : SimulationStatus(org.vcell.sbmlsim.api.common.SimulationStatus) Gson(com.google.gson.Gson) SimulationInfo(org.vcell.sbmlsim.api.common.SimulationInfo)

Example 3 with SimulationInfo

use of org.vcell.sbmlsim.api.common.SimulationInfo in project vcell by virtualcell.

the class VCellSbmlSimCLI method process_submit.

private void process_submit() throws Exception {
    Gson gson = new Gson();
    SimulationSpec simspec = null;
    if (simspecJSON != null) {
        try {
            simspec = gson.fromJson(simspecJSON, SimulationSpec.class);
        } catch (Exception e) {
            throw new CliException("failed to parse JSON text for simspec (Simulation Specification): " + e.getMessage(), e);
        }
    } else {
        throw new CliException("simhandle (simulation handle option) not specified");
    }
    SBMLModel model = null;
    if (sbmlfile != null) {
        model = new SBMLModel(sbmlfile);
    } else {
        throw new CliException("sbmlfile (SBML file option) not specified");
    }
    if (outputfile == null) {
        throw new CliException("output file not specified");
    }
    SimulationInfo siminfo = simService.computeModel(model, simspec);
    String siminfoJSON = gson.toJson(siminfo);
    FileUtils.write(outputfile, siminfoJSON);
}
Also used : SBMLModel(org.vcell.sbmlsim.api.common.SBMLModel) Gson(com.google.gson.Gson) SimulationSpec(org.vcell.sbmlsim.api.common.SimulationSpec) SimulationInfo(org.vcell.sbmlsim.api.common.SimulationInfo)

Example 4 with SimulationInfo

use of org.vcell.sbmlsim.api.common.SimulationInfo 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();
// }
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) SimulationTask(cbit.vcell.messaging.server.SimulationTask) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) ISize(org.vcell.util.ISize) TempSimulation(cbit.vcell.solver.TempSimulation) VCImage(cbit.image.VCImage) SimulationContext(cbit.vcell.mapping.SimulationContext) SolverEvent(cbit.vcell.solver.server.SolverEvent) GeometrySpec(cbit.vcell.geometry.GeometrySpec) TimeBounds(cbit.vcell.solver.TimeBounds) Simulation(cbit.vcell.solver.Simulation) TempSimulation(cbit.vcell.solver.TempSimulation) Random(java.util.Random) BioModel(cbit.vcell.biomodel.BioModel) SolverListener(cbit.vcell.solver.server.SolverListener) File(java.io.File) SimulationJob(cbit.vcell.solver.SimulationJob) DataAccessException(org.vcell.util.DataAccessException) SimulationInfo(org.vcell.sbmlsim.api.common.SimulationInfo)

Example 5 with SimulationInfo

use of org.vcell.sbmlsim.api.common.SimulationInfo in project vcell by virtualcell.

the class SimulationServiceImplTest method test.

@Test
public void test() throws URISyntaxException, Exception {
    SimulationServiceImpl simService = new SimulationServiceImpl();
    // TODO - Eliminate code duplication.
    URL sbmlFileUrl = SimulationServiceImplTest.class.getResource("../optoPlexin_PRG_rule_based.xml");
    File file = new File(sbmlFileUrl.toURI());
    VCMongoMessage.enabled = false;
    // TODO - Encapsulate this common setup stuff into VCellService initialization.
    // Then all VCell-based scripts can focus on the customization below.
    System.setProperty(PropertyLoader.installationRoot, new File("..").getAbsolutePath());
    ResourceUtil.setNativeLibraryDirectory();
    NativeLib.HDF5.load();
    file = new File("src/test/resources/org/vcell/sbmlsim/optoPlexin_PRG_rule_based.xml");
    Assert.assertTrue(file.exists());
    SBMLModel sbmlModel = new SBMLModel(file);
    SimulationSpec simSpec = new SimulationSpec();
    SimulationInfo simInfo = simService.computeModel(sbmlModel, simSpec);
    long timeMS = System.currentTimeMillis();
    while (simService.getStatus(simInfo).getSimState() != SimulationState.done && simService.getStatus(simInfo).getSimState() != SimulationState.failed) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        long time2MS = System.currentTimeMillis();
        if (time2MS - timeMS > 10000) {
            fail("timed out after 10 seconds");
        }
    }
    List<VariableInfo> vars = simService.getVariableList(simInfo);
    Assert.assertNotNull(vars);
    // TODO - Assert more things.
    final TimePoints timePoints = simService.getTimePoints(simInfo);
    vars.stream().forEach(var -> {
        try {
            System.out.println(// 
            var.getVariableDisplayName() + "[0] = " + simService.getData(simInfo, var, 0));
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    });
}
Also used : TimePoints(org.vcell.sbmlsim.api.common.TimePoints) SBMLModel(org.vcell.sbmlsim.api.common.SBMLModel) VariableInfo(org.vcell.sbmlsim.api.common.VariableInfo) File(java.io.File) SimulationSpec(org.vcell.sbmlsim.api.common.SimulationSpec) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) SimulationInfo(org.vcell.sbmlsim.api.common.SimulationInfo) Test(org.junit.Test)

Aggregations

SimulationInfo (org.vcell.sbmlsim.api.common.SimulationInfo)6 Gson (com.google.gson.Gson)4 File (java.io.File)2 SBMLModel (org.vcell.sbmlsim.api.common.SBMLModel)2 SimulationSpec (org.vcell.sbmlsim.api.common.SimulationSpec)2 TimePoints (org.vcell.sbmlsim.api.common.TimePoints)2 VariableInfo (org.vcell.sbmlsim.api.common.VariableInfo)2 VCImage (cbit.image.VCImage)1 BioModel (cbit.vcell.biomodel.BioModel)1 GeometrySpec (cbit.vcell.geometry.GeometrySpec)1 MathMappingCallbackTaskAdapter (cbit.vcell.mapping.MathMappingCallbackTaskAdapter)1 SimulationContext (cbit.vcell.mapping.SimulationContext)1 MathMappingCallback (cbit.vcell.mapping.SimulationContext.MathMappingCallback)1 MathModel (cbit.vcell.mathmodel.MathModel)1 SimulationTask (cbit.vcell.messaging.server.SimulationTask)1 Simulation (cbit.vcell.solver.Simulation)1 SimulationJob (cbit.vcell.solver.SimulationJob)1 TempSimulation (cbit.vcell.solver.TempSimulation)1 TimeBounds (cbit.vcell.solver.TimeBounds)1 UniformOutputTimeSpec (cbit.vcell.solver.UniformOutputTimeSpec)1