/*
 * Copyright (C) 2008-2012 Delft University of Technology - All rights reserved.
 *
 * Author: Reijer Idema
 */

#include <string>				// USES string
#include <iostream>				// USES cout
#include <cstdlib>				// USES EXIT_SUCCESS, EXIT_FAILURE
#include "PowerFlower/PowerFlower.h"		// USES PowerFlower
#include "PowerFlower/PowerSystemExt.h"		// USES PowerSystemExt
#include "PowerSystem/Interface/MatPowerInterface.h"	// USES MatPowerInterface

using namespace std;
POWERFLOWER_NAMESPACE_USING

int main(int argc, char **argv)
{
	// Initialize PowerFlower framework (initializes PETSc)
	if (!PowerFlower::Initialize(argc, argv, NULL))
	{
		cout << "Failed to initialize PowerFlower framework." << endl;
		PowerFlower::Finalize();
		return EXIT_FAILURE;
	}
	cout << "PowerFlower framework initialized." << endl;

	// Solve power system data
	PowerFlower pf;

	cout << "Reading power system data." << endl;
	if (!pf.ReadPowerSystemFromOptions())
	{
		cout << "Failed." << endl;
	}
	else
	{
		cout << "Success." << endl;

		cout << "Solving power flow problem..." << endl;
		if (!pf.PowerFlow())
		{
			cout << "Failed." << endl;
		}
		else
		{
			cout << "Success." << endl;
		}

		cout << "Running contingency analysis..." << endl;
		pf.ContingencyAnalysis();
	}

	// Finalize PowerFlower framework (clears active objects that require PETSc for clearing and finalizes PETSc)
	PowerFlower::Finalize();
	cout << "PowerFlower framework finalized." << endl;
	cout << "Done." << endl;
	return EXIT_SUCCESS;
}
