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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
- Added `HYGOV` governor model implementation for PhasorDynamics.
- Added `REPCA` controller model implementation for PhasorDynamics.
- Added `REECB` electrical-control model implementation for PhasorDynamics.
- Implemented `tagDifferentiable()` for `PowerElectronics` models.

## v0.1

Expand Down
2 changes: 2 additions & 0 deletions GridKit/Model/PowerElectronics/Capacitor/Capacitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int Capacitor<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are differentials
tag_.resize(size_, true);
return 0;
}

Expand Down
32 changes: 16 additions & 16 deletions GridKit/Model/PowerElectronics/CircuitComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ namespace GridKit
*/
int allocate() override
{
jacobian_coo_rows_ = std::make_unique<IdxT[]>(static_cast<size_t>(nnz_));
jacobian_coo_cols_ = std::make_unique<IdxT[]>(static_cast<size_t>(nnz_));
jacobian_coo_values_ = std::make_unique<RealT[]>(static_cast<size_t>(nnz_));
jacobian_coo_rows_ = std::make_unique<IdxT[]>(nnz_);
jacobian_coo_cols_ = std::make_unique<IdxT[]>(nnz_);
jacobian_coo_values_ = std::make_unique<RealT[]>(nnz_);

y_ext_ = std::make_unique<const ScalarT*[]>(static_cast<size_t>(size_));
yp_ext_ = std::make_unique<const ScalarT*[]>(static_cast<size_t>(size_));
f_ext_ = std::make_unique<ScalarT*[]>(static_cast<size_t>(size_));
connection_nodes_ = std::make_unique<IdxT[]>(static_cast<size_t>(size_));
y_ext_ = std::make_unique<const ScalarT*[]>(size_);
yp_ext_ = std::make_unique<const ScalarT*[]>(size_);
f_ext_ = std::make_unique<ScalarT*[]>(size_);
connection_nodes_ = std::make_unique<IdxT[]>(size_);

if (!allocated_)
{
Expand Down Expand Up @@ -250,7 +250,7 @@ namespace GridKit
{
assert(rows.size() == cols.size());
assert(rows.size() == vals.size());
assert(current_jac_size_ + rows.size() <= static_cast<size_t>(nnz_));
assert(current_jac_size_ + rows.size() <= nnz_);

for (size_t i = 0; i < rows.size(); i++)
{
Expand All @@ -265,22 +265,22 @@ namespace GridKit
public:
IdxT size() final
{
return size_;
return static_cast<IdxT>(size_);
}

IdxT size() const
{
return size_;
return static_cast<IdxT>(size_);
}

IdxT nnz() final
{
return nnz_;
return static_cast<IdxT>(nnz_);
}

IdxT nnz() const
{
return nnz_;
return static_cast<IdxT>(nnz_);
}

IdxT sizeQuadrature() final
Expand Down Expand Up @@ -491,11 +491,11 @@ namespace GridKit

protected:
/// The number of variables in this component. Should be equal to `n_extern_` plus `n_intern_`. \see size()
IdxT size_{0};
size_t size_{0};
/// The number of nonzero elements in this component's Jacobian. \see nnz()
IdxT nnz_{0};
IdxT size_quad_{0};
IdxT size_opt_{0};
size_t nnz_{0};
IdxT size_quad_{0};
IdxT size_opt_{0};

// COO Jacobian buffers
std::unique_ptr<IdxT[]> jacobian_coo_rows_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "DistributedGenerator.hpp"

#include <cmath>
#include <iostream>
#include <vector>

namespace GridKit
Expand Down Expand Up @@ -74,6 +73,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int DistributedGenerator<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are differentials
tag_.resize(size_, true);
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int InductionMotor<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are differentials
tag_.resize(size_, true);
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions GridKit/Model/PowerElectronics/Inductor/Inductor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int Inductor<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are differentials
tag_.resize(size_, true);
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int LinearTransformer<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are differentials
tag_.resize(size_, true);
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int MicrogridBusDQ<ScalarT, IdxT>::evaluateInternalResidual()
{
// No internal variables - tag doesn't matter.
tag_.resize(size_, false);
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int MicrogridLine<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are differentials
tag_.resize(size_, true);
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int MicrogridLoad<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are differentials
tag_.resize(size_, true);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion GridKit/Model/PowerElectronics/NodeBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ namespace GridKit
allocateVectors(static_cast<IdxT>(size));
}

tag_.resize(size);
tag_.resize(size, false);
variable_indices_.resize(size);
residual_indices_.resize(size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int SynchronousMachine<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are differentials
tag_.resize(size_, true);
return 0;
}

Expand Down
37 changes: 35 additions & 2 deletions GridKit/Model/PowerElectronics/SystemModelPowerElectronics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <algorithm>
#include <cassert>
#include <format>
#include <vector>

#include <GridKit/Constants.hpp>
Expand All @@ -12,6 +13,8 @@
#include <GridKit/Model/PowerElectronics/NodeBase.hpp>
#include <GridKit/ScalarTraits.hpp>

#include "GridKit/Utilities/Logger/Logger.hpp"

namespace GridKit
{
template <class ScalarT, typename IdxT>
Expand Down Expand Up @@ -149,8 +152,6 @@ namespace GridKit
abs_tol_.setToZero(memory::HOST);
}

tag_.resize(size_);

{ // Start node internal indexing after all component internals for proper KLU ordering
size_t node_internal_idx = component_internal_size;
for (node_type* node : nodes_)
Expand Down Expand Up @@ -295,6 +296,38 @@ namespace GridKit

int tagDifferentiable() final
{
for (size_t i = 0; i < components_.size(); i++)
{
component_type* component = components_[i];

if (int err = component->tagDifferentiable())
{
return err;
}

if (component->tag().size() != component->size())
{
GridKit::Utilities::Logger::error() << std::format("Component {} has an ill-configured tag(). Expected tags for {} variables, got {} instead.\n", i, component->size(), component->tag().size());
return 1;
}
}

tag_.resize(size_, false);

size_t idx = 0;
for (component_type* comp : components_)
{
const auto& external_indices = comp->getExternIndices();
for (IdxT i = 0; i < comp->size(); i++)
{
if (!external_indices.contains(i))
{
tag_[idx] = comp->tag()[i];
idx++;
}
}
}

return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int TransmissionLine<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are algebraics
tag_.resize(size_, false);
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace GridKit
template <class ScalarT, typename IdxT>
int VoltageSource<ScalarT, IdxT>::tagDifferentiable()
{
// All variables are algebraics
tag_.resize(size_, false);
return 0;
}

Expand Down
23 changes: 23 additions & 0 deletions examples/PowerElectronics/Microgrid/Microgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,29 @@ int main(int /* argc */, char const** /* argv */)
sysmodel->getCsrJacobian()->print();
}

sysmodel->tagDifferentiable();

bool all_internal_diff = true;
bool all_external_alg = true;

const size_t num_node_vars = 4 * 2 + 1;

for (size_t i = 0; i < sysmodel->size() - num_node_vars; i++)
{
all_internal_diff = all_internal_diff && sysmodel->tag()[i];
if (!sysmodel->tag()[i])
{
std::cout << "Broken on " << i << '\n';
}
}

for (size_t i = sysmodel->size() - num_node_vars; i < sysmodel->size(); i++)
{
all_external_alg = all_external_alg && !sysmodel->tag()[i];
}

std::cout << "Verify all internal variables are differential: " << all_internal_diff << ", and all external variabels are algebraic: " << all_external_alg << '\n';

// Create numerical integrator and configure it for the generator model
auto* idas = new AnalysisManager::Sundials::Ida<double, size_t>(sysmodel);

Expand Down
Loading