Skip to content

Commit 5090ce8

Browse files
committed
use prebuilt SImID_SIMULATIONKEY_JOBINDEX_ fdata files if exists.
1 parent 9856808 commit 5090ce8

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

vcell-native/src/main/java/org/vcell/libvcell/SolverUtils.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import cbit.vcell.messaging.server.SimulationTask;
1313
import cbit.vcell.mongodb.VCMongoMessage;
1414
import cbit.vcell.parser.ExpressionException;
15+
import cbit.vcell.simdata.SimulationData;
1516
import cbit.vcell.solver.*;
1617
import cbit.vcell.xml.XMLSource;
1718
import cbit.vcell.xml.XmlHelper;
@@ -30,6 +31,7 @@
3031
import java.io.ByteArrayInputStream;
3132
import java.io.File;
3233
import java.io.InputStream;
34+
import java.math.BigDecimal;
3335
import java.util.ArrayList;
3436
import java.util.List;
3537

@@ -49,24 +51,57 @@ public static void vcmlToFiniteVolumeInput(String vcml_content, String simulatio
4951
if (sim == null) {
5052
throw new IllegalArgumentException("Simulation not found: " + simulation_name);
5153
}
52-
FieldDataIdentifierSpec[] fdiSpecs = getFieldDataIdentifierSpecs(sim, parentDir);
54+
FieldDataIdentifierSpec[] fdiSpecs = getFieldDataIdentifierSpecs(sim, outputDir, parentDir);
5355

5456
TempSimulation tempSimulation = new TempSimulation(sim, false);
5557
tempSimulation.setSimulationOwner(sim.getSimulationOwner());
5658
SimulationJob tempSimulationJob = new SimulationJob(tempSimulation, 0, fdiSpecs);
59+
60+
renameExistingFieldDataFiles(tempSimulation.getKey(), tempSimulationJob.getJobIndex(), outputDir);
61+
5762
SimulationTask simTask = new SimulationTask(tempSimulationJob, 0);
5863
LocalFVSolverStandalone solver = new LocalFVSolverStandalone(simTask, outputDir);
5964
solver.initialize();
6065
}
6166

