1212import cbit .vcell .messaging .server .SimulationTask ;
1313import cbit .vcell .mongodb .VCMongoMessage ;
1414import cbit .vcell .parser .ExpressionException ;
15+ import cbit .vcell .simdata .SimulationData ;
1516import cbit .vcell .solver .*;
1617import cbit .vcell .xml .XMLSource ;
1718import cbit .vcell .xml .XmlHelper ;
3031import java .io .ByteArrayInputStream ;
3132import java .io .File ;
3233import java .io .InputStream ;
34+ import java .math .BigDecimal ;
3335import java .util .ArrayList ;
3436import 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 ());
0 commit comments