Search in sources :

Example 41 with MathDescription

use of cbit.vcell.math.MathDescription in project vcell by virtualcell.

the class Generate2DExpModelOpAbstract method generateModel.

public final GeneratedModelResults generateModel(double deltaX, double bleachRadius, double cellRadius, double bleachDuration, double bleachRate, double postbleachDelay, double postbleachDuration, double psfSigma, double outputTimeStep, double primaryDiffusionRate, double primaryFraction, double bleachMonitorRate, double secondaryDiffusionRate, double secondaryFraction, String extracellularName, String cytosolName, Context context) throws PropertyVetoException, ExpressionException, GeometryException, ImageException, ModelException, MappingException, MathException, MatrixException {
    double domainSize = 2.2 * cellRadius;
    Extent extent = new Extent(domainSize, domainSize, 1.0);
    Origin origin = new Origin(-extent.getX() / 2.0, -extent.getY() / 2.0, -extent.getZ() / 2.0);
    String EXTRACELLULAR_NAME = extracellularName;
    String CYTOSOL_NAME = cytosolName;
    AnalyticSubVolume cytosolSubVolume = new AnalyticSubVolume(CYTOSOL_NAME, new Expression("pow(x,2)+pow(y,2)<pow(" + cellRadius + ",2)"));
    AnalyticSubVolume extracellularSubVolume = new AnalyticSubVolume(EXTRACELLULAR_NAME, new Expression(1.0));
    Geometry geometry = new Geometry("geometry", 2);
    geometry.getGeometrySpec().setExtent(extent);
    geometry.getGeometrySpec().setOrigin(origin);
    geometry.getGeometrySpec().addSubVolume(extracellularSubVolume);
    geometry.getGeometrySpec().addSubVolume(cytosolSubVolume, true);
    geometry.getGeometrySurfaceDescription().updateAll();
    BioModel bioModel = new BioModel(null);
    bioModel.setName("unnamed");
    Model model = new Model("model");
    bioModel.setModel(model);
    model.addFeature(EXTRACELLULAR_NAME);
    Feature extracellular = (Feature) model.getStructure(EXTRACELLULAR_NAME);
    model.addFeature(CYTOSOL_NAME);
    Feature cytosol = (Feature) model.getStructure(CYTOSOL_NAME);
    SpeciesContext immobileSC = model.createSpeciesContext(cytosol);
    SpeciesContext primarySC = model.createSpeciesContext(cytosol);
    SpeciesContext secondarySC = model.createSpeciesContext(cytosol);
    // 
    // common bleaching rate for all species
    // 
    double bleachStart = 10 * outputTimeStep - bleachDuration - postbleachDelay;
    double bleachEnd = bleachStart + bleachDuration;
    Expression bleachRateExp = createBleachExpression(bleachRadius, bleachRate, bleachMonitorRate, bleachStart, bleachEnd);
    {
        SimpleReaction immobileBWM = model.createSimpleReaction(cytosol);
        GeneralKinetics immobileBWMKinetics = new GeneralKinetics(immobileBWM);
        immobileBWM.setKinetics(immobileBWMKinetics);
        immobileBWM.addReactant(immobileSC, 1);
        immobileBWMKinetics.getReactionRateParameter().setExpression(Expression.mult(bleachRateExp, new Expression(immobileSC.getName())));
    }
    {
        SimpleReaction primaryBWM = model.createSimpleReaction(cytosol);
        GeneralKinetics primaryBWMKinetics = new GeneralKinetics(primaryBWM);
        primaryBWM.setKinetics(primaryBWMKinetics);
        primaryBWM.addReactant(primarySC, 1);
        primaryBWMKinetics.getReactionRateParameter().setExpression(Expression.mult(bleachRateExp, new Expression(primarySC.getName())));
    }
    {
        SimpleReaction secondaryBWM = model.createSimpleReaction(cytosol);
        GeneralKinetics secondaryBWMKinetics = new GeneralKinetics(secondaryBWM);
        secondaryBWM.setKinetics(secondaryBWMKinetics);
        secondaryBWM.addReactant(secondarySC, 1);
        secondaryBWMKinetics.getReactionRateParameter().setExpression(Expression.mult(bleachRateExp, new Expression(secondarySC.getName())));
    }
    // create simulation context
    SimulationContext simContext = bioModel.addNewSimulationContext("simContext", SimulationContext.Application.NETWORK_DETERMINISTIC);
    simContext.setGeometry(geometry);
    FeatureMapping cytosolFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(cytosol);
    FeatureMapping extracellularFeatureMapping = (FeatureMapping) simContext.getGeometryContext().getStructureMapping(extracellular);
    SubVolume cytSubVolume = geometry.getGeometrySpec().getSubVolume(CYTOSOL_NAME);
    SubVolume exSubVolume = geometry.getGeometrySpec().getSubVolume(EXTRACELLULAR_NAME);
    // unused? SurfaceClass pmSurfaceClass = geometry.getGeometrySurfaceDescription().getSurfaceClass(exSubVolume, cytSubVolume);
    cytosolFeatureMapping.setGeometryClass(cytSubVolume);
    extracellularFeatureMapping.setGeometryClass(exSubVolume);
    cytosolFeatureMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
    extracellularFeatureMapping.getUnitSizeParameter().setExpression(new Expression(1.0));
    double fixedFraction = 1.0 - primaryFraction - secondaryFraction;
    SpeciesContextSpec immobileSCS = simContext.getReactionContext().getSpeciesContextSpec(immobileSC);
    immobileSCS.getInitialConditionParameter().setExpression(new Expression(fixedFraction));
    immobileSCS.getDiffusionParameter().setExpression(new Expression(0.0));
    SpeciesContextSpec primarySCS = simContext.getReactionContext().getSpeciesContextSpec(primarySC);
    primarySCS.getInitialConditionParameter().setExpression(new Expression(primaryFraction));
    primarySCS.getDiffusionParameter().setExpression(new Expression(primaryDiffusionRate));
    SpeciesContextSpec secondarySCS = simContext.getReactionContext().getSpeciesContextSpec(secondarySC);
    secondarySCS.getInitialConditionParameter().setExpression(new Expression(secondaryFraction));
    secondarySCS.getDiffusionParameter().setExpression(new Expression(secondaryDiffusionRate));
    simContext.getMicroscopeMeasurement().addFluorescentSpecies(immobileSC);
    simContext.getMicroscopeMeasurement().addFluorescentSpecies(primarySC);
    simContext.getMicroscopeMeasurement().addFluorescentSpecies(secondarySC);
    simContext.getMicroscopeMeasurement().setConvolutionKernel(new GaussianConvolutionKernel(new Expression(psfSigma), new Expression(psfSigma)));
    MathMapping mathMapping = simContext.createNewMathMapping();
    MathDescription mathDesc = mathMapping.getMathDescription();
    simContext.setMathDescription(mathDesc);
    User owner = context.getDefaultOwner();
    int meshSize = (int) (domainSize / deltaX);
    if (meshSize % 2 == 0) {
        // want an odd-sized mesh in x and y ... so centered at the origin.
        meshSize = meshSize + 1;
    }
    TimeBounds timeBounds = new TimeBounds(0.0, postbleachDuration);
    // 
    // simulation to use for data generation (output time steps as recorded by the microscope)
    // 
    double bleachBlackoutBegin = bleachStart - postbleachDelay;
    double bleachBlackoutEnd = bleachEnd + postbleachDelay;
    // ArrayList<Double> times = new ArrayList<Double>();
    // double time = 0;
    // while (time<=timeBounds.getEndingTime()){
    // if (time<=bleachBlackoutBegin || time>bleachBlackoutEnd){
    // // postbleachDelay is the time it takes to switch the filters.
    // times.add(time);
    // }
    // time += outputTimeStep.getData();
    // }
    // double[] timeArray = new double[times.size()];
    // for (int i=0;i<timeArray.length;i++){
    // timeArray[i] = times.get(i);
    // }
    // OutputTimeSpec fakeDataSimOutputTimeSpec = new ExplicitOutputTimeSpec(timeArray);
    OutputTimeSpec fakeDataSimOutputTimeSpec = new UniformOutputTimeSpec(outputTimeStep);
    KeyValue fakeDataSimKey = context.createNewKeyValue();
    SimulationVersion fakeDataSimVersion = new SimulationVersion(fakeDataSimKey, "fakeDataSim", owner, new GroupAccessNone(), new KeyValue("0"), new BigDecimal(0), new Date(), VersionFlag.Current, "", null);
    Simulation fakeDataSim = new Simulation(fakeDataSimVersion, mathDesc);
    simContext.addSimulation(fakeDataSim);
    fakeDataSim.getSolverTaskDescription().setTimeBounds(timeBounds);
    fakeDataSim.getMeshSpecification().setSamplingSize(new ISize(meshSize, meshSize, 1));
    fakeDataSim.getSolverTaskDescription().setSolverDescription(SolverDescription.SundialsPDE);
    fakeDataSim.getSolverTaskDescription().setOutputTimeSpec(fakeDataSimOutputTimeSpec);
    // 
    // simulation to use for viewing the protocol (output time steps to understand the physics)
    // 
    KeyValue fullExperimentSimKey = context.createNewKeyValue();
    SimulationVersion fullExperimentSimVersion = new SimulationVersion(fullExperimentSimKey, "fullExperiment", owner, new GroupAccessNone(), new KeyValue("0"), new BigDecimal(0), new Date(), VersionFlag.Current, "", null);
    Simulation fullExperimentSim = new Simulation(fullExperimentSimVersion, mathDesc);
    simContext.addSimulation(fullExperimentSim);
    OutputTimeSpec fullExperimentOutputTimeSpec = new UniformOutputTimeSpec(outputTimeStep / 10.0);
    fullExperimentSim.getSolverTaskDescription().setTimeBounds(timeBounds);
    fullExperimentSim.getMeshSpecification().setSamplingSize(new ISize(meshSize, meshSize, 1));
    fullExperimentSim.getSolverTaskDescription().setSolverDescription(SolverDescription.SundialsPDE);
    fullExperimentSim.getSolverTaskDescription().setOutputTimeSpec(fullExperimentOutputTimeSpec);
    GeneratedModelResults results = new GeneratedModelResults();
    results.bioModel_2D = bioModel;
    results.simulation_2D = fakeDataSim;
    results.bleachBlackoutBeginTime = bleachBlackoutBegin;
    results.bleachBlackoutEndTime = bleachBlackoutEnd;
    return results;
}
Also used : Origin(org.vcell.util.Origin) User(org.vcell.util.document.User) KeyValue(org.vcell.util.document.KeyValue) Extent(org.vcell.util.Extent) MathDescription(cbit.vcell.math.MathDescription) ISize(org.vcell.util.ISize) SpeciesContext(cbit.vcell.model.SpeciesContext) GeneralKinetics(cbit.vcell.model.GeneralKinetics) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) Feature(cbit.vcell.model.Feature) TimeBounds(cbit.vcell.solver.TimeBounds) GroupAccessNone(org.vcell.util.document.GroupAccessNone) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) OutputTimeSpec(cbit.vcell.solver.OutputTimeSpec) SimulationVersion(org.vcell.util.document.SimulationVersion) FeatureMapping(cbit.vcell.mapping.FeatureMapping) SubVolume(cbit.vcell.geometry.SubVolume) AnalyticSubVolume(cbit.vcell.geometry.AnalyticSubVolume) SimpleReaction(cbit.vcell.model.SimpleReaction) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) SimulationContext(cbit.vcell.mapping.SimulationContext) GaussianConvolutionKernel(cbit.vcell.mapping.MicroscopeMeasurement.GaussianConvolutionKernel) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Geometry(cbit.vcell.geometry.Geometry) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) BioModel(cbit.vcell.biomodel.BioModel) BioModel(cbit.vcell.biomodel.BioModel) Model(cbit.vcell.model.Model) MathMapping(cbit.vcell.mapping.MathMapping) AnalyticSubVolume(cbit.vcell.geometry.AnalyticSubVolume)

