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

/**
 * @file PowerSystemExt.h
 * Extended Power System class header.
 */
#ifndef PowerFlower_PowerSystemExt_h
#define PowerFlower_PowerSystemExt_h

#include <string>						// USES string
#include <list>							// HASA list
#include "../PetscUtils/PetscUtils.h"	// HASA PetscErrorCode
#include "../PowerSystem/PowerSystem.h"	// ISA PowerSystem
#include "PowerFlower.def"

POWERFLOWER_NAMESPACE_BEGIN

// FORWARD DECLARATIONS
class LogEvents;
class AdmittanceMatrix;
class PowerVector;
class VoltageVector;

/**
 * Extended Power System class.
 * Implements the extended power system class.
 * Adds an admittance matrix and power vector to the power system model,
 * which are updated with operations that change the power system.
 */
class PowerSystemExt : public POWERSYSTEM_NAMESPACE_NAME::PowerSystem
{
public:

	typedef PowerSystem::Interface Interface;		///< Type of the input/output interface.
	typedef PowerSystem::BusType BusType;			///< Type of the busses.
	typedef PowerSystem::BranchType BranchType;		///< Type of the branches.
	typedef PowerSystem::BusIndexType BusIndexType;					///< Type of bus indices.
	typedef PowerSystem::BranchIndexType BranchIndexType;			///< Type of branch indices.
	typedef PowerSystem::ConstBranchIterator ConstBranchIterator;	///< Type of the const branch iterator to const branch.

	// LIFECYCLE
	PowerSystemExt(void);
	PowerSystemExt(const LogEvents* pLogEvents);
	~PowerSystemExt(void);

	// OPERATIONS
	void Clear(void);
	bool Read(const std::string& rSourceName, Interface& rInterface);
	std::string ToString(void);

	bool BuildAdmittanceMatrix(void);
	bool BuildPowerVector(void);
	bool BuildVoltageVector(void);

	// ACCESS
	double SetBasePower(double val);
	bool AddBus(const BusType& rBus);
	bool SetBus(BusIndexType i, const BusType& rBus);
	bool IsConnected(BusIndexType i, BusIndexType j) const;
	bool AddBranch(const BranchType& rBranch);
	bool RemoveBranch(ConstBranchIterator& rBranchIterator);
	bool ActivateBranch(const ConstBranchIterator& rBranchIterator);
	bool DeactivateBranch(const ConstBranchIterator& rBranchIterator);
	BranchIndexType RemoveInactiveBranches(void);	// rebuilds admittance matrix if it existed

	const AdmittanceMatrix* GetAdmittanceMatrix(void) const { return mpAdmittanceMatrix; }
	const PowerVector* GetPowerVector(void) const { return mpPowerVector; }
	const VoltageVector* GetVoltageVector(void) const { return mpVoltageVector; }

	// TEMPORARY
	void Test(void);

private:
	static const std::string msErrorBasePowerUpdate;
	static const std::string msErrorBusUpdate;
	static const std::string msErrorBranchUpdate;
	PetscErrorCode mErr;					///< Most recent PETSc error code.
	const LogEvents* mpLogEvents;			///< PETSc log events.

	AdmittanceMatrix* mpAdmittanceMatrix;	///< Admittance matrix.
	PowerVector* mpPowerVector;				///< Power vector.
	VoltageVector* mpVoltageVector;			///< Voltage vector.
};

POWERFLOWER_NAMESPACE_END

#endif // PowerFlower_PowerSystemExt_h
