コード例 #1
0
ファイル: TestEnvironment.cs プロジェクト: dorchard/mucell
        public void TestGroupOperators()
        {
            //create an environemnt, and an array of 100 cells
            MuCell.Model.Environment env = new MuCell.Model.Environment(new Vector3(1, 1, 1));
            CellInstance[] cells = new CellInstance[100];
            CellDefinition celDef = new CellDefinition("testCellDef");
            for (int i = 0; i < 100; i++)
            {
                CellInstance cell = new CellInstance(celDef);
                cells[i] = cell;
            }

            //assert that there are no groups yet formed in the env
            Assert.That( env.GetGroups().Count == 0 );

            //obtain an unused group index, and create a group with 25 cells in it
            int newGroupIndex1 = env.GetUnusedGroupIndex();
            for (int i = 0; i < 25; i++)
            {
                env.AddCellToGroup(newGroupIndex1, cells[i]);
            }

            //assert that there is now one group
            Assert.That(env.GetGroups().Count == 1);

            //assert that it is the correct group
            Assert.That(env.ContainsGroup(newGroupIndex1));

            //assert that there are 25 cells in the group
            Assert.That(env.CellsFromGroup(newGroupIndex1).Count == 25);

            //obtain another unused group index
            int newGroupIndex2 = env.GetUnusedGroupIndex();

            //group indexes should differ
            Assert.That(newGroupIndex1 != newGroupIndex2);

            //add some cells to the new group index
            for (int i = 25; i < 50; i++)
            {
                env.AddCellToGroup(newGroupIndex2, cells[i]);
            }

            //assert that there is now one group
            Assert.That(env.GetGroups().Count == 2);
        }
コード例 #2
0
ファイル: CellDefinition.cs プロジェクト: dorchard/mucell
        /// <summary>
        /// 	Constructs a cell instance object using this as the cell definition
        /// </summary>
        /// <returns>
        /// A newly instantiated <see cref="CellInstance" />
        /// </returns>
        public CellInstance createCell()
        {
            // Create a new instance
            CellInstance newInstance = new CellInstance(this);

            if (this.sbmlModel.listOfReactions != null)
            {
                // Fold all relevant reactions into a list of functions
                List<EffectReactionEvaluationFunction> reactions = new List<EffectReactionEvaluationFunction>();
                foreach (SBML.Reaction reaction in this.sbmlModel.listOfReactions)
                {
                    reactions.Add(reaction.ToEffectReactionEvaluationFunction());
                }

                // Add the reaction function list
                newInstance.setReactions(reactions);
            }

            // Add the compnents from the model
            if (this.sbmlModel.listOfComponents != null)
            {
                foreach (SBML.ExtracellularComponents.ComponentBase component in this.sbmlModel.listOfComponents)
                {

                    newInstance.Components.Add(component.CreateWorldStateObject());
                }
            }

            return newInstance;
        }
コード例 #3
0
ファイル: TestFormulas.cs プロジェクト: dorchard/mucell
        public void testWithVariables()
        {
            // create a formula
            MuCell.Model.SBML.Reader.SBMLReader reader = new MuCell.Model.SBML.Reader.SBMLReader();
            MuCell.Model.SBML.Model model = new MuCell.Model.SBML.Model();
            Species s = new Species("X");
            s.BoundaryCondition = false;
            model.AddId(s.ID, s); // else FormulaParser can't find what X is
            reader.model = model;

            FormulaParser fp = new FormulaParser(reader, "10+sin(X)", model);
            MathTree formulaTree = fp.getFormulaTree();

            // fold the function
            CellEvaluationFunction fun = formulaTree.ToCellEvaluationFunction();

            // create a new CellInstance with a variable X
            CellInstance cell = new CellInstance(new CellDefinition());
            cell.setSpeciesAmount("X", 2.3d);

            // evaluate and test that this gets the right result
            Assert.AreEqual(10+Math.Sin(2.3), fun(new StateSnapshot(), cell));
        }
