Search in sources :

Example 1 with FieldDataIdentifierSpec

use of cbit.vcell.field.FieldDataIdentifierSpec in project vcell by virtualcell.

the class SmoldynFileWriter method writeDataProcessor.

private void writeDataProcessor() throws DataAccessException, IOException, MathException, DivideByZeroException, ExpressionException {
    Simulation simulation = simTask.getSimulation();
    DataProcessingInstructions dpi = simulation.getDataProcessingInstructions();
    if (dpi == null) {
        printWriter.println(SmoldynVCellMapper.SmoldynKeyword.cmd + " " + SmoldynVCellMapper.SmoldynKeyword.B + " " + VCellSmoldynKeyword.vcellDataProcess + " begin " + DataProcessingInstructions.ROI_TIME_SERIES);
        printWriter.println(SmoldynVCellMapper.SmoldynKeyword.cmd + " " + SmoldynVCellMapper.SmoldynKeyword.B + " " + VCellSmoldynKeyword.vcellDataProcess + " end");
    } else {
        FieldDataIdentifierSpec fdis = dpi.getSampleImageFieldData(simulation.getVersion().getOwner());
        if (fdis == null) {
            throw new DataAccessException("Can't find sample image in data processing instructions");
        }
        File userDirectory = outputFile.getParentFile();
        String secondarySimDataDir = PropertyLoader.getProperty(PropertyLoader.secondarySimDataDirInternalProperty, null);
        DataSetControllerImpl dsci = new DataSetControllerImpl(null, userDirectory.getParentFile(), secondarySimDataDir == null ? null : new File(secondarySimDataDir));
        CartesianMesh origMesh = dsci.getMesh(fdis.getExternalDataIdentifier());
        SimDataBlock simDataBlock = dsci.getSimDataBlock(null, fdis.getExternalDataIdentifier(), fdis.getFieldFuncArgs().getVariableName(), fdis.getFieldFuncArgs().getTime().evaluateConstant());
        VariableType varType = fdis.getFieldFuncArgs().getVariableType();
        VariableType dataVarType = simDataBlock.getVariableType();
        if (!varType.equals(VariableType.UNKNOWN) && !varType.equals(dataVarType)) {
            throw new IllegalArgumentException("field function variable type (" + varType.getTypeName() + ") doesn't match real variable type (" + dataVarType.getTypeName() + ")");
        }
        double[] origData = simDataBlock.getData();
        String filename = SimulationJob.createSimulationJobID(Simulation.createSimulationID(simulation.getKey()), simTask.getSimulationJob().getJobIndex()) + SimulationData.getDefaultFieldDataFileNameForSimulation(fdis.getFieldFuncArgs());
        File fdatFile = new File(userDirectory, filename);
        DataSet.writeNew(fdatFile, new String[] { fdis.getFieldFuncArgs().getVariableName() }, new VariableType[] { simDataBlock.getVariableType() }, new ISize(origMesh.getSizeX(), origMesh.getSizeY(), origMesh.getSizeZ()), new double[][] { origData });
        printWriter.println(SmoldynVCellMapper.SmoldynKeyword.cmd + " " + SmoldynVCellMapper.SmoldynKeyword.B + " " + VCellSmoldynKeyword.vcellDataProcess + " begin " + dpi.getScriptName());
        StringTokenizer st = new StringTokenizer(dpi.getScriptInput(), "\n\r");
        while (st.hasMoreTokens()) {
            String str = st.nextToken();
            if (str.trim().length() > 0) {
                printWriter.println(SmoldynVCellMapper.SmoldynKeyword.cmd + " " + SmoldynVCellMapper.SmoldynKeyword.B + " " + VCellSmoldynKeyword.vcellDataProcess + " " + str);
            }
        }
        printWriter.println(SmoldynVCellMapper.SmoldynKeyword.cmd + " " + SmoldynVCellMapper.SmoldynKeyword.B + " " + VCellSmoldynKeyword.vcellDataProcess + " SampleImageFile " + fdis.getFieldFuncArgs().getVariableName() + " " + fdis.getFieldFuncArgs().getTime().infix() + " " + fdatFile);
        printWriter.println(SmoldynVCellMapper.SmoldynKeyword.cmd + " " + SmoldynVCellMapper.SmoldynKeyword.B + " " + VCellSmoldynKeyword.vcellDataProcess + " end");
    }
}
Also used : VariableType(cbit.vcell.math.VariableType) ISize(org.vcell.util.ISize) CartesianMesh(cbit.vcell.solvers.CartesianMesh) StringTokenizer(java.util.StringTokenizer) Simulation(cbit.vcell.solver.Simulation) SimDataBlock(cbit.vcell.simdata.SimDataBlock) DataProcessingInstructions(cbit.vcell.solver.DataProcessingInstructions) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) DataSetControllerImpl(cbit.vcell.simdata.DataSetControllerImpl) File(java.io.File) DataAccessException(org.vcell.util.DataAccessException)

