예제 #1
0
 public static void EquilibriumFunction(Matter.Energy e1, Matter.Energy e2, double p1, double p2, double rate)
 {
     int nm1 = -1, nm2 = -1;
     Chemistry.EquilibriumFunction(e1.Value, e2.Value, p1, p2, rate, out nm1, out nm2);
     e1.Value = nm1;
     e2.Value = nm2;
 }
예제 #2
0
        public Matter.ElementType GetElement(Matter.ElementType et)
        {
            foreach (Matter.ElementType met in this._typeArray)
                if (met.MyName == et.MyName)
                    return met;

            return null;
        }
예제 #3
0
        //Constructor
        public Chemistry(Matter.ElementType[] typeArray, Reaction[] reactArray)
        {
            this._typeArray = typeArray;
            this._reactionArray = reactArray;

            //Link Reactions to appropriate types
            for (int i = 0; i < this._typeArray.Length; i++)
            {
                for (int j = 0; j < this._reactionArray.Length; j++)
                {
                    if (this._typeArray[i].MyName == this._reactionArray[j].Reactant.MyName ||
                        (this._reactionArray[j].IsReversible && this._typeArray[i].MyName == this._reactionArray[j].Product.MyName))
                        this._typeArray[i].AddReaction(this._reactionArray[j]);

                }
            }
        }
예제 #4
0
        public Matter RemoveMixedContent(Matter m)
        {
            if (!this._myMixedContents.Contains(m))
                throw new Exception("Mixture does not contain matter input");

            this._myMixedContents.Remove(m);
            this.MixMass -= m.TotalMass;
            m._owner = null;

            return m;
        }
예제 #5
0
        public void DegenerateExistence()
        {
            if (this.ElementUnits != 0)
            {
                int a = 7 * 8 / 9;
                int b = a * 9;
                int c = a / b * 6;
            }

            if (this._owner == null)
            {
                //Handle Mixture
                var tm = this.TotalMass;
                while (this._myMixedContents.Count > 0)
                {
                    var cm = this.RemoveMixedContent(this._myMixedContents[0]);
                    var ht = (int)(this._myHeatE.Value * this.ElementMass / tm) + 1;
                    if (ht > cm._myHeatE.Value)
                        ht = cm._myHeatE.Value;
                    cm.AbsorbHeat(ht);
                    this.ReleaseHeat(ht);

                    if (cm.ElementTypeName == "DNA")
                        cm = new Matter(new MatterChunk(cm, cm._myType.Reactions[0].Reactant, cm.ElementUnits));

                    this._mySimulator.AddAgent(cm);
                }

                this._mySimulator.RemoveAgent(this);

                //Handle Heat
                if (this._myHeatE.Value > 0)
                {

                }
            }
            else
            {
                //Handle Mixture
                if (this._myMixedContents.Count > 0)
                {
                    int a = 7 * 8 / 9;
                    int b = a * 9;
                    int c = a / b * 6;
                }
                //Remove Energy
                if (this._myHeatE.Value > 0)
                    this._owner._myHeatE.Absorb(this._myHeatE);

                //Remove Physical Traces
                this._owner.RemoveMixedContent(this);
            }
        }
예제 #6
0
        public void AbsorbMatter(Matter m)
        {
            var mc = m._myMixedContents.ToArray<Matter>();
            for (int i = 0; i < mc.Length; i++)
                this.AbsorbChunk(new MatterChunk(m.RemoveMixedContent(mc[i]), mc[i].ElementUnits));

            var mUnits = m.ElementUnits;
            if (mUnits == 0)
                this.AbsorbHeat(m._myHeatE.Value);
            else
                this.AbsorbChunk(new MatterChunk(m, mUnits));
        }
