Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update topology to not necessarily go through sparse mapping #426

Merged
merged 37 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a984196
add comments, add test, add wip on build_dense_mapping
Laurynas-Jagutis Nov 13, 2023
6316d18
Merge remote-tracking branch 'origin/main' into feature/update-topology
Laurynas-Jagutis Nov 13, 2023
a66b989
work in progress
Laurynas-Jagutis Nov 13, 2023
7ebe6f3
fix dense mapping indvector and reorder
Laurynas-Jagutis Nov 23, 2023
1830797
Merge remote-tracking branch 'origin/main' into feature/update-topology
Laurynas-Jagutis Nov 23, 2023
a63d8ec
fix narrowing conversion
Laurynas-Jagutis Nov 23, 2023
404c3a5
remove assertion
Laurynas-Jagutis Nov 23, 2023
ac073bb
comment out an assert
Laurynas-Jagutis Nov 23, 2023
7a3e00b
Merge branch 'main' into feature/update-topology
mgovers Nov 24, 2023
e77692c
update test, combine two loops
Laurynas-Jagutis Nov 24, 2023
3b0ae2e
Merge branch 'feature/update-topology' of https://github.com/PowerGri…
Laurynas-Jagutis Nov 24, 2023
dee666b
Merge remote-tracking branch 'origin/main' into feature/update-topology
Laurynas-Jagutis Nov 24, 2023
6dccb02
wip
Laurynas-Jagutis Nov 28, 2023
c924777
finished the new sorting, works well
Laurynas-Jagutis Nov 29, 2023
0984153
setup if statement, count operations
Laurynas-Jagutis Nov 30, 2023
2f89787
Merge remote-tracking branch 'origin/main' into feature/update-topology
Laurynas-Jagutis Nov 30, 2023
084e463
benchmarks
Laurynas-Jagutis Dec 7, 2023
0d6dcf8
Merge remote-tracking branch 'origin/main' into feature/update-topology
Laurynas-Jagutis Dec 7, 2023
a6404da
almost finished full script
Laurynas-Jagutis Dec 13, 2023
771a20f
finish script, obtain results
Laurynas-Jagutis Dec 14, 2023
601a344
reverse the idxvector
Laurynas-Jagutis Dec 15, 2023
43a1531
Merge remote-tracking branch 'origin/main' into feature/update-topology
Laurynas-Jagutis Dec 15, 2023
2c85fce
hardcode, remove benchmark, remove comments
Laurynas-Jagutis Dec 15, 2023
9dce76c
cast a value to Idx
Laurynas-Jagutis Dec 15, 2023
17e878d
possible fixes for failing checks
Laurynas-Jagutis Dec 15, 2023
70c876b
possible fixes for failing checks2
Laurynas-Jagutis Dec 15, 2023
3f39f43
create a test for comparison sort
Laurynas-Jagutis Dec 15, 2023
c3daedd
create a second test
Laurynas-Jagutis Dec 15, 2023
225a6a9
Merge branch 'main' into feature/update-topology
mgovers Dec 18, 2023
ab30c76
resolve comments
mgovers Dec 19, 2023
194195a
resolve remaining comments
mgovers Dec 19, 2023
aa36ef0
implement do not go via sparse when dense mapping
mgovers Dec 19, 2023
ed83f7f
cleanup
mgovers Dec 19, 2023
34b3644
fix
mgovers Dec 19, 2023
9393ab1
iota not correctly implemented in libc++/11
mgovers Dec 19, 2023
bbfda49
fix
mgovers Dec 19, 2023
1fa03ce
Merge branch 'main' into feature/update-topology
mgovers Dec 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define POWER_GRID_MODEL_SPARSE_MAPPING_HPP

#include "power_grid_model.hpp"
using namespace std;
Laurynas-Jagutis marked this conversation as resolved.
Show resolved Hide resolved

