Search in sources :

Example 1 with SpatialReferenceData

use of cbit.vcell.opt.SpatialReferenceData in project vcell by virtualcell.

the class OptXmlWriter method getCopasiDataXML.

public static Element getCopasiDataXML(ParameterEstimationTask parameterEstimationTask) throws IOException {
    ReferenceData refData = parameterEstimationTask.getModelOptimizationSpec().getReferenceData();
    Element refDataElement = null;
    if (refData instanceof SimpleReferenceData) {
        refDataElement = new Element(OptXmlTags.SimpleReferenceData_Tag);
    } else if (refData instanceof SpatialReferenceData) {
        refDataElement = new Element(OptXmlTags.SpatialReferenceData_Tag);
    }
    if (refDataElement != null) {
        // write variable declarations
        // independent variable is t and dimension is 1, these are fixed.
        Element timeVarElement = new Element(OptXmlTags.Variable_Tag);
        timeVarElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Independent);
        timeVarElement.setAttribute(OptXmlTags.VariableName_Attr, ReservedVariable.TIME.getName());
        refDataElement.addContent(timeVarElement);
        // check if t is at the first column
        int timeIndex = refData.findColumn(ReservedVariable.TIME.getName());
        if (timeIndex != 0) {
            throw new RuntimeException("t must be the first column");
        }
        // add all other dependent variables, recall that the dependent variables start from 2nd column onward in reference data
        File expDataFile = File.createTempFile("expData", ".txt", ResourceUtil.getVcellHome());
        PrintWriter pw = new PrintWriter(expDataFile);
        pw.print("# Time\t");
        for (int i = 1; i < refData.getNumDataColumns(); i++) {
            ReferenceDataMappingSpec rdms = parameterEstimationTask.getModelOptimizationSpec().getReferenceDataMappingSpec(refData.getColumnNames()[i]);
            Element variableElement = new Element(OptXmlTags.Variable_Tag);
            variableElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Dependent);
            variableElement.setAttribute(OptXmlTags.VariableName_Attr, rdms.getModelObject().getName());
            refDataElement.addContent(variableElement);
            pw.print(refData.getColumnNames()[i] + "\t");
        }
        pw.println();
        // write data
        for (int i = 0; i < refData.getNumDataRows(); i++) {
            double[] data = refData.getDataByRow(i);
            for (int j = 0; j < data.length; j++) {
                pw.print(data[j] + "\t");
            }
            pw.println();
        }
        pw.close();
        Element dataFileElement = new Element(OptXmlTags.ExperimentalDataFile_Tag);
        dataFileElement.setAttribute(OptXmlTags.ExperimentalDataFile_Attr_LastRow, (refData.getNumDataRows() + 1) + "");
        dataFileElement.addContent(expDataFile.getAbsolutePath());
        refDataElement.addContent(dataFileElement);
    }
    return refDataElement;
}
Also used : SpatialReferenceData(cbit.vcell.opt.SpatialReferenceData) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) ReferenceData(cbit.vcell.opt.ReferenceData) ReferenceDataMappingSpec(cbit.vcell.modelopt.ReferenceDataMappingSpec) Element(org.jdom.Element) File(java.io.File) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) SpatialReferenceData(cbit.vcell.opt.SpatialReferenceData) Constraint(cbit.vcell.opt.Constraint) PrintWriter(java.io.PrintWriter)

Example 2 with SpatialReferenceData

use of cbit.vcell.opt.SpatialReferenceData in project vcell by virtualcell.

the class OptXmlWriter method getModelXML.