예제 #7
0
        public Matter AbsorbChunk(MatterChunk m)
        {
            //Check for Stackable Exact Match
            if (m.MyType.IsStackable && this.TypeMatches(m))
            {
                this._FullTypeAbsorb(m);
                return this;
            }

            //If Permeable, allow entry itno mixed contents
            if (this._myType.IsPermeable)
            {
                for (int i = 0; i < this._myMixedContents.Count; i++)
                {
                    if (m.MyType.IsStackable && this._myMixedContents[i].TypeMatches(m))
                    {
                        this._myMixedContents[i]._FullTypeAbsorb(m);
                        return this._myMixedContents[i];
                    }
                }
                return this._AddMixedContent(m);
            }

            //Send Absorbtion to parent
            if (this._owner != null)
            {
                return this._owner.AbsorbChunk(m);
            }

            //SEnd Absorbtion to environment
            if (this._mySimulator != null)
            {
                var nm = new Matter(m);
                this._mySimulator.AddAgent(nm);
                return nm;
            }

            //Throw uhandled
            throw new Exception("Uhandled region");
        }
예제 #8
0
 //Constructor
 public Reaction(Matter.ElementType reactant, Matter.ElementType product)
 {
     this._reactant = reactant;
     this._product = product;
 }
예제 #9
0
            public MatterChunk(Matter parMatter, ElementType newType, int elementUnits)
            {
                this.MyType = newType;

                if (parMatter.ElementUnits - elementUnits <= 0)
                {
                    elementUnits = parMatter.ElementUnits;
                    //throw new Exception("unhandled invalid mass");
                    parMatter.NeedsRemoval = true;
                }

                parMatter.ElementUnits -= elementUnits;
                this.ElementUnits = elementUnits;

                var percSplit = (1.0 * elementUnits / (elementUnits + parMatter.ElementUnits));
                this.MyHeat = parMatter.ReleaseHeat((int)(percSplit * parMatter.ElementHeat));
                this.MyCharge = new Energy();
            }
예제 #10
0
        public static void MassNeighborEqualize(Matter m1, Matter m2)
        {
            List<Matter> ms1 = new List<Matter>(), ms2 = new List<Matter>();

            //Create side by side list of applicable matter
            if (m1._myType.IsStackable)
                ms1.Add(m1);

            if (m2._myType.IsStackable)
                ms2.Add(m2);

            if (m1._myType.IsPermeable)
            {
                for (int i = 0; i < m1._myMixedContents.Count; i++)
                {
                    if (m1._myMixedContents[i]._myType.IsStackable)
                        ms1.Add(m1._myMixedContents[i]);
                }
            }

            if (m2._myType.IsPermeable)
            {
                for (int i = 0; i < m2._myMixedContents.Count; i++)
                {
                    if (m2._myMixedContents[i]._myType.IsStackable)
                        ms2.Add(m2._myMixedContents[i]);
                }
            }

            //Get Matches for simple equilibrium function and remove from old list
            List<Matter[]> matches = new List<Matter[]>();
            for (int i = 0; i < ms1.Count; i++)
            {
                var x = (from Matter tm2 in ms2
                         where ms1[i].TypeMatches(tm2)
                         select tm2).ToList<Matter>();

                if (x.Count == 0)
                    continue;

                if (x.Count > 1)
                    throw new Exception("Two matches?");

                if (ms1[i] == x[0])
                    throw new Exception("Unknown event by matching objects");

                if (Math.Abs(((1.0 * ms1[i].ElementUnits) / x[0].ElementUnits) - 1) > .001)
                    matches.Add(new Matter[] { ms1[i], x[0] });

                ms1.RemoveAt(i);
                i--;
                ms2.Remove(x[0]);
            }

            //FLow what is already matching
            for (int i = 0; i < matches.Count; i++)
            {
                int nmm1 = 0, nmm2 = 0;

                Chemistry.EquilibriumFunction(matches[i][0].ElementUnits, matches[i][1].ElementUnits, 1, 1, 1, out nmm1, out nmm2);

                matches[i][0].ElementUnits = nmm1;
                matches[i][1].ElementUnits = nmm2;
            }

            //Go through non matches
            if (m2._myType.IsPermeable)
            {
                for (int i = 0; i < ms1.Count; i++)
                {
                    int nmm1 = 0, nmm2 = 0;

                    Chemistry.EquilibriumFunction(ms1[i].ElementUnits, 0, 1, 1, 1, out nmm1, out nmm2);

                    if (nmm2 == 0)
                        continue;

                    //ms1[i]._elementMass = nmm1;
                    m2.AbsorbChunk(ms1[i].Split(nmm2));
                }
            }
            if (m1._myType.IsPermeable)
            {
                for (int i = 0; i < ms2.Count; i++)
                {
                    int nmm1 = 0, nmm2 = 0;

                    Chemistry.EquilibriumFunction(ms2[i].ElementUnits, 0, 1, 1, 1, out nmm1, out nmm2);

                    if (nmm2 == 0)
                        continue;

                    //ms2[i]._elementMass = nmm1;
                    m1.AbsorbChunk(ms2[i].Split(nmm2));
                }
            }
        }
