Example #1
0
        public DataTable Render()
        {
            _streamTableData = new DataTable();

            if (Streams.Count == 0)
            {
                return(_streamTableData);
            }


            // _streamTableData.Columns.Add("Number", typeof(Int32));
            _streamTableData.Columns.Add("Property");
            _streamTableData.Columns.Add("Unit");

            var system = Streams.First().System;

            foreach (var stream in Streams.Distinct())
            {
                var col = _streamTableData.Columns.Add(stream.Name);
            }

            List <string> properties = new List <string> {
                "Temperature", "Pressure", "Enthalpy", "Vapour Fraction"
            };
            //List<string> properties = new List<string> { "Name", "From Unit", "From Port", "To Unit", "To Port", "", "Temperature", "Pressure", "Enthalpy", "Vapour Fraction" };
            List <string> propertyUnits = new List <string> {
                system.VariableFactory.Output.UnitDictionary[UnitsOfMeasure.PhysicalDimension.Temperature].Symbol,
                system.VariableFactory.Output.UnitDictionary[UnitsOfMeasure.PhysicalDimension.Pressure].Symbol,
                system.VariableFactory.Output.UnitDictionary[UnitsOfMeasure.PhysicalDimension.SpecificMolarEnthalpy].Symbol, ""
            };

            int     i = 0;
            DataRow workRow;

            for (i = 0; i < properties.Count; i++)
            {
                workRow = _streamTableData.NewRow();

                //workRow["Number"] = i;
                workRow["Property"] = properties[i];
                workRow["Unit"]     = propertyUnits[i];

                foreach (var stream in Streams)
                {
                    workRow[stream.Name] = GetStreamProperty(properties[i], stream);
                }
                _streamTableData.Rows.Add(workRow);
            }

            workRow = _streamTableData.NewRow();
            // workRow["Number"] = i++;
            workRow["Property"] = "Phase";
            workRow["Unit"]     = "";
            foreach (var stream in Streams)
            {
                workRow[stream.Name] = stream.State;
            }
            _streamTableData.Rows.Add(workRow);

            workRow = _streamTableData.NewRow();
            // workRow["Number"] = i++;
            _streamTableData.Rows.Add(workRow);

            var components = system.Components;

            // Molar flows
            if (PrintMolarVariables)
            {
                workRow = _streamTableData.NewRow();
                //   workRow["Number"] = i++;
                workRow["Property"] = "Total Molar Flow";
                workRow["Unit"]     = system.VariableFactory.Output.UnitDictionary[UnitsOfMeasure.PhysicalDimension.MolarFlow].Symbol;
                foreach (var stream in Streams)
                {
                    workRow[stream.Name] = stream.Mixed.TotalMolarflow.ValueInOutputUnit.ToString("0.000");
                }
                _streamTableData.Rows.Add(workRow);

                workRow = _streamTableData.NewRow();
                // workRow["Number"] = i++;
                workRow["Property"] = "";// "Component Molar Flows";
                _streamTableData.Rows.Add(workRow);


                for (int j = 0; j < components.Count; j++)
                {
                    workRow = _streamTableData.NewRow();
                    //   workRow["Number"] = i++;
                    workRow["Property"] = "ṅ [" + components[j].ID + "]";
                    workRow["Unit"]     = system.VariableFactory.Output.UnitDictionary[UnitsOfMeasure.PhysicalDimension.MolarFlow].Symbol;
                    foreach (var stream in Streams)
                    {
                        var value = stream.Mixed.ComponentMolarflow[j].ValueInOutputUnit;
                        if (value > 1e-8)
                        {
                            workRow[stream.Name] = value.ToString("0.000");
                        }
                        else
                        {
                            workRow[stream.Name] = null;
                        }
                    }
                    _streamTableData.Rows.Add(workRow);
                }

                //Molar fractions
                workRow = _streamTableData.NewRow();
                //  workRow["Number"] = i++;
                workRow["Property"] = "";// "Component Molar Fractions";
                _streamTableData.Rows.Add(workRow);


                for (int j = 0; j < components.Count; j++)
                {
                    workRow = _streamTableData.NewRow();
                    //    workRow["Number"] = i++;
                    workRow["Property"] = "x [" + components[j].ID + "]";
                    workRow["Unit"]     = "mol/mol";
                    foreach (var stream in Streams)
                    {
                        // workRow[stream.Name] = stream.Mixed.ComponentMolarFraction[j].ValueInSI.ToString("0.0000");

                        var value = stream.Mixed.ComponentMolarFraction[j].ValueInSI;
                        if (value > 1e-8)
                        {
                            workRow[stream.Name] = value.ToString("0.000");
                        }
                        else
                        {
                            workRow[stream.Name] = null;
                        }
                    }
                    _streamTableData.Rows.Add(workRow);
                }

                workRow = _streamTableData.NewRow();
                //  workRow["Number"] = i++;
                _streamTableData.Rows.Add(workRow);
            }



            // Mass flows
            workRow = _streamTableData.NewRow();
            // workRow["Number"] = i++;
            workRow["Property"] = "Total Mass Flow";
            workRow["Unit"]     = system.VariableFactory.Output.UnitDictionary[UnitsOfMeasure.PhysicalDimension.MassFlow].Symbol;
            foreach (var stream in Streams)
            {
                workRow[stream.Name] = stream.Mixed.TotalMassflow.ValueInOutputUnit.ToString("0.000");
            }
            _streamTableData.Rows.Add(workRow);

            workRow = _streamTableData.NewRow();
            //  workRow["Number"] = i++;
            workRow["Property"] = "";//"Component Mass Flows";
            _streamTableData.Rows.Add(workRow);

            for (int j = 0; j < components.Count; j++)
            {
                var sumForCompJ = Streams.Sum(s => s.Mixed.ComponentMassflow[j].ValueInOutputUnit);

                if (sumForCompJ > 1e-6 || !RemoveMissingComponents)
                {
                    workRow = _streamTableData.NewRow();
                    //       workRow["Number"] = i++;
                    workRow["Property"] = "" + components[j].ID + "";
                    workRow["Unit"]     = system.VariableFactory.Output.UnitDictionary[UnitsOfMeasure.PhysicalDimension.MassFlow].Symbol;
                    foreach (var stream in Streams)
                    {
                        // workRow[stream.Name] = stream.Mixed.ComponentMassflow[j].ValueInOutputUnit.ToString("0.000");

                        var value = stream.Mixed.ComponentMassflow[j].ValueInOutputUnit;
                        if (value > 1e-8)
                        {
                            workRow[stream.Name] = value.ToString("0.000");
                        }
                        else
                        {
                            workRow[stream.Name] = null;
                        }
                    }
                    _streamTableData.Rows.Add(workRow);
                }
            }

            //Mass fractions
            workRow = _streamTableData.NewRow();
            //   workRow["Number"] = i++;
            workRow["Property"] = "";//"Component Mass Fractions";
            _streamTableData.Rows.Add(workRow);
            for (int j = 0; j < components.Count; j++)
            {
                var sumForCompJ = Streams.Sum(s => s.Mixed.ComponentMassFraction[j].ValueInSI);

                if (sumForCompJ > 1e-6 || !RemoveMissingComponents)
                {
                    workRow = _streamTableData.NewRow();
                    //          workRow["Number"] = i++;
                    workRow["Property"] = "" + components[j].ID + "";
                    workRow["Unit"]     = "w-%";
                    foreach (var stream in Streams)
                    {
                        //workRow[stream.Name] = stream.Mixed.ComponentMassFraction[j].ValueInSI.ToString("P2");

                        var value = stream.Mixed.ComponentMassFraction[j].ValueInSI;
                        if (value > 1e-8)
                        {
                            workRow[stream.Name] = value.ToString("0.000");
                        }
                        else
                        {
                            workRow[stream.Name] = null;
                        }
                    }
                    _streamTableData.Rows.Add(workRow);
                }
            }
            return(_streamTableData);
        }
