Search in sources :

Example 1 with SolverStatus

use of cbit.vcell.solver.server.SolverStatus in project vcell by virtualcell.

the class ComsolSolver method startSolver.

@Override
public void startSolver() {
    try {
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_STARTING, SimulationMessage.MESSAGE_SOLVER_STARTING_INIT));
        initialize();
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_RUNNING, SimulationMessage.MESSAGE_SOLVER_RUNNING_START));
        fireSolverStarting(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING);
        String filePrefix = simTask.getSimulationJob().getSimulationJobID();
        try {
            comsolService.connectComsol();
            File reportFile = new File(getSaveDirectory(), filePrefix + ".comsoldat");
            File javaFile = new File(getSaveDirectory(), filePrefix + ".java");
            File mphFile = new File(getSaveDirectory(), filePrefix + ".mph");
            comsolService.solve(this.vccModel, reportFile, javaFile, mphFile);
        } finally {
            comsolService.disconnectComsol();
        }
        File logFile = new File(getSaveDirectory(), filePrefix + ".log");
        FileUtils.writeStringToFile(logFile, createLogFileContents(simTask.getSimulation().getSolverTaskDescription()));
        VisMesh visMesh = new VisMesh();
        File reportFile = new File(getSaveDirectory(), filePrefix + ".comsoldat");
        ComsolMeshReader.read(visMesh, reportFile);
        time = simTask.getSimulation().getSolverTaskDescription().getTimeBounds().getEndingTime();
        progress = 1.0;
        fireSolverPrinted(time);
        fireSolverProgress(progress);
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_FINISHED, SimulationMessage.MESSAGE_SOLVER_FINISHED));
        fireSolverFinished();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, SimulationMessage.solverAborted(e.getMessage())));
        fireSolverAborted(SimulationMessage.solverAborted(e.getMessage()));
    }
}
Also used : VisMesh(org.vcell.vis.vismesh.thrift.VisMesh) SolverStatus(cbit.vcell.solver.server.SolverStatus) File(java.io.File) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) SolverException(cbit.vcell.solver.SolverException)

Example 2 with SolverStatus

use of cbit.vcell.solver.server.SolverStatus in project vcell by virtualcell.

the class SmoldynSolver method initialize.

/**
 *  This method takes the place of the old runUnsteady()...
 */
protected void initialize() throws SolverException {
    if (lg.isTraceEnabled())
        lg.trace("SmoldynSolver.initialize()");
    fireSolverStarting(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING_INIT);
    writeFunctionsFile();
    // write subdomains file
    try {
        SubdomainInfo.write(new File(getBaseName() + SimDataConstants.SUBDOMAINS_FILE_SUFFIX), simTask.getSimulation().getMathDescription());
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new SolverException(e1.getMessage());
    } catch (MathException e1) {
        e1.printStackTrace();
        throw new SolverException(e1.getMessage());
    }
    String inputFilename = getInputFilename();
    if (lg.isTraceEnabled())
        lg.trace("SmoldynSolver.initialize() baseName = " + getBaseName());
    setSolverStatus(new SolverStatus(SolverStatus.SOLVER_RUNNING, SimulationMessage.MESSAGE_SOLVER_RUNNING_INPUT_FILE));
    fireSolverStarting(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING_INPUT_FILE);
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(inputFilename);
        if (SystemUtils.IS_OS_WINDOWS) {
            // 
            // the windows executable is compiled under cygwin (or mingw) and expect Unix style line termination characters.
            // so SmoldynInput file is written with platform default Java convention (different on windows) to a String.
            // Then the string is translated to Unix style before written to the file.
            // smoldyn is particularly sensitive to this issue, other compiled solvers are more tolerant.
            // 
            StringWriter stringWriter = new StringWriter();
            PrintWriter pw2 = new PrintWriter(stringWriter);
            SmoldynFileWriter stFileWriter = new SmoldynFileWriter(pw2, false, getBaseName(), simTask, bMessaging);
            stFileWriter.write();
            String fileContents = stringWriter.getBuffer().toString();
            fileContents = fileContents.replace("\r\n", "\n");
            pw.write(fileContents);
        } else {
            // 
            // for linux or macos, no translation is necessary.
            // 
            SmoldynFileWriter stFileWriter = new SmoldynFileWriter(pw, false, getBaseName(), simTask, bMessaging);
            stFileWriter.write();
        }
    } catch (Exception e) {
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, SimulationMessage.solverAborted("Could not generate input file: " + e.getMessage())));
        e.printStackTrace(System.out);
        throw new SolverException(e.getMessage());
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
    setSolverStatus(new SolverStatus(SolverStatus.SOLVER_RUNNING, SimulationMessage.MESSAGE_SOLVER_RUNNING_START));
    // get executable path+name.
    setMathExecutable(new MathExecutable(getMathExecutableCommand(), getSaveDirectory()));
// setMathExecutable(new cbit.vcell.solvers.MathExecutable(executableName + " gibson " + getBaseName() + ".stochInput" + " " + getBaseName() + ".stoch"));
}
Also used : StringWriter(java.io.StringWriter) MathException(cbit.vcell.math.MathException) IOException(java.io.IOException) SolverException(cbit.vcell.solver.SolverException) MathExecutable(cbit.vcell.solvers.MathExecutable) File(java.io.File) SolverStatus(cbit.vcell.solver.server.SolverStatus) IOException(java.io.IOException) SolverException(cbit.vcell.solver.SolverException) MathException(cbit.vcell.math.MathException) PrintWriter(java.io.PrintWriter)

Example 3 with SolverStatus

use of cbit.vcell.solver.server.SolverStatus in project vcell by virtualcell.

the class FRAPStudy method runFVSolverStandalone.

public static void runFVSolverStandalone(File simulationDataDir, Simulation sim, ExternalDataIdentifier imageDataExtDataID, ExternalDataIdentifier roiExtDataID, ClientTaskStatusSupport progressListener, boolean bCheckSteadyState) throws Exception {
    FieldFunctionArguments[] fieldFunctionArgs = FieldUtilities.getFieldFunctionArguments(sim.getMathDescription());
    FieldDataIdentifierSpec[] fieldDataIdentifierSpecs = new FieldDataIdentifierSpec[fieldFunctionArgs.length];
    for (int i = 0; i < fieldDataIdentifierSpecs.length; i++) {
        if (fieldFunctionArgs[i].getFieldName().equals(imageDataExtDataID.getName())) {
            fieldDataIdentifierSpecs[i] = new FieldDataIdentifierSpec(fieldFunctionArgs[i], imageDataExtDataID);
        } else if (fieldFunctionArgs[i].getFieldName().equals(roiExtDataID.getName())) {
            fieldDataIdentifierSpecs[i] = new FieldDataIdentifierSpec(fieldFunctionArgs[i], roiExtDataID);
        } else {
            throw new RuntimeException("failed to resolve field named " + fieldFunctionArgs[i].getFieldName());
        }
    }
    int jobIndex = 0;
    SimulationTask simTask = new SimulationTask(new SimulationJob(sim, jobIndex, fieldDataIdentifierSpecs), 0);
    // if we need to check steady state, do the following two lines
    if (bCheckSteadyState) {
        simTask.getSimulation().getSolverTaskDescription().setStopAtSpatiallyUniformErrorTolerance(ErrorTolerance.getDefaultSpatiallyUniformErrorTolerance());
        simTask.getSimulation().getSolverTaskDescription().setErrorTolerance(new ErrorTolerance(1e-6, 1e-2));
    }
    SolverUtilities.prepareSolverExecutable(sim.getSolverTaskDescription().getSolverDescription());
    FVSolverStandalone fvSolver = new FVSolverStandalone(simTask, simulationDataDir, false);
    fvSolver.startSolver();
    SolverStatus status = fvSolver.getSolverStatus();
    while (status.getStatus() != SolverStatus.SOLVER_FINISHED && status.getStatus() != SolverStatus.SOLVER_ABORTED) {
        if (progressListener != null) {
            progressListener.setProgress((int) (fvSolver.getProgress() * 100));
            if (progressListener.isInterrupted()) {
                fvSolver.stopSolver();
                throw UserCancelException.CANCEL_GENERIC;
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            ex.printStackTrace(System.out);
        // catch interrupted exception and ignore it, otherwise it will popup a dialog in user interface saying"sleep interrupted"
        }
        status = fvSolver.getSolverStatus();
    }
    if (status.getStatus() == SolverStatus.SOLVER_FINISHED) {
        String roiMeshFileName = SimulationData.createCanonicalMeshFileName(roiExtDataID.getKey(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT, false);
        String imageDataMeshFileName = SimulationData.createCanonicalMeshFileName(imageDataExtDataID.getKey(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT, false);
        String simulationMeshFileName = SimulationData.createCanonicalMeshFileName(sim.getVersion().getVersionKey(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT, false);
        // delete old external data mesh files and copy simulation mesh file to them
        File roiMeshFile = new File(simulationDataDir, roiMeshFileName);
        File imgMeshFile = new File(simulationDataDir, imageDataMeshFileName);
        File simMeshFile = new File(simulationDataDir, simulationMeshFileName);
        if (!roiMeshFile.delete()) {
            throw new Exception("Couldn't delete ROI Mesh file " + roiMeshFile.getAbsolutePath());
        }
        if (!imgMeshFile.delete()) {
            throw new Exception("Couldn't delete ImageData Mesh file " + imgMeshFile.getAbsolutePath());
        }
        FileUtils.copyFile(simMeshFile, roiMeshFile);
        FileUtils.copyFile(simMeshFile, imgMeshFile);
    } else {
        throw new Exception("Sover did not finish normally." + status.toString());
    }
}
Also used : SimulationTask(cbit.vcell.messaging.server.SimulationTask) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) FVSolverStandalone(cbit.vcell.solvers.FVSolverStandalone) ImageException(cbit.image.ImageException) UserCancelException(org.vcell.util.UserCancelException) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) ErrorTolerance(cbit.vcell.solver.ErrorTolerance) SolverStatus(cbit.vcell.solver.server.SolverStatus) File(java.io.File) SimulationJob(cbit.vcell.solver.SimulationJob)

Example 4 with SolverStatus

use of cbit.vcell.solver.server.SolverStatus in project vcell by virtualcell.

the class AbstractCompiledSolver method runSolver.

/**
 * Insert the method's description here.
 * Creation date: (6/26/2001 3:08:31 PM)
 */
public void runSolver() {
    try {
        setCurrentTime(simTask.getSimulationJob().getSimulation().getSolverTaskDescription().getTimeBounds().getStartingTime());
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_STARTING, SimulationMessage.MESSAGE_SOLVER_STARTING_INIT));
        // fireSolverStarting("initializing");
        // depends on solver; the initialize() method in actual solver will fire detailed messages
        initialize();
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_RUNNING, SimulationMessage.MESSAGE_SOLVER_RUNNING_START));
        fireSolverStarting(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING);
        if (OperatingSystemInfo.getInstance().isLinux()) {
            final String LD_LIB_PATH = "LD_LIBRARY_PATH";
            File solversDir = ResourceUtil.getLocalSolversDirectory();
            String existingLD_LIB_PATH = null;
            Map<String, String> envMap = System.getenv();
            Iterator<String> envIter = envMap.keySet().iterator();
            while (envIter.hasNext()) {
                String key = envIter.next();
                String val = envMap.get(key).toString();
                // System.out.println(key+"\n     "+val);
                if (key.equals(LD_LIB_PATH)) {
                    existingLD_LIB_PATH = val;
                    if (existingLD_LIB_PATH != null && existingLD_LIB_PATH.length() > 0 && !existingLD_LIB_PATH.endsWith(":")) {
                        existingLD_LIB_PATH += ":";
                    }
                    break;
                }
            }
            String newLD_LIB_PATH = (existingLD_LIB_PATH == null ? "" : existingLD_LIB_PATH) + solversDir.getAbsolutePath();
            System.out.println("-----Setting executable " + LD_LIB_PATH + " to " + newLD_LIB_PATH);
            getMathExecutable().addEnvironmentVariable(LD_LIB_PATH, newLD_LIB_PATH);
        }
        getMathExecutable().start();
        cleanup();
        // getMathExecutable().start() may end prematurely (error or user stop), so check status before firing...
        if (getMathExecutable().getStatus().equals(org.vcell.util.exe.ExecutableStatus.COMPLETE)) {
            setSolverStatus(new SolverStatus(SolverStatus.SOLVER_FINISHED, SimulationMessage.MESSAGE_SOLVER_FINISHED));
            fireSolverFinished();
        }
    } catch (SolverException integratorException) {
        lg.error(integratorException.getMessage(), integratorException);
        cleanup();
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, SimulationMessage.solverAborted(integratorException.getMessage())));
        fireSolverAborted(SimulationMessage.solverAborted(integratorException.getMessage()));
    } catch (org.vcell.util.exe.ExecutableException executableException) {
        lg.error(executableException.getMessage(), executableException);
        cleanup();
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, SimulationMessage.solverAborted("Could not execute code: " + executableException.getMessage())));
        fireSolverAborted(SimulationMessage.solverAborted(executableException.getMessage()));
    } catch (Exception exception) {
        if (lg.isWarnEnabled())
            lg.warn("AbstractODESolver.start() : Caught Throwable instead of SolverException -- THIS EXCEPTION SHOULD NOT HAPPEN!");
        lg.error(exception.getMessage(), exception);
        cleanup();
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, SimulationMessage.solverAborted(exception.getMessage())));
        fireSolverAborted(SimulationMessage.solverAborted(exception.getMessage()));
    } finally {
        synchronized (this) {
            fieldThread = null;
        }
    }
}
Also used : SolverException(cbit.vcell.solver.SolverException) SolverStatus(cbit.vcell.solver.server.SolverStatus) File(java.io.File) SolverException(cbit.vcell.solver.SolverException)

