Beispiel #1
0
    void OnGUI()
    {
        CUE cue = CUE.GetInstance();

        ReactionType[] reactionTypes = cue.ReactionTypes;

        // Make a background box
        GUI.Box(new Rect(10, 10, 300, 30 + reactionTypes.Length * 25), "Reaction For Molecule");

        if (GUI.Button(new Rect(20, 40, 280, 20), "Attach"))
        {
            ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
            Thread      myThread         = new Thread(myThreadDelegate);
            myThread.Start();
            Thread.Sleep(100);
            Debug.Log("Main - aborting my thread.");
            myThread.Abort();
            myThread.Join();
            Debug.Log("Main ending.");
        }

        int i = 1;

        foreach (var item in reactionTypes)
        {
            if (GUI.Button(new Rect(20, 40 + 25 * i, 280, 20), item.ToString()))
            {
            }

            i++;
        }
    }
    protected override void OnItemGui(MoleculeSpecies item)
    {
        if (item == null)
        {
            base.OnItemGui(item);
            return;
        }

        CUE cue = CUE.GetInstance();

        item.Name            = EditorGUILayout.TextField("Name", item.Name);
        item.InitialQuantity = EditorGUILayout.IntField("Initial Quantity [1]", item.InitialQuantity);
        item.Mass            = EditorGUILayout.FloatField("Mass [u]", item.Mass);
        EditorGUILayout.LabelField("~Size [nm]", Utils.ScaleToNm(item.Size).ToString());
        //EditorGUILayout.LabelField("ID", item.GetInstanceID().ToString());

        EditorUtility.SetDirty(item);

        if (GUILayout.Button("remove"))
        {
            cue.RemoveSpecies(item);

            EditorUtility.SetDirty(cue);
        }
    }
Beispiel #3
0
        /// <summary>
        /// Set a molecule ready for reactiong.
        /// Called when molecules collide with each other.
        /// When all molecules are ready, cue.ReactionManager.PerformReaction is called.
        /// </summary>
        /// <param name="molecule">Molecule.</param>
        public void Ready(Molecule molecule)
        {
            bool allReady = true;

            for (int i = 0; i < molecules.Count; i++)
            {
                if (molecule == molecules[i])
                {
                    ready[i] = true;

                    if (!allReady)
                    {
                        break;
                    }
                }

                allReady = allReady & ready[i];
            }

            if (allReady && !performed)
            {
                performed = true;

                CUE cue = CUE.GetInstance();
                cue.ReactionManager.PerformReaction(this);
            }
        }
Beispiel #4
0
        private void Deselect()
        {
            CameraControl.Follow = null;
            CUE cue = CUE.GetInstance();

            cue.ReactionManager.SelectedMolecule = null;

            RemoveScript();
        }
Beispiel #5
0
        void OnDrawGizmos()
        {
            Gizmos.color = new Color(0, 0, 1, 0.5f);

            CUE cue = CUE.GetInstance();

            float r = Utils.GetSphereRadius(cue.Volume);

            Gizmos.DrawSphere(Vector3.zero, Utils.ScaleFromNm(r));
        }
        /// <summary>
        /// Simulates one step.
        /// </summary>
        private void SimulateStep()
        {
            Debug.Log("Step");

            CUE cue = CUE.GetInstance();

            SimulationStep step = simulator.Step(cue.SimulationStep);

            EnqueueStep(step);
            nextStep = nextStep.AddSeconds(cue.VisualizationStep);
        }
