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

Working Example of Solving RLCircuit and Microgrid with Jacobians #1

Merged
merged 46 commits into from
May 3, 2024
Merged
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
cbabd88
Add bus factory.
pelesh Oct 20, 2022
1603057
Add New Components and Changes to Composition
reid-g Jul 27, 2023
b9ce19f
Added COO Sparse Matrices, Component Jacobians, and Discrete Generator
reid-g Nov 16, 2023
be22b55
Added Jacobian Assembly. Fixed and Added New Functionality to COO Mat…
reid-g Nov 20, 2023
3f74458
Jacobian Assembly, IDA + KLU Cmake, RL Circuit
reid-g Dec 11, 2023
a42761c
Added the Transmission Line Component
reid-g Jan 30, 2024
fcf1cb9
Added Microgrid Example
reid-g Feb 19, 2024
1aa8865
Working Microgrid (Finite Difference)
reid-g Feb 19, 2024
723c785
Fixed the Jacobian in Microgrid Example
reid-g Feb 22, 2024
a3e5af8
Added a max step size control for model objects
reid-g Feb 22, 2024
c3f071e
Update Solver/Dynamic/CMakeLists.txt
reid-g Mar 12, 2024
7be79c7
Updated name DiscreteGenerator -> DistributedGenerator
reid-g Mar 12, 2024
d715372
Fixed Some Bugs
reid-g Mar 12, 2024
001d3c2
Fixed Memory Error with Grid3BusSys
reid-g Mar 15, 2024
d258ad5
Fixed Unitalized Variable Stepsize
reid-g Mar 22, 2024
c8de307
Fix max number of backward steps in IDA solver.
pelesh Apr 9, 2024
36df264
Merge pull request #4 from ORNL/composer-ida-fix
reid-g Apr 9, 2024
31e6f68
Fixing indentation from tabs to spaces
reid-g Apr 9, 2024
f8d6943
Formatting Fix Composer-Dev (#6)
reid-g Apr 10, 2024
8f55e94
Remove parts of generator model from BusPV (#7)
pelesh Apr 10, 2024
7bb9374
Formatting updates + Fixed Warnings
reid-g Apr 10, 2024
425b7d0
Add bus factory.
pelesh Oct 20, 2022
e0c778e
Add New Components and Changes to Composition
reid-g Jul 27, 2023
b57ec42
Added COO Sparse Matrices, Component Jacobians, and Discrete Generator
reid-g Nov 16, 2023
de2307c
Added Jacobian Assembly. Fixed and Added New Functionality to COO Mat…
reid-g Nov 20, 2023
1dc614c
Jacobian Assembly, IDA + KLU Cmake, RL Circuit
reid-g Dec 11, 2023
e005183
Added the Transmission Line Component
reid-g Jan 30, 2024
eb6fde4
Added Microgrid Example
reid-g Feb 19, 2024
1952b25
Working Microgrid (Finite Difference)
reid-g Feb 19, 2024
efc71f8
Fixed the Jacobian in Microgrid Example
reid-g Feb 22, 2024
9f99077
Added a max step size control for model objects
reid-g Feb 22, 2024
60b328c
Update Solver/Dynamic/CMakeLists.txt
reid-g Mar 12, 2024
6c223b3
Updated name DiscreteGenerator -> DistributedGenerator
reid-g Mar 12, 2024
226afca
Fixed Some Bugs
reid-g Mar 12, 2024
861b33c
Fixed Memory Error with Grid3BusSys
reid-g Mar 15, 2024
efddb28
Fixed Unitalized Variable Stepsize
reid-g Mar 22, 2024
3cb53a8
Fix max number of backward steps in IDA solver.
pelesh Apr 9, 2024
a2e71ab
Fixing indentation from tabs to spaces
reid-g Apr 9, 2024
17a0cb7
Formatting Fix Composer-Dev (#6)
reid-g Apr 10, 2024
d2ac21a
Formatting updates + Fixed Warnings
reid-g Apr 10, 2024
a192d47
Rebasing with BusPV Fixes
reid-g Apr 10, 2024
b0c02a1
Merge branch 'composer-dev' of https://github.com/ORNL/GridKit into c…
reid-g Apr 10, 2024
ad7f4af
Minor comments and style corrections.
pelesh Apr 13, 2024
7e685c5
Multiple Format & Comment Updates + Valgrind Error Fixes
reid-g Apr 16, 2024
a43404f
Formatting Updates with "this" and using + Minor Fixes
reid-g Apr 17, 2024
9250411
Added Better Description to the MicrogridBus
reid-g May 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added Jacobian Assembly. Fixed and Added New Functionality to COO Mat…
…rices

+Matrix Assembly
+Refactored COO Matrix
+COO can return CSR Data format
reid-g committed Apr 10, 2024
commit de2307c4b2f22914a30a47ac8155ec253a9d5738
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ int Capacitor<ScalarT, IdxT>::evaluateJacobian()
COO_Matrix<ScalarT,IdxT> Jacder = COO_Matrix<ScalarT, IdxT>(rcordder, ccordder, valsder,3,3);

//Perform dF/dy + \alpha dF/dy'
this->J_.AXPY(this->alpha_, &Jacder);
this->J_.AXPY(this->alpha_, Jacder);

return 0;
}
Original file line number Diff line number Diff line change
@@ -237,7 +237,7 @@ int DiscreteGenerator<ScalarT, IdxT>::evaluateJacobian()

//Perform dF/dy + \alpha dF/dy'

this->J_.AXPY(-this->alpha_, &Jacder);
this->J_.AXPY(-this->alpha_, Jacder);

return 0;
}
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ int Inductor<ScalarT, IdxT>::evaluateJacobian()
COO_Matrix<ScalarT,IdxT> Jacder = COO_Matrix<ScalarT, IdxT>(rcordder, ccordder, valsder,3,3);

//Perform dF/dy + \alpha dF/dy'
this->J_.AXPY(this->alpha_, &Jacder);
this->J_.AXPY(this->alpha_, Jacder);

return 0;
}
9 changes: 6 additions & 3 deletions Examples/RLCircuit/RLCircuit.cpp
Original file line number Diff line number Diff line change
@@ -80,9 +80,12 @@ int main(int argc, char const *argv[])
}
std::cout << "}\n";

