public void MegaTest() { EmissionsService es = EmissionsService.Instance; XmlDocument doc = new XmlDocument(); doc.Load(_testDataFile); ArrayList avgErrors = new ArrayList(); foreach (XmlNode testNode in doc.SelectNodes("/EmissionTests/Test")) { Console.WriteLine("Running Test Scenario : " + testNode.Attributes["num"].InnerText); bool printedHeader = false; XmlNode stimNode = testNode.SelectSingleNode("Stimulus"); XmlNode responseNode = testNode.SelectSingleNode("Response"); foreach (XmlNode modelResultNode in responseNode.SelectNodes("Model")) { string model = modelResultNode.Attributes["name"].InnerText; //if ( "No Emissions".Equals(model) ) System.Diagnostics.Debugger.Break(); Tester tester = new Tester(m_brs, m_lateBound); foreach (XmlNode materialNode in stimNode.SelectNodes("Material")) { string matlName = materialNode.Attributes["name"].InnerText; string strGallons = materialNode.Attributes["quantity"].InnerText; tester.AddGallons(matlName, double.Parse(strGallons)); } Hashtable parameters = new Hashtable(); foreach (XmlNode paramNode in stimNode.SelectNodes("Parameter")) { double valu = double.Parse(paramNode.Attributes["value"].InnerText); string name = paramNode.Attributes["name"].InnerText; #region Convert to appropriate units and store data in parameter hashtable switch (name) { case ("controlTemperature"): { parameters.Add(PN.ControlTemperature_K, valu + K.CELSIUS_TO_KELVIN); break; } case ("finalTankTemperature"): { parameters.Add(PN.FinalTemperature_K, valu + K.CELSIUS_TO_KELVIN); break; } case ("initialTankTemperature"): { parameters.Add(PN.InitialTemperature_K, valu + K.CELSIUS_TO_KELVIN); tester.SetInitialTemperature(valu); break; } case ("fillVolumeInGallons"): { //parameters.Add("FillVolume",valu*K.litersPerGallon); MaterialType mt = m_brs.MyMaterialCatalog["Unknown"]; Mixture addend = new Mixture("Addend", Guid.NewGuid()); double desiredVolume = valu * K.litersPerGallon; addend.AddMaterial(mt.CreateMass(desiredVolume, 35.0)); // Only works because we know its density is 1.0 ... parameters.Add(PN.MaterialToAdd, addend); break; } case ("freeSpaceInGallons"): { //parameters.Add("FreeSpace",valu*K.litersPerGallon); // We are expecting "VesselVolume" instead. double vesselVolume = ((valu * K.litersPerGallon) + tester.CurrentMixture.Volume) * .001 /*m^3/liter*/; parameters.Add(PN.VesselVolume_M3, vesselVolume); break; } case ("initialPressureIn_mmHg"): { parameters.Add(PN.InitialPressure_P, valu * K.pascalsPer_mmHg); break; } case ("finalPressureIn_mmHg"): { parameters.Add(PN.FinalPressure_P, valu * K.pascalsPer_mmHg); break; } case ("batchCycleTimeForSweepInHours"): { parameters.Add(PN.GasSweepDuration_Min, valu * 60.0); break; } case ("gasSweepRateInSCFM"): { parameters.Add(PN.GasSweepRate_M3PerMin, valu / K.cubicFtPerCubicMeter); break; } case ("numberOfMolesOfGasEvolved"): { parameters.Add(PN.MolesOfGasEvolved, valu); break; } case ("leakRateOfAirIntoSystem"): { //parameters.Add("VacuumOpLeakRateOfAir",valu*K.kgPerPound); parameters.Add(PN.AirLeakRate_KgPerMin, valu * K.kgPerPound); break; } case ("batchCycleTimeForVacuumOps"): { //parameters.Add("VacuumOpCycleTime",valu); parameters.Add(PN.AirLeakDuration_Min, valu); break; } case ("systemPressureForVacuumOpsIn_mmHg"): { parameters.Add(PN.VacuumSystemPressure_P, valu * K.pascalsPer_mmHg); break; } case ("systemPressure"): { parameters.Add(PN.SystemPressure_P, valu * K.pascalsPer_mmHg); break; } default: { Console.WriteLine("Unknown parameter " + valu + " encountered under key " + name); break; } } #endregion } if (!printedHeader) { foreach (DictionaryEntry de in parameters) { Console.WriteLine(" > " + de.Key + " = " + de.Value); } Console.WriteLine("Initial : " + tester.InitialMixture.ToString()); printedHeader = true; } //if ( "Vacuum Distillation".Equals(model) ) System.Diagnostics.Debugger.Break(); Console.WriteLine("\r\n\r\nTesting " + model); Console.WriteLine(" Initial --- Emissions --- Computation"); Console.WriteLine(" Mixture Expected Actual Error"); // Console.WriteLine("Acetic Acid.............................0.0386 0.0387 0.0386 0.2%"); ArrayList originalSubstances = new ArrayList(); foreach (Substance s in tester.InitialMixture.Constituents) { originalSubstances.Add(s.MaterialType); } Mixture final, emission; es.ProcessEmissions(tester.InitialMixture, out final, out emission, false, model, parameters); //Console.WriteLine("Emission : " + emission.ToString()); #region analyze resulting mixture to ensure that it is "Close Enough" Hashtable observed = new Hashtable(); foreach (MaterialType mt in originalSubstances) { observed.Add(mt.Name, emission.ContainedMassOf(mt)); } Hashtable expected = new Hashtable(); foreach (XmlNode goldStd in modelResultNode.SelectNodes("Material")) { expected.Add(goldStd.Attributes["name"].InnerText, double.Parse(goldStd.Attributes["kilograms"].InnerText)); } ArrayList alObserved = new ArrayList(observed.Keys); alObserved.Sort(); double maxErr = 0.0; double avgErr = 0.0; ArrayList errors = new ArrayList(); foreach (string substance in alObserved) { MaterialType mt = m_brs.MyMaterialCatalog[substance]; double expectedKg = (double)expected[substance]; double observedKg = (double)observed[substance]; double err = Math.Abs((expectedKg - observedKg) / expectedKg) * 100.0; if (double.IsNaN(err) && expectedKg == 0.0 && observedKg == 0.0) { err = 0.0; } if (err > maxErr) { maxErr = err; } avgErr += err; errors.Add(err); System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(substance); for (int i = 40; i > substance.Length; i--) { sb.Append("."); } string initialKgStr = string.Format("{0:F4}", tester.InitialMixture.ContainedMassOf(mt)); sb.Append(initialKgStr); for (int i = 10; i > initialKgStr.Length; i--) { sb.Append(" "); } string expectedKgStr = string.Format("{0:F4}", expectedKg); sb.Append(expectedKgStr); for (int i = 10; i > expectedKgStr.Length; i--) { sb.Append(" "); } string observedKgStr = string.Format("{0:F4}", observedKg); sb.Append(observedKgStr); for (int i = 10; i > observedKgStr.Length; i--) { sb.Append(" "); } sb.Append(string.Format("{0:F1}%", err)); Console.WriteLine(sb.ToString()); } avgErr /= alObserved.Count; double stdevErr = 0.0; foreach (double err in errors) { stdevErr += ((err - avgErr) * (err - avgErr)); } stdevErr /= alObserved.Count; stdevErr = Math.Sqrt(stdevErr); string msg = string.Format("Max Error was {0:F1}%, Avg Error was = {1:F1}%, StDev Error was {2:F5}%.", maxErr, avgErr, stdevErr); Console.WriteLine(msg); avgErrors.Add(avgErr); } #endregion } double avgAvgErr = 0.0; double maxAvgErr = 0.0; foreach (double avgErr in avgErrors) { avgAvgErr += avgErr; if (avgErr > maxAvgErr) { maxAvgErr = avgErr; } } avgAvgErr /= avgErrors.Count; Console.WriteLine("\r\n\r\nFinal Report: After running " + avgErrors.Count + " models over " + (avgErrors.Count / 8) + " random scenarios,\r\nthe average avgErr for a scenario was {0:F2}%, and the maximum avgErr is {1:F2}%.", avgAvgErr, maxAvgErr); }
public void TestMultiMaterialFill() { Tester tester = new Tester(m_brs, m_lateBound); XmlDocument doc = new XmlDocument(); doc.LoadXml( @"<Initial><Material name=""Acetic Acid"" quantity=""0.667469348137951"" /> <Material name=""Acetone"" quantity=""0.701595088793707"" /> <Material name=""Acetonitrile"" quantity=""7.74765135149828"" /> <Material name=""Avermectin Oils1"" quantity=""5.11139268759237"" /> <Material name=""Butanol"" quantity=""7.97490558492714"" /> <Material name=""n-Butyl Acetate"" quantity=""8.27308291023275"" /> <Material name=""t-Butyldimethyl Silanol1"" quantity=""1.65958795308116"" /> <Material name=""Cyclohexane"" quantity=""7.36130623489679"" /> <Material name=""Dimethylaminopyridine1"" quantity=""2.6021636475819"" /> <Material name=""Dimethylformamide"" quantity=""5.06004851081411"" /> <Material name=""Dimethylsulfate"" quantity=""2.30273722312541"" /> <Material name=""Dimethylsulfide"" quantity=""3.87140443263175"" /> <Material name=""Dimethylsulfoxide"" quantity=""2.15980615101746"" /> <Material name=""Dodecylbenzylsulfonic Acid1"" quantity=""0.208108848989061"" /> <Material name=""Ethanol"" quantity=""1.34833602297508"" /> <Material name=""Ethyl Acetate"" quantity=""7.40831733560577"" /> <Material name=""Ethyl Ether"" quantity=""1.95091067904183"" /> <Material name=""n-Ethyl Pyrolidone (NEP)"" quantity=""8.87039565894306"" /> <Material name=""Ethyl-7-Chloro-2-Oxoheptanate1"" quantity=""8.61100613540551"" /> <Material name=""Heptane"" quantity=""3.58865063804605"" /> <Material name=""Hexane"" quantity=""1.03702260229598"" /> <Material name=""Isoamyl Alcohol"" quantity=""5.29583051581673"" /> <Material name=""Isopropanol"" quantity=""3.58755016866492"" /> <Material name=""Isopropyl Acetate"" quantity=""7.07505481181436"" /> <Material name=""Methane Sulfonic Acid"" quantity=""5.80474831899849"" /> <Material name=""Methanol"" quantity=""6.20548322620126"" /> <Material name=""Methyl Acetate"" quantity=""7.32496631672837"" /> <Material name=""Methyl Ethyl Ketone"" quantity=""7.47882739989032"" /> <Material name=""Methylene Chloride"" quantity=""3.96251060718787"" /> <Material name=""Phenyl Phosphate1"" quantity=""2.45257239902978"" /> <Material name=""n-Propanol"" quantity=""6.86345072317098"" /> <Material name=""Tetrahydrofuran"" quantity=""2.10546193742448"" /> <Material name=""Toluene"" quantity=""5.17537586631969"" /> <Material name=""Triethylamine"" quantity=""4.85390965121515"" /> <Material name=""Hydrazine"" quantity=""2.52805452911558"" /> <Material name=""Chloroform"" quantity=""7.70778849148554"" /> <Material name=""Pyrrolidine"" quantity=""0.0257893931240725"" /> <Material name=""MTBE"" quantity=""4.83912114279304"" /> <Material name=""Water"" quantity=""4.4795790335534"" /> </Initial>"); foreach (XmlNode node in doc.SelectNodes("/Initial/Material")) { tester.AddGallons(node.Attributes["name"].InnerText, double.Parse(node.Attributes["quantity"].InnerText)); } // Create a mixture with a 200,000 gallon volume. MaterialType mt = m_brs.MyMaterialCatalog["Unknown"]; Mixture addend = new Mixture("Addend", Guid.NewGuid()); double desiredVolume = 53990 * K.litersPerGallon; addend.AddMaterial(mt.CreateMass(desiredVolume, 35.0)); //Console.WriteLine("There are " + (addend.Volume/K.litersPerGallon) + " gallons of " + mt.Name + " being added."); double controlTemperature = 19.7292085 + K.CELSIUS_TO_KELVIN; tester.DoFill(addend, controlTemperature); Mixture emission = tester.LastEmission; Mixture resultant = tester.CurrentMixture; string knownGood = "Mixture (40.00 deg C) of 8.8577 kg of Chloroform, 8.8237 kg of Methylene Chloride, 6.6420 kg of Dimethylsulfide, 5.6593 kg of Methyl Acetate, 2.9698 kg of Ethyl Ether, 2.4233 kg of Ethyl Acetate, 2.3301 kg of Methanol, 2.1779 kg of Cyclohexane, 2.1184 kg of Acetonitrile, 2.0769 kg of Methyl Ethyl Ketone, 1.9382 kg of MTBE, 1.4243 kg of Isopropyl Acetate, 1.3159 kg of Tetrahydrofuran, 0.6678 kg of Triethylamine, 0.5016 kg of Acetone, 0.4785 kg of Toluene, 0.4370 kg of Isopropanol, 0.4052 kg of Hexane, 0.3934 kg of n-Propanol, 0.3838 kg of Water, 0.3808 kg of Heptane, 0.2876 kg of n-Butyl Acetate, 0.2262 kg of Ethanol, 0.1583 kg of Butanol, 0.1309 kg of Hydrazine, 0.0645 kg of Dimethylformamide, 0.0475 kg of Isoamyl Alcohol, 0.0386 kg of Acetic Acid, 0.0087 kg of Ethyl-7-Chloro-2-Oxoheptanate1, 0.0055 kg of Dimethylsulfoxide, 0.0051 kg of Avermectin Oils1, 0.0051 kg of Pyrrolidine, 0.0050 kg of n-Ethyl Pyrolidone (NEP), 0.0047 kg of Dimethylsulfate, 0.0026 kg of Dimethylaminopyridine1, 0.0025 kg of Phenyl Phosphate1, 0.0017 kg of t-Butyldimethyl Silanol1, 0.0002 kg of Dodecylbenzylsulfonic Acid1 and 0.0000 kg of Methane Sulfonic Acid"; EvaluateResults(knownGood, "Fill", emission); }