Beispiel #7
0
    public override void OnInspectorGUI()
    {
        Molecule t   = (Molecule)target;
        CUE      cue = CUE.GetInstance();

        MoleculeSpeciesPopup speciesPopup = new MoleculeSpeciesPopup(cue, false);

        t.Species = speciesPopup.Popup(t.Species, false);

        EditorUtility.SetDirty(t);

        if (GUILayout.Button("show CellUnity Editor"))
        {
            CellUnityWindow.Init();
        }
    }
        public void Reload()
        {
            CUE cue = CUE.GetInstance();

            SimulationState oldState = state;

            if (state != SimulationState.Stopped)
            {
                Stop();
            }

            if (simulator == null)
            {
                this.simulator = new Copasi.CopasiSimulator();
                simulator.Init(cue);
            }
            else
            {
                simulator.Reload();
            }

            cue.ScriptManager.GetOrAddScript <CellUnity.Simulation.SimulatorScript>();

            simulationThread = new Thread(new ThreadStart(RunSimulation));
            simulationThread.IsBackground = true;

            // restore old state

            if (oldState == SimulationState.Running)
            {
                Start();
            }
            else if (oldState == SimulationState.Paused)
            {
                Pause();
            }
            else if (oldState == SimulationState.Stopped)
            {
                state = SimulationState.Stopped;
            }
        }
    protected override void OnItemGui(ReactionType item)
    {
        CUE cue = CUE.GetInstance();

        if (item == null)
        {
            base.OnItemGui(item);
            return;
        }

        item.Name = EditorGUILayout.TextField("Name", item.Name);

        EditorGUILayout.BeginHorizontal();

        item.Reagents = GuiSpeciesList(cue, item.Reagents);

        EditorGUILayout.LabelField(" \u2192 ", GUILayout.MaxWidth(30));

        item.Products = GuiSpeciesList(cue, item.Products);

        EditorGUILayout.EndHorizontal();

        item.Rate = EditorGUILayout.FloatField("Rate [nl/(nmol*s)]", item.Rate);

        EditorUtility.SetDirty(item);

        if (Application.isPlaying)
        {
            if (GUILayout.Button("start reaction"))
            {
                cue.ReactionManager.InitiateReaction(item, true);
            }
        }

        if (GUILayout.Button("remove"))
        {
            cue.RemoveReaction(item);

            EditorUtility.SetDirty(cue);
        }
    }
        /// <summary>
        /// Should be called every frame.
        /// Processes Simulation results and initiates reactions.
        /// </summary>
        public void Update()
        {
            if (runInMainThread)
            {
                MainThreadRunSimulation();
            }

            // Process Simulation Results
            while (true)
            {
                SimulationStep step;
                lock (stepsQueueLock)
                {
                    if (stepsQueue.Count == 0)
                    {
                        break;
                    }
                    step = stepsQueue.Dequeue();
                }

                CUE cue = CUE.GetInstance();

                System.Text.StringBuilder info = new System.Text.StringBuilder();                 // TODO: remove

                foreach (ReactionCount item in step.Reactions)
                {
                    info.Append(item.ReactionType.ToString() + ": " + item.Count + ";   ");

                    for (ulong i = 0; i < item.Count; i++)
                    {
                        cue.ReactionManager.InitiateReaction(item.ReactionType, true);
                    }
                }

                Debug.Log(info.ToString());
            }
        }
Beispiel #11
0
        void OnGUI()
        {
            CUE cue = CUE.GetInstance();

            // Get selected molecule
            Molecule m = cue.ReactionManager.SelectedMolecule;

            // Set following
            if (following)
            {
                CameraControl.Follow = m.gameObject;
            }
            else
            {
                CameraControl.Follow = null;
            }

            // Draw menu or remove script if no moleule is selected
            if (m == null)
            {
                RemoveScript();
            }
            else
            {
                GuiBoxReset();

                GuiBeginBox("Molecule", 94);

                string reaction;
                string reactionStatus;

                if (m.ReactionPrep != null)
                {
                    reaction       = m.ReactionPrep.ReactionType.ToString();
                    reactionStatus = "performing";
                }
                else
                {
                    ReactionType selectedReaction = cue.ReactionManager.SelectedReaction;
                    if (selectedReaction != null)
                    {
                        reaction       = selectedReaction.ToString();
                        reactionStatus = "waiting";
                    }
                    else
                    {
                        reaction       = "none";
                        reactionStatus = "";
                    }
                }

                GuiCaptionValue("Species:", m.Species.Name);
                GuiCaptionValue("Reaction:", reaction + "\n\r" + reactionStatus);

                GUILayout.BeginHorizontal();

                if (GUILayout.Button(following ? "Stay" : "Follow"))
                {
                    following = !following;
                }

                if (GUILayout.Button("Deselect"))
                {
                    Deselect();
                }

                if (GUILayout.Button("Hide"))
                {
                    Hide();
                }

                GUILayout.EndHorizontal();

                GuiEndBox();

                //
                // Reactions
                //

                List <ReactionType> reactions = new List <ReactionType>();
                foreach (var r in cue.ReactionTypes)
                {
                    if (Utils.ArrayContains <MoleculeSpecies>(r.Reagents, m.Species))
                    {
                        reactions.Add(r);
                    }
                }

                GuiBeginBox("Reaction", (reactions.Count + 1) * (guiReactionHeight) + guiReactionSpace);

                if (GUILayout.Button("none"))
                {
                    cue.ReactionManager.SelectedReaction = null;
                }

                GUILayout.Space(guiReactionSpace);

                foreach (var r in reactions)
                {
                    if (GUILayout.Button(r.ToString()))
                    {
                        cue.ReactionManager.SelectedReaction = r;
                    }
                }

                GuiEndBox();

                if (Input.GetKey("escape"))
                {
                    Deselect();
                }
                if (Input.GetKey("return"))
                {
                    Hide();
                }
            }
        }
Beispiel #12
0
 void OnGUI()
 {
     CueEditor.CueGui(CUE.GetInstance());
 }
