Search in sources :

Example 1 with ODESolver

use of cbit.vcell.solver.ode.ODESolver 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)

Example 2 with ODESolver

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

the class SolverHandler method simulateAllVcmlTasks.

@Deprecated
public HashMap<String, ODESolverResultSet> simulateAllVcmlTasks(File vcmlPath, File outputDir) throws Exception {
    // create the VCDocument(s) (bioModel(s) + application(s) + simulation(s)), do sanity checks
    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;
    VCDocument singleDoc = null;
    try {
        singleDoc = VCMLHandler.convertVcmlToVcDocument(vcmlPath);
    } catch (Exception e) {
        System.err.println("Unable to Parse SED-ML into Bio-Model, failed with err: " + e.getMessage());
        throw e;
    }
    try {
        sanityCheck(singleDoc);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    assert singleDoc != null;
    docName = singleDoc.getName();
    bioModel = (BioModel) singleDoc;
    sims = bioModel.getSimulations();
    for (Simulation sim : sims) {
        sim = new TempSimulation(sim, false);
        SolverTaskDescription std = sim.getSolverTaskDescription();
        SolverDescription sd = std.getSolverDescription();
        String kisao = sd.getKisao();
        SimulationJob simJob = new SimulationJob(sim, 0, null);
        SimulationTask simTask = new SimulationTask(simJob, 0);
        Solver solver = SolverFactory.createSolver(outputDir, simTask, false);
        ODESolverResultSet odeSolverResultSet = null;
        try {
            if (solver instanceof AbstractCompiledSolver) {
                ((AbstractCompiledSolver) solver).runSolver();
                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 {
                    System.err.println("Solver results are not compatible with CSV format");
                }
            // TODO: Add support for JAVA solvers and implement interpolation
            // odeSolverResultSet = CLIUtils.interpolate(odeSolverResultSet, (UniformTimeCourse) sedmlSim);
            } else {
                // this should actually never happen...
                throw new Exception("Unexpected solver: " + kisao + " " + solver);
            }
            if (solver.getSolverStatus().getStatus() == SolverStatus.SOLVER_FINISHED) {
                System.out.println("Succesful execution: Model '" + docName + "' Task '" + sim.getDescription() + "'.");
            } else {
                System.err.println("Solver status: " + solver.getSolverStatus().getStatus());
                System.err.println("Solver message: " + solver.getSolverStatus().getSimulationMessage().getDisplayMessage());
                throw new Exception();
            }
        } catch (Exception e) {
            System.err.println("Failed execution: Model '" + docName + "' Task '" + sim.getDescription() + "'.");
            if (e.getMessage() != null) {
                // something else than failure caught by solver instance during execution
                System.err.println(e.getMessage());
            }
        }
        if (odeSolverResultSet != null) {
            resultsHash.put(sim.getName(), odeSolverResultSet);
        }
        CLIUtils.removeIntermediarySimFiles(outputDir);
    }
    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) VCDocument(org.vcell.util.document.VCDocument) GibsonSolver(cbit.vcell.solver.stoch.GibsonSolver) SBMLImportException(org.vcell.sbml.vcell.SBMLImportException) ODESolver(cbit.vcell.solver.ode.ODESolver) HybridSolver(cbit.vcell.solver.stoch.HybridSolver) LinkedHashMap(java.util.LinkedHashMap) AbstractCompiledSolver(cbit.vcell.solvers.AbstractCompiledSolver) BioModel(cbit.vcell.biomodel.BioModel) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet)

Aggregations

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