public static Element getModelXML(PdeObjectiveFunction pdeObjectiveFunction, String[] parameterNames) {
    Element modelElement = new Element(OptXmlTags.Model_Tag);
    try {
        SpatialReferenceData refData = pdeObjectiveFunction.getReferenceData();
        double refDataEndTime = refData.getDataByRow(refData.getNumDataRows() - 1)[0];
        // 
        // post the problem either as an IDA or CVODE model
        // 
        org.vcell.util.document.SimulationVersion simVersion = new org.vcell.util.document.SimulationVersion(new KeyValue("12345"), "name", new org.vcell.util.document.User("user", new KeyValue("123")), new org.vcell.util.document.GroupAccessNone(), // versionBranchPointRef
        null, // branchID
        new java.math.BigDecimal(1.0), new java.util.Date(), org.vcell.util.document.VersionFlag.Archived, "", null);
        Simulation simulation = new Simulation(simVersion, pdeObjectiveFunction.getMathDescription());
        simulation.getMeshSpecification().setSamplingSize(refData.getDataISize());
        double[] times = refData.getDataByColumn(0);
        double minDt = Double.POSITIVE_INFINITY;
        for (int i = 1; i < times.length; i++) {
            minDt = Math.min(minDt, times[i] - times[i - 1]);
        }
        simulation.getSolverTaskDescription().setTimeBounds(new cbit.vcell.solver.TimeBounds(0.0, refDataEndTime));
        simulation.getSolverTaskDescription().setSolverDescription(SolverDescription.FiniteVolume);
        simulation.getSolverTaskDescription().setTimeStep(new TimeStep(minDt / 5, minDt / 5, minDt / 5));
        // clone and resample geometry
        Geometry resampledGeometry = null;
        try {
            resampledGeometry = (Geometry) BeanUtils.cloneSerializable(simulation.getMathDescription().getGeometry());
            GeometrySurfaceDescription geoSurfaceDesc = resampledGeometry.getGeometrySurfaceDescription();
            ISize newSize = simulation.getMeshSpecification().getSamplingSize();
            geoSurfaceDesc.setVolumeSampleSize(newSize);
            geoSurfaceDesc.updateAll();
        } catch (Exception e) {
            e.printStackTrace();
            throw new SolverException(e.getMessage());
        }
        SimulationTask simTask = new SimulationTask(new SimulationJob(simulation, 0, pdeObjectiveFunction.getFieldDataIDSs()), 0);
        StringWriter simulationInputStringWriter = new StringWriter();
        FiniteVolumeFileWriter fvFileWriter = new FiniteVolumeFileWriter(new PrintWriter(simulationInputStringWriter, true), simTask, resampledGeometry, pdeObjectiveFunction.getWorkingDirectory());
        fvFileWriter.write(parameterNames);
        simulationInputStringWriter.close();
        modelElement.setAttribute(OptXmlTags.ModelType_Attr, OptXmlTags.ModelType_Attr_FVSOLVER);
        CDATA simulationInputText = new CDATA(simulationInputStringWriter.getBuffer().toString());
        modelElement.addContent(simulationInputText);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new OptimizationException("failed to create fv input file: " + e.getMessage());
    }
    return modelElement;
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) KeyValue(org.vcell.util.document.KeyValue) SimulationTask(cbit.vcell.messaging.server.SimulationTask) GeometrySurfaceDescription(cbit.vcell.geometry.surface.GeometrySurfaceDescription) ISize(org.vcell.util.ISize) Element(org.jdom.Element) FiniteVolumeFileWriter(cbit.vcell.solvers.FiniteVolumeFileWriter) TimeStep(cbit.vcell.solver.TimeStep) StringWriter(java.io.StringWriter) SpatialReferenceData(cbit.vcell.opt.SpatialReferenceData) SimulationJob(cbit.vcell.solver.SimulationJob) PrintWriter(java.io.PrintWriter) Constraint(cbit.vcell.opt.Constraint) OptimizationException(cbit.vcell.opt.OptimizationException) XMLStreamException(javax.xml.stream.XMLStreamException) SBMLException(org.sbml.jsbml.SBMLException) XmlParseException(cbit.vcell.xml.XmlParseException) SolverException(cbit.vcell.solver.SolverException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) Geometry(cbit.vcell.geometry.Geometry) Simulation(cbit.vcell.solver.Simulation) SolverException(cbit.vcell.solver.SolverException) CDATA(org.jdom.CDATA)

Example 3 with SpatialReferenceData

use of cbit.vcell.opt.SpatialReferenceData in project vcell by virtualcell.

the class OptXmlWriter method getDataXML.

public static Element getDataXML(ReferenceData refData) {
    Element refDataElement = null;
    if (refData instanceof SimpleReferenceData) {
        refDataElement = new Element(OptXmlTags.SimpleReferenceData_Tag);
    } else if (refData instanceof SpatialReferenceData) {
        refDataElement = new Element(OptXmlTags.SpatialReferenceData_Tag);
    }
    if (refDataElement != null) {
        // write variable declarations
        // independent variable is t and dimension is 1, these are fixed.
        Element timeVarElement = new Element(OptXmlTags.Variable_Tag);
        timeVarElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Independent);
        timeVarElement.setAttribute(OptXmlTags.VariableName_Attr, ReservedVariable.TIME.getName());
        timeVarElement.setAttribute(OptXmlTags.VariableDimension_Attr, "1");
        refDataElement.addContent(timeVarElement);
        // check if t is at the first column
        int timeIndex = refData.findColumn(ReservedVariable.TIME.getName());
        if (timeIndex != 0) {
            throw new RuntimeException("t must be the first column");
        }
        // add all other dependent variables, recall that the dependent variables start from 2nd column onward in reference data
        for (int i = 1; i < refData.getNumDataColumns(); i++) {
            Element variableElement = new Element(OptXmlTags.Variable_Tag);
            variableElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Dependent);
            variableElement.setAttribute(OptXmlTags.VariableName_Attr, refData.getColumnNames()[i]);
            variableElement.setAttribute(OptXmlTags.VariableDimension_Attr, refData.getDataSize() + "");
            refDataElement.addContent(variableElement);
        }
        // write data
        Element dataRowElement = new Element(OptXmlTags.Datarow_Tag);
        for (int i = 0; i < refData.getNumDataRows(); i++) {
            double[] data = refData.getDataByRow(i);
            Element rowElement = new Element(OptXmlTags.Row_Tag);
            StringBuffer rowText = new StringBuffer();
            for (int j = 0; j < data.length; j++) {
                rowText.append(data[j] + " ");
            }
            rowElement.addContent(rowText.toString());
            dataRowElement.addContent(rowElement);
        }
        refDataElement.addContent(dataRowElement);
        // write weights
        Element weightRowElement = new Element(OptXmlTags.WeightDatarow_Tag);
        // add weight type
        if (// print weights in multiple rows, each row has one-to-more elements
        refData.getWeights() instanceof ElementWeights) {
            weightRowElement.setAttribute(OptXmlTags.WeightType_Attr, OptXmlTags.WeightType_Attr_Element);
            double[][] weightData = ((ElementWeights) refData.getWeights()).getWeightData();
            // elementWeights: first dimensin is number of rows, second dimension is number of vars
            for (int i = 0; i < weightData.length; i++) {
                double[] rowData = weightData[i];
                Element rowElement = new Element(OptXmlTags.Row_Tag);
                StringBuffer rowText = new StringBuffer();
                for (int j = 0; j < rowData.length; j++) {
                    rowText.append(rowData[j] + " ");
                }
                rowElement.addContent(rowText.toString());
                weightRowElement.addContent(rowElement);
            }
        } else if (// print weights in one row, the row has one-to-more elements
        refData.getWeights() instanceof VariableWeights) {
            weightRowElement.setAttribute(OptXmlTags.WeightType_Attr, OptXmlTags.WeightType_Attr_Variable);
            double[] weightData = ((VariableWeights) refData.getWeights()).getWeightData();
            Element rowElement = new Element(OptXmlTags.Row_Tag);
            StringBuffer rowText = new StringBuffer();
            for (int j = 0; j < weightData.length; j++) {
                rowText.append(weightData[j] + " ");
            }
            rowElement.addContent(rowText.toString());
            weightRowElement.addContent(rowElement);
        } else // time weights. Print weights in multiple rows, each row has one element
        {
            weightRowElement.setAttribute(OptXmlTags.WeightType_Attr, OptXmlTags.WeightType_Attr_Time);
            double[] weightData = ((TimeWeights) refData.getWeights()).getWeightData();
            for (int j = 0; j < weightData.length; j++) {
                Element rowElement = new Element(OptXmlTags.Row_Tag);
                StringBuffer rowText = new StringBuffer();
                rowText.append(weightData[j] + " ");
                rowElement.addContent(rowText.toString());
                weightRowElement.addContent(rowElement);
            }
        }
        refDataElement.addContent(weightRowElement);
    }
    return refDataElement;
}
Also used : VariableWeights(cbit.vcell.opt.VariableWeights) ElementWeights(cbit.vcell.opt.ElementWeights) Element(org.jdom.Element) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) SpatialReferenceData(cbit.vcell.opt.SpatialReferenceData) Constraint(cbit.vcell.opt.Constraint)

