Search in sources :

Example 36 with Parameter

use of cbit.vcell.model.Parameter 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 37 with Parameter

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

the class TransformMassActions method transformOne.

public TransformedReaction transformOne(ReactionStep origRS) throws PropertyVetoException, IOException, ClassNotFoundException {
    TransformedReaction transformedRS = new TransformedReaction();
    if (origRS instanceof SimpleReaction) {
        // we separate mass action and general law, because if it passes, mass action uses label 'ok' and general uses label 'stochastic capable'
        if (origRS.getKinetics().getKineticsDescription().equals(KineticsDescription.MassAction)) {
            Expression rateExp = origRS.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate).getExpression();
            try {
                Parameter forwardRateParameter = origRS.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_KForward);
                Parameter reverseRateParameter = origRS.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_KReverse);
                MassActionSolver.MassActionFunction maFunc = MassActionSolver.solveMassAction(forwardRateParameter, reverseRateParameter, rateExp, origRS);
                // set transformed reaction step
                transformedRS.setTransformType(TransformedReaction.TRANSFORMABLE_WITH_NOCHANGE);
                transformedRS.setTransformRemark(TransformedReaction.Label_Ok);
                transformedRS.setMassActionFunction(maFunc);
            } catch (Exception e) {
                // Mass Action Solver failed to parse the rate expression
                transformedRS.setMassActionFunction(new MassActionSolver.MassActionFunction());
                transformedRS.setTransformType(TransformedReaction.NOTRANSFORMABLE);
                transformedRS.setTransformRemark(TransformedReaction.Label_Failed + " " + e.getMessage());
            }
        } else if (origRS.getKinetics().getKineticsDescription().equals(KineticsDescription.General)) {
            Expression rateExp = origRS.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate).getExpression();
            try {
                Parameter forwardRateParameter = null;
                Parameter reverseRateParameter = null;
                MassActionSolver.MassActionFunction maFunc = MassActionSolver.solveMassAction(forwardRateParameter, reverseRateParameter, rateExp, origRS);
                // set transformed reaction step
                transformedRS.setMassActionFunction(maFunc);
                // if number of reactant is 0, or number of product is 0, we can not transform it to mass action law
                if (((SimpleReaction) origRS).getNumReactants() == 0 || ((SimpleReaction) origRS).getNumProducts() == 0) {
                    transformedRS.setTransformType(TransformedReaction.NOTRANSFORMABLE_STOCHCAPABLE);
                    transformedRS.setTransformRemark(TransformedReaction.Label_StochForm_NotTransformable);
                } else {
                    transformedRS.setTransformType(TransformedReaction.TRANSFORMABLE);
                    transformedRS.setTransformRemark(TransformedReaction.Label_Transformable);
                }
            } catch (Exception e) {
                // Mass Action Solver failed to parse the rate expression
                transformedRS.setMassActionFunction(new MassActionSolver.MassActionFunction());
                transformedRS.setTransformType(TransformedReaction.NOTRANSFORMABLE);
                transformedRS.setTransformRemark(TransformedReaction.Label_Failed + " " + e.getMessage());
            }
        } else // other kinetic rate laws other than MassAction and General
        {
            transformedRS.setMassActionFunction(new MassActionSolver.MassActionFunction());
            transformedRS.setTransformType(TransformedReaction.NOTRANSFORMABLE);
            transformedRS.setTransformRemark(TransformedReaction.Label_Failed + TransformedReaction.Label_FailedOtherReacLaw);
        }
    } else // flux
    {
        if (origRS instanceof FluxReaction) {
            // fluxes which are described by general density function/permeability
            if (origRS.getKinetics().getKineticsDescription().equals(KineticsDescription.General) || origRS.getKinetics().getKineticsDescription().equals(KineticsDescription.GeneralPermeability)) {
                Expression rateExp = origRS.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_ReactionRate).getExpression();
                try {
                    // forward and reverse rate parameters may be null
                    Parameter forwardRateParameter = null;
                    Parameter reverseRateParameter = null;
                    if (origRS.getKinetics().getKineticsDescription().equals(KineticsDescription.GeneralPermeability)) {
                        forwardRateParameter = origRS.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_Permeability);
                        reverseRateParameter = origRS.getKinetics().getKineticsParameterFromRole(Kinetics.ROLE_Permeability);
                    }
                    MassActionSolver.MassActionFunction maFunc = MassActionSolver.solveMassAction(forwardRateParameter, reverseRateParameter, rateExp, origRS);
                    // set transformed reaction step
                    transformedRS.setMassActionFunction(maFunc);
                    transformedRS.setTransformType(TransformedReaction.NOTRANSFORMABLE_STOCHCAPABLE);
                    transformedRS.setTransformRemark(TransformedReaction.Label_StochForm_NotTransformable);
                } catch (Exception e) {
                    // Mass Action Solver failed to parse the rate expression
                    transformedRS.setMassActionFunction(new MassActionSolver.MassActionFunction());
                    transformedRS.setTransformType(TransformedReaction.NOTRANSFORMABLE);
                    transformedRS.setTransformRemark(TransformedReaction.Label_Failed + " " + e.getMessage());
                }
            } else // other fluxes which are not described by general density function or general permeability
            {
                transformedRS.setMassActionFunction(new MassActionSolver.MassActionFunction());
                transformedRS.setTransformType(TransformedReaction.NOTRANSFORMABLE);
                transformedRS.setTransformRemark(TransformedReaction.Label_Failed + TransformedReaction.Label_FailedOtherFluxLaw);
            }
        }
    }
    return transformedRS;
}
Also used : SimpleReaction(cbit.vcell.model.SimpleReaction) Expression(cbit.vcell.parser.Expression) MassActionFunction(cbit.vcell.model.MassActionSolver.MassActionFunction) Parameter(cbit.vcell.model.Parameter) FluxReaction(cbit.vcell.model.FluxReaction) MassActionSolver(cbit.vcell.model.MassActionSolver) MassActionFunction(cbit.vcell.model.MassActionSolver.MassActionFunction) PropertyVetoException(java.beans.PropertyVetoException) IOException(java.io.IOException)

