use of org.vcell.util.exe.Executable in project vcell by virtualcell.
the class VCellSBMLSolver method solveVCell.
public File solveVCell(String filePrefix, File outDir, String sbmlFileName, SimSpec testSpec) throws IOException, SolverException, SbmlException {
try {
cbit.util.xml.VCLogger logger = new LocalLogger();
//
// Instantiate an SBMLImporter to get the speciesUnitsHash - to compute the conversion factor from VC->SB species units.
// and import SBML (sbml->bioModel)
org.vcell.sbml.vcell.SBMLImporter sbmlImporter = new org.vcell.sbml.vcell.SBMLImporter(sbmlFileName, logger, false);
BioModel bioModel = sbmlImporter.getBioModel();
if (bRoundTrip) {
// Round trip the bioModel (bioModel->sbml->bioModel).
// export bioModel as sbml and save
String vcml_sbml = cbit.vcell.xml.XmlHelper.exportSBML(bioModel, 2, 1, 0, false, bioModel.getSimulationContext(0), null);
// re-import bioModel from exported sbml
XMLSource vcml_sbml_Src = new XMLSource(vcml_sbml);
BioModel newBioModel = (BioModel) XmlHelper.importSBML(logger, vcml_sbml_Src, false);
// have rest of code use the round-tripped biomodel
bioModel = newBioModel;
}
//
// select only Application, generate math, and create a single Simulation.
//
SimulationContext simContext = bioModel.getSimulationContext(0);
MathMapping mathMapping = simContext.createNewMathMapping();
MathDescription mathDesc = mathMapping.getMathDescription();
simContext.setMathDescription(mathDesc);
SimulationVersion simVersion = new SimulationVersion(new KeyValue("100"), "unnamed", null, null, null, null, null, null, null, null);
Simulation sim = new Simulation(simVersion, mathDesc);
sim.setName("unnamed");
// if time factor from SBML is not 1 (i.e., it is not in secs but in minutes or hours), convert endTime to min/hr as : endTime*timeFactor
// double endTime = testSpec.getEndTime()*timeFactor;
double endTime = testSpec.getEndTime();
sim.getSolverTaskDescription().setTimeBounds(new TimeBounds(0, endTime));
TimeStep timeStep = new TimeStep();
sim.getSolverTaskDescription().setTimeStep(new TimeStep(timeStep.getMinimumTimeStep(), timeStep.getDefaultTimeStep(), endTime / 10000));
sim.getSolverTaskDescription().setOutputTimeSpec(new UniformOutputTimeSpec((endTime - 0) / testSpec.getNumTimeSteps()));
sim.getSolverTaskDescription().setErrorTolerance(new ErrorTolerance(testSpec.getAbsTolerance(), testSpec.getRelTolerance()));
// sim.getSolverTaskDescription().setErrorTolerance(new ErrorTolerance(1e-10, 1e-12));
// Generate .idaInput string
File idaInputFile = new File(outDir, filePrefix + SimDataConstants.IDAINPUT_DATA_EXTENSION);
PrintWriter idaPW = new java.io.PrintWriter(idaInputFile);
SimulationJob simJob = new SimulationJob(sim, 0, null);
SimulationTask simTask = new SimulationTask(simJob, 0);
IDAFileWriter idaFileWriter = new IDAFileWriter(idaPW, simTask);
idaFileWriter.write();
idaPW.close();
// use the idastandalone solver
File idaOutputFile = new File(outDir, filePrefix + SimDataConstants.IDA_DATA_EXTENSION);
// String sundialsSolverExecutable = "C:\\Developer\\Eclipse\\workspace\\VCell 4.8\\SundialsSolverStandalone_NoMessaging.exe";
String executableName = null;
try {
executableName = SolverUtilities.getExes(SolverDescription.IDA)[0].getAbsolutePath();
} catch (IOException e) {
throw new RuntimeException("failed to get executable for solver " + SolverDescription.IDA.getDisplayLabel() + ": " + e.getMessage(), e);
}
Executable executable = new Executable(new String[] { executableName, idaInputFile.getAbsolutePath(), idaOutputFile.getAbsolutePath() });
executable.start();
/* // Generate .cvodeInput string
File cvodeFile = new File(outDir,filePrefix+SimDataConstants.CVODEINPUT_DATA_EXTENSION);
PrintWriter cvodePW = new java.io.PrintWriter(cvodeFile);
SimulationJob simJob = new SimulationJob(sim, 0, null);
CVodeFileWriter cvodeFileWriter = new CVodeFileWriter(cvodePW, simJob);
cvodeFileWriter.write();
cvodePW.close();
// use the cvodeStandalone solver
File cvodeOutputFile = new File(outDir,filePrefix+SimDataConstants.IDA_DATA_EXTENSION);
String sundialsSolverExecutable = PropertyLoader.getRequiredProperty(PropertyLoader.sundialsSolverExecutableProperty);
Executable executable = new Executable(new String[]{sundialsSolverExecutable, cvodeFile.getAbsolutePath(), cvodeOutputFile.getAbsolutePath()});
executable.start();
*/
// get the result
ODESolverResultSet odeSolverResultSet = getODESolverResultSet(simJob, idaOutputFile.getPath());
// remove CVOde input and output files ??
idaInputFile.delete();
idaOutputFile.delete();
//
// print header
//
File outputFile = new File(outDir, "results" + filePrefix + ".csv");
java.io.PrintStream outputStream = new java.io.PrintStream(new java.io.BufferedOutputStream(new java.io.FileOutputStream(outputFile)));
outputStream.print("time");
for (int i = 0; i < testSpec.getVarsList().length; i++) {
outputStream.print("," + testSpec.getVarsList()[i]);
}
outputStream.println();
//
// extract data for time and species
//
double[][] data = new double[testSpec.getVarsList().length + 1][];
int column = odeSolverResultSet.findColumn("t");
data[0] = odeSolverResultSet.extractColumn(column);
int origDataLength = data[0].length;
for (int i = 0; i < testSpec.getVarsList().length; i++) {
column = odeSolverResultSet.findColumn(testSpec.getVarsList()[i]);
if (column == -1) {
Variable var = simJob.getSimulationSymbolTable().getVariable(testSpec.getVarsList()[i]);
data[i + 1] = new double[data[0].length];
if (var instanceof cbit.vcell.math.Constant) {
double value = ((cbit.vcell.math.Constant) var).getExpression().evaluateConstant();
for (int j = 0; j < data[i + 1].length; j++) {
data[i + 1][j] = value;
}
} else {
throw new RuntimeException("Did not find " + testSpec.getVarsList()[i] + " in simulation");
}
} else {
data[i + 1] = odeSolverResultSet.extractColumn(column);
}
}
//
// for each time, print row
//
int index = 0;
double[] sampleTimes = new double[testSpec.getNumTimeSteps() + 1];
for (int i = 0; i <= testSpec.getNumTimeSteps(); i++) {
sampleTimes[i] = endTime * i / testSpec.getNumTimeSteps();
}
Model vcModel = bioModel.getModel();
ReservedSymbol kMole = vcModel.getKMOLE();
for (int i = 0; i < sampleTimes.length; i++) {
//
while (true) {
//
if (index == odeSolverResultSet.getRowCount() - 1) {
if (data[0][index] == sampleTimes[i]) {
break;
} else {
throw new RuntimeException("sampleTime does not match at last time point");
}
}
//
if (data[0][index + 1] > sampleTimes[i]) {
break;
}
//
// sampleTime must be later in our data list.
//
index++;
}
// if data[0][index] == sampleTime no need to interpolate
if (data[0][index] == sampleTimes[i]) {
// if timeFactor is not 1.0, time is not in seconds (mins or hrs); if timeFactor is 60, divide sampleTime/60; if it is 3600, divide sampleTime/3600.
// if (timeFactor != 1.0) {
// outputStream.print(data[0][index]/timeFactor);
// } else {
outputStream.print(data[0][index]);
// }
for (int j = 0; j < testSpec.getVarsList().length; j++) {
// SBMLImporter.SBVCConcentrationUnits spConcUnits = speciesUnitsHash.get(testSpec.getVarsList()[j]);
// if (spConcUnits != null) {
// VCUnitDefinition sbunits = spConcUnits.getSBConcentrationUnits();
// VCUnitDefinition vcunits = spConcUnits.getVCConcentrationUnits();
// SBMLUnitParameter unitFactor = SBMLUtils.getConcUnitFactor("spConcParam", vcunits, sbunits, kMole);
// outputStream.print("," + data[j + 1][index] * unitFactor.getExpression().evaluateConstant()); //earlier, hack unitfactor = 0.000001
// earlier, hack unitfactor = 0.000001
outputStream.print("," + data[j + 1][index]);
// }
}
// System.out.println("No interpolation needed!");
outputStream.println();
} else {
// if data[0][index] < sampleTime, must interpolate
double fraction = (sampleTimes[i] - data[0][index]) / (data[0][index + 1] - data[0][index]);
// if timeFactor is not 1.0, time is not in seconds (mins or hrs); if timeFactor is 60, divide sampleTime/60; if it is 3600, divide sampleTime/3600.
// if (timeFactor != 1.0) {
// outputStream.print(sampleTimes[i]/timeFactor);
// } else {
outputStream.print(sampleTimes[i]);
// }
for (int j = 0; j < testSpec.getVarsList().length; j++) {
double interpolatedValue = 0.0;
double[] speciesVals = null;
double[] times = null;
// Currently using 2nd order interpolation
if (index == 0) {
// can only do 1st order interpolation
times = new double[] { data[0][index], data[0][index + 1] };
speciesVals = new double[] { data[j + 1][index], data[j + 1][index + 1] };
interpolatedValue = MathTestingUtilities.taylorInterpolation(sampleTimes[i], times, speciesVals);
} else if (index >= 1 && index <= origDataLength - 3) {
double val_1 = Math.abs(sampleTimes[i] - data[0][index - 1]);
double val_2 = Math.abs(sampleTimes[i] - data[0][index + 2]);
if (val_1 < val_2) {
times = new double[] { data[0][index - 1], data[0][index], data[0][index + 1] };
speciesVals = new double[] { data[j + 1][index - 1], data[j + 1][index], data[j + 1][index + 1] };
} else {
times = new double[] { data[0][index], data[0][index + 1], data[0][index + 2] };
speciesVals = new double[] { data[j + 1][index], data[j + 1][index + 1], data[j + 1][index + 2] };
}
interpolatedValue = MathTestingUtilities.taylorInterpolation(sampleTimes[i], times, speciesVals);
} else {
times = new double[] { data[0][index - 1], data[0][index], data[0][index + 1] };
speciesVals = new double[] { data[j + 1][index - 1], data[j + 1][index], data[j + 1][index + 1] };
interpolatedValue = MathTestingUtilities.taylorInterpolation(sampleTimes[i], times, speciesVals);
}
// // Currently using 1st order interpolation
// times = new double[] { data[0][index], data[0][index+1] };
// speciesVals = new double[] { data[j+1][index], data[j+1][index+1] };
// interpolatedValue = taylorInterpolation(sampleTimes[i], times, speciesVals);
// interpolatedValue = interpolatedValue * unitFactor.getExpression().evaluateConstant(); //earlier, hack unitfactor = 0.000001
// System.out.println("Sample time: " + sampleTimes[i] + ", between time[" + index + "]=" + data[0][index]+" and time["+(index+1)+"]="+(data[0][index+1])+", interpolated = "+interpolatedValue);
outputStream.print("," + interpolatedValue);
}
outputStream.println();
}
}
outputStream.close();
return outputFile;
} catch (Exception e) {
e.printStackTrace(System.out);
// File outputFile = new File(outDir,"results" + filePrefix + ".csv");
throw new SolverException(e.getMessage());
}
}
use of org.vcell.util.exe.Executable in project vcell by virtualcell.
the class BioformatsImageDatasetReader method launchServer.
private void launchServer(int port) {
synchronized (BioformatsImageDatasetReader.class) {
if (serverThread != null) {
return;
}
Runnable runnable = new Runnable() {
@Override
public void run() {
Executable executable = new Executable(new String[] { "java", "-jar", bioformatsExecutableJarFile.getAbsolutePath(), Integer.toString(port) });
try {
executable.start();
} catch (ExecutableException e) {
e.printStackTrace();
serverThread = null;
}
}
};
serverThread = new Thread(runnable, "bioformatsServerThread");
serverThread.setDaemon(true);
serverThread.start();
try {
// give some time for the service to start listening
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
}
use of org.vcell.util.exe.Executable in project vcell by virtualcell.
the class PythonSupport method checkPackage.
private static boolean checkPackage(File pythonExe, PythonPackage pythonPackage) {
String[] cmd;
if (OperatingSystemInfo.getInstance().isWindows()) {
// cmd = new String[] { "cmd", "/C", condaExe.getAbsolutePath(), "-c", "'import "+pythonPackage.pythonModuleName+"'"};
cmd = new String[] { pythonExe.getAbsolutePath(), "-c", "\"import " + pythonPackage.pythonModuleName + "\"" };
} else {
cmd = new String[] { "bash", "-c", pythonExe.getAbsolutePath() + " -c 'import " + pythonPackage.pythonModuleName + "'" };
}
System.out.println(Arrays.asList(cmd).toString());
IExecutable exe = new Executable(cmd);
try {
System.out.println("checking package " + pythonPackage.condaName);
exe.start(new int[] { 0 });
System.out.println("Exit value: " + exe.getExitValue());
System.out.println(exe.getStdoutString());
System.out.println(exe.getStderrString());
return true;
} catch (ExecutableException e) {
// e.printStackTrace();
System.out.println("Exit value: " + exe.getExitValue());
System.out.println(exe.getStdoutString());
System.out.println(exe.getStderrString());
return false;
}
}
use of org.vcell.util.exe.Executable in project vcell by virtualcell.
the class PythonSupport method checkPython.
private static boolean checkPython(File pythonExe) {
String[] cmd;
if (OperatingSystemInfo.getInstance().isWindows()) {
// cmd = new String[] {"cmd", "/C", managedMiniconda.pythonExe.getAbsolutePath(), "--version"};
cmd = new String[] { pythonExe.getAbsolutePath(), "--version" };
} else {
cmd = new String[] { pythonExe.getAbsolutePath(), "--version" };
}
IExecutable exe = new Executable(cmd);
try {
exe.start(new int[] { 0 });
System.out.println("Exit value: " + exe.getExitValue());
System.out.println("stdout=\"" + exe.getStdoutString() + "\"");
System.out.println("stderr=\"" + exe.getStderrString() + "\"");
if (exe.getExitValue() != 0) {
throw new RuntimeException("Python test failed with return code " + exe.getExitValue() + ": " + exe.getStderrString());
}
// }
return true;
} catch (ExecutableException e) {
e.printStackTrace();
throw new RuntimeException("Python test invocation failed: " + e.getMessage(), e);
}
}
use of org.vcell.util.exe.Executable in project vcell by virtualcell.
the class VisitSupport method launchVisToolLinux.
public static void launchVisToolLinux(File visitExecutable) throws ExecutableException, IOException {
File mpirun = null;
if (visitExecutable == null) {
File userDir = new File(System.getProperty("user.home"));
System.out.println(userDir.getAbsolutePath() + " " + userDir.exists());
// find -L ~/ -name 'visit' -executable -type f -print
// 'find -L /home/frm -name 'visit' -executable -type f -print'
Executable exec = new Executable(new String[] { "/bin/sh", "-c", "find -L " + userDir.getAbsolutePath() + " -maxdepth 4 -name 'visit' -executable -type f -print" });
// exec.setWorkingDir(userDir);
exec.start();
System.out.println(exec.getExitValue());
System.out.println(exec.getStdoutString());
// System.out.println(exec.getStderrString());
if (exec.getExitValue() == 0) {
visitExecutable = new File(exec.getStdoutString().trim());
}
// look for mpirun starting 2 levels above visit executable dir location
exec = new Executable(new String[] { "/bin/sh", "-c", "find -L " + visitExecutable.getParentFile().getParent() + " -maxdepth 6 -name 'mpirun' -executable -type f -print" });
exec.start();
System.out.println(exec.getExitValue());
System.out.println(exec.getStdoutString());
System.err.println(exec.getStderrString());
// System.out.println(exec.getStderrString());
if (exec.getExitValue() == 0) {
String mpiStr = exec.getStdoutString();
if (mpiStr != null) {
mpiStr = mpiStr.trim();
}
if (mpiStr != null && mpiStr.length() != 0) {
mpirun = new File(mpiStr);
} else {
mpirun = null;
}
}
if (mpirun == null) {
// See if any mpi run installed
exec = new Executable(new String[] { "/bin/sh", "-c", "which mpirun" });
exec.start();
System.out.println(exec.getExitValue());
System.out.println(exec.getStdoutString());
System.out.println(exec.getStderrString());
if (exec.getExitValue() == 0) {
String mpiStr = exec.getStdoutString();
if (mpiStr != null) {
mpiStr = mpiStr.trim();
}
if (mpiStr != null && mpiStr.length() != 0) {
mpirun = new File(mpiStr);
} else {
mpirun = null;
}
}
}
}
System.out.println(visitExecutable.getAbsolutePath());
if (visitExecutable == null || !visitExecutable.exists() || !visitExecutable.isFile()) {
throw new IOException("visit executable not found");
}
VCellConfiguration.setFileProperty(PropertyLoader.visitExe, visitExecutable);
System.out.println(mpirun.getAbsolutePath());
if (mpirun == null || !mpirun.exists() || !mpirun.isFile()) {
throw new IOException("mpirun executable not found");
}
File visMainCLI = getVisToolPythonScript();
if (!visMainCLI.exists() || !visMainCLI.isFile()) {
throw new IOException("vcell/visit main python file not found, " + visMainCLI.getAbsolutePath());
}
File scriptFile = File.createTempFile("VCellVisitLaunch", ".command");
System.out.println("Launch script location=" + scriptFile.getAbsolutePath());
Executable exec = new Executable(new String[] { /*"/bin/sh","-c",*/
scriptFile.getAbsolutePath() });
//
// get existing environment variables and add visit command and python script to it.
//
Map<String, String> envVariables = System.getenv();
ArrayList<String> envVarList = new ArrayList<String>();
for (String varname : envVariables.keySet()) {
String value = envVariables.get(varname);
if (varname.equals("PATH")) {
value = value + ":" + visitExecutable.getParent();
}
// envVarList.add(varname+"="+value);
exec.addEnvironmentVariable(varname, value);
System.out.println(varname + "=" + value);
}
String visitCommandString = visitExecutable.getPath().replace("\"", "");
System.out.println("Visit Command String = " + visitCommandString);
// envVarList.add(visitCommandString);
if (lg.isInfoEnabled()) {
lg.info("visitcmd=" + visitExecutable.getPath().replace("\"", ""));
}
// envVarList.add("pythonscript="+visMainCLI.getPath());
String pythonScriptString = visMainCLI.getPath();
System.out.println("Python script = " + pythonScriptString);
scriptFile.setExecutable(true);
BufferedWriter writer = new BufferedWriter(new FileWriter(scriptFile));
// export PATH="$HOME/opt/bin:$PATH"
System.out.println("export PATH=\"$PATH:" + mpirun.getParent() + "\"\n");
writer.append("export PATH=\"$PATH:" + mpirun.getParent() + "\"\n");
System.out.println(visitCommandString + " -cli -uifile " + pythonScriptString + "\n");
writer.append(visitCommandString + " -cli -uifile " + pythonScriptString + "\n");
writer.close();
// cli
// mdserver
// engine_ser
new Thread(new Runnable() {
@Override
public void run() {
try {
exec.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
// envVarList.toArray(new String[0]));
if (lg.isInfoEnabled()) {
lg.info("Started VCellVisIt");
}
}
Aggregations