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
16 changes: 8 additions & 8 deletions GridKit/Model/PhasorDynamics/Branch/BranchEnzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ namespace GridKit
ScalarT,
IdxT>::eval(this,
static_cast<size_t>(bus1_->size()),
static_cast<size_t>((bus1_->y()).size()),
static_cast<size_t>(bus1_->size()),
(bus1_->getResidualIndices()).data(),
(bus1_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus1_->y()).data(),
bus1_->yData(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
Expand All @@ -56,12 +56,12 @@ namespace GridKit
ScalarT,
IdxT>::eval(this,
static_cast<size_t>(bus2_->size()),
static_cast<size_t>((bus2_->y()).size()),
static_cast<size_t>(bus2_->size()),
(bus2_->getResidualIndices()).data(),
(bus2_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus2_->y()).data(),
bus2_->yData(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
Expand All @@ -73,12 +73,12 @@ namespace GridKit
ScalarT,
IdxT>::eval(this,
static_cast<size_t>(bus1_->size()),
static_cast<size_t>((bus2_->y()).size()),
static_cast<size_t>(bus2_->size()),
(bus1_->getResidualIndices()).data(),
(bus2_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus2_->y()).data(),
bus2_->yData(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
Expand All @@ -91,12 +91,12 @@ namespace GridKit
ScalarT,
IdxT>::eval(this,
static_cast<size_t>(bus2_->size()),
static_cast<size_t>((bus1_->y()).size()),
static_cast<size_t>(bus1_->size()),
(bus2_->getResidualIndices()).data(),
(bus1_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus1_->y()).data(),
bus1_->yData(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
Expand Down
4 changes: 1 addition & 3 deletions GridKit/Model/PhasorDynamics/Bus/BusImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ namespace GridKit
size_t size = static_cast<size_t>(size_);

// Resize component model data
f_.resize(size);
y_.resize(size);
yp_.resize(size);
this->allocateState(size);
tag_.resize(size);
variable_indices_.resize(size);
residual_indices_.resize(size);
Expand Down
2 changes: 1 addition & 1 deletion GridKit/Model/PhasorDynamics/BusFault/BusFaultEnzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace GridKit
(bus_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus_->y()).data(),
bus_->yData(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace GridKit
(bus_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus_->y()).data(),
bus_->yData(),
ws_.data(),
J_rows_buffer_,
J_cols_buffer_,
Expand Down
4 changes: 1 addition & 3 deletions GridKit/Model/PhasorDynamics/Exciter/IEEET1/Ieeet1Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ namespace GridKit
{
// Resize component model data
auto size = static_cast<size_t>(size_); // avoid compiler warnings
f_.resize(size);
y_.resize(size);
yp_.resize(size);
this->allocateState(size);
tag_.resize(size);
variable_indices_.resize(size);
residual_indices_.resize(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace GridKit
(bus_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus_->y()).data(),
bus_->yData(),
ws_.data(),
J_rows_buffer_,
J_cols_buffer_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ namespace GridKit
int SexsPti<ScalarT, IdxT>::allocate()
{
auto size = static_cast<size_t>(size_);
f_.resize(size);
y_.resize(size);
yp_.resize(size);
this->allocateState(size);
tag_.resize(size);
variable_indices_.resize(size);
residual_indices_.resize(size);
Expand Down
4 changes: 1 addition & 3 deletions GridKit/Model/PhasorDynamics/Governor/Tgov1/Tgov1Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ namespace GridKit
{
// Allocate local component data
auto size = static_cast<size_t>(size_); // avoid compiler warnings
f_.resize(size);
y_.resize(size);
yp_.resize(size);
this->allocateState(size);
tag_.resize(size);
variable_indices_.resize(size);
residual_indices_.resize(size);
Expand Down
130 changes: 121 additions & 9 deletions GridKit/Model/PhasorDynamics/GridElement.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cassert>
#include <vector>

#include <GridKit/AutomaticDifferentiation/DependencyTracking/Variable.hpp>
Expand All @@ -14,6 +15,89 @@ namespace GridKit
{
using Log = ::GridKit::Utilities::Logger;

/// Owns state storage for standalone elements, or aliases a SystemModel-owned global slice.
/// The owned storage remains the std::vector returned through Evaluator; the data accessors
/// are the live state used by component implementations.
template <class ScalarT>
class StateVector
{
public:
using StorageT = std::vector<ScalarT>;
using size_type = typename StorageT::size_type;

StateVector() = default;

// Non-copyable/movable: data_ aliases either storage_ or a global slice, and a GridElement
// (which owns these) is never copied or moved -- it is held by raw pointer in SystemModel.
StateVector(const StateVector&) = delete;
StateVector& operator=(const StateVector&) = delete;
StateVector(StateVector&&) = delete;
StateVector& operator=(StateVector&&) = delete;

template <typename SizeT>
void resize(SizeT size)
{
const auto sz = static_cast<size_type>(size);
if (viewing_)
{
assert(sz == size_);
return;
}

storage_.resize(sz);
data_ = storage_.data();
size_ = storage_.size();
}

void view(ScalarT* data, size_type size)
{
data_ = data;
size_ = size;
viewing_ = true;
}

ScalarT* data()
{
return data_;
}

const ScalarT* data() const
{
return data_;
}

size_type size() const
{
return size_;
}

ScalarT& operator[](size_type i)
{
return data_[i];
}

const ScalarT& operator[](size_type i) const
{
return data_[i];
}

StorageT& storage()
{
return storage_;
}

const StorageT& storage() const
{
return storage_;
}

private:
StorageT storage_;
ScalarT* data_{nullptr};
size_type size_{0};
bool viewing_{false};
};

/**
* @brief Model base class for all system constituents
*/
Expand Down Expand Up @@ -65,24 +149,26 @@ namespace GridKit
msa = max_steps_;
}

// Evaluator requires std::vector references. These return owned storage; when an element is
// bound into a SystemModel, live state is accessed through y_/yp_/f_ instead.
std::vector<ScalarT>& y() override
{
return y_;
return y_.storage();
}

const std::vector<ScalarT>& y() const override
{
return y_;
return y_.storage();
}

std::vector<ScalarT>& yp() override
{
return yp_;
return yp_.storage();
}

const std::vector<ScalarT>& yp() const override
{
return yp_;
return yp_.storage();
}

std::vector<bool>& tag() override
Expand All @@ -97,12 +183,31 @@ namespace GridKit

std::vector<ScalarT>& getResidual() override
{
return f_;
return f_.storage();
}

const std::vector<ScalarT>& getResidual() const override
{
return f_;
return f_.storage();
}

ScalarT* yData()
{
return y_.data();
}

const ScalarT* yData() const
{
return y_.data();
}

/// Bind this element's y/yp/f to its slice of the SystemModel's global vectors, so the
/// element reads and writes the global state in place (no scatter/gather).
void bindGlobalState(ScalarT* y, ScalarT* yp, ScalarT* f, std::size_t n)
{
y_.view(n == 0 ? nullptr : y, n);
yp_.view(n == 0 ? nullptr : yp, n);
f_.view(n == 0 ? nullptr : f, n);
}

MatrixT& getJacobian() override
Expand Down Expand Up @@ -148,17 +253,24 @@ namespace GridKit
}

protected:
void allocateState(std::size_t size)
{
f_.resize(size);
y_.resize(size);
yp_.resize(size);
}

IdxT size_{0};
IdxT nnz_{0};
/// Global (system-level) variable indices
std::vector<IdxT> variable_indices_;
/// Global (system-level) residual indices
std::vector<IdxT> residual_indices_;

std::vector<ScalarT> y_;
std::vector<ScalarT> yp_;
StateVector<ScalarT> y_;
StateVector<ScalarT> yp_;
StateVector<ScalarT> f_;
std::vector<bool> tag_;
std::vector<ScalarT> f_;
std::vector<ScalarT> g_;

MatrixT J_;
Expand Down
2 changes: 1 addition & 1 deletion GridKit/Model/PhasorDynamics/Load/LoadEnzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace GridKit
(bus_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus_->y()).data(),
bus_->yData(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
Expand Down
4 changes: 1 addition & 3 deletions GridKit/Model/PhasorDynamics/Load/LoadImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ namespace GridKit
// std::cout << "Allocate Load..." << std::endl;

auto size = static_cast<size_t>(size_); // avoid compiler warnings
f_.resize(size);
y_.resize(size);
yp_.resize(size);
this->allocateState(size);
tag_.resize(size);
variable_indices_.resize(size);
residual_indices_.resize(size);
Expand Down
2 changes: 1 addition & 1 deletion GridKit/Model/PhasorDynamics/LoadZIP/LoadZIPEnzyme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace GridKit
(bus_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus_->y()).data(),
bus_->yData(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
Expand Down
4 changes: 1 addition & 3 deletions GridKit/Model/PhasorDynamics/LoadZIP/LoadZIPImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ namespace GridKit
// std::cout << "Allocate Load..." << std::endl;

auto size = static_cast<size_t>(size_); // avoid compiler warnings
f_.resize(size);
y_.resize(size);
yp_.resize(size);
this->allocateState(size);
tag_.resize(size);
variable_indices_.resize(size);
residual_indices_.resize(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ namespace GridKit
int Ieeest<ScalarT, IdxT>::allocate()
{
auto size = static_cast<size_t>(size_);
f_.resize(size);
y_.resize(size);
yp_.resize(size);
this->allocateState(size);
tag_.resize(size);
variable_indices_.resize(size);
residual_indices_.resize(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace GridKit
(bus_->getVariableIndices()).data(),
y_.data(),
yp_.data(),
(bus_->y()).data(),
bus_->yData(),
J_rows_buffer_,
J_cols_buffer_,
J_vals_buffer_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,7 @@ namespace GridKit
{
// Resize component model data
auto size = static_cast<size_t>(size_);
f_.resize(size);
y_.resize(size);
yp_.resize(size);
this->allocateState(size);
tag_.resize(size);
variable_indices_.resize(size);
residual_indices_.resize(size);
Expand Down
Loading
Loading