Search in sources :

Example 31 with ModelUnitSystem

use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.

the class ModelTable method getModel.

/**
 * This method was created in VisualAge.
 * @return Model
 * @param rset ResultSet
 * @param log SessionLog
 */
public Model getModel(ResultSet rset, Connection con) throws SQLException, DataAccessException {
    java.math.BigDecimal groupid = rset.getBigDecimal(VersionTable.privacy_ColumnName);
    Version version = getVersion(rset, DbDriver.getGroupAccessFromGroupID(con, groupid));
    ModelUnitSystem modelUnitSystem = ModelUnitSystem.createDefaultVCModelUnitSystem();
    String unitSystemXML = rset.getString(ModelTable.table.unitSystemXML.toString());
    if (!rset.wasNull()) {
        unitSystemXML = org.vcell.util.TokenMangler.getSQLRestoredString(unitSystemXML);
        XmlReader xmlReader = new XmlReader(false);
        modelUnitSystem = xmlReader.getUnitSystem(XmlUtil.stringToXML(unitSystemXML, null).getRootElement());
    }
    return new Model(version, modelUnitSystem);
}
Also used : Version(org.vcell.util.document.Version) VCellSoftwareVersion(org.vcell.util.document.VCellSoftwareVersion) Model(cbit.vcell.model.Model) XmlReader(cbit.vcell.xml.XmlReader) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 32 with ModelUnitSystem

use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.

the class ParameterTableModel method setValueAt.

