Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GENROU With Jacobian #18

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ComponentLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@

add_subdirectory(PowerFlow)
add_subdirectory(PowerElectronics)
add_subdirectory(DynamicPhasor)

5 changes: 5 additions & 0 deletions ComponentLib/DynamicPhasor/Bus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gridkit_add_library(slackbus
SOURCES
SlackBus.cpp
OUTPUT_NAME
gridkit_slackbus)
100 changes: 100 additions & 0 deletions ComponentLib/DynamicPhasor/Bus/PhasorBus.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* Paul Moon 6/26/2024 */

#ifndef _PHASOR_BUS_HPP_
#define _PHASOR_BUS_HPP_

#include <ModelEvaluatorImpl.hpp>

namespace ModelLib
{
/*!
* @brief Base class for all dynamic phasor buses.
*
* Derived bus types:
* 0 - swing bus (Vr and Vi are constants)
* 1 - PV bus (P and V are constants)
* 2 - PQ bus (P and Q are constants)
*
* @todo Consider static instead of dynamic polymorphism for
* bus types. Create Bus class that takes template parameter
* BusType.
*/
template <class ScalarT, typename IdxT>
class PhasorBus : public ModelEvaluatorImpl<ScalarT, IdxT>
{
protected:
using ModelEvaluatorImpl<ScalarT, IdxT>::size_;
using ModelEvaluatorImpl<ScalarT, IdxT>::nnz_;
using ModelEvaluatorImpl<ScalarT, IdxT>::time_;
using ModelEvaluatorImpl<ScalarT, IdxT>::alpha_;
using ModelEvaluatorImpl<ScalarT, IdxT>::rtol_;
using ModelEvaluatorImpl<ScalarT, IdxT>::atol_;
using ModelEvaluatorImpl<ScalarT, IdxT>::y_;
using ModelEvaluatorImpl<ScalarT, IdxT>::yp_;
using ModelEvaluatorImpl<ScalarT, IdxT>::tag_;
using ModelEvaluatorImpl<ScalarT, IdxT>::f_;
using ModelEvaluatorImpl<ScalarT, IdxT>::g_;
using ModelEvaluatorImpl<ScalarT, IdxT>::yB_;
using ModelEvaluatorImpl<ScalarT, IdxT>::ypB_;
using ModelEvaluatorImpl<ScalarT, IdxT>::fB_;
using ModelEvaluatorImpl<ScalarT, IdxT>::gB_;
using ModelEvaluatorImpl<ScalarT, IdxT>::param_;
using ModelEvaluatorImpl<ScalarT, IdxT>::param_up_;
using ModelEvaluatorImpl<ScalarT, IdxT>::param_lo_;

public:
typedef typename ModelEvaluatorImpl<ScalarT, IdxT>::real_type real_type;

enum BusType{PQ=1, PV, Slack, Isolated};

PhasorBus(IdxT id) : busID_(id) {}
virtual ~PhasorBus(){}

// Set defaults for ModelEvaluator methods
virtual int allocate() { return 0;}
virtual int initialize() { return 0;}
virtual int tagDifferentiable() { return 0;}
virtual int evaluateResidual() { return 0;}
virtual int evaluateJacobian() { return 0;}
virtual int evaluateIntegrand() { return 0;}

virtual int initializeAdjoint() { return 0;}
virtual int evaluateAdjointResidual() { return 0;}
//virtual int evaluateAdjointJacobian() { return 0;}
virtual int evaluateAdjointIntegrand() { return 0;}
virtual void updateTime(real_type, real_type) {} // <- throw exception here

// Pure virtual methods specific to Bus types
virtual ScalarT& Vr() = 0;
virtual const ScalarT& Vr() const = 0;
virtual ScalarT& Vi() = 0;
virtual const ScalarT& Vi() const = 0;
virtual ScalarT& P() = 0;
virtual const ScalarT& P() const = 0;
virtual ScalarT& Q() = 0;
virtual const ScalarT& Q() const = 0;

virtual ScalarT& lambdaP() = 0;
virtual const ScalarT& lambdaP() const = 0;
virtual ScalarT& lambdaQ() = 0;
virtual const ScalarT& lambdaQ() const = 0;
virtual ScalarT& PB() = 0;
virtual const ScalarT& PB() const = 0;
virtual ScalarT& QB() = 0;
virtual const ScalarT& QB() const = 0;

virtual const int BusType() const = 0;

virtual const IdxT BusID() const
{
return busID_;
}

protected:
const IdxT busID_;
}; // class PhasorBus

} // namespace ModelLib


#endif // _PHASOR_BUS_HPP_
87 changes: 87 additions & 0 deletions ComponentLib/DynamicPhasor/Bus/SlackBus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* Created by Paul Moon 6/26/2024 */
#include <iostream>
#include <cmath>
#include "SlackBus.hpp"