Beispiel #13
0
        public MoleculeSpecies Create()
        {
            // find a name if not specified
            findName();

            // create main object of the species
            GameObject mol = new GameObject(name);

            mol.transform.position = Vector3.zero;

            float colliderRadius = 0;

            //
            // Find center of all gameObjects
            //

            Vector3 center = Vector3.zero;

            foreach (var obj in gameObjects)
            {
                // set main object as parent
                obj.transform.parent = mol.transform;

                center += obj.transform.position;

                // remove collider for performance boost
                if (obj.collider != null)
                {
                    obj.collider.enabled = false;
                    CellUnity.Utility.ScriptManager.RemoveComponent(obj.collider);
                }
            }

            center = center / gameObjects.Length;

            //
            // locate object to (0,0,0) by setting center to (0,0,0)
            // and calculate the colliderRadius by making sure every atom is inside this radius
            //
            foreach (var obj in gameObjects)
            {
                obj.transform.position -= center;

                colliderRadius = Math.Max(colliderRadius, Math.Abs(obj.renderer.bounds.min.x));
                colliderRadius = Math.Max(colliderRadius, Math.Abs(obj.renderer.bounds.min.y));
                colliderRadius = Math.Max(colliderRadius, Math.Abs(obj.renderer.bounds.min.z));
                colliderRadius = Math.Max(colliderRadius, Math.Abs(obj.renderer.bounds.max.x));
                colliderRadius = Math.Max(colliderRadius, Math.Abs(obj.renderer.bounds.max.y));
                colliderRadius = Math.Max(colliderRadius, Math.Abs(obj.renderer.bounds.max.z));
            }

            // Create a new species and add it to the CUE
            CUE             cue     = CUE.GetInstance();
            MoleculeSpecies species = cue.CreateMoleculeSpecies();

            species.Name = name;
            cue.AddSpecies(species);

            // Add the molecule script to the main object
            CellUnity.Molecule script = mol.AddComponent <CellUnity.Molecule>();
            script.Species = species;
            species.Mass   = mass;
            species.Size   = colliderRadius * 2;

            // Add a sphere collider to the main object
            SphereCollider sphereCollider = mol.AddComponent <SphereCollider> ();
            Rigidbody      rigidbody      = mol.AddComponent <Rigidbody> ();

            rigidbody.useGravity  = false;
            rigidbody.mass        = mass;
            sphereCollider.radius = colliderRadius;

            // Add a diffusion script to the main object
            View.Diffusion diffusion = mol.AddComponent <View.Diffusion> ();
            diffusion.Intensity = Diffusion;

            // create the prefab used as template for the species
            string assetPath = "Assets/Molecules/" + name + ".prefab";

            UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab(assetPath);
            PrefabUtility.ReplacePrefab(mol, prefab);
            AssetDatabase.Refresh();

            species.PrefabPath = assetPath;

            EditorUtility.SetDirty(cue);

            // Delete game objects
            foreach (var obj in gameObjects)
            {
                GameObject.DestroyImmediate(obj);
            }
            GameObject.DestroyImmediate(mol);

            return(species);
        }
Beispiel #14
0
 // Use this for initialization
 void Start()
 {
     cue = CUE.GetInstance();
     Debug.Log("Auto Reaction started");
 }
Beispiel #15
0
 // Use this for initialization
 void Start()
 {
     this.cue = CUE.GetInstance();
 }
Beispiel #16
0
        /// <summary>
        /// Initiates a reaction.
        /// </summary>
        /// <returns><c>true</c>, if the reaction was initiated, <c>false</c> otherwise.</returns>
        /// <param name="reaction">Reaction.</param>
        /// <param name="queueIfNotPossible">If set to <c>true</c>, the reaction is queued and performed later if the reaction is not possible at the moment.</param>
        public bool InitiateReaction(ReactionType reaction, bool queueIfNotPossible)
        {
            CUE cue = CUE.GetInstance();

            ReactionPrep reactionPrep = new ReactionPrep(reaction);

            bool moleculesFound;

            // Select molecules

            // Prefer selected molecule if suitable
            if (
                selectedMolecule != null && selectedReaction != null &&                 // molecule and reaction must be selected
                selectedReaction == reaction &&                                         // selected reaction must match current reaction
                selectedMolecule.ReactionPrep == null                                   // selected molecule must not be already involved in a reaction
                )
            {
                moleculesFound = cue.Molecules.FindMolecuelsForReaction(reactionPrep, selectedMolecule);
            }
            else
            {
                // select random molecules
                moleculesFound = cue.Molecules.FindMolecuelsForReaction(reactionPrep);
            }

            if (moleculesFound)
            {
                // Ready reactions for 0 to 1 reagents because Collision is never called for these molecules

                if (reactionPrep.MoleculeCount == 0)
                {
                    reactionPrep.Ready(null);
                }
                if (reactionPrep.MoleculeCount == 1)
                {
                    reactionPrep.Ready(reactionPrep.Molecules[0]);                     // necessary if only one reagent in reaction
                }

                return(true);
            }
            else
            {
                // not sufficient molecules for reaction

                reactionPrep.Release();                 // remove ReactonPrep from Molecules

                if (queueIfNotPossible)
                {
                    // --> queue Reaction for later

                    ShortKeyDict <ReactionType, int> .Entry entry;
                    if (!openReactions.Find(reaction, out entry))
                    {
                        entry = openReactions.Set(reaction, 0);
                    }

                    entry.Value++;
                }

                return(false);
            }
        }