induct->updateTime(0.0, 1.0);
induct->evaluateJacobian();
induct->getJacobian().printMatrix(true);

sysmodel->updateTime(0.0, 1.0);
sysmodel->evaluateJacobian();
sysmodel->getJacobian().printMatrix();




return 0;
49 changes: 40 additions & 9 deletions Examples/SparseTest/SparseTest.cpp
Original file line number Diff line number Diff line change
@@ -10,9 +10,6 @@

#include <SparseMatrix/COO_Matrix.hpp>




int main(int argc, char const *argv[])
{
std::vector<double> val{0.1, 0.2, 0.3, 0.4};
@@ -35,21 +32,55 @@ int main(int argc, char const *argv[])
}

std::cout << "A:\n";
A.printMatrix(true);
A.printMatrix();

std::vector<double> val2{0.5, 0.6, 0.7, 0.8, 1.0};
std::vector<size_t> x2{0,2,0,2,1};
std::vector<size_t> y2{3,3,2,2,3};
COO_Matrix<double, size_t> B = COO_Matrix<double, size_t>(x2,y2,val2,m,n);

std::cout << "B:\n";
B.printMatrix(true);
B.printMatrix();

A.AXPY(2.0, &B);
A.AXPY(2.0, B);

std::cout << "A + 2B:\n";
A.printMatrix(true);

A.printMatrix();

std::vector<size_t> r;
std::vector<size_t> c;
std::vector<double> v;
std::tie(r,c,v) = A.getDataToCSR();

for (size_t i = 0; i < r.size() - 1; i++)
{
std::cout << r[i] << std::endl;
size_t rdiff = r[i+1] - r[i];
for (size_t j = 0; j < rdiff; j++)
{
std::cout << c[j + r[i]] << ", " << v[j + r[i]] << std::endl;
}
}
std::cout << r[r.size()-1] << std::endl;

//Basic Verification test
std::vector<size_t> rtest = {0, 2, 4, 7, 8};
std::vector<size_t> ctest = {2,3,2,3,1,2,3,2};
std::vector<double> valtest = {1.4, 1.0, 0.4, 2.2, 0.1, 1.6, 1.2, 0.3};

