Пример #1
0
        public void TestWithParameters()
        {
            // Create a model
            MuCell.Model.SBML.Model model = new MuCell.Model.SBML.Model();
            MuCell.Model.SBML.Species species1 = new MuCell.Model.SBML.Species();
            MuCell.Model.SBML.Species species2 = new MuCell.Model.SBML.Species();
            MuCell.Model.SBML.Parameter parameter = new MuCell.Model.SBML.Parameter();

            parameter.ID = "k_1";
            parameter.Value = 0.07d;

            model.listOfParameters = new List<MuCell.Model.SBML.Parameter>();
            model.listOfParameters.Add(parameter);

            model.listOfSpecies = new List<MuCell.Model.SBML.Species>();
            model.listOfSpecies.Add(species1);
            model.listOfSpecies.Add(species2);

            // Set some values for species1
            species1.ID = "s1";
            species1.InitialAmount = 1.0d;

            // Set some values for species2
            species2.ID = "s2";
            species2.InitialAmount = 12.0d;

            model.AddId("s1", species1);
            model.AddId("s2", species2);
            model.AddId("k_1", parameter);

            // Set up the reaction
            MuCell.Model.SBML.Reaction reaction1 = new MuCell.Model.SBML.Reaction("reaction1");

            // Set up the kinetic law
            reaction1.KineticLaw = new MuCell.Model.SBML.KineticLaw(model);
            reaction1.KineticLaw.Formula = "s1*s2*k_1";

            // set up the species reference for the reactants
            MuCell.Model.SBML.SpeciesReference ref1 = new MuCell.Model.SBML.SpeciesReference(species1, 1);
            // set up the species references for the products
            MuCell.Model.SBML.SpeciesReference ref2 = new MuCell.Model.SBML.SpeciesReference(species2, 3);

            // Add the references
            reaction1.Reactants.Add(ref1);
            reaction1.Products.Add(ref2);

            // set up the cell definition
            MuCell.Model.CellDefinition celldef1 = new MuCell.Model.CellDefinition("cell1");
            celldef1.addSBMLModel(model);

            // set up a cell
            MuCell.Model.CellInstance cell1 = celldef1.createCell();

            cell1.localSpeciesDelta.Add("s1", 0.0d);
            cell1.localSpeciesDelta.Add("s2", 0.0d);

            cell1.localSpeciesVariables.Add("s1", 1.0d);
            cell1.localSpeciesVariables.Add("s2", 12.0d);

            // set up the statesnaphost
            MuCell.Model.StateSnapshot state1 = new MuCell.Model.StateSnapshot();
            state1.Cells.Add(cell1);

            // get the eval function
            MuCell.Model.EffectReactionEvaluationFunction fun1 = reaction1.ToEffectReactionEvaluationFunction();

            // Evaluate
            fun1(state1, cell1);

            cell1.setSpeciesAmount("s1", cell1.UnitTestGetSpeciesAmountsAndDeltas("s1"));
            cell1.setSpeciesAmount("s2", cell1.UnitTestGetSpeciesAmountsAndDeltas("s2"));

            cell1.UnitTestClearDeltas();

            AssertDouble.AreEqual(1.0d-0.84d, cell1.getLocalSimulationSpeciesAmount("s1"));
            AssertDouble.AreEqual(12.0d+0.84d*3.0d, cell1.getLocalSimulationSpeciesAmount("s2"));

            System.Console.WriteLine("about to eval");

            // Evaluate again
            fun1(state1, cell1);

            cell1.setSpeciesAmount("s1", cell1.UnitTestGetSpeciesAmountsAndDeltas("s1"));
            cell1.setSpeciesAmount("s2", cell1.UnitTestGetSpeciesAmountsAndDeltas("s2"));

            cell1.UnitTestClearDeltas();

            AssertDouble.AreEqual(0.16d-0.162624d, cell1.getLocalSimulationSpeciesAmount("s1"));
            AssertDouble.AreEqual(14.52d+0.162624d*3.0d, cell1.getLocalSimulationSpeciesAmount("s2"));

            // Check the param is the same
            Assert.AreEqual(0.07m, parameter.Value);
        }