Example 2 with FieldDataIdentifierSpec

use of cbit.vcell.field.FieldDataIdentifierSpec in project vcell by virtualcell.

the class FRAPOptData method runRefSimulation.

public KeyValue runRefSimulation(final ClientTaskStatusSupport progressListener) throws Exception {
    BioModel bioModel = null;
    if (progressListener != null) {
        progressListener.setMessage("Running Reference Simulation...");
    }
    try {
        FieldDataIdentifierSpec psfFieldFunc = FRAPStudy.getPSFFieldData(getLocalWorkspace());
        bioModel = FRAPStudy.createNewRefBioModel(expFrapStudy, REFERENCE_DIFF_RATE_STR, getRefTimeStep(), LocalWorkspace.createNewKeyValue(), LocalWorkspace.getDefaultOwner(), psfFieldFunc, expFrapStudy.getStartingIndexForRecovery());
        // change time bound and time step
        Simulation sim = bioModel.getSimulations()[0];
        ROIDataGenerator roiDataGenerator = getExpFrapStudy().getROIDataGenerator(getLocalWorkspace());
        sim.getMathDescription().getPostProcessingBlock().addDataGenerator(roiDataGenerator);
        System.out.println("run FRAP Reference Simulation...");
        // run simulation
        FRAPStudy.runFVSolverStandalone_ref(new File(getLocalWorkspace().getDefaultSimDataDirectory()), bioModel.getSimulation(0), getExpFrapStudy().getFrapDataExternalDataInfo().getExternalDataIdentifier(), getExpFrapStudy().getRoiExternalDataInfo().getExternalDataIdentifier(), psfFieldFunc.getExternalDataIdentifier(), progressListener, true);
        KeyValue referenceSimKeyValue = sim.getVersion().getVersionKey();
        return referenceSimKeyValue;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        if (bioModel != null && bioModel.getSimulations() != null) {
            FRAPStudy.removeExternalDataAndSimulationFiles(bioModel.getSimulations()[0].getVersion().getVersionKey(), null, null, getLocalWorkspace());
        }
        throw e;
    }
}
Also used : KeyValue(org.vcell.util.document.KeyValue) Simulation(cbit.vcell.solver.Simulation) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) BioModel(cbit.vcell.biomodel.BioModel) File(java.io.File) UserCancelException(org.vcell.util.UserCancelException)

Example 3 with FieldDataIdentifierSpec

use of cbit.vcell.field.FieldDataIdentifierSpec 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 FieldDataIdentifierSpec

use of cbit.vcell.field.FieldDataIdentifierSpec in project vcell by virtualcell.

the class ROIDataGenerator method getSampleImageFieldData.

public FieldDataIdentifierSpec getSampleImageFieldData(User user) {
    // key
    String key = fieldDataKey.toString();
    String fieldInput = fieldFuncArguments.infix();
    StringTokenizer st = null;
    int index = fieldInput.indexOf(FieldFunctionDefinition.FUNCTION_name);
    if (index >= 0) {
        st = new StringTokenizer(fieldInput.substring(index), "\n");
        if (st.hasMoreTokens()) {
            String fieldFunction = st.nextToken();
            try {
                Expression exp = new Expression(fieldFunction);
                FieldFunctionArguments[] ffa = FieldUtilities.getFieldFunctionArguments(exp);
                return new FieldDataIdentifierSpec(ffa[0], new ExternalDataIdentifier(KeyValue.fromString(key), user, ffa[0].getFieldName()));
            } catch (ExpressionException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("Failed to load data processing script.");
            }
        }
    }
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) Expression(cbit.vcell.parser.Expression) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) ExternalDataIdentifier(org.vcell.util.document.ExternalDataIdentifier) ExpressionException(cbit.vcell.parser.ExpressionException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException)

