Search in sources :

Example 51 with MathModel

use of cbit.vcell.mathmodel.MathModel in project vcell by virtualcell.

the class OptXmlWriter method getCoapsiOptProblemDescriptionXML.

public static Element getCoapsiOptProblemDescriptionXML(ParameterEstimationTask parameterEstimationTask, MathMappingCallback mathMappingCallback) throws IOException, XmlParseException, ExpressionException, SBMLException, XMLStreamException {
    OptimizationSpec optimizationSpec = parameterEstimationTask.getModelOptimizationMapping().getOptimizationSpec();
    Element optProblemDescriptionElement = new Element(OptXmlTags.OptProblemDescription_Tag);
    File modelSbmlFile = File.createTempFile("mathModel", ".xml", ResourceUtil.getVcellHome());
    SimulationContext simulationContext = parameterEstimationTask.getSimulationContext();
    simulationContext.refreshMathDescription(mathMappingCallback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
    MathModel vcellMathModel = new MathModel(null);
    vcellMathModel.setMathDescription(simulationContext.getMathDescription());
    // get math model string
    String sbmlString = MathModel_SBMLExporter.getSBMLString(vcellMathModel, 2, 4);
    // String modelSbml = XmlHelper.exportSBML(simulationContext.getBioModel(), 3, 1, 0, false, simulationContext, null);
    XmlUtil.writeXMLStringToFile(sbmlString, modelSbmlFile.getAbsolutePath(), true);
    Element element = new Element(OptXmlTags.MathModelSbmlFile_Tag);
    element.addContent(modelSbmlFile.getAbsolutePath());
    optProblemDescriptionElement.addContent(element);
    if (optimizationSpec.isComputeProfileDistributions()) {
        optProblemDescriptionElement.setAttribute(OptXmlTags.ComputeProfileDistributions_Attr, optimizationSpec.isComputeProfileDistributions() + "");
    }
    optProblemDescriptionElement.addContent(getVCellOptionsXML(parameterEstimationTask.getOptimizationSolverSpec()));
    optProblemDescriptionElement.addContent(getParameterDescriptionXML(optimizationSpec));
    Element dataElement = getCopasiDataXML(parameterEstimationTask);
    optProblemDescriptionElement.addContent(dataElement);
    element = getCopasiOptimizationMethodXML(parameterEstimationTask.getOptimizationSolverSpec().getCopasiOptimizationMethod());
    optProblemDescriptionElement.addContent(element);
    return optProblemDescriptionElement;
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) Element(org.jdom.Element) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) SimulationContext(cbit.vcell.mapping.SimulationContext) File(java.io.File)

Example 52 with MathModel

use of cbit.vcell.mathmodel.MathModel in project vcell by virtualcell.

the class HybridSolverTester method runHybridTest.