Пример #2
0
        public void TestWithSpecies()
        {
            // Create a model
            MuCell.Model.SBML.Model model = new MuCell.Model.SBML.Model();
            MuCell.Model.SBML.Species species1 = new MuCell.Model.SBML.Species();
            MuCell.Model.SBML.Species species2 = new MuCell.Model.SBML.Species();

            model.listOfSpecies = new List<MuCell.Model.SBML.Species>();
            model.listOfSpecies.Add(species1);
            model.listOfSpecies.Add(species2);

            // Set some values for species1
            species1.ID = "s1";
            species1.InitialAmount = 4.0d;

            // Set some values for species2
            species2.ID = "s2";
            species2.InitialAmount = 0.1d;

            model.AddId("s1", species1);
            model.AddId("s2", species2);

            // Set up the reaction
            MuCell.Model.SBML.Reaction reaction1 = new MuCell.Model.SBML.Reaction("reaction1");

            model.listOfReactions = new List<MuCell.Model.SBML.Reaction>();
            model.listOfReactions.Add(reaction1);

            // Set up the kinetic law
            reaction1.KineticLaw = new MuCell.Model.SBML.KineticLaw(model);
            reaction1.KineticLaw.Formula = "(s1)*2";

            // set up the species reference for the reactants
            MuCell.Model.SBML.SpeciesReference ref1 = new MuCell.Model.SBML.SpeciesReference(species1, 1);
            // set up the species references for the products
            MuCell.Model.SBML.SpeciesReference ref2 = new MuCell.Model.SBML.SpeciesReference(species1, 0.75);
            MuCell.Model.SBML.SpeciesReference ref3 = new MuCell.Model.SBML.SpeciesReference(species2, 2);

            // Add the references
            reaction1.Reactants.Add(ref1);
            reaction1.Products.Add(ref2);
            reaction1.Products.Add(ref3);

            // set up the cell definition
            MuCell.Model.CellDefinition celldef1 = new MuCell.Model.CellDefinition("cell1");
            celldef1.addSBMLModel(model);

            // set up a cell
            MuCell.Model.CellInstance cell1 = celldef1.createCell();

            cell1.localSpeciesDelta.Add("s1", 0.0d);
            cell1.localSpeciesDelta.Add("s2", 0.0d);

            cell1.localSpeciesVariables.Add("s1", 4.0d);
            cell1.localSpeciesVariables.Add("s2", 0.1d);

            // set up the statesnaphost
            MuCell.Model.StateSnapshot state1 = new MuCell.Model.StateSnapshot();
            state1.Cells.Add(cell1);

            // get the eval function
            MuCell.Model.EffectReactionEvaluationFunction fun1 = reaction1.ToEffectReactionEvaluationFunction();

            // Evaluate
            fun1(state1, cell1);

            cell1.setSpeciesAmount("s1", cell1.UnitTestGetSpeciesAmountsAndDeltas("s1"));
            cell1.setSpeciesAmount("s2", cell1.UnitTestGetSpeciesAmountsAndDeltas("s2"));

            cell1.UnitTestClearDeltas();

            Assert.AreEqual(2.0d, cell1.getLocalSimulationSpeciesAmount("s1"));
            Assert.AreEqual(16.1d, cell1.getLocalSimulationSpeciesAmount("s2"));

            // Evaluate again
            fun1(state1, cell1);

            cell1.setSpeciesAmount("s1", cell1.UnitTestGetSpeciesAmountsAndDeltas("s1"));
            cell1.setSpeciesAmount("s2", cell1.UnitTestGetSpeciesAmountsAndDeltas("s2"));

            cell1.UnitTestClearDeltas();

            Assert.AreEqual(1.0d, cell1.getLocalSimulationSpeciesAmount("s1"));
            Assert.AreEqual(24.1d, cell1.getLocalSimulationSpeciesAmount("s2"));
        }