Example 5 with FieldDataIdentifierSpec

use of cbit.vcell.field.FieldDataIdentifierSpec in project vcell by virtualcell.

the class SimulationTaskMessage method toMessage.

/**
 * Insert the method's description here.
 * Creation date: (12/31/2003 11:08:17 AM)
 * @return javax.jms.Message
 * @param session cbit.vcell.messaging.VCellSession
 * @throws VCMessagingException
 */
private VCMessage toMessage(VCMessageSession session) throws VCMessagingException {
    VCMessage message;
    try {
        message = session.createTextMessage(XmlHelper.simTaskToXML(simTask));
    } catch (XmlParseException e) {
        e.printStackTrace(System.out);
        throw new VCMessagingException("failed to restore Simulation Task from XML", e);
    }
    // must have
    message.setStringProperty(VCMessagingConstants.MESSAGE_TYPE_PROPERTY, MessageConstants.MESSAGE_TYPE_SIMULATION_JOB_VALUE);
    // must have
    message.setIntProperty(MessageConstants.JOBINDEX_PROPERTY, simTask.getSimulationJob().getJobIndex());
    // must have
    message.setIntProperty(MessageConstants.TASKID_PROPERTY, simTask.getTaskID());
    // might be used to remove from the job queue when do stopSimulation
    message.setStringProperty(VCMessagingConstants.USERNAME_PROPERTY, simTask.getUserName());
    // might be used to remove from the job queue when do stopSimulation
    message.setLongProperty(MessageConstants.SIMKEY_PROPERTY, Long.parseLong(simTask.getSimKey() + ""));
    // for worker message filter
    message.setDoubleProperty(MessageConstants.SIZE_MB_PROPERTY, simTask.getEstimatedMemorySizeMB());
    if (simTask.getComputeResource() != null) {
        // for worker message filter
        message.setStringProperty(MessageConstants.COMPUTE_RESOURCE_PROPERTY, simTask.getComputeResource());
    }
    FieldDataIdentifierSpec[] fieldDataIDs = simTask.getSimulationJob().getFieldDataIdentifierSpecs();
    if (fieldDataIDs != null && fieldDataIDs.length > 0) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < fieldDataIDs.length; i++) {
            sb.append(fieldDataIDs[i].toCSVString() + "\n");
        }
        message.setStringProperty(MessageConstants.FIELDDATAID_PROPERTY, sb.toString());
    }
    return message;
}
Also used : VCMessage(cbit.vcell.message.VCMessage) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) XmlParseException(cbit.vcell.xml.XmlParseException) VCMessagingException(cbit.vcell.message.VCMessagingException)

Aggregations

FieldDataIdentifierSpec (cbit.vcell.field.FieldDataIdentifierSpec)26 FieldFunctionArguments (cbit.vcell.field.FieldFunctionArguments)15 DataAccessException (org.vcell.util.DataAccessException)11 SimulationJob (cbit.vcell.solver.SimulationJob)10 File (java.io.File)10 Simulation (cbit.vcell.solver.Simulation)9 ExternalDataIdentifier (org.vcell.util.document.ExternalDataIdentifier)9 Expression (cbit.vcell.parser.Expression)8 SimulationTask (cbit.vcell.messaging.server.SimulationTask)7 VariableType (cbit.vcell.math.VariableType)6 ExpressionException (cbit.vcell.parser.ExpressionException)6 UserCancelException (org.vcell.util.UserCancelException)6 ImageException (cbit.image.ImageException)5 DataSetControllerImpl (cbit.vcell.simdata.DataSetControllerImpl)5 SolverStatus (cbit.vcell.solver.server.SolverStatus)5 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)5 SimDataBlock (cbit.vcell.simdata.SimDataBlock)4 CartesianMesh (cbit.vcell.solvers.CartesianMesh)4 IOException (java.io.IOException)4 StringTokenizer (java.util.StringTokenizer)4