Skip to content

Commit

Permalink
pass tests of sparse matrix plus, minus, multiply,divide, inverse. ad…
Browse files Browse the repository at this point in the history
…d matrix expand,shrink and change size functions.
  • Loading branch information
changgang committed Mar 11, 2024
1 parent 4861da6 commit 619a2fb
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 53 deletions.
21 changes: 16 additions & 5 deletions code/steps/STEPS.depend
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"header/basic/sparse_matrix_csparse.h"
"header/basic/complex_sparse_matrix_csparse.h"

1710076670 d:\steps\code\steps\header\basic\sparse_matrix_csparse.h
1710130550 d:\steps\code\steps\header\basic\sparse_matrix_csparse.h
"cs.h"
<vector>
<ctime>
Expand Down Expand Up @@ -709,7 +709,7 @@
"header/basic/utility.h"
"header/data_imexporter/psse_imexporter.h"

1708511336 source:d:\steps\code\steps\main_tests.cpp
1710119997 source:d:\steps\code\steps\main_tests.cpp
<istream>
<cstdlib>
<cstring>
Expand Down Expand Up @@ -960,7 +960,7 @@
"header/power_system_database.h"
"header/STEPS.h"

1625794610 d:\steps\code\steps\header\basic\sparse_matrix_test.h
1710137344 d:\steps\code\steps\header\basic\sparse_matrix_test.h
<istream>
<iostream>
"cpptest.h"
Expand Down Expand Up @@ -3211,7 +3211,7 @@
<iostream>
<fstream>

1710079984 source:d:\steps\code\steps\source\basic\sparse_matrix_csparse.cpp
1710130955 source:d:\steps\code\steps\source\basic\sparse_matrix_csparse.cpp
"header/basic/sparse_matrix_csparse.h"
"header/basic/constants.h"
"header/basic/utility.h"
Expand Down Expand Up @@ -3478,7 +3478,7 @@

1574318720 d:\steps\code\thirdparty\suitesparse\umfpack\include\umfpack_global.h

1710076869 source:d:\steps\code\steps\source\basic\sparse_matrix_test.cpp
1710137449 source:d:\steps\code\steps\source\basic\sparse_matrix_test.cpp
"header/basic/test_macro.h"
"header/basic/sparse_matrix_test.h"
"header/basic/utility.h"
Expand Down Expand Up @@ -6550,3 +6550,14 @@
"header/STEPS.h"
<iostream>

1710082082 source:d:\steps\code\steps\main_small_signal_analysis.cpp
<istream>
<iostream>
"header/power_system_database.h"
"header/steps_namespace.h"
"header/toolkit/dynamic_simulator/dynamic_simulator.h"
"header/toolkit/powerflow_solver/powerflow_solver.h"
"header/basic/utility.h"
"header/data_imexporter/psse_imexporter.h"
"header/prepare_for_tests/prepare_models_for_test.h"

7 changes: 6 additions & 1 deletion code/steps/header/basic/sparse_matrix_csparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SPARSE_MATRIX_CSPARSE : public SPARSE_MATRIX
virtual bool matrix_in_triplet_form() const;

virtual void compress_and_merge_duplicate_entries();
void compress_and_merge_cs_matrix(cs* matrix);

virtual void transpose();

