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