Search in sources :

Example 6 with MathExecutable

use of cbit.vcell.solvers.MathExecutable in project vcell by virtualcell.

the class NFSimSolver method main.

public static void main(String[] args) {
    String baseName = "SimID_";
    String workingDir = "C:\\Users\\jmasison\\.vcell\\simdata\\user\\sub\\";
    String executable = "C:\\Users\\jmasison\\workspace\\VCell_trunk\\localsolvers\\win64\\NFsim_x64.exe";
    String blankDelimiters = " \t";
    String debugfile = "C:\\Users\\jmasison\\.vcell\\simdata\\user\\sub\\debug.txt";
    int seed = 1807259453;
    int steps = 100;
    // unit system is in sec
    double duration = 0.1;
    Map<Integer, Double> timestepMap = new HashMap<>();
    BufferedReader br = null;
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(debugfile, "UTF-8");
        for (int i = 1; i < steps + 1; i++) {
            // print				writer.println("\t\t\t\tentering i loop");
            // C:\\TEMP\\eee\\SimID_000.nfsimInput
            String input = workingDir + baseName + "000" + ".nfsimInput";
            String output = workingDir + baseName + i + ".gdat";
            String species = workingDir + baseName + i + ".species";
            double interval = duration / steps;
            // we start at timepoint 0
            double dur = interval * (i - 1);
            String sdur = "" + dur;
            if (sdur.length() > 6) {
                sdur = sdur.substring(0, 6);
            }
            // average length of patterns at this timepoint, over many simulations
            double averageOfAverage = 0;
            int numSimulations = 50;
            for (int ii = 0; ii < numSimulations; ii++) {
                // print					writer.println("\t\t\t\t\tentering ii loop");
                // calculate a new seed for this simulation (same start seeds will be used for each time point)
                int curSeed = seed + ii * 538297;
                // String command = executable + " -seed " + curSeed + " -vcell -xml " + input + " -o " + output + " -sim " + sdur + " -ss " + species + " -oSteps 1" + " -notf -cb";
                // Process p = Runtime.getRuntime().exec(command);
                // p.waitFor();
                String curspecies = species + "_sim_" + ii;
                String[] commands = new String[] { executable, "-seed", Integer.toString(curSeed), "-vcell", "-xml", input, "-o", output, "-sim", sdur, "-ss", curspecies, "-oSteps", "1", "-notf", "-cb" };
                MathExecutable mathExe = new MathExecutable(commands, new File(workingDir));
                try {
                    mathExe.start();
                } catch (ExecutableException e) {
                    e.printStackTrace();
                    System.out.println("exe failed, exitValue=" + mathExe.getExitValue() + ", \nstderr=" + mathExe.getStderrString() + ", \nstdout=" + mathExe.getStdoutString());
                }
                // command = "cmd.exe /c del " + workingDir + "*.gdat";
                // p = Runtime.getRuntime().exec(command);
                // p.waitFor();
                // delete the intermediate .gdat files
                // MathExecutable mathExeDel = new MathExecutable(new String[] {"cmd.exe", "/c", "del", workingDir, "*.gdat"}, new File(workingDir));
                // try {
                // mathExeDel.start();
                // } catch (ExecutableException e) {
                // e.printStackTrace();
                // }
                Map<Integer, Integer> occurencesMap = new HashMap<>();
                br = new BufferedReader(new FileReader(curspecies));
                // ex: A(s!1,t!2).B(u!1).B(u!2)  5
                String line;
                while ((line = br.readLine()) != null) {
                    if (line.startsWith("#")) {
                        continue;
                    }
                    // how many molecules in the above pattern => 3
                    int numMolecules = 1;
                    // how many instances of this pattern => 5
                    int number;
                    StringTokenizer nextLine = new StringTokenizer(line, blankDelimiters);
                    // A(s!1,t!2).B(u!1).B(u!2)
                    String pattern = nextLine.nextToken();
                    for (int j = 0; j < pattern.length(); j++) {
                        if (pattern.charAt(j) == '.') {
                            // numMolecules equals the number of 'dot' characters in string + 1
                            numMolecules++;
                        }
                    }
                    // 5
                    String s = nextLine.nextToken();
                    number = Integer.parseInt(s);
                    if (occurencesMap.containsKey(numMolecules)) {
                        int currentNumber = occurencesMap.get(numMolecules);
                        currentNumber += number;
                        occurencesMap.put(numMolecules, currentNumber);
                    } else {
                        occurencesMap.put(numMolecules, number);
                    }
                }
                double up = 0;
                double down = 0;
                for (Map.Entry<Integer, Integer> entry : occurencesMap.entrySet()) {
                    Integer key = entry.getKey();
                    if (key == 1) {
                        continue;
                    }
                    Integer value = entry.getValue();
                    up += key * value;
                    down += value;
                // print						writer.println(curspecies+"_"+i+" : "+ up + ", " + down);
                }
                double average;
                if (down > 0) {
                    average = up / down;
                } else {
                    average = 0;
                }
                // print					writer.println("average: "+average);; writer.println();
                averageOfAverage += average;
                // print					writer.println("Avergae_of_average_for_sim_"+ii+"_time_"+i+" : "+average);; writer.println();
                br.close();
            // if uncimmented make sure to use mathexecutable
            // command = "cmd.exe /c del " + workingDir + "*.species";
            // p = Runtime.getRuntime().exec(command);
            // p.waitFor();
            }
            averageOfAverage = averageOfAverage / numSimulations;
            // print				writer.println("Final_average_for_time_"+i+" : "+averageOfAverage); writer.println();
            // save average for time step (i) to map
            timestepMap.put(i, averageOfAverage);
        }
        // exited both loops
        // print		writer.println();
        writer.println("Time\tAverageCLusterSize");
        for (int j = 1; j < steps + 1; j++) {
            writer.println((j - 1) + "\t" + timestepMap.get(j));
        }
        writer.println();
        writer.println("Done");
        writer.close();
        System.out.println("\n\n\n\nDone\n\n");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Also used : HashMap(java.util.HashMap) ExecutableException(org.vcell.util.exe.ExecutableException) MathExecutable(cbit.vcell.solvers.MathExecutable) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 7 with MathExecutable

use of cbit.vcell.solvers.MathExecutable in project vcell by virtualcell.

the class NFSimSolver method initialize.

/**
 * This method takes the place of the old runUnsteady()...
 */
protected void initialize() throws SolverException {
    if (lg.isTraceEnabled())
        lg.trace("NFSimSolver.initialize()");
    fireSolverStarting(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING_INIT);
    writeFunctionsFile();
    String inputFilename = getInputFilename();
    if (lg.isTraceEnabled())
        lg.trace("NFSimSolver.initialize() inputFilename = " + getInputFilename());
    setSolverStatus(new SolverStatus(SolverStatus.SOLVER_RUNNING, SimulationMessage.MESSAGE_SOLVER_RUNNING_INPUT_FILE));
    fireSolverStarting(SimulationMessage.MESSAGE_SOLVEREVENT_STARTING_INPUT_FILE);
    try (PrintWriter pw = new PrintWriter(inputFilename)) {
        NFSimFileWriter stFileWriter = new NFSimFileWriter(pw, 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());
    }
    PrintWriter lg = null;
    String logFilename = getLogFilename();
    String outputFilename = getOutputFilename();
    try {
        lg = new PrintWriter(logFilename);
        String shortOutputFilename = outputFilename.substring(1 + outputFilename.lastIndexOf("\\"));
        lg.println(NFSIM_DATA_IDENTIFIER + " " + shortOutputFilename);
    } catch (Exception e) {
        setSolverStatus(new SolverStatus(SolverStatus.SOLVER_ABORTED, SimulationMessage.solverAborted("Could not generate log file: " + e.getMessage())));
        e.printStackTrace(System.out);
        throw new SolverException(e.getMessage());
    } finally {
        if (lg != null) {
            lg.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 : SolverException(cbit.vcell.solver.SolverException) MathExecutable(cbit.vcell.solvers.MathExecutable) SolverStatus(cbit.vcell.solver.server.SolverStatus) ExecutableException(org.vcell.util.exe.ExecutableException) SolverException(cbit.vcell.solver.SolverException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

MathExecutable (cbit.vcell.solvers.MathExecutable)7 IOException (java.io.IOException)7 SolverException (cbit.vcell.solver.SolverException)6 SolverStatus (cbit.vcell.solver.server.SolverStatus)6 PrintWriter (java.io.PrintWriter)6 MathException (cbit.vcell.math.MathException)3 ExpressionException (cbit.vcell.parser.ExpressionException)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 ExecutableException (org.vcell.util.exe.ExecutableException)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1