How to read test system data

If the test system just has 3 buses, we could define the system in the C++ code. However, if the system is not that small, it would take a lot of work to define the system in the C++ code.

What I do is:

  1. Use .txt files to save test system data, like bus data, transmission line data, generator data, load factors, etc. The data have some fixed formats. For example, take bus data for example, the first column is the bus number, and the second column is the peak load at the bus.
  2. Read the .txt files, and then put the data into usable variables in the C++ code. For example, I could have a variable called BusLoad that obtains the data from the second column of the bus data.

Once I put my data into a fixed format, how do I read the data?

First, I define a global structure which is used to contain the input data and its length:

struct InputData { // Data structure for input system data
	vector<double> Vec; // a vector that contains system information
	int Len; // the length of the above vector
};

Second, I define a global function that can be used to open .txt files and read the data:

InputData ReadSystemData(string FileDir, string FileName) // This function reads test system data from external files.
{
	string DataFileLoc = FileDir + FileName;
	ifstream DataFile(DataFileLoc.c_str());
	InputData DataIn;
	if (DataFile.is_open())
	{
		while (!DataFile.eof())
		{
			double DataTemp = 0;
			DataFile >> DataTemp;
			DataIn.Vec.push_back(DataTemp);
		}
		DataFile.close();
		DataIn.Len = DataIn.Vec.end() - DataIn.Vec.begin() - 1; // the last character of each file is \0.
		std::cout << FileName << " have been successfully loaded." << endl;
	}
	else
	{
		std::cout << FileName << " cannot be opened!" << endl;
	}
	return DataIn;
}

Then, I can read the .txt file by calling the ReadSystemData function (let’s say I want to read the file bus.txt):

string FileDir = "The_path_where_I_save_my_system_data/"; // Please make sure the value of FileDir ends with a slash. 
InputData BusData = ReadSystemData(FileDir, "bus.txt");

How do I convert the data into usable variables?

We need to first define the columns in the bus.txt file. Let’s say it has two columns, the first one is the bus number and the second one is the peak load, then:

int BusCol = 2;

Then we could know how many rows there are in the bus.txt file, because we know the total bus data length is BusData.Len:

int BusRow = BusData.Len / BusCol; // total number of buses

Then I will define a variable to receive the data of the peak load at each bus:

double * BusLoad = new double[BusRow];
for (int i = 0; i<BusRow; i++) {
	int j = i * BusCol;
	BusLoad[i] = BusData.Vec[j + 1];
}

Then I could use BusLoad when I define my constraints.

==================Another Method to Convert Data====================

Some data files can be converted into a matrix directly. For example, let’s we have a scenario data file (scen.txt), it includes a matrix of 7 columns and 24 rows. Each column is a scenario and each row is an hour. This data can be received by one variable as a matrix directly and we don’t need to separate each column into a variable.

To do so, first, I define a global function to convert the input data into a matrix:

double ** Vec2Mat(vector<double> DataVec, int Row, int Col)
{
	double ** DataMat = new double*[Row];
	for (int i = 0; i<Row; i++)
	{
		DataMat[i] = new double[Col];
		for (int j = 0; j<Col; j++)
		DataMat[i][j] = DataVec[i*Col+j];
	}
	return DataMat;
}

Then I read the data:

string FileDir = "The_path_where_I_save_my_system_data/"; // Please make sure the value of FileDir ends with a slash. 
InputData ScenData = ReadSystemData(FileDir, "scen.txt");

Then I call the function to convert the data into a matrix:

ScenRow = 24;
ScenCol = 7;
double ** Scen = Vec2Mat(ScenData.Vec, ScenRow, ScenCol);