Search in sources :

Example 6 with MathOverrides

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

the class ITextWriter method writeSimulation.

// container can be a chapter or a section of a chapter.
protected void writeSimulation(Section container, Simulation sim) throws DocumentException {
    if (sim == null) {
        return;
    }
    Section simSection = container.addSection(sim.getName(), container.numberDepth() + 1);
    writeMetadata(simSection, sim.getName(), sim.getDescription(), null, "Simulation ");
    // add overriden params
    Table overParamTable = null;
    MathOverrides mo = sim.getMathOverrides();
    if (mo != null) {
        String[] constants = mo.getOverridenConstantNames();
        for (int i = 0; i < constants.length; i++) {
            String actualStr = "", defStr = "";
            Expression tempExp = mo.getDefaultExpression(constants[i]);
            if (tempExp != null) {
                defStr = tempExp.infix();
            }
            if (mo.isScan(constants[i])) {
                actualStr = mo.getConstantArraySpec(constants[i]).toString();
            } else {
                tempExp = mo.getActualExpression(constants[i], 0);
                if (tempExp != null) {
                    actualStr = tempExp.infix();
                }
            }
            if (overParamTable == null) {
                overParamTable = getTable(3, 75, 1, 3, 3);
                overParamTable.setAlignment(Table.ALIGN_LEFT);
                overParamTable.addCell(createCell("Overriden Parameters", getBold(DEF_HEADER_FONT_SIZE), 3, 1, Element.ALIGN_CENTER, true));
                overParamTable.addCell(createHeaderCell("Name", getBold(), 1));
                overParamTable.addCell(createHeaderCell("Actual Value", getBold(), 1));
                overParamTable.addCell(createHeaderCell("Default Value", getBold(), 1));
            }
            overParamTable.addCell(createCell(constants[i], getFont()));
            overParamTable.addCell(createCell(actualStr, getFont()));
            overParamTable.addCell(createCell(defStr, getFont()));
        }
    }
    if (overParamTable != null) {
        simSection.add(overParamTable);
    }
    // add spatial details
    // sim.isSpatial();
    Table meshTable = null;
    MeshSpecification mesh = sim.getMeshSpecification();
    if (mesh != null) {
        Geometry geom = mesh.getGeometry();
        Extent extent = geom.getExtent();
        String extentStr = "(" + extent.getX() + ", " + extent.getY() + ", " + extent.getZ() + ")";
        ISize meshSize = mesh.getSamplingSize();
        String meshSizeStr = "(" + meshSize.getX() + ", " + meshSize.getY() + ", " + meshSize.getZ() + ")";
        meshTable = getTable(2, 75, 1, 3, 3);
        meshTable.setAlignment(Table.ALIGN_LEFT);
        meshTable.addCell(createCell("Geometry Setting", getBold(DEF_HEADER_FONT_SIZE), 2, 1, Element.ALIGN_CENTER, true));
        meshTable.addCell(createCell("Geometry Size (um)", getFont()));
        meshTable.addCell(createCell(extentStr, getFont()));
        meshTable.addCell(createCell("Mesh Size (elements)", getFont()));
        meshTable.addCell(createCell(meshSizeStr, getFont()));
    }
    if (meshTable != null) {
        simSection.add(meshTable);
    }
    // write advanced sim settings
    Table simAdvTable = null;
    SolverTaskDescription solverDesc = sim.getSolverTaskDescription();
    if (solverDesc != null) {
        String solverName = solverDesc.getSolverDescription().getDisplayLabel();
        simAdvTable = getTable(2, 75, 1, 3, 3);
        simAdvTable.setAlignment(Table.ALIGN_LEFT);
        simAdvTable.addCell(createCell("Advanced Settings", getBold(DEF_HEADER_FONT_SIZE), 2, 1, Element.ALIGN_CENTER, true));
        simAdvTable.addCell(createCell("Solver Name", getFont()));
        simAdvTable.addCell(createCell(solverName, getFont()));
        simAdvTable.addCell(createCell("Time Bounds - Starting", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeBounds().getStartingTime(), getFont()));
        simAdvTable.addCell(createCell("Time Bounds - Ending", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeBounds().getEndingTime(), getFont()));
        simAdvTable.addCell(createCell("Time Step - Min", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeStep().getMinimumTimeStep(), getFont()));
        simAdvTable.addCell(createCell("Time Step - Default", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeStep().getDefaultTimeStep(), getFont()));
        simAdvTable.addCell(createCell("Time Step - Max", getFont()));
        simAdvTable.addCell(createCell("" + solverDesc.getTimeStep().getMaximumTimeStep(), getFont()));
        ErrorTolerance et = solverDesc.getErrorTolerance();
        if (et != null) {
            simAdvTable.addCell(createCell("Error Tolerance - Absolute", getFont()));
            simAdvTable.addCell(createCell("" + et.getAbsoluteErrorTolerance(), getFont()));
            simAdvTable.addCell(createCell("Error Tolerance - Relative", getFont()));
            simAdvTable.addCell(createCell("" + et.getRelativeErrorTolerance(), getFont()));
        }
        OutputTimeSpec ots = solverDesc.getOutputTimeSpec();
        if (ots.isDefault()) {
            simAdvTable.addCell(createCell("Keep Every", getFont()));
            simAdvTable.addCell(createCell("" + ((DefaultOutputTimeSpec) ots).getKeepEvery(), getFont()));
            simAdvTable.addCell(createCell("Keep At Most", getFont()));
            simAdvTable.addCell(createCell("" + ((DefaultOutputTimeSpec) ots).getKeepAtMost(), getFont()));
        } else if (ots.isUniform()) {
            simAdvTable.addCell(createCell("Output Time Step", getFont()));
            simAdvTable.addCell(createCell("" + ((UniformOutputTimeSpec) ots).getOutputTimeStep(), getFont()));
        } else if (ots.isExplicit()) {
            simAdvTable.addCell(createCell("Output Time Points", getFont()));
            simAdvTable.addCell(createCell("" + ((ExplicitOutputTimeSpec) ots).toCommaSeperatedOneLineOfString(), getFont()));
        }
        simAdvTable.addCell(createCell("Use Symbolic Jacobian (T/F)", getFont()));
        simAdvTable.addCell(createCell((solverDesc.getUseSymbolicJacobian() ? " T " : " F "), getFont()));
        Constant sp = solverDesc.getSensitivityParameter();
        if (sp != null) {
            simAdvTable.addCell(createCell("Sensitivity Analysis Param", getFont()));
            simAdvTable.addCell(createCell(sp.getName(), getFont()));
        }
    }
    if (simAdvTable != null) {
        simSection.add(simAdvTable);
    }
}
Also used : Table(com.lowagie.text.Table) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) Extent(org.vcell.util.Extent) ISize(org.vcell.util.ISize) Constant(cbit.vcell.math.Constant) Section(com.lowagie.text.Section) MeshSpecification(cbit.vcell.solver.MeshSpecification) Geometry(cbit.vcell.geometry.Geometry) MathOverrides(cbit.vcell.solver.MathOverrides) DefaultOutputTimeSpec(cbit.vcell.solver.DefaultOutputTimeSpec) ExplicitOutputTimeSpec(cbit.vcell.solver.ExplicitOutputTimeSpec) OutputTimeSpec(cbit.vcell.solver.OutputTimeSpec) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) Expression(cbit.vcell.parser.Expression) ErrorTolerance(cbit.vcell.solver.ErrorTolerance) SolverTaskDescription(cbit.vcell.solver.SolverTaskDescription)

