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
1 change: 1 addition & 0 deletions GridKit/LinearAlgebra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ add_subdirectory(DenseMatrix)
install(
FILES
${CMAKE_CURRENT_SOURCE_DIR}/MemoryUtils.hpp
${CMAKE_CURRENT_SOURCE_DIR}/Vector.hpp
DESTINATION
include/GridKit/LinearAlgebra)
119 changes: 119 additions & 0 deletions GridKit/LinearAlgebra/Vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#pragma once

#include <cassert>
#include <cstddef>
#include <stdexcept>
#include <vector>

namespace GridKit
{
namespace LinearAlgebra
{
/**
* @brief Owns vector storage or aliases externally-owned contiguous data.
*
* Templated on ScalarT to hold AD types, so it cannot reuse Re::Solve's
* Vector directly. storage() exists only to satisfy
* the std::vector&-based Model::Evaluator interface.
*
* Future work: change Evaluator's state accessors (y/yp/getResidual/tag) to
* return Vector<ScalarT>&, which removes storage() and lets SUNDIALS use
* data()/size() directly. Finalize this public API before it becomes an
* interface type.
*/
template <class ScalarT>
class Vector
{
public:
using StorageT = std::vector<ScalarT>;
using size_type = typename StorageT::size_type;

Vector() = default;

Vector(const Vector&) = delete;
Vector& operator=(const Vector&) = delete;
Vector(Vector&&) = delete;
Vector& operator=(Vector&&) = delete;

void resize(size_type size)
{
if (!owns_data_)
{
if (size != size_)
{
throw std::runtime_error("Cannot resize externally-owned vector data");
}
return;
}

storage_.resize(size);
data_ = storage_.empty() ? nullptr : storage_.data();
size_ = storage_.size();
}

void setDataPointer(ScalarT* data, size_type size)
{
if (data == nullptr && size != 0)
{
throw std::runtime_error("Cannot set nonzero vector data to nullptr");
}

data_ = data;
size_ = size;
owns_data_ = false;
storage_.clear();
}

ScalarT* data()
{
return data_;
}

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

size_type size() const
{
return size_;
}

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

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

StorageT& storage()
{
if (!owns_data_)
{
throw std::runtime_error("Vector storage is externally owned");
}
return storage_;
}

const StorageT& storage() const
{
if (!owns_data_)
{
throw std::runtime_error("Vector storage is externally owned");
}
return storage_;
}

private:
StorageT storage_;
ScalarT* data_{nullptr};
size_type size_{0};
bool owns_data_{true};
};
} // namespace LinearAlgebra
} // namespace GridKit
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
55 changes: 46 additions & 9 deletions GridKit/Model/PhasorDynamics/GridElement.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once

#include <cstddef>
#include <vector>

#include <GridKit/AutomaticDifferentiation/DependencyTracking/Variable.hpp>
#include <GridKit/CommonMath.hpp>
#include <GridKit/LinearAlgebra/Vector.hpp>
#include <GridKit/Model/Evaluator.hpp>
#include <GridKit/Utilities/Errors.hpp>
#include <GridKit/Utilities/Logger/Logger.hpp>
Expand All @@ -23,6 +25,7 @@ namespace GridKit
public:
using RealT = typename Model::Evaluator<ScalarT, IdxT>::RealT;
using MatrixT = typename Model::Evaluator<ScalarT, IdxT>::MatrixT;
using VectorT = LinearAlgebra::Vector<ScalarT>;

GridElement()
: size_{0}
Expand Down Expand Up @@ -67,22 +70,22 @@ namespace GridKit

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 +100,39 @@ 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)
{
if (n == 0)
{
y_.setDataPointer(nullptr, 0);
yp_.setDataPointer(nullptr, 0);
f_.setDataPointer(nullptr, 0);
return;
}

y_.setDataPointer(y, n);
yp_.setDataPointer(yp, n);
f_.setDataPointer(f, n);
}

MatrixT& getJacobian() override
Expand Down Expand Up @@ -148,17 +178,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_;
VectorT y_;
VectorT yp_;
VectorT 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
Loading
Loading