Skip to content

Commit d6acd4d

Browse files
committed
revise based on comments
Signed-off-by: Tony Xiang <[email protected]>
1 parent f1b7cdb commit d6acd4d

File tree

7 files changed

+31
-22
lines changed

7 files changed

+31
-22
lines changed

include/power_grid_model/math_solver/iterative_current_pf_solver.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class IterativeCurrentPFSolver : public IterativePFSolver<sym, IterativeCurrentP
171171
// Solve the linear equations I_inj = YU
172172
// inplace
173173
void solve_matrix() {
174-
sparse_solver_.solve(*mat_data_, *perm_, rhs_u_, rhs_u_);
174+
sparse_solver_.solve_with_prefactorization(*mat_data_, *perm_, rhs_u_, rhs_u_);
175175
}
176176

177177
// Find maximum deviation in voltage among all buses

include/power_grid_model/math_solver/iterative_linear_se_solver.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ class IterativeLinearSESolver {
621621
prepare_rhs(y_bus, measured_values, output.u);
622622
// solve with prefactorization
623623
sub_timer = Timer(calculation_info, 2225, "Solve sparse linear equation (pre-factorized)");
624-
sparse_solver_.solve((std::vector<SEGainBlock<sym>> const&)data_gain_, perm_, x_rhs_, x_rhs_);
624+
sparse_solver_.solve_with_prefactorization(data_gain_, perm_, x_rhs_, x_rhs_);
625625
sub_timer = Timer(calculation_info, 2226, "Iterate unknown");
626626
max_dev = iterate_unknown(output.u, measured_values.has_angle_measurement());
627627
}

include/power_grid_model/math_solver/linear_pf_solver.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class LinearPFSolver {
105105
// solve
106106
// u vector will have I_injection for slack bus for now
107107
sub_timer = Timer(calculation_info, 2222, "Solve sparse linear equation");
108-
sparse_solver_.solve(mat_data_, perm_, output.u, output.u);
108+
sparse_solver_.prefactorize_and_solve(mat_data_, perm_, output.u, output.u);
109109

110110
// calculate math result
111111
sub_timer = Timer(calculation_info, 2223, "Calculate Math Result");

include/power_grid_model/math_solver/newton_raphson_pf_solver.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class NewtonRaphsonPFSolver : public IterativePFSolver<sym, NewtonRaphsonPFSolve
363363

364364
// Solve the linear Equations
365365
void solve_matrix() {
366-
sparse_solver_.solve(data_jac_, perm_, del_x_pq_, del_x_pq_);
366+
sparse_solver_.prefactorize_and_solve(data_jac_, perm_, del_x_pq_, del_x_pq_);
367367
}
368368

369369
// Get maximum deviation among all bus voltages

include/power_grid_model/math_solver/sparse_lu_solver.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct sparse_lu_entry_trait<Tensor, RHSVector, XVector, enable_tensor_lu_t<Tens
6464
struct BlockPerm {
6565
typename LUFactor::PermutationPType p;
6666
typename LUFactor::PermutationQType q;
67-
}; //Extract permutation matrices p and q from LUFactor
67+
}; // Extract permutation matrices p and q from LUFactor
6868
using BlockPermArray = std::vector<BlockPerm>;
6969
};
7070

@@ -90,18 +90,19 @@ class SparseLUSolver {
9090
}
9191

