Search in sources :

Example 1 with SimResampleInfoProvider

use of org.vcell.util.document.SimResampleInfoProvider 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)

Aggregations

FieldDataIdentifierSpec (cbit.vcell.field.FieldDataIdentifierSpec)1 FieldDataParameterVariable (cbit.vcell.field.FieldDataParameterVariable)1 FieldFunctionArguments (cbit.vcell.field.FieldFunctionArguments)1 Expression (cbit.vcell.parser.Expression)1 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)1 VariableSymbolTable (cbit.vcell.parser.VariableSymbolTable)1 VCSimulationDataIdentifier (cbit.vcell.solver.VCSimulationDataIdentifier)1 VCSimulationDataIdentifierOldStyle (cbit.vcell.solver.VCSimulationDataIdentifierOldStyle)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Vector (java.util.Vector)1 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)1 DataAccessException (org.vcell.util.DataAccessException)1 ExternalDataIdentifier (org.vcell.util.document.ExternalDataIdentifier)1 SimResampleInfoProvider (org.vcell.util.document.SimResampleInfoProvider)1