Expand Down Expand Up @@ -86,5 +87,9 @@ SPARSE_MATRIX_CSPARSE operator*(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE&
SPARSE_MATRIX_CSPARSE operator/(double b, SPARSE_MATRIX_CSPARSE& A);
SPARSE_MATRIX_CSPARSE inv(SPARSE_MATRIX_CSPARSE&A);
SPARSE_MATRIX_CSPARSE concatenate_matrix_diagnally(vector<SPARSE_MATRIX_CSPARSE*> matrix);
SPARSE_MATRIX_CSPARSE build_identity_matrix(SPARSE_MATRIX_CSPARSE&A);
SPARSE_MATRIX_CSPARSE build_identity_matrix(const SPARSE_MATRIX_CSPARSE&A);
SPARSE_MATRIX_CSPARSE build_identity_matrix(unsigned int n);
SPARSE_MATRIX_CSPARSE change_matrix_to_new_size(const SPARSE_MATRIX_CSPARSE&A, unsigned int mrow, unsigned int ncol);
SPARSE_MATRIX_CSPARSE expand_matrix_to_new_size(const SPARSE_MATRIX_CSPARSE&A, unsigned int mrow, unsigned int ncol);
SPARSE_MATRIX_CSPARSE shrink_matrix_to_new_size(const SPARSE_MATRIX_CSPARSE&A, unsigned int mrow, unsigned int ncol);
#endif // SPARSE_MATRIX_CSPARSE_H
6 changes: 6 additions & 0 deletions code/steps/header/basic/sparse_matrix_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ class SPARSE_MATRIX_TEST : public Test::Suite
void test_matrix_add();
void test_matrix_minus();
void test_matrix_multiply();
void test_matrix_divide();
void test_matrix_inverse();
void test_concatenate_matrix_diagnally();
void test_build_identity_matrix();
void test_expand_matrix_to_new_size();
void test_shrink_matrix_to_new_size();
void test_change_matrix_to_new_size();

void test_save_matrix_to_file();
private:
Expand Down
5 changes: 3 additions & 2 deletions code/steps/main_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,10 @@ int main(int argc, char* argv[])
ts.add(unique_ptr<Test::Suite>(new OWNERSHIP_TEST));
ts.add(unique_ptr<Test::Suite>(new RATING_TEST));
ts.add(unique_ptr<Test::Suite>(new UTILITY_TEST));
*/

ts.add(unique_ptr<Test::Suite>(new SPARSE_MATRIX_TEST));
ts.add(unique_ptr<Test::Suite>(new COMPLEX_SPARSE_MATRIX_TEST));
/*ts.add(unique_ptr<Test::Suite>(new COMPLEX_SPARSE_MATRIX_TEST));
ts.add(unique_ptr<Test::Suite>(new FAULT_TEST));
ts.add(unique_ptr<Test::Suite>(new BUS_INDEX_TEST));
Expand Down Expand Up @@ -360,7 +361,7 @@ int main(int argc, char* argv[])
ts.add(unique_ptr<Test::Suite>(new CONVERTER_STATION_TEST));
*/
//ts.add(unique_ptr<Test::Suite>(new VSG0_TEST));
ts.add(unique_ptr<Test::Suite>(new LVRT_CONTROL_TEST));
//ts.add(unique_ptr<Test::Suite>(new LVRT_CONTROL_TEST));

#endif // ENABLE_STEPS_TEST

Expand Down
146 changes: 111 additions & 35 deletions code/steps/source/basic/sparse_matrix_csparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,41 +231,10 @@ void SPARSE_MATRIX_CSPARSE::compress_and_merge_duplicate_entries()
// compress the sparse matrix
if(matrix_in_triplet_form())
{
cs *mat; // temp mat
csi *tempi; // temp index
double *tempd; // temp value

// real part
// compress the matrix
mat = cs_compress(matrix_real);
// swap
matrix_real->nzmax = mat->nzmax;
matrix_real->m = mat->m;
matrix_real->n = mat->n;
matrix_real->nz = mat->nz;
tempi = matrix_real->p; matrix_real->p = mat->p; mat->p = tempi;
tempi = matrix_real->i; matrix_real->i = mat->i; mat->i = tempi;
tempd = matrix_real->x; matrix_real->x = mat->x; mat->x = tempd;
// free temp mat
cs_spfree(mat);

cs_dupl(matrix_real); // merge duplicate entries

compress_and_merge_cs_matrix(matrix_real);
// imaginary part
// compress the matrix
mat = cs_compress(matrix_imag);
// swap
matrix_imag->nzmax = mat->nzmax;
matrix_imag->m = mat->m;
matrix_imag->n = mat->n;
matrix_imag->nz = mat->nz;
tempi = matrix_imag->p; matrix_imag->p = mat->p; mat->p = tempi;
tempi = matrix_imag->i; matrix_imag->i = mat->i; mat->i = tempi;
tempd = matrix_imag->x; matrix_imag->x = mat->x; mat->x = tempd;
// free temp mat
cs_spfree(mat);

cs_dupl(matrix_imag); // merge duplicate entries
compress_and_merge_cs_matrix(matrix_imag);

// at last, transpose twice
transpose();
Expand All @@ -275,6 +244,28 @@ void SPARSE_MATRIX_CSPARSE::compress_and_merge_duplicate_entries()
}
}

void SPARSE_MATRIX_CSPARSE::compress_and_merge_cs_matrix(cs* matrix)
{
cs *mat; // temp mat
csi *tempi; // temp index
double *tempd; // temp value

// compress the matrix
mat = cs_compress(matrix);
// swap
matrix->nzmax = mat->nzmax;
matrix->m = mat->m;
matrix->n = mat->n;
matrix->nz = mat->nz;
tempi = matrix->p; matrix->p = mat->p; mat->p = tempi;
tempi = matrix->i; matrix->i = mat->i; mat->i = tempi;
tempd = matrix->x; matrix->x = mat->x; mat->x = tempd;
// free temp mat
cs_spfree(mat);

cs_dupl(matrix); // merge duplicate entries
}

