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;
}
Aggregations