Search in sources :

Example 1 with AbstractJavaSolver

use of cbit.vcell.solver.ode.AbstractJavaSolver in project vcell by virtualcell.

the class SolverHandler method simulateAllTasks.

public HashMap<String, ODESolverResultSet> simulateAllTasks(CLIUtils utils, ExternalDocInfo externalDocInfo, SedML sedml, File outputDirForSedml, String outDir, String outputBaseDir, String sedmlLocation, boolean keepTempFiles, boolean exactMatchOnly) throws Exception {
    // create the VCDocument(s) (bioModel(s) + application(s) + simulation(s)), do sanity checks
    cbit.util.xml.VCLogger sedmlImportLogger = new LocalLogger();
    String inputFile = externalDocInfo.getFile().getAbsolutePath();
    String bioModelBaseName = org.vcell.util.FileUtils.getBaseName(inputFile);
    List<VCDocument> docs = null;
    // Key String is SEDML Task ID
    HashMap<String, ODESolverResultSet> resultsHash = new LinkedHashMap<String, ODESolverResultSet>();
    String docName = null;
    BioModel bioModel = null;
    Simulation[] sims = null;
    String outDirRoot = outputDirForSedml.toString().substring(0, outputDirForSedml.toString().lastIndexOf(System.getProperty("file.separator")));
    try {
        docs = XmlHelper.sedmlToBioModel(sedmlImportLogger, externalDocInfo, sedml, null, sedmlLocation, exactMatchOnly);
    } catch (Exception e) {
        System.err.println("Unable to Parse SED-ML into Bio-Model, failed with err: " + e.getMessage());
        throw e;
    }
    if (docs != null) {
        countBioModels = docs.size();
    }
    int simulationCount = 0;
    int bioModelCount = 0;
    boolean hasSomeSpatial = false;
    boolean bTimeoutFound = false;
    for (VCDocument doc : docs) {
        try {
            sanityCheck(doc);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        // continue;
        }
        docName = doc.getName();
        bioModel = (BioModel) doc;
        sims = bioModel.getSimulations();
        for (Simulation sim : sims) {
            if (sim.getImportedTaskID() == null) {
                // this is a simulation not matching the imported task, so we skip it
                continue;
            }
            String logTaskMessage = "Initializing simulation... ";
            String logTaskError = "";
            long startTimeTask = System.currentTimeMillis();
            SimulationTask simTask;
            String kisao = "null";
            ODESolverResultSet odeSolverResultSet = null;
            SolverTaskDescription std = null;
            SolverDescription sd = null;
            try {
                SimulationOwner so = sim.getSimulationOwner();
                sim = new TempSimulation(sim, false);
                sim.setSimulationOwner(so);
                std = sim.getSolverTaskDescription();
                sd = std.getSolverDescription();
                kisao = sd.getKisao();
                if (kisao == null) {
                    throw new RuntimeException("KISAO is null.");
                }
                SimulationJob simJob = new SimulationJob(sim, 0, null);
                simTask = new SimulationTask(simJob, 0);
                Solver solver = SolverFactory.createSolver(outputDirForSedml, simTask, false);
                logTaskMessage += "done. Starting simulation... ";
                if (sd.isSpatial()) {
                    hasSomeSpatial = true;
                }
                // else
                if (solver instanceof AbstractCompiledSolver) {
                    ((AbstractCompiledSolver) solver).runSolver();
                    System.out.println(solver);
                    System.out.println(solver.getSolverStatus());
                    if (solver instanceof ODESolver) {
                        odeSolverResultSet = ((ODESolver) solver).getODESolverResultSet();
                    } else if (solver instanceof GibsonSolver) {
                        odeSolverResultSet = ((GibsonSolver) solver).getStochSolverResultSet();
                    } else if (solver instanceof HybridSolver) {
                        odeSolverResultSet = ((HybridSolver) solver).getHybridSolverResultSet();
                    } else {
                        String str = "Solver results are not compatible with CSV format. ";
                        System.err.println(str);
                    // keepTempFiles = true;		// temp fix for Jasraj
                    // throw new RuntimeException(str);
                    }
                } else if (solver instanceof AbstractJavaSolver) {
                    ((AbstractJavaSolver) solver).runSolver();
                    odeSolverResultSet = ((ODESolver) solver).getODESolverResultSet();
                    // must interpolate data for uniform time course which is not supported natively by the Java solvers
                    Task task = (Task) sedml.getTaskWithId(sim.getImportedTaskID());
                    assert task != null;
                    org.jlibsedml.Simulation sedmlSim = sedml.getSimulation(task.getSimulationReference());
                    if (sedmlSim instanceof UniformTimeCourse) {
                        odeSolverResultSet = CLIUtils.interpolate(odeSolverResultSet, (UniformTimeCourse) sedmlSim);
                        logTaskMessage += "done. Interpolating... ";
                    }
                } else {
                    // this should actually never happen...
                    String str = "Unexpected solver: " + kisao + " " + solver + ". ";
                    throw new RuntimeException(str);
                }
                if (solver.getSolverStatus().getStatus() == SolverStatus.SOLVER_FINISHED) {
                    // File aaa = new File("C:\\TEMP\\aaa.hdf5");
                    // CLIUtils.exportPDE2HDF5(sim, outputDirForSedml, aaa);
                    logTaskMessage += "done. ";
                    System.out.println("Succesful execution: Model '" + docName + "' Task '" + sim.getDescription() + "'.");
                    long endTimeTask = System.currentTimeMillis();
                    long elapsedTime = endTimeTask - startTimeTask;
                    int duration = (int) Math.ceil(elapsedTime / 1000.0);
                    String msg = "Running simulation " + simTask.getSimulation().getName() + ", " + elapsedTime + " ms";
                    System.out.println(msg);
                    // we only count the number of simulations (tasks) that succeeded
                    countSuccessfulSimulationRuns++;
                    utils.updateTaskStatusYml(sedmlLocation, sim.getImportedTaskID(), CLIUtils.Status.SUCCEEDED, outDir, duration + "", kisao);
                    utils.setOutputMessage(sedmlLocation, sim.getImportedTaskID(), outDir, "task", logTaskMessage);
                    CLIUtils.drawBreakLine("-", 100);
                } else {
                    System.err.println("Solver status: " + solver.getSolverStatus().getStatus());
                    System.err.println("Solver message: " + solver.getSolverStatus().getSimulationMessage().getDisplayMessage());
                    String error = solver.getSolverStatus().getSimulationMessage().getDisplayMessage() + " ";
                    throw new RuntimeException(error);
                }
            // CLIUtils.finalStatusUpdate( CLIUtils.Status.SUCCEEDED, outDir);
            } catch (Exception e) {
                // File aaa = new File("C:\\TEMP\\aaa.hdf5");
                // CLIUtils.exportPDE2HDF5(sim, outputDirForSedml, aaa);
                String error = "Failed execution: Model '" + docName + "' Task '" + sim.getDescription() + "'. ";
                System.err.println(error);
                long endTime = System.currentTimeMillis();
                long elapsedTime = endTime - startTimeTask;
                int duration = (int) Math.ceil(elapsedTime / 1000.0);
                String msg = "Running simulation for " + elapsedTime + " ms";
                System.out.println(msg);
                if (sim.getImportedTaskID() == null) {
                    String str = "'null' imported task id, this should never happen. ";
                    System.err.println();
                    logTaskError += str;
                } else {
                    utils.updateTaskStatusYml(sedmlLocation, sim.getImportedTaskID(), CLIUtils.Status.FAILED, outDir, duration + "", kisao);
                }
                // CLIUtils.finalStatusUpdate(CLIUtils.Status.FAILED, outDir);
                if (e.getMessage() != null) {
                    // something else than failure caught by solver instance during execution
                    logTaskError += (e.getMessage() + ". ");
                    System.err.println(e.getMessage());
                } else {
                    logTaskError += (error + ". ");
                }
                String type = e.getClass().getSimpleName();
                utils.setOutputMessage(sedmlLocation, sim.getImportedTaskID(), outDir, "task", logTaskMessage);
                utils.setExceptionMessage(sedmlLocation, sim.getImportedTaskID(), outDir, "task", type, logTaskError);
                String sdl = "";
                if (sd != null && sd.getShortDisplayLabel() != null && !sd.getShortDisplayLabel().isEmpty()) {
                    sdl = sd.getShortDisplayLabel();
                } else {
                    sdl = kisao;
                }
                if (logTaskError.contains("Process timed out")) {
                    if (bTimeoutFound == false) {
                        // don't repeat this for each task
                        String str = logTaskError.substring(0, logTaskError.indexOf("Process timed out"));
                        // truncate the rest of the spam
                        str += "Process timed out";
                        CLIStandalone.writeDetailedErrorList(outputBaseDir, bioModelBaseName + ",  solver: " + sdl + ": " + type + ": " + str);
                        bTimeoutFound = true;
                    }
                } else {
                    CLIStandalone.writeDetailedErrorList(outputBaseDir, bioModelBaseName + ",  solver: " + sdl + ": " + type + ": " + logTaskError);
                }
                CLIUtils.drawBreakLine("-", 100);
            }
            if (odeSolverResultSet != null) {
                resultsHash.put(sim.getImportedTaskID(), odeSolverResultSet);
            } else {
                // if any task fails, we still put it in the hash with a null value
                resultsHash.put(sim.getImportedTaskID(), null);
            }
            if (keepTempFiles == false) {
                CLIUtils.removeIntermediarySimFiles(outputDirForSedml);
            }
            simulationCount++;
        }
        bioModelCount++;
    }
    System.out.println("Ran " + simulationCount + " simulations for " + bioModelCount + " biomodels.");
    if (hasSomeSpatial) {
        CLIStandalone.writeSpatialList(outputBaseDir, bioModelBaseName);
    }
    return resultsHash;
}
Also used : AbstractCompiledSolver(cbit.vcell.solvers.AbstractCompiledSolver) HybridSolver(cbit.vcell.solver.stoch.HybridSolver) Solver(cbit.vcell.solver.server.Solver) ODESolver(cbit.vcell.solver.ode.ODESolver) GibsonSolver(cbit.vcell.solver.stoch.GibsonSolver) AbstractJavaSolver(cbit.vcell.solver.ode.AbstractJavaSolver) SimulationTask(cbit.vcell.messaging.server.SimulationTask) Task(org.jlibsedml.Task) SimulationTask(cbit.vcell.messaging.server.SimulationTask) GibsonSolver(cbit.vcell.solver.stoch.GibsonSolver) LinkedHashMap(java.util.LinkedHashMap) AbstractJavaSolver(cbit.vcell.solver.ode.AbstractJavaSolver) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) VCDocument(org.vcell.util.document.VCDocument) VCLogger(cbit.util.xml.VCLogger) SBMLImportException(org.vcell.sbml.vcell.SBMLImportException) ODESolver(cbit.vcell.solver.ode.ODESolver) HybridSolver(cbit.vcell.solver.stoch.HybridSolver) AbstractCompiledSolver(cbit.vcell.solvers.AbstractCompiledSolver) BioModel(cbit.vcell.biomodel.BioModel) UniformTimeCourse(org.jlibsedml.UniformTimeCourse)

Aggregations

VCLogger (cbit.util.xml.VCLogger)1 BioModel (cbit.vcell.biomodel.BioModel)1 SimulationTask (cbit.vcell.messaging.server.SimulationTask)1 AbstractJavaSolver (cbit.vcell.solver.ode.AbstractJavaSolver)1 ODESolver (cbit.vcell.solver.ode.ODESolver)1 ODESolverResultSet (cbit.vcell.solver.ode.ODESolverResultSet)1 Solver (cbit.vcell.solver.server.Solver)1 GibsonSolver (cbit.vcell.solver.stoch.GibsonSolver)1 HybridSolver (cbit.vcell.solver.stoch.HybridSolver)1 AbstractCompiledSolver (cbit.vcell.solvers.AbstractCompiledSolver)1 LinkedHashMap (java.util.LinkedHashMap)1 Task (org.jlibsedml.Task)1 UniformTimeCourse (org.jlibsedml.UniformTimeCourse)1 SBMLImportException (org.vcell.sbml.vcell.SBMLImportException)1 VCDocument (org.vcell.util.document.VCDocument)1