Пример #3
0
        public void TestGenerateTimeSeries()
        {
            // simulation
            MuCell.Model.Simulation simulation = new MuCell.Model.Simulation("simulation1");

            // experiment
            MuCell.Model.Experiment experiment = new MuCell.Model.Experiment("experiment1");

            // Hopf model
            MuCell.Model.SBML.Reader.SBMLReader s = new MuCell.Model.SBML.Reader.SBMLReader("../../UnitTests/smallest.Hopf.level2.xml");
            List<MuCell.Model.SBML.Model> models = new List<MuCell.Model.SBML.Model>();
            models.Add(s.model);

            // Cell definition
            MuCell.Model.CellDefinition celldef1 = new MuCell.Model.CellDefinition("celldef1");
            celldef1.addSBMLModel(s.model);

            experiment.addCellDefinition(celldef1);

            // Cells
            List<MuCell.Model.CellInstance> cells = new List<MuCell.Model.CellInstance>();

            for(int i=0;i<5;i++)
            {
                cells.Add(celldef1.createCell());
            }

            // StateSnapshot for intial state
            MuCell.Model.StateSnapshot initialState = new MuCell.Model.StateSnapshot(cells);
            MuCell.Model.Vector3 size = new MuCell.Model.Vector3(1, 1, 1);
            initialState.SimulationEnvironment = new MuCell.Model.Environment(size);

            // trime series
            MuCell.Model.TimeSeries ts1 = new MuCell.Model.TimeSeries("Average X", "X/celldef1", 0.01001d);
            ts1.Initialize(models, experiment, simulation);

            MuCell.Model.TimeSeries ts2 = new MuCell.Model.TimeSeries("Total X", "X", 0.01001d);
            ts2.Initialize(models, experiment, simulation);

            // Parameters
            MuCell.Model.SimulationParameters parameters = new MuCell.Model.SimulationParameters();

            parameters.TimeSeries.Add(ts1);
            parameters.TimeSeries.Add(ts2);

            parameters.InitialState = initialState;
            parameters.SimulationLength = 0.04004d;
            parameters.SnapshotInterval = 1;
            parameters.StepTime = 0.01001d;

            parameters.RelativeTolerance = 1E-8;
            parameters.SolverMethod = MuCell.Model.Solver.SolverMethods.RungeKutta;

            // Simulation
            simulation.Parameters = parameters;

            // Start simulation
            simulation.StartSimulation();

            // Now check the results

            Assert.AreEqual(5, ts1.Series.Count);
            Assert.AreEqual(5, ts2.Series.Count);

            double[] ts = ts1.Series.ToArray();
            double[] tst = ts2.Series.ToArray();

            Assert.AreEqual(2.5d, ts[0]);
            AssertDouble.AreEqual(rk_x_t1, ts[1]);
            AssertDouble.AreEqual(rk_x_t2, ts[2]);
            AssertDouble.AreEqual(rk_x_t3, ts[3]);
            AssertDouble.AreEqual(rk_x_t4, ts[4]);

            Assert.AreEqual(5*2.5d, tst[0]);
            AssertDouble.AreEqual(5*rk_x_t1, tst[1]);
            AssertDouble.AreEqual(5*rk_x_t2, tst[2]);
            AssertDouble.AreEqual(5*rk_x_t3, tst[3]);
            AssertDouble.AreEqual(5*rk_x_t4, tst[4]);
        }
Пример #4
0
        public void InvalidTimeSeriesEquation()
        {
            // simulation
            MuCell.Model.Simulation simulation = new MuCell.Model.Simulation("simulation1");

            // experiment
            MuCell.Model.Experiment experiment = new MuCell.Model.Experiment("experiment1");

            // Hopf model
            MuCell.Model.SBML.Reader.SBMLReader s = new MuCell.Model.SBML.Reader.SBMLReader("../../UnitTests/smallest.Hopf.level2.xml");
            List<MuCell.Model.SBML.Model> models = new List<MuCell.Model.SBML.Model>();
            models.Add(s.model);

            // Cell definition
            MuCell.Model.CellDefinition celldef1 = new MuCell.Model.CellDefinition("celldef1");
            celldef1.addSBMLModel(s.model);

            experiment.addCellDefinition(celldef1);

            // Cells
            List<MuCell.Model.CellInstance> cells = new List<MuCell.Model.CellInstance>();

            for(int i=0;i<5;i++)
            {
                cells.Add(celldef1.createCell());
            }

            // StateSnapshot for intial state
            MuCell.Model.StateSnapshot initialState = new MuCell.Model.StateSnapshot(cells);
            MuCell.Model.Vector3 size = new MuCell.Model.Vector3(1, 1, 1);
            initialState.SimulationEnvironment = new MuCell.Model.Environment(size);

            // Parameters
            simulation.Parameters.InitialState = initialState;
            simulation.Parameters.SimulationLength = 3;
            simulation.Parameters.SnapshotInterval = 1;
            simulation.Parameters.StepTime = 1;

            // Time series
            MuCell.Model.TimeSeries ts1 = new MuCell.Model.TimeSeries("Average A", "(A)", 1.0);
            ts1.Initialize(models, experiment, simulation);
        }
