00001
00012 #include <stdlib.h>
00013 #include <stdio.h>
00014 #include <errno.h>
00015 #include <math.h>
00016 #include "network.h"
00017
00018 int import_cdf(char *file)
00019 {
00020 int n_bus=0;
00021 int n_branch=0;
00022 int linenum=0;
00023 int errors = 0;
00024 OBJECT *bus[10000]; memset(bus,0,sizeof(bus));
00025 OBJECT *branch[10000]; memset(branch,0,sizeof(branch));
00026
00027 OBJECT *swing[100]; memset(swing,0,sizeof(swing));
00028 int count[100]; memset(count,0,sizeof(count));
00029 char line[1024];
00030 char *ff;
00031 FILE *fp;
00032 OBJECT *obj;
00033 enum {LD_INIT, LD_READY, LD_BUS, LD_BRANCH} state = LD_INIT;
00034
00035
00036 char buffer[1024];
00037 ff = gl_findfile(file,NULL,FF_READ,buffer,sizeof(buffer)-1);
00038 fp = ff?fopen(ff, "r"):NULL;
00039 if (fp==NULL)
00040 return 0;
00041 while (fgets(line,sizeof(line),fp)!=NULL)
00042 {
00043 linenum++;
00044 switch (state) {
00045 case LD_INIT:
00046 if (sscanf(line+31,"%lg",&mvabase)==1)
00047 state=LD_READY;
00048 break;
00049 case LD_READY:
00050 if (mvabase>0 && strncmp(line,"BUS",3)==0)
00051 state = LD_BUS;
00052 else if (mvabase>0 && strncmp(line,"BRANCH",6)==0)
00053 state = LD_BRANCH;
00054
00055 break;
00056 case LD_BUS:
00057 if (atoi(line)>0)
00058 { int n, area, zone, type;
00059 int64 remote;
00060 char32 name;
00061 double Vm, Va, Lr, Li, Gr, Gi, bV, dV, Qh, Ql, G, B;
00062 if (sscanf(line,"%d %12[^\n] %d %d%d %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %lg %" FMT_INT64 "d",
00063 &n, name, &area, &zone, &type, &Vm, &Va, &Lr, &Li, &Gr, &Gi, &bV, &dV, &Qh, &Ql, &G, &B, &remote)==18 && n_bus<10000)
00064 {
00065 obj = gl_create_object(node_class);
00066 obj->parent = NULL;
00067 bus[n] = obj;
00068 node *p = OBJECTDATA(obj,node);
00069 p->create();
00070 obj->name = new char[strlen(name)+1];
00071 strcpy(obj->name,name);
00072 p->flow_area_num = area;
00073 p->loss_zone_num = zone;
00074 p->type = (BUSTYPE)type;
00075 p->V.SetPolar(Vm,Va*PI/180);
00076 p->S.SetRect((Lr-Gr)/mvabase,(Li-Gi)/mvabase);
00077 p->base_kV = bV;
00078 p->desired_kV = dV;
00079 p->Qmax_MVAR = Qh;
00080 p->Qmin_MVAR = Ql;
00081 p->G = G;
00082 p->B = B;
00084 p->remote_bus_id = (OBJECT*)remote;
00085 if (p->type==SWING) swing[area]=obj;
00086 count[area]++;
00087 n_bus++;
00088 #ifdef DEBUG_OBS
00089 if (p->type!=PQ)
00090 {
00091 p->Vstdev = 0.01;
00092 p->Vobs.Re() = gl_random_normal(p->V.Re(),p->Vstdev);
00093 p->Vobs.Im() = gl_random_normal(p->V.Im(),p->Vstdev);
00094 }
00095 if (p->type!=SWING)
00096 p->V = complex(1,0);
00097 else if (p->Vstdev>0)
00098 p->V.SetPolar(p->Vobs.Mag(),0);
00099 #endif
00100 }
00101 else
00102 {
00103 gl_output("%s(%d) : missing bus data", file, linenum);
00104 errors++;
00105 }
00106 }
00107 else
00108 state = LD_READY;
00109 break;
00110 case LD_BRANCH:
00111 if (atoi(line)>0)
00112 { int from, to, area, zone, type, circuit;
00113 double R, X, B, n=0;
00114 if (sscanf(line,"%d %d %d %d %d %d %lg %lg %lg %*lg %*lg %*lg %*d %*d %lg",
00115 &from,&to,&area,&zone,&type,&circuit,&R,&X,&B, &n)>=9 && n_branch<10000)
00116 {
00117 obj = gl_create_object(link_class);
00118 obj->parent = NULL;
00119 branch[n_branch] = obj;
00120 link *p = OBJECTDATA(obj,link);
00121 p->create();
00122 p->from = bus[from];
00123 p->to = bus[to];
00124 if (n>0.0)
00125 p->turns_ratio = n;
00126 else
00127 p->turns_ratio = 1.0;
00128 p->Y = complex(1)/complex(R,X);
00129 p->B = B;
00130 n_branch++;
00131 }
00132 else
00133 {
00134 gl_output("%s(%d) : missing branch data", file, linenum);
00135 errors++;
00136 }
00137 }
00138 else
00139 state = LD_READY;
00140 break;
00141 default:
00142 break;
00143 }
00144 }
00145 fclose(fp);
00146
00147 int n;
00148
00149 for (n=0; n<sizeof(count)/sizeof(count[0]); n++)
00150 {
00151 if (count[n]>0 && swing[n]==NULL)
00152 {
00153 gl_output("%s : flow area %d has no swing bus", file, n);
00154 errors++;
00155 }
00156 }
00157
00158
00159 for (n=0; n<sizeof(bus)/sizeof(bus[0]); n++)
00160 {
00161 OBJECT *obj = bus[n];
00162 if (obj==NULL) continue;
00163 node *p = OBJECTDATA(obj,node);
00164 if (p->type!=SWING)
00165 {
00166 OBJECT *swingbus = swing[p->flow_area_num];
00167 if (swingbus!=NULL)
00168 gl_set_parent(obj,swingbus);
00169 }
00170 p->remote_bus_id= bus[(int64)p->remote_bus_id];
00171 }
00172
00173
00174 for (n=0; n<sizeof(branch)/sizeof(branch[0]); n++)
00175 {
00176 OBJECT *obj = branch[n];
00177 if (obj==NULL) continue;
00178 link *p = OBJECTDATA(obj,link);
00179 if (p->from!=NULL && p->to!=NULL)
00180 gl_set_parent(obj, p->from->rank<p->to->rank ? p->from : p->to);
00181
00182
00183 p->sync(TS_NEVER);
00184 }
00185
00186 return errors>0 ? -errors : n_bus+n_branch;
00187 }
00188 EXPORT int import_file(char *file)
00189 {
00190 char *ext = strrchr(file,'.');
00191 if (ext!=NULL && stricmp(ext,".cdf")==0)
00192 return import_cdf(file);
00193 errno = ENOENT;
00194 return 0;
00195 }
00196