//Loads the reactions from a file, and returns a dictionary
    public static Dictionary <string, ReactionClass> LoadReactions()
    {
        JSONEditor json = new JSONEditor("reactions");

        int numberOfReactions = json.NumberOfObjects();

        Dictionary <string, ReactionClass> Reagents = new Dictionary <string, ReactionClass>();

        if (numberOfReactions > 0)
        {
            for (int i = 0; i < numberOfReactions; i++)
            {
                ReactionClass reaction = new ReactionClass();

                reaction.name = json.GetString(i, "name");

                reaction.reagent1    = json.GetString(i, "aName");
                reaction.reagent2    = json.GetString(i, "bName");
                reaction.mainProduct = json.GetString(i, "cName");
                reaction.subProduct  = json.GetString(i, "dName");

                reaction.stoichiometryR1          = json.GetInt(i, "aMultiply");
                reaction.stoichiometryR2          = json.GetInt(i, "bMultiply");
                reaction.stoichiometryMainProduct = json.GetInt(i, "cMultiply");
                reaction.stoichiometrySubProduct  = json.GetInt(i, "dMultiply");

                Reagents.Add(reaction.name, reaction);
            }
        }
        return(Reagents);
    }
 public ReactionClass(ReactionClass re)
 {
     this.name                     = re.name;
     this.reagent1                 = re.reagent1;
     this.stoichiometryR1          = re.stoichiometryR1;
     this.reagent2                 = re.reagent2;
     this.stoichiometryR2          = re.stoichiometryR2;
     this.mainProduct              = re.mainProduct;
     this.stoichiometryMainProduct = re.stoichiometryMainProduct;
     this.subProduct               = re.subProduct;
     this.stoichiometrySubProduct  = re.stoichiometrySubProduct;
 }
