diff --git a/CHANGELOG.md b/CHANGELOG.md index e2753c5c6..1d17bf9a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,8 @@ - Minor performance improvements to residual evaluation in PowerElectronics module. - Added full support for sparse Jacobians obtained with Enzyme in PhasorDynamics. - Added `Node` class to the PowerElectronics module to separate nodes from circuit components. -- Refactor Jacobian assembly in `PowerElectronics` module to reuse the CSR pattern. +- Refactored Jacobian assembly in `PowerElectronics` module to reuse the CSR pattern. +- Refactored Jacobian assembly in `PhasorDyanmcics` module to reuse the CSR pattern. ## v0.1 - Refactored code to support adding different model families. diff --git a/GridKit/LinearAlgebra/SparseMatrix/CMakeLists.txt b/GridKit/LinearAlgebra/SparseMatrix/CMakeLists.txt index 14c11bebf..3dc4bd274 100644 --- a/GridKit/LinearAlgebra/SparseMatrix/CMakeLists.txt +++ b/GridKit/LinearAlgebra/SparseMatrix/CMakeLists.txt @@ -1,4 +1,4 @@ -gridkit_add_library(SparseMatrix +gridkit_add_library(sparse_matrix SOURCES CooMatrix.cpp CsrMatrix.cpp diff --git a/GridKit/LinearAlgebra/SparseMatrix/CooMatrix.cpp b/GridKit/LinearAlgebra/SparseMatrix/CooMatrix.cpp index f99521df3..2c6c8836d 100644 --- a/GridKit/LinearAlgebra/SparseMatrix/CooMatrix.cpp +++ b/GridKit/LinearAlgebra/SparseMatrix/CooMatrix.cpp @@ -400,35 +400,42 @@ namespace GridKit map_to_dedup_ = new IdxT[static_cast(nnz_)]; IdxT* csr_row_data = new IdxT[static_cast(n_ + 1)](); - map_to_dedup_[0] = 0; - csr_row_data[h_row_data_[0] + 1]++; - - // Deduplicate sorted entries - IdxT w = 0; - for (IdxT i = 1; i < nnz_; i++) + if (nnz_ == 0) + { + csr_row_data[0] = 0; + } + else { - if (h_row_data_[i] == h_row_data_[w] && h_col_data_[i] == h_col_data_[w]) + map_to_dedup_[0] = 0; + csr_row_data[h_row_data_[0] + 1]++; + + // Deduplicate sorted entries + IdxT w = 0; + for (IdxT i = 1; i < nnz_; i++) { - h_val_data_[w] += h_val_data_[i]; - map_to_dedup_[i] = w; + if (h_row_data_[i] == h_row_data_[w] && h_col_data_[i] == h_col_data_[w]) + { + h_val_data_[w] += h_val_data_[i]; + map_to_dedup_[i] = w; + } + else + { + ++w; + h_row_data_[w] = h_row_data_[i]; + h_col_data_[w] = h_col_data_[i]; + h_val_data_[w] = h_val_data_[i]; + map_to_dedup_[i] = w; + csr_row_data[h_row_data_[w] + 1]++; + } } - else + // nnz after deduplication + nnz_ = w + 1; + + for (IdxT i = 0; i < n_; i++) { - ++w; - h_row_data_[w] = h_row_data_[i]; - h_col_data_[w] = h_col_data_[i]; - h_val_data_[w] = h_val_data_[i]; - map_to_dedup_[i] = w; - csr_row_data[h_row_data_[w] + 1]++; + csr_row_data[i + 1] += csr_row_data[i]; } } - // nnz after deduplication - nnz_ = w + 1; - - for (IdxT i = 0; i < n_; i++) - { - csr_row_data[i + 1] += csr_row_data[i]; - } return csr_row_data; } diff --git a/GridKit/Model/Evaluator.hpp b/GridKit/Model/Evaluator.hpp index ffec10cf2..5753c72fa 100644 --- a/GridKit/Model/Evaluator.hpp +++ b/GridKit/Model/Evaluator.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include diff --git a/GridKit/Model/PhasorDynamics/CMakeLists.txt b/GridKit/Model/PhasorDynamics/CMakeLists.txt index 9545732cb..7e4d57ae7 100644 --- a/GridKit/Model/PhasorDynamics/CMakeLists.txt +++ b/GridKit/Model/PhasorDynamics/CMakeLists.txt @@ -20,6 +20,7 @@ gridkit_add_library(phasor_dynamics_core LINK_LIBRARIES PUBLIC GridKit::definitions PUBLIC GridKit::utilities_logger + PUBLIC GridKit::sparse_matrix INCLUDE_DIRECTORIES PRIVATE ${GRIDKIT_THIRD_PARTY_DIR}/nlohmann-json/include PRIVATE ${GRIDKIT_THIRD_PARTY_DIR}/magic-enum/include) diff --git a/GridKit/Model/PhasorDynamics/SystemModel.hpp b/GridKit/Model/PhasorDynamics/SystemModel.hpp index 66a2faf88..3ce7b1f57 100644 --- a/GridKit/Model/PhasorDynamics/SystemModel.hpp +++ b/GridKit/Model/PhasorDynamics/SystemModel.hpp @@ -36,6 +36,7 @@ namespace GridKit using signal_type = PhasorDynamics::SignalNode; using component_type = PhasorDynamics::Component; using RealT = typename Model::Evaluator::RealT; + using CsrMatrix = typename Model::Evaluator::CsrMatrix; using PhasorDynamics::Component::gridkit_component_id_; using PhasorDynamics::Component::size_; @@ -270,6 +271,16 @@ namespace GridKit delete signal; } } + if (csr_jac_ != nullptr) + { + delete csr_jac_; + csr_jac_ = nullptr; + } + if (map_to_csr_ != nullptr) + { + delete[] map_to_csr_; + map_to_csr_ = nullptr; + } } /** @@ -605,70 +616,145 @@ namespace GridKit */ int evaluateJacobian() override { - bool sort = !jacobian_allocated_; // sort in axpy only if not allocated - if (!jacobian_allocated_) - { - J_.zeroMatrix(); - } - else - { - J_.zeroValuedMatrix(); - } - - std::vector ctemp{}; - std::vector rtemp{}; - std::vector valtemp{}; - // Initialize bus Jacobians for (const auto& bus : buses_) { bus->evaluateJacobian(); } - // Jacobian blocks owed by components - // Also updates bus Jacobians + // Evaluate component Jacobians and update bus Jacobians for (const auto& component : components_) { component->evaluateJacobian(); - auto component_jacobian = component->getJacobian(); + } - std::tuple&, std::vector&, std::vector&> component_jacobian_entries = component_jacobian.getEntries(); - const auto [rows, columns, values] = component_jacobian_entries; - for (size_t i = 0; i < rows.size(); ++i) + // Build or update system CSR Jacobian + if (csr_jac_ == nullptr) + { + // Count the number of non-zeros + IdxT nnz_dup = 0; + for (const auto& component : components_) { - rtemp.push_back(rows[i]); - ctemp.push_back(columns[i]); - valtemp.push_back(values[i]); + auto component_jacobian = component->getJacobian(); + + std::tuple&, std::vector&, std::vector&> component_jacobian_entries = component_jacobian.getEntries(); + const auto [rows, columns, values] = component_jacobian_entries; + for (size_t i = 0; i < rows.size(); ++i) + { + ++nnz_dup; + } } - // Need axpy because some branches may connect to the same buses, and own the same - // off-diagonal locations. @todo implement a more efficient approach. - J_.axpy(1.0, rtemp, ctemp, valtemp, sort); - rtemp.clear(); - ctemp.clear(); - valtemp.clear(); - } + for (const auto& bus : buses_) + { + auto bus_jacobian = bus->getJacobian(); - // Bus Jacobians - for (const auto& bus : buses_) - { - auto bus_jacobian = bus->getJacobian(); + std::tuple&, std::vector&, std::vector&> bus_jacobian_entries = bus_jacobian.getEntries(); + const auto [rows, columns, values] = bus_jacobian_entries; + for (size_t i = 0; i < rows.size(); ++i) + { + ++nnz_dup; + } + } + + // Allocate COO triplet arrays (we own these until we hand off to CsrMatrix) + IdxT* rows_dup = new IdxT[nnz_dup]; + IdxT* cols_dup = new IdxT[nnz_dup]; + RealT* vals_dup = new RealT[nnz_dup]; - std::tuple&, std::vector&, std::vector&> bus_jacobian_entries = bus_jacobian.getEntries(); - const auto [rows, columns, values] = bus_jacobian_entries; - for (size_t i = 0; i < rows.size(); ++i) + IdxT counter = 0; + for (const auto& component : components_) { - rtemp.push_back(rows[i]); - ctemp.push_back(columns[i]); - valtemp.push_back(values[i]); + auto component_jacobian = component->getJacobian(); + + std::tuple&, std::vector&, std::vector&> component_jacobian_entries = component_jacobian.getEntries(); + const auto [rows, columns, values] = component_jacobian_entries; + for (size_t i = 0; i < rows.size(); ++i) + { + rows_dup[counter] = rows[i]; + cols_dup[counter] = columns[i]; + vals_dup[counter] = values[i]; + counter++; + } } - } + for (const auto& bus : buses_) + { + auto bus_jacobian = bus->getJacobian(); + + std::tuple&, std::vector&, std::vector&> bus_jacobian_entries = bus_jacobian.getEntries(); + const auto [rows, columns, values] = bus_jacobian_entries; + for (size_t i = 0; i < rows.size(); ++i) + { + rows_dup[counter] = rows[i]; + cols_dup[counter] = columns[i]; + vals_dup[counter] = values[i]; + counter++; + } + } + + // Build the system COO Jacobian + LinearAlgebra::CooMatrix jac(size_, size_, nnz_dup, &rows_dup, &cols_dup, &vals_dup); + + // Populate CSR data with sort and deduplicate + IdxT* row_ptrs = jac.getCsrRowData(); + + // Deduplicated nnz + nnz_ = jac.getNnz(); + + // Allocate cols/vals with deduplicated nnz + IdxT* cols = new IdxT[nnz_]; + RealT* vals = new RealT[nnz_]; + + std::copy(jac.getColData(), jac.getColData() + nnz_, cols); + std::copy(jac.getValues(), jac.getValues() + nnz_, vals); - J_.axpy(1.0, rtemp, ctemp, valtemp, sort); + // Create the CSR Jacobian + csr_jac_ = new CsrMatrix(size_, size_, nnz_, &row_ptrs, &cols, &vals); - // Flag Jacobian as allocated - if (!jacobian_allocated_) + const IdxT* map_to_sorted = jac.getMapToSorted(); + const IdxT* map_to_dedup = jac.getMapToDeduplicated(); + + // Build a mappping from original COO index to CSR index + map_to_csr_ = new IdxT[nnz_dup]; + for (IdxT i = 0; i < nnz_dup; ++i) + { + map_to_csr_[map_to_sorted[i]] = map_to_dedup[i]; + } + } + else { - jacobian_allocated_ = true; + // Zero out values + RealT* vals = csr_jac_->getValues(); + for (IdxT i = 0; i < csr_jac_->getNnz(); ++i) + { + vals[i] = 0.0; + } + + // Update CSR values from component and bus Jacobians + IdxT counter = 0; + for (const auto& component : components_) + { + auto component_jacobian = component->getJacobian(); + + std::tuple&, std::vector&, std::vector&> component_jacobian_entries = component_jacobian.getEntries(); + const auto [rows, columns, values] = component_jacobian_entries; + for (size_t i = 0; i < rows.size(); ++i) + { + vals[map_to_csr_[counter]] += values[i]; + ++counter; + } + } + for (const auto& bus : buses_) + { + auto bus_jacobian = bus->getJacobian(); + + std::tuple&, std::vector&, std::vector&> bus_jacobian_entries = bus_jacobian.getEntries(); + const auto [rows, columns, values] = bus_jacobian_entries; + for (size_t i = 0; i < rows.size(); ++i) + { + vals[map_to_csr_[counter]] += values[i]; + ++counter; + } + } } // J_.printMatrix("System Jacobian"); @@ -676,6 +762,11 @@ namespace GridKit return 0; } + CsrMatrix* getCsrJacobian() const override + { + return csr_jac_; + } + bool monitoring() const override { return !monitor_.empty(); @@ -839,7 +930,9 @@ namespace GridKit std::map gridkit_fault_indices_; ///< Map between fault_id and component_id bool owns_components_{false}; - bool jacobian_allocated_{false}; + + IdxT* map_to_csr_{nullptr}; + CsrMatrix* csr_jac_{nullptr}; /// Variable monitor Model::VariableMonitorController monitor_; diff --git a/GridKit/Model/PowerElectronics/SystemModelPowerElectronics.hpp b/GridKit/Model/PowerElectronics/SystemModelPowerElectronics.hpp index 634409227..edb4114b7 100644 --- a/GridKit/Model/PowerElectronics/SystemModelPowerElectronics.hpp +++ b/GridKit/Model/PowerElectronics/SystemModelPowerElectronics.hpp @@ -9,9 +9,6 @@ #include #include -#include -#include -#include #include #include #include diff --git a/GridKit/Solver/Dynamic/CMakeLists.txt b/GridKit/Solver/Dynamic/CMakeLists.txt index 4e4abd1e4..26e0dca41 100644 --- a/GridKit/Solver/Dynamic/CMakeLists.txt +++ b/GridKit/Solver/Dynamic/CMakeLists.txt @@ -21,7 +21,7 @@ if (GRIDKIT_ENABLE_SUNDIALS_SPARSE) PUBLIC SUNDIALS::sunlinsolklu PUBLIC GridKit::definitions PUBLIC GridKit::utilities_logger - PUBLIC GridKit::SparseMatrix) + PUBLIC GridKit::sparse_matrix) else() gridkit_add_library(solvers_dyn SOURCES @@ -33,5 +33,5 @@ else() PUBLIC SUNDIALS::idas PUBLIC GridKit::definitions PUBLIC GridKit::utilities_logger - PUBLIC GridKit::SparseMatrix) + PUBLIC GridKit::sparse_matrix) endif() diff --git a/GridKit/Solver/Dynamic/Ida.cpp b/GridKit/Solver/Dynamic/Ida.cpp index 39e26c3a5..9397bfa99 100644 --- a/GridKit/Solver/Dynamic/Ida.cpp +++ b/GridKit/Solver/Dynamic/Ida.cpp @@ -168,20 +168,8 @@ namespace AnalysisManager { int retval = 0; - sunindextype n = static_cast(model_->size()); - sunindextype nnz; - - /// @todo Remove this flag and require all apps to configure CsrJac - bool use_csr_jac = (model_->getCsrJacobian() != nullptr); - - if (use_csr_jac) - { - nnz = static_cast(model_->getCsrJacobian()->getNnz()); - } - else - { - nnz = static_cast((model_->getJacobian()).nnz()); - } + sunindextype n = static_cast(model_->size()); + sunindextype nnz = static_cast(model_->getCsrJacobian()->getNnz()); JacobianMat_ = SUNSparseMatrix(n, n, @@ -196,14 +184,7 @@ namespace AnalysisManager retval = IDASetLinearSolver(solver_, linearSolver_, JacobianMat_); checkOutput(retval, "IDASetLinearSolver"); - if (use_csr_jac) - { - retval = IDASetJacFn(solver_, this->CsrJac); - } - else - { - retval = IDASetJacFn(solver_, this->Jac); - } + retval = IDASetJacFn(solver_, this->Jac); checkOutput(retval, "IDASetJacFn"); return retval; @@ -782,47 +763,6 @@ namespace AnalysisManager return 0; } - /** - * @brief Jacobian evaluation - * - * @tparam ScalarT - * @tparam IdxT - */ - template - int Ida::Jac(RealT t, RealT cj, N_Vector yy, N_Vector yp, N_Vector, SUNMatrix J, void* user_data, N_Vector, N_Vector, N_Vector) - { - - GridKit::Model::Evaluator* model = static_cast*>(user_data); - - model->updateTime(t, cj); - copyVec(yy, model->y()); - copyVec(yp, model->yp()); - - model->evaluateJacobian(); - GridKit::LinearAlgebra::COO_Matrix& Jac = model->getJacobian(); - - // Get reference to the jacobian entries - std::tuple&, std::vector&, std::vector&> tpm = Jac.getEntries(); - const auto [r, c, val] = tpm; - - // get the CSR row pointers from COO matrix - std::vector csrrowdata = Jac.getCSRRowData(); - - SUNMatZero(J); - - // Set row pointers - sunindextype* rowptrs = SUNSparseMatrix_IndexPointers(J); - std::copy(csrrowdata.cbegin(), csrrowdata.cend(), rowptrs); - - sunindextype* colvals = SUNSparseMatrix_IndexValues(J); - RealT* data = SUNSparseMatrix_Data(J); - // Copy data from model jac to sundials - std::copy(c.cbegin(), c.cend(), colvals); - std::copy(val.cbegin(), val.cend(), data); - - return 0; - } - /** * @brief Jacobian evaluation * @@ -830,11 +770,9 @@ namespace AnalysisManager * * @tparam ScalarT * @tparam IdxT - * - * @todo Update PhasorDynamics to use this implementation. */ template - int Ida::CsrJac(RealT t, RealT cj, N_Vector yy, N_Vector yp, N_Vector, SUNMatrix J, void* user_data, N_Vector, N_Vector, N_Vector) + int Ida::Jac(RealT t, RealT cj, N_Vector yy, N_Vector yp, N_Vector, SUNMatrix J, void* user_data, N_Vector, N_Vector, N_Vector) { GridKit::Model::Evaluator* model = static_cast*>(user_data); diff --git a/GridKit/Solver/Dynamic/Ida.hpp b/GridKit/Solver/Dynamic/Ida.hpp index 0c4c09bf1..20ff56aa8 100644 --- a/GridKit/Solver/Dynamic/Ida.hpp +++ b/GridKit/Solver/Dynamic/Ida.hpp @@ -151,17 +151,6 @@ namespace AnalysisManager N_Vector tmp2, N_Vector tmp3); - static int CsrJac(RealT t, - RealT cj, - N_Vector yy, - N_Vector yp, - N_Vector resvec, - SUNMatrix J, - void* user_data, - N_Vector tmp1, - N_Vector tmp2, - N_Vector tmp3); - static int Integrand(RealT t, N_Vector yy, N_Vector yp, diff --git a/GridKit/Utilities/MapFromCOO.hpp b/GridKit/Utilities/MapFromCOO.hpp index 947f7cb04..d4fa91c29 100644 --- a/GridKit/Utilities/MapFromCOO.hpp +++ b/GridKit/Utilities/MapFromCOO.hpp @@ -1,6 +1,6 @@ /** - * @file MapToCOO.hpp + * @file MapFromCOO.hpp * * @author Nicholson Koukpaizan , ORNL * @todo This should go away once we settle on a sparse format diff --git a/GridKit/Utilities/MapFromCsr.hpp b/GridKit/Utilities/MapFromCsr.hpp new file mode 100644 index 000000000..6a4028f32 --- /dev/null +++ b/GridKit/Utilities/MapFromCsr.hpp @@ -0,0 +1,39 @@ + +/** + * @file MapFromCsr.hpp + * + * @author Nicholson Koukpaizan , ORNL + * @todo Move to a more permanent location + * + */ +#pragma once + +#include +#include + +namespace GridKit +{ + namespace Testing + { + template + std::vector MapFromCsr(LinearAlgebra::CsrMatrix* matrix) + { + IdxT* row_data = matrix->getRowData(); + IdxT* column_data = matrix->getColData(); + RealT* values = matrix->getValues(); + IdxT n_rows = matrix->getNumRows(); + + std::vector dependencies(n_rows); + + for (IdxT i = 0; i < n_rows; ++i) + { + for (IdxT j = row_data[i]; j < row_data[i + 1]; ++j) + { + dependencies[i].insert(std::make_pair(column_data[j], values[j])); + } + } + + return dependencies; + } + } // namespace Testing +} // namespace GridKit diff --git a/examples/LinearAlgebra/SparseTest/CMakeLists.txt b/examples/LinearAlgebra/SparseTest/CMakeLists.txt index c4c664d5b..4b0e608fe 100644 --- a/examples/LinearAlgebra/SparseTest/CMakeLists.txt +++ b/examples/LinearAlgebra/SparseTest/CMakeLists.txt @@ -1,7 +1,7 @@ add_executable(spmattest SparseTest.cpp) -target_link_libraries(spmattest GridKit::SparseMatrix) +target_link_libraries(spmattest GridKit::sparse_matrix) add_test(NAME SparseMatrixTest COMMAND spmattest) install(TARGETS spmattest RUNTIME DESTINATION bin) diff --git a/examples/PowerElectronics/Microgrid/Microgrid.cpp b/examples/PowerElectronics/Microgrid/Microgrid.cpp index fc2f379ef..94c84c9be 100644 --- a/examples/PowerElectronics/Microgrid/Microgrid.cpp +++ b/examples/PowerElectronics/Microgrid/Microgrid.cpp @@ -23,6 +23,7 @@ int main(int /* argc */, char const** /* argv */) double rel_tol = 1.0e-8; size_t max_step_number = 3000; bool use_jac = true; + bool debug_output = true; // Create model auto* sysmodel = new GridKit::PowerElectronicsModel(rel_tol, abs_tol, use_jac, max_step_number); @@ -288,14 +289,14 @@ int main(int /* argc */, char const** /* argv */) std::cout << sysmodel->y().size() << std::endl; std::cout << vec_size_internals << ", " << vec_size_externals << "\n"; - // Create Intial points for states + // Create initial points for states for (size_t i = 0; i < vec_size_total; i++) { sysmodel->y()[i] = 0.0; sysmodel->yp()[i] = 0.0; } - // Create Intial derivatives specifics generated in MATLAB + // Create initial derivatives specifics generated in MATLAB // DGs 1 sysmodel->yp()[2] = parms1.Vn_; sysmodel->yp()[4] = parms1.Kpv_ * parms1.Vn_; @@ -316,18 +317,27 @@ int main(int /* argc */, char const** /* argv */) sysmodel->initialize(); sysmodel->evaluateResidual(); - // // Optional debugging output - // std::vector& fres = sysmodel->getResidual(); - // std::cout << "Verify Intial Resisdual is Zero: {\n"; - // for (size_t i = 0; i < fres.size(); i++) - // { - // printf("%lu : %e \n", i, fres[i]); - // } - // std::cout << "}\n"; + // Optional debuging output + if (debug_output) + { + std::vector& fres = sysmodel->getResidual(); + std::cout << "Verify initial Resisdual is Zero: {\n"; + for (size_t i = 0; i < fres.size(); i++) + { + printf("%lu : %e \n", i, fres[i]); + } + std::cout << "}\n"; + } sysmodel->updateTime(0.0, 1.0e-8); sysmodel->evaluateJacobian(); - std::cout << "Intial Jacobian with alpha:\n"; + + // Optional debuging output + if (debug_output) + { + std::cout << "Initial Jacobian with alpha:\n"; + sysmodel->getCsrJacobian()->print(); + } // Create numerical integrator and configure it for the generator model auto* idas = new AnalysisManager::Sundials::Ida(sysmodel); @@ -344,12 +354,15 @@ int main(int /* argc */, char const** /* argv */) std::vector& yfinial = sysmodel->y(); - // // Optional debugging output - // std::cout << "Final Vector y\n"; - // for (size_t i = 0; i < yfinial.size(); i++) - // { - // std::cout << yfinial[i] << "\n"; - // } + // Optional debugging output + if (debug_output) + { + std::cout << "Final Vector y\n"; + for (size_t i = 0; i < yfinial.size(); i++) + { + std::cout << yfinial[i] << "\n"; + } + } // Generated from MATLAB code ODE form with tolerances of 1e-12 std::vector true_vec{ diff --git a/examples/PowerElectronics/RLCircuit/RLCircuit.cpp b/examples/PowerElectronics/RLCircuit/RLCircuit.cpp index c8f970cc9..4b51090e2 100644 --- a/examples/PowerElectronics/RLCircuit/RLCircuit.cpp +++ b/examples/PowerElectronics/RLCircuit/RLCircuit.cpp @@ -73,7 +73,7 @@ int main(int /* argc */, char const** /* argv */) // Grounding for IDA. If no grounding then circuit is \mu > 1 // v_0 (grounded) - // Create Intial points + // Create initial points sysmodel.y()[0] = vinit; // v_1 sysmodel.y()[1] = vinit; // v_2 sysmodel.y()[2] = 0.0; // i_L @@ -87,7 +87,7 @@ int main(int /* argc */, char const** /* argv */) sysmodel.initialize(); sysmodel.evaluateResidual(); - std::cout << "Verify Intial Resisdual is Zero: {"; + std::cout << "Verify initial resisdual is zero: {"; for (double i : sysmodel.getResidual()) { std::cout << i << ", "; @@ -96,7 +96,7 @@ int main(int /* argc */, char const** /* argv */) sysmodel.updateTime(0.0, 1.0); sysmodel.evaluateJacobian(); - std::cout << "Intial Jacobian with alpha = 1:\n"; + std::cout << "Initial Jacobian with alpha = 1:\n"; sysmodel.getCsrJacobian()->print(); // Create numerical integrator and configure it for the generator model diff --git a/examples/PowerElectronics/ScaleMicrogrid/ScaleMicrogrid.cpp b/examples/PowerElectronics/ScaleMicrogrid/ScaleMicrogrid.cpp index aa201a6eb..6262dd61b 100644 --- a/examples/PowerElectronics/ScaleMicrogrid/ScaleMicrogrid.cpp +++ b/examples/PowerElectronics/ScaleMicrogrid/ScaleMicrogrid.cpp @@ -287,14 +287,14 @@ int test(index_type Nsize, real_type error_tol, bool debug_output) std::cout << vec_size_internals << ", " << vec_size_externals << "\n"; } - // Create Intial points for states. Every state is to specified to the zero intially + // Create initial points for states. Every state is to specified to the zero intially for (index_type i = 0; i < vec_size_total; i++) { sys_model->y()[i] = 0.0; sys_model->yp()[i] = 0.0; } - // Create Intial derivatives specifics generated in MATLAB + // Create initial derivatives specifics generated in MATLAB for (index_type i = 0; i < 2 * Nsize; i++) { sys_model->yp()[13 * i - 1 + 3] = DGParams_list[i].Vn_; @@ -312,7 +312,7 @@ int test(index_type Nsize, real_type error_tol, bool debug_output) std::vector& fres = sys_model->getResidual(); if (debug_output) { - std::cout << "Verify Intial Resisdual is Zero: {\n"; + std::cout << "Verify initial resisdual is zero: {\n"; for (index_type i = 0; i < fres.size(); i++) { std::cout << i << " : " << fres[i] << "\n"; @@ -326,7 +326,10 @@ int test(index_type Nsize, real_type error_tol, bool debug_output) // print the jacobian in matrix market format if (debug_output) - std::cout << "Intial Jacobian with alpha:\n"; + { + std::cout << "Initial Jacobian with alpha:\n"; + sys_model->getCsrJacobian()->print(); + } // Create numerical integrator and configure it for the generator model auto* idas = new AnalysisManager::Sundials::Ida(sys_model); diff --git a/tests/UnitTests/LinearAlgebra/SparseMatrix/CMakeLists.txt b/tests/UnitTests/LinearAlgebra/SparseMatrix/CMakeLists.txt index 3741a17c3..97e0e0868 100644 --- a/tests/UnitTests/LinearAlgebra/SparseMatrix/CMakeLists.txt +++ b/tests/UnitTests/LinearAlgebra/SparseMatrix/CMakeLists.txt @@ -1,10 +1,10 @@ -add_executable(test_sparse_csr runSparseTests.cpp) -target_link_libraries(test_sparse_csr GridKit::SparseMatrix GridKit::testing) +add_executable(test_sparse_csr runSparseCsrTests.cpp) +target_link_libraries(test_sparse_csr GridKit::sparse_matrix GridKit::testing) add_test(NAME SparseTest COMMAND $) add_executable(test_sparse_coo runSparseCooTests.cpp) -target_link_libraries(test_sparse_coo GridKit::SparseMatrix GridKit::testing) +target_link_libraries(test_sparse_coo GridKit::sparse_matrix GridKit::testing) add_test(NAME SparseCooTest COMMAND $) diff --git a/tests/UnitTests/LinearAlgebra/SparseMatrix/SparseTests.hpp b/tests/UnitTests/LinearAlgebra/SparseMatrix/SparseCsrTests.hpp similarity index 100% rename from tests/UnitTests/LinearAlgebra/SparseMatrix/SparseTests.hpp rename to tests/UnitTests/LinearAlgebra/SparseMatrix/SparseCsrTests.hpp diff --git a/tests/UnitTests/LinearAlgebra/SparseMatrix/runSparseTests.cpp b/tests/UnitTests/LinearAlgebra/SparseMatrix/runSparseCsrTests.cpp similarity index 98% rename from tests/UnitTests/LinearAlgebra/SparseMatrix/runSparseTests.cpp rename to tests/UnitTests/LinearAlgebra/SparseMatrix/runSparseCsrTests.cpp index 393e9ad0b..066237c4c 100644 --- a/tests/UnitTests/LinearAlgebra/SparseMatrix/runSparseTests.cpp +++ b/tests/UnitTests/LinearAlgebra/SparseMatrix/runSparseCsrTests.cpp @@ -1,4 +1,4 @@ -#include "SparseTests.hpp" +#include "SparseCsrTests.hpp" using namespace GridKit; using namespace LinearAlgebra; diff --git a/tests/UnitTests/PhasorDynamics/SystemTests.hpp b/tests/UnitTests/PhasorDynamics/SystemTests.hpp index 9a2ad9399..28d8f3299 100644 --- a/tests/UnitTests/PhasorDynamics/SystemTests.hpp +++ b/tests/UnitTests/PhasorDynamics/SystemTests.hpp @@ -13,7 +13,7 @@ #include #include #include -#include +#include namespace GridKit { @@ -270,10 +270,11 @@ namespace GridKit // Evaluate and get the system Jacobian system.evaluateResidual(); system.evaluateJacobian(); - GridKit::LinearAlgebra::COO_Matrix system_jacobian = system.getJacobian(); - system_jacobian.printMatrix("System Jacobian"); + GridKit::LinearAlgebra::CsrMatrix* system_jacobian = system.getCsrJacobian(); + std::cout << "Sparse Csr Matrix: System Jacobian\n"; + system_jacobian->print(); - return GridKit::Testing::MapFromCOO(system_jacobian); + return GridKit::Testing::MapFromCsr(system_jacobian); } #endif };