Search in sources :

Example 6 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.

the class MathTestingUtilities method getConstructedResultSet.

/**
 * Insert the method's description here.
 * Creation date: (1/17/2003 3:47:43 PM)
 * @return cbit.vcell.solver.ode.ODESolverResultSet
 * @param sim cbit.vcell.solver.Simulation
 */
public static ODESolverResultSet getConstructedResultSet(MathDescription mathDesc, double[] time) throws Exception {
    if (mathDesc.getGeometry().getDimension() != 0) {
        throw new RuntimeException("can only handle non-spatial simulations.");
    }
    Simulation sim = new Simulation(mathDesc);
    SimulationSymbolTable simSymbolTable = new SimulationSymbolTable(sim, 0);
    ODESolverResultSet resultSet = new ODESolverResultSet();
    resultSet.addDataColumn(new ODESolverResultSetColumnDescription("t"));
    for (int i = 0; i < time.length; i++) {
        resultSet.addRow(new double[] { time[i] });
    }
    java.util.Enumeration<SubDomain> subDomainEnum = mathDesc.getSubDomains();
    String errorString = "Variable(s) : ";
    while (subDomainEnum.hasMoreElements()) {
        SubDomain subDomain = subDomainEnum.nextElement();
        java.util.Enumeration<Equation> enumEquations = subDomain.getEquations();
        while (enumEquations.hasMoreElements()) {
            Equation equation = enumEquations.nextElement();
            Expression constructedSolution = equation.getExactSolution();
            if (constructedSolution != null) {
                constructedSolution = new Expression(constructedSolution);
                constructedSolution.bindExpression(simSymbolTable);
                constructedSolution = simSymbolTable.substituteFunctions(constructedSolution);
                constructedSolution = constructedSolution.flatten();
                resultSet.addFunctionColumn(new FunctionColumnDescription(constructedSolution, equation.getVariable().getName(), null, equation.getVariable().getName(), false));
            } else {
                errorString = errorString + equation.getVariable().getName() + ", ";
            }
        }
    }
    if (!errorString.equals("Variable(s) : ")) {
        throw new RuntimeException(errorString + " don't have a constructed solution");
    }
    return resultSet;
}
Also used : SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) OdeEquation(cbit.vcell.math.OdeEquation) PdeEquation(cbit.vcell.math.PdeEquation) Equation(cbit.vcell.math.Equation) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 7 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.

the class MathTestingUtilities method getExactResultSet.

/**
 * Insert the method's description here.
 * Creation date: (1/17/2003 3:47:43 PM)
 * @return cbit.vcell.solver.ode.ODESolverResultSet
 * @param sim cbit.vcell.solver.Simulation
 */
public static ODESolverResultSet getExactResultSet(MathDescription mathDesc, double[] time, Constant sensitivityParam) throws Exception {
    if (mathDesc.getGeometry().getDimension() != 0) {
        throw new RuntimeException("can only handle non-spatial simulations.");
    }
    Simulation sim = new Simulation(mathDesc);
    SimulationSymbolTable simSymbolTable = new SimulationSymbolTable(sim, 0);
    ODESolverResultSet resultSet = new ODESolverResultSet();
    resultSet.addDataColumn(new ODESolverResultSetColumnDescription("t"));
    for (int i = 0; i < time.length; i++) {
        resultSet.addRow(new double[] { time[i] });
    }
    java.util.Enumeration<SubDomain> subDomainEnum = mathDesc.getSubDomains();
    while (subDomainEnum.hasMoreElements()) {
        SubDomain subDomain = subDomainEnum.nextElement();
        java.util.Enumeration<Equation> enumEquations = subDomain.getEquations();
        while (enumEquations.hasMoreElements()) {
            Equation equation = enumEquations.nextElement();
            Expression exactSolution = equation.getExactSolution();
            if (exactSolution != null) {
                exactSolution = new Expression(exactSolution);
                exactSolution.bindExpression(simSymbolTable);
                exactSolution = simSymbolTable.substituteFunctions(exactSolution);
                exactSolution.bindExpression(simSymbolTable);
                exactSolution = exactSolution.flatten();
                resultSet.addFunctionColumn(new FunctionColumnDescription(exactSolution, equation.getVariable().getName(), null, equation.getVariable().getName(), false));
                if (sensitivityParam != null) {
                    exactSolution = equation.getExactSolution();
                    Expression exactSensitivity = new Expression(exactSolution);
                    exactSensitivity.bindExpression(simSymbolTable);
                    exactSensitivity = simSymbolTable.substituteFunctions(exactSensitivity);
                    exactSensitivity.bindExpression(simSymbolTable);
                    exactSensitivity = exactSensitivity.differentiate(sensitivityParam.getName());
                    exactSensitivity = exactSensitivity.flatten();
                    VolVariable volVar = (VolVariable) equation.getVariable();
                    String sensName = SensVariable.getSensName(volVar, sensitivityParam);
                    resultSet.addFunctionColumn(new FunctionColumnDescription(exactSensitivity, sensName, null, sensName, false));
                }
            } else {
                throw new RuntimeException("variable " + equation.getVariable().getName() + " doesn't have an exact solution");
            }
        }
    }
    return resultSet;
}
Also used : VolVariable(cbit.vcell.math.VolVariable) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) OdeEquation(cbit.vcell.math.OdeEquation) PdeEquation(cbit.vcell.math.PdeEquation) Equation(cbit.vcell.math.Equation) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 8 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription in project vcell by virtualcell.

the class DefaultODESolver method createODESolverResultSet.

/**
 */
private ODESolverResultSet createODESolverResultSet() throws ExpressionException {
    SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
    // 
    // create symbol table for binding expression
    // 
    String[] symbols = new String[fieldIdentifiers.size()];
    for (int i = 0; i < symbols.length; i++) {
        symbols[i] = ((Variable) fieldIdentifiers.elementAt(i)).getName();
    }
    // Initialize the ResultSet...
    ODESolverResultSet odeSolverResultSet = new ODESolverResultSet();
    odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(ReservedVariable.TIME.getName()));
    for (int i = 0; i < getStateVariableCount(); i++) {
        StateVariable stateVariable = getStateVariable(i);
        if (stateVariable instanceof SensStateVariable) {
            SensStateVariable sensStateVariable = (SensStateVariable) stateVariable;
            odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(sensStateVariable.getVariable().getName(), sensStateVariable.getParameter().getName(), sensStateVariable.getVariable().getName()));
        } else {
            odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(stateVariable.getVariable().getName()));
        }
    }
    Variable[] variables = simSymbolTable.getVariables();
    for (int i = 0; i < variables.length; i++) {
        if (variables[i] instanceof Function && SimulationSymbolTable.isFunctionSaved((Function) variables[i])) {
            Function function = (Function) variables[i];
            Expression exp1 = new Expression(function.getExpression());
            try {
                exp1 = simSymbolTable.substituteFunctions(exp1);
            } catch (MathException e) {
                e.printStackTrace(System.out);
                throw new RuntimeException("Substitute function failed on function " + function.getName() + " " + e.getMessage());
            }
            odeSolverResultSet.addFunctionColumn(new FunctionColumnDescription(exp1.flatten(), function.getName(), null, function.getName(), false));
        }
    }
    // 
    if (getSensitivityParameter() != null) {
        if (odeSolverResultSet.findColumn(getSensitivityParameter().getName()) == -1) {
            FunctionColumnDescription fcd = new FunctionColumnDescription(new Expression(getSensitivityParameter().getConstantValue()), getSensitivityParameter().getName(), null, getSensitivityParameter().getName(), false);
            odeSolverResultSet.addFunctionColumn(fcd);
        }
        StateVariable[] stateVars = (StateVariable[]) org.vcell.util.BeanUtils.getArray(fieldStateVariables, StateVariable.class);
        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());
                try {
                    depSensFnExpr = simSymbolTable.substituteFunctions(depSensFnExpr);
                } catch (MathException e) {
                    e.printStackTrace(System.out);
                    throw new RuntimeException("Substitute function failed on function " + depSensFunction.getName() + " " + e.getMessage());
                }
                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) {
                    FunctionColumnDescription cd = new FunctionColumnDescription(depSensFnExpr.flatten(), depSensFnName, getSensitivityParameter().getName(), depSensFnName, false);
                    odeSolverResultSet.addFunctionColumn(cd);
                }
            }
        }
    }
    return (odeSolverResultSet);
}
Also used : ReservedVariable(cbit.vcell.math.ReservedVariable) Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription)

Example 9 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription 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 10 with ODESolverResultSetColumnDescription

use of cbit.vcell.math.ODESolverResultSetColumnDescription 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)

Aggregations

ODESolverResultSetColumnDescription (cbit.vcell.math.ODESolverResultSetColumnDescription)26 ODESolverResultSet (cbit.vcell.solver.ode.ODESolverResultSet)18 FunctionColumnDescription (cbit.vcell.math.FunctionColumnDescription)16 Expression (cbit.vcell.parser.Expression)14 ExpressionException (cbit.vcell.parser.ExpressionException)12 IOException (java.io.IOException)8 MathException (cbit.vcell.math.MathException)6 SimulationSymbolTable (cbit.vcell.solver.SimulationSymbolTable)6 FileNotFoundException (java.io.FileNotFoundException)6 DataSource (cbit.vcell.modelopt.DataSource)5 BufferedReader (java.io.BufferedReader)5 Function (cbit.vcell.math.Function)4 ReferenceData (cbit.vcell.opt.ReferenceData)4 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)4 AnnotatedFunction (cbit.vcell.solver.AnnotatedFunction)4 SolverException (cbit.vcell.solver.SolverException)4 RowColumnResultSet (cbit.vcell.math.RowColumnResultSet)3 ExpressionBindingException (cbit.vcell.parser.ExpressionBindingException)3 Simulation (cbit.vcell.solver.Simulation)3 Color (java.awt.Color)3