00001 #include "PSSE_Raw_Data.h"
00002 #include <fstream>
00003 #include <iostream>
00004 #include <sstream>
00005 #include <cstring>
00006 #include <cassert>
00007 #include <cstdlib>
00008 using namespace std;
00009
00010 static char* GETTOK(char* l)
00011 {
00012 static const char tok = ',';
00013 static char* str;
00014 static int pos;
00015 if (l != NULL)
00016 {
00017 str = l;
00018 pos = 0;
00019 }
00020 else
00021 {
00022 str += (pos+1);
00023 pos = 0;
00024 }
00025 while (str[pos] != tok && str[pos] != '\0')
00026 {
00027 pos++;
00028 }
00029 str[pos] = '\0';
00030 return str;
00031 }
00032
00033 #define DEGS_TO_RADS (3.1416/180.0)
00034 const int PSSE_Raw_Data::LINE_LEN = 5000;
00035
00036 PSSE_Raw_Data::PSSE_Raw_Data(const char* data_file, bool noisy):
00037 AutoInitializingData()
00038 {
00039 map<unsigned,unsigned> busID_to_index;
00040 char* line = new char[LINE_LEN];
00041 ifstream fin(data_file);
00042 if (fin.bad() && fin == 0)
00043 {
00044 throw DataFileFormatException("Could not open file");
00045 }
00046
00047 fin.getline(line,LINE_LEN,'\r');
00048 if (noisy) cout << line << endl;
00049 stringstream sin(line);
00050 double Sbase; int IC; sin >> IC >> Sbase;
00051 if (noisy) cout << "Sbase = " << Sbase << endl;
00052
00053 fin.getline(line,LINE_LEN,'\r'); fin.getline(line,LINE_LEN,'\r');
00054
00055 int flag, index = 0;
00056 do
00057 {
00058 fin.getline(line,LINE_LEN,'\r');
00059 if (noisy) cout << line << endl;
00060 flag = atoi(GETTOK(line));
00061 bus_data_t bus_data;
00062 if (flag != 0)
00063 {
00064
00065 bus_data.ID = flag;
00066
00067 GETTOK(NULL);
00068
00069 GETTOK(NULL);
00070
00071 GETTOK(NULL);
00072
00073 double B_real = atof(GETTOK(NULL));
00074 double B_imag = atof(GETTOK(NULL));
00075 bus_data.B = Complex(B_real,B_imag);
00076 bus_data.B /= Sbase;
00077
00078 GETTOK(NULL);
00079 GETTOK(NULL);
00080
00081 bus_data.v = atof(GETTOK(NULL));
00082 bus_data.theta = atof(GETTOK(NULL))*DEGS_TO_RADS;
00083
00084 bus_data.load_mw = 0.0;
00085 bus_data.load_mvar = 0.0;
00086 bus_data.genr_mw = 0.0;
00087 bus_data.genr_mvar = 0.0;
00088
00089 if (bus_data.v != 0)
00090 {
00091 busID_to_index[bus_data.ID] = index;
00092 bus_data.ID = index++;
00093 nodes.push_back(bus_data);
00094 }
00095 if (noisy)
00096 {
00097 cout << "BUS " << flag << " V = " << bus_data.v <<
00098 " ang. " << bus_data.theta/DEGS_TO_RADS << endl;
00099 }
00100 }
00101 }
00102 while (flag != 0);
00103
00104 do
00105 {
00106 fin.getline(line,LINE_LEN,'\r');
00107 if (noisy) cout << line << endl;
00108 flag = atoi(GETTOK(line));
00109
00110 if (flag != 0 && busID_to_index.find(flag) != busID_to_index.end())
00111 {
00112
00113 bus_data_t& bus_data = nodes[busID_to_index[flag]];
00114
00115 GETTOK(NULL);
00116 GETTOK(NULL);
00117 GETTOK(NULL);
00118 GETTOK(NULL);
00119
00120
00121 bus_data.load_mw += atof(GETTOK(NULL))/Sbase;
00122 bus_data.load_mvar += atof(GETTOK(NULL))/Sbase;
00123
00124 bus_data.load_mw += atof(GETTOK(NULL))/Sbase;
00125 bus_data.load_mvar += atof(GETTOK(NULL))/Sbase;
00126
00127 bus_data.load_mw += atof(GETTOK(NULL))/Sbase;
00128 bus_data.load_mvar += atof(GETTOK(NULL))/Sbase;
00129
00130 if (bus_data.load_mw < 0.0)
00131 {
00132 bus_data.genr_mw -= bus_data.load_mw;
00133 bus_data.genr_mvar -= bus_data.load_mvar;
00134 bus_data.load_mw = 0.0;
00135 bus_data.load_mvar = 0.0;
00136
00137 assert(bus_data.genr_mw > 0.0);
00138 addGenr(bus_data,Sbase);
00139 }
00140 if (noisy)
00141 {
00142 cout << "BUS " << flag << " Load P,Q = " << bus_data.load_mw*Sbase << ","
00143 << bus_data.load_mvar*Sbase << " , Genr P,Q = " <<
00144 bus_data.genr_mw*Sbase << "," << bus_data.genr_mvar*Sbase << endl;
00145 }
00146 }
00147 }
00148 while (flag != 0);
00149
00150 index = 0;
00151 do
00152 {
00153 fin.getline(line,LINE_LEN,'\r');
00154 if (noisy) cout << line << endl;
00155 flag = atoi(GETTOK(line));
00156
00157 if (flag != 0 && busID_to_index.find(flag) != busID_to_index.end())
00158 {
00159
00160 bus_data_t& bus_data = nodes[busID_to_index[flag]];
00161
00162 GETTOK(NULL);
00163
00164 double mw = atof(GETTOK(NULL))/Sbase;
00165 double mvar = atof(GETTOK(NULL))/Sbase;
00166
00167 if (mw <= 0.0)
00168 {
00169 bus_data.load_mw -= mw;
00170 bus_data.load_mvar -= mvar;
00171 }
00172 else
00173 {
00174 bus_data.genr_mw += mw;
00175 bus_data.genr_mvar += mvar;
00176
00177 if (genrs.find(bus_data.ID) == genrs.end()) index++;
00178
00179 assert(bus_data.genr_mw > 0.0);
00180 addGenr(bus_data,Sbase);
00181 }
00182 if (noisy)
00183 {
00184 cout << "BUS " << flag << "(" << index << ") Genr P,Q = " << bus_data.genr_mw*Sbase << ","
00185 << bus_data.genr_mvar*Sbase << endl;
00186 cout << "BUS " << flag << " Load P,Q = " << bus_data.load_mw*Sbase << ","
00187 << bus_data.load_mvar*Sbase << endl;
00188 }
00189 }
00190 }
00191 while (flag != 0);
00192 int line_count = 0;
00193
00194 do
00195 {
00196 skip_line:
00197 fin.getline(line,LINE_LEN,'\r');
00198 if (noisy) cout << line << endl;
00199 flag = atoi(GETTOK(line));
00200 line_t line_data;
00201
00202 if (flag != 0)
00203 {
00204
00205
00206 int to_bus = abs(atoi(GETTOK(NULL)));
00207
00208 if (busID_to_index.find(flag) == busID_to_index.end() ||
00209 busID_to_index.find(to_bus) == busID_to_index.end())
00210 goto skip_line;
00211
00212 line_data.from = busID_to_index[flag];
00213 line_data.to = busID_to_index[to_bus];
00214
00215 GETTOK(NULL);
00216
00217 double real_line_y = atof(GETTOK(NULL));
00218 double imag_line_y = atof(GETTOK(NULL));
00219 line_data.y = 1.0/Complex(real_line_y,imag_line_y);
00220
00221 line_data.B = Complex(0,atof(GETTOK(NULL)));
00222
00223 GETTOK(NULL);
00224 GETTOK(NULL);
00225 GETTOK(NULL);
00226
00227 line_data.turns = atof(GETTOK(NULL));
00228 if (line_data.turns != 0.0)
00229 {
00230 line_data.turns = 1.0/line_data.turns;
00231 line_data.phase_shift = -atof(GETTOK(NULL))*DEGS_TO_RADS;
00232 line_data.tside = line_data.from;
00233 }
00234 else GETTOK(NULL);
00235
00236 Complex BI(atof(GETTOK(NULL)),atof(GETTOK(NULL)));
00237 Complex BJ(atof(GETTOK(NULL)),atof(GETTOK(NULL)));
00238 nodes[line_data.from].B += BI;
00239 nodes[line_data.to].B += BJ;
00240
00241 int status = atoi(GETTOK(NULL));
00242
00243 if (status == 1)
00244 lines.push_back(line_data);
00245 if (noisy)
00246 {
00247 cout << "LINE " << line_data.from << "->" << line_data.to << " y = " << line_data.y << endl;
00248 }
00249 line_count++;
00250 }
00251 }
00252 while (flag != 0);
00253 cout << "Got " << line_count << " transmission lines" << endl;
00254
00255 delete [] line;
00256
00257 setInitialConditions();
00258 }
00259
00260 PSSE_Raw_Data::~PSSE_Raw_Data()
00261 {
00262 }