Example 5 with SolverStatus

use of cbit.vcell.solver.server.SolverStatus in project vcell by virtualcell.

the class AbstractCompiledSolver method stopSolver.

/**
 */
public final synchronized void stopSolver() {
    if (fieldThread != null && getMathExecutable() != null) {
        fieldThread.interrupt();
        getMathExecutable().stop();
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_STOPPED, SimulationMessage.MESSAGE_SOLVER_STOPPED_BY_USER));
        fireSolverStopped();
    }
}
Also used : SolverStatus(cbit.vcell.solver.server.SolverStatus)

Aggregations

SolverStatus (cbit.vcell.solver.server.SolverStatus)25 IOException (java.io.IOException)14 SimulationJob (cbit.vcell.solver.SimulationJob)12 SimulationTask (cbit.vcell.messaging.server.SimulationTask)11 SolverException (cbit.vcell.solver.SolverException)11 File (java.io.File)11 PrintWriter (java.io.PrintWriter)8 FVSolverStandalone (cbit.vcell.solvers.FVSolverStandalone)7 Simulation (cbit.vcell.solver.Simulation)6 MathExecutable (cbit.vcell.solvers.MathExecutable)6 UserCancelException (org.vcell.util.UserCancelException)6 ImageException (cbit.image.ImageException)5 FieldDataIdentifierSpec (cbit.vcell.field.FieldDataIdentifierSpec)5 ExpressionException (cbit.vcell.parser.ExpressionException)5 FieldFunctionArguments (cbit.vcell.field.FieldFunctionArguments)4 TempSimulation (cbit.vcell.solver.TempSimulation)4 Solver (cbit.vcell.solver.server.Solver)4 MathException (cbit.vcell.math.MathException)3 ODEDataBlock (cbit.vcell.simdata.ODEDataBlock)3 SimulationData (cbit.vcell.simdata.SimulationData)3