Search in sources :

Example 71 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class ODESimData method readIn.

/**
 * JMW : This really should be synchronized...
 */
public void readIn(DataInputStream input) throws IOException {
    formatID = input.readUTF();
    if (formatID.equals(SIMPLE_ODE_DATA_FORMAT_ID)) {
        this.mathName = input.readUTF();
        // read data from old format file
        double saveInterval = input.readDouble();
        int savedNumber = input.readInt();
        int variableNumber = input.readInt();
        String[] variableNames = new String[variableNumber];
        double[][] dataValues = new double[savedNumber][variableNumber];
        for (int i = 0; i < variableNumber; i++) {
            int flag = input.readInt();
            variableNames[i] = input.readUTF();
            for (int j = 0; j < savedNumber; j++) {
                dataValues[j][i] = input.readDouble();
            }
        }
        // now put data in new data structure
        int rowCount = savedNumber;
        int columnCount = variableNumber + 1;
        addDataColumn(new ODESolverResultSetColumnDescription("t", "t"));
        for (int c = 1; c < columnCount; c++) {
            addDataColumn(new ODESolverResultSetColumnDescription(variableNames[c - 1], variableNames[c - 1]));
        }
        double[] values = new double[columnCount];
        for (int c = 0; c < columnCount; c++) values[c] = 0.0;
        for (int r = 0; r < rowCount; r++) {
            values[0] = r * saveInterval / 1000.0;
            addRow(values);
        }
        for (int c = 1; c < columnCount; c++) {
            for (int r = 0; r < rowCount; r++) {
                setValue(r, c, dataValues[r][c - 1]);
            }
        }
    } else if (formatID.equals(GENERIC_ODE_DATA_FORMAT_ID)) {
        this.mathName = input.readUTF();
        int rowCount = input.readInt();
        int columnCount = input.readInt();
        for (int c = 0; c < columnCount; c++) {
            String columnName = input.readUTF();
            String columnDisplayName = input.readUTF();
            addDataColumn(new ODESolverResultSetColumnDescription(columnName, columnDisplayName));
        }
        double[] values = new double[columnCount];
        for (int r = 0; r < rowCount; r++) {
            for (int c = 0; c < columnCount; c++) {
                values[c] = input.readDouble();
            }
            addRow(values);
        }
    } else if (formatID.equals(COMPACT_ODE_DATA_FORMAT_ID)) {
        this.mathName = input.readUTF();
        int rowCount = input.readInt();
        int columnCount = input.readInt();
        for (int c = 0; c < columnCount; c++) {
            String columnName = input.readUTF();
            String columnDisplayName = input.readUTF();
            String columnParameterName = input.readUTF();
            if (columnParameterName.equals("null")) {
                columnParameterName = null;
            }
            addDataColumn(new ODESolverResultSetColumnDescription(columnName, columnParameterName, columnDisplayName));
        }
        double[] values = new double[columnCount];
        for (int r = 0; r < rowCount; r++) {
            for (int c = 0; c < columnCount; c++) {
                values[c] = input.readDouble();
            }
            addRow(values);
        }
        try {
            int functionCount = input.readInt();
            for (int c = 0; c < functionCount; c++) {
                String columnName = input.readUTF();
                String columnDisplayName = input.readUTF();
                String columnParameterName = input.readUTF();
                if (columnParameterName.equals("null")) {
                    columnParameterName = null;
                }
                String expressionString = input.readUTF();
                try {
                    Expression expression = new Expression(expressionString);
                    addFunctionColumn(new FunctionColumnDescription(expression, columnName, columnParameterName, columnDisplayName, false));
                } catch (ExpressionBindingException e) {
                    e.printStackTrace(System.out);
                    System.out.println("ODESimData.readIn(): unable to bind expression '" + expressionString + "'");
                } catch (ExpressionException e) {
                    e.printStackTrace(System.out);
                    System.out.println("ODESimData.readIn(): unable to parse expression '" + expressionString + "'");
                }
            }
        } catch (EOFException e) {
        }
    } else {
        throw new IOException("DataInputStream is wrong format '" + formatID + "'");
    }
}
Also used : IOException(java.io.IOException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ExpressionException(cbit.vcell.parser.ExpressionException) Expression(cbit.vcell.parser.Expression) EOFException(java.io.EOFException) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 72 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class ODESimData method readIDADataFile.

public static ODESimData readIDADataFile(VCDataIdentifier vcdId, File dataFile, int keepMost, File functionsFile) throws DataAccessException {
    // read ida file
    System.out.println("reading ida file : " + dataFile);
    ODESimData odeSimData = new ODESimData();
    odeSimData.formatID = IDA_DATA_FORMAT_ID;
    odeSimData.mathName = vcdId.getID();
    BufferedReader bufferedReader = null;
    try {
        bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile)));
        // Read header
        String line = bufferedReader.readLine();
        if (line == null) {
            // throw exception
            return null;
        }
        StringTokenizer st = new StringTokenizer(line, ":");
        while (st.hasMoreTokens()) {
            odeSimData.addDataColumn(new ODESolverResultSetColumnDescription(st.nextToken()));
        }
        // Read data
        while ((line = bufferedReader.readLine()) != null) {
            st = new StringTokenizer(line);
            double[] values = new double[odeSimData.getDataColumnCount()];
            int count = 0;
            while (st.hasMoreTokens()) {
                values[count++] = Double.valueOf(st.nextToken()).doubleValue();
            }
            if (count == odeSimData.getDataColumnCount()) {
                odeSimData.addRow(values);
            } else {
                break;
            }
        }
    // 
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    } finally {
        try {
            if (bufferedReader != null) {
                bufferedReader.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }
    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());
        }
    }
    if (keepMost > 0) {
        odeSimData.trimRows(keepMost);
    }
    return odeSimData;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) 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) StringTokenizer(java.util.StringTokenizer) Expression(cbit.vcell.parser.Expression) BufferedReader(java.io.BufferedReader) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 73 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class ODESimData method readNFSIMDataFile.

