00001 #include "IEEE_CDF_Data.h"
00002 #include <fstream>
00003 #include <iostream>
00004 #include <cstring>
00005 #include <cstdlib>
00006 using namespace std;
00007
00008 #define DEGS_TO_RADS (3.14/180.0)
00009 const int IEEE_CDF_Data::LINE_LEN = 1000;
00010
00011 void IEEE_CDF_Data::read_field(int start, int end)
00012 {
00013 bzero(buffer,LINE_LEN);
00014
00015 start--;
00016 end--;
00017 for (int i = start; i <= end; i++)
00018 {
00019 buffer[i-start] = line[i];
00020 }
00021 }
00022
00023 IEEE_CDF_Data::IEEE_CDF_Data(const char* data_file, bool noisy, bool init):
00024 AutoInitializingData()
00025 {
00026 map<unsigned,unsigned> busID_to_index;
00027 line = new char[LINE_LEN];
00028 buffer = new char[LINE_LEN];
00029 ifstream fin(data_file);
00030 if (fin.bad() && fin == 0)
00031 {
00032 throw DataFileFormatException("Could not open file");
00033 }
00034
00035 fin.getline(line,LINE_LEN);
00036 read_field(32,37);
00037 double Sbase = atof(buffer);
00038 if (noisy) cout << "Sbase = " << Sbase << endl;
00039
00040 fin.getline(line,LINE_LEN);
00041
00042 int flag, index = 0;
00043 do
00044 {
00045 fin.getline(line,LINE_LEN);
00046 read_field(1,4);
00047 flag = atoi(buffer);
00048 bus_data_t bus_data;
00049 if (flag != -999)
00050 {
00051
00052 bus_data.ID = flag;
00053 busID_to_index[bus_data.ID] = index;
00054 bus_data.ID = index++;
00055
00056 read_field(28,33); bus_data.v = atof(buffer);
00057 read_field(34,40); bus_data.theta = atof(buffer);
00058 bus_data.theta *= DEGS_TO_RADS;
00059 read_field(41,49); bus_data.load_mw = atof(buffer)/Sbase;
00060 read_field(50,59); bus_data.load_mvar = atof(buffer)/Sbase;
00061 read_field(60,67); bus_data.genr_mw = atof(buffer)/Sbase;
00062 read_field(68,75); bus_data.genr_mvar = atof(buffer)/Sbase;
00063 read_field(107,114); double real_B = atof(buffer);
00064 read_field(115,122); double imag_B = atof(buffer);
00065 bus_data.B = Complex(real_B,imag_B);
00066
00067 if (bus_data.genr_mw < 0.0)
00068 {
00069 bus_data.load_mw -= bus_data.genr_mw;
00070 bus_data.load_mvar -= bus_data.genr_mvar;
00071 bus_data.genr_mw = 0.0;
00072 bus_data.genr_mvar = 0.0;
00073 }
00074
00075 if (bus_data.load_mw < 0.0)
00076 {
00077 bus_data.genr_mw -= bus_data.load_mw;
00078 bus_data.genr_mvar -= bus_data.load_mvar;
00079 bus_data.load_mw = 0.0;
00080 bus_data.load_mvar = 0.0;
00081 }
00082
00083 read_field(77,83); double Vbase = atof(buffer);
00084 if (Vbase <= 0.0) Vbase = 115.0;
00085
00086 if ((bus_data.genr_mw != 0 || bus_data.genr_mvar != 0))
00087 {
00088 if (noisy)
00089 cout << "BUS " << flag << " (genr #" << genr_nodes.size() << ") P,Q = " <<
00090 bus_data.genr_mw << "," << bus_data.genr_mvar << endl;
00091 addGenr(bus_data,Sbase,Vbase);
00092 }
00093 nodes.push_back(bus_data);
00094 if (noisy)
00095 cout << "BUS " << flag << " P,Q = " <<
00096 bus_data.load_mw << "," << bus_data.load_mvar << endl;
00097 }
00098 }
00099 while (flag != -999);
00100
00101 fin.getline(buffer,LINE_LEN);
00102
00103 int line_count = 0;
00104 do
00105 {
00106 fin.getline(line,LINE_LEN);
00107 read_field(1,4);
00108 flag = atoi(buffer);
00109 line_t line_data;
00110 if (flag != -999)
00111 {
00112
00113 line_data.from = busID_to_index[flag];
00114
00115 read_field(6,9); line_data.to = busID_to_index[atoi(buffer)];
00116
00117 read_field(41,50); line_data.B = Complex(0,atof(buffer));
00118 read_field(20,29); double real_line_y = atof(buffer);
00119 read_field(30,40); double imag_line_y = atof(buffer);
00120 line_data.y = Complex(real_line_y,imag_line_y);
00121
00122 read_field(77,82); line_data.turns = atof(buffer);
00123 read_field(84,90); line_data.phase_shift = -DEGS_TO_RADS*atof(buffer);
00124 if (line_data.turns != 0.0 || line_data.phase_shift != 0.0)
00125 {
00126 if (line_data.turns == 0.0) line_data.turns = 1.0;
00127 else line_data.turns = 1.0/line_data.turns;
00128 line_data.tside = line_data.from;
00129 }
00130 line_data.y = 1.0/line_data.y;
00131 lines.push_back(line_data);
00132 line_count++;
00133 }
00134 }
00135 while (flag != -999);
00136 cout << "Got " << line_count << " transmission lines" << endl;
00137
00138 delete [] line;
00139 delete [] buffer;
00140
00141 if (init)
00142 setInitialConditions();
00143 }
00144
00145 IEEE_CDF_Data::~IEEE_CDF_Data()
00146 {
00147 }