9292
// solve with new matrix data, need to factorize first
93-
void solve(std::vector<Tensor>& data, // matrix data, factorize in-place
94-
BlockPermArray& block_perm_array, // pre-allocated permutation array, will be overwritten
95-
std::vector<RHSVector> const& rhs, std::vector<XVector>& x) {
93+
void prefactorize_and_solve(
94+
std::vector<Tensor>& data, // matrix data, factorize in-place
95+
BlockPermArray& block_perm_array, // pre-allocated permutation array, will be overwritten
96+
std::vector<RHSVector> const& rhs, std::vector<XVector>& x) {
9697
prefactorize(data, block_perm_array);
9798
// call solve with const method
98-
solve((std::vector<Tensor> const&)data, block_perm_array, rhs, x);
99+
solve_with_prefactorization((std::vector<Tensor> const&)data, block_perm_array, rhs, x);
99100
}
100101

101102
// solve with existing pre-factorization
102-
void solve(std::vector<Tensor> const& data, // pre-factoirzed data, const ref
103-
BlockPermArray const& block_perm_array, // pre-calculated permutation, const ref
104-
std::vector<RHSVector> const& rhs, std::vector<XVector>& x) {
103+
void solve_with_prefactorization(std::vector<Tensor> const& data, // pre-factoirzed data, const ref
104+
BlockPermArray const& block_perm_array, // pre-calculated permutation, const ref
105+
std::vector<RHSVector> const& rhs, std::vector<XVector>& x) {
105106
// local reference
106107
auto const& row_indptr = *row_indptr_;
107108
auto const& col_indices = *col_indices_;
@@ -333,7 +334,7 @@ class SparseLUSolver {
333334

334335
private:
335336
Idx size_;
336-
Idx nnz_;
337+
Idx nnz_; // number of non zeroes (in block)
337338
std::shared_ptr<IdxVector const> row_indptr_;
338339
std::shared_ptr<IdxVector const> col_indices_;
339340
std::shared_ptr<IdxVector const> diag_lu_;

tests/cpp_unit_tests/test_sparse_lu_solver.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ TEST_CASE("Test Sparse LU solver") {
4646
auto col_indices = std::make_shared<IdxVector const>(IdxVector{0, 1, 2, 0, 1, 2, 0, 1, 2});
4747
auto diag_lu = std::make_shared<IdxVector const>(IdxVector{0, 4, 8});
4848

49-
SECTION("Test scalar(double) calculation") {
49+
SECTION("Scalar(double) calculation") {
5050
// [4 1 5 3 21
5151
// 3 7 f * [-1] = [ 2 ]
5252
// 2 f 6] 2 18
@@ -62,23 +62,31 @@ TEST_CASE("Test Sparse LU solver") {
6262
SparseLUSolver<double, double, double>::BlockPermArray block_perm{};
6363

6464
SECTION("Test calculation") {
65-
solver.solve(data, block_perm, rhs, x);
65+
solver.prefactorize_and_solve(data, block_perm, rhs, x);
6666
check_result(x, x_ref);
6767
}
6868

6969
SECTION("Test (pseudo) singular") {
7070
data[0] = 0.0;
71-
CHECK_THROWS_AS(solver.solve(data, block_perm, rhs, x), SparseMatrixError);
71+
CHECK_THROWS_AS(solver.prefactorize_and_solve(data, block_perm, rhs, x), SparseMatrixError);
7272
}
7373

7474
SECTION("Test prefactorize") {
7575
solver.prefactorize(data, block_perm);
76-
solver.solve((std::vector<double> const&)data, block_perm, rhs, x);
76+
solver.solve_with_prefactorization((std::vector<double> const&)data, block_perm, rhs, x);
7777
check_result(x, x_ref);
7878
}
79+
80+
SECTION("Data is prefactorized by solve") {
81+
auto prefactorized_data = data;
82+
auto prefactorized_block_perm = block_perm;
83+
solver.prefactorize(prefactorized_data, prefactorized_block_perm);
84+
solver.prefactorize_and_solve(data, block_perm, rhs, x);
85+
CHECK(prefactorized_data == data);
86+
}
7987
}
8088

81-
SECTION("Test block(double 2*2) calculation") {
89+
SECTION("Block(double 2*2) calculation") {
8290
// [ 0 1 1 2 3 4 3 38
8391
// 100 0 7 -1 5 6 4 356
8492
// 1 2 0 200 f f * [ -1 ] = [ -389 ]
@@ -105,17 +113,17 @@ TEST_CASE("Test Sparse LU solver") {
105113
SparseLUSolver<Tensor, Array, Array>::BlockPermArray block_perm(3);
106114

107115
SECTION("Test calculation") {
108-
solver.solve(data, block_perm, rhs, x);
116+
solver.prefactorize_and_solve(data, block_perm, rhs, x);
109117
check_result(x, x_ref);
110118
}
111119
SECTION("Test (pseudo) singular") {
112120
data[0](0, 1) = 0.0;
113-
CHECK_THROWS_AS(solver.solve(data, block_perm, rhs, x), SparseMatrixError);
121+
CHECK_THROWS_AS(solver.prefactorize_and_solve(data, block_perm, rhs, x), SparseMatrixError);
114122
}
115123

116124
SECTION("Test prefactorize") {
117125
solver.prefactorize(data, block_perm);
118-
solver.solve((std::vector<Tensor> const&)data, block_perm, rhs, x);
126+
solver.solve_with_prefactorization((std::vector<Tensor> const&)data, block_perm, rhs, x);
119127
check_result(x, x_ref);
120128
}
121129
}

tests/cpp_unit_tests/test_y_bus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ TEST_CASE("Test one bus system") {
211211
TEST_CASE("Test fill-in y bus") {
212212
/*
213213
* struct
214-
* [1] --0--> [0] --[1]--> [2]
214+
* [1] --0--> [0] --1--> [2]
215215
* extra fill-in: (1, 2) by removing node 0
216216
*
217217
* [

0 commit comments

Comments
 (0)