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

/*
 * Power System Bus class source.
 */
#include <sstream>		// USES stringstream
#include "Bus.h"

POWERSYSTEM_NAMESPACE_BEGIN

/////////////////////////////// PUBLIC ///////////////////////////////////////

//============================= LIFECYCLE ====================================

Bus::Bus(void) :
	mName(""), mType(msLoadBusId),
	mActivePowerDemand(0), mReactivePowerDemand(0),
	mActivePowerGeneration(0), mReactivePowerGeneration(0),
	mShuntConductance(0), mShuntSusceptance(0),
	mVoltageAngle(0), mVoltageMagnitude(0), mBaseKiloVoltage(0) { }

//============================= OPERATIONS ===================================

std::string Bus::ToString(void) const
{
	std::stringstream ss;
	ss << *this;
    return ss.str();
}

//============================= ACCESS     ===================================

std::string Bus::GetName(void) const { return mName; }
std::string Bus::SetName(const std::string& rName) { return (mName = rName); }

bool Bus::IsLoadType(void) const { return (mType == msLoadBusId); }
bool Bus::IsGeneratorType(void) const { return (mType == msGeneratorBusId); }
bool Bus::IsSlackType(void) const { return (mType == msSlackBusId); }

void Bus::SetLoadType(void) { mType = msLoadBusId; }
void Bus::SetGeneratorType(void) { mType = msGeneratorBusId; }
void Bus::SetSlackType(void) { mType = msSlackBusId; }

double Bus::GetActivePowerDemand(void) const { return mActivePowerDemand; }
double Bus::SetActivePowerDemand(double val) { return (mActivePowerDemand = val); }

double Bus::GetReactivePowerDemand(void) const { return mReactivePowerDemand; }
double Bus::SetReactivePowerDemand(double val) { return (mReactivePowerDemand = val); }

double Bus::GetActivePowerGeneration(void) const { return mActivePowerGeneration; }
double Bus::SetActivePowerGeneration(double val) { return (mActivePowerGeneration = val); }

double Bus::GetReactivePowerGeneration(void) const { return mReactivePowerGeneration; }
double Bus::SetReactivePowerGeneration(double val) { return (mReactivePowerGeneration = val); }

double Bus::GetShuntConductance(void) const { return mShuntConductance; }
double Bus::SetShuntConductance(double val) { return (mShuntConductance = val); }

double Bus::GetShuntSusceptance(void) const { return mShuntSusceptance; }
double Bus::SetShuntSusceptance(double val) { return (mShuntSusceptance = val); }

double Bus::GetVoltageAngle(void) const { return mVoltageAngle; }
double Bus::SetVoltageAngle(double val) { return (mVoltageAngle = val); }

double Bus::GetVoltageMagnitude(void) const { return mVoltageMagnitude; }
double Bus::SetVoltageMagnitude(double val) { return (mVoltageMagnitude = val); }

double Bus::GetBaseKiloVoltage(void) const { return mBaseKiloVoltage; }
double Bus::SetBaseKiloVoltage(double val) { return (mBaseKiloVoltage = val); }

/////////////////////////////// NON-MEMBER ///////////////////////////////////

std::ostream& operator<<(std::ostream& rOutput, const Bus& rBus)
{
	std::string sep = "\t";		// tap seperator
	rOutput << rBus.GetName() << sep;
	rOutput << (rBus.IsLoadType() ? "L" : (rBus.IsGeneratorType() ? "G" : "S")) << sep;
	rOutput << rBus.GetVoltageMagnitude() << sep << rBus.GetVoltageAngle() << sep;
	rOutput << rBus.GetActivePowerDemand() << sep << rBus.GetReactivePowerDemand() << sep;
	rOutput << rBus.GetActivePowerGeneration() << sep << rBus.GetReactivePowerGeneration() << sep;
	rOutput << rBus.GetShuntConductance() << sep << rBus.GetShuntSusceptance() << sep;
	rOutput << rBus.GetBaseKiloVoltage();
	return rOutput;
}

POWERSYSTEM_NAMESPACE_END
