Search in sources :

Example 11 with FieldFunctionArguments

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

the class DataSetControllerImpl method fieldFunctionSubstitution.

private Expression fieldFunctionSubstitution(OutputContext outputContext, final VCDataIdentifier vcdID, Expression functionExpression) throws ExpressionException, DataAccessException, IOException, MathException {
    SimResampleInfoProvider simResampleInfoProvider = null;
    Expression origExpression = new Expression(functionExpression);
    if (vcdID instanceof VCSimulationDataIdentifier) {
        simResampleInfoProvider = ((VCSimulationDataIdentifier) vcdID);
    } else if (vcdID instanceof VCSimulationDataIdentifierOldStyle) {
        simResampleInfoProvider = ((VCSimulationDataIdentifierOldStyle) vcdID);
    } else if (vcdID instanceof ExternalDataIdentifier) {
        simResampleInfoProvider = ((ExternalDataIdentifier) vcdID);
    } else {
        return origExpression;
    }
    FieldFunctionArguments[] fieldfuncArgumentsArr = FieldUtilities.getFieldFunctionArguments(origExpression);
    if (fieldfuncArgumentsArr == null || fieldfuncArgumentsArr.length == 0) {
        return origExpression;
    }
    String[] origSymbols = origExpression.getSymbols();
    Vector<SymbolTableEntry> originalSymbolTablEntrryV = new Vector<SymbolTableEntry>();
    for (int i = 0; origSymbols != null && i < origSymbols.length; i++) {
        if (!originalSymbolTablEntrryV.contains(origExpression.getSymbolBinding(origSymbols[i]))) {
            originalSymbolTablEntrryV.add(origExpression.getSymbolBinding(origSymbols[i]));
        }
    }
    Expression exp = new Expression(origExpression);
    // 
    // Handle Field Data Function field(...)
    // 
    double[][] resampledFieldDatas = null;
    HashMap<String, Integer> substSymbolIndexH = new HashMap<String, Integer>();
    // if(fieldfuncArgumentsArr != null && fieldfuncArgumentsArr.length > 0){
    FieldDataIdentifierSpec[] fieldDataIdentifierSpecArr = getFieldDataIdentifierSpecs(fieldfuncArgumentsArr, simResampleInfoProvider.getOwner());
    // Substitute Field Data Functions for simple symbols for lookup-------
    for (int i = 0; i < fieldfuncArgumentsArr.length; i += 1) {
        for (int j = 0; j < fieldDataIdentifierSpecArr.length; j++) {
            if (fieldfuncArgumentsArr[i].equals(fieldDataIdentifierSpecArr[j].getFieldFuncArgs())) {
                String substFieldName = fieldfuncArgumentsArr[i].getFieldName() + "_" + fieldfuncArgumentsArr[i].getVariableName() + "_" + fieldfuncArgumentsArr[i].getTime().evaluateConstant();
                substFieldName = TokenMangler.fixTokenStrict(substFieldName);
                if (exp.hasSymbol(substFieldName)) {
                    throw new DataAccessException("Substitute Field data name is not unique");
                }
                String fieldFuncString = SimulationData.createCanonicalFieldFunctionSyntax(fieldDataIdentifierSpecArr[j].getExternalDataIdentifier().getName(), fieldfuncArgumentsArr[i].getVariableName(), fieldfuncArgumentsArr[i].getTime().evaluateConstant(), fieldfuncArgumentsArr[i].getVariableType().getTypeName());
                exp.substituteInPlace(new Expression(fieldFuncString), new Expression(substFieldName));
                substSymbolIndexH.put(substFieldName, i);
                break;
            }
        }
    }
    // ----------------------------------------------------------------------
    boolean[] bResample = new boolean[fieldDataIdentifierSpecArr.length];
    Arrays.fill(bResample, true);
    writeFieldFunctionData(outputContext, fieldDataIdentifierSpecArr, bResample, getMesh(simResampleInfoProvider), simResampleInfoProvider, getMesh(simResampleInfoProvider).getNumMembraneElements(), FVSolverStandalone.HESM_KEEP_AND_CONTINUE);
    resampledFieldDatas = new double[fieldfuncArgumentsArr.length][];
    for (int i = 0; i < fieldfuncArgumentsArr.length; i += 1) {
        // File resampledFile =
        // new File(getUserDir(vcsdID.getOwner()),
        // vcsdID.getID()+
        // FieldDataIdentifierSpec.getDefaultFieldDataFileNameForSimulation(fieldfuncArgumentsArr[i])
        // );
        // File resampledFile = new File(getPrimaryUserDir(simResampleInfoProvider.getOwner(), true),
        // SimulationData.createCanonicalResampleFileName(
        // simResampleInfoProvider, fieldfuncArgumentsArr[i]));
        File resampledFile = ((SimulationData) getVCData(simResampleInfoProvider)).getFieldDataFile(simResampleInfoProvider, fieldfuncArgumentsArr[i]);
        resampledFieldDatas[i] = DataSet.fetchSimData(fieldfuncArgumentsArr[i].getVariableName(), resampledFile);
    }
    // }
    // Rebind all the symbols
    String[] dependentIDs = exp.getSymbols();
    VariableSymbolTable varSymbolTable = new VariableSymbolTable();
    for (int i = 0; dependentIDs != null && i < dependentIDs.length; i++) {
        SymbolTableEntry newSymbolTableEntry = null;
        for (int j = 0; j < originalSymbolTablEntrryV.size(); j++) {
            if (originalSymbolTablEntrryV.elementAt(j).getName().equals(dependentIDs[i])) {
                newSymbolTableEntry = originalSymbolTablEntrryV.elementAt(j);
                break;
            }
        }
        if (newSymbolTableEntry == null) {
            if (substSymbolIndexH.containsKey(dependentIDs[i])) {
                int resampledDataIndex = substSymbolIndexH.get(dependentIDs[i]).intValue();
                FieldDataParameterVariable fieldDataParameterVariable = new FieldDataParameterVariable(dependentIDs[i], resampledFieldDatas[resampledDataIndex]);
                newSymbolTableEntry = fieldDataParameterVariable;
            }
        }
        if (newSymbolTableEntry == null) {
            throw new DataAccessException("Field Data Couldn't find substituted expression while evaluating function");
        }
        varSymbolTable.addVar(newSymbolTableEntry);
    }
    exp.bindExpression(varSymbolTable);
    return exp;
}
Also used : HashMap(java.util.HashMap) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) ExternalDataIdentifier(org.vcell.util.document.ExternalDataIdentifier) VariableSymbolTable(cbit.vcell.parser.VariableSymbolTable) Vector(java.util.Vector) DataAccessException(org.vcell.util.DataAccessException) FieldDataParameterVariable(cbit.vcell.field.FieldDataParameterVariable) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) SimResampleInfoProvider(org.vcell.util.document.SimResampleInfoProvider) VCSimulationDataIdentifier(cbit.vcell.solver.VCSimulationDataIdentifier) Expression(cbit.vcell.parser.Expression) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) VCSimulationDataIdentifierOldStyle(cbit.vcell.solver.VCSimulationDataIdentifierOldStyle) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 12 with FieldFunctionArguments

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