namespace ModelLib {

/*!
* @brief Constructor for a slack bus
*
* Arguments passed to ModelEvaluatorImpl:
* - Number of equations = 0 (size_)
* - Number of variables = 0 (size_)
* - Number of quadratures = 0
* - Number of optimization parameters = 0
*/
template <class ScalarT, typename IdxT>
SlackBus<ScalarT, IdxT>::SlackBus()
: PhasorBus<ScalarT, IdxT>(0), Vr_(0.0), Vi_(0.0), P_(0.0), Q_(0.0), PB_(0.0), QB_(0.0)
{
//std::cout << "Create SlackBus..." << std::endl;
//std::cout << "Number of equations is " << size_ << std::endl;

size_ = 0;
}

/*!
* @brief SlackBus constructor.
*
* Arguments passed to ModelEvaluatorImpl:
* - Number of equations = 0 (size_)
* - Number of variables = 0 (size_)
* - Number of quadratures = 0
* - Number of optimization parameters = 0
*/
template <class ScalarT, typename IdxT>
SlackBus<ScalarT, IdxT>::SlackBus(ScalarT Vr, ScalarT Vi)
: PhasorBus<ScalarT, IdxT>(0), Vr_(Vr), Vi_(Vi), P_(0.0), Q_(0.0), PB_(0.0), QB_(0.0)
{
//std::cout << "Create SlackBus..." << std::endl;
//std::cout << "Number of equations is " << size_ << std::endl;
P() = 0.0;
Q() = 0.0;
size_ = 0;
}

template <class ScalarT, typename IdxT>
SlackBus<ScalarT, IdxT>::SlackBus(BusData& data)
: PhasorBus<ScalarT, IdxT>(data.bus_i), Vr_(data.Vm*cos(data.Va)), Vi_(data.Vm*sin(data.Va))
{
//std::cout << "Create SlackBus..." << std::endl;
//std::cout << "Number of equations is " << size_ << std::endl;
P() = 0.0;
Q() = 0.0;
size_ = 0;
}

template <class ScalarT, typename IdxT>
SlackBus<ScalarT, IdxT>::~SlackBus()
{
}

template <class ScalarT, typename IdxT>
int SlackBus<ScalarT, IdxT>::evaluateResidual()
{
// std::cout << "Evaluating residual of a slack bus ...\n";
P() = 0.0;
Q() = 0.0;
return 0;
}

template <class ScalarT, typename IdxT>
int SlackBus<ScalarT, IdxT>::evaluateAdjointResidual()
{
PB() = 0.0;
QB() = 0.0;
return 0;
}


// Available template instantiations
template class SlackBus<double, long int>;
template class SlackBus<double, size_t>;


} // namespace ModelLib

146 changes: 146 additions & 0 deletions ComponentLib/DynamicPhasor/Bus/SlackBus.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* Created by Paul Moon 6/26/2024 */
#ifndef _SLACK_BUS_HPP_
#define _SLACK_BUS_HPP_

#include <PowerSystemData.hpp>
#include "PhasorBus.hpp"

namespace ModelLib
{
/*!
* @brief Implementation of a slack bus.
*
* Slack bus sets voltage _V_ and phase _theta_ as constants.
* Active and reactive power, _P_ and _Q_, are component model outputs,
* but are computed outside the SlackBus class.
*
*
*/
template <class ScalarT, typename IdxT>
class SlackBus : public PhasorBus<ScalarT, IdxT>
{
using PhasorBus<ScalarT, IdxT>::size_;
using PhasorBus<ScalarT, IdxT>::y_;
using PhasorBus<ScalarT, IdxT>::yp_;
using PhasorBus<ScalarT, IdxT>::f_;
using PhasorBus<ScalarT, IdxT>::g_;
using PhasorBus<ScalarT, IdxT>::atol_;
using PhasorBus<ScalarT, IdxT>::rtol_;

public:
using real_type = typename ModelEvaluatorImpl<ScalarT, IdxT>::real_type;
using BusData = GridKit::PowerSystemData::BusData<real_type, IdxT>;

SlackBus();
SlackBus(ScalarT Vr, ScalarT Vi);
SlackBus(BusData& data);
virtual ~SlackBus();
virtual int evaluateResidual();
virtual int evaluateAdjointResidual();

/// @todo Should slack bus allow changing voltage?
virtual ScalarT& Vr()
{
return Vr_;
}

virtual const ScalarT& Vr() const
{
return Vr_;
}

/// @todo Should slack bus allow changing phase?
virtual ScalarT& Vi()
{
return Vi_;
}

virtual const ScalarT& Vi() const
{
return Vi_;
}

virtual ScalarT& P()
{
return P_;
}

virtual const ScalarT& P() const
{
return P_;
}

virtual ScalarT& Q()
{
return Q_;
}

virtual const ScalarT& Q() const
{
return Q_;
}

/// @todo Should slack bus allow changing voltage?
virtual ScalarT& lambdaP()
{
return thetaB_;
}

virtual const ScalarT& lambdaP() const
{
return thetaB_;
}

/// @todo Should slack bus allow changing phase?
virtual ScalarT& lambdaQ()
{
return VB_;
}

virtual const ScalarT& lambdaQ() const
{
return VB_;
}

virtual ScalarT& PB()
{
return PB_;
}

virtual const ScalarT& PB() const
{
return PB_;
}

virtual ScalarT& QB()
{
return QB_;
}

virtual const ScalarT& QB() const
{
return QB_;
}

virtual const int BusType() const
{
return PhasorBus<ScalarT, IdxT>::BusType::Slack;
}

private:
ScalarT Vr_;
ScalarT Vi_;
ScalarT P_;
ScalarT Q_;

ScalarT VB_;
ScalarT thetaB_;
ScalarT PB_;
ScalarT QB_;

}; // class SlackBus

} // namespace ModelLib


#endif // _SLACK_BUS_HPP_
3 changes: 3 additions & 0 deletions ComponentLib/DynamicPhasor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_subdirectory(SynchronousMachine)
add_subdirectory(Governor)
add_subdirectory(Bus)
5 changes: 5 additions & 0 deletions ComponentLib/DynamicPhasor/Governor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gridkit_add_library(governor
SOURCES
Governor.cpp
OUTPUT_NAME
gridkit_governor)
Loading