Example #2
0
        private void Load(string Path)
        {
            if (File.Exists(Path))
            {
                canNetwork.Items.Clear();
                NetworkParameters = Newtonsoft.Json.JsonConvert.DeserializeObject <SimulationParameters>(File.ReadAllText(Path));

                var networkDir = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Path), System.IO.Path.GetFileNameWithoutExtension(Path));

                try
                {
                    var nodes = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Node> >(File.ReadAllText(System.IO.Path.Combine(networkDir, "nodes.json")));
                    canNetwork.AddItems(nodes);
                    NodeStore.Nodes = nodes;

                    try
                    {
                        var links = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Link> >(File.ReadAllText(System.IO.Path.Combine(networkDir, "links.json")));
                        canNetwork.AddItems(links);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Links are corrupt.");
                    }

                    try
                    {
                        var streams = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Stream> >(File.ReadAllText(System.IO.Path.Combine(networkDir, "streams.json")));
                        canNetwork.AddItems(streams);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Streams are corrupt.");
                    }

                    try
                    {
                        var domains = Newtonsoft.Json.JsonConvert.DeserializeObject <List <Domain> >(File.ReadAllText(System.IO.Path.Combine(networkDir, "domains.json")));
                        canNetwork.AddItems(domains);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Domains are corrupt.");
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Nodes are corrupt.");
                }

                try
                {
                    DisplayProperties.Reset();
                    var displayProperties = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Tuple <Type, object> > >(File.ReadAllText(System.IO.Path.Combine(networkDir, "display.json")));

                    foreach (var property in displayProperties)
                    {
                        if (property.Value.Item1.IsEnum)
                        {
                            dynamic value = Enum.ToObject(property.Value.Item1, property.Value.Item2);

                            var field = typeof(DisplayProperties).GetField(property.Key);
                            field.SetValue(null, value);
                        }
                        else
                        {
                            var field = typeof(DisplayProperties).GetField(property.Key);
                            field.SetValue(null, property.Value.Item2);
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Display properties are corrupt.");
                }

                // JSON can't store circular reference to parent or reference to actual Node / Link / Stream / Domain object.
                foreach (var trace in NetworkParameters.Traces)
                {
                    foreach (var attribute in trace.Attributes)
                    {
                        attribute.Parent = trace;
                        if (attribute.Element is Node)
                        {
                            attribute.Element = NodeStore.Nodes.First((x) => x.Name == (attribute.Element as Node).Name);
                        }
                        else if (attribute.Element is Link)
                        {
                            attribute.Element = Links.First((x) => x.Name == (attribute.Element as Link).Name);
                        }
                        else if (attribute.Element is Stream)
                        {
                            attribute.Element = Streams.First((x) => x.Name == (attribute.Element as Stream).Name);
                        }
                        else if (attribute.Element is Domain)
                        {
                            attribute.Element = Domains.First((x) => x.Name == (attribute.Element as Domain).Name);
                        }
                    }
                }

                // Fix up the links in plot attributes.
                foreach (var plot in NetworkParameters.Plots)
                {
                    foreach (var attribute in plot.Attributes)
                    {
                        attribute.TraceParameter = NetworkParameters.Traces.First((x) => x.Name == attribute.TraceParameter.Name);
                    }
                }

                canNetwork.Invalidate();
                NodeStore.Nodes = null;
                SetTitle();
            }
            else
            {
                MessageBox.Show($"Cannot find file \"{Path}\"");
            }
        }