Search in sources :

Example 51 with AnnotatedFunction

use of cbit.vcell.solver.AnnotatedFunction in project vcell by virtualcell.

the class MergedData method readFunctions.

/**
 * Insert the method's description here.
 * Creation date: (1/15/2004 11:48:25 AM)
 */
private void readFunctions(OutputContext outputContext) throws FileNotFoundException, IOException {
    Vector<AnnotatedFunction> annotatedFuncsVector = FunctionFileGenerator.readFunctionsFile(getFunctionsFile(), dataInfo.getID());
    // add user-defined functions from output context, if any
    if (outputContext != null) {
        for (int i = 0; i < outputContext.getOutputFunctions().length; i++) {
            annotatedFuncsVector.add(outputContext.getOutputFunctions()[i]);
        }
    }
    // 
    // Convert this annotatedfunctionsVector into the field annotatedFunctionsList.
    // 
    annotatedFunctionList.clear();
    for (int i = 0; i < annotatedFuncsVector.size(); i++) {
        AnnotatedFunction annotatedFunction = annotatedFuncsVector.elementAt(i);
        try {
            functionBindAndSubstitute(annotatedFunction);
            addFunctionToList(annotatedFunction);
        } catch (ExpressionException e) {
            throw new RuntimeException("Could not add function " + annotatedFunction.getName() + " to annotatedFunctionList");
        }
    }
}
Also used : ExpressionException(cbit.vcell.parser.ExpressionException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 52 with AnnotatedFunction

use of cbit.vcell.solver.AnnotatedFunction in project vcell by virtualcell.

the class MergedData method getVarAndFunctionDataIdentifiers.

/**
 * This method was created in VisualAge.
 * @return java.lang.String[]
 */
public DataIdentifier[] getVarAndFunctionDataIdentifiers(OutputContext outputContext) throws IOException, DataAccessException {
    getFunctionDataIdentifiers(outputContext);
    DataIdentifier[] dis = new DataIdentifier[dataSetIdentifierList.size()];
    for (int i = 0; i < dataSetIdentifierList.size(); i++) {
        DataSetIdentifier dsi = (DataSetIdentifier) dataSetIdentifierList.elementAt(i);
        String displayName = dsi.getName();
        if (dsi.isFunction()) {
            AnnotatedFunction f = null;
            for (int j = 0; j < annotatedFunctionList.size(); j++) {
                AnnotatedFunction function = (AnnotatedFunction) annotatedFunctionList.elementAt(j);
                if (function.getName().equals(dsi.getName())) {
                    f = function;
                    break;
                }
            }
            if (f != null) {
                displayName = f.getDisplayName();
            }
        }
        dis[i] = new DataIdentifier(dsi.getName(), dsi.getVariableType(), dsi.getDomain(), dsi.isFunction(), displayName);
    }
    return dis;
}
Also used : VCDataIdentifier(org.vcell.util.document.VCDataIdentifier) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 53 with AnnotatedFunction

use of cbit.vcell.solver.AnnotatedFunction in project vcell by virtualcell.

the class SundialsSolver method createFunctionList.

public Vector<AnnotatedFunction> createFunctionList() {
    // 
    // add appropriate Function columns to result set
    // 
    Vector<AnnotatedFunction> funcList = super.createFunctionList();
    SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
    if (getSensitivityParameter() != null) {
        try {
            AnnotatedFunction saf = new AnnotatedFunction(getSensitivityParameter().getName(), new Expression(getSensitivityParameter().getConstantValue()), getSensitivityParameter().getDomain(), "", VariableType.NONSPATIAL, FunctionCategory.PREDEFINED);
            if (!funcList.contains(saf)) {
                funcList.add(saf);
            }
            Variable[] variables = simSymbolTable.getVariables();
            StateVariable[] stateVars = createStateVariables();
            for (int i = 0; i < variables.length; i++) {
                if (variables[i] instanceof Function && SimulationSymbolTable.isFunctionSaved((Function) variables[i])) {
                    Function depSensFunction = (Function) variables[i];
                    Expression depSensFnExpr = new Expression(depSensFunction.getExpression());
                    depSensFnExpr = simSymbolTable.substituteFunctions(depSensFnExpr);
                    depSensFnExpr = getFunctionSensitivity(depSensFnExpr, getSensitivityParameter(), stateVars);
                    // depSensFnExpr = depSensFnExpr.flatten(); 	// already bound and flattened in getFunctionSensitivity, no need here.....
                    String depSensFnName = new String("sens_" + depSensFunction.getName() + "_wrt_" + getSensitivityParameter().getName());
                    if (depSensFunction != null) {
                        AnnotatedFunction af = new AnnotatedFunction(depSensFnName, depSensFnExpr.flatten(), variables[i].getDomain(), "", VariableType.NONSPATIAL, FunctionCategory.PREDEFINED);
                        funcList.add(af);
                    }
                }
            }
        } catch (MathException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error adding function to resultSet: " + e.getMessage());
        } catch (ExpressionException e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Error adding function to resultSet: " + e.getMessage());
        }
    }
    return funcList;
}
Also used : Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) ExpressionException(cbit.vcell.parser.ExpressionException) Function(cbit.vcell.math.Function) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 54 with AnnotatedFunction