Example 4 with SpatialReferenceData

use of cbit.vcell.opt.SpatialReferenceData in project vcell by virtualcell.

the class OptXmlWriter method getObjectiveFunctionXML.

public static Element getObjectiveFunctionXML(OptimizationSpec optimizationSpec) {
    Element objectiveFunctionElement = new Element(OptXmlTags.ObjectiveFunction_Tag);
    ObjectiveFunction objectiveFunction = optimizationSpec.getObjectiveFunction();
    if (objectiveFunction instanceof ExplicitObjectiveFunction) {
        ExplicitObjectiveFunction explicitObjectiveFunction = (ExplicitObjectiveFunction) objectiveFunction;
        objectiveFunctionElement.setAttribute(OptXmlTags.ObjectiveFunctionType_Attr, OptXmlTags.ObjectiveFunctionType_Attr_Explicit);
        Element expressionElement = new Element(OptXmlTags.Expression_Tag);
        expressionElement.addContent(explicitObjectiveFunction.getExpression().infix());
        objectiveFunctionElement.addContent(expressionElement);
    } else if (objectiveFunction instanceof ExplicitFitObjectiveFunction) {
        ExplicitFitObjectiveFunction explicitFitObjectiveFunction = (ExplicitFitObjectiveFunction) objectiveFunction;
        objectiveFunctionElement.setAttribute(OptXmlTags.ObjectiveFunctionType_Attr, OptXmlTags.ObjectiveFunctionType_Attr_ExplicitFit);
        // add expression list
        Element expressionListElement = new Element(OptXmlTags.ExpressionList_Tag);
        objectiveFunctionElement.addContent(expressionListElement);
        ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs = explicitFitObjectiveFunction.getExpressionDataPairs();
        for (int i = 0; i < expDataPairs.length; i++) {
            Element expressionElement = new Element(OptXmlTags.Expression_Tag);
            expressionElement.setAttribute(OptXmlTags.ExpRefDataColumnIndex_Attr, expDataPairs[i].getReferenceDataIndex() + "");
            expressionElement.addContent(expDataPairs[i].getFitFunction().infix());
            expressionListElement.addContent(expressionElement);
        }
        if (explicitFitObjectiveFunction.getReferenceData() instanceof SimpleReferenceData) {
            Element dataElement = getDataXML(explicitFitObjectiveFunction.getReferenceData());
            objectiveFunctionElement.addContent(dataElement);
        } else {
            throw new OptimizationException("Optimization XML Writer exports SimpleReferenceData only.");
        }
    } else if (objectiveFunction instanceof PdeObjectiveFunction) {
        PdeObjectiveFunction pdeObjectiveFunction = (PdeObjectiveFunction) objectiveFunction;
        objectiveFunctionElement.setAttribute(OptXmlTags.ObjectiveFunctionType_Attr, OptXmlTags.ObjectiveFunctionType_Attr_PDE);
        SpatialReferenceData refData = pdeObjectiveFunction.getReferenceData();
        Element dataElement = getDataXML(refData);
        objectiveFunctionElement.addContent(dataElement);
        Element modelElement = getModelXML(pdeObjectiveFunction, optimizationSpec.getParameterNames());
        objectiveFunctionElement.addContent(modelElement);
        Simulation tempSim = new Simulation(pdeObjectiveFunction.getMathDescription());
        SimulationSymbolTable simSymbolTable = new SimulationSymbolTable(tempSim, 0);
        // 
        for (int i = 1; i < refData.getNumVariables(); i++) {
            Element modelMappingElement = new Element(OptXmlTags.ModelMapping_Tag);
            modelMappingElement.setAttribute(OptXmlTags.ModelMappingDataColumn_Attr, refData.getVariableNames()[i]);
            modelMappingElement.setAttribute(OptXmlTags.ModelMappingWeight_Attr, String.valueOf(refData.getVariableWeights()[i]));
            Expression mappingExpression = null;
            try {
                Variable var = pdeObjectiveFunction.getMathDescription().getVariable(refData.getVariableNames()[i]);
                if (var instanceof Function) {
                    Expression exp = new Expression(var.getExpression());
                    exp.bindExpression(simSymbolTable);
                    exp = MathUtilities.substituteFunctions(exp, simSymbolTable);
                    mappingExpression = exp.flatten();
                } else {
                    mappingExpression = new Expression(var.getName());
                }
            } catch (ExpressionException e) {
                e.printStackTrace();
                throw new OptimizationException(e.getMessage());
            }
            modelMappingElement.addContent(mappingExpression.infix());
            objectiveFunctionElement.addContent(modelMappingElement);
        }
    }
    return objectiveFunctionElement;
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) ReservedVariable(cbit.vcell.math.ReservedVariable) Variable(cbit.vcell.math.Variable) Element(org.jdom.Element) PdeObjectiveFunction(cbit.vcell.opt.PdeObjectiveFunction) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) ExplicitObjectiveFunction(cbit.vcell.opt.ExplicitObjectiveFunction) ObjectiveFunction(cbit.vcell.opt.ObjectiveFunction) PdeObjectiveFunction(cbit.vcell.opt.PdeObjectiveFunction) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) Constraint(cbit.vcell.opt.Constraint) ExpressionException(cbit.vcell.parser.ExpressionException) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) Function(cbit.vcell.math.Function) ExplicitObjectiveFunction(cbit.vcell.opt.ExplicitObjectiveFunction) ObjectiveFunction(cbit.vcell.opt.ObjectiveFunction) PdeObjectiveFunction(cbit.vcell.opt.PdeObjectiveFunction) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) ExplicitObjectiveFunction(cbit.vcell.opt.ExplicitObjectiveFunction) SpatialReferenceData(cbit.vcell.opt.SpatialReferenceData)