Example #3
0
    //! Make the reaction using concentration A and B.
    public void MakeReaction(string reactionName, float concentrationA, float concentrationB)
    {
        ReactionClass reaction = ReactionsSaver.LoadReactions() [reactionName];

        float realConcentrationA;
        float realConcentrationB;

        float realConcentrationC;
        float realConcentrationD;

        realConcentrationB = concentrationB;         // limitant!!!

        realConcentrationA = realConcentrationB * reaction.stoichiometryR1;

        realConcentrationC = realConcentrationA * reaction.stoichiometryMainProduct;

        realConcentrationD = realConcentrationA * reaction.stoichiometrySubProduct;

        float phC = 0, phD = 0;
        float turbidityC = 0, turbidityD = 0;

        Dictionary <string, Compound> reagents = CompoundFactory.GetInstance().CupboardCollection;

        if (!reagents [reaction.mainProduct].IsSolid)
        {
            phC        = reagents [reaction.mainProduct].PH;
            turbidityC = reagents [reaction.mainProduct].Turbidity;
        }

        if (!reagents [reaction.subProduct].IsSolid)
        {
            phD        = reagents [reaction.mainProduct].PH;
            turbidityD = reagents [reaction.mainProduct].Turbidity;
        }

        polarizability = (reagents [reaction.mainProduct].Polarizability * realConcentrationC + reagents [reaction.subProduct].Polarizability * realConcentrationD) / (realConcentrationC + realConcentrationD);
        conductibility = (reagents [reaction.mainProduct].Conductibility * realConcentrationC + reagents [reaction.subProduct].Conductibility * realConcentrationD) / (realConcentrationC + realConcentrationD);
        solubility     = (reagents [reaction.mainProduct].Solubility * realConcentrationC + reagents [reaction.subProduct].Solubility * realConcentrationD) / (realConcentrationC + realConcentrationD);

        ph        = (phC * realConcentrationC + phD * realConcentrationD) / (realConcentrationC + realConcentrationD);
        turbidity = (turbidityC * realConcentrationC + turbidityD * realConcentrationD) / (realConcentrationC + realConcentrationD);

/*		float colorR = (reagents [reaction.cName].color.r * realConcentrationC + reagents [reaction.dName].color.r * realConcentrationD) / (realConcentrationC + realConcentrationD);
 *              float colorG = (reagents [reaction.cName].color.g * realConcentrationC + reagents [reaction.dName].color.g * realConcentrationD) / (realConcentrationC + realConcentrationD);
 *              float colorB = (reagents [reaction.cName].color.b * realConcentrationC + reagents [reaction.dName].color.b * realConcentrationD) / (realConcentrationC + realConcentrationD);
 *              float colorA = (reagents [reaction.cName].color.a * realConcentrationC + reagents [reaction.dName].color.a * realConcentrationD) / (realConcentrationC + realConcentrationD);
 *
 *              color = new Color (colorR, colorG, colorB, colorA);*/
    }
    void OnGUI()
    {
        EditorGUILayout.BeginVertical();
        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Width(0), GUILayout.Height(0));

        EditorGUILayout.LabelField("Deletar Reacao:");

        Dictionary <string, ReactionClass> reactions = ReactionsSaver.LoadReactions();

        string[] names = new string[reactions.Count];

        int counter = 0;

        foreach (string name in reactions.Keys)
        {
            names[counter] = name;
            counter++;
        }

        indexOfReactions = EditorGUILayout.Popup(indexOfReactions, names);

        selectedReaction = names[indexOfReactions];

        if (selectedReaction != selectedReactionLastInteraction)
        {
            selected = false;
        }

        selectedReactionLastInteraction = selectedReaction;

        if (!selected)
        {
            if (GUILayout.Button("Checar"))
            {
                selected = true;
                reaction = reactions[names[indexOfReactions]];
            }
        }

        EditorGUILayout.Space();
        EditorGUILayout.Space();

        if (selected)
        {
            EditorGUILayout.LabelField("Nome: " + reaction.name);

            EditorGUILayout.Space();

            string bString = "";
            if (reaction.stoichiometryR2 != 0)
            {
                bString = " + " + reaction.stoichiometryR2.ToString() + " " + reaction.reagent2;
            }

            string dString = "";
            if (reaction.stoichiometrySubProduct != 0)
            {
                dString = " + " + reaction.stoichiometrySubProduct.ToString() + " " + reaction.subProduct;
            }

            EditorGUILayout.LabelField(reaction.stoichiometryR1.ToString() + " " + reaction.reagent1 + bString + " = " + reaction.stoichiometryMainProduct.ToString() + " " + reaction.mainProduct + dString);

            EditorGUILayout.Space();

            if (!deleteSelected)
            {
                if (GUILayout.Button("DELETAR!"))
                {
                    deleteSelected = true;
                }
            }
            else
            {
                EditorGUILayout.LabelField("Deletar?");
                if (GUILayout.Button("Sim"))
                {
                    Dictionary <string, ReactionClass> allReactions = ReactionsSaver.LoadReactions();

                    allReactions.Remove(names[indexOfReactions]);
//					ReactionsSaver.SaveReactions(allReactions);

                    Debug.Log("Reaçao " + names[indexOfReactions] + " Removida com sucesso!");

                    this.Close();
                }
                if (GUILayout.Button("Nao"))
                {
                    this.Close();
                }
            }
        }

        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndVertical();
    }
    /*public Mixture(Compound _product, Reagent[] _leftovers) {
     *      this.product = _product;
     *      this.leftovers = _leftovers;
     * }*/

    //Creates a Mixture, completing the reaction
    public Mixture(Compound r1, Compound r2)
    {
        ReactionClass reaction = ReactionTable.GetInstance().GetReaction(r1.Formula, r2.Formula);

        if (reaction != null)
        {
            //Makes sure the reagents are being dealt in the correct order
            if (reaction.reagent1 != r1.Formula)
            {
                string aux = reaction.reagent1;
                reaction.reagent1 = reaction.reagent2;
                reaction.reagent2 = aux;

                int auxSt = reaction.stoichiometryR1;
                reaction.stoichiometryR1 = reaction.stoichiometryR2;
                reaction.stoichiometryR2 = auxSt;
            }
            product = CompoundFactory.GetInstance().GetCompound(reaction.mainProduct);

            //Calculates the limiting reagent
            float limitingFactor1 = ((r1.Molarity / 1000) * r1.Volume) / reaction.stoichiometryR1;
            float limitingFactor2 = ((r2.Molarity / 1000) * r2.Volume) / reaction.stoichiometryR2;
            Debug.Log("limit1 =" + limitingFactor1.ToString() + "; limit2 = " + limitingFactor2.ToString());

            float trueLimiting;
            float limitingStoichiometry;
            if (limitingFactor1 <= limitingFactor2)               //Case: limiting reagent is R1
            {
                trueLimiting          = limitingFactor1;
                limitingStoichiometry = reaction.stoichiometryR1;
                //Defining the order of reagents. The first is always the limiting reagent
                leftovers.Add(r1);
                leftovers.Add(r2);
            }
            else
            {
                trueLimiting          = limitingFactor2;
                limitingStoichiometry = reaction.stoichiometryR2;

                leftovers.Add(r2);
                leftovers.Add(r1);
            }

            //Calculating mass of product
            //mass = limiting mols consumed * (stoichiometry of product / limiting stoichiometry) * product molar mass
            float productMass = ((trueLimiting * limitingStoichiometry) * reaction.stoichiometryMainProduct / limitingStoichiometry) * product.MolarMass;
            //Calculating total amount of water
            waterVolume  = (r1.RealMass - ((r1.Molarity / 1000) * r1.Volume * r1.MolarMass)) / Compound.waterDensity;
            waterVolume += (r2.RealMass - ((r2.Molarity / 1000) * r2.Volume * r2.MolarMass)) / Compound.waterDensity;
            if (reaction.subProduct == "H2O")
            {
                waterVolume += (((trueLimiting * limitingStoichiometry) * (reaction.stoichiometrySubProduct / limitingStoichiometry)) * Compound.waterMolarMass) / Compound.waterDensity;
            }


            //Setting product's molarity
            product.Purity   = 1.0f;
            product.Molarity = ((product.Purity * product.Density) / product.MolarMass);
            product.RealMass = productMass;
            this.Volume      = r1.Volume + r2.Volume;        //TODO:For the time being, the final volume is the sum of both volumes

            //Setting leftovers's values
            leftovers[0].Purity   = 1.0f;
            leftovers[0].Molarity = 0.0f;
            leftovers[0].RealMass = 0.0f;
            leftovers[0].Density  = 0.0f;
            leftovers[0].Volume   = 0.0f;

            float massOfReagent = (leftovers[1].Molarity * (leftovers[1].Volume / 1000) * leftovers[1].MolarMass)
                                  - (((trueLimiting * limitingStoichiometry) * (reaction.stoichiometryR2 / limitingStoichiometry)) * leftovers[1].MolarMass); // Mass = previous mass of reagent - mass consumed;

            leftovers[1].setValues(CompoundFactory.GetInstance().GetCupboardCompound(leftovers[1].Name));
            leftovers[1].Purity   = 1.0f;
            leftovers[1].RealMass = massOfReagent;
            if (leftovers[1].IsSolid)
            {
                //CHECK FOR PRECIPITATE
                //FOR THE TIME BEING, IT WILL BE DILUTED, AND WON'T CHANGE THE VOLUME
                leftovers[1].Volume = 0.0f;
            }
            else
            {
                //leftovers[1].Volume = leftovers[1].RealMass * leftovers[1].Density;
                leftovers[1].Volume = 0.0f;
            }

            //Setting Mixture's values
            //this.RealMass = this.GetMass ();
            //this.Volume = this.GetVolume ();
            this.Density = this.RealMass / this.Volume;
            this.Name    = reaction.name;

            //Setting product's values that depends on the mixture's final values
            product.Molarity      = (productMass / product.MolarMass) / (this.Volume * 0.001f);
            leftovers[1].Molarity = (leftovers[1].RealMass / leftovers[1].MolarMass) / (this.Volume * 0.001f);

            /*
             * TODO: VERIFICAR PRECIPITADOS
             */

            /*if ((reaction.subProduct != "H2O") || (reaction.subProduct != "")) { TODO: VERIFICAR REACTION.SUBPRODUCT E COMPONENTSAVER UTILIZANDO NAME AO INVES DE FORMULA
             *      leftovers.Add((Compound)CompoundFactory.GetInstance ().GetCompound (reaction.subProduct));
             *      leftovers[2].Purity = 1.0f;
             *      leftovers[2].RealMass = (trueLimiting * reaction.stoichiometrySubProduct / limitingStoichiometry) * leftovers[3].MolarMass;
             *      leftovers[2].Molarity = leftovers[3].RealMass / this.Volume;
             *      if(leftovers[2].IsSolid) {
             *              //CHECK FOR PRECIPITATE
             *              //FOR THE TIME BEING, IT WILL BE DILUTED, AND WON'T CHANGE THE VOLUME
             *              leftovers[2].Volume = 0.0f;
             *      }
             *      else {
             *              leftovers[2].Volume = leftovers[3].RealMass * leftovers[3].Density;
             *      }
             * }*/
        }
        else             //There was no reaction
        {
            Debug.Log("falha");
            product = null;

            leftovers.Add(r1);
            leftovers.Add(r2);

            Name = "UnknownMixture";

            float auxVolume = 0.0f;

            //Cases where the physical states are the same, the volumes do not change
            if (leftovers[0].IsSolid && (!leftovers[1].IsSolid))              //Case: l0 is solid, l1 is liquid
            {
                leftovers[0].Volume = 0.0f;
                this.IsSolid        = false;
            }
            else if (leftovers[1].IsSolid && (!leftovers[0].IsSolid))               //Case: l0 is liquid, l1 is solid
            {
                leftovers[1].Volume = 0.0f;
                this.IsSolid        = false;
            }
            else if (leftovers[0].IsSolid && leftovers[1].IsSolid)
            {
                this.IsSolid = true;
            }
            else if ((!leftovers[0].IsSolid) && (!leftovers[1].IsSolid))
            {
                this.IsSolid = false;
            }
        }
    }