use of cbit.vcell.solver.AnnotatedFunction in project vcell by virtualcell.

the class ODESimData method readNCDataFile.

public static ODESimData readNCDataFile(VCDataIdentifier vcdId, File dataFile, File functionsFile) throws DataAccessException {
    // read ida file
    System.out.println("reading NetCDF file : " + dataFile);
    ODESimData odeSimData = new ODESimData();
    odeSimData.formatID = NETCDF_DATA_FORMAT_ID;
    odeSimData.mathName = vcdId.getID();
    // read .stoch file, this funciton here equals to getODESolverRestultSet()+getStateVariableResultSet()  in ODE.
    try {
        NetCDFEvaluator ncEva = new NetCDFEvaluator();
        NetCDFReader ncReader = null;
        try {
            ncEva.setNetCDFTarget(dataFile.getAbsolutePath());
            ncReader = ncEva.getNetCDFReader();
        } catch (Exception e) {
            e.printStackTrace(System.err);
            throw new RuntimeException("Cannot open simulation result file: " + dataFile.getAbsolutePath() + "!");
        }
        // Read result according to trial number
        if (ncReader.getNumTrials() == 1) {
            // Read header
            String[] varNames = ncReader.getSpeciesNames_val();
            // first column will be time t.
            odeSimData.addDataColumn(new ODESolverResultSetColumnDescription("t"));
            // following columns are stoch variables
            for (int i = 0; i < varNames.length; i++) {
                odeSimData.addDataColumn(new ODESolverResultSetColumnDescription(varNames[i]));
            }
            // Read data
            // data only, no time points
            ArrayDouble data = (ArrayDouble) ncEva.getTimeSeriesData(1);
            double[] timePoints = ncReader.getTimePoints();
            System.out.println("time points length is " + timePoints.length);
            // shape[0]:num of timepoints, shape[1]: num of species
            int[] shape = data.getShape();
            if (// one species
            shape.length == 1) {
                ArrayDouble.D1 temData = (ArrayDouble.D1) data;
                System.out.println("one species in time series data and size is " + temData.getSize());
                for (// rows
                int k = 0; // rows
                k < timePoints.length; // rows
                k++) {
                    double[] values = new double[odeSimData.getDataColumnCount()];
                    values[0] = timePoints[k];
                    for (int i = 1; i < odeSimData.getDataColumnCount(); i++) {
                        values[i] = temData.get(k);
                    }
                    odeSimData.addRow(values);
                }
            }
            if (// more than one species
            shape.length == 2) {
                ArrayDouble.D2 temData = (ArrayDouble.D2) data;
                System.out.println("multiple species in time series, the length of time series is :" + data.getShape()[0] + ", and the total number of speceis is: " + data.getShape()[1]);
                for (// rows
                int k = 0; // rows
                k < timePoints.length; // rows
                k++) {
                    double[] values = new double[odeSimData.getDataColumnCount()];
                    values[0] = timePoints[k];
                    for (int i = 1; i < odeSimData.getDataColumnCount(); i++) {
                        values[i] = temData.get(k, i - 1);
                    }
                    odeSimData.addRow(values);
                }
            }
        } else if (ncReader.getNumTrials() > 1) {
            // Read header
            String[] varNames = ncReader.getSpeciesNames_val();
            // first column will be time t.
            odeSimData.addDataColumn(new ODESolverResultSetColumnDescription("TrialNo"));
            // following columns are stoch variables
            for (int i = 0; i < varNames.length; i++) {
                odeSimData.addDataColumn(new ODESolverResultSetColumnDescription(varNames[i]));
            }
            // Read data
            // data only, no trial numbers
            ArrayDouble data = (ArrayDouble) ncEva.getDataOverTrials(ncReader.getTimePoints().length - 1);
            int[] trialNum = ncEva.getNetCDFReader().getTrialNumbers();
            // System.out.println("total trials are "+trialNum.length);
            // shape[0]:number of trials, shape[1]: num of species
            int[] shape = data.getShape();
            if (// one species
            shape.length == 1) {
                ArrayDouble.D1 temData = (ArrayDouble.D1) data;
                // System.out.println("one species over trials, size is: "+temData.getSize());
                for (// rows
                int k = 0; // rows
                k < trialNum.length; // rows
                k++) {
                    double[] values = new double[odeSimData.getDataColumnCount()];
                    values[0] = trialNum[k];
                    for (int i = 1; i < odeSimData.getDataColumnCount(); i++) {
                        values[i] = temData.get(k);
                    }
                    odeSimData.addRow(values);
                }
            }
            if (// more than one species
            shape.length == 2) {
                ArrayDouble.D2 temData = (ArrayDouble.D2) data;
                // System.out.println("multiple species in multiple trials, the length of trials is :"+data.getShape()[0]+", and the total number of speceis is: "+data.getShape()[1]);
                for (// rows
                int k = 0; // rows
                k < trialNum.length; // rows
                k++) {
                    double[] values = new double[odeSimData.getDataColumnCount()];
                    values[0] = trialNum[k];
                    for (int i = 1; i < odeSimData.getDataColumnCount(); i++) {
                        values[i] = temData.get(k, i - 1);
                    }
                    odeSimData.addRow(values);
                }
            }
        } else {
            throw new RuntimeException("Number of trials should be a countable positive value, from 1 to N.");
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
        throw new RuntimeException("Problem encountered in parsing hybrid simulation results.\n" + e.getMessage());
    }
    if (!odeSimData.getColumnDescriptions(0).getName().equals(SimDataConstants.HISTOGRAM_INDEX_NAME)) {
        Vector<AnnotatedFunction> funcList;
        try {
            funcList = FunctionFileGenerator.readFunctionsFile(functionsFile, vcdId.getID());
            for (AnnotatedFunction func : funcList) {
                try {
                    Expression expression = new Expression(func.getExpression());
                    odeSimData.addFunctionColumn(new FunctionColumnDescription(expression, func.getName(), null, func.getName(), false));
                } catch (ExpressionException e) {
                    throw new RuntimeException("Could not add function " + func.getName() + " to annotatedFunctionList");
                }
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace(System.out);
            throw new DataAccessException(e1.getMessage());
        } catch (IOException e1) {
            e1.printStackTrace(System.out);
            throw new DataAccessException(e1.getMessage());
        }
    }
    return odeSimData;
}
Also used : NetCDFEvaluator(cbit.vcell.solver.stoch.NetCDFEvaluator) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) ExpressionException(cbit.vcell.parser.ExpressionException) NetCDFReader(cbit.vcell.solver.stoch.NetCDFReader) ArrayDouble(ucar.ma2.ArrayDouble) Expression(cbit.vcell.parser.Expression) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 55 with AnnotatedFunction

use of cbit.vcell.solver.AnnotatedFunction in project vcell by virtualcell.

the class ChomboVtkFileWriter method getVtuVarInfos.

public VtuVarInfo[] getVtuVarInfos(ChomboFiles chomboFiles, OutputContext outputContext, VCData vcData) throws DataAccessException, IOException {
    // 
    // read the time=0 chombo dataset into memory to get the var names (probably a more efficient way of doing this).
    // 
    ChomboDataset chomboDataset;
    try {
        int timeIndex = 0;
        chomboDataset = ChomboFileReader.readDataset(chomboFiles, chomboFiles.getTimeIndices().get(timeIndex));
    } catch (Exception e) {
        throw new DataAccessException("failed to read chombo dataset: " + e.getMessage(), e);
    }
    DataIdentifier[] dataIdentifiers = vcData.getVarAndFunctionDataIdentifiers(outputContext);
    for (DataIdentifier di : dataIdentifiers) {
        System.out.println(((di.getDomain() != null) ? di.getDomain().getName() : "none") + "::" + di.getName() + "-" + di.getVariableType());
    }
    // 
    // for each ChomboDomain get list of built-in (mesh) variables, component (regular) volume variables, and Membrane Variables (still tied to the volume).
    // 
    ArrayList<VtuVarInfo> varInfos = new ArrayList<VtuVarInfo>();
    for (ChomboCombinedVolumeMembraneDomain chomboCombinedVolumeMembraneDomain : chomboDataset.getCombinedVolumeMembraneDomains()) {
        ChomboMeshData chomboMeshData = chomboCombinedVolumeMembraneDomain.getChomboMeshData();
        // 
        // process Volume variables for this combined domain (chombo stores membrane data with volume)
        // 
        {
            String volumeDomainName = chomboCombinedVolumeMembraneDomain.getVolumeDomainName();
            VariableDomain volVariableDomain = VariableDomain.VARIABLEDOMAIN_VOLUME;
            for (String builtinVarName : chomboMeshData.getVolumeBuiltinNames()) {
                String varName = builtinVarName;
                String displayName = "(" + volumeDomainName + ")  " + varName;
                String expressionString = null;
                boolean bMeshVariable = true;
                varInfos.add(new VtuVarInfo(varName, displayName, volumeDomainName, volVariableDomain, expressionString, DataType.CellData, bMeshVariable));
            }
            for (String componentVarName : chomboMeshData.getVisibleVolumeDataNames()) {
                String varName = componentVarName;
                String displayName = "(" + volumeDomainName + ")  " + varName;
                String expressionString = null;
                boolean bMeshVariable = false;
                varInfos.add(new VtuVarInfo(varName, displayName, volumeDomainName, volVariableDomain, expressionString, DataType.CellData, bMeshVariable));
            }
            for (DataIdentifier dataID : dataIdentifiers) {
                if (dataID.isVisible() && dataID.getVariableType().getVariableDomain() == VariableDomain.VARIABLEDOMAIN_VOLUME && (dataID.getDomain() == null || dataID.getDomain().getName().equals(volumeDomainName))) {
                    String displayName = "(" + volumeDomainName + ")  " + dataID.getDisplayName();
                    String expressionString = null;
                    AnnotatedFunction f = vcData.getFunction(outputContext, dataID.getName());
                    if (f != null) {
                        expressionString = f.getExpression().infix();
                    }
                    boolean bMeshVar = false;
                    varInfos.add(new VtuVarInfo(dataID.getName(), displayName, volumeDomainName, volVariableDomain, expressionString, DataType.CellData, bMeshVar));
                }
            }
        }
        // 
        // process membrane variables for this combined domain (chombo stores membrane data with volume)
        // 
        {
            String memDomainName = chomboCombinedVolumeMembraneDomain.getMembraneDomainName();
            VariableDomain memVariableDomain = VariableDomain.VARIABLEDOMAIN_MEMBRANE;
            for (ChomboMembraneVarData membraneVarData : chomboMeshData.getMembraneVarData()) {
                String varName = membraneVarData.getName();
                String displayName = "(" + membraneVarData.getDomainName() + ")  " + varName;
                String expressionString = null;
                boolean bMeshVariable = false;
                varInfos.add(new VtuVarInfo(varName, displayName, memDomainName, memVariableDomain, expressionString, DataType.CellData, bMeshVariable));
            }
            for (String builtinVarName : chomboMeshData.getMembraneBuiltinNames()) {
                String varName = builtinVarName;
                String displayName = "(" + memDomainName + ")  " + varName;
                String expressionString = null;
                boolean bMeshVariable = true;
                varInfos.add(new VtuVarInfo(varName, displayName, memDomainName, memVariableDomain, expressionString, DataType.CellData, bMeshVariable));
            }
            for (DataIdentifier dataID : dataIdentifiers) {
                if (dataID.isVisible() && dataID.getVariableType().getVariableDomain() == VariableDomain.VARIABLEDOMAIN_MEMBRANE && (dataID.getDomain() == null || dataID.getDomain().getName().equals(memDomainName))) {
                    String displayName = "(" + memDomainName + ")  " + dataID.getDisplayName();
                    String expressionString = null;
                    AnnotatedFunction f = vcData.getFunction(outputContext, dataID.getName());
                    if (f != null) {
                        expressionString = f.getExpression().infix();
                    }
                    boolean bMeshVar = false;
                    varInfos.add(new VtuVarInfo(dataID.getName(), displayName, memDomainName, memVariableDomain, expressionString, DataType.CellData, bMeshVar));
                }
            }
        }
    }
    return varInfos.toArray(new VtuVarInfo[0]);
}
Also used : VtuVarInfo(org.vcell.vis.io.VtuVarInfo) DataIdentifier(cbit.vcell.simdata.DataIdentifier) VariableDomain(cbit.vcell.math.VariableType.VariableDomain) ArrayList(java.util.ArrayList) ChomboMembraneVarData(org.vcell.vis.chombo.ChomboMembraneVarData) DivideByZeroException(cbit.vcell.parser.DivideByZeroException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) MathException(cbit.vcell.math.MathException) ChomboCombinedVolumeMembraneDomain(org.vcell.vis.chombo.ChomboDataset.ChomboCombinedVolumeMembraneDomain) ChomboMeshData(org.vcell.vis.chombo.ChomboMeshData) DataAccessException(org.vcell.util.DataAccessException) ChomboDataset(org.vcell.vis.chombo.ChomboDataset) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Aggregations

AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)61 DataAccessException (org.vcell.util.DataAccessException)21 ExpressionException (cbit.vcell.parser.ExpressionException)20 Expression (cbit.vcell.parser.Expression)17 ArrayList (java.util.ArrayList)17 Simulation (cbit.vcell.solver.Simulation)15 IOException (java.io.IOException)15 OutputContext (cbit.vcell.simdata.OutputContext)14 MathException (cbit.vcell.math.MathException)12 VCDataIdentifier (org.vcell.util.document.VCDataIdentifier)12 SimulationContext (cbit.vcell.mapping.SimulationContext)11 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)10 DataIdentifier (cbit.vcell.simdata.DataIdentifier)9 VCSimulationDataIdentifier (cbit.vcell.solver.VCSimulationDataIdentifier)9 FileNotFoundException (java.io.FileNotFoundException)8 Domain (cbit.vcell.math.Variable.Domain)7 VariableType (cbit.vcell.math.VariableType)7 XmlParseException (cbit.vcell.xml.XmlParseException)7 PropertyVetoException (java.beans.PropertyVetoException)7 File (java.io.File)7