Example 7 with MathOverrides

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

the class XmlHelper method applyOverridesForSBML.

/**
 * applyOverrides: private method to apply overrides from the simulation in 'simJob' to simContext, if any.
 * 				Start off by cloning biomodel, since all the references are required in cloned simContext and is
 * 				best retained by cloning biomodel.
 * @param bm - biomodel to be cloned
 * @param sc - simulationContext to be cloned and overridden using math overrides in simulation
 * @param simJob - simulationJob from where simulation with overrides is obtained.
 * @return
 */
public static SimulationContext applyOverridesForSBML(BioModel bm, SimulationContext sc, SimulationJob simJob) {
    SimulationContext overriddenSimContext = sc;
    if (simJob != null) {
        Simulation sim = simJob.getSimulation();
        // need to clone Biomodel, simContext, etc. only if simulation has override(s)
        try {
            if (sim != null && sim.getMathOverrides().hasOverrides()) {
                // BioModel clonedBM = (BioModel)BeanUtils.cloneSerializable(bm);
                BioModel clonedBM = XMLToBioModel(new XMLSource(bioModelToXML(bm)));
                clonedBM.refreshDependencies();
                // get the simContext in cloned Biomodel that corresponds to 'sc'
                SimulationContext[] simContexts = clonedBM.getSimulationContexts();
                for (int i = 0; i < simContexts.length; i++) {
                    if (simContexts[i].getName().equals(sc.getName())) {
                        overriddenSimContext = simContexts[i];
                        break;
                    }
                }
                // 
                overriddenSimContext.getModel().refreshDependencies();
                overriddenSimContext.refreshDependencies();
                MathMapping mathMapping = overriddenSimContext.createNewMathMapping();
                MathSymbolMapping msm = mathMapping.getMathSymbolMapping();
                MathOverrides mathOverrides = sim.getMathOverrides();
                String[] moConstNames = mathOverrides.getOverridenConstantNames();
                for (int i = 0; i < moConstNames.length; i++) {
                    cbit.vcell.math.Constant overriddenConstant = mathOverrides.getConstant(moConstNames[i]);
                    // Expression overriddenExpr = mathOverrides.getActualExpression(moConstNames[i], 0);
                    Expression overriddenExpr = mathOverrides.getActualExpression(moConstNames[i], simJob.getJobIndex());
                    // The above constant (from mathoverride) is not the same instance as the one in the MathSymbolMapping hash.
                    // Hence retreive the correct instance from mathSymbolMapping (mathMapping -> mathDescription) and use it to
                    // retrieve its value (symbolTableEntry) from hash.
                    cbit.vcell.math.Variable overriddenVar = msm.findVariableByName(overriddenConstant.getName());
                    cbit.vcell.parser.SymbolTableEntry[] stes = msm.getBiologicalSymbol(overriddenVar);
                    if (stes == null) {
                        throw new NullPointerException("No matching biological symbol for : " + overriddenConstant.getName());
                    }
                    if (stes.length > 1) {
                        throw new RuntimeException("Cannot have more than one mapping entry for constant : " + overriddenConstant.getName());
                    }
                    if (stes[0] instanceof Parameter) {
                        Parameter param = (Parameter) stes[0];
                        if (param.isExpressionEditable()) {
                            if (param instanceof Kinetics.KineticsParameter) {
                                // Kinetics param has to be set separately for the integrity of the kinetics object
                                Kinetics.KineticsParameter kinParam = (Kinetics.KineticsParameter) param;
                                ReactionStep[] rs = overriddenSimContext.getModel().getReactionSteps();
                                for (int j = 0; j < rs.length; j++) {
                                    if (rs[j].getNameScope().getName().equals(kinParam.getNameScope().getName())) {
                                        rs[j].getKinetics().setParameterValue(kinParam, overriddenExpr);
                                    }
                                }
                            } else if (param instanceof cbit.vcell.model.ExpressionContainer) {
                                // If it is any other editable param, set its expression with the
                                ((cbit.vcell.model.ExpressionContainer) param).setExpression(overriddenExpr);
                            }
                        }
                    }
                // end - if (stes[0] is Parameter)
                }
            // end  - for moConstNames
            }
        // end if (sim has MathOverrides)
        } catch (Exception e) {
            e.printStackTrace(System.out);
            throw new RuntimeException("Could not apply overrides from simulation to application parameters : " + e.getMessage());
        }
    }
    // end if (simJob != null)
    return overriddenSimContext;
}
Also used : SimulationContext(cbit.vcell.mapping.SimulationContext) MathSymbolMapping(cbit.vcell.mapping.MathSymbolMapping) XMLStreamException(javax.xml.stream.XMLStreamException) SbmlException(org.vcell.sbml.SbmlException) SBMLException(org.sbml.jsbml.SBMLException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) MathOverrides(cbit.vcell.solver.MathOverrides) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) BioModel(cbit.vcell.biomodel.BioModel) ReactionStep(cbit.vcell.model.ReactionStep) MathMapping(cbit.vcell.mapping.MathMapping) Parameter(cbit.vcell.model.Parameter) Kinetics(cbit.vcell.model.Kinetics)