コード例 #4
0
ファイル: Simulation.cs プロジェクト: dorchard/mucell
        /// <summary>
        /// Private method for performing the spatial simulation.
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="currentState"></param>
        /// <param name="time"></param>
        /// <param name="StepTime"></param>
        private void SpatialSimulator(CellInstance cell, StateSnapshot currentState, double time, double StepTime)
        {
            Vector3 p = cell.CellInstanceSpatialContext.Position;
            Vector3 v = cell.CellInstanceSpatialContext.Velocity;
            Vector3 ang = cell.CellInstanceSpatialContext.Orientation;
            float visc = currentState.SimulationEnvironment.Viscosity;
            float h = (float)StepTime;

            //viscous drag (Stokes's drag)
            v.x += -visc * h * v.x * 1.5f;
            v.y += -visc * h * v.y * 1.5f;
            v.z += -visc * h * v.z * 1.5f;

            //cell collisions
            if (currentState.SimulationEnvironment.EnableCellCollisions)
            {
                foreach (CellInstance cellB in currentState.Cells)
                {
                    if (cell != cellB) //avoid collision with self
                    {
                        //the vector from the centre of this cell to cellB
                        Vector3 dist = new Vector3(p.x - cellB.CellInstanceSpatialContext.Position.x,
                                                   p.y - cellB.CellInstanceSpatialContext.Position.y,
                                                   p.z - cellB.CellInstanceSpatialContext.Position.z);
                        float distMag = dist.magnitude();

                        if (distMag < cell.CellInstanceSpatialContext.Radius*0.001 + cellB.CellInstanceSpatialContext.Radius*0.001)
                        {
                            /*
                             * Collision occured: accelerate this cell away from the centre of the cell it is colliding with:
                             */

                            dist.unitVect(); //normalize
                            dist.x *= -h*0.1f;
                            dist.y *= -h*0.1f;
                            dist.z *= -h*0.1f;

                            //accelerate this cell and the one it collided with in oppose directions:
                            v.x -= dist.x;
                            v.y -= dist.y;
                            v.z -= dist.z;
                            cellB.CellInstanceSpatialContext.Accelerate(dist);

                        }

                    }
                }

            }

            //positional change
            p.x += v.x * h;
            p.y += v.y * h;
            p.z += v.z * h;

            //check that boundary conditions are maintained (most of the time this will be true, and will be the only test needed)
            if (!currentState.SimulationEnvironment.Boundary.InsideBoundary(p))
            {
                /*
                 * If the cell is not inside the environment boundary, we move it back
                 * and attempt to move the cell by individual components (this allows
                 * the cell to "slide" along the edges of the boundary without
                 * breaking the boundary conditions). In most cases these three boundary
                 * checks should not be necessary
                 */

                //begin by checking x component, so move back y and z components
                p.y -= v.y * h;
                p.z -= v.z * h;
                if (!currentState.SimulationEnvironment.Boundary.InsideBoundary(p))
                {
                    //x failed, so move back x
                    p.x -= v.x * h;
                }

                //check y component
                p.y += v.y * h;
                if (!currentState.SimulationEnvironment.Boundary.InsideBoundary(p))
                {
                    //y failed
                    p.y -= v.y * h;
                }

                //check z component
                p.z += v.z * h;
                if (!currentState.SimulationEnvironment.Boundary.InsideBoundary(p))
                {
                    //z failed
                    p.z -= v.z * h;
                }

            }

            cell.CellInstanceSpatialContext.Position = p;
            cell.CellInstanceSpatialContext.Velocity = v;
            cell.CellInstanceSpatialContext.Orientation = ang;
        }
コード例 #5
0
ファイル: Simulation.cs プロジェクト: dorchard/mucell
 public static void SpatialSimulatorZZZ(CellInstance cell, StateSnapshot currentState, double time, double StepTime)
 {
 }