예제 #11
0
        public static void InternalHeatEqualize(Matter m)
        {
            if (m._myMixedContents.Count == 0)
                return;

            //Cycle through mixcontents and react heat
            var percentage = 1 / m._myMixedContents.Count;
            foreach (Matter mc in m._myMixedContents)
                Chemistry.EquilibriumFunction(m._myHeatE, mc._myHeatE, m.ElementMass, mc.TotalMass, percentage);
        }
예제 #12
0
 public static void HeatNeighborEqualize(Matter m1, Matter m2)
 {
     Chemistry.EquilibriumFunction(m1._myHeatE, m2._myHeatE, m1.TotalMass, m2.TotalMass, Matter._heatFlowRate);
 }
예제 #13
0
            //Function
            public bool VerifyReaction(Matter m)
            {
                //Make sure matter is reactant, otherwise set reversed
                var reversed = false;
                if (this._reversible && m.TypeMatches(this._product))
                    reversed = true;

                //If not reversed and m does not equal reactant, throw exception
                if (!m.TypeMatches(this._reactant) && !reversed)
                    throw new Exception("Matter Object provided does not satisfy given conditions");

                //Get Probability of states
                var temp = 1.0 * m.ElementHeat / m.TotalMass;
                if (temp == 0)
                    return false;

                return true;
            }
예제 #14
0
            public virtual bool React(Matter m)
            {
                //Make sure matter is reactant, otherwise set reversed
                var reversed = false;
                if (this._reversible && m.TypeMatches(this._product))
                    reversed = true;

                //If not reversed and m does not equal reactant, throw exception
                if (!m.TypeMatches(this._reactant) && !reversed)
                    throw new Exception("Matter Object provided does not satisfy given conditions");

                //Get Probability of states
                var temp = 1.0 * m.ElementHeat / m.TotalMass;
                var parts = m.ElementUnits;
                if (temp == 0 || parts == 0)
                    return false;

                //Create Vars
                var muparts = 0;
                var muNparts = 0;
                Matter.ElementType toElement;

                //Handle Case If reversed for muEnergy
                if (reversed)
                {
                    muparts = this._product.Mu;
                    muNparts = this._reactant.Mu;
                    toElement = this._reactant;
                }
                else
                {
                    muparts = this._reactant.Mu;
                    muNparts = this._product.Mu;
                    toElement = this._product;
                }

                var reactKR = Math.Exp((muparts - (muNparts + this._activationEnergy)) / temp);

                if (parts == 0)
                {
                    int a = 7; int b = 3; int c = a / b;
                }

                parts--;
                var ReactionRate = temp * parts * reactKR;

                //If State is greater thanequalto one, then create x amount of new state
                if (ReactionRate > 1)// || StateMuNew >= 1)
                {
                    if (this._reactant.MyName == "DNA")
                    {
                        int a = 7; int b = 3; int c = a / b;
                    }

                    ///////////////////////
                    //Add DATA
                    if (m.Owner != null)
                        m.Owner.MySimulator.MyData.AddData("ReactionRate", ReactionRate);
                    else
                        m.MySimulator.MyData.AddData("ReactionRate", ReactionRate);
                    //////////////////////

                    var enDif = muparts - muNparts;
                    var amt = (int)ReactionRate;

                    if (enDif > 0 && enDif * amt > m.ElementHeat)
                        return false;

                    if (amt > parts)
                        amt = parts;
                    //var amt = (int)StateMuNew;
                    var newChunk = new Matter.MatterChunk(m, toElement, amt);

                    //Handle heat
                    if (enDif < 0)
                        m.ReleaseHeat(amt * Math.Abs(enDif));
                    else if (enDif > 0)
                        m.AbsorbHeat(amt * enDif);
                    else
                        throw new Exception("EnDif is zero or did not interact with environment");
                    var nm = m.AbsorbChunk(newChunk);

                    if (!m.NeedsRemoval && !m.IsRemoved)
                        Matter.HeatNeighborEqualize(nm, m);

                    if (m.IsElementEmpty())
                        m.DegenerateExistence();
                }

                return false;
            }