Example 8 with MathOverrides

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

the class SimulationWorkspace method applyChanges.

/**
 * Insert the method's description here.
 * Creation date: (5/11/2004 3:52:25 PM)
 */
private static String applyChanges(Simulation clonedSimulation, Simulation simulation) {
    // we cannot simply replace the simulation with the clone because of two reasons:
    // major - vetoable listeners are transient, thus not preserved during serialization/deserialization
    // minor - proper refreshing the simulation list would require delete/reinsert which would make it non-trivial to avoid reordering
    String errors = "";
    try {
        simulation.setName(clonedSimulation.getName());
    } catch (java.beans.PropertyVetoException exc) {
        errors += "\n" + exc.getMessage();
    }
    try {
        simulation.setDescription(clonedSimulation.getDescription());
    } catch (java.beans.PropertyVetoException exc) {
        errors += "\n" + exc.getMessage();
    }
    simulation.setMathOverrides(new MathOverrides(simulation, clonedSimulation.getMathOverrides()));
    try {
        simulation.setMeshSpecification(clonedSimulation.getMeshSpecification());
    } catch (java.beans.PropertyVetoException exc) {
        errors += "\n" + exc.getMessage();
    }
    try {
        simulation.setSolverTaskDescription(new SolverTaskDescription(simulation, clonedSimulation.getSolverTaskDescription()));
    } catch (java.beans.PropertyVetoException exc) {
        errors += "\n" + exc.getMessage();
    }
    simulation.setDataProcessingInstructions(clonedSimulation.getDataProcessingInstructions());
    simulation.setIsDirty(true);
    return errors;
}
Also used : MathOverrides(cbit.vcell.solver.MathOverrides) SolverTaskDescription(cbit.vcell.solver.SolverTaskDescription)