/*
Sparse mapping technique
Expand Down Expand Up @@ -50,35 +51,82 @@ struct SparseMapping {
IdxVector reorder;
};

// input from the test: (const idx_B_in_A{3, 5, 2, 1, 1, 2}, 7)
inline SparseMapping build_sparse_mapping(IdxVector const& idx_B_in_A, Idx const n_B) {
// n_A becomes the size of idx_B_in_A, thus it is 6
auto const n_A = static_cast<Idx>(idx_B_in_A.size());
using SparseEntry = std::pair<Idx, Idx>;
// entries vector with size n_A
std::vector<SparseEntry> entries(n_A);
// idx_B_in_A{3, 5, 2, 1, 1, 2}, then count = {0, 1, 2, 3, 4, 5} since count is affected by the length of
// idx_B_in_A j_B is an element from idx_B_in_A, and i_A is the current value from IdxCount{0} returns
// entries[{0,3},{1,5},{2,2},{3,1},{4,1},{5,2}]
std::transform(idx_B_in_A.cbegin(), idx_B_in_A.cend(), IdxCount{0}, entries.begin(), [](Idx j_B, Idx i_A) {
return SparseEntry{i_A, j_B};
});
// initialize result
SparseMapping sparse_mapping;
// size of indpntr will become 8
sparse_mapping.indptr.resize(n_B + 1);
// size of reorder will be 6
sparse_mapping.reorder.resize(n_A);
// counting sort column, keep row as sorted
IdxVector counter(n_B, 0);
// counter = {n_Bx0} = {0, 0, 0, 0, 0, 0, 0}
IdxVector counter(n_B, 0); // amount of load gens connected to each bus
// For every entry, ex. {0,3}, we add 1 to counter at entry.second place in this case 3rd, so it becomes {0, 0, 0,
// 1, 0, 0, 0} counter should become {0, 2, 2, 1, 0, 1, 0}
for (auto const& entry : entries) {
++counter[entry.second];
}
// accumulate value to indptr
// [a, b, c, d], the inclusive scan would produce [a, a+b, a+b+c, a+b+c+d]
// counter = {0, 2, 2, 1, 0, 1, 0}, indptr becomes {0, 0, 2, 4, 5, 5, 6, 6}
std::inclusive_scan(counter.cbegin(), counter.cend(), sparse_mapping.indptr.begin() + 1);
// copy back
// copy everything except the indptr[0] to counter.
// counter becomes {0, 2, 4, 5, 5, 6, 6}
std::copy(sparse_mapping.indptr.cbegin() + 1, sparse_mapping.indptr.cend(), counter.begin());
// sort
// crbegin() points to the element at the end of the container
// crend() points to the position before the first element
// at this point: entries[{0,3},{1,5},{2,2},{3,1},{4,1},{5,2}], counter {0, 2, 4, 5, 5, 6, 6}
// Reverse order the first entry in entries: {5,2}, it_entry->second = 2
// counter[2] is 4, decremented to 3(counter becomes {0, 2, 3, 5, 5, 6, 6}), thus sparse_mapping.reorder[3] is set
// to it_entry->first which is 5. sparse_mapping.reorder = {0, 0, 0, 5, 0, 0},
// Final result: sparse_mapping.reorder = {3, 4, 2, 5, 0, 1}
for (auto it_entry = entries.crbegin(); it_entry != entries.crend(); ++it_entry) {
sparse_mapping.reorder[--counter[it_entry->second]] = it_entry->first;
}
// the first is the index before reordering of load_gen
// it entry second is the index of the bus
// reorder at the end contains for each new index of the load gen the old index of the load gen in a linear way
assert(sparse_mapping.indptr[0] == 0);
assert(sparse_mapping.indptr.back() == n_A);
return sparse_mapping;
}

} // namespace power_grid_model
struct DenseMapping {
IdxVector indvector;
IdxVector reorder;
};

inline DenseMapping build_dense_mapping(IdxVector const& idx_B_in_A, Idx const n_B) {
auto const n_A = static_cast<Idx>(idx_B_in_A.size());

DenseMapping dense_mapping;

dense_mapping.indvector.resize(n_A);
// size of reorder will be 6
dense_mapping.reorder.resize(n_A);

IdxVector vec = idx_B_in_A;

sort(vec.begin(), vec.end());
Laurynas-Jagutis marked this conversation as resolved.
Show resolved Hide resolved

dense_mapping.indvector = vec;

return dense_mapping;
}

} // namespace power_grid_model
mgovers marked this conversation as resolved.
Show resolved Hide resolved
#endif
8 changes: 8 additions & 0 deletions tests/cpp_unit_tests/test_sparse_mapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ TEST_CASE("Test sparse mapping") {
CHECK(mapping.reorder == mapping_2.reorder);
}

TEST_CASE("Test dense mapping") {
IdxVector const idx_B_in_A{3, 5, 2, 1, 1, 2};
DenseMapping mapping{{1, 1, 2, 2, 3, 5}, {3, 4, 2, 5, 0, 1}};
DenseMapping mapping_2 = build_dense_mapping(idx_B_in_A, 7);

CHECK(mapping.indvector == mapping_2.indvector);
}

} // namespace power_grid_model