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