the class SimulationSymbolTable method getFunctionVariableType.

/**
 * Insert the method's description here.
 * Creation date: (2/19/2004 11:17:15 AM)
 * @return cbit.vcell.simdata.VariableType
 * @param function cbit.vcell.math.Function
 * @param variableNames java.lang.String[]
 * @param variableTypes cbit.vcell.simdata.VariableType[]
 */
public static VariableType getFunctionVariableType(Function function, MathDescription mathDescription, String[] variableNames, VariableType[] variableTypes, boolean isSpatial) throws InconsistentDomainException {
    if (!isSpatial) {
        return VariableType.NONSPATIAL;
    }
    VariableType domainFuncType = null;
    // initial guess, restrict variable type to be consistent with domain.
    if (function.getDomain() != null) {
        String domainName = function.getDomain().getName();
        if (mathDescription != null) {
            SubDomain subdomain = mathDescription.getSubDomain(domainName);
            if (subdomain instanceof MembraneSubDomain) {
                domainFuncType = VariableType.MEMBRANE_REGION;
            } else {
                domainFuncType = VariableType.VOLUME_REGION;
            }
        }
    }
    Expression exp = function.getExpression();
    String[] symbols = exp.getSymbols();
    ArrayList<VariableType> varTypeList = new ArrayList<VariableType>();
    boolean bExplicitFunctionOfSpace = false;
    if (symbols != null) {
        for (int j = 0; j < symbols.length; j++) {
            if (symbols[j].equals(ReservedVariable.X.getName()) || symbols[j].equals(ReservedVariable.Y.getName()) || symbols[j].equals(ReservedVariable.Z.getName())) {
                bExplicitFunctionOfSpace = true;
                continue;
            }
            for (int k = 0; k < variableNames.length; k++) {
                if (symbols[j].equals(variableNames[k])) {
                    varTypeList.add(variableTypes[k]);
                    break;
                } else if (symbols[j].equals(variableNames[k] + InsideVariable.INSIDE_VARIABLE_SUFFIX) || symbols[j].equals(variableNames[k] + OutsideVariable.OUTSIDE_VARIABLE_SUFFIX)) {
                    if (variableTypes[k].equals(VariableType.VOLUME)) {
                        varTypeList.add(VariableType.MEMBRANE);
                    } else if (variableTypes[k].equals(VariableType.VOLUME_REGION)) {
                        varTypeList.add(VariableType.MEMBRANE_REGION);
                    }
                    break;
                }
            }
        }
    }
    // Size Functions
    Set<FunctionInvocation> sizeFunctionInvocationSet = SolverUtilities.getSizeFunctionInvocations(function.getExpression());
    for (FunctionInvocation fi : sizeFunctionInvocationSet) {
        String functionName = fi.getFunctionName();
        if (functionName.equals(MathFunctionDefinitions.Function_regionArea_current.getFunctionName())) {
            varTypeList.add(VariableType.MEMBRANE_REGION);
        } else if (functionName.equals(MathFunctionDefinitions.Function_regionVolume_current.getFunctionName())) {
            varTypeList.add(VariableType.VOLUME_REGION);
        }
    }
    // Membrane Normal Functions
    FunctionInvocation[] functionInvocations = function.getExpression().getFunctionInvocations(null);
    for (FunctionInvocation fi : functionInvocations) {
        String functionName = fi.getFunctionName();
        if (functionName.equals(MathFunctionDefinitions.Function_normalX.getFunctionName()) || functionName.equals(MathFunctionDefinitions.Function_normalY.getFunctionName())) {
            varTypeList.add(VariableType.MEMBRANE);
        }
    }
    FieldFunctionArguments[] fieldFuncArgs = FieldUtilities.getFieldFunctionArguments(function.getExpression());
    if (fieldFuncArgs != null && fieldFuncArgs.length > 0) {
        varTypeList.add(fieldFuncArgs[0].getVariableType());
    }
    VariableType funcType = domainFuncType;
    for (VariableType vt : varTypeList) {
        if (funcType == null) {
            funcType = vt;
        } else {
            // 
            if (vt.isExpansionOf(funcType)) {
                funcType = vt;
            } else if (vt.equals(VariableType.VOLUME)) {
                if (funcType.equals(VariableType.MEMBRANE_REGION)) {
                    funcType = VariableType.MEMBRANE;
                }
            } else if (vt.equals(VariableType.VOLUME_REGION)) {
            } else if (vt.equals(VariableType.MEMBRANE)) {
                if (domainFuncType != null && domainFuncType.getVariableDomain().equals(VariableDomain.VARIABLEDOMAIN_VOLUME)) {
                    throw new InconsistentDomainException("Function '" + function.getName() + "' defined on a volume subdomain '" + function.getDomain().getName() + "' references a variable or a function defined on a membrane subdomain");
                }
            } else if (vt.equals(VariableType.MEMBRANE_REGION)) {
                if (funcType.equals(VariableType.VOLUME)) {
                    if (domainFuncType != null && domainFuncType.getVariableDomain().equals(VariableDomain.VARIABLEDOMAIN_VOLUME)) {
                        throw new InconsistentDomainException("Function '" + function.getName() + "' defined on '" + function.getDomain().getName() + "' references a size function defined on a membrane");
                    }
                    funcType = VariableType.MEMBRANE;
                } else if (funcType.equals(VariableType.VOLUME_REGION)) {
                    if (domainFuncType != null && domainFuncType.getVariableDomain().equals(VariableDomain.VARIABLEDOMAIN_VOLUME)) {
                        throw new InconsistentDomainException("Function '" + function.getName() + "' defined on '" + function.getDomain().getName() + "' references a size function defined on a membrane");
                    }
                    funcType = VariableType.MEMBRANE_REGION;
                }
            } else if (vt.incompatibleWith(funcType)) {
                throw new InconsistentDomainException("Function domains conflict between variable domains '" + vt.getDefaultLabel() + "' and '" + funcType.getDefaultLabel() + " for function " + function.getName());
            }
        }
    }
    // 
    if (funcType != null && bExplicitFunctionOfSpace) {
        if (funcType.equals(VariableType.MEMBRANE_REGION)) {
            funcType = VariableType.MEMBRANE;
        } else if (funcType.equals(VariableType.VOLUME_REGION)) {
            funcType = VariableType.VOLUME;
        } else if (funcType.equals(VariableType.CONTOUR_REGION)) {
            funcType = VariableType.CONTOUR;
        }
    }
    if (funcType == null) {
        // no knowledge from expression, default variable type
        return VariableType.VOLUME;
    }
    return funcType;
}
Also used : MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) FunctionInvocation(cbit.vcell.parser.FunctionInvocation) VariableType(cbit.vcell.math.VariableType) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) ArrayList(java.util.ArrayList) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Expression(cbit.vcell.parser.Expression) InconsistentDomainException(cbit.vcell.math.InconsistentDomainException)