public static ODESimData readNFSIMDataFile(VCDataIdentifier vcdId, File dataFile, File functionsFile) throws DataAccessException, IOException {
    System.out.println("reading NetCDF file : " + dataFile);
    ODESimData odeSimData = new ODESimData();
    odeSimData.formatID = NETCDF_DATA_FORMAT_ID;
    odeSimData.mathName = vcdId.getID();
    String file = dataFile.getPath();
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String firstLine = reader.readLine();
    StringTokenizer st = new StringTokenizer(firstLine);
    // #
    st.nextToken();
    // time
    st.nextToken();
    // first column will be time t.
    odeSimData.addDataColumn(new ODESolverResultSetColumnDescription("t"));
    int count = st.countTokens();
    String varName = new String();
    for (int i = 0; i < count; i++) {
        varName = st.nextToken();
        odeSimData.addDataColumn(new ODESolverResultSetColumnDescription(varName));
    }
    // Read data
    // String         ls = System.getProperty("line.separator");
    // StringBuilder  stringBuilder = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        double[] values = new double[odeSimData.getDataColumnCount()];
        st = new StringTokenizer(line);
        count = st.countTokens();
        String sData = new String();
        for (int i = 0; i < count; i++) {
            sData = st.nextToken();
            double dData = Double.parseDouble(sData);
            values[i] = dData;
        }
        odeSimData.addRow(values);
    }
    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 : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) StringTokenizer(java.util.StringTokenizer) Expression(cbit.vcell.parser.Expression) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) DataAccessException(org.vcell.util.DataAccessException) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 74 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class RungeKuttaFehlbergSolver method step.

/**
 * Integrate over time step using the forward Euler method (1st order explicit)
 * results must be stored in NumVectors-1 = vector(4);
 *  t is the current time
 *  h is the time step
 */
protected void step(double t, double h) throws SolverException {
    try {
        double[] oldValues = getValueVector(0);
        double[] newValues = getValueVector(1);
        // 
        // update time
        oldValues[getTimeIndex()] = t;
        // 
        for (int i = 0; i < 6; i++) {
            newValues[getTimeIndex()] = t + a[i] * h;
            for (int j = 0; j < getStateVariableCount(); j++) {
                int J = getVariableIndex(j);
                newValues[J] = oldValues[J];
                for (int m = 0; m < i; m++) {
                    newValues[J] += b[i][m] * k[m][J];
                }
            }
            for (int j = 0; j < getStateVariableCount(); j++) {
                k[i][getVariableIndex(j)] = h * evaluate(newValues, j);
            }
        }
        for (int i = 0; i < getStateVariableCount(); i++) {
            int I = getVariableIndex(i);
            newValues[I] = oldValues[I];
            for (int j = 0; j < 6; j++) {
                newValues[I] += c[j] * k[j][I];
            }
        }
    } catch (ExpressionException expressionException) {
        throw new SolverException(expressionException.getMessage());
    }
}
Also used : SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 75 with ExpressionException

use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.

the class XmlBase method unMangleExpression.

public static Expression unMangleExpression(String expStr) {
    Expression tempExp = null;
    // 
    try {
        tempExp = new Expression(unMangle(expStr));
        tempExp = MathFunctionDefinitions.fixFunctionSyntax(tempExp);
    } catch (ExpressionException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    }
    return tempExp;
}
Also used : Expression(cbit.vcell.parser.Expression) ExpressionException(cbit.vcell.parser.ExpressionException)

Aggregations

ExpressionException (cbit.vcell.parser.ExpressionException)199 Expression (cbit.vcell.parser.Expression)138 MathException (cbit.vcell.math.MathException)58 PropertyVetoException (java.beans.PropertyVetoException)51 DataAccessException (org.vcell.util.DataAccessException)34 ArrayList (java.util.ArrayList)32 Variable (cbit.vcell.math.Variable)30 IOException (java.io.IOException)29 Element (org.jdom.Element)26 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)25 MappingException (cbit.vcell.mapping.MappingException)24 Function (cbit.vcell.math.Function)24 Vector (java.util.Vector)24 ModelException (cbit.vcell.model.ModelException)23 SolverException (cbit.vcell.solver.SolverException)23 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)22 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)21 Constant (cbit.vcell.math.Constant)20 MathDescription (cbit.vcell.math.MathDescription)19 Structure (cbit.vcell.model.Structure)18