Example 9 with MathOverrides

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

the class RestDatabaseService method saveSimulation.

public SimulationSaveResponse saveSimulation(BiomodelSimulationSaveServerResource resource, User vcellUser, List<OverrideRepresentation> overrideRepresentations) throws PermissionException, ObjectNotFoundException, DataAccessException, SQLException, XmlParseException, PropertyVetoException, MappingException, ExpressionException {
    String simId = resource.getAttribute(VCellApiApplication.SIMULATIONID);
    KeyValue simKey = new KeyValue(simId);
    SimulationRep simRep = getSimulationRep(simKey);
    if (simRep == null) {
        throw new ObjectNotFoundException("Simulation with key " + simKey + " not found");
    }
    boolean myModel = simRep.getOwner().compareEqual(vcellUser);
    // get the bioModel
    String biomodelId = resource.getAttribute(VCellApiApplication.BIOMODELID);
    KeyValue biomodelKey = new KeyValue(biomodelId);
    BigString bioModelXML = this.databaseServerImpl.getBioModelXML(vcellUser, biomodelKey);
    BioModel bioModel = XmlHelper.XMLToBioModel(new XMLSource(bioModelXML.toString()));
    // copy the simulation as new
    Simulation origSimulation = null;
    for (Simulation sim : bioModel.getSimulations()) {
        if (sim.getKey().equals(simKey)) {
            origSimulation = sim;
        }
    }
    if (origSimulation == null) {
        throw new RuntimeException("cannot find original Simulation");
    }
    SimulationContext simContext = bioModel.getSimulationContext(origSimulation);
    Simulation newUnsavedSimulation = simContext.copySimulation(origSimulation);
    // make appropriate changes
    // MATH OVERRIDES
    MathOverrides mathOverrides = new MathOverrides(newUnsavedSimulation);
    for (OverrideRepresentation overrideRep : overrideRepresentations) {
        overrideRep.applyMathOverrides(mathOverrides);
    }
    newUnsavedSimulation.setMathOverrides(mathOverrides);
    // save bioModel
    String editedBioModelXML = XmlHelper.bioModelToXML(bioModel);
    ServerDocumentManager serverDocumentManager = new ServerDocumentManager(this.databaseServerImpl);
    String modelName = bioModel.getName();
    if (!myModel) {
        modelName = modelName + "_" + Math.abs(new Random().nextInt());
    }
    String newBioModelXML = serverDocumentManager.saveBioModel(new QueryHashtable(), vcellUser, editedBioModelXML, modelName, null);
    BioModel savedBioModel = XmlHelper.XMLToBioModel(new XMLSource(newBioModelXML));
    Simulation savedSimulation = null;
    for (Simulation sim : savedBioModel.getSimulations()) {
        if (sim.getName().equals(newUnsavedSimulation.getName())) {
            savedSimulation = sim;
        }
    }
    if (savedSimulation == null) {
        throw new RuntimeException("cannot find new Simulation");
    }
    return new SimulationSaveResponse(savedBioModel, savedSimulation);
}
Also used : QueryHashtable(cbit.sql.QueryHashtable) KeyValue(org.vcell.util.document.KeyValue) BigString(org.vcell.util.BigString) SimulationContext(cbit.vcell.mapping.SimulationContext) BigString(org.vcell.util.BigString) OverrideRepresentation(org.vcell.rest.common.OverrideRepresentation) ServerDocumentManager(cbit.vcell.modeldb.ServerDocumentManager) MathOverrides(cbit.vcell.solver.MathOverrides) Simulation(cbit.vcell.solver.Simulation) Random(java.util.Random) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) BioModel(cbit.vcell.biomodel.BioModel) XMLSource(cbit.vcell.xml.XMLSource) SimulationRep(cbit.vcell.modeldb.SimulationRep)