Example 13 with FieldFunctionArguments

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

the class RunRefSimulationFastOp method runRefSimulation.

private RowColumnResultSet runRefSimulation(ROI cellROI, ROI[] imageDataROIs, UShortImage psf, FloatImage initRefConc, double experimentalRecoveryTime, LocalWorkspace localWorkspace, ClientTaskStatusSupport progressListener) throws Exception {
    User owner = LocalWorkspace.getDefaultOwner();
    KeyValue simKey = LocalWorkspace.createNewKeyValue();
    // 
    // save first image from normalized time series as the initial concentration field data
    // 
    ExternalDataInfo initialConcentrationExtData = createNewExternalDataInfo(localWorkspace, INITCONC_EXTDATA_NAME);
    Extent extent = initRefConc.getExtent();
    Origin origin = initRefConc.getOrigin();
    ISize isize = new ISize(initRefConc.getNumX(), initRefConc.getNumY(), initRefConc.getNumZ());
    saveExternalData(initRefConc, INITCONC_EXTDATA_VARNAME, initialConcentrationExtData.getExternalDataIdentifier(), localWorkspace);
    FieldFunctionArguments initConditionFFA = new FieldFunctionArguments(INITCONC_EXTDATA_NAME, INITCONC_EXTDATA_VARNAME, new Expression(0.0), VariableType.VOLUME);
    // 
    // save ROIs as a multivariate field data
    // 
    ExternalDataInfo roiExtData = createNewExternalDataInfo(localWorkspace, ROI_EXTDATA_NAME);
    saveROIsAsExternalData(imageDataROIs, localWorkspace, roiExtData.getExternalDataIdentifier());
    ArrayList<FieldFunctionArguments> roiFFAs = new ArrayList<FieldFunctionArguments>();
    for (ROI roi : imageDataROIs) {
        roiFFAs.add(new FieldFunctionArguments(ROI_EXTDATA_NAME, ROI_MASK_NAME_PREFIX + roi.getROIName(), new Expression(0.0), VariableType.VOLUME));
    }
    // 
    // save PSF as a field data
    // 
    ExternalDataInfo psfExtData = createNewExternalDataInfo(localWorkspace, PSF_EXTDATA_NAME);
    savePsfAsExternalData(psf, PSF_EXTDATA_VARNAME, psfExtData.getExternalDataIdentifier(), localWorkspace);
    FieldFunctionArguments psfFFA = new FieldFunctionArguments(PSF_EXTDATA_NAME, PSF_EXTDATA_VARNAME, new Expression(0.0), VariableType.VOLUME);
    TimeBounds timeBounds = getEstimatedRefTimeBound(experimentalRecoveryTime);
    double timeStepVal = REFERENCE_DIFF_DELTAT;
    Expression chirpedDiffusionRate = new Expression(REFERENCE_DIFF_RATE_COEFFICIENT + "*(t+" + REFERENCE_DIFF_DELTAT + ")");
    BioModel bioModel = createRefSimBioModel(simKey, owner, origin, extent, cellROI, timeStepVal, timeBounds, VAR_NAME, new Expression(initConditionFFA.infix()), psfFFA, chirpedDiffusionRate);
    if (progressListener != null) {
        progressListener.setMessage("Running Reference Simulation...");
    }
    // run simulation
    Simulation simulation = bioModel.getSimulation(0);
    ROIDataGenerator roiDataGenerator = getROIDataGenerator(localWorkspace, imageDataROIs);
    simulation.getMathDescription().getPostProcessingBlock().addDataGenerator(roiDataGenerator);
    runFVSolverStandalone(new File(localWorkspace.getDefaultSimDataDirectory()), simulation, initialConcentrationExtData.getExternalDataIdentifier(), roiExtData.getExternalDataIdentifier(), psfExtData.getExternalDataIdentifier(), progressListener, true);
    KeyValue referenceSimKeyValue = simulation.getVersion().getVersionKey();
    VCSimulationIdentifier vcSimID = new VCSimulationIdentifier(referenceSimKeyValue, LocalWorkspace.getDefaultOwner());
    VCSimulationDataIdentifier vcSimDataID = new VCSimulationDataIdentifier(vcSimID, 0);
    File hdf5File = new File(localWorkspace.getDefaultSimDataDirectory(), vcSimDataID.getID() + SimDataConstants.DATA_PROCESSING_OUTPUT_EXTENSION_HDF5);
    // get post processing info (time points, variable sizes)
    DataOperation.DataProcessingOutputInfoOP dataOperationInfo = new DataOperation.DataProcessingOutputInfoOP(null, /*no vcDataIdentifier OK*/
    false, null);
    DataOperationResults.DataProcessingOutputInfo dataProcessingOutputInfo = (DataOperationResults.DataProcessingOutputInfo) DataSetControllerImpl.getDataProcessingOutput(dataOperationInfo, hdf5File);
    // get post processing data
    DataOperation.DataProcessingOutputDataValuesOP dataOperationDataValues = new DataOperation.DataProcessingOutputDataValuesOP(null, /*no vcDataIdentifier OK*/
    ROI_EXTDATA_NAME, TimePointHelper.createAllTimeTimePointHelper(), DataIndexHelper.createSliceDataIndexHelper(0), null, null);
    DataOperationResults.DataProcessingOutputDataValues dataProcessingOutputDataValues = (DataOperationResults.DataProcessingOutputDataValues) DataSetControllerImpl.getDataProcessingOutput(dataOperationDataValues, hdf5File);
    // 
    // delete the simulation files
    // 
    // 
    // remove reference simulation files and field data files
    // 
    File userDir = new File(localWorkspace.getDefaultSimDataDirectory());
    File[] oldSimFilesToDelete = getSimulationFileNames(userDir, referenceSimKeyValue);
    for (int i = 0; oldSimFilesToDelete != null && i < oldSimFilesToDelete.length; i++) {
        oldSimFilesToDelete[i].delete();
    }
    deleteCanonicalExternalData(localWorkspace, initialConcentrationExtData.getExternalDataIdentifier());
    deleteCanonicalExternalData(localWorkspace, roiExtData.getExternalDataIdentifier());
    deleteCanonicalExternalData(localWorkspace, roiExtData.getExternalDataIdentifier());
    // get ref sim time points (distorted by time dilation acceleration)
    double[] rawRefDataTimePoints = dataProcessingOutputInfo.getVariableTimePoints();
    // get shifted time points
    double[] correctedRefDataTimePoints = shiftTimeForBaseDiffRate(rawRefDataTimePoints);
    double[][] refData = dataProcessingOutputDataValues.getDataValues();
    // 
    // for rowColumnResultSet with { "t", "roi1", .... , "roiN" } for reference data
    // 
    int numROIs = imageDataROIs.length;
    String[] columnNames = new String[numROIs + 1];
    columnNames[0] = "t";
    for (int i = 0; i < numROIs; i++) {
        columnNames[i + 1] = imageDataROIs[i].getROIName();
    }
    RowColumnResultSet reducedData = new RowColumnResultSet(columnNames);
    for (int i = 0; i < correctedRefDataTimePoints.length; i++) {
        double[] row = new double[numROIs + 1];
        row[0] = correctedRefDataTimePoints[i];
        double[] roiData = refData[i];
        for (int j = 0; j < numROIs; j++) {
            // roiData[0] is the average over the cell .. postbleach this shouldn't change for pure diffusion
            row[j + 1] = roiData[j + 1];
        }
        reducedData.addRow(row);
    }
    return reducedData;
}
Also used : Origin(org.vcell.util.Origin) VCSimulationIdentifier(cbit.vcell.solver.VCSimulationIdentifier) User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) Extent(org.vcell.util.Extent) ISize(org.vcell.util.ISize) ArrayList(java.util.ArrayList) TimeBounds(cbit.vcell.solver.TimeBounds) RowColumnResultSet(cbit.vcell.math.RowColumnResultSet) DataOperation(cbit.vcell.simdata.DataOperation) ExternalDataInfo(org.vcell.vmicro.workflow.data.ExternalDataInfo) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) ROI(cbit.vcell.VirtualMicroscopy.ROI) VCSimulationDataIdentifier(cbit.vcell.solver.VCSimulationDataIdentifier) ROIDataGenerator(org.vcell.vmicro.workflow.data.ROIDataGenerator) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) BioModel(cbit.vcell.biomodel.BioModel) DataOperationResults(cbit.vcell.simdata.DataOperationResults) File(java.io.File)

