Skip to content

Refactor examples #110

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

Merged
merged 9 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
40 changes: 28 additions & 12 deletions examples/PhasorDynamics/Example1/example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ int main()
double stop = static_cast<double>(clock());

double error_V = 0.0; // error in |V|
double error_w = 0.0; // error in rotor speed

// Read through the simulation data stored in the buffer.
// Since we captured by reference, output should be available
Expand All @@ -141,22 +142,37 @@ int main()
if (err > error_V)
error_V = err;

std::cout << "GridKit: t = " << data.ti
<< ", |V| = " << std::sqrt(data.Vr * data.Vr + data.Vi * data.Vi)
<< ", w = " << (1.0 + data.dw) << "\n";
std::cout << "Ref : t = " << ref_sol[0]
<< ", |V| = " << ref_sol[2]
<< ", w = " << ref_sol[1]
<< "\n";
std::cout << "Error in |V| = "
<< err
<< "\n";
std::cout << "\n";
err = std::abs(1.0 + data.dw - ref_sol[1]) / (1.0 + ref_sol[1]);
if (err > error_w)
error_w = err;

// // Optional output
// std::cout << "GridKit: t = " << data.ti
// << ", |V| = " << std::sqrt(data.Vr * data.Vr + data.Vi * data.Vi)
// << ", w = " << (1.0 + data.dw) << "\n";
// std::cout << "Ref : t = " << ref_sol[0]
// << ", |V| = " << ref_sol[2]
// << ", w = " << ref_sol[1]
// << "\n";
// std::cout << "Error in |V| = "
// << err
// << "\n";
// std::cout << "\n";
}

double error_V_allowed = 2e-4;
double error_w_allowed = 1e-4;

// Tolerances based on Powerworld reference accuracy
int status = 0;
std::cout << "Max error in |V| = " << error_V << "\n";
if (error_V > 2e-4)
if (error_V > error_V_allowed)
{
std::cout << "Test failed with error too large!\n";
status = 1;
}
std::cout << "Max error in w = " << error_w << "\n";
if (error_w > error_w_allowed)
{
std::cout << "Test failed with error too large!\n";
status = 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/PhasorDynamics/Example2/example2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ int main()
}
// fileout.close();

std::cout << "Worst error " << worst_error
std::cout << "Max error " << worst_error
<< " at time t = " << worst_error_time << "\n";
std::cout << "\n\nComplete in " << (stop - start) / CLOCKS_PER_SEC << " seconds\n";

Expand Down
27 changes: 18 additions & 9 deletions examples/PowerElectronics/DistributedGeneratorTest/DGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @param argv
* @return int
*/
int main(int argc, char const* argv[])
int main(int /* argc */, char const** /* argv */)
{

GridKit::DistributedGeneratorParameters<double, size_t> parms;
Expand Down Expand Up @@ -49,12 +49,12 @@ int main(int argc, char const* argv[])

dg->evaluateResidual();

std::cout << "Output: {";
for (double i : dg->getResidual())
{
printf("%e ,", i);
}
std::cout << "}\n";
// std::cout << "Output: {";
// for (double i : dg->getResidual())
// {
// printf("%e ,", i);
// }
// std::cout << "}\n";

// Generated from matlab code with same parameters and inputs
std::vector<double> true_vec{3.141592277589793e+02,
Expand All @@ -74,11 +74,20 @@ int main(int argc, char const* argv[])
3.337988298081817e+03,
2.684419146397466e+03};

std::cout << "Test the Relative Error\n";
std::cout << "Testing the DistributedGenerator model ...\n";
double error_allowed = 10 * std::numeric_limits<double>::epsilon();
for (size_t i = 0; i < true_vec.size(); i++)
{
printf("%e ,\n", (true_vec[i] - dg->getResidual()[i]) / true_vec[i]);
double error = std::abs(true_vec[i] - dg->getResidual()[i]) / std::abs(1.0 + true_vec[i]);
if (error > error_allowed)
{
std::cout << "Model error for equation " << i << " is: " << error << "\n";
std::cout << "Maximum allowed error is: " << error_allowed << "\n";
std::cout << "Test FAILED!\n";
return 1;
}
}
std::cout << "Test successful!\n";

return 0;
}
63 changes: 43 additions & 20 deletions examples/PowerElectronics/Microgrid/Microgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
#include <Solver/Dynamic/DynamicSolver.hpp>
#include <Solver/Dynamic/Ida.hpp>

