Esempio n. 1
0
        public bool LoadOutagesFromCSV(string filename)
        {
            StreamReader streamReader = null;

            try
            {
                streamReader = new StreamReader(filename);

                //Skip header
                streamReader.ReadLine();
                List <Outage> items = new List <Outage>();
                while (!streamReader.EndOfStream)
                {
                    //Read and split line
                    string   line      = streamReader.ReadLine();
                    string[] strValues = line.Split(';');

                    //Create Outlet object
                    Outage item = new Outage();

                    item.From        = strValues[0];
                    item.To          = strValues[1];
                    item.ElementName = strValues[2];
                    item.TsoOrigin   = strValues[3];
                    item.id          = $"OU-{rnd.Next(100)}-{rnd.Next(10000)}";

                    items.Add(item);
                }
                outputStream.WriteLine($"Loaded {items.Count} outages");
                outages = items;
            } catch (Exception e)
            {
                outputStream.WriteLine($"Failed to load outages: {e.Message}");
                if (streamReader != null)
                {
                    streamReader.Close();
                }
                Summary = "Failed to load outages from the file.";
                return(false);
            }
            if (streamReader != null)
            {
                streamReader.Close();
            }
            return(true);
        }
Esempio n. 2
0
        public bool Generate()
        {
            List <string> tsoOrigins = new List <string>();

            foreach (var item in criticalBranches)
            {
                if (!tsoOrigins.Contains(item.TsoOrigin))
                {
                    tsoOrigins.Add(item.TsoOrigin);
                }
            }


            /////////////////////////////////////////
            /////// ContingencyDictionary.xml ///////
            /////////////////////////////////////////
            #region ContingencyDictionary.xml
            outputStream.WriteLine("Generating ContingencyDictionary.xml...");

            XmlDocument doc      = new XmlDocument();
            XmlElement  rootNode = doc.CreateElement("FlowBasedContingency");
            rootNode.SetAttribute("DtdRelease", "4");
            rootNode.SetAttribute("DtdVersion", "0");
            rootNode.SetAttribute("xmlns", "flowbased");
            rootNode.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

            XmlAttribute attr = doc.CreateAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
            attr.Value = "flowbasedcontingency-01.xsd";
            rootNode.SetAttributeNode(attr);
            rootNode.Attributes[rootNode.Attributes.Count - 1].Prefix = "xsi";
            doc.AppendChild(rootNode);

            WriteHeader(rootNode, "ContingencyTimeInterval");

            XmlElement outagesElement = doc.CreateElement("outages");
            rootNode.AppendChild(outagesElement);

            foreach (var item in outages)
            {
                XmlElement outage = doc.CreateElement("outage");
                outage.SetAttribute("id", item.id);

                XmlElement branch = doc.CreateElement("branch");
                branch.SetAttribute("eic", item.TsoOrigin);
                branch.SetAttribute("elementName", item.ElementName);
                branch.SetAttribute("from", item.From);
                branch.SetAttribute("to", item.To);

                outage.AppendChild(branch);
                outagesElement.AppendChild(outage);
            }

            //Save and validate
            outputStream.WriteLine("Saving...");
            if (SaveXML(doc, Path.Combine(outputPath, "ContingencyDictionary.xml")))
            {
                Validator validator = new Validator(Path.Combine(outputPath, "ContingencyDictionary.xml"), Path.Combine(outputPath, "flowbasedcontingency-01.xsd"), outputStream, validationResultPrompt);
                if (!validator.Validate(consoleOnly))
                {
                    Summary = "Validation failed.";
                    return(false);
                }
            }
            outputStream.WriteLine();

            #endregion

            //////////////////////////////////////////////
            /////// IndividualCriticalBranches.xml ///////
            //////////////////////////////////////////////
            #region IndividualCriticalBranches.xml
            //Generate IndividualCriticalBranches.xml for each tsoOrigin
            foreach (var tsoOrigin in tsoOrigins)
            {
                outputStream.WriteLine($"Generating IndividualCriticalBranches.xml for tsoOrigin {tsoOrigin}...");

                doc = new XmlDocument();

                rootNode = doc.CreateElement("FlowBasedConstraintDocument");
                rootNode.SetAttribute("DtdRelease", "4");
                rootNode.SetAttribute("DtdVersion", "0");
                rootNode.SetAttribute("xmlns", "flowbased");
                rootNode.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

                attr       = doc.CreateAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
                attr.Value = "flowbasedconstraintdocument-17.xsd";

                rootNode.SetAttributeNode(attr);

                doc.AppendChild(rootNode);

                WriteHeader(rootNode, "ConstraintTimeInterval");

                XmlElement criticalBranchesXml = doc.CreateElement("criticalBranches");
                rootNode.AppendChild(criticalBranchesXml);

                foreach (var criticalBranch in criticalBranches)
                {
                    int outagesCount = rnd.Next(settings.outagesPerBranch) + 1;

                    //Use only criticalBranches with same tsoOrigin
                    if (criticalBranch.TsoOrigin != tsoOrigin)
                    {
                        continue;
                    }

                    for (int i = 0; i < outagesCount; i++)
                    {
                        Outage outage = null;
                        //Find i-th possible outage
                        int skippedOutages = 0;
                        foreach (var item in outages)
                        {
                            if (item.TsoOrigin != criticalBranch.TsoOrigin)
                            {
                                continue;
                            }
                            if (item.From == criticalBranch.From)
                            {
                                continue;
                            }
                            if (item.To == criticalBranch.To)
                            {
                                continue;
                            }

                            if (skippedOutages != i)
                            {
                                skippedOutages++;
                            }
                            else
                            {
                                outage = item;
                                break;
                            }
                        }

                        if (outage == null)
                        {
                            outputStream.WriteLine("No possible outage!");
                            Summary = "Could not find possible outage for critical branch.";
                            return(false);
                        }

                        XmlElement criticalBranchElement = doc.CreateElement("criticalBranch");
                        criticalBranchElement.SetAttribute("id", "testId");

                        XmlElement timeIntervalXml = doc.CreateElement("timeInterval");
                        timeIntervalXml.SetAttribute("v", settings.constraintTimeInterval);
                        criticalBranchElement.AppendChild(timeIntervalXml);

                        XmlElement branchElement = doc.CreateElement("branch");
                        branchElement.SetAttribute("from", criticalBranch.From);
                        branchElement.SetAttribute("to", criticalBranch.To);
                        branchElement.SetAttribute("order", criticalBranch.Order);
                        branchElement.SetAttribute("name", "testName");
                        branchElement.SetAttribute("eic", "testEic");

                        criticalBranchElement.AppendChild(branchElement);

                        //Append custom values
                        foreach (var item in criticalBranch.details)
                        {
                            XmlElement detailXml = doc.CreateElement(item.Key);
                            detailXml.InnerText = item.Value;
                            criticalBranchElement.AppendChild(detailXml);
                        }

                        XmlElement tsoOriginElement = doc.CreateElement("tsoOrigin");
                        tsoOriginElement.InnerText = tsoOrigin;
                        criticalBranchElement.AppendChild(tsoOriginElement);

                        XmlElement outageElement = doc.CreateElement("outage");
                        outageElement.SetAttribute("id", outage.id);
                        criticalBranchElement.AppendChild(outageElement);

                        criticalBranchesXml.AppendChild(criticalBranchElement);
                    }
                }
                outputStream.WriteLine("Saving...");

                //Save and validate
                if (SaveXML(doc, Path.Combine(outputPath, $"IndividualCriticalBranches_{tsoOrigin}.xml")))
                {
                    Validator validator = new Validator(Path.Combine(outputPath, $"IndividualCriticalBranches_{tsoOrigin}.xml"), Path.Combine(outputPath, "flowbasedconstraintdocument-17.xsd"), outputStream, validationResultPrompt);
                    if (!validator.Validate(consoleOnly))
                    {
                        Summary = "Validation failed.";
                        return(false);
                    }
                }
                outputStream.WriteLine();
            }
            #endregion
            return(true);
        }