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

/**
 * @file Branch.h
 * Power System Branch class header.
 */
#ifndef PowerSystem_Branch_h
#define PowerSystem_Branch_h

#include <iostream>				// USES ostream
#include "PowerSystem.def"

POWERSYSTEM_NAMESPACE_BEGIN

/**
 * Power System Branch class.
 * Implements the power system Branch class, describing a line or transformer
 * between a "from bus" and a "to bus" in the power system.
 */
class Branch
{
public:
	// TYPES

	typedef unsigned int BusIndexType;		///< Type of the from and to bus indices

	// LIFECYCLE

	Branch(void);

	// OPERATIONS

	/**
	 * Writes the branch data to a string.
	 * @return string with branch data.
	 */
	std::string ToString(void) const;

	// ACCESS

	/**
	 * Checks whether the branch is in service or not.
	 * @return \c true if in service, \c false if not.
	 */
	bool IsActive(void) const;

	/**
	 * Sets the branch to be in service.
	 */
	void Activate(void);

	/**
	 * Sets the branch to be out of service.
	 */
	void Deactivate(void);

	/**
	 * Gets the bus index of the from bus.
	 * @return from bus index.
	 */
	BusIndexType GetFromBus(void) const;

	/**
	 * Sets the from bus to a given bus index.
	 * @param i bus index of the from bus.
	 * @return set bus index of the from bus.
	 */
	BusIndexType SetFromBus(BusIndexType i);

	/**
	 * Gets the bus index of the to bus.
	 * @return to bus index.
	 */
	BusIndexType GetToBus(void) const;

	/**
	 * Sets the to bus to a given bus index.
	 * @param i bus index of the to bus.
	 * @return set bus index of the to bus.
	 */
	BusIndexType SetToBus(BusIndexType i);

	/**
	 * Gets the branch resistance (R) in p.u.
	 * @return branch resistance.
	 */
	double GetResistance(void) const;

	/**
	 * Sets the branch resistance (R) in p.u.
	 * @param val branch resistance to set.
	 * @return set branch resistance.
	 */
	double SetResistance(double val);

	/**
	 * Gets the branch reactance (X) in p.u.
	 * @return branch reactance.
	 */
	double GetReactance(void) const;

	/**
	 * Sets the branch reactance (X) in p.u.
	 * @param val branch reactance to set.
	 * @return set branch reactance.
	 */
	double SetReactance(double val);

	/**
	 * Gets the branch susceptance (B) in p.u.
	 * @return branch susceptance.
	 */
	double GetSusceptance(void) const;

	/**
	 * Sets the branch susceptance (B) in p.u.
	 * @param val branch susceptance to set.
	 * @return set branch susceptance.
	 */
	double SetSusceptance(double val);

	/**
	 * Gets the branch transformer turns ratio.
	 * A ratio of 0 can be used to define that the branch is not a transformer, and is thus treated as a ratio of 1.
	 * @return branch transformer ratio.
	 */
	double GetRatio(void) const;

	/**
	 * Sets the branch transformer turns ratio.
	 * A ratio of 0 can be used to define that the branch is not a transformer, and is thus treated as a ratio of 1.
	 * @param val branch transformer ratio to set.
	 * @return set branch transformer ratio.
	 */
	double SetRatio(double val);

	/**
	 * Gets the branch transformer angle in radians.
	 * @return branch transformer angle.
	 */
	double GetAngle(void) const;

	/**
	 * Sets the branch transformer angle in radians.
	 * @param val branch transformer angle to set.
	 * @return set branch transformer angle.
	 */
	double SetAngle(double val);

private:
	bool mIsActive;			///< \c true if line is in service, \c false if out of service.
	BusIndexType mFromBus;		///< Bus index of the from bus.
	BusIndexType mToBus;		///< Bus index of the to bus.
	double mResistance;			///< Line resistance (R) in p.u.
	double mReactance;			///< Line reactance (X) in p.u.
	double mSusceptance;		///< Line susceptance (B) in p.u.
	double mRatio;				///< Transformer turns ratio (0 for lines) between the from bus and to bus.
	double mAngle;				///< Transformer phase shift angle in radians between the from bus and to bus.
};

// NON-MEMBER
/**
 * Writes branch data to an output stream.
 * @param rOutput output stream to write the data to.
 * @param rBranch branch to write data from.
 * @return output stream to which the data was written.
 */
std::ostream& operator<<(std::ostream& rOutput, const Branch& rBranch);

POWERSYSTEM_NAMESPACE_END

#endif // PowerSystem_Branch_h