Example 14 with FieldFunctionArguments

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

the class RunRefSimulationFastOp method runFVSolverStandalone.

private void runFVSolverStandalone(File simulationDataDir, Simulation sim, ExternalDataIdentifier initialConditionExtDataID, ExternalDataIdentifier roiExtDataID, ExternalDataIdentifier psfExtDataID, 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(initialConditionExtDataID.getName())) {
            fieldDataIdentifierSpecs[i] = new FieldDataIdentifierSpec(fieldFunctionArgs[i], initialConditionExtDataID);
        } else if (fieldFunctionArgs[i].getFieldName().equals(roiExtDataID.getName())) {
            fieldDataIdentifierSpecs[i] = new FieldDataIdentifierSpec(fieldFunctionArgs[i], roiExtDataID);
        } else if (fieldFunctionArgs[i].getFieldName().equals(psfExtDataID.getName())) {
            fieldDataIdentifierSpecs[i] = new FieldDataIdentifierSpec(fieldFunctionArgs[i], psfExtDataID);
        } 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);
    SolverUtilities.prepareSolverExecutable(sim.getSolverTaskDescription().getSolverDescription());
    // if we need to check steady state, do the following two lines
    if (bCheckSteadyState) {
        simTask.getSimulation().getSolverTaskDescription().setStopAtSpatiallyUniformErrorTolerance(ErrorTolerance.getDefaultSpatiallyUniformErrorTolerance());
    }
    FVSolverStandalone fvSolver = new FVSolverStandalone(simTask, simulationDataDir, false);
    fvSolver.startSolver();
    // fvSolver.runSolver();
    SolverStatus status = fvSolver.getSolverStatus();
    while (status.getStatus() != SolverStatus.SOLVER_FINISHED && status.getStatus() != SolverStatus.SOLVER_ABORTED && status.getStatus() != SolverStatus.SOLVER_STOPPED) {
        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) {
        throw new Exception("Sover did not finish normally." + status);
    }
}
Also used : SimulationTask(cbit.vcell.messaging.server.SimulationTask) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) SolverStatus(cbit.vcell.solver.server.SolverStatus) SimulationJob(cbit.vcell.solver.SimulationJob) FVSolverStandalone(cbit.vcell.solvers.FVSolverStandalone) ImageException(cbit.image.ImageException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) IOException(java.io.IOException) UserCancelException(org.vcell.util.UserCancelException)

