Skip to content

Commit d475911

Browse files
committed
System external/residual calls
1 parent c1febd8 commit d475911

4 files changed

Lines changed: 90 additions & 16 deletions

File tree

‎GridKit/Model/PhasorDynamics/Component.hpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ namespace GridKit
6363

6464
virtual int verify() const = 0;
6565

66+
/**
67+
* @brief Evaluate this component's internal residual.
68+
*
69+
* The default preserves the existing single-phase residual evaluation
70+
* for components that have not yet separated internal and external
71+
* residuals.
72+
*/
73+
virtual int evaluateInternalResidual()
74+
{
75+
return this->evaluateResidual();
76+
}
77+
78+
/**
79+
* @brief Evaluate contributions to residuals owned elsewhere.
80+
*/
81+
virtual int evaluateExternalResidual()
82+
{
83+
return 0;
84+
}
85+
6686
IdxT size() override final
6787
{
6888
return size_;

‎GridKit/Model/PhasorDynamics/SystemModel.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ namespace GridKit
8888

8989
int tagDifferentiable() override;
9090
int setAbsoluteTolerance(RealT rel_tol) override;
91+
int evaluateInternalResidual() override;
92+
int evaluateExternalResidual() override;
9193
int evaluateResidual() override;
9294
int evaluateJacobian() override;
9395

‎GridKit/Model/PhasorDynamics/SystemModelImpl.hpp‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -663,20 +663,13 @@ namespace GridKit
663663
}
664664

665665
/**
666-
* @brief Compute system residual vector
666+
* @brief Evaluate bus residuals and component internal residuals.
667667
*
668-
* Buses and components read and write their bound system-vector slices
669-
* directly.
670-
*
671-
* @warning Residuals must be computed for buses, before component
672-
* residuals are computed. Buses own residuals for currents
673-
* Ir and Ii, but the contributions to these residuals come
674-
* from components. Buses assign their residual values, while components
675-
* add to those values by in-place addition. This is why (for now) bus
676-
* residuals need to be computed first.
668+
* Buses assign their residuals before components evaluate so that legacy
669+
* component contributions continue to accumulate correctly.
677670
*/
678671
template <typename scalar_type, typename index_type>
679-
int SystemModel<scalar_type, index_type>::evaluateResidual()
672+
int SystemModel<scalar_type, index_type>::evaluateInternalResidual()
680673
{
681674
for (const auto& bus : buses_)
682675
{
@@ -685,9 +678,35 @@ namespace GridKit
685678

686679
for (const auto& component : components_)
687680
{
688-
component->evaluateResidual();
681+
component->evaluateInternalResidual();
682+
}
683+
684+
return 0;
685+
}
686+
687+
/**
688+
* @brief Evaluate contributions to residuals owned elsewhere.
689+
*/
690+
template <typename scalar_type, typename index_type>
691+
int SystemModel<scalar_type, index_type>::evaluateExternalResidual()
692+
{
693+
for (const auto& component : components_)
694+
{
695+
component->evaluateExternalResidual();
689696
}
690697

698+
return 0;
699+
}
700+
701+
/**
702+
* @brief Compute and assemble the system residual vector.
703+
*/
704+
template <typename scalar_type, typename index_type>
705+
int SystemModel<scalar_type, index_type>::evaluateResidual()
706+
{
707+
evaluateInternalResidual();
708+
evaluateExternalResidual();
709+
691710
f_.setDataUpdated();
692711

693712
return 0;

‎tests/UnitTests/PhasorDynamics/SystemTests.hpp‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ namespace GridKit
9898
}
9999
};
100100

101+
class ExternalResidualBranch final : public PhasorDynamics::Branch<ScalarT, IdxT>
102+
{
103+
public:
104+
using BranchT = PhasorDynamics::Branch<ScalarT, IdxT>;
105+
using BranchT::BranchT;
106+
107+
int evaluateExternalResidual() override final
108+
{
109+
++external_residual_evaluations_;
110+
return 0;
111+
}
112+
113+
std::size_t externalResidualEvaluations() const
114+
{
115+
return external_residual_evaluations_;
116+
}
117+
118+
private:
119+
std::size_t external_residual_evaluations_{0};
120+
};
121+
101122
public:
102123
SystemTests() = default;
103124
~SystemTests() = default;
@@ -206,12 +227,24 @@ namespace GridKit
206227
PhasorDynamics::Bus<ScalarT, IdxT> bus2(Vr2, Vi2);
207228
system.addBus(&bus2);
208229

209-
PhasorDynamics::Branch<ScalarT, IdxT> branch(&bus1, &bus2, R, X, G, B);
230+
ExternalResidualBranch branch(&bus1, &bus2, R, X, G, B);
210231
system.addComponent(&branch);
211232

212-
system.allocate();
213-
system.initialize();
214-
system.evaluateResidual();
233+
success *= system.allocate() == 0;
234+
success *= system.initialize() == 0;
235+
236+
const auto external_residual_evaluations = branch.externalResidualEvaluations();
237+
238+
success *= system.evaluateResidual() == 0;
239+
success *= branch.externalResidualEvaluations() == external_residual_evaluations + 1;
240+
241+
success *= isEqual(bus1.Ir(), Ir1);
242+
success *= isEqual(bus1.Ii(), Ii1);
243+
success *= isEqual(bus2.Ir(), Ir2);
244+
success *= isEqual(bus2.Ii(), Ii2);
245+
246+
success *= system.evaluateResidual() == 0;
247+
success *= branch.externalResidualEvaluations() == external_residual_evaluations + 2;
215248

216249
success *= isEqual(bus1.Ir(), Ir1);
217250
success *= isEqual(bus1.Ii(), Ii1);

0 commit comments

Comments
 (0)