62-
private static FieldDataIdentifierSpec[] getFieldDataIdentifierSpecs(Simulation sim, File parentDir) throws MathException, ExpressionException {
67+
private static void renameExistingFieldDataFiles(KeyValue tempSimKey, int jobId, File outputDir) {
68+
File[] files = outputDir.listFiles();
69+
if (files != null) {
70+
for (File file : files) {
71+
if (file.getName().startsWith("SimID_SIMULATIONKEY_JOBINDEX_")) {
72+
String newName = file.getName().replace("SIMULATIONKEY",tempSimKey.toString()).replace("JOBINDEX",String.valueOf(jobId));
73+
File newFile = new File(outputDir, newName);
74+
if (!file.renameTo(newFile)){
75+
throw new RuntimeException("Could not rename " + file.getName() + " to " + newFile.getAbsolutePath());
76+
}
77+
}
78+
}
79+
}
80+
}
81+
82+
private static FieldDataIdentifierSpec[] getFieldDataIdentifierSpecs(Simulation sim, File outputDir, File parentDir) throws MathException, ExpressionException {
6383
FieldDataIdentifierSpec[] fdiSpecs = null;
6484
FieldFunctionArguments[] fieldFuncArgs = FieldUtilities.getFieldFunctionArguments(sim.getMathDescription());
6585
if (fieldFuncArgs != null) {
6686
List<FieldDataIdentifierSpec> fdiSpecList = new ArrayList<>();
6787
for (FieldFunctionArguments fieldFuncArg : fieldFuncArgs) {
6888
if (fieldFuncArg != null) {
6989
String name = fieldFuncArg.getFieldName();
90+
//
91+
// First, check if the resampled field data files are already present (e.g. if pyvcell wrote the files directly from image data)
92+
//
93+
ExternalDataIdentifier fakeExtDataId = new ExternalDataIdentifier(sim.getKey(), User.tempUser, name);
94+
String fieldDataFileName = SimulationData.createCanonicalResampleFileName(fakeExtDataId, fieldFuncArg);
95+
fieldDataFileName = fieldDataFileName.replace("SimID_" + sim.getKey().toString() + "_0_", "SimID_SIMULATIONKEY_JOBINDEX_");
96+
File preexistingFieldDataFile = new File(outputDir, fieldDataFileName);
97+
if (preexistingFieldDataFile.exists()) {
98+
fdiSpecList.add(new FieldDataIdentifierSpec(fieldFuncArg, fakeExtDataId));
99+
continue;
100+
}
101+
102+
//
103+
// If not, check if the field data directory exists as a subdirectory of the parentDir - holding simulation results.
104+
//
70105
File fieldDataDir = new File(parentDir, name);
71106
if (!fieldDataDir.exists()) {
72107
throw new IllegalArgumentException("Field data directory does not exist: " + fieldDataDir.getAbsolutePath());

vcell-native/src/main/java/org/vcell/libvcell/solvers/LocalFiniteVolumeFileWriter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import cbit.vcell.solver.Simulation;
1818
import cbit.vcell.solvers.FiniteVolumeFileWriter;
1919
import org.vcell.util.DataAccessException;
20+
import org.vcell.util.document.ExternalDataIdentifier;
21+
import org.vcell.util.document.User;
2022

2123
import java.io.File;
2224
import java.io.IOException;
@@ -106,7 +108,8 @@ private VariableType getVariableTypeFromFieldDataFiles(FieldDataIdentifierSpec f
106108
// First, look to see if the processed field data file already exists, if so, use it to determine the variable type
107109
//
108110
DataSet dataSet = new DataSet();
109-
File existingFieldDataFile = new File(workingDirectory, SimulationData.createCanonicalResampleFileName(fieldDataIDSpec.getExternalDataIdentifier(), ffa));
111+
ExternalDataIdentifier existingFieldDataID = new ExternalDataIdentifier(this.simTask.getSimKey(), User.tempUser, ffa.getFieldName());
112+
File existingFieldDataFile = new File(workingDirectory, SimulationData.createCanonicalResampleFileName(existingFieldDataID, ffa));
110113
if (existingFieldDataFile.exists()) {
111114
// field data file may already exist
112115
// 1. from pyvcell writing the field data file directly into this directory for an image-based field data

vcell-native/src/test/java/org/vcell/libvcell/SolverEntrypointsTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ public void testSbmlToFiniteVolumeInput_vcml_instead() throws IOException {
5050
assertEquals("expecting SBML content, not VCML", exc.getMessage());
5151
}
5252

53+
@Test
54+
public void testVcmlToFiniteVolumeInput_field_data_already_sampled() throws SolverException, ExpressionException, MappingException, IOException, XmlParseException, MathException, InterruptedException {
55+
String vcmlContent = getFileContentsAsString("/FieldDataDemo.vcml");
56+
File parent_dir = Files.createTempDirectory("vcmlToFiniteVolumeInput_"+UUID.randomUUID()).toFile();
57+
File output_dir = new File(parent_dir, "output_dir");
58+
assertEquals(0, countFiles(output_dir));
59+
// prepopulate the output_dir with the resampled field data files, should use these instead.
60+
extractTgz(SolverEntrypointsTest.class.getResourceAsStream("/test2_lsm_DEMO_resampled.tgz"), output_dir);
61+
listFilesInDirectory(output_dir);
62+
assertEquals(2, countFiles(output_dir));
63+
64+
String simulationName = "Simulation0";
65+
vcmlToFiniteVolumeInput(vcmlContent, simulationName, parent_dir, output_dir);
66+
listFilesInDirectory(output_dir);
67+
assertEquals(6, countFiles(output_dir));
68+
}
69+
5370
@Test
5471
public void testVcmlToFiniteVolumeInput_field_data() throws SolverException, ExpressionException, MappingException, IOException, XmlParseException, MathException, InterruptedException {
5572
String vcmlContent = getFileContentsAsString("/FieldDataDemo.vcml");
@@ -70,7 +87,7 @@ public void testVcmlToFiniteVolumeInput_field_data() throws SolverException, Exp
7087
}
7188

7289
@Test
73-
public void testVcmlToFiniteVolumeInput_field_data_not_found() throws SolverException, ExpressionException, MappingException, IOException, XmlParseException, MathException, InterruptedException {
90+
public void testVcmlToFiniteVolumeInput_field_data_not_found() throws IOException {
7491
String vcmlContent = getFileContentsAsString("/FieldDataDemo.vcml");
7592
File parent_dir = Files.createTempDirectory("vcmlToFiniteVolumeInput_"+UUID.randomUUID()).toFile();
7693
File output_dir = new File(parent_dir, "output_dir");
76.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)