예제 #15
0
 public Reaction(Matter.ElementType reactant, Matter.ElementType product, double AE, bool reversible)
     : this(reactant, product)
 {
     this._activationEnergy = AE;
     this._reversible = true;
 }
예제 #16
0
        //Getters
        public bool TypeMatches(Matter m)
        {
            if (this._myType.MyName == m._myType.MyName)
                return true;

            return false;
        }
예제 #17
0
        private Matter _AddMixedContent(MatterChunk m)
        {
            if (this.IsElementEmpty() && this.IsRemoved)
                throw new Exception("Unhandled empty state");

            var nm = new Matter(m);
            this._myMixedContents.Add(nm);
            nm._owner = this;
            this.MixMass += nm.TotalMass;
            return nm;
        }
예제 #18
0
        public static void MatterPhyCollision(Matter m1, Matter m2)
        {
            //If not matching but m1 can absorb m2
            if (m2._myType.IsStackable && m1._myType.IsPermeable && m1.TotalMass * _naturalAbsorbtionRation >= m2.TotalMass)
            {
                m1.AbsorbMatter(m2);
                return;
            }
            //Handle complementary case of above
            else if (m1._myType.IsStackable && m2._myType.IsPermeable && m1.TotalMass * _naturalAbsorbtionRation >= m2.TotalMass)
            {
                m2.AbsorbMatter(m1);
                return;
            }
            else if (m1.TotalMass + m2.TotalMass < 1500)
            {
                //If matchign types
                if (m1.TypeMatches(m2))
                {
                    m1.AbsorbMatter(m2);
                    return;
                }

                //If not matching but m1 can absorb m2
                if (m2._myType.IsStackable && m1._myType.IsPermeable)
                {
                    m1.AbsorbMatter(m2);
                    return;
                }

                //Handle complementary case of above
                if (m1._myType.IsStackable && m2._myType.IsPermeable)
                {
                    m2.AbsorbMatter(m1);
                    return;
                }
            }
            else
                Matter.MassNeighborEqualize(m1, m2);

            Matter.HeatNeighborEqualize(m1, m2);
        }
예제 #19
0
            public MatterChunk(Matter parMatter, int elementUnits)
            {
                this.MyType = parMatter._myType;

                if (parMatter.ElementUnits - elementUnits <= 0)
                {
                    elementUnits = parMatter.ElementUnits;
                    //throw new Exception("This unhandled invalid mass");
                    parMatter.NeedsRemoval = true;
                }

                parMatter.ElementUnits -= elementUnits;
                this.ElementUnits = elementUnits;

                this.MyHeat = new Energy();
                this.MyCharge = new Energy();
            }
예제 #20
0
 //Constructor
 public MolecularReaction(Matter.ElementType reactant, Matter.ElementType product, double AE, bool reversible)
     : base(reactant, product, 0, reversible)
 {
     //if (!product.IsMolecule)
         //throw new Exception("Molecular Properties of Product is not a Molecule");
 }