Skip to content
Open
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
5 changes: 3 additions & 2 deletions GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ if(GRIDKIT_ENABLE_ENZYME)
ClangEnzymeFlags
COMPILE_OPTIONS
PRIVATE
-mllvm
-enzyme-auto-sparsity=1
"SHELL:-mllvm -enzyme-auto-sparsity=1"
# Preserve runtime-order stores for Enzyme sparsification.
"SHELL:-mllvm -simplifycfg-sink-common=false"
-fno-math-errno)
else()
gridkit_add_library(
Expand Down
89 changes: 35 additions & 54 deletions GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/Ieeest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@

#pragma once

#include <cstddef>
#include <memory>
#include <vector>
Comment thread
nkoukpaizan marked this conversation as resolved.

#include <GridKit/Definitions.hpp>
#include <GridKit/Model/PhasorDynamics/Component.hpp>
#include <GridKit/Model/PhasorDynamics/ComponentSignals.hpp>
#include <GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/IeeestData.hpp>
#include <GridKit/Model/VariableMonitor.hpp>

namespace GridKit
{
namespace PhasorDynamics
{
namespace Stabilizer
{
template <typename real_type, typename index_type>
struct IeeestData;
} // namespace Stabilizer

template <typename scalar_type, typename index_type>
class SignalNode;

} // namespace PhasorDynamics
} // namespace GridKit

namespace GridKit
{
namespace PhasorDynamics
{
namespace Stabilizer
{
/// Internal variables of `Ieeest`
Expand Down Expand Up @@ -60,38 +53,38 @@ namespace GridKit
template <typename scalar_type, typename index_type>
class Ieeest : public Component<scalar_type, index_type>
{
using Component<scalar_type, index_type>::gridkit_component_id_;
using Component<scalar_type, index_type>::abs_tol_;
using Component<scalar_type, index_type>::allocated_;
using Component<scalar_type, index_type>::alpha_;
using Component<scalar_type, index_type>::f_;
using Component<scalar_type, index_type>::gridkit_component_id_;
using Component<scalar_type, index_type>::J_cols_buffer_;
using Component<scalar_type, index_type>::J_rows_buffer_;
using Component<scalar_type, index_type>::J_vals_buffer_;
using Component<scalar_type, index_type>::nnz_;
using Component<scalar_type, index_type>::residual_indices_;
using Component<scalar_type, index_type>::size_;
using Component<scalar_type, index_type>::tag_;
using Component<scalar_type, index_type>::abs_tol_;
using Component<scalar_type, index_type>::time_;
using Component<scalar_type, index_type>::variable_indices_;
using Component<scalar_type, index_type>::wb_;
using Component<scalar_type, index_type>::y_;
using Component<scalar_type, index_type>::yp_;
using Component<scalar_type, index_type>::wb_;
using Component<scalar_type, index_type>::h_;
using Component<scalar_type, index_type>::J_rows_buffer_;
using Component<scalar_type, index_type>::J_cols_buffer_;
using Component<scalar_type, index_type>::J_vals_buffer_;
using Component<scalar_type, index_type>::variable_indices_;
using Component<scalar_type, index_type>::residual_indices_;
using Component<scalar_type, index_type>::allocated_;

public:
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = typename Component<ScalarT, IdxT>::RealT;
using ModelDataT = IeeestData<RealT, IdxT>;
using SignalT = SignalNode<ScalarT, IdxT>;
using MonitorT = Model::VariableMonitor<Ieeest, IeeestData>;
using ScalarT = scalar_type;
using IdxT = index_type;
using RealT = typename Component<ScalarT, IdxT>::RealT;
using SignalT = SignalNode<ScalarT, IdxT>;
using ModelDataT = IeeestData<RealT, IdxT>;
using MonitorT = Model::VariableMonitor<Ieeest, IeeestData>;
using InternalVariablesT = IeeestInternalVariables;
using ExternalVariablesT = IeeestExternalVariables;

Ieeest();
Ieeest(const ModelDataT& data);
explicit Ieeest(const ModelDataT& data);
~Ieeest();

int setGridKitComponentID(IdxT) override final;
int setGridKitComponentID(IdxT component_id) override final;
int allocate() override final;
int verify() const override final;
int initialize() override final;
Expand All @@ -100,7 +93,6 @@ namespace GridKit
int evaluateResidual() override final;
int evaluateJacobian() override final;

/// Get the `ComponentSignals` from this `Ieeest`
auto getSignals()
-> ComponentSignals<ScalarT,
IdxT,
Expand All @@ -120,6 +112,12 @@ namespace GridKit
ScalarT*);

private:
void initializeParameters(const ModelDataT& data);
void initializeMonitor();
void setDerivedParameters();

static constexpr RealT TIME_CONSTANT_MINIMUM = static_cast<RealT>(1.0e-3);

RealT A1_{0};
RealT A2_{0};
RealT A3_{0};
Expand All @@ -139,34 +137,17 @@ namespace GridKit
RealT Vcu_{0};
RealT Tdelay_{0};

RealT a0_{1};
IdxT order_{0};

RealT a1_{0};
RealT a2_{0};
RealT a3_{0};
RealT a4_{0};

// Precomputed masks and safe inverse coefficients for branch-free degenerate paths.
RealT use_notch_{0};
RealT bypass_notch_{1};
RealT use_4th_order_{0};
RealT use_3rd_order_{0};
RealT use_2nd_order_{0};
RealT safe_inv_a4_{0};
RealT safe_inv_a3_{0};
RealT safe_inv_a2_{0};
RealT use_T2_block_{1};
RealT bypass_T2_block_{0};
RealT use_T4_block_{1};
RealT bypass_T4_block_{0};
RealT use_T6_block_{1};
RealT bypass_T6_block_{0};
IdxT parameter_error_count_{0};

ComponentSignals<ScalarT, IdxT, IeeestInternalVariables, IeeestExternalVariables> signals_;

std::unique_ptr<MonitorT> monitor_;

void initializeParameters(const ModelDataT& data);
void initializeMonitor();
std::unique_ptr<MonitorT> monitor_;

std::vector<ScalarT> ws_;
std::vector<IdxT> ws_indices_;
Expand Down
98 changes: 43 additions & 55 deletions GridKit/Model/PhasorDynamics/Stabilizer/IEEEST/IeeestEnzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ namespace GridKit
{
namespace Stabilizer
{
/**
* @brief Jacobian evaluation experimental
*
* @tparam ScalarT - Scalar data type
* @tparam IdxT - Index data type
* @return int - error code, 0 = success
*/
template <typename scalar_type, typename index_type>
int Ieeest<scalar_type, index_type>::evaluateJacobian()
{
Expand All @@ -29,9 +22,6 @@ namespace GridKit

if (J_rows_buffer_ == nullptr)
{
// Reserve space for the dense blocks.
// The size of the buffer is the sum of maximum capacities of the blocks.
// Enyme will compute the appropriate nnz from sparsification.
auto size = static_cast<size_t>(size_);
auto signal_size = static_cast<size_t>(ws_.size());
auto buffer_size = 2 * size * size + size * signal_size;
Expand All @@ -40,63 +30,61 @@ namespace GridKit
J_vals_buffer_ = new RealT[buffer_size];
}

using ModelT = GridKit::PhasorDynamics::Stabilizer::Ieeest<scalar_type, index_type>;
using Fn = GridKit::Enzyme::Sparse::MemberFunctions;

nnz_ = 0;

GridKit::Enzyme::Sparse::DfDy<GridKit::PhasorDynamics::Stabilizer::Ieeest<ScalarT, IdxT>,
GridKit::Enzyme::Sparse::MemberFunctions::InternalResidualWithSignal>::eval(this,
static_cast<size_t>(f_.getSize()),
static_cast<size_t>(y_.getSize()),
(this->getResidualIndices()).data(),
(this->getVariableIndices()).data(),
y_.getData(),
yp_.getData(),
wb_.data(),
ws_.data(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
nnz_);
GridKit::Enzyme::Sparse::DfDy<ModelT, Fn::InternalResidualWithSignal>::eval(this,
static_cast<size_t>(f_.getSize()),
static_cast<size_t>(y_.getSize()),
(this->getResidualIndices()).data(),
(this->getVariableIndices()).data(),
y_.getData(),
yp_.getData(),
wb_.data(),
ws_.data(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
nnz_);

GridKit::Enzyme::Sparse::DfDyp<GridKit::PhasorDynamics::Stabilizer::Ieeest<ScalarT, IdxT>,
GridKit::Enzyme::Sparse::MemberFunctions::InternalResidualWithSignal>::eval(this,
static_cast<size_t>(f_.getSize()),
static_cast<size_t>(y_.getSize()),
(this->getResidualIndices()).data(),
(this->getVariableIndices()).data(),
y_.getData(),
yp_.getData(),
wb_.data(),
ws_.data(),
alpha_,
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
nnz_);
GridKit::Enzyme::Sparse::DfDyp<ModelT, Fn::InternalResidualWithSignal>::eval(this,
static_cast<size_t>(f_.getSize()),
static_cast<size_t>(y_.getSize()),
(this->getResidualIndices()).data(),
(this->getVariableIndices()).data(),
y_.getData(),
yp_.getData(),
wb_.data(),
ws_.data(),
alpha_,
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
nnz_);

GridKit::Enzyme::Sparse::DfDws<GridKit::PhasorDynamics::Stabilizer::Ieeest<ScalarT, IdxT>,
GridKit::Enzyme::Sparse::MemberFunctions::InternalResidualWithSignal>::eval(this,
static_cast<size_t>(f_.getSize()),
ws_.size(),
(this->getResidualIndices()).data(),
ws_indices_.data(),
y_.getData(),
yp_.getData(),
wb_.data(),
ws_.data(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
nnz_);
GridKit::Enzyme::Sparse::DfDws<ModelT, Fn::InternalResidualWithSignal>::eval(this,
static_cast<size_t>(f_.getSize()),
ws_.size(),
(this->getResidualIndices()).data(),
ws_indices_.data(),
y_.getData(),
yp_.getData(),
wb_.data(),
ws_.data(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
nnz_);

this->constructCoo();

return 0;
}

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

} // namespace Stabilizer
} // namespace PhasorDynamics
} // namespace GridKit
Loading
Loading