Example 38 with Parameter

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

the class ModelUnitConverter method convertVarsWithUnitFactors.

private static void convertVarsWithUnitFactors(SymbolTable oldSymbolTable, SymbolTable newSymbolTable, Parameter parameter) throws ExpressionException {
    // get old unit
    VCUnitDefinition oldExprUnit = null;
    Parameter oldParameter = (Parameter) oldSymbolTable.getEntry(parameter.getName());
    if (oldParameter == null) {
        System.err.println("parameter " + parameter.getName() + " was not found in the old symbol table");
    } else if (oldParameter.getUnitDefinition() == null) {
        System.err.println("parameter " + parameter.getName() + " has a null unit in old model, can't convert");
    } else {
        oldExprUnit = oldParameter.getUnitDefinition();
    }
    // get new unit
    VCUnitDefinition newExprUnit = null;
    if (parameter.getUnitDefinition() == null) {
        System.err.println("parameter " + parameter.getName() + " has a null unit in new model, can't convert");
    } else {
        newExprUnit = parameter.getUnitDefinition();
    }
    convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldExprUnit, newExprUnit, parameter.getExpression());
}
Also used : VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Parameter(cbit.vcell.model.Parameter)

Example 39 with Parameter

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

the class ModelUnitConverter method createBioModelWithNewUnitSystem.