Example 10 with MathOverrides

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

the class MathOverridesPanel method initialize.

/**
 * Initialize the class.
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initialize() {
    try {
        // user code begin {1}
        // user code end
        setName("MathOverridesPanel");
        setLayout(new GridBagLayout());
        setSize(404, 262);
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.weightx = 1.0;
        add(getJLabelTitle(), gridBagConstraints);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(getJTableFixed().getEnclosingScrollPane(), gridBagConstraints);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.NONE;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        removeUnusedButton = new JButton();
        removeUnusedButton.setForeground(Color.red);
        removeUnusedButton.setText("remove unused parameter overrides");
        removeUnusedButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                MathOverrides mathOverrides = getMathOverrides();
                if (mathOverrides != null) {
                    mathOverrides.removeUnusedOverrides();
                    removeUnusedButton.setVisible(false);
                }
            }
        });
        add(removeUnusedButton, gridBagConstraints);
        initConnections();
        connEtoM1();
        connEtoC2();
    } catch (java.lang.Throwable ivjExc) {
        handleException(ivjExc);
    }
// user code begin {2}
// user code end
}
Also used : MathOverrides(cbit.vcell.solver.MathOverrides) GridBagConstraints(java.awt.GridBagConstraints) GridBagLayout(java.awt.GridBagLayout) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton)

Aggregations

MathOverrides (cbit.vcell.solver.MathOverrides)11 Simulation (cbit.vcell.solver.Simulation)5 Constant (cbit.vcell.math.Constant)4 Expression (cbit.vcell.parser.Expression)4 BioModel (cbit.vcell.biomodel.BioModel)3 SimulationContext (cbit.vcell.mapping.SimulationContext)3 ExpressionException (cbit.vcell.parser.ExpressionException)3 SolverTaskDescription (cbit.vcell.solver.SolverTaskDescription)3 MathMapping (cbit.vcell.mapping.MathMapping)2 MathSymbolMapping (cbit.vcell.mapping.MathSymbolMapping)2 ExplicitOutputTimeSpec (cbit.vcell.solver.ExplicitOutputTimeSpec)2 SimulationJob (cbit.vcell.solver.SimulationJob)2 TimeBounds (cbit.vcell.solver.TimeBounds)2 IOException (java.io.IOException)2 Random (java.util.Random)2 SbmlException (org.vcell.sbml.SbmlException)2 KeyValue (org.vcell.util.document.KeyValue)2 QueryHashtable (cbit.sql.QueryHashtable)1 CSGObject (cbit.vcell.geometry.CSGObject)1 Geometry (cbit.vcell.geometry.Geometry)1