Aggregations

Constraint (cbit.vcell.opt.Constraint)4 SpatialReferenceData (cbit.vcell.opt.SpatialReferenceData)4 Element (org.jdom.Element)4 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)3 OptimizationException (cbit.vcell.opt.OptimizationException)2 ExpressionException (cbit.vcell.parser.ExpressionException)2 Simulation (cbit.vcell.solver.Simulation)2 PrintWriter (java.io.PrintWriter)2 Geometry (cbit.vcell.geometry.Geometry)1 GeometrySurfaceDescription (cbit.vcell.geometry.surface.GeometrySurfaceDescription)1 Function (cbit.vcell.math.Function)1 ReservedVariable (cbit.vcell.math.ReservedVariable)1 Variable (cbit.vcell.math.Variable)1 SimulationTask (cbit.vcell.messaging.server.SimulationTask)1 ReferenceDataMappingSpec (cbit.vcell.modelopt.ReferenceDataMappingSpec)1 ElementWeights (cbit.vcell.opt.ElementWeights)1 ExplicitFitObjectiveFunction (cbit.vcell.opt.ExplicitFitObjectiveFunction)1 ExplicitObjectiveFunction (cbit.vcell.opt.ExplicitObjectiveFunction)1 ObjectiveFunction (cbit.vcell.opt.ObjectiveFunction)1 PdeObjectiveFunction (cbit.vcell.opt.PdeObjectiveFunction)1