public static BioModel createBioModelWithNewUnitSystem(BioModel oldBioModel, ModelUnitSystem newUnitSystem) throws ExpressionException, XmlParseException {
    // new BioModel has new unit system applied to all built-in units ... but expressions still need to be corrected (see below).
    BioModel newBioModel = XmlHelper.cloneBioModelWithNewUnitSystem(oldBioModel, newUnitSystem);
    Model newModel = newBioModel.getModel();
    Model oldModel = oldBioModel.getModel();
    for (Parameter p : newBioModel.getModel().getModelParameters()) {
        convertVarsWithUnitFactors(oldBioModel.getModel(), newBioModel.getModel(), p);
    }
    for (ReactionStep reactionStep : newBioModel.getModel().getReactionSteps()) {
        SymbolTable oldSymbolTable = oldBioModel.getModel().getReactionStep(reactionStep.getName());
        SymbolTable newSymbolTable = reactionStep;
        for (Parameter p : reactionStep.getKinetics().getUnresolvedParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
        for (Parameter p : reactionStep.getKinetics().getKineticsParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
    }
    for (ReactionRule reactionRule : newBioModel.getModel().getRbmModelContainer().getReactionRuleList()) {
        SymbolTable oldSymbolTable = oldBioModel.getModel().getRbmModelContainer().getReactionRule(reactionRule.getName()).getKineticLaw().getScopedSymbolTable();
        SymbolTable newSymbolTable = reactionRule.getKineticLaw().getScopedSymbolTable();
        for (Parameter p : reactionRule.getKineticLaw().getUnresolvedParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
        for (Parameter p : reactionRule.getKineticLaw().getLocalParameters()) {
            convertVarsWithUnitFactors(oldSymbolTable, newSymbolTable, p);
        }
    }
    for (SimulationContext simContext : newBioModel.getSimulationContexts()) {
        SimulationContext oldSimContext = oldBioModel.getSimulationContext(simContext.getName());
        // ArrayList<Parameter> parameterList = new ArrayList<Parameter>();
        for (StructureMapping mapping : simContext.getGeometryContext().getStructureMappings()) {
            Structure oldStructure = oldModel.getStructure(mapping.getStructure().getName());
            StructureMapping oldMapping = oldSimContext.getGeometryContext().getStructureMapping(oldStructure);
            for (Parameter p : mapping.computeApplicableParameterList()) {
                convertVarsWithUnitFactors(oldMapping, mapping, p);
            }
        }
        for (SpeciesContextSpec spec : simContext.getReactionContext().getSpeciesContextSpecs()) {
            SpeciesContext oldSpeciesContext = oldModel.getSpeciesContext(spec.getSpeciesContext().getName());
            SpeciesContextSpec oldSpec = oldSimContext.getReactionContext().getSpeciesContextSpec(oldSpeciesContext);
            for (Parameter p : spec.computeApplicableParameterList()) {
                convertVarsWithUnitFactors(oldSpec, spec, p);
            }
        }
        for (int i = 0; i < simContext.getElectricalStimuli().length; i++) {
            ElectricalStimulus newElectricalStimulus = simContext.getElectricalStimuli()[i];
            ElectricalStimulus oldElectricalStimulus = oldSimContext.getElectricalStimuli()[i];
            for (Parameter p : newElectricalStimulus.getParameters()) {
                convertVarsWithUnitFactors(oldElectricalStimulus.getNameScope().getScopedSymbolTable(), newElectricalStimulus.getNameScope().getScopedSymbolTable(), p);
            }
        }
        // convert events : trigger and delay parameters and event assignments
        for (int i = 0; simContext.getBioEvents() != null && oldSimContext.getBioEvents() != null && i < simContext.getBioEvents().length; i++) {
            BioEvent newBioEvent = simContext.getBioEvents()[i];
            BioEvent oldBioEvent = oldSimContext.getBioEvent(newBioEvent.getName());
            for (Parameter p : newBioEvent.getEventParameters()) {
                convertVarsWithUnitFactors(oldBioEvent.getNameScope().getScopedSymbolTable(), newBioEvent.getNameScope().getScopedSymbolTable(), p);
            }
            // for each event assignment expression
            for (int e = 0; e < newBioEvent.getEventAssignments().size(); e++) {
                ScopedSymbolTable newSymbolTable = newBioEvent.getNameScope().getScopedSymbolTable();
                ScopedSymbolTable oldSymbolTable = oldBioEvent.getNameScope().getScopedSymbolTable();
                EventAssignment newEventAssignment = newBioEvent.getEventAssignments().get(e);
                EventAssignment oldEventAssignment = oldBioEvent.getEventAssignments().get(e);
                VCUnitDefinition oldTargetUnit = oldEventAssignment.getTarget().getUnitDefinition();
                VCUnitDefinition newTargetUnit = newEventAssignment.getTarget().getUnitDefinition();
                Expression eventAssgnExpr = newEventAssignment.getAssignmentExpression();
                convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, eventAssgnExpr);
            }
        }
        /**
         * @TODO: If rate rule variable unit is TBD, we still need to handle the rate expression unit.
         */
        // convert rate rules
        RateRule[] rateRules = simContext.getRateRules();
        if (rateRules != null && rateRules.length > 0) {
            for (RateRule rateRule : rateRules) {
                RateRule oldRateRule = oldSimContext.getRateRule(rateRule.getName());
                ScopedSymbolTable oldSymbolTable = oldRateRule.getSimulationContext();
                ScopedSymbolTable newSymbolTable = rateRule.getSimulationContext();
                VCUnitDefinition oldTargetUnit = oldRateRule.getRateRuleVar().getUnitDefinition();
                VCUnitDefinition newTargetUnit = rateRule.getRateRuleVar().getUnitDefinition();
                Expression rateRuleExpr = rateRule.getRateRuleExpression();
                convertExprWithUnitFactors(oldSymbolTable, newSymbolTable, oldTargetUnit, newTargetUnit, rateRuleExpr);
            }
        }
    }
    // end  for - simulationContext
    return newBioModel;
}
Also used : ReactionRule(cbit.vcell.model.ReactionRule) EventAssignment(cbit.vcell.mapping.BioEvent.EventAssignment) ScopedSymbolTable(cbit.vcell.parser.ScopedSymbolTable) SymbolTable(cbit.vcell.parser.SymbolTable) SpeciesContext(cbit.vcell.model.SpeciesContext) SimulationContext(cbit.vcell.mapping.SimulationContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) StructureMapping(cbit.vcell.mapping.StructureMapping) ElectricalStimulus(cbit.vcell.mapping.ElectricalStimulus) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) ReactionStep(cbit.vcell.model.ReactionStep) Model(cbit.vcell.model.Model) Parameter(cbit.vcell.model.Parameter) RateRule(cbit.vcell.mapping.RateRule) ScopedSymbolTable(cbit.vcell.parser.ScopedSymbolTable) BioEvent(cbit.vcell.mapping.BioEvent) Structure(cbit.vcell.model.Structure)

Example 40 with Parameter

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

the class SimulationContext method copySimulationContext.

public static SimulationContext copySimulationContext(SimulationContext srcSimContext, String newSimulationContextName, boolean bSpatial, Application simContextType) throws java.beans.PropertyVetoException, ExpressionException, MappingException, GeometryException, ImageException {
    Geometry newClonedGeometry = new Geometry(srcSimContext.getGeometry());
    newClonedGeometry.precomputeAll(new GeometryThumbnailImageFactoryAWT());
    // if stoch copy to ode, we need to check is stoch is using particles. If yes, should convert particles to concentraton.
    // the other 3 cases are fine. ode->ode, ode->stoch, stoch-> stoch
    SimulationContext destSimContext = new SimulationContext(srcSimContext, newClonedGeometry, simContextType);
    if (srcSimContext.getApplicationType() == Application.NETWORK_STOCHASTIC && !srcSimContext.isUsingConcentration() && simContextType == Application.NETWORK_DETERMINISTIC) {
        try {
            destSimContext.convertSpeciesIniCondition(true);
        } catch (MappingException e) {
            e.printStackTrace();
            throw new java.beans.PropertyVetoException(e.getMessage(), null);
        }
    }
    if (srcSimContext.getGeometry().getDimension() > 0 && !bSpatial) {
        // copy the size over
        destSimContext.setGeometry(new Geometry("nonspatial", 0));
        StructureMapping[] srcStructureMappings = srcSimContext.getGeometryContext().getStructureMappings();
        StructureMapping[] destStructureMappings = destSimContext.getGeometryContext().getStructureMappings();
        for (StructureMapping destStructureMapping : destStructureMappings) {
            for (StructureMapping srcStructureMapping : srcStructureMappings) {
                if (destStructureMapping.getStructure() == srcStructureMapping.getStructure()) {
                    if (srcStructureMapping.getUnitSizeParameter() != null) {
                        Expression sizeRatio = srcStructureMapping.getUnitSizeParameter().getExpression();
                        GeometryClass srcGeometryClass = srcStructureMapping.getGeometryClass();
                        GeometricRegion[] srcGeometricRegions = srcSimContext.getGeometry().getGeometrySurfaceDescription().getGeometricRegions(srcGeometryClass);
                        if (srcGeometricRegions != null) {
                            double size = 0;
                            for (GeometricRegion srcGeometricRegion : srcGeometricRegions) {
                                size += srcGeometricRegion.getSize();
                            }
                            destStructureMapping.getSizeParameter().setExpression(Expression.mult(sizeRatio, new Expression(size)));
                        }
                    }
                    break;
                }
            }
        }
        // If changing spatial to non-spatial
        // set diffusion to 0, velocity and boundary to null
        // srcSimContext.getReactionContext().getspe
        Parameter[] allParameters = destSimContext.getAllParameters();
        if (allParameters != null && allParameters.length > 0) {
            for (int i = 0; i < allParameters.length; i++) {
                if (allParameters[i] instanceof SpeciesContextSpecParameter) {
                    SpeciesContextSpecParameter speciesContextSpecParameter = (SpeciesContextSpecParameter) allParameters[i];
                    int role = speciesContextSpecParameter.getRole();
                    if (role == SpeciesContextSpec.ROLE_DiffusionRate) {
                        speciesContextSpecParameter.setExpression(new Expression(0));
                    } else if (role == SpeciesContextSpec.ROLE_BoundaryValueXm || role == SpeciesContextSpec.ROLE_BoundaryValueXp || role == SpeciesContextSpec.ROLE_BoundaryValueYm || role == SpeciesContextSpec.ROLE_BoundaryValueYp || role == SpeciesContextSpec.ROLE_BoundaryValueZm || role == SpeciesContextSpec.ROLE_BoundaryValueZp) {
                        speciesContextSpecParameter.setExpression(null);
                    } else if (role == SpeciesContextSpec.ROLE_VelocityX || role == SpeciesContextSpec.ROLE_VelocityY || role == SpeciesContextSpec.ROLE_VelocityZ) {
                        speciesContextSpecParameter.setExpression(null);
                    }
                }
            }
        }
    }
    destSimContext.fixFlags();
    destSimContext.setName(newSimulationContextName);
    return destSimContext;
}
Also used : GeometryClass(cbit.vcell.geometry.GeometryClass) SurfaceGeometricRegion(cbit.vcell.geometry.surface.SurfaceGeometricRegion) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) GeometricRegion(cbit.vcell.geometry.surface.GeometricRegion) Geometry(cbit.vcell.geometry.Geometry) PropertyVetoException(java.beans.PropertyVetoException) GeometryThumbnailImageFactoryAWT(cbit.vcell.geometry.GeometryThumbnailImageFactoryAWT) Expression(cbit.vcell.parser.Expression) Parameter(cbit.vcell.model.Parameter) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter)

Aggregations

Parameter (cbit.vcell.model.Parameter)54 Expression (cbit.vcell.parser.Expression)33 ModelParameter (cbit.vcell.model.Model.ModelParameter)29 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)19 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)19 SpeciesContext (cbit.vcell.model.SpeciesContext)19 ExpressionException (cbit.vcell.parser.ExpressionException)18 Model (cbit.vcell.model.Model)15 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)15 PropertyVetoException (java.beans.PropertyVetoException)15 ReactionStep (cbit.vcell.model.ReactionStep)14 SpeciesContextSpecParameter (cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter)13 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)12 SpeciesContextSpec (cbit.vcell.mapping.SpeciesContextSpec)11 Structure (cbit.vcell.model.Structure)11 ArrayList (java.util.ArrayList)10 StructureMapping (cbit.vcell.mapping.StructureMapping)9 MathDescription (cbit.vcell.math.MathDescription)9 ProxyParameter (cbit.vcell.model.ProxyParameter)9 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)9