Example 15 with FieldFunctionArguments

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

the class RunRefSimulationOp method runRefSimulation.

private static ImageTimeSeries<FloatImage> runRefSimulation(LocalWorkspace localWorkspace, ROI cellROI, double timeStepVal, TimeBounds timeBounds, FloatImage initRefConc, double baseDiffusionRate, ClientTaskStatusSupport progressListener) throws Exception {
    String INITCONC_EXTDATA_NAME = "initConc";
    String INITCONC_EXTDATA_VARNAME = "conc";
    String VAR_NAME = "species";
    User owner = LocalWorkspace.getDefaultOwner();
    KeyValue simKey = LocalWorkspace.createNewKeyValue();
    ExternalDataInfo initialConcentrationExtData = createNewExternalDataInfo(localWorkspace, INITCONC_EXTDATA_NAME);
    Extent extent = initRefConc.getExtent();
    Origin origin = initRefConc.getOrigin();
    ISize isize = new ISize(initRefConc.getNumX(), initRefConc.getNumY(), initRefConc.getNumZ());
    saveExternalData(initRefConc, INITCONC_EXTDATA_VARNAME, initialConcentrationExtData.getExternalDataIdentifier(), localWorkspace);
    FieldFunctionArguments initConditionFFA = new FieldFunctionArguments(INITCONC_EXTDATA_NAME, INITCONC_EXTDATA_VARNAME, new Expression(0.0), VariableType.VOLUME);
    BioModel bioModel = createRefSimBioModel(simKey, owner, origin, extent, cellROI, timeStepVal, timeBounds, VAR_NAME, new Expression(initConditionFFA.infix()), baseDiffusionRate);
    if (progressListener != null) {
        progressListener.setMessage("Running Reference Simulation...");
    }
    // run simulation
    runFVSolverStandalone(new File(localWorkspace.getDefaultSimDataDirectory()), bioModel.getSimulation(0), initialConcentrationExtData.getExternalDataIdentifier(), progressListener, true);
    VCDataIdentifier vcDataIdentifier = new VCSimulationDataIdentifier(new VCSimulationIdentifier(simKey, owner), 0);
    double[] dataTimes = localWorkspace.getDataSetControllerImpl().getDataSetTimes(vcDataIdentifier);
    FloatImage[] solutionImages = new FloatImage[dataTimes.length];
    for (int i = 0; i < dataTimes.length; i++) {
        SimDataBlock simDataBlock = localWorkspace.getDataSetControllerImpl().getSimDataBlock(null, vcDataIdentifier, VAR_NAME, dataTimes[i]);
        double[] doubleData = simDataBlock.getData();
        float[] floatPixels = new float[doubleData.length];
        for (int j = 0; j < doubleData.length; j++) {
            floatPixels[j] = (float) doubleData[j];
        }
        solutionImages[i] = new FloatImage(floatPixels, origin, extent, isize.getX(), isize.getY(), isize.getZ());
    }
    ImageTimeSeries<FloatImage> solution = new ImageTimeSeries<FloatImage>(FloatImage.class, solutionImages, dataTimes, 1);
    return solution;
}
Also used : Origin(org.vcell.util.Origin) VCSimulationIdentifier(cbit.vcell.solver.VCSimulationIdentifier) User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) FloatImage(cbit.vcell.VirtualMicroscopy.FloatImage) ExternalDataInfo(org.vcell.vmicro.workflow.data.ExternalDataInfo) Extent(org.vcell.util.Extent) FieldFunctionArguments(cbit.vcell.field.FieldFunctionArguments) ISize(org.vcell.util.ISize) VCSimulationDataIdentifier(cbit.vcell.solver.VCSimulationDataIdentifier) SimDataBlock(cbit.vcell.simdata.SimDataBlock) Expression(cbit.vcell.parser.Expression) BioModel(cbit.vcell.biomodel.BioModel) File(java.io.File) VCDataIdentifier(org.vcell.util.document.VCDataIdentifier) ImageTimeSeries(org.vcell.vmicro.workflow.data.ImageTimeSeries)

Aggregations

FieldFunctionArguments (cbit.vcell.field.FieldFunctionArguments)30 Expression (cbit.vcell.parser.Expression)21 FieldDataIdentifierSpec (cbit.vcell.field.FieldDataIdentifierSpec)15 ExternalDataIdentifier (org.vcell.util.document.ExternalDataIdentifier)13 ImageException (cbit.image.ImageException)10 DataAccessException (org.vcell.util.DataAccessException)10 UserCancelException (org.vcell.util.UserCancelException)9 KeyValue (org.vcell.util.document.KeyValue)8 ExpressionException (cbit.vcell.parser.ExpressionException)7 Extent (org.vcell.util.Extent)7 BioModel (cbit.vcell.biomodel.BioModel)6 MathException (cbit.vcell.math.MathException)6 SimulationJob (cbit.vcell.solver.SimulationJob)6 File (java.io.File)6 ROI (cbit.vcell.VirtualMicroscopy.ROI)5 SimulationContext (cbit.vcell.mapping.SimulationContext)5 MathDescription (cbit.vcell.math.MathDescription)5 VCSimulationDataIdentifier (cbit.vcell.solver.VCSimulationDataIdentifier)5 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)5 VCImageUncompressed (cbit.image.VCImageUncompressed)4