public void TestRemoval() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); Mixture mixture = new Mixture(model, "Contents of vat 1", Guid.NewGuid()); MaterialCatalog cat = brs.MyMaterialCatalog; mixture.AddMaterial(cat["Acetone"].CreateMass(100, 20)); mixture.AddMaterial(cat["Potassium Sulfate"].CreateMass(100, 20)); mixture.AddMaterial(cat["Ammonia"].CreateMass(100, 20)); Debug.WriteLine("Mixture has the following stuff..."); DiagnosticAids.DumpMaterial(mixture); Assert.IsTrue(mixture.Mass.Equals(300D), "Mixture is not 300 kg"); Debug.WriteLine("Removing 100 kg of Acetone."); IMaterial matl = mixture.RemoveMaterial(cat["Acetone"], 100); DiagnosticAids.DumpMaterial(matl); Assert.IsTrue(mixture.Mass.Equals(200D), "Mixture is not 200 kg"); Debug.WriteLine("Remaining is the following mixture:"); DiagnosticAids.DumpMaterial(mixture); }
public void TestVolumetricsOfDissolvedGases() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); brs.MyMaterialCatalog.Add(new MaterialType(model, "Nitrous Oxide", Guid.NewGuid(), .001, 5, MaterialState.Gas)); brs.MyMaterialCatalog.Add(new MaterialType(model, "Pixie Breath", Guid.NewGuid(), .001, 5, MaterialState.Gas)); Mixture mixture = new Mixture(model, "Contents of vat 1", Guid.NewGuid()); MaterialCatalog cat = brs.MyMaterialCatalog; AddSubstance(ref mixture, cat["Nitrous Oxide"], 100, 20); Assert.IsTrue(mixture.Volume.Equals(100000D), "Mass is not 10000 liters"); AddSubstance(ref mixture, cat["Water"], 100, 50); Assert.IsTrue(mixture.Volume.Equals(100D), "Mass is not 100 liters"); RemoveSubstance(ref mixture, cat["Water"], 100); Assert.IsTrue(mixture.Volume.Equals(100000D), "Mass is not 10000 liters"); AddSubstance(ref mixture, cat["Pixie Breath"], 100, 20); Assert.IsTrue(mixture.Volume.Equals(200000D), "Mass is not 100 liters"); }
public void TestCombinatorics() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); Mixture mixture = new Mixture(model, "Contents of vat 1", Guid.NewGuid()); MaterialCatalog cat = brs.MyMaterialCatalog; AddSubstance(ref mixture, cat["Nitrous Acid"], 100, 20); AddSubstance(ref mixture, cat["Potassium Hydroxide"], 150, 41); AddSubstance(ref mixture, cat["Water"], 100, 100); Assert.IsTrue(mixture.Mass.Equals(350D), "Mass is not 350 kg"); Assert.IsTrue(Math.Abs(mixture.Temperature - (18150D / 350D)) < 0.00001, "Temperature is not 51.86724 C."); IMaterial matl = mixture.RemoveMaterial(cat["Nitrous Acid"]); Debug.WriteLine("Removing all avaliable " + matl.MaterialType.Name); DiagnosticAids.DumpMaterial(mixture); Assert.IsTrue(mixture.Mass.Equals(250D), "Mass is not 250 kg"); Debug.WriteLine("Adding " + matl.MaterialType.Name + " back in."); mixture.AddMaterial(matl); DiagnosticAids.DumpMaterial(mixture); Assert.IsTrue(mixture.Mass.Equals(350D), "Mass is not 350 kg"); Debug.WriteLine("Removing 50 kg of the " + matl.MaterialType.Name); matl = mixture.RemoveMaterial(matl.MaterialType, 50.0); DiagnosticAids.DumpMaterial(mixture); Assert.IsTrue(mixture.Mass.Equals(300D), "Mass is not 300 kg"); }
/// <summary> /// Loads the reaction processor in the model with sample reactions. /// </summary> /// <param name="brs">The instance of ISupportsReactions that we will load..</param> private static void LoadSampleReactions(BasicReactionSupporter brs) { Reaction r1 = new Reaction(null, "Reaction 1", Guid.NewGuid()); r1.AddReactant(brs.MyMaterialCatalog["Caustic Soda"], 0.5231); r1.AddReactant(brs.MyMaterialCatalog["Hydrochloric Acid"], 0.4769); r1.AddProduct(brs.MyMaterialCatalog["Water"], 0.2356); r1.AddProduct(brs.MyMaterialCatalog["Sodium Chloride"], 0.7644); brs.MyReactionProcessor.AddReaction(r1); Reaction r2 = new Reaction(null, "Reaction 2", Guid.NewGuid()); r2.AddReactant(brs.MyMaterialCatalog["Sulfuric Acid"], 0.533622); r2.AddReactant(brs.MyMaterialCatalog["Potassium Hydroxide"], 0.466378); r2.AddProduct(brs.MyMaterialCatalog["Water"], 0.171333); r2.AddProduct(brs.MyMaterialCatalog["Potassium Sulfate"], 0.828667); brs.MyReactionProcessor.AddReaction(r2); Reaction r3 = new Reaction(null, "Reaction 3", Guid.NewGuid()); r3.AddReactant(brs.MyMaterialCatalog["Caustic Soda"], 0.459681368); r3.AddReactant(brs.MyMaterialCatalog["Nitrous Acid"], 0.540318632); r3.AddProduct(brs.MyMaterialCatalog["Water"], 0.207047552); r3.AddProduct(brs.MyMaterialCatalog["Sodium Nitrite"], 0.792952448); brs.MyReactionProcessor.AddReaction(r3); Reaction r4 = new Reaction(null, "Reaction 4", Guid.NewGuid()); r4.AddReactant(brs.MyMaterialCatalog["Potassium Hydroxide"], 0.544102); r4.AddReactant(brs.MyMaterialCatalog["Nitrous Acid"], 0.455898); r4.AddProduct(brs.MyMaterialCatalog["Water"], 0.174698); r4.AddProduct(brs.MyMaterialCatalog["Potassium Nitrite"], 0.825302); brs.MyReactionProcessor.AddReaction(r4); }
public void TestRemovalByMassFromSubstance() { Model model = new Model("Hello, world.", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); LoadSampleCatalog(brs.MyMaterialCatalog); // Now try to remove by mass from a substance. Debug.WriteLine("\r\nWe now work on a substance."); MaterialType mt = brs.MyMaterialCatalog["Hydrochloric Acid"]; Substance s = (Substance)mt.CreateMass(100, 20); Debug.WriteLine("We have " + s); MaterialTransferSpecByMass tsbm = new MaterialTransferSpecByMass(brs.MyMaterialCatalog["Hydrochloric Acid"], 10, TimeSpan.FromMinutes(5)); Debug.WriteLine("Want to remove " + tsbm); IMaterial removee = tsbm.GetExtract(s); Debug.WriteLine("Successful in removing " + removee + ".\r\nWhat remains is "); Debug.WriteLine(s); Assert.IsTrue(Math.Abs(s.Mass - 90.00) < 0.01, "The Water part is not 90 kg"); Assert.IsTrue(tsbm.Duration.Equals(new TimeSpan(0, 0, 5, 0, 0)), "Removing 10 kg Water part did not take 5 Min"); }
public TSTestJig(double mH2O, double mNANO2) { m_brs = new BasicReactionSupporter(); Initialize(m_brs); m_mixture = new Mixture(null, "Test Mixture"); m_brs.MyReactionProcessor.Watch(m_mixture); m_mixture.AddMaterial(m_brs.MyMaterialCatalog["Water"].CreateMass(mH2O, 20)); // Add 250 kg. m_mixture.AddMaterial(m_brs.MyMaterialCatalog["Sodium Nitrite"].CreateMass(mNANO2, 20)); // Add 100 kg NaNO2. }
public void TestReactions() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); LoadSampleCatalog(brs.MyMaterialCatalog); // Define reaction Reaction r = new Reaction(null, "Reaction", Guid.NewGuid()); r.AddReactant(brs.MyMaterialCatalog["Potassium Hydroxide"], 0.544102); r.AddReactant(brs.MyMaterialCatalog["Nitrous Acid"], 0.455898); r.AddProduct(brs.MyMaterialCatalog["Water"], 0.174698); r.AddProduct(brs.MyMaterialCatalog["Potassium Nitrite"], 0.825302); brs.MyReactionProcessor.AddReaction(r); Mixture mixture = new Mixture(model, "Contents of vat 1", Guid.NewGuid()); brs.MyReactionProcessor.Watch(mixture); Debug.WriteLine("Adding two reactants to the mixture."); AddSubstance(ref mixture, brs.MyMaterialCatalog["Potassium Hydroxide"], 150, 41); AddSubstance(ref mixture, brs.MyMaterialCatalog["Nitrous Acid"], 100, 20); /* Calculation: * 0.544102 + 0.455898 = 0.174698 + 0.825302 * 100 % Ractions = min(150kg / 0.544102, 100 kg / 0.455898) = about 219.35 * --> Left over Pot Hyd = 150 kg - (219.35 * 0.544102) = about 30.65 kg * Water = 219.35 * 0.174698 = about 38.32 kg * Pot Nit = 219.35 * 0.825302 = about 181.03 kg * = 250.00 kg */ Substance water = null; Substance potassiumH = null; Substance potassiumN = null; foreach (Substance s in mixture.Constituents) { switch (s.Name) { case "Water": water = s; break; case "Potassium Hydroxide": potassiumH = s; break; case "Potassium Nitrite": potassiumN = s; break; } } Assert.IsTrue(Math.Abs(potassiumH.Mass - 30.65) < 0.01, "The Potassium Hidroxide part is not 30.65 kg"); Assert.IsTrue(Math.Abs(water.Mass - 38.32) < 0.01, "The Water part is not 38.32 kg"); Assert.IsTrue(Math.Abs(potassiumN.Mass - 181.03) < 0.01, "The Potassium Nitrite part is not 181.03 kg"); Debug.WriteLine(mixture); }
public void TestMaterialSpecifications() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); Guid city = Guid.NewGuid(); Guid dist = Guid.NewGuid(); Guid perr = Guid.NewGuid(); MaterialType waterType = (MaterialType)brs.MyMaterialCatalog["Water"]; Substance w1 = (Substance)waterType.CreateMass(100, 37); w1.SetMaterialSpec(city, 100); Debug.WriteLine("\r\nCreating 100 kg of City Water (" + city + ")."); DumpMaterialSpecs(w1); Substance w2 = (Substance)waterType.CreateMass(40, 37); w2.SetMaterialSpec(dist, 40); Debug.WriteLine("\r\nCreating 40 kg of Distilled Water (" + dist + ")."); DumpMaterialSpecs(w2); Debug.WriteLine("\r\nAdding city water to distilled water."); w1.Add(w2); DumpMaterialSpecs(w1); Debug.WriteLine("\r\nRemoving 60 kg of the blended water."); Substance w3 = w1.Remove(60); DumpMaterialSpecs(w1); DumpMaterialSpecs(w3); Debug.WriteLine("\r\nCloning the blended water."); Substance w4 = (Substance)w3.Clone(); DumpMaterialSpecs(w4); Debug.WriteLine("\r\nPurifying the cloned blended water."); w4.ConvertMaterialSpec(dist, city); Debug.WriteLine("Purified clone:"); DumpMaterialSpecs(w4); Debug.WriteLine("...and original water:"); DumpMaterialSpecs(w3); double amtDistilled = w1.GetMaterialSpec(dist); Debug.WriteLine("\r\nThere are " + amtDistilled + " kg of distilled water."); w1.ConvertMaterialSpec(dist, city); Debug.WriteLine("\r\nConverting distilled water to city water."); DumpMaterialSpecs(w1); }
private void Initialize(BasicReactionSupporter brs) { MaterialCatalog mcat = brs.MyMaterialCatalog; ReactionProcessor rp = brs.MyReactionProcessor; // Model Name Guid SpecGrav SpecHeat mcat.Add(new MaterialType(null, "Water", Guid.NewGuid(), 1.0000, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Hydrochloric Acid", Guid.NewGuid(), 1.1890, 2.5500, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Caustic Soda", Guid.NewGuid(), 2.0000, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Sodium Chloride", Guid.NewGuid(), 2.1650, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Sulfuric Acid 98%", Guid.NewGuid(), 1.8420, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Potassium Hydroxide", Guid.NewGuid(), 1.3000, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Potassium Sulfate", Guid.NewGuid(), 1.0000, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Nitrous Acid", Guid.NewGuid(), 1.0000, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Sodium Nitrite", Guid.NewGuid(), 2.3800, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Potassium Nitrite", Guid.NewGuid(), 1.9150, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Aluminum Hydroxide", Guid.NewGuid(), 1.0000, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Ammonia", Guid.NewGuid(), 1.0000, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Ammonium Hydroxide", Guid.NewGuid(), 1.0000, 4.1800, MaterialState.Liquid)); mcat.Add(new MaterialType(null, "Bromine", Guid.NewGuid(), 3.1200, 4.1800, MaterialState.Liquid)); Reaction r1 = new Reaction(null, "Reaction 1", Guid.NewGuid()); r1.AddReactant(mcat["Caustic Soda"], 0.5231); r1.AddReactant(mcat["Hydrochloric Acid"], 0.4769); r1.AddProduct(mcat["Water"], 0.2356); r1.AddProduct(mcat["Sodium Chloride"], 0.7644); rp.AddReaction(r1); Reaction r2 = new Reaction(null, "Reaction 2", Guid.NewGuid()); r2.AddReactant(mcat["Sulfuric Acid 98%"], 0.533622); r2.AddReactant(mcat["Potassium Hydroxide"], 0.466378); r2.AddProduct(mcat["Water"], 0.171333); r2.AddProduct(mcat["Potassium Sulfate"], 0.828667); rp.AddReaction(r2); Reaction r3 = new Reaction(null, "Reaction 3", Guid.NewGuid()); r3.AddReactant(mcat["Caustic Soda"], 0.459681368); r3.AddReactant(mcat["Nitrous Acid"], 0.540318632); r3.AddProduct(mcat["Water"], 0.207047552); r3.AddProduct(mcat["Sodium Nitrite"], 0.792952448); rp.AddReaction(r3); Reaction r4 = new Reaction(null, "Reaction 4", Guid.NewGuid()); r4.AddReactant(mcat["Potassium Hydroxide"], 0.544102); r4.AddReactant(mcat["Nitrous Acid"], 0.455898); r4.AddProduct(mcat["Water"], 0.174698); r4.AddProduct(mcat["Potassium Nitrite"], 0.825302); rp.AddReaction(r4); }
public void TestBadReactionDefinition1() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); MaterialType waterType = (MaterialType)brs.MyMaterialCatalog["Water"]; Reaction r1 = new Reaction(model, "Reaction 1", Guid.NewGuid()); r1.AddReactant(brs.MyMaterialCatalog["Water"], 2.0); r1.AddProduct(brs.MyMaterialCatalog["Water"], 2.0); r1.HeatOfReaction = 0; brs.MyReactionProcessor.AddReaction(r1); }
public void TestReactions3() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); MaterialType potassiumSulfateType = (MaterialType)brs.MyMaterialCatalog["Potassium Sulfate"]; MaterialType acetoneType = (MaterialType)brs.MyMaterialCatalog["Acetone"]; MaterialType hexaneType = (MaterialType)brs.MyMaterialCatalog["Hexane"]; Reaction r1 = new Reaction(model, "Reaction 1", Guid.NewGuid()); r1.AddReactant(potassiumSulfateType, 1.0); r1.AddReactant(acetoneType, 2.5); r1.AddProduct(hexaneType, 3.0); r1.AddProduct(acetoneType, 0.5); r1.HeatOfReaction = 0; brs.MyReactionProcessor.AddReaction(r1); Mixture m = new Mixture(model, "Mixture"); string results = ""; r1.ReactionGoingToHappenEvent += new ReactionGoingToHappenEvent(delegate(Reaction reaction, Mixture mixture) { results += ("\r\nBefore = " + m.ToString("F2", "F4")); }); r1.ReactionHappenedEvent += new ReactionHappenedEvent(delegate(ReactionInstance reactionInstance) { results += ("\r\nAfter = " + m.ToString("F2", "F4") + "\r\n"); }); brs.MyReactionProcessor.Watch(m); Console.WriteLine(r1.ToString()); m.AddMaterial(potassiumSulfateType.CreateMass(100, 37)); m.AddMaterial(acetoneType.CreateMass(100, 37)); Console.WriteLine(results); Assert.AreEqual(results, EXPECTED_3); }
public void TestVaporSpaceMixtureHandling() { Model model = new Model("Hello, world.", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); LoadSampleCatalog(brs.MyMaterialCatalog); Mixture mixture = new Mixture(model, "VaporTester", Guid.NewGuid()); mixture.AddMaterial(brs.MyMaterialCatalog["Methanol"].CreateMass(200, 37)); mixture.AddMaterial(brs.MyMaterialCatalog["Methylene Chloride"].CreateMass(200, 37)); mixture.AddMaterial(brs.MyMaterialCatalog["Water"].CreateMass(200, 37)); double T = 315; double V = 2.0; Console.WriteLine("Starting with " + mixture + " and estimating vapor at " + T + " degrees C in a " + V + " cubic meter space."); Mixture vs = mixture.GetVaporFor(V, T); Console.WriteLine(vs.ToString()); }
public void TestReactions2() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); MaterialType waterType = (MaterialType)brs.MyMaterialCatalog["Water"]; MaterialType ethanolType = (MaterialType)brs.MyMaterialCatalog["Ethanol"]; Reaction r1 = new Reaction(model, "Reaction 1", Guid.NewGuid()); r1.AddReactant(brs.MyMaterialCatalog["Water"], 2.0); r1.AddProduct(brs.MyMaterialCatalog["Ethanol"], 2.0); r1.HeatOfReaction = 0; brs.MyReactionProcessor.AddReaction(r1); string actualResults = ""; Mixture m = new Mixture(model, "Mixture"); r1.ReactionGoingToHappenEvent += new ReactionGoingToHappenEvent(delegate(Reaction reaction, Mixture mixture) { actualResults += ("\r\nBefore = " + m.ToString("F2", "F4")); }); r1.ReactionHappenedEvent += new ReactionHappenedEvent(delegate(ReactionInstance reactionInstance) { actualResults += ("\r\nAfter = " + m.ToString("F2", "F4") + "\r\n"); }); brs.MyReactionProcessor.Watch(m); m.AddMaterial(waterType.CreateMass(100, 37)); m.AddMaterial(waterType.CreateMass(10, 37)); Console.WriteLine(actualResults); string expected = "\r\nBefore = Mixture (37.00 deg C) of 100.0000 kg of Water\r\nAfter = Mixture (37.00 deg C) of 100.0000 kg of Ethanol\r\n\r\nBefore = Mixture (37.00 deg C) of 100.0000 kg of Ethanol and 10.0000 kg of Water\r\nAfter = Mixture (37.00 deg C) of 110.0000 kg of Ethanol\r\n"; Assert.AreEqual(expected, actualResults); }
public void TestCompositeConstruction() { BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); MaterialCatalog cat = brs.MyMaterialCatalog; Mixture mixture = Mixture.Create(cat["Acetone"].CreateMass(100, 20), cat["Potassium Sulfate"].CreateMass(100, 20), cat["Ammonia"].CreateMass(100, 20)); Debug.WriteLine("Mixture has the following stuff..."); DiagnosticAids.DumpMaterial(mixture); Assert.IsTrue(mixture.Mass.Equals(300D), "Mixture is not 300 kg"); Debug.WriteLine("Removing 100 kg of Acetone."); IMaterial matl = mixture.RemoveMaterial(cat["Acetone"], 100); DiagnosticAids.DumpMaterial(matl); Assert.IsTrue(mixture.Mass.Equals(200D), "Mixture is not 200 kg"); Debug.WriteLine("Remaining is the following mixture:"); DiagnosticAids.DumpMaterial(mixture); }
public void TestRemovalByPercentageFromSubstance() { Model model = new Model("Hello, world.", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); LoadSampleCatalog(brs.MyMaterialCatalog); Debug.WriteLine("\r\nWe now work on a substance."); MaterialType mt = brs.MyMaterialCatalog["Hydrochloric Acid"]; Substance s = (Substance)mt.CreateMass(100, 20); Debug.WriteLine("We have " + s); MaterialTransferSpecByPercentage tsbp = new MaterialTransferSpecByPercentage(brs.MyMaterialCatalog["Hydrochloric Acid"], .75, TimeSpan.FromMinutes(5)); IMaterial removee = tsbp.GetExtract(s); Debug.WriteLine("Want to remove " + tsbp); Debug.WriteLine("Successful in removing " + removee + ".\r\nWhat remains is "); Debug.WriteLine(s); Assert.IsTrue(Math.Abs(s.Mass - 25.00) < 0.01, "The Water part is not 25 kg"); Assert.IsTrue(tsbp.Duration.Hours == 6 && tsbp.Duration.Minutes == 15, "Removing 75% Water part did not take 5 Min"); }
public void TestRemovalByMassFromMixture() { Model model = new Model("Hello, world.", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); LoadSampleCatalog(brs.MyMaterialCatalog); // Define reactions Reaction r = new Reaction(null, "Reaction", Guid.NewGuid()); r.AddReactant(brs.MyMaterialCatalog["Caustic Soda"], 0.5231); r.AddReactant(brs.MyMaterialCatalog["Hydrochloric Acid"], 0.4769); r.AddProduct(brs.MyMaterialCatalog["Water"], 0.2356); r.AddProduct(brs.MyMaterialCatalog["Sodium Chloride"], 0.7644); brs.MyReactionProcessor.AddReaction(r); Mixture mixture = new Mixture(model, "Test mixture 1", Guid.NewGuid()); brs.MyReactionProcessor.Watch(mixture); // Add substances AddSubstance(ref mixture, brs.MyMaterialCatalog["Caustic Soda"], 40, 20.0); AddSubstance(ref mixture, brs.MyMaterialCatalog["Hydrochloric Acid"], 40, 20.0); mixture.Temperature = 100.0; // Definitions to retrive substances from mixture Substance water = null; Substance hydrochloricAcid = null; Substance sodiumCloride = null; foreach (Substance su in mixture.Constituents) { switch (su.Name) { case "Water": water = su; break; case "Hydrochloric Acid": hydrochloricAcid = su; break; case "Sodium Chloride": sodiumCloride = su; break; } } _Debug.Assert(Math.Abs(hydrochloricAcid.Mass - 3.53) < 0.01, "The Hydrochloric Acid part is not 3.53 kg"); _Debug.Assert(Math.Abs(water.Mass - 18.01) < 0.01, "The Water part is not 18.01 kg"); _Debug.Assert(Math.Abs(sodiumCloride.Mass - 58.45) < 0.01, "The Sodium Cloride part is not 58.45 kg"); // Remove 10 kg of water MaterialTransferSpecByMass tsbm = new MaterialTransferSpecByMass(brs.MyMaterialCatalog["Water"], 10, TimeSpan.FromMinutes(5)); _Debug.WriteLine("Want to remove " + tsbm); IMaterial removee = tsbm.GetExtract(mixture); _Debug.WriteLine("Successful in removing " + removee + ".\r\nWhat remains is "); _Debug.WriteLine(mixture); foreach (Substance su in mixture.Constituents) { switch (su.Name) { case "Water": water = su; break; case "Hydrochloric Acid": hydrochloricAcid = su; break; case "Sodium Chloride": sodiumCloride = su; break; } } _Debug.Assert(Math.Abs(hydrochloricAcid.Mass - 3.53) < 0.01, "The Hydrochloric Acid part is not 3.53 kg"); _Debug.Assert(Math.Abs(water.Mass - 8.01) < 0.01, "The Water part is not 8.01 kg"); _Debug.Assert(Math.Abs(sodiumCloride.Mass - 58.45) < 0.01, "The Sodium Chloride part is not 58.45 kg"); _Debug.Assert(tsbm.Duration.Equals(new TimeSpan(0, 0, 5, 0, 0)), "Removing 10 kg Water part did not take 5 Min"); }
public void TestReactionBasics() { BasicReactionSupporter brs = new BasicReactionSupporter(); Initialize(brs); Mixture mixture = new Mixture(null, "test mixture"); mixture.OnReactionHappened += new ReactionHappenedEvent(mixture_OnReactionHappened); brs.MyReactionProcessor.Watch(mixture); mixture.AddMaterial(brs.MyMaterialCatalog["Hydrochloric Acid"].CreateMass(10, 20)); mixture.AddMaterial(brs.MyMaterialCatalog["Caustic Soda"].CreateMass(12, 44)); foreach (IMaterial constituent in mixture.Constituents) { _Debug.WriteLine(constituent.ToString()); } /* Calculation mass: * 0.5231 + 0.4769 = 0.2356 + 0.7644 * 100 % Ractions = min(10 kg / 0.4769, 12 kg / 0.5231) = about 20.97 * --> Left over Caustic Soda = 12 kg - (20.97 * 0.5231)= about 1.03 kg * Water = 20.97 * 0.2356 = about 4.94 kg * Sodium Cloride = 20.97 * 0.7644 = about 16.03 kg * = 22.00 kg * Calculation temperature: * Hydrochloric Acid specific heat = 4.18 * Caustic Soda specific heat = 2.55 * Heat of Hydrochloric Acid is 10 kg * 2.55 * 20 dC = about 510.00 dC * Heat of Caustic Soda is 12 kg * 4.18 * 44 dC = about 2207.04 dC * Subtotal = about 2717.04 dC * Divide by 10 * 2.55 + 12 * 4.18 = 75.66 * Mixture temperature is 2717.04 / 75.66 = about 35.91 dC */ Substance water = null; Substance sodium = null; Substance soda = null; foreach (Substance s in mixture.Constituents) { switch (s.Name) { case "Water": water = s; break; case "Sodium Chloride": sodium = s; break; case "Caustic Soda": soda = s; break; } } _Debug.Assert(Math.Abs(soda.Mass - 1.03) < 0.01, "The Caustic Soda part is not 2.88 kg"); _Debug.Assert(Math.Abs(water.Mass - 4.94) < 0.01, "The Water part is not 4.5 kg"); _Debug.Assert(Math.Abs(sodium.Mass - 16.03) < 0.01, "The Sodium Cloride part is not 14.61 kg"); Console.WriteLine(mixture.Temperature); _Debug.Assert(Math.Abs(mixture.Temperature - 35.91) < 0.01, "The temperature is not 33.09 degrees C"); }
public void TestCatalyticReactionBasics() { BasicReactionSupporter brs = new BasicReactionSupporter(); Initialize(brs); int nReactions = 0; Mixture mixture = new Mixture(null, "test mixture"); mixture.OnReactionHappened += new ReactionHappenedEvent(mixture_OnReactionHappened); mixture.OnReactionHappened += new ReactionHappenedEvent(delegate(ReactionInstance r) { nReactions++; }); brs.MyReactionProcessor.Watch(mixture); mixture.AddMaterial(brs.MyMaterialCatalog["Hydrogen"].CreateMass(10, 20)); mixture.AddMaterial(brs.MyMaterialCatalog["Oil"].CreateMass(12, 44)); foreach (IMaterial constituent in mixture.Constituents) { _Debug.WriteLine(constituent.ToString()); } mixture.AddMaterial(brs.MyMaterialCatalog["Palladium"].CreateMass(1, 44)); foreach (IMaterial constituent in mixture.Constituents) { _Debug.WriteLine(constituent.ToString()); } _Debug.Assert(nReactions == 1, String.Format("Reaction occurred {0} times, but should have happened once.", nReactions)); if (nReactions != 1) { Console.WriteLine("Test failed. Catalytic reaction happened {0} times, but should only have happened once.", nReactions); if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } } // Now check that a reaction without the catalyst does not happen. nReactions = 0; mixture = new Mixture(null, "test mixture"); mixture.OnReactionHappened += new ReactionHappenedEvent(mixture_OnReactionHappened); mixture.OnReactionHappened += new ReactionHappenedEvent(delegate(ReactionInstance r) { nReactions++; }); brs.MyReactionProcessor.Watch(mixture); mixture.AddMaterial(brs.MyMaterialCatalog["Hydrogen"].CreateMass(10, 20)); mixture.AddMaterial(brs.MyMaterialCatalog["Oil"].CreateMass(12, 44)); foreach (IMaterial constituent in mixture.Constituents) { _Debug.WriteLine(constituent.ToString()); } foreach (IMaterial constituent in mixture.Constituents) { _Debug.WriteLine(constituent.ToString()); } _Debug.Assert(nReactions == 0, String.Format("Reaction occurred {0} times, but should not have happened.", nReactions)); if (nReactions != 0) { Console.WriteLine("Test failed. Catalytic reaction happened {0} times, but should not have happened.", nReactions); if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } } }
/// <summary> /// Initializes the specified model by populating the material catalog and reaction /// processor with sample material types and reactions. /// </summary> /// <param name="brs">The instance of ISupportsReactions that we will load..</param> public static void InitializeForTesting(BasicReactionSupporter brs) { LoadSampleCatalog(brs.MyMaterialCatalog); LoadSampleReactions(brs); }
public void TestSecondaryReactions() { Model model = new Model("Hello, world.", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); LoadSampleCatalog(brs.MyMaterialCatalog); // Add more substances to catalog brs.MyMaterialCatalog.Add(new MaterialType(model, "Tuberus Magnificus", Guid.NewGuid(), 1.02, 4.9, MaterialState.Solid, 22.5)); brs.MyMaterialCatalog.Add(new MaterialType(model, "French Fried Potatoes", Guid.NewGuid(), 1.02, 5.1, MaterialState.Liquid, 22.8)); // Define reactions Reaction r1 = new Reaction(null, "Reaction 1", Guid.NewGuid()); r1.AddReactant(brs.MyMaterialCatalog["Caustic Soda"], 0.5231); r1.AddReactant(brs.MyMaterialCatalog["Hydrochloric Acid"], 0.4769); r1.AddProduct(brs.MyMaterialCatalog["Water"], 0.2356); r1.AddProduct(brs.MyMaterialCatalog["Sodium Chloride"], 0.7644); brs.MyReactionProcessor.AddReaction(r1); Reaction r2 = new Reaction(null, "Make French Fries", Guid.NewGuid()); r2.AddReactant(brs.MyMaterialCatalog["Sodium Chloride"], 0.1035); r2.AddReactant(brs.MyMaterialCatalog["Tuberus Magnificus"], 0.8965); r2.AddProduct(brs.MyMaterialCatalog["French Fried Potatoes"], 1.000); brs.MyReactionProcessor.AddReaction(r2); Mixture mixture = new Mixture(model, "Contents of vat 1", Guid.NewGuid()); brs.MyReactionProcessor.Watch(mixture); AddSubstance(ref mixture, brs.MyMaterialCatalog["Caustic Soda"], 40, 20.0); AddSubstance(ref mixture, brs.MyMaterialCatalog["Hydrochloric Acid"], 40, 20.0); AddSubstance(ref mixture, brs.MyMaterialCatalog["Tuberus Magnificus"], 40, 20.0); mixture.Temperature = 100.0; Substance water = null; Substance hydrochloricAcid = null; Substance sodiumCloride = null; Substance ff = null; foreach (Substance s in mixture.Constituents) { switch (s.Name) { case "Water": water = s; break; case "Hydrochloric Acid": hydrochloricAcid = s; break; case "Sodium Chloride": sodiumCloride = s; break; case "French Fried Potatoes": ff = s; break; } } Assert.IsTrue(Math.Abs(hydrochloricAcid.Mass - 3.53) < 0.01, "The Hydrochloric Acid part is not 3.53 kg"); Assert.IsTrue(Math.Abs(water.Mass - 18.01) < 0.01, "The Water part is not 18.01 kg"); Assert.IsTrue(Math.Abs(sodiumCloride.Mass - 53.83) < 0.01, "The Sodium Cloride part is not 53.83 kg"); Assert.IsTrue(Math.Abs(ff.Mass - 44.62) < 0.01, "The French Fries part is not 44.62 kg"); Debug.WriteLine(mixture); }
public void TestMaterialTransferrer() { #region Create and initialize the model Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); #endregion Create and initialize the model #region Create handles to material types MaterialType potassiumSulfateType = (MaterialType)brs.MyMaterialCatalog["Potassium Sulfate"]; MaterialType acetoneType = (MaterialType)brs.MyMaterialCatalog["Acetone"]; MaterialType hexaneType = (MaterialType)brs.MyMaterialCatalog["Hexane"]; MaterialType waterType = (MaterialType)brs.MyMaterialCatalog["Water"]; #endregion Create handles to material types #region Create the source mixture Mixture from = new Mixture("From"); from.AddMaterial(potassiumSulfateType.CreateMass(100, 37)); from.AddMaterial(acetoneType.CreateMass(100, 37)); from.AddMaterial(hexaneType.CreateMass(100, 37)); #endregion Create the source mixture #region Create the destination mixture Mixture to = new Mixture("To"); to.AddMaterial(waterType.CreateMass(55, 50)); #endregion Create the destination mixture #region Create the mixture that will act as the exemplar Mixture what = new Mixture("What"); what.AddMaterial(potassiumSulfateType.CreateMass(25, 37)); what.AddMaterial(acetoneType.CreateMass(35, 37)); what.AddMaterial(hexaneType.CreateMass(45, 37)); #endregion Create the mixture that will act as the exemplar TimeSpan duration = TimeSpan.FromHours(2.0); MaterialTransferrer mxfr = new MaterialTransferrer(model, ref from, ref to, what, duration); // Create the start event for the transfer. DateTime startAt = new DateTime(2009, 2, 23, 3, 0, 0); model.Executive.RequestEvent(delegate(IExecutive exec, object userData) { mxfr.Start(); }, startAt, 0.0, null); // Create a pause-til-start event. model.Executive.RequestEvent(delegate(IExecutive exec, object userData) { Console.WriteLine("Waiting for start."); mxfr.BlockTilStart(); Console.WriteLine("Start has occurred!"); }, startAt - TimeSpan.FromHours(2.0), 0.0, null, ExecEventType.Detachable); // Create a pause-til-done event. model.Executive.RequestEvent(delegate(IExecutive exec, object userData) { Console.WriteLine("Waiting for finish."); mxfr.BlockTilDone(); Console.WriteLine("Finish has occurred!"); }, startAt - TimeSpan.FromHours(2.0), 0.0, null, ExecEventType.Detachable); // Create a series of (unrelated) events that will force updates during the transfer. DateTime update = startAt; while (update <= (startAt + duration)) { model.Executive.RequestEvent(delegate(IExecutive exec, object userData) { Console.WriteLine("{2} : From = {0}\r\nTo = {1}", from, to, exec.Now); }, update, 0.0, null); update += TimeSpan.FromMinutes(20.0); } model.Start(); }
public void TestChangeNotifications() { Model model = new Model("Test Model", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); InitializeForTesting(brs); MaterialType waterType = (MaterialType)brs.MyMaterialCatalog["Water"]; string results = string.Empty; Substance w1 = (Substance)waterType.CreateMass(100, 37); w1.MaterialChanged += new MaterialChangeListener(delegate(IMaterial material, MaterialChangeType type) { results += string.Format("{0} changed in {1}\r\n", material, type); }); w1.Temperature = 85.0; w1.Add((Substance)waterType.CreateMass(100, 37)); Assert.IsTrue(results.Equals(RESULT1)); results = string.Empty; w1.SuspendChangeEvents(); w1.Temperature = 95.0; w1.Add((Substance)waterType.CreateMass(100, 37)); w1.ResumeChangeEvents(false); Assert.IsTrue(results.Equals(string.Empty)); results = string.Empty; w1.SuspendChangeEvents(); w1.Temperature = 95.0; w1.Add((Substance)waterType.CreateMass(100, 37)); w1.ResumeChangeEvents(true); Assert.IsTrue(results.Equals(RESULT2)); // NOW, SAME TEST, BUT ON A MIXTURE INSTEAD. MaterialType acetoneType = (MaterialType)brs.MyMaterialCatalog["Acetone"]; Mixture m1 = new Mixture("My Goo"); m1.MaterialChanged += new MaterialChangeListener(delegate(IMaterial material, MaterialChangeType type) { results += string.Format("{0} changed in {1}\r\n", material, type); }); results = string.Empty; m1.AddMaterial((Substance)waterType.CreateMass(100, 37)); m1.AddMaterial((Substance)acetoneType.CreateMass(100, 45)); Assert.IsTrue(results.Equals(RESULT3)); results = string.Empty; m1.SuspendChangeEvents(); m1.AddMaterial((Substance)waterType.CreateMass(100, 99)); m1.AddMaterial((Substance)acetoneType.CreateMass(100, 99)); Assert.IsTrue(results.Equals(string.Empty)); m1.ResumeChangeEvents(false); Assert.IsTrue(results.Equals(string.Empty)); results = string.Empty; m1.SuspendChangeEvents(); m1.AddMaterial((Substance)waterType.CreateMass(100, 99)); m1.AddMaterial((Substance)acetoneType.CreateMass(100, 99)); Assert.IsTrue(results.Equals(string.Empty)); m1.ResumeChangeEvents(true); Assert.IsTrue(results.Equals(RESULT4)); }
public void TestRP_CombineAPI() { // AEL: I am not sure if we should test more here or if it is sufficient to check if reactions happened or not? BasicReactionSupporter brs = new BasicReactionSupporter(); Initialize(brs); IMaterial m1 = brs.MyMaterialCatalog["Hydrochloric Acid"].CreateMass(10, 20); IMaterial m2 = brs.MyMaterialCatalog["Caustic Soda"].CreateMass(12, 44); IMaterial m3 = brs.MyMaterialCatalog["Nitrous Acid"].CreateMass(2, 60); IMaterial resultA; ArrayList observedReactions, observedReactionInstances; _Debug.WriteLine("Part A : Reaction should happen..."); bool reactionAHappened = brs.MyReactionProcessor.CombineMaterials(new IMaterial[] { m1.Clone(), m2.Clone(), m3.Clone() }, out resultA, out observedReactions, out observedReactionInstances); //bool reactionHappened = brs.MyReactionProcessor.CombineMaterials(new IMaterial[]{m1,m2,m3},out result, out observedReactions, out observedReactionInstances); _Debug.WriteLine("Engine says that reactions " + (reactionAHappened?"happened.":"didn't happen.")); _Debug.WriteLine("\r\nObserved Reactions"); foreach (Reaction r in observedReactions) { _Debug.WriteLine(r.ToString()); } _Debug.WriteLine("\r\nObserved Reaction Instances"); foreach (ReactionInstance ri in observedReactionInstances) { _Debug.WriteLine(ri.ToString()); } _Debug.WriteLine("\r\nMixture"); _Debug.WriteLine(resultA.ToString()); IMaterial resultB; _Debug.WriteLine("Part B : Reaction should not happen..."); bool reactionBHappened = brs.MyReactionProcessor.CombineMaterials(new IMaterial[] { m1.Clone(), m3.Clone() }, out resultB, out observedReactions, out observedReactionInstances); _Debug.WriteLine("Engine says that reactions " + (reactionBHappened?"happened.":"didn't happen.")); _Debug.WriteLine("\r\nObserved Reactions"); foreach (Reaction r in observedReactions) { _Debug.WriteLine(r.ToString()); } _Debug.WriteLine("\r\nObserved Reaction Instances"); foreach (ReactionInstance ri in observedReactionInstances) { _Debug.WriteLine(ri.ToString()); } _Debug.WriteLine("\r\nMixture"); _Debug.WriteLine(resultB.ToString()); _Debug.Assert(!reactionBHappened, "Reaction B should not have happened"); }
public void TestRemovalByPercentageFromMixture() { Model model = new Model("Hello, world.", Guid.NewGuid()); BasicReactionSupporter brs = new BasicReactionSupporter(); LoadSampleCatalog(brs.MyMaterialCatalog); // Define reactions Reaction r = new Reaction(null, "Reaction", Guid.NewGuid()); r.AddReactant(brs.MyMaterialCatalog["Caustic Soda"], 0.5231); r.AddReactant(brs.MyMaterialCatalog["Hydrochloric Acid"], 0.4769); r.AddProduct(brs.MyMaterialCatalog["Water"], 0.2356); r.AddProduct(brs.MyMaterialCatalog["Sodium Chloride"], 0.7644); brs.MyReactionProcessor.AddReaction(r); Mixture mixture = new Mixture(model, "Test mixture 1", Guid.NewGuid()); brs.MyReactionProcessor.Watch(mixture); // Add substances AddSubstance(ref mixture, brs.MyMaterialCatalog["Caustic Soda"], 40, 20.0); AddSubstance(ref mixture, brs.MyMaterialCatalog["Hydrochloric Acid"], 40, 20.0); mixture.Temperature = 100.0; // Definitions to retrive substances from mixture Substance water = null; Substance hydrochloricAcid = null; Substance sodiumCloride = null; foreach (Substance su in mixture.Constituents) { switch (su.Name) { case "Water": water = su; break; case "Hydrochloric Acid": hydrochloricAcid = su; break; case "Sodium Chloride": sodiumCloride = su; break; } } Assert.IsTrue(Math.Abs(hydrochloricAcid.Mass - 3.53) < 0.01, "The Hydrochloric Acid part is not 3.53 kg"); Assert.IsTrue(Math.Abs(water.Mass - 18.01) < 0.01, "The Water part is not 18.01 kg"); Assert.IsTrue(Math.Abs(sodiumCloride.Mass - 58.45) < 0.01, "The Sodium Cloride part is not 58.45 kg"); // Duration for removing mass given is per 1 kg MaterialTransferSpecByPercentage tsbp = new MaterialTransferSpecByPercentage(brs.MyMaterialCatalog["Water"], .5, TimeSpan.FromMinutes(5)); IMaterial removee = tsbp.GetExtract(mixture); Debug.WriteLine("Want to remove " + tsbp); Debug.WriteLine("Successful in removing " + removee + ".\r\nWhat remains is "); Debug.WriteLine(mixture); // Now try to remove by mass from a substance. foreach (Substance su in mixture.Constituents) { switch (su.Name) { case "Water": water = su; break; case "Hydrochloric Acid": hydrochloricAcid = su; break; case "Sodium Chloride": sodiumCloride = su; break; } } Assert.IsTrue(Math.Abs(hydrochloricAcid.Mass - 3.53) < 0.01, "The Hydrochloric Acid part is not 3.53 kg"); Assert.IsTrue(Math.Abs(water.Mass - 9.00) < 0.01, "The Water part is not 9.00 kg"); Assert.IsTrue(Math.Abs(sodiumCloride.Mass - 58.45) < 0.01, "The Sodium Cloride part is not 58.45 kg"); Assert.IsTrue(tsbp.Duration.Minutes == 45, "Removing 50% Water part did not take 5 Min"); // there are also a few seconds and miliseconds }