コード例 #6
0
ファイル: TestExperiment.cs プロジェクト: dorchard/mucell
        public void TestSerializationEquality()
        {
            String filename = "../../UnitTests/expt1.serialized.xml";

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

            Simulation simulation1 = new Simulation("simulation1");
            Results results = new Results();

            MuCell.Model.SBML.Model model = new MuCell.Model.SBML.Model();
            CellDefinition cellDef1 = new CellDefinition("cellDef1");
            cellDef1.addSBMLModel(model);

            for (int i = 0; i < 5; i++)
            {
                StateSnapshot snapshot = new StateSnapshot();
                for (int j = 0; j < 10; j++)
                {
                    CellInstance cell = new CellInstance(cellDef1);
                    cell.GroupID = j;
                    cell.CellInstanceSpatialContext.Position = new Vector3(1*j, 1+i, 2+j);
                    cell.CellInstanceSpatialContext.Velocity = new Vector3(4*i, 5+j, 3+i);
                    cell.CellInstanceSpatialContext.Volume = new Vector3(10+j, 14*i, 15*i);
                    cell.CellInstanceSpatialContext.Orientation = new Vector3(2+i, 3*j, 4*j);

                    snapshot.Cells.Add(cell);
                }
                MuCell.Model.Environment environment = new MuCell.Model.Environment(new Vector3(10 * i, 14 * i, 15 * i));

                SerializableDictionary<int, MuCell.Model.SBML.Group> dict = new SerializableDictionary<int, MuCell.Model.SBML.Group>();

                // create new groups x3
                MuCell.Model.SBML.Group group1 = new MuCell.Model.SBML.Group(1);
                group1.Col = System.Drawing.Color.Beige;
                MuCell.Model.SBML.Group group2 = new MuCell.Model.SBML.Group(2);
                group2.Col = System.Drawing.Color.Brown;
                MuCell.Model.SBML.Group group3 = new MuCell.Model.SBML.Group(3);
                group3.Col = System.Drawing.Color.Green;
                dict.Add(1, group1); dict.Add(2, group2); dict.Add(3, group3);
                environment.Groups = dict;

                //SerializableDictionary<int, MuCell.Model.NutrientField> nutDict = new SerializableDictionary<int, MuCell.Model.NutrientField>();

                //// create new nutrients x2
                //MuCell.Model.NutrientField nut1 = new MuCell.Model.NutrientField(1);
                //MuCell.Model.NutrientField nut2 = new MuCell.Model.NutrientField(2);
                //nut2.Col = System.Drawing.Color.Fuchsia;
                //nutDict.Add(1,nut1); nutDict.Add(2,nut2);
                //environment.Nutrients = nutDict;

                snapshot.SimulationEnvironment = environment;

                results.StateSnapshots.Add(snapshot);
            }
            results.CurrentState = results.StateSnapshots[4];

            for (int i = 0; i < 3; i++)
            {
                TimeSeries timeSeries = new TimeSeries("Function" + i, 1.2 * i);

                for (int j = 0; j < 20; j++)
                {
                    timeSeries.Series.Add(0.43+i*j+0.031*j);
                }

                results.TimeSeries.Add(timeSeries);
            }
            results.FilePath = "some-file-path";

            simulation1.SimulationResults = results;

            SimulationParameters simulationParams1 = new SimulationParameters();
            simulationParams1.TimeSeries = results.TimeSeries;
            simulationParams1.InitialState = results.StateSnapshots[0];
            // add to simulation
            simulation1.Parameters = simulationParams1;
            // expt.addSimulation
            experiment.addSimulation(simulation1);

            XmlSerializer s = new XmlSerializer(typeof(MuCell.Model.Experiment));
            TextWriter w = new StreamWriter(filename);
            s.Serialize(w, experiment);
            w.Close();

            TextReader tr = new StreamReader(filename);
            Experiment deserializedExpt = (Experiment)s.Deserialize(tr);
            tr.Close();

            Assert.IsTrue(experiment.exptEquals(deserializedExpt));
        }