Пример #5
0
        public void RunHopfSimulationRK(MuCell.Model.SBML.Reader.SBMLReader s)
        {
            // Cell definition
            MuCell.Model.CellDefinition celldef1 = new MuCell.Model.CellDefinition("celldef1");
            celldef1.addSBMLModel(s.model);

            // Cells
            List<MuCell.Model.CellInstance> cells = new List<MuCell.Model.CellInstance>();
            MuCell.Model.CellInstance cell1 = celldef1.createCell();
            cells.Add(cell1);

            // StateSnapshot for intial state
            MuCell.Model.StateSnapshot initialState = new MuCell.Model.StateSnapshot(cells);
            MuCell.Model.Vector3 size = new MuCell.Model.Vector3(1, 1, 1);
            initialState.SimulationEnvironment = new MuCell.Model.Environment(size);

            // Parameters
            MuCell.Model.SimulationParameters parameters = new MuCell.Model.SimulationParameters();
            parameters.InitialState = initialState;
            parameters.SimulationLength = 0.01001d;
            parameters.SnapshotInterval = 2;
            parameters.StepTime = 0.01001d;

            parameters.SolverMethod = MuCell.Model.Solver.SolverMethods.RungeKutta;

            // Simulation
            MuCell.Model.Simulation simulation = new MuCell.Model.Simulation("simulation1");
            simulation.Parameters = parameters;

            // Assert that is not complete at all
            Assert.AreEqual(0.0d, simulation.SimulationProgress());

            // 	Test the initial conditions
            Assert.AreEqual(1.0d, cell1.getSpeciesAmount("A"));
            Assert.AreEqual(2.5d, cell1.getSpeciesAmount("X"));
            Assert.AreEqual(2.5d, cell1.getSpeciesAmount("Y"));
            Assert.AreEqual(2.5d, cell1.getSpeciesAmount("Z"));
            Assert.AreEqual(0.0d, cell1.getSpeciesAmount("_void_"));

            // Start simulation
            simulation.StartSimulation();

            // Assert that is complete
            Assert.AreEqual(1.0d, simulation.SimulationProgress());

            // 	Test the results (test generated from the Python model)
            AssertDouble.AreEqual(1.0d, cell1.getSpeciesAmount("A"));
            AssertDouble.AreEqual(rk_x_t1, cell1.getSpeciesAmount("X"));
            AssertDouble.AreEqual(rk_y_t1, cell1.getSpeciesAmount("Y"));
            AssertDouble.AreEqual(rk_z_t1, cell1.getSpeciesAmount("Z"));
            Assert.AreEqual(0.0d, cell1.getSpeciesAmount("_void_"));

            // Make the simulation run a bit longer
            parameters.SimulationLength = 0.02002;
            simulation.UnPauseSimulation();

            AssertDouble.AreEqual(1.0d, cell1.getSpeciesAmount("A"));
            AssertDouble.AreEqual(rk_x_t2, cell1.getSpeciesAmount("X"));
            AssertDouble.AreEqual(rk_y_t2, cell1.getSpeciesAmount("Y"));
            AssertDouble.AreEqual(rk_z_t2, cell1.getSpeciesAmount("Z"));
            Assert.AreEqual(0.0d, cell1.getSpeciesAmount("_void_"));

            // Make the simulation run a bit longer
            parameters.SimulationLength = 0.03003;
            simulation.UnPauseSimulation();

            AssertDouble.AreEqual(1.0d, cell1.getSpeciesAmount("A"));
            AssertDouble.AreEqual(rk_x_t3, cell1.getSpeciesAmount("X"));
            AssertDouble.AreEqual(rk_y_t3, cell1.getSpeciesAmount("Y"));
            AssertDouble.AreEqual(rk_z_t3, cell1.getSpeciesAmount("Z"));
            Assert.AreEqual(0.0d, cell1.getSpeciesAmount("_void_"));
        }