public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    Parameter parameter = getValueAt(rowIndex);
    // try {
    switch(columnIndex) {
        case COLUMN_NAME:
            {
                try {
                    if (aValue instanceof String) {
                        String newName = (String) aValue;
                        if (!parameter.getName().equals(newName)) {
                            if (parameter instanceof Kinetics.KineticsParameter) {
                                reactionStep.getKinetics().renameParameter(parameter.getName(), newName);
                            } else if (parameter instanceof Kinetics.KineticsProxyParameter) {
                                parameter.setName(newName);
                            }
                            fireTableRowsUpdated(rowIndex, rowIndex);
                        }
                    }
                } catch (ExpressionException e) {
                    e.printStackTrace(System.out);
                    PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
                } catch (java.beans.PropertyVetoException e) {
                    e.printStackTrace(System.out);
                    PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter name:\n" + e.getMessage());
                }
                break;
            }
        case COLUMN_IS_GLOBAL:
            {
                if (aValue.equals(Boolean.FALSE)) {
                    // check box has been <unset> (<true> to <false>) : change param from global to local
                    if ((parameter instanceof KineticsProxyParameter) && ((((KineticsProxyParameter) parameter).getTarget() instanceof Model.ReservedSymbol) || (((KineticsProxyParameter) parameter).getTarget() instanceof SpeciesContext) || (((KineticsProxyParameter) parameter).getTarget() instanceof ModelQuantity))) {
                        PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a " + ((KineticsProxyParameter) parameter).getTarget().getClass() + " in the model; cannot convert it to a local kinetic parameter.");
                    } else {
                        try {
                            reactionStep.getKinetics().convertParameterType(parameter, false);
                        } catch (PropertyVetoException pve) {
                            pve.printStackTrace(System.out);
                            PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + pve.getMessage());
                        } catch (ExpressionBindingException e) {
                            e.printStackTrace(System.out);
                            PopupGenerator.showErrorDialog(ownerTable, "Unable to convert parameter : \'" + parameter.getName() + "\' to local kinetics parameter : " + e.getMessage());
                        }
                    }
                } else {
                    // check box has been <set> (<false> to <true>) : change param from local to global
                    if ((parameter instanceof KineticsParameter) && (((KineticsParameter) parameter).getRole() != Kinetics.ROLE_UserDefined)) {
                        PopupGenerator.showErrorDialog(ownerTable, "Parameter : \'" + parameter.getName() + "\' is a pre-defined kinetics parameter (not user-defined); cannot convert it to a model level (global) parameter.");
                    } else {
                        ModelParameter mp = reactionStep.getKinetics().getReactionStep().getModel().getModelParameter(parameter.getName());
                        // model already had the model parameter 'param', but check if 'param' value is different from
                        // model parameter with same name. If it is, the local value will be overridden by global (model) param
                        // value, and user should be warned.
                        String choice = "Ok";
                        if (mp != null && !(mp.getExpression().compareEqual(parameter.getExpression()))) {
                            String msgStr = "Model already has a global parameter named : \'" + parameter.getName() + "\'; with value = \'" + mp.getExpression().infix() + "\'; This local parameter \'" + parameter.getName() + "\' with value = \'" + parameter.getExpression().infix() + "\' will be overridden by the global value. \nPress \'Ok' to override " + "local value with global value of \'" + parameter.getName() + "\'. \nPress \'Cancel\' to retain new local value.";
                            choice = PopupGenerator.showWarningDialog(ownerTable, msgStr, new String[] { "Ok", "Cancel" }, "Ok");
                        }
                        if (choice.equals("Ok")) {
                            try {
                                // Now 'parameter' is a local kinetic parameter. If it is not numeric, and if its expression
                                // contains other local kinetic parameters, warn user that 'parameter' cannot be promoted because
                                // of its expression containing other local parameters.
                                boolean bPromoteable = true;
                                if (!parameter.getExpression().isNumeric()) {
                                    String[] symbols = parameter.getExpression().getSymbols();
                                    for (int i = 0; i < symbols.length; i++) {
                                        if (reactionStep.getKinetics().getKineticsParameter(symbols[i]) != null) {
                                            PopupGenerator.showErrorDialog(ownerTable, "Parameter \'" + parameter.getName() + "\' contains other local kinetic parameters; Cannot convert it to global until the referenced parameters are global.");
                                            bPromoteable = false;
                                        }
                                    }
                                }
                                if (bPromoteable) {
                                    reactionStep.getKinetics().convertParameterType(parameter, true);
                                }
                            } catch (PropertyVetoException pve) {
                                pve.printStackTrace(System.out);
                                PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + pve.getMessage());
                            } catch (ExpressionBindingException e) {
                                e.printStackTrace(System.out);
                                PopupGenerator.showErrorDialog(ownerTable, "Cannot convert parameter \'" + parameter.getName() + "\' to global parameter : " + e.getMessage());
                            }
                        }
                    }
                }
                fireTableRowsUpdated(rowIndex, rowIndex);
                break;
            }
        case COLUMN_VALUE:
            {
                try {
                    if (aValue instanceof ScopedExpression) {
                        // }
                        throw new RuntimeException("unexpected value type ScopedExpression");
                    } else if (aValue instanceof String) {
                        String newExpressionString = (String) aValue;
                        if (parameter instanceof Kinetics.KineticsParameter) {
                            reactionStep.getKinetics().setParameterValue((Kinetics.KineticsParameter) parameter, new Expression(newExpressionString));
                        } else if (parameter instanceof Kinetics.KineticsProxyParameter) {
                            parameter.setExpression(new Expression(newExpressionString));
                        }
                    }
                    reactionStep.getKinetics().resolveUndefinedUnits();
                    fireTableRowsUpdated(rowIndex, rowIndex);
                } catch (java.beans.PropertyVetoException e) {
                    e.printStackTrace(System.out);
                    PopupGenerator.showErrorDialog(ownerTable, "Error:\n" + e.getMessage());
                } catch (ExpressionException e) {
                    e.printStackTrace(System.out);
                    PopupGenerator.showErrorDialog(ownerTable, "Expression error:\n" + e.getMessage());
                }
                break;
            }
        case COLUMN_UNITS:
            {
                try {
                    if (aValue instanceof String && parameter instanceof Kinetics.KineticsParameter && ((Kinetics.KineticsParameter) parameter).getRole() == Kinetics.ROLE_UserDefined) {
                        String newUnitString = (String) aValue;
                        Kinetics.KineticsParameter kineticsParm = (Kinetics.KineticsParameter) parameter;
                        ModelUnitSystem modelUnitSystem = reactionStep.getModel().getUnitSystem();
                        if (!kineticsParm.getUnitDefinition().getSymbol().equals(newUnitString)) {
                            kineticsParm.setUnitDefinition(modelUnitSystem.getInstance(newUnitString));
                            reactionStep.getKinetics().resolveUndefinedUnits();
                            fireTableRowsUpdated(rowIndex, rowIndex);
                        }
                    }
                } catch (VCUnitException e) {
                    e.printStackTrace(System.out);
                    PopupGenerator.showErrorDialog(ownerTable, "Error changing parameter unit:\n" + e.getMessage());
                }
                break;
            }
    }
// }catch (java.beans.PropertyVetoException e){
// e.printStackTrace(System.out);
// }
}
Also used : KineticsProxyParameter(cbit.vcell.model.Kinetics.KineticsProxyParameter) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) SpeciesContext(cbit.vcell.model.SpeciesContext) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) ExpressionException(cbit.vcell.parser.ExpressionException) PropertyVetoException(java.beans.PropertyVetoException) VCUnitException(cbit.vcell.units.VCUnitException) ModelParameter(cbit.vcell.model.Model.ModelParameter) ScopedExpression(cbit.gui.ScopedExpression) ModelQuantity(cbit.vcell.model.ModelQuantity) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) ScopedExpression(cbit.gui.ScopedExpression) Expression(cbit.vcell.parser.Expression) KineticsProxyParameter(cbit.vcell.model.Kinetics.KineticsProxyParameter) ModelParameter(cbit.vcell.model.Model.ModelParameter) KineticsProxyParameter(cbit.vcell.model.Kinetics.KineticsProxyParameter) Parameter(cbit.vcell.model.Parameter) KineticsParameter(cbit.vcell.model.Kinetics.KineticsParameter) UnresolvedParameter(cbit.vcell.model.Kinetics.UnresolvedParameter) Kinetics(cbit.vcell.model.Kinetics) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 33 with ModelUnitSystem