void SPARSE_MATRIX_CSPARSE::transpose()
{
// transpose a compress format matrix
Expand Down Expand Up @@ -748,11 +739,13 @@ cs* SPARSE_MATRIX_CSPARSE::get_cs_imag_matrix()
void SPARSE_MATRIX_CSPARSE::set_cs_real_matrix(cs* matrix)
{
matrix_real = matrix;
update_clock_when_matrix_is_changed();
}

void SPARSE_MATRIX_CSPARSE::set_cs_imag_matrix(cs* matrix)
{
matrix_imag = matrix;
update_clock_when_matrix_is_changed();
}

vector<double>& operator/(vector<double>&b, SPARSE_MATRIX_CSPARSE& A)
Expand All @@ -770,6 +763,7 @@ SPARSE_MATRIX_CSPARSE operator+(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE&
SPARSE_MATRIX_CSPARSE C;
C.clear();
C.set_cs_real_matrix(c);
C.compress_and_merge_duplicate_entries();
return C;
}

Expand All @@ -783,6 +777,7 @@ SPARSE_MATRIX_CSPARSE operator-(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE&
SPARSE_MATRIX_CSPARSE C;
C.clear();
C.set_cs_real_matrix(c);
C.compress_and_merge_duplicate_entries();
return C;
}

Expand All @@ -796,6 +791,8 @@ SPARSE_MATRIX_CSPARSE operator*(SPARSE_MATRIX_CSPARSE&A, SPARSE_MATRIX_CSPARSE&
SPARSE_MATRIX_CSPARSE C;
C.clear();
C.set_cs_real_matrix(c);
C.convert_to_triplet_form();
C.compress_and_merge_duplicate_entries();
return C;
}

Expand Down Expand Up @@ -871,7 +868,7 @@ SPARSE_MATRIX_CSPARSE inv(SPARSE_MATRIX_CSPARSE&A)
return B;
}

SPARSE_MATRIX_CSPARSE concatenate_matrix_diagnally(vector<SPARSE_MATRIX_CSPARSE*> matrix)
SPARSE_MATRIX_CSPARSE concatenate_matrix_diagnally(const vector<SPARSE_MATRIX_CSPARSE*> matrix)
{
SPARSE_MATRIX_CSPARSE MATRIX;
unsigned int m=0, n = 0;
Expand All @@ -896,11 +893,15 @@ SPARSE_MATRIX_CSPARSE concatenate_matrix_diagnally(vector<SPARSE_MATRIX_CSPARSE*
return MATRIX;
}

SPARSE_MATRIX_CSPARSE build_identity_matrix(SPARSE_MATRIX_CSPARSE&A)
SPARSE_MATRIX_CSPARSE build_identity_matrix(const SPARSE_MATRIX_CSPARSE&A)
{
// B = I,I.column_count = A.matrix_column_count
unsigned int n = A.get_matrix_column_count();
return build_identity_matrix(n);
}

SPARSE_MATRIX_CSPARSE build_identity_matrix(unsigned int n)
{
vector<double> b;
for(unsigned int i=0; i<n; ++i)
b.push_back(0.0);
Expand All @@ -914,5 +915,80 @@ SPARSE_MATRIX_CSPARSE build_identity_matrix(SPARSE_MATRIX_CSPARSE&A)
for(unsigned int j=0; j<n; ++j)
B.add_entry(i, j, b[j]);
}
B.compress_and_merge_duplicate_entries();
return B;
}

SPARSE_MATRIX_CSPARSE change_matrix_to_new_size(const SPARSE_MATRIX_CSPARSE&A, unsigned int mrow, unsigned int ncol)
{
unsigned int m = A.get_matrix_row_count();
unsigned int n = A.get_matrix_column_count();
if(mrow==m and ncol==n)
return A;
else
{
if(mrow>=m and ncol>=n)
return expand_matrix_to_new_size(A, mrow, ncol);
else
{
if(mrow<=m and ncol<=n)
return shrink_matrix_to_new_size(A, mrow, ncol);
else
{
if(mrow<=m and ncol>=n)
{
SPARSE_MATRIX_CSPARSE B = shrink_matrix_to_new_size(A, mrow, n);
return expand_matrix_to_new_size(B, mrow, ncol);
}
else
{
SPARSE_MATRIX_CSPARSE B = shrink_matrix_to_new_size(A, m, ncol);
return expand_matrix_to_new_size(B, mrow, ncol);
}
}
}
}
}

SPARSE_MATRIX_CSPARSE expand_matrix_to_new_size(const SPARSE_MATRIX_CSPARSE&A, unsigned int mrow, unsigned int ncol)
{
unsigned int m = A.get_matrix_row_count();
unsigned int n = A.get_matrix_column_count();
if(mrow<m or ncol<n)
return A;
else
{
SPARSE_MATRIX_CSPARSE B = A;
B.convert_to_triplet_form();
B.add_entry(mrow-1, ncol-1, complex<double>(0,0));
B.compress_and_merge_duplicate_entries();
return B;
}
}

SPARSE_MATRIX_CSPARSE shrink_matrix_to_new_size(const SPARSE_MATRIX_CSPARSE&A, unsigned int mrow, unsigned int ncol)
{
unsigned int m = A.get_matrix_row_count();
unsigned int n = A.get_matrix_column_count();
if(mrow>m or ncol>n)
return A;
else
{
SPARSE_MATRIX_CSPARSE B;
unsigned int nz = A.get_matrix_entry_count();
for(unsigned int k=0; k<nz; ++k)
{
unsigned int i = A.get_row_number_of_entry_index(k);
unsigned int j = A.get_column_number_of_entry_index(k);
if(i>=mrow or j>=ncol)
continue;
else
{
complex<double> x = A.get_entry_value(k);
B.add_entry(i,j,x);
}
}
B.compress_and_merge_duplicate_entries();
return B;
}
}
Loading

0 comments on commit 619a2fb

Please sign in to comment.