public void runHybridTest(String site) {
    try {
        double[] timePoints = null;
        File mathModelFile = new File(mathModelVCMLFileName);
        XMLSource vcmlSource = new XMLSource(mathModelFile);
        MathModel mathModel = XmlHelper.XMLToMathModel(vcmlSource);
        File simDataDir = mathModelFile.getParentFile();
        Simulation sim = mathModel.getSimulations()[0];
        // run multiple trials and each run will have a simID = origSimID + i
        for (int i = 0; i < numRuns; i++) {
            System.out.println("--------------Trial No: " + (startTrialNo + i) + "----------------");
            // replace random seed
            sim.getSolverTaskDescription().getSmoldynSimulationOptions().setRandomSeed(new Integer(startTrialNo + i));
            // create sim job
            int jobIndex = startTrialNo + i;
            SimulationTask simTask = new SimulationTask(new SimulationJob(sim, jobIndex, null), 0);
            /*
				 * When you want to run the multiple trials on local uncomment the line below.
				 */
            // ResourceUtil.prepareSolverExecutable(sim.getSolverTaskDescription().getSolverDescription());
            /*
				 * When you want to run the multiple trials on server (without VCell user interface), use the next line of code 
				 * Corresponding changes should be made in the shell script runhybridtest for the location of executable on server
				 */
            FVSolverStandalone fvSolver = new FVSolverStandalone(simTask, simDataDir, false);
            fvSolver.startSolver();
            SolverStatus status = fvSolver.getSolverStatus();
            while (status.getStatus() != SolverStatus.SOLVER_FINISHED && status.getStatus() != SolverStatus.SOLVER_ABORTED) {
                System.out.println("progress: " + (int) (fvSolver.getProgress() * 100) + "%");
                // ask status every 2 seconds
                Thread.sleep(2000);
                status = fvSolver.getSolverStatus();
            }
            if (status.getStatus() == SolverStatus.SOLVER_FINISHED) {
                // get data
                VCSimulationIdentifier vcSimID = new VCSimulationIdentifier(sim.getVersion().getVersionKey(), sim.getVersion().getOwner());
                VCSimulationDataIdentifier vcSimDataID = new VCSimulationDataIdentifier(vcSimID, jobIndex);
                File hdf5File = new File(simDataDir, vcSimDataID.getID() + SimDataConstants.DATA_PROCESSING_OUTPUT_EXTENSION_HDF5);
                DataOperationResults.DataProcessingOutputInfo dataProcessingOutputInfo = (DataOperationResults.DataProcessingOutputInfo) DataSetControllerImpl.getDataProcessingOutput(new DataOperation.DataProcessingOutputInfoOP(vcSimDataID, false, null), hdf5File);
                if (i == 0) {
                    // do only one time
                    timePoints = dataProcessingOutputInfo.getVariableTimePoints();
                    for (int j = 0; j < varNames.length; j++) {
                        // row: numTimePoints, col:first col time + numRuns
                        double[][] data = new double[numRuns + 1][timePoints.length];
                        data[0] = timePoints;
                        results.add(data);
                    }
                }
                // write into results after each run
                for (int j = 0; j < varNames.length; j++) {
                    results.get(j)[i + 1] = dataProcessingOutputInfo.getVariableStatValues().get(varNames[j]);
                }
                // delete the file generated for this run
                deleteSimFiles(simDataDir, vcSimDataID);
            } else {
                throw new Exception("Sover did not finish normally." + status);
            }
        }
        // write to output file, tab delimited
        if (results != null && results.size() > 0) {
            for (int j = 0; j < varNames.length; j++) {
                File file = new File(simDataDir, "SimID_" + sim.getVersion().getVersionKey().toString() + "_" + varNames[j] + "_" + startTrialNo + ".txt");
                PrintWriter pw = new PrintWriter(file);
                double[][] data = results.get(j);
                if (data != null) {
                    for (int k = 0; k < data.length; k++) {
                        if (!bPrintTime && k == 0) {
                            continue;
                        }
                        String rowStr = (k == 0) ? "Time\t" : ("trialNo_" + (startTrialNo + k - 1) + "\t");
                        double[] rowData = data[k];
                        for (int q = 0; q < rowData.length; q++) {
                            rowStr += rowData[q] + "\t";
                        }
                        pw.println(rowStr);
                    }
                }
                pw.close();
            }
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) VCSimulationIdentifier(cbit.vcell.solver.VCSimulationIdentifier) SimulationTask(cbit.vcell.messaging.server.SimulationTask) BigString(org.vcell.util.BigString) VCSimulationDataIdentifier(cbit.vcell.solver.VCSimulationDataIdentifier) FVSolverStandalone(cbit.vcell.solvers.FVSolverStandalone) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Simulation(cbit.vcell.solver.Simulation) DataOperationResults(cbit.vcell.simdata.DataOperationResults) File(java.io.File) XMLSource(cbit.vcell.xml.XMLSource) SolverStatus(cbit.vcell.solver.server.SolverStatus) SimulationJob(cbit.vcell.solver.SimulationJob) PrintWriter(java.io.PrintWriter)

Example 53 with MathModel

use of cbit.vcell.mathmodel.MathModel in project vcell by virtualcell.

the class XmlHelper method XMLToMathModel.

static MathModel XMLToMathModel(XMLSource xmlSource, boolean printkeys) throws XmlParseException {
    MathModel mathModel = null;
    if (xmlSource == null) {
        throw new XmlParseException("Invalid xml for Geometry.");
    }
    Document xmlDoc = xmlSource.getXmlDoc();
    // NOTES:
    // * The root element can be <Biomodel> (old-style vcml) OR <vcml> (new-style vcml)
    // * With the old-style vcml, the namespace was " "
    // * With the new-style vcml, there is an intermediate stage where the namespace for <vcml> root
    // was set to "http://sourceforge.net/projects/VCell/version0.4" for some models and
    // "http://sourceforge.net/projects/vcell/vcml" for some models; and the namespace for child element
    // <biomdel>, etc. was " "
    // * The final new-style vcml has (should have) the namespace "http://sourceforge.net/projects/vcell/vcml"
    // for <vcml> and all children elements.
    // The code below attempts to take care of this situation.
    Element root = xmlDoc.getRootElement();
    Namespace ns = null;
    if (root.getName().equals(XMLTags.VcmlRootNodeTag)) {
        // NEW WAY - with xml string containing xml declaration, vcml element, namespace, etc ...
        ns = root.getNamespace();
        Element mathRoot = root.getChild(XMLTags.MathModelTag, ns);
        if (mathRoot == null) {
            mathRoot = root.getChild(XMLTags.MathModelTag);
            // mathRoot was null, so obtained the <Mathmodel> element with namespace " ";
            // Re-set the namespace so that the correct XMLReader constructor is invoked.
            ns = null;
        }
        root = mathRoot;
    }
    // else - root is assumed to be old-style vcml with <Mathmodel> as root.
    // common for both new-style (with xml declaration, vcml element, etc) and old-style (mathmodel is root)
    // If namespace is null, xml is the old-style xml with mathmodel as root, so invoke XMLReader without namespace argument.
    XmlReader reader = null;
    if (ns == null) {
        reader = new XmlReader(printkeys);
    } else {
        reader = new XmlReader(printkeys, ns);
    }
    mathModel = reader.getMathModel(root);
    mathModel.refreshDependencies();
    return mathModel;
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) Element(org.jdom.Element) Document(org.jdom.Document) VCDocument(org.vcell.util.document.VCDocument) Namespace(org.jdom.Namespace)

Example 54 with MathModel

use of cbit.vcell.mathmodel.MathModel in project vcell by virtualcell.

the class StandaloneRuleBasedTest method main.

public static void main(String[] args) {
    try {
        PropertyLoader.loadProperties();
    } catch (Exception e) {
        e.printStackTrace();
    }
    final int numTrials = 40;
    // 30 seconds
    final long bngTimeoutDurationMS = 30000;
    VCDatabaseVisitor vcDatabaseVisitor = new VCDatabaseVisitor() {

        @Override
        public void visitMathModel(MathModel mathModel, PrintStream logFilePrintStream) {
            throw new IllegalArgumentException("Not Implemented");
        }

        @Override
        public void visitGeometry(Geometry geometry, PrintStream logFilePrintStream) {
            throw new IllegalArgumentException("Not Implemented");
        }

        @Override
        public void visitBioModel(BioModel bioModel, PrintStream logFilePrintStream) {
            SimulationContext[] simulationContexts = bioModel.getSimulationContexts();
            for (SimulationContext simContext : simulationContexts) {
                if ((simContext.getApplicationType() == Application.NETWORK_STOCHASTIC) && simContext.getGeometry().getDimension() == 0) {
                    File baseDirectory = createDirFile(simContext);
                    try {
                        checkNonspatialStochasticSimContext(simContext, baseDirectory, numTrials, bngTimeoutDurationMS);
                    } catch (Exception e) {
                        e.printStackTrace();
                        if (!e.getMessage().contains("Only Mass Action Kinetics supported ")) {
                            StochtestFileUtils.writeMessageTofile(baseDirectory, e.getMessage());
                        }
                    }
                }
            }
        }

        @Override
        public boolean filterMathModel(MathModelInfo mathModelInfo) {
            return false;
        }

        @Override
        public boolean filterGeometry(GeometryInfo geometryInfo) {
            return false;
        }

        @Override
        public boolean filterBioModel(BioModelInfo bioModelInfo) {
            return // bioModelInfo.getVersion().getName().equals("model");
            bioModelInfo.getVersion().getName().equals("simpleModel_Network_orig");
        }
    };
    String currentUserID = "schaff";
    String[] allUsers = new String[] { /*-all*/
    currentUserID, "-" };
    VCDatabaseScanner.scanBioModels(allUsers, vcDatabaseVisitor, false);
}
Also used : PrintStream(java.io.PrintStream) MathModel(cbit.vcell.mathmodel.MathModel) BioModelInfo(org.vcell.util.document.BioModelInfo) MathModelInfo(org.vcell.util.document.MathModelInfo) SimulationContext(cbit.vcell.mapping.SimulationContext) Geometry(cbit.vcell.geometry.Geometry) BioModel(cbit.vcell.biomodel.BioModel) GeometryInfo(cbit.vcell.geometry.GeometryInfo) VCDatabaseVisitor(cbit.vcell.modeldb.VCDatabaseVisitor) File(java.io.File)

Example 55 with MathModel

use of cbit.vcell.mathmodel.MathModel in project vcell by virtualcell.

the class MathGenerationDebugger method compareMath_latest.

private void compareMath_latest() {
    try {
        MathModel mathModel1 = new MathModel(null);
        MathModel mathModel2 = new MathModel(null);
        mathModel1.setMathDescription(mathGenerationResults.mathDesc_original);
        mathModel2.setMathDescription(mathGenerationResults.mathDesc_latest);
        getMathDebuggerPanel().setMathModel1(mathModel1);
        getMathDebuggerPanel().setMathModel2(mathModel2);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        DialogUtils.showErrorDialog(this, e.getMessage(), e);
    }
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel)

Aggregations

MathModel (cbit.vcell.mathmodel.MathModel)70 BioModel (cbit.vcell.biomodel.BioModel)26 Simulation (cbit.vcell.solver.Simulation)24 DataAccessException (org.vcell.util.DataAccessException)21 Geometry (cbit.vcell.geometry.Geometry)20 MathDescription (cbit.vcell.math.MathDescription)20 SimulationContext (cbit.vcell.mapping.SimulationContext)19 XmlParseException (cbit.vcell.xml.XmlParseException)13 MathModelInfo (org.vcell.util.document.MathModelInfo)12 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)11 UserCancelException (org.vcell.util.UserCancelException)11 VCDocument (org.vcell.util.document.VCDocument)11 XMLSource (cbit.vcell.xml.XMLSource)10 ExpressionException (cbit.vcell.parser.ExpressionException)9 File (java.io.File)9 KeyValue (org.vcell.util.document.KeyValue)8 PropertyVetoException (java.beans.PropertyVetoException)7 IOException (java.io.IOException)7 BigString (org.vcell.util.BigString)7 BioModelInfo (org.vcell.util.document.BioModelInfo)7