Example #1
0
        public IEnumerable <IdentifiedObject> Transform(CimContext context, IEnumerable <IdentifiedObject> input)
        {
            HashSet <PhysicalNetworkModel.IdentifiedObject> dropList = new HashSet <IdentifiedObject>();

            List <PhysicalNetworkModel.IdentifiedObject> addList = new List <IdentifiedObject>();


            foreach (var inputCimObject in input)
            {
                if (inputCimObject is ACLineSegment && ((ACLineSegment)inputCimObject).PSRType == "InternalCable" && ((ACLineSegment)inputCimObject).BaseVoltage > 1000)
                {
                    var acls = inputCimObject as ACLineSegment;

                    // acls terminal connections
                    var aclsTerminalConnections = context.GetConnections(acls).ToArray();

                    // Only remove cables attached to power transformers
                    if (acls.GetNeighborConductingEquipments(context).Exists(n => n is PowerTransformer))
                    {
                        if (aclsTerminalConnections.Length == 2)
                        {
                            var cn1 = aclsTerminalConnections[0].ConnectivityNode;
                            var cn2 = aclsTerminalConnections[1].ConnectivityNode;

                            // Move everything that is connected to c1 to c2

                            var cn1equipments = context.GetConnections(cn1).ToArray();

                            foreach (var cn1Eq in cn1equipments)
                            {
                                context.ConnectTerminalToAnotherConnectitityNode(cn1Eq.Terminal, cn2);
                            }

                            // remove cable and it's terminals
                            dropList.Add(acls);
                            dropList.Add(aclsTerminalConnections[0].Terminal);
                            dropList.Add(aclsTerminalConnections[1].Terminal);
                        }
                    }
                }
            }


            // return objects, except the one dropped
            foreach (var inputObj in input)
            {
                if (!dropList.Contains(inputObj))
                {
                    yield return(inputObj);
                }
            }

            // yield added objects,
            foreach (var inputObj in addList)
            {
                yield return(inputObj);
            }
        }
Example #2
0
        public static List <IdentifiedObject> Filter(CimContext context, FilterRule rule)
        {
            //List<IdentifiedObject> result = new List<IdentifiedObject>();
            Dictionary <string, IdentifiedObject> result = new Dictionary <string, IdentifiedObject>();

            HashSet <string> assetRefs        = new HashSet <string>();
            HashSet <string> assetInfoRefs    = new HashSet <string>();
            HashSet <string> assetModelRefs   = new HashSet <string>();
            HashSet <string> manufacturerRefs = new HashSet <string>();

            HashSet <PhysicalNetworkModel.ConnectivityNode> cnAlreadyWritten = new HashSet <PhysicalNetworkModel.ConnectivityNode>();

            FeederInfoContext feederContext = new FeederInfoContext(context);

            feederContext.CreateFeederObjects();



            foreach (var cimObject in context.GetAllObjects())
            {
                if ((cimObject is PhysicalNetworkModel.ConductingEquipment &&
                     ((PhysicalNetworkModel.ConductingEquipment)cimObject).BaseVoltage >= rule.MinVoltageLevel) ||
                    !(cimObject is PhysicalNetworkModel.ConductingEquipment) ||
                    cimObject is PhysicalNetworkModel.PowerTransformer ||
                    cimObject is PhysicalNetworkModel.ExternalNetworkInjection ||
                    (cimObject is EnergyConsumer && ((PowerSystemResource)cimObject).PSRType == "Aftagepunkt_fællesmaaling")
                    )
                {
                    if (
                        cimObject is PhysicalNetworkModel.ACLineSegment ||
                        cimObject is PhysicalNetworkModel.BusbarSection ||
                        cimObject is PhysicalNetworkModel.LoadBreakSwitch ||
                        cimObject is PhysicalNetworkModel.Breaker ||
                        cimObject is PhysicalNetworkModel.Disconnector ||
                        cimObject is PhysicalNetworkModel.Fuse ||
                        cimObject is PhysicalNetworkModel.Substation ||
                        cimObject is PhysicalNetworkModel.VoltageLevel ||
                        cimObject is PhysicalNetworkModel.Bay ||
                        cimObject is PhysicalNetworkModel.PowerTransformer ||
                        cimObject is PhysicalNetworkModel.PowerTransformerEnd ||
                        cimObject is PhysicalNetworkModel.ExternalNetworkInjection ||
                        cimObject is PhysicalNetworkModel.PetersenCoil ||
                        cimObject is PhysicalNetworkModel.CurrentTransformer ||
                        cimObject is PhysicalNetworkModel.PotentialTransformer ||
                        cimObject is PhysicalNetworkModel.EnergyConsumer ||
                        cimObject is PhysicalNetworkModel.RatioTapChanger ||
                        cimObject is PhysicalNetworkModel.LinearShuntCompensator/* ||
                                                                                 * cimObject is PhysicalNetworkModel.Asset ||
                                                                                 * cimObject is PhysicalNetworkModel.AssetInfo ||
                                                                                 * cimObject is PhysicalNetworkModel.ProductAssetModel ||
                                                                                 * cimObject is PhysicalNetworkModel.Manufacturer */
                        )

                    {
                        // Find substation
                        Substation partOfSt = null;

                        if (cimObject is Substation)
                        {
                            partOfSt = (Substation)cimObject;
                        }

                        if (cimObject.IsInsideSubstation(context))
                        {
                            partOfSt = cimObject.GetSubstation(true, context);
                        }

                        if (cimObject is ExternalNetworkInjection)
                        {
                            var eni       = cimObject as ExternalNetworkInjection;
                            var neighbors = eni.GetNeighborConductingEquipments();

                            if (neighbors.Exists(c => c.IsInsideSubstation() && (rule.IncludeSpecificSubstations == null || rule.IncludeSpecificSubstations.Contains(c.GetSubstation().name))))
                            {
                                var ce = neighbors.First(c => c.IsInsideSubstation() && (rule.IncludeSpecificSubstations == null || rule.IncludeSpecificSubstations.Contains(c.GetSubstation().name)));

                                // put injection inside substation
                                eni.BaseVoltage = ce.BaseVoltage;
                                if (ce is BusbarSection)
                                {
                                    eni.EquipmentContainer = new EquipmentEquipmentContainer()
                                    {
                                        @ref = context.GetObject <VoltageLevel>(ce.EquipmentContainer.@ref).mRID
                                    };
                                }
                                else
                                {
                                    eni.EquipmentContainer = new EquipmentEquipmentContainer()
                                    {
                                        @ref = context.GetObject <Bay>(ce.EquipmentContainer.@ref).VoltageLevel.@ref
                                    };
                                }
                            }
                        }

                        // Tap changer
                        if (cimObject is RatioTapChanger)
                        {
                            var tap = cimObject as RatioTapChanger;

                            if (!(tap.TransformerEnd != null && tap.TransformerEnd.@ref != null && result.ContainsKey(tap.TransformerEnd.@ref)))
                            {
                                continue;
                            }

                            var ptEnd = context.GetObject <PowerTransformerEnd>(tap.TransformerEnd.@ref);
                        }


                        //  AuxiliaryEquipment - transfer the one pointing to 60 kV breakers only
                        if (cimObject is AuxiliaryEquipment)
                        {
                            var aux = cimObject as AuxiliaryEquipment;

                            // If not connected to terminal, then skip
                            if (aux.Terminal == null)
                            {
                                continue;
                            }

                            var swTerminal = context.GetObject <Terminal>(aux.Terminal.@ref);
                            var ctObj      = context.GetObject <IdentifiedObject>(swTerminal.ConductingEquipment.@ref);

                            if (ctObj is Switch)
                            {
                                Switch ctSw = ctObj as Switch;

                                // If not connedted to breaker, then skip
                                if (!(ctSw is Breaker))
                                {
                                    continue;
                                }

                                // if not 60000 volt, then skip
                                if (ctSw.BaseVoltage != 60000)
                                {
                                    continue;
                                }

                                // If bay name contains transformer, skup
                                var swBayName = ctSw.GetBay(true, context).name;
                                if (swBayName.ToLower().Contains("transformer"))
                                {
                                    continue;
                                }
                            }
                        }


                        // Generel voltage check
                        if (!(cimObject is ConductingEquipment) ||
                            rule.MinVoltageLevel == 0 ||
                            ((ConductingEquipment)cimObject).BaseVoltage == 0 ||
                            ((ConductingEquipment)cimObject).BaseVoltage >= rule.MinVoltageLevel ||
                            (cimObject is EnergyConsumer && ((PowerSystemResource)cimObject).PSRType == "Aftagepunkt_fællesmaaling"))
                        {
                            // Add high voltage measured customer, even if lv modelled
                            if (cimObject is EnergyConsumer && ((EnergyConsumer)cimObject).PSRType == "Aftagepunkt_fællesmaaling" && ((EnergyConsumer)cimObject).BaseVoltage == 400)
                            {
                                // Don't add low voltage consumers if min voltage > 15000
                                if (rule.MinVoltageLevel > 15000)
                                {
                                    continue;
                                }


                                var ec = cimObject as EnergyConsumer;


                                if (ec.name == "571313124501138119")
                                {
                                }

                                var ecAclsNeighbors = context.GetNeighborConductingEquipments(ec).Where(c => c is ACLineSegment).ToList();

                                if (ecAclsNeighbors.Count == 1)
                                {
                                    var ecTerminal = context.GetConnections(ec)[0].Terminal;

                                    var aclsTrafoConnections = context.GetNeighborConductingEquipments(ecAclsNeighbors[0]).Where(c => c is PowerTransformer).ToList();

                                    if (aclsTrafoConnections.Count == 1)
                                    {
                                        var trafo   = aclsTrafoConnections[0];
                                        var trafoCn = context.GetConnections(trafo).Where(c => c.Terminal.sequenceNumber == "2").ToList();

                                        if (trafoCn.Count == 1)
                                        {
                                            var trafoTerminal2   = trafoCn[0].Terminal;
                                            var trafoTerminal2Cn = trafoCn[0].ConnectivityNode;

                                            context.ConnectTerminalToAnotherConnectitityNode(ecTerminal, trafoTerminal2Cn);

                                            var ptSt = trafo.GetSubstation(false, context);

                                            if (ptSt != null)
                                            {
                                                var stVls = context.GetSubstationVoltageLevels(ptSt);

                                                var vl = stVls.Find(o => o.BaseVoltage == ec.BaseVoltage);

                                                if (vl != null)
                                                {
                                                    ec.EquipmentContainer = new EquipmentEquipmentContainer()
                                                    {
                                                        @ref = vl.mRID
                                                    };
                                                }
                                            }

                                            //ec.EquipmentContainer = new EquipmentEquipmentContainer() { @ref = trafo.EquipmentContainer.@ref };
                                        }
                                    }
                                }
                                else if (ecAclsNeighbors.Count > 1)
                                {
                                    Logger.Log(LogLevel.Warning, "Cannot convert: " + ec.name + " multiply cables connected to customer. Must be modelled in PF.");
                                    continue;
                                }
                            }

                            // Check if substation should be filtered away
                            if (cimObject is Substation st)
                            {
                                var priVoltage = st.GetPrimaryVoltageLevel(context);
                                if (priVoltage < rule.MinVoltageLevel)
                                {
                                    continue;
                                }
                            }

                            // Check if power transformer should be filtered away
                            if (cimObject is PowerTransformer pt)
                            {
                                var ends = pt.GetEnds(context);

                                if (!ends.Exists(e => e.BaseVoltage >= rule.MinVoltageLevel))
                                {
                                    continue;
                                }
                            }

                            // Check if power transformer should be filtered away
                            if (cimObject is PowerTransformerEnd ptEnd)
                            {
                                var ptOfPtEnd = context.GetObject <PowerTransformer>(ptEnd.PowerTransformer.@ref);

                                var ends = ptOfPtEnd.GetEnds(context);

                                if (!ends.Exists(e => e.BaseVoltage >= rule.MinVoltageLevel))
                                {
                                    continue;
                                }
                            }


                            // Check if object part of substation should be filtered away due to voltage level
                            if (partOfSt != null)
                            {
                                var priVoltage = partOfSt.GetPrimaryVoltageLevel(context);
                                if (priVoltage < rule.MinVoltageLevel)
                                {
                                    continue;
                                }
                            }


                            // If part of substation, check if we should filter away
                            if (partOfSt != null && (rule.IncludeSpecificSubstations == null ||
                                                     (rule.IncludeSpecificSubstations.Count > 1 &&
                                                      !rule.IncludeSpecificSubstations.Contains(partOfSt.name))))
                            {
                                // Don't filter anything away, if not specific substations specified
                                if (rule.IncludeSpecificSubstations != null)
                                {
                                    bool skip = true;

                                    // Check if substation is feeded from included primary substation

                                    if (partOfSt.PSRType == "SecondarySubstation" &&
                                        partOfSt.InternalFeeders != null &&
                                        partOfSt.InternalFeeders.Count > 0 &&
                                        partOfSt.InternalFeeders[0].ConnectionPoint != null &&
                                        partOfSt.InternalFeeders[0].ConnectionPoint.Substation != null &&
                                        partOfSt.InternalFeeders[0].ConnectionPoint.Substation.name != null)
                                    {
                                        var feededStName = partOfSt.InternalFeeders[0].ConnectionPoint.Substation.name;

                                        if (rule.IncludeSpecificSubstations == null || rule.IncludeSpecificSubstations.Contains(feededStName))
                                        {
                                            skip = false;
                                        }
                                    }

                                    if (skip)
                                    {
                                        continue;
                                    }
                                }
                            }

                            // If acls, check if we should filter away
                            if (cimObject is ACLineSegment && (rule.IncludeSpecificLines == null || rule.IncludeSpecificLines.Count > 1))
                            {
                                bool continueToCheck = true;

                                var acls = cimObject as ACLineSegment;

                                // check if feeded from primary substation
                                var aclsFeeders = feederContext.GeConductingEquipmentFeeders(acls);

                                if (aclsFeeders != null && aclsFeeders.Count > 0)
                                {
                                    if (rule.IncludeSpecificSubstations == null)
                                    {
                                        continueToCheck = false;
                                    }
                                    else
                                    {
                                        var feededStName = aclsFeeders[0].ConnectionPoint.Substation.name;

                                        if (rule.IncludeSpecificSubstations == null || rule.IncludeSpecificSubstations.Contains(feededStName))
                                        {
                                            continueToCheck = false;
                                        }
                                    }
                                }

                                if (continueToCheck)
                                {
                                    if (acls.PSRType == "InternalCable")
                                    {
                                        var aclsSt = acls.GetSubstation(false, context);

                                        if (!(aclsSt != null && (rule.IncludeSpecificSubstations == null || rule.IncludeSpecificSubstations.Contains(aclsSt.name))))
                                        {
                                            continue;
                                        }
                                    }
                                    else if (acls.name != null && acls.name.Contains("#"))
                                    {
                                        var nameSplit = acls.name.Split('#');

                                        var nameWithoutDelStr = nameSplit[0].ToUpper();

                                        if (rule.IncludeSpecificLines != null && !rule.IncludeSpecificLines.Contains(nameWithoutDelStr))
                                        {
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                            }

                            // If min voltagelevel > 400, don't include cable boxes and stuff inside cable boxes
                            if (rule.MinVoltageLevel > 400 &&
                                partOfSt != null
                                &&
                                (partOfSt.PSRType == "CableBox" || partOfSt.PSRType == "T-Junction"))
                            {
                                continue;
                            }

                            if (rule.MinVoltageLevel > 400 &&
                                partOfSt != null &&
                                partOfSt.PSRType == "Tower" &&
                                partOfSt.GetPrimaryVoltageLevel(context) < 1000)
                            {
                                continue;
                            }



                            // don't add voltage level, we do this later
                            if (cimObject is VoltageLevel)
                            {
                                continue;
                            }

                            result.Add(cimObject.mRID, cimObject);

                            // Add terminals if conducting equipment
                            if (cimObject is ConductingEquipment)
                            {
                                var ci = cimObject as PhysicalNetworkModel.ConductingEquipment;

                                foreach (var tc in context.GetConnections(ci))
                                {
                                    string stName = "";

                                    if (partOfSt != null && partOfSt.name != null)
                                    {
                                        stName = partOfSt.name + "_";
                                    }

                                    //tc.Terminal.phases = PhysicalNetworkModel.PhaseCode.ABCN;
                                    tc.Terminal.name = stName + ci.name + "_T" + tc.Terminal.sequenceNumber;
                                    result.Add(tc.Terminal.mRID, tc.Terminal);


                                    // add connectivity node, if not already added
                                    if (!cnAlreadyWritten.Contains(tc.ConnectivityNode))
                                    {
                                        result.Add(tc.ConnectivityNode.mRID, tc.ConnectivityNode);
                                    }

                                    cnAlreadyWritten.Add(tc.ConnectivityNode);
                                }
                            }

                            // Add location
                            if (cimObject is PowerSystemResource)
                            {
                                var psrObj = cimObject as PowerSystemResource;

                                if (psrObj.PSRType != "InternalCable")
                                {
                                    if (psrObj.Location != null && psrObj.Location.@ref != null)
                                    {
                                        var loc = context.GetObject <PhysicalNetworkModel.LocationExt>(psrObj.Location.@ref);
                                        result.Add(loc.mRID, loc);
                                    }
                                }
                            }

                            // Add substation voltage levels
                            if (cimObject is Substation)
                            {
                                var psrObj = cimObject as Substation;

                                var voltageLevels = context.GetSubstationVoltageLevels(psrObj);

                                foreach (var vl in voltageLevels)
                                {
                                    result.Add(vl.mRID, vl);
                                }
                            }
                        }
                    }
                }
            }

            // Add protective equipment (relays)
            foreach (var cimObject in context.GetAllObjects())
            {
                if (cimObject is ProtectionEquipmentExt)
                {
                    var pe = cimObject as ProtectionEquipmentExt;

                    if (pe.ProtectedSwitches != null && pe.ProtectedSwitches.Length > 0 && result.ContainsKey(pe.ProtectedSwitches[0].@ref))
                    {
                        result.Add(cimObject.mRID, cimObject);
                    }
                }
            }

            //////////////////////////////////////////////////////////////////////////////////7
            // Add Asset stuff
            //////////////////////////////////////////////////////////////////////////////////7

            // Asset ref
            foreach (var cimObject in result.Values)
            {
                if (cimObject is PowerSystemResource)
                {
                    var psr = cimObject as PowerSystemResource;

                    if (psr.Assets != null && psr.Assets.@ref != null)
                    {
                        assetRefs.Add(psr.Assets.@ref);
                    }
                }
            }

            // Asset
            foreach (var cimObject in context.GetAllObjects())
            {
                if (cimObject is PhysicalNetworkModel.Asset)
                {
                    var asset = cimObject as PhysicalNetworkModel.Asset;

                    if (!assetRefs.Contains(asset.mRID))
                    {
                        continue;
                    }

                    if (asset.AssetInfo != null && asset.AssetInfo.@ref != null && !assetInfoRefs.Contains(asset.AssetInfo.@ref))
                    {
                        assetInfoRefs.Add(asset.AssetInfo.@ref);
                    }

                    result.Add(asset.mRID, asset);
                }
            }

            // Asset info
            foreach (var cimObject in context.GetAllObjects())
            {
                if (cimObject is PhysicalNetworkModel.AssetInfo)
                {
                    var assetInfo = cimObject as PhysicalNetworkModel.AssetInfo;

                    if (!assetInfoRefs.Contains(assetInfo.mRID))
                    {
                        continue;
                    }


                    if (!(cimObject is PhysicalNetworkModel.CurrentTransformerInfoExt ||
                          cimObject is PhysicalNetworkModel.CableInfoExt ||
                          cimObject is PhysicalNetworkModel.OverheadWireInfoExt))
                    {
                    }

                    if (assetInfo.AssetModel != null && assetInfo.AssetModel.@ref != null && !assetModelRefs.Contains(assetInfo.AssetModel.@ref))
                    {
                        assetModelRefs.Add(assetInfo.AssetModel.@ref);
                    }

                    result.Add(assetInfo.mRID, assetInfo);
                }
            }

            // Asset model
            foreach (var cimObject in context.GetAllObjects())
            {
                if (cimObject is PhysicalNetworkModel.ProductAssetModel)
                {
                    var assetModel = cimObject as PhysicalNetworkModel.ProductAssetModel;

                    if (!assetModelRefs.Contains(assetModel.mRID))
                    {
                        continue;
                    }

                    if (assetModel.Manufacturer != null && assetModel.Manufacturer.@ref != null && !manufacturerRefs.Contains(assetModel.Manufacturer.@ref))
                    {
                        manufacturerRefs.Add(assetModel.Manufacturer.@ref);
                    }

                    result.Add(assetModel.mRID, assetModel);
                }
            }

            // Manufacturer
            foreach (var cimObject in context.GetAllObjects())
            {
                if (cimObject is PhysicalNetworkModel.Manufacturer)
                {
                    var manu = cimObject as PhysicalNetworkModel.Manufacturer;

                    if (!manufacturerRefs.Contains(manu.mRID))
                    {
                        continue;
                    }

                    result.Add(manu.mRID, manu);
                }
            }

            return(result.Values.ToList());
        }
        public IEnumerable <IdentifiedObject> Transform(CimContext context, IEnumerable <IdentifiedObject> input)
        {
            HashSet <PhysicalNetworkModel.IdentifiedObject> dropList = new HashSet <IdentifiedObject>();

            List <PhysicalNetworkModel.IdentifiedObject> addList = new List <IdentifiedObject>();

            foreach (var inputCimObject in input)
            {
                // Find connectivity nodes outside substations
                if (inputCimObject is ConnectivityNode && !dropList.Contains(inputCimObject) && !inputCimObject.IsInsideSubstation(context))
                {
                    ConnectivityNode cn = inputCimObject as ConnectivityNode;

                    // Handle that acls cn relationship might have changed
                    var cnNeighborsx = cn.GetNeighborConductingEquipments(context);

                    // If two acls and we are above low voltage, merge them
                    if (cnNeighborsx.Count(o => o.BaseVoltage > 5000) > 0)
                    {
                        // acls <-> acls
                        if (cnNeighborsx.Count(o => o is ACLineSegmentExt) == 2)
                        {
                            var acls = cnNeighborsx.Where(o => o is ACLineSegmentExt).ToList();

                            var acls1 = acls[0] as ACLineSegment;
                            var acls2 = acls[1] as ACLineSegment;

                            // NEVER MERGE
                            bool theSame = false;

                            // Compate bch
                            if (acls1.bch != null && acls2.bch != null && !CompareAclsValue(acls1.length.Value, acls1.bch.Value, acls2.length.Value, acls2.bch.Value))
                            {
                                theSame = false;
                            }

                            // Compate b0ch
                            if (acls1.b0ch != null && acls2.b0ch != null && !CompareAclsValue(acls1.length.Value, acls1.b0ch.Value, acls2.length.Value, acls2.b0ch.Value))
                            {
                                theSame = false;
                            }

                            // Compare gch
                            if (acls1.gch != null && acls2.gch != null && !CompareAclsValue(acls1.length.Value, acls1.gch.Value, acls2.length.Value, acls2.gch.Value))
                            {
                                theSame = false;
                            }

                            // Compare g0ch
                            if (acls1.g0ch != null && acls2.g0ch != null && !CompareAclsValue(acls1.length.Value, acls1.g0ch.Value, acls2.length.Value, acls2.g0ch.Value))
                            {
                                theSame = false;
                            }

                            // Compare r
                            if (acls1.r != null && acls2.r != null && !CompareAclsValue(acls1.length.Value, acls1.r.Value, acls2.length.Value, acls2.r.Value))
                            {
                                theSame = false;
                            }

                            // Compare r0
                            if (acls1.r0 != null && acls2.r0 != null && !CompareAclsValue(acls1.length.Value, acls1.r0.Value, acls2.length.Value, acls2.r0.Value))
                            {
                                theSame = false;
                            }

                            // Compare x
                            if (acls1.x != null && acls2.x != null && !CompareAclsValue(acls1.length.Value, acls1.x.Value, acls2.length.Value, acls2.x.Value))
                            {
                                theSame = false;
                            }

                            // Compare x0
                            if (acls1.x0 != null && acls2.x0 != null && !CompareAclsValue(acls1.length.Value, acls1.x0.Value, acls2.length.Value, acls2.x0.Value))
                            {
                                theSame = false;
                            }


                            // If the cables have the same eletrical charastica, merge them
                            if (theSame)
                            {
                                // ACLS 1 will survive, ACLS 2 and the CN will die
                                dropList.Add(cn);
                                dropList.Add(acls2);

                                // drop acls 2 terminals
                                foreach (var tc in context.GetConnections(acls2))
                                {
                                    dropList.Add(tc.Terminal);
                                }

                                var loc1 = context.GetObject <LocationExt>(acls1.Location.@ref);
                                var loc2 = context.GetObject <LocationExt>(acls2.Location.@ref);

                                LineMerger lm = new LineMerger();

                                // Convert to NTS geometries
                                lm.Add(GetGeometry(loc1));
                                lm.Add(GetGeometry(loc2));

                                // Merge the two line strings
                                var mergedLineList = lm.GetMergedLineStrings();

                                if (mergedLineList.Count != 1)
                                {
                                    throw new Exception("Cannot merge ACLS: " + acls1.mRID + " and " + acls2.mRID);
                                }

                                // Overwrite loc 1 coordinated with merged strings
                                loc1.coordinates = GetPoints((ILineString)mergedLineList[0]).ToArray();

                                // Sum length
                                acls1.length.Value += acls2.length.Value;

                                // Sum bch
                                if (acls1.bch != null && acls2.bch != null)
                                {
                                    acls1.bch.Value += acls2.bch.Value;
                                }

                                // Sum b0ch
                                if (acls1.b0ch != null && acls2.b0ch != null)
                                {
                                    acls1.b0ch.Value += acls2.b0ch.Value;
                                }

                                // Sum gch
                                if (acls1.gch != null && acls2.gch != null)
                                {
                                    acls1.gch.Value += acls2.gch.Value;
                                }

                                // Sum g0ch
                                if (acls1.g0ch != null && acls2.g0ch != null)
                                {
                                    acls1.g0ch.Value += acls2.g0ch.Value;
                                }

                                // Sum r
                                if (acls1.r != null && acls2.r != null)
                                {
                                    acls1.r.Value += acls2.r.Value;
                                }

                                // Sum r0
                                if (acls1.r0 != null && acls2.r0 != null)
                                {
                                    acls1.r0.Value += acls2.r0.Value;
                                }

                                // Sum x
                                if (acls1.x != null && acls2.x != null)
                                {
                                    acls1.x.Value += acls2.x.Value;
                                }

                                // Sum x0
                                if (acls1.x0 != null && acls2.x0 != null)
                                {
                                    acls1.x0.Value += acls2.x0.Value;
                                }

                                // Find cn in the other end of ACLS 2
                                var acls2otherEndCn = context.GetConnections(acls2).Find(o => o.ConnectivityNode != cn);

                                // Get terminal of ACLS 1 that point to ACLS 2
                                var acls1Terminal = acls1.GetTerminal(acls2, true, context);

                                // Disconnect ACLS 2 terminals
                                var acls2connections = context.GetConnections(acls2);

                                List <Terminal> terminalsToDisconnect = new List <Terminal>();

                                foreach (var acls2con in acls2connections)
                                {
                                    terminalsToDisconnect.Add(acls2con.Terminal);
                                }

                                foreach (var t2d in terminalsToDisconnect)
                                {
                                    context.DisconnectTerminalFromConnectitityNode(t2d);
                                }

                                // Change terminal of ACLS 1 to point to ACLS 2 other end CN
                                context.ConnectTerminalToAnotherConnectitityNode(acls1Terminal, acls2otherEndCn.ConnectivityNode);
                            }
                            else
                            {
                                // Cable are not the same, we need to add a susbstation to act as a junction

                                // Create muffe station
                                var st = new PhysicalNetworkModel.Substation();
                                st.mRID    = Guid.NewGuid().ToString();
                                st.name    = "Junction";
                                st.PSRType = "Junction";
                                addList.Add(st);

                                // Create voltage level
                                var vl = new PhysicalNetworkModel.VoltageLevel();
                                vl.mRID                = Guid.NewGuid().ToString();
                                vl.BaseVoltage         = acls1.BaseVoltage;
                                vl.name                = "VL";
                                vl.EquipmentContainer1 = new VoltageLevelEquipmentContainer()
                                {
                                    @ref = st.mRID
                                };
                                addList.Add(vl);

                                // Relate cn to voltage level
                                if (_mappingContext.ConnectivityNodeToVoltageLevel.ContainsKey(cn))
                                {
                                    _mappingContext.ConnectivityNodeToVoltageLevel.Remove(cn);
                                }

                                _mappingContext.ConnectivityNodeToVoltageLevel.Add(cn, vl);
                            }
                        }
                        // <> 2 kabler
                        else
                        {
                            // Create muffe station
                            var st = new PhysicalNetworkModel.Substation();
                            st.mRID    = Guid.NewGuid().ToString();
                            st.name    = "MUFFE";
                            st.PSRType = "Junction";
                            addList.Add(st);

                            // Create voltage level
                            var vl = new PhysicalNetworkModel.VoltageLevel();
                            vl.mRID                = Guid.NewGuid().ToString();
                            vl.BaseVoltage         = cnNeighborsx[0].BaseVoltage;
                            vl.name                = "VL";
                            vl.EquipmentContainer1 = new VoltageLevelEquipmentContainer()
                            {
                                @ref = st.mRID
                            };
                            addList.Add(vl);

                            // Relate cn to voltage level
                            if (_mappingContext.ConnectivityNodeToVoltageLevel.ContainsKey(cn))
                            {
                                _mappingContext.ConnectivityNodeToVoltageLevel.Remove(cn);
                            }

                            _mappingContext.ConnectivityNodeToVoltageLevel.Add(cn, vl);
                        }
                    }
                }
            }

            // return objects, except the one dropped
            foreach (var inputObj in input)
            {
                if (!dropList.Contains(inputObj))
                {
                    yield return(inputObj);
                }
            }

            // yield added objects,
            foreach (var inputObj in addList)
            {
                yield return(inputObj);
            }
        }
Example #4
0
        public IEnumerable <IdentifiedObject> Transform(CimContext context, IEnumerable <IdentifiedObject> input)
        {
            HashSet <PhysicalNetworkModel.IdentifiedObject> dropList = new HashSet <IdentifiedObject>();

            List <PhysicalNetworkModel.IdentifiedObject> addList = new List <IdentifiedObject>();

            foreach (var inputCimObject in input)
            {
                if (inputCimObject is EnergyConsumer)
                {
                    if (inputCimObject.name == "571313124501138119")
                    {
                    }

                    var ec = inputCimObject as EnergyConsumer;

                    if (ec.BaseVoltage > 400)
                    {
                        // ec terminal connections
                        var ecTerminalConnections = context.GetConnections(inputCimObject);

                        if (ecTerminalConnections.Count == 1)
                        {
                            var ecTerminal = ecTerminalConnections[0].Terminal;

                            var ecCn = ecTerminalConnections[0].ConnectivityNode;

                            var ecCnConnections = context.GetConnections(ecCn);

                            if (ecCnConnections.Exists(o => o.ConductingEquipment is ACLineSegment))
                            {
                                var acls = ecCnConnections.First(o => o.ConductingEquipment is ACLineSegment).ConductingEquipment;

                                // find other end of acls
                                var aclsConnections = context.GetConnections(acls);

                                // Can we find another cn (in the other end of the cable)
                                if (aclsConnections.Exists(o => o.ConnectivityNode != ecCn))
                                {
                                    var aclsCn = aclsConnections.First(o => o.ConnectivityNode != ecCn).ConnectivityNode;

                                    context.ConnectTerminalToAnotherConnectitityNode(ecTerminal, aclsCn);

                                    var aclsSt = aclsCn.GetSubstation(false, context);

                                    if (aclsSt != null)
                                    {
                                        var stVls = context.GetSubstationVoltageLevels(aclsSt);

                                        var vl = stVls.Find(o => o.BaseVoltage == ec.BaseVoltage);

                                        if (vl != null)
                                        {
                                            ec.EquipmentContainer = new EquipmentEquipmentContainer()
                                            {
                                                @ref = vl.mRID
                                            };


                                            dropList.Add(acls);

                                            dropList.Add(aclsConnections[0].Terminal);
                                            dropList.Add(aclsConnections[1].Terminal);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }


            // return objects, except the one dropped
            foreach (var inputObj in input)
            {
                if (inputObj.name == "571313124501006982")
                {
                }

                if (!dropList.Contains(inputObj))
                {
                    yield return(inputObj);
                }
            }

            // yield added objects,
            foreach (var inputObj in addList)
            {
                yield return(inputObj);
            }
        }