assert(rtest.size() == r.size());
assert(ctest.size() == c.size());
assert(valtest.size() == v.size());

int failval = 0;
for (size_t i = 0; i < rtest.size(); i++) if (r[i] != rtest[i]) failval--;
for (size_t i = 0; i < ctest.size(); i++)
{
double vdiff = v[i] - valtest[i];
if (c[i] != ctest[i] || -1e-14 > vdiff || vdiff > 1e-14) failval--;
}

return 0;
std::cout << failval << std::endl;

return failval;
}
65 changes: 53 additions & 12 deletions PowerElectronicsModel.hpp
Original file line number Diff line number Diff line change
@@ -23,8 +23,8 @@ class PowerElectronicsModel : public ModelEvaluatorImpl<ScalarT, IdxT>
// using ModelEvaluatorImpl<ScalarT, IdxT>::size_quad_;
// using ModelEvaluatorImpl<ScalarT, IdxT>::size_opt_;
using ModelEvaluatorImpl<ScalarT, IdxT>::nnz_;
// using ModelEvaluatorImpl<ScalarT, IdxT>::time_;
// using ModelEvaluatorImpl<ScalarT, IdxT>::alpha_;
using ModelEvaluatorImpl<ScalarT, IdxT>::time_;
using ModelEvaluatorImpl<ScalarT, IdxT>::alpha_;
using ModelEvaluatorImpl<ScalarT, IdxT>::y_;
using ModelEvaluatorImpl<ScalarT, IdxT>::yp_;
// using ModelEvaluatorImpl<ScalarT, IdxT>::yB_;
@@ -98,6 +98,11 @@ class PowerElectronicsModel : public ModelEvaluatorImpl<ScalarT, IdxT>
return 0;
}

/**
* @brief Set intial y and y' of each component
*
* @return int
*/
int initialize()
{

@@ -106,7 +111,18 @@ class PowerElectronicsModel : public ModelEvaluatorImpl<ScalarT, IdxT>
{
component->initialize();
}
this->distributeVectors();

return 0;
}

/**
* @brief Distribute y and y' to each component based of node connection graph
*
* @return int
*/
int distributeVectors()
{
for(const auto& component : components_)
{
for(IdxT j=0; j<component->size(); ++j)
@@ -123,23 +139,19 @@ class PowerElectronicsModel : public ModelEvaluatorImpl<ScalarT, IdxT>
return 0;
}

/**
* @brief Evaluate Residuals at each component then collect them
*
* @return int
*/
int evaluateResidual()
{
for (IdxT i = 0; i < this->f_.size(); i++)
{
f_[i] = 0;
}

// Update variables
for(const auto& component : components_)
{
for(IdxT j=0; j<component->size(); ++j)
{
component->y()[j] = y_[component->getNodeConnection(j)];
component->yp()[j] = yp_[component->getNodeConnection(j)];
}
component->evaluateResidual();
}
this->distributeVectors();

// Update system residual vector

@@ -161,6 +173,25 @@ class PowerElectronicsModel : public ModelEvaluatorImpl<ScalarT, IdxT>
*/
int evaluateJacobian()
{
this->J_.zeroMatrix();
this->distributeVectors();

//Evaluate component jacs
for(const auto& component : components_)
{
component->evaluateJacobian();
std::vector<IdxT> r;
std::vector<IdxT> c;
std::vector<ScalarT> v;
std::tie(r, c, v) = component->getJacobian().getEntrieCopies();
for (IdxT i = 0; i < static_cast<IdxT>(r.size()); i++)
{
r[i] = component->getNodeConnection(r[i]);
c[i] = component->getNodeConnection(c[i]);
}
this->J_.AXPY(1.0, r, c, v);
}

return 0;
}

@@ -205,8 +236,18 @@ class PowerElectronicsModel : public ModelEvaluatorImpl<ScalarT, IdxT>
return 0;
}

/**
* @brief Distribute time and time scaling for each component
*
* @param t
* @param a
*/
void updateTime(ScalarT t, ScalarT a)
{
for(const auto& component : components_)
{
component->updateTime(t, a);
}
}

void addComponent(component_type* component)
521 changes: 334 additions & 187 deletions SparseMatrix/COO_Matrix.hpp

Large diffs are not rendered by default.