int main(int argc, char const* argv[])
int main(int /* argc */, char const** /* argv */)
{
///@todo Needs to be modified. Some components are small relative to others thus there error is high (or could be matlab vector issue)
/// @todo Needs to be modified. Some components are small relative to others thus
/// there error is high (or could be matlab vector issue)
double abs_tol = 1.0e-8;
double rel_tol = 1.0e-8;
size_t max_step_amount = 3000;
size_t max_step_number = 3000;
bool use_jac = true;

// Create model
GridKit::PowerElectronicsModel<double, size_t>* sysmodel = new GridKit::PowerElectronicsModel<double, size_t>(rel_tol, abs_tol, use_jac, max_step_amount);
auto* sysmodel = new GridKit::PowerElectronicsModel<double, size_t>(rel_tol, abs_tol, use_jac, max_step_number);

// Modeled after the problem in the paper
double RN = 1.0e4;
Expand Down Expand Up @@ -315,20 +316,21 @@ int main(int argc, char const* argv[])
sysmodel->initialize();
sysmodel->evaluateResidual();

std::vector<double>& 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 debugging output
// std::vector<double>& 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";

sysmodel->updateTime(0.0, 1.0e-8);
sysmodel->evaluateJacobian();
std::cout << "Intial Jacobian with alpha:\n";

// Create numerical integrator and configure it for the generator model
AnalysisManager::Sundials::Ida<double, size_t>* idas = new AnalysisManager::Sundials::Ida<double, size_t>(sysmodel);
auto* idas = new AnalysisManager::Sundials::Ida<double, size_t>(sysmodel);

double t_init = 0.0;
double t_final = 1.0;
Expand All @@ -342,13 +344,14 @@ int main(int argc, char const* argv[])

std::vector<double>& yfinial = sysmodel->y();

std::cout << "Final Vector y\n";
for (size_t i = 0; i < yfinial.size(); i++)
{
std::cout << yfinial[i] << "\n";
}
// // Optional debugging output
// std::cout << "Final Vector y\n";
// for (size_t i = 0; i < yfinial.size(); i++)
// {
// std::cout << yfinial[i] << "\n";
// }

// Generate from MATLAB code ODE form with tolerances of 1e-12
// Generated from MATLAB code ODE form with tolerances of 1e-12
std::vector<double> true_vec{
2.297543153595780e+04,
1.275311524125022e+04,
Expand Down Expand Up @@ -421,11 +424,31 @@ int main(int argc, char const* argv[])
3.604108939430972e+02,
-3.492842627398574e+01};

std::cout << "Test the Relative Error\n";
// std::cout << "Test the Relative Error\n";
// for (size_t i = 0; i < true_vec.size(); i++)
// {
// printf("%lu : %e ,\n", i, abs(true_vec[i] - yfinial[i]) / abs(true_vec[i]));
// }

std::cout << "Testing the DistributedGenerator model ...\n";
double error_allowed = 1e-4;
double max_error = 0.0;
for (size_t i = 0; i < true_vec.size(); i++)
{
printf("%lu : %e ,\n", i, abs(true_vec[i] - yfinial[i]) / abs(true_vec[i]));
double error = std::abs(true_vec[i] - yfinial[i]) / std::abs(1.0 + true_vec[i]);
if (error > max_error)
max_error = error;
if (error > error_allowed)
{
std::cout << "Model error for equation " << i << " is: " << error << "\n";
std::cout << "Maximum allowed error is: " << error_allowed << "\n";
std::cout << "Test FAILED!\n";
return 1;
}
}
std::cout << "Max error = " << max_error << "\n";
std::cout << "Allowed error = " << error_allowed << "\n";
std::cout << "Test successful!\n";

delete idas;
delete sysmodel;
Expand Down
2 changes: 1 addition & 1 deletion examples/PowerFlow/Grid3Bus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The mathematical model of the power flow problem is formulated as a set of nonli
The model and its parameters are described in Figure 1:

<div align="center">
<img align="center" src="../../docs/Figures/example1.jpg">
<img align="center" src="../../../docs/Figures/example1.jpg">


Figure 1: A simple 3-bus grid example.
Expand Down
Loading