use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.

the class SimulationContext method getUnitInfo.

/**
 * SimulationContext constructor.
 * This constructor differs with the previous one with one more boolean input parameter, which specifies whether
 * the new application is a stochastic application or not.
 */
// @Deprecated
// public SimulationContext(Model model, Geometry geometry, boolean bStoch, boolean bRuleBased) throws PropertyVetoException {
// this(model,geometry,null,null,legacyType(bStoch, bRuleBased));
// }
@Override
public UnitInfo getUnitInfo() throws UnsupportedOperationException {
    if (unitInfo != null) {
        return unitInfo;
    }
    if (bioModel == null) {
        throw new UnsupportedOperationException("bioModel not set yet");
    }
    Model m = bioModel.getModel();
    ModelUnitSystem unitSys = m.getUnitSystem();
    VCUnitDefinition tu = unitSys.getTimeUnit();
    String sym = tu.getSymbol();
    unitInfo = new UnitInfo(sym);
    return unitInfo;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Model(cbit.vcell.model.Model) BioModel(cbit.vcell.biomodel.BioModel) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 34 with ModelUnitSystem

use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.

the class SpeciesContextSpec method convertConcentrationToParticles.

public Expression convertConcentrationToParticles(Expression iniConcentration) throws ExpressionException, MappingException {
    Expression iniParticlesExpr = null;
    Structure structure = getSpeciesContext().getStructure();
    double structSize = computeStructureSize();
    if (structure instanceof Membrane) {
        // particles = iniConcentration(molecules/um2)*size(um2)
        try {
            iniParticlesExpr = new Expression((Math.round(iniConcentration.evaluateConstant() * structSize)));
        } catch (ExpressionException e) {
            iniParticlesExpr = Expression.mult(iniConcentration, new Expression(structSize)).flatten();
        }
    } else {
        // convert concentration(particles/volume) to number of particles
        // particles = [iniConcentration(uM)*size(um3)]/KMOLE
        // @Note : 'kMole' variable here is used only as a var name, it does not represent the previously known ReservedSymbol KMOLE.
        ModelUnitSystem modelUnitSystem = getSimulationContext().getModel().getUnitSystem();
        VCUnitDefinition volSubstanceToStochastic = modelUnitSystem.getStochasticSubstanceUnit().divideBy(modelUnitSystem.getVolumeSubstanceUnit());
        double volSubstanceToStochasticScale = volSubstanceToStochastic.getDimensionlessScale().doubleValue();
        try {
            iniParticlesExpr = new Expression((Math.round(iniConcentration.evaluateConstant() * structSize * volSubstanceToStochasticScale)));
        } catch (ExpressionException e) {
            Expression numeratorExpr = Expression.mult(iniConcentration, new Expression(structSize));
            Expression exp = new Expression(volSubstanceToStochasticScale);
            iniParticlesExpr = Expression.mult(numeratorExpr, exp).flatten();
        }
    }
    return iniParticlesExpr;
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) ExpressionException(cbit.vcell.parser.ExpressionException) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 35 with ModelUnitSystem

use of cbit.vcell.model.ModelUnitSystem in project vcell by virtualcell.

the class CurrentClampElectricalDevice method initializeParameters.

private void initializeParameters() throws ExpressionException {
    ElectricalDevice.ElectricalDeviceParameter[] parameters = new ElectricalDevice.ElectricalDeviceParameter[3];
    // 
    // set the transmembrane current (total current, if necessary derive it from the current density).
    // 
    ElectricalDeviceParameter transMembraneCurrent = null;
    ModelUnitSystem modelUnitSystem = mathMapping.getSimulationContext().getModel().getUnitSystem();
    VCUnitDefinition currentUnit = modelUnitSystem.getCurrentUnit();
    if (currentClampStimulus instanceof TotalCurrentClampStimulus) {
        TotalCurrentClampStimulus stimulus = (TotalCurrentClampStimulus) currentClampStimulus;
        LocalParameter currentParameter = stimulus.getCurrentParameter();
        transMembraneCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TransmembraneCurrent], new Expression(currentParameter.getExpression()), ROLE_TransmembraneCurrent, currentUnit);
    } else if (currentClampStimulus instanceof CurrentDensityClampStimulus) {
        CurrentDensityClampStimulus stimulus = (CurrentDensityClampStimulus) currentClampStimulus;
        LocalParameter currentDensityParameter = stimulus.getCurrentDensityParameter();
        // 
        // here we have to determine the expression for current (from current density).
        // 
        Feature feature1 = currentClampStimulus.getElectrode().getFeature();
        Feature feature2 = mathMapping.getSimulationContext().getGroundElectrode().getFeature();
        Membrane membrane = null;
        StructureTopology structTopology = mathMapping.getSimulationContext().getModel().getStructureTopology();
        if (structTopology.getParentStructure(feature1) != null && structTopology.getOutsideFeature((Membrane) structTopology.getParentStructure(feature1)) == feature2) {
            membrane = ((Membrane) structTopology.getParentStructure(feature1));
        } else if (structTopology.getParentStructure(feature2) != null && structTopology.getOutsideFeature((Membrane) structTopology.getParentStructure(feature2)) == feature1) {
            membrane = ((Membrane) structTopology.getParentStructure(feature2));
        }
        if (membrane == null) {
            throw new RuntimeException("current clamp based on current density crosses multiple membranes, unable to " + "determine single membrane to convert current density into current in Application '" + mathMapping.getSimulationContext().getName() + "'.");
        }
        MembraneMapping membraneMapping = (MembraneMapping) mathMapping.getSimulationContext().getGeometryContext().getStructureMapping(membrane);
        StructureMappingParameter sizeParameter = membraneMapping.getSizeParameter();
        Expression area = null;
        if (mathMapping.getSimulationContext().getGeometry().getDimension() == 0 && (sizeParameter.getExpression() == null || sizeParameter.getExpression().isZero())) {
            area = membraneMapping.getNullSizeParameterValue();
        } else {
            area = new Expression(sizeParameter, mathMapping.getNameScope());
        }
        transMembraneCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TransmembraneCurrent], Expression.mult(new Expression(currentDensityParameter.getExpression()), area), ROLE_TransmembraneCurrent, currentUnit);
    } else {
        throw new RuntimeException("unexpected current clamp stimulus type : " + currentClampStimulus.getClass().getName());
    }
    ElectricalDeviceParameter totalCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TotalCurrent], new Expression(transMembraneCurrent, getNameScope()), ROLE_TotalCurrent, currentUnit);
    ElectricalDeviceParameter voltage = new ElectricalDeviceParameter(DefaultNames[ROLE_Voltage], null, ROLE_Voltage, modelUnitSystem.getVoltageUnit());
    parameters[0] = totalCurrent;
    parameters[1] = transMembraneCurrent;
    parameters[2] = voltage;
    // 
    // add any user-defined parameters
    // 
    LocalParameter[] stimulusParameters = currentClampStimulus.getLocalParameters();
    for (int i = 0; stimulusParameters != null && i < stimulusParameters.length; i++) {
        if (stimulusParameters[i].getRole() == ElectricalStimulus.ElectricalStimulusParameterType.UserDefined) {
            ElectricalDeviceParameter newParam = new ElectricalDeviceParameter(stimulusParameters[i].getName(), new Expression(stimulusParameters[i].getExpression()), ROLE_UserDefined, stimulusParameters[i].getUnitDefinition());
            parameters = (ElectricalDeviceParameter[]) BeanUtils.addElement(parameters, newParam);
        }
    }
    setParameters(parameters);
}
Also used : MembraneMapping(cbit.vcell.mapping.MembraneMapping) StructureTopology(cbit.vcell.model.Model.StructureTopology) CurrentDensityClampStimulus(cbit.vcell.mapping.CurrentDensityClampStimulus) StructureMappingParameter(cbit.vcell.mapping.StructureMapping.StructureMappingParameter) TotalCurrentClampStimulus(cbit.vcell.mapping.TotalCurrentClampStimulus) Feature(cbit.vcell.model.Feature) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Membrane(cbit.vcell.model.Membrane) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Aggregations

ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)53 Expression (cbit.vcell.parser.Expression)41 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)36 ModelParameter (cbit.vcell.model.Model.ModelParameter)17 ExpressionException (cbit.vcell.parser.ExpressionException)17 SpeciesContext (cbit.vcell.model.SpeciesContext)16 PropertyVetoException (java.beans.PropertyVetoException)16 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)14 Membrane (cbit.vcell.model.Membrane)12 Model (cbit.vcell.model.Model)12 Parameter (cbit.vcell.model.Parameter)12 BioModel (cbit.vcell.biomodel.BioModel)11 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)11 Feature (cbit.vcell.model.Feature)11 Structure (cbit.vcell.model.Structure)11 ArrayList (java.util.ArrayList)11 ReactionStep (cbit.vcell.model.ReactionStep)10 Kinetics (cbit.vcell.model.Kinetics)9 SubVolume (cbit.vcell.geometry.SubVolume)7 StructureMappingParameter (cbit.vcell.mapping.StructureMapping.StructureMappingParameter)7