|
3 | 3 | #include <iomanip> |
4 | 4 | #include <iostream> |
5 | 5 |
|
| 6 | +#include <LinearAlgebra/SparsityPattern/Variable.hpp> |
6 | 7 | #include <Model/PhasorDynamics/Bus/Bus.hpp> |
7 | 8 | #include <Model/PhasorDynamics/Bus/BusInfinite.hpp> |
8 | 9 | #include <Model/PhasorDynamics/Load/Load.hpp> |
@@ -65,6 +66,58 @@ namespace GridKit |
65 | 66 |
|
66 | 67 | return success.report(__func__); |
67 | 68 | } |
| 69 | + |
| 70 | + TestOutcome jacobian() |
| 71 | + { |
| 72 | + TestStatus success = true; |
| 73 | + |
| 74 | + real_type R{2.0}; ///< Load resistance |
| 75 | + real_type X{4.0}; ///< Load reactance |
| 76 | + |
| 77 | + Sparse::Variable Vr{10.0}; ///< Bus real voltage |
| 78 | + Sparse::Variable Vi{20.0}; ///< Bus imaginary voltage |
| 79 | + |
| 80 | + Vr.setVariableNumber(0); ///< Independent variables: first |
| 81 | + Vi.setVariableNumber(1); ///< Independent variables: second |
| 82 | + |
| 83 | + PhasorDynamics::BusInfinite<Sparse::Variable, IdxT> bus(Vr, Vi); |
| 84 | + |
| 85 | + PhasorDynamics::Load<Sparse::Variable, IdxT> load(&bus, R, X); |
| 86 | + load.evaluateResidual(); ///< Computes the residual and the Jacobian values by tracking |
| 87 | + ///< the dependencies |
| 88 | + |
| 89 | + std::vector<Sparse::Variable> residuals{bus.Ir(), bus.Ii()}; |
| 90 | + std::vector<Sparse::Variable::DependencyMap> ref = analyticalJacobian(R, X); |
| 91 | + |
| 92 | + /// Compare dependencies computed automatically to the ones computed analytically |
| 93 | + for (size_t i = 0; i < residuals.size(); ++i) |
| 94 | + { |
| 95 | + Sparse::Variable res = residuals[i]; |
| 96 | + const Sparse::Variable::DependencyMap& dependencies = res.getDependencies(); |
| 97 | + success *= (GridKit::Testing::isEqual(dependencies, ref[i])); |
| 98 | + } |
| 99 | + |
| 100 | + return success.report(__func__); |
| 101 | + } |
| 102 | + private: |
| 103 | + std::vector<Sparse::Variable::DependencyMap> analyticalJacobian(const real_type R, |
| 104 | + const real_type X) |
| 105 | + { |
| 106 | + const real_type b = -X / (R * R + X * X); |
| 107 | + const real_type g = R / (R * R + X * X); |
| 108 | + |
| 109 | + real_type dIr_dVr = -g; |
| 110 | + real_type dIr_dVi = -b; |
| 111 | + |
| 112 | + real_type dIi_dVr = b; |
| 113 | + real_type dIi_dVi = -g; |
| 114 | + |
| 115 | + std::vector<Sparse::Variable::DependencyMap> dependencies(2); |
| 116 | + dependencies[0] = {{0, dIr_dVr}, {1, dIr_dVi}}; |
| 117 | + dependencies[1] = {{0, dIi_dVr}, {1, dIi_dVi}}; |
| 118 | + |
| 119 | + return dependencies; |
| 120 | + } |
68 | 121 | }; |
69 | 122 |
|
70 | 123 | } // namespace Testing |
|
0 commit comments