コード例 #7
0
ファイル: Simulation.cs プロジェクト: dorchard/mucell
        /// <summary>
        /// Private method for performing the spatial simulation.
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="currentState"></param>
        /// <param name="time"></param>
        /// <param name="StepTime"></param>
        private void SpatialSimulator(CellInstance cell, StateSnapshot currentState, double time, double StepTime)
        {
            Vector3 p = cell.CellInstanceSpatialContext.Position;
            Vector3 v = cell.CellInstanceSpatialContext.Velocity;
            Vector3 ang = cell.CellInstanceSpatialContext.Orientation;
            float visc = currentState.SimulationEnvironment.Viscosity;
            float h = (float)StepTime;

            //viscous drag (Stokes's drag)
            v.x += -visc * h * v.x * 1.5f;
            v.y += -visc * h * v.y * 1.5f;
            v.z += -visc * h * v.z * 1.5f;

            //positional change
            p.x += v.x * h;
            p.y += v.y * h;
            p.z += v.z * h;

            //check that boundary conditions are maintained (most of the time this will be true, and will be the only test needed)
            if (!currentState.SimulationEnvironment.Boundary.InsideBoundary(p))
            {
                /*
                 * If the cell is not inside the environment boundary, we move it back
                 * and attempt to move the cell by individual components (this allows
                 * the cell to "slide" along the edges of the boundary without
                 * breaking the boundary conditions). In most cases these three boundary
                 * checks should not be necessary
                 */

                //begin by checking x component, so move back y and z components
                p.y -= v.y * h;
                p.z -= v.z * h;
                if (!currentState.SimulationEnvironment.Boundary.InsideBoundary(p))
                {
                    //x failed, so move back x
                    p.x -= v.x * h;
                }

                //check y component
                p.y += v.y * h;
                if (!currentState.SimulationEnvironment.Boundary.InsideBoundary(p))
                {
                    //y failed
                    p.y -= v.y * h;
                }

                //check z component
                p.z += v.z * h;
                if (!currentState.SimulationEnvironment.Boundary.InsideBoundary(p))
                {
                    //z failed
                    p.z -= v.z * h;
                }

            }

            cell.CellInstanceSpatialContext.Position = p;
            cell.CellInstanceSpatialContext.Velocity = v;
            cell.CellInstanceSpatialContext.Orientation = ang;
        }
コード例 #8
0
 public CellToSort(CellInstance cell,float depth)
 {
     this.cell = cell;
     this.depth = depth;
 }
コード例 #9
0
ファイル: CellInstance.cs プロジェクト: dorchard/mucell
        public bool exptEquals(CellInstance other)
        {
            if (this.GroupID != other.GroupID)
            {
                Console.Write("CellInstance objects not equal: ");
                Console.WriteLine("this.GroupID='" + this.GroupID + "'; other.GroupID='" + other.GroupID);
                return false;
            }
            if (this.CellInstanceDefinition.exptEquals(other.CellInstanceDefinition) == false)
            {
                Console.Write("CellInstance objects not equal: ");
                Console.Write("this.CellInstanceDefinition.Name='" + this.CellInstanceDefinition.Name);
                Console.WriteLine("'; other.CellInstanceDefinition.Name'" + other.CellInstanceDefinition.Name);
                return false;
            }
            if (this.CellInstanceSpatialContext.exptEquals(other.CellInstanceSpatialContext) == false)
            {
                Console.Write("CellInstance objects not equal: ");
                Console.WriteLine("this.CellInstanceSpatialContext != other.CellInstanceSpatialContext");
                return false;
            }

            return true;
        }
コード例 #10
0
ファイル: CellInstance.cs プロジェクト: dorchard/mucell
        /// <summary>
        /// Clones a CellInstance
        /// </summary>
        /// <returns>
        /// A <see cref="Object"/>
        /// </returns>
        public Object Clone()
        {
            // Clone the component world states
            List<SBML.ExtracellularComponents.ComponentWorldStateBase> clonedComponents = new List<SBML.ExtracellularComponents.ComponentWorldStateBase>();
            foreach (SBML.ExtracellularComponents.ComponentWorldStateBase componentWorldState in this.components)
            {
                clonedComponents.Add((SBML.ExtracellularComponents.ComponentWorldStateBase)componentWorldState.Clone());
            }

            // create the objects
            CellInstance cloned = new CellInstance(this.cellInstanceDefinition, (SpatialContext)this.cellInstanceSpatialContext.Clone(),
                this.species, this.speciesVariables, this.indexToSpeciesMap, this.speciesToIndexMap,
                this.solver, clonedComponents, this.randomObject);
            // Set reactions
            cloned.GroupID = this.GroupID;
            cloned.reactions = this.reactions;
            return cloned;
        }