Example 42 with MathDescription

use of cbit.vcell.math.MathDescription in project vcell by virtualcell.

the class OutputFunctionsPanel method getPossibleGeometryClassesAndVariableTypes.

private ArrayList<Object> getPossibleGeometryClassesAndVariableTypes(Expression expr) throws ExpressionException, InconsistentDomainException {
    SimulationOwner simulationOwner = getSimulationWorkspace().getSimulationOwner();
    MathDescription mathDescription = simulationOwner.getMathDescription();
    boolean bSpatial = simulationOwner.getGeometry().getDimension() > 0;
    if (!bSpatial) {
        return null;
    }
    // making sure that output function is not direct function of constant.
    expr.bindExpression(outputFunctionContext);
    // here use math description as symbol table because we allow
    // new expression itself to be function of constant.
    expr = MathUtilities.substituteFunctions(expr, outputFunctionContext).flatten();
    String[] symbols = expr.getSymbols();
    // using bit operation to determine whether geometry classes for symbols in expression are vol, membrane or both. 01 => vol; 10 => membrane; 11 => both
    int gatherFlag = 0;
    Set<GeometryClass> geomClassSet = new HashSet<GeometryClass>();
    ArrayList<Object> objectsList = new ArrayList<Object>();
    boolean bHasVariable = false;
    VariableType[] varTypes = null;
    if (symbols != null && symbols.length > 0) {
        // making sure that new expression is defined in the same domain
        varTypes = new VariableType[symbols.length];
        for (int i = 0; i < symbols.length; i++) {
            if (ReservedMathSymbolEntries.getReservedVariableEntry(symbols[i]) != null) {
                varTypes[i] = VariableType.VOLUME;
            } else {
                Variable var = mathDescription.getVariable(symbols[i]);
                if (var == null) {
                    var = mathDescription.getPostProcessingBlock().getDataGenerator(symbols[i]);
                }
                varTypes[i] = VariableType.getVariableType(var);
                bHasVariable = true;
                if (var.getDomain() != null) {
                    GeometryClass varGeoClass = simulationOwner.getGeometry().getGeometryClass(var.getDomain().getName());
                    geomClassSet.add(varGeoClass);
                    if (varGeoClass instanceof SubVolume) {
                        gatherFlag |= 1;
                    } else if (varGeoClass instanceof SurfaceClass) {
                        gatherFlag |= 2;
                    }
                }
                if (varTypes[i].equals(VariableType.POSTPROCESSING)) {
                    gatherFlag |= 4;
                }
            }
        }
    }
    if (gatherFlag > 4) {
        throw new RuntimeException("cannot mix post processing variables with membrane or volume variables");
    }
    int numGeomClasses = geomClassSet.size();
    if (numGeomClasses == 0) {
        if (bHasVariable) {
            // if there are no variables (like built in function, vcRegionArea), check with flattened expression to find out the variable type of the new expression
            Function flattenedFunction = new Function(getFunctionNameTextField().getText(), expr, null);
            flattenedFunction.bind(outputFunctionContext);
            VariableType newVarType = SimulationSymbolTable.getFunctionVariableType(flattenedFunction, simulationOwner.getMathDescription(), symbols, varTypes, bSpatial);
            objectsList.add(newVarType);
        } else {
            objectsList.add(VariableType.VOLUME);
            objectsList.add(VariableType.MEMBRANE);
        }
    } else if (numGeomClasses == 1) {
        objectsList.add(geomClassSet.iterator().next());
        if (gatherFlag == 1) {
            objectsList.add(VariableType.MEMBRANE);
        }
    } else if (gatherFlag == 1) {
        // all volumes
        if (numGeomClasses == 2) {
            // all subvolumes, if there are only 2, check for adjacency.
            GeometryClass[] geomClassesArray = geomClassSet.toArray(new GeometryClass[0]);
            SurfaceClass sc = simulationOwner.getGeometry().getGeometrySurfaceDescription().getSurfaceClass((SubVolume) geomClassesArray[0], (SubVolume) geomClassesArray[1]);
            if (sc != null) {
                objectsList.add(sc);
            }
        }
        objectsList.add(VariableType.VOLUME);
    } else if (gatherFlag == 2) {
        // all membranes
        objectsList.add(VariableType.MEMBRANE);
    } else if (gatherFlag == 3) {
        // mixed - both vols and membranes
        // add only membranes?
        objectsList.add(VariableType.MEMBRANE);
    }
    return objectsList;
}
Also used : GeometryClass(cbit.vcell.geometry.GeometryClass) Variable(cbit.vcell.math.Variable) VariableType(cbit.vcell.math.VariableType) MathDescription(cbit.vcell.math.MathDescription) SurfaceClass(cbit.vcell.geometry.SurfaceClass) ArrayList(java.util.ArrayList) SimulationOwner(cbit.vcell.solver.SimulationOwner) Function(cbit.vcell.math.Function) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) SubVolume(cbit.vcell.geometry.SubVolume) HashSet(java.util.HashSet)

Example 43 with MathDescription

use of cbit.vcell.math.MathDescription in project vcell by virtualcell.

the class MathDebuggerPanel method getMathModel2.

public MathModel getMathModel2() throws PropertyVetoException {
    MathDescription mathDesc = mathDescEditor2.getMathDescription();
    if (mathDesc == null) {
        return null;
    } else {
        MathModel mathModel = new MathModel(null);
        mathModel.setName("MATH 2");
        mathModel.setMathDescription(mathDesc);
        return mathModel;
    }
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) MathDescription(cbit.vcell.math.MathDescription)

Example 44 with MathDescription

use of cbit.vcell.math.MathDescription in project vcell by virtualcell.

the class MathDebuggerPanel method main.

public static void main(java.lang.String[] args) {
    try {
        javax.swing.JFrame frame = new javax.swing.JFrame();
        MathDebuggerPanel aMathDebuggerPanel;
        aMathDebuggerPanel = new MathDebuggerPanel();
        frame.setContentPane(aMathDebuggerPanel);
        frame.setTitle("Math Descriptions Comparator");
        frame.setSize(aMathDebuggerPanel.getSize());
        frame.addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(java.awt.event.WindowEvent e) {
                System.exit(0);
            }
        });
        MathModel mathModel1 = new MathModel(null);
        mathModel1.setName("math1");
        Geometry geometry1 = new Geometry("geo", 0);
        MathDescription mathDesc1 = mathModel1.getMathDescription();
        mathDesc1.setGeometry(geometry1);
        mathDesc1.addSubDomain(new CompartmentSubDomain("Compartment", CompartmentSubDomain.NON_SPATIAL_PRIORITY));
        MathModel mathModel2 = new MathModel(null);
        mathModel2.setName("math2");
        Geometry geometry2 = new Geometry("geo", 0);
        MathDescription mathDesc2 = mathModel2.getMathDescription();
        mathDesc2.setGeometry(geometry2);
        mathDesc2.addSubDomain(new CompartmentSubDomain("Compartment", CompartmentSubDomain.NON_SPATIAL_PRIORITY));
        aMathDebuggerPanel.setMathModel1(mathModel1);
        aMathDebuggerPanel.setMathModel2(mathModel2);
        frame.setSize(1500, 800);
        frame.setVisible(true);
    } catch (Throwable exception) {
        System.err.println("Exception occurred in main() of javax.swing.JPanel");
        exception.printStackTrace(System.out);
    }
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) MathDescription(cbit.vcell.math.MathDescription) Geometry(cbit.vcell.geometry.Geometry) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain)

Example 45 with MathDescription

use of cbit.vcell.math.MathDescription in project vcell by virtualcell.

the class MathDebuggerPanel method paste.

private void paste() throws PropertyVetoException, MathException, XmlParseException {
    String copiedText = (String) VCellTransferable.getFromClipboard(DataFlavor.stringFlavor);
    if (!copiedText.contains("BEGINMATH:") && !copiedText.contains("ENDMATH")) {
        throw new RuntimeException("Make sure each math description is enclosed with 'BEGINMATH:' and 'ENDMATH' tags for the 'Paste' operation to work.");
    }
    String[] mathStrings = copiedText.split("BEGINMATH:");
    String mathString1 = null;
    String mathString2 = null;
    for (int i = 0; i < mathStrings.length; i++) {
        mathStrings[i] = mathStrings[i].trim();
        if (mathStrings[i].trim().startsWith("MathDescription {")) {
            int endIndex = mathStrings[i].indexOf("ENDMATH");
            if (endIndex > 0) {
                if (mathString1 == null) {
                    mathString1 = mathStrings[i].substring(0, endIndex);
                } else if (mathString2 == null) {
                    mathString2 = mathStrings[i].substring(0, endIndex);
                } else {
                    throw new RuntimeException("too many math Descriptions (expecting only two)");
                }
            }
        }
    }
    if (mathString1 != null) {
        MathDescription mathDesc = MathDescription.fromEditor(getMathDescEditor1().getMathDescription(), mathString1);
        MathModel mathModel = new MathModel(null);
        mathModel.setName("Math1");
        mathModel.setMathDescription(mathDesc);
        setMathModel1(mathModel);
    }
    if (mathString2 != null) {
        MathDescription mathDesc = MathDescription.fromEditor(getMathDescEditor2().getMathDescription(), mathString2);
        MathModel mathModel = new MathModel(null);
        mathModel.setName("Math2");
        mathModel.setMathDescription(mathDesc);
        setMathModel2(mathModel);
    }
    compareTree();
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) MathDescription(cbit.vcell.math.MathDescription)

Aggregations

MathDescription (cbit.vcell.math.MathDescription)120 Simulation (cbit.vcell.solver.Simulation)48 Geometry (cbit.vcell.geometry.Geometry)32 SimulationContext (cbit.vcell.mapping.SimulationContext)32 Variable (cbit.vcell.math.Variable)32 Expression (cbit.vcell.parser.Expression)30 ExpressionException (cbit.vcell.parser.ExpressionException)27 PropertyVetoException (java.beans.PropertyVetoException)25 BioModel (cbit.vcell.biomodel.BioModel)24 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)22 Constant (cbit.vcell.math.Constant)22 MathException (cbit.vcell.math.MathException)21 MathModel (cbit.vcell.mathmodel.MathModel)21 KeyValue (org.vcell.util.document.KeyValue)20 SubDomain (cbit.vcell.math.SubDomain)19 ArrayList (java.util.ArrayList)18 SubVolume (cbit.vcell.geometry.SubVolume)17 Model (cbit.vcell.model.Model)17 DataAccessException (org.vcell.util.DataAccessException)17 Function (cbit.vcell.math.Function)15