network/check.cpp

Go to the documentation of this file.
00001 
00015 #include <stdlib.h>
00016 #include <stdio.h>
00017 #include <errno.h>
00018 #include <math.h>
00019 #include "network.h"
00020 
00021 EXPORT int check(void)
00022 {
00023     unsigned int errcount=0;
00024     OBJECT *obj;
00025     FINDLIST *nodes = gl_find_objects(FL_NEW,FT_CLASS,SAME,"node",FT_END);
00026     FINDLIST *links = gl_find_objects(FL_NEW,FT_CLASS,SAME,"link",FT_END);
00027     FINDLIST *swings = gl_find_objects(FL_NEW,FT_CLASS,SAME,"node",AND,FT_PROPERTY,"type",EQ,"3",FT_END);
00028 
00029     // check links for connectivity
00030     int linkcount[10000];
00031     memset(linkcount,0,sizeof(linkcount));
00032     obj=NULL;
00033     while ((obj=gl_find_next(links,obj))!=NULL)
00034     {
00035         link *branch=OBJECTDATA(obj,link);
00036         if (branch->from==NULL && branch->to==NULL)
00037         {
00038             gl_error("link:%d is not connected on either end", obj->id);
00039             errcount++;
00040         }
00041         else if (branch->from==NULL) 
00042         {
00043             gl_error("link:%d is not connected on 'from' end", obj->id);
00044             errcount++;
00045             linkcount[branch->to->id]++;
00046         }
00047         else if (branch->to==NULL) 
00048         {
00049             gl_error("link:%d is not connected on 'to' end", obj->id);
00050             errcount++;
00051             linkcount[branch->from->id]++;
00052         }
00053         else
00054         {
00055             linkcount[branch->to->id]++;
00056             linkcount[branch->from->id]++;
00057             if (branch->Y.Mag()==0)
00058                 gl_warning("link:%d is open", obj->id);
00059         }
00060     }
00061 
00062     // find swing buses and check link connectivity
00063     struct {
00064         OBJECT *swing;
00065         int count;
00066     } areas[1000];
00067     memset(areas,0,sizeof(areas));
00068     obj=NULL;
00069     while ( (obj=gl_find_next(nodes,obj))!=NULL)
00070     {
00071         node *bus=OBJECTDATA(obj,node);
00072         int n = bus->flow_area_num;
00073         areas[n].count++;
00074         if (bus->type==SWING)
00075         {
00076             if (areas[n].swing!=NULL)
00077                 gl_warning("flow area %d has more than one swing bus (node:%d and node:%d)", n, areas[n].swing->id, obj->id);
00078             else
00079                 areas[n].swing = obj;
00080         }
00081         if (linkcount[obj->id]==0)
00082             gl_warning("node:%d is not connected to anything", obj->id);
00083     }
00084 
00085     // check each area
00086     int i;
00087     for (i=0; i<sizeof(areas)/sizeof(areas[0]); i++)
00088     {
00089         if (areas[i].count>0 && areas[i].swing==NULL)
00090         {
00091             gl_error("flow area %d has no swing bus", i);
00092             errcount++;
00093         }
00094 
00095     }
00096 
00097     // check for islands without swing buses
00098     obj=NULL;
00099     OBJECTNUM bus[10000];
00100     memset(bus,0xff,sizeof(bus));
00101     while ( (obj=gl_find_next(swings,obj))!=NULL)
00102     {
00103         // mark swing bus
00104         bus[obj->id]=obj->id;
00105 
00106         // scan all links to spread swing info until no changes made
00107         bool changed;
00108         do {
00109             OBJECT *p=NULL;
00110             changed=false;
00111             while ((p=gl_find_next(links,p))!=NULL)
00112             {
00113                 link *q=OBJECTDATA(p,link);
00114                 OBJECT *f = q->from;
00115                 OBJECT *t = q->to;
00116                 if (f==NULL || t==NULL)
00117                     continue;
00118                 if (bus[f->id]==obj->id && bus[t->id]==0xffffffff)
00119                 {
00120                     changed = true;
00121                     bus[t->id]=obj->id;
00122                 }
00123                 else if (bus[t->id]==obj->id && bus[f->id]==0xffffffff)
00124                 {
00125                     changed = true;
00126                     bus[f->id]=obj->id;
00127                 }
00128             }
00129         } while (changed);
00130     }
00131     obj=NULL;
00132     while ( (obj=gl_find_next(nodes,obj))!=NULL)
00133     {
00134         if (bus[obj->id]==0xffffffff)
00135         {
00136             gl_warning("node:%d is not connected to any swing bus", obj->id);
00137             errcount++;
00138         }
00139     }
00140     gl_free(nodes);
00141     gl_free(links);
00142 
00143     gl_output("Network check complete: %d errors found",errcount);
00144     return errcount;
00145 }
00146 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy