Skip to content

fix several memory leaks #134

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 8 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#include <iomanip>
#include <iostream>
#include <memory>

#include <IpIpoptApplication.hpp>
#include <IpSolveStatistics.hpp>
Expand All @@ -20,47 +20,47 @@ int main()
using namespace GridKit::Testing;

// Create an infinite bus
BaseBus<double, size_t>* bus = new BusSlack<double, size_t>(1.0, 0.0);
std::unique_ptr<BaseBus<double, size_t>> bus = std::make_unique<BusSlack<double, size_t>>(1.0, 0.0);

// Attach a generator to that bus
Generator2<double, size_t>* gen = new Generator2<double, size_t>(bus);
Generator2<double, size_t> gen(bus.get());

// Create a system model
SystemModel<double, size_t>* model = new SystemModel<double, size_t>();
model->addBus(bus);
model->addComponent(gen);
SystemModel<double, size_t> model;
model.addBus(bus.get());
model.addComponent(&gen);

// allocate model components
model->allocate();
model.allocate();

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

double t_init = 0.0;
double t_final = 20.0;

// setup simulation
idas->configureSimulation();
idas->configureAdjoint();
idas->getDefaultInitialCondition();
idas->initializeSimulation(t_init);
idas->configureQuadrature();
idas->initializeQuadrature();
idas.configureSimulation();
idas.configureAdjoint();
idas.getDefaultInitialCondition();
idas.initializeSimulation(t_init);
idas.configureQuadrature();
idas.initializeQuadrature();

double t_fault = 0.02;
double t_clear = 0.06;
idas->runSimulation(t_fault);
idas.runSimulation(t_fault);
// create initial condition after a fault
{
gen->V() = 0.0;
idas->runSimulation(t_clear, 2);
gen->V() = 1.0;
gen->theta() = -0.01;
idas->saveInitialCondition();
gen.V() = 0.0;
idas.runSimulation(t_clear, 2);
gen.V() = 1.0;
gen.theta() = -0.01;
idas.saveInitialCondition();
}

// Set integration time for dynamic constrained optimization
idas->setIntegrationTime(t_init, t_final, 100);
idas.setIntegrationTime(t_init, t_final, 100);

// Guess optimization parameter value
double Pm = 0.7;
Expand All @@ -87,10 +87,10 @@ int main()

// Create dynamic objective interface to Ipopt solver
Ipopt::SmartPtr<Ipopt::TNLP> ipoptDynamicObjectiveInterface =
new IpoptInterface::DynamicObjective<double, size_t>(idas);
new IpoptInterface::DynamicObjective<double, size_t>(&idas);

// Initialize problem
model->param()[0] = Pm;
model.param()[0] = Pm;

// Solve the problem
status = ipoptApp->OptimizeTNLP(ipoptDynamicObjectiveInterface);
Expand All @@ -101,24 +101,24 @@ int main()
// Print result
std::cout << "\nSucess:\n The problem solved in "
<< ipoptApp->Statistics()->IterationCount() << " iterations!\n"
<< " Optimal value of Pm = " << model->param()[0] << "\n"
<< " Optimal value of Pm = " << model.param()[0] << "\n"
<< " The final value of the objective function G(Pm) = "
<< ipoptApp->Statistics()->FinalObjective() << "\n\n";
}

// Store dynamic objective optimization results
double* results = new double[model->sizeParams()];
for (unsigned i = 0; i < model->sizeParams(); ++i)
double* results = new double[model.sizeParams()];
for (unsigned i = 0; i < model.sizeParams(); ++i)
{
results[i] = model->param()[i];
results[i] = model.param()[i];
}

// Create dynamic constraint interface to Ipopt solver
Ipopt::SmartPtr<Ipopt::TNLP> ipoptDynamicConstraintInterface =
new IpoptInterface::DynamicConstraint<double, size_t>(idas);
new IpoptInterface::DynamicConstraint<double, size_t>(&idas);

// Initialize problem
model->param()[0] = Pm;
model.param()[0] = Pm;

// Solve the problem
status = ipoptApp->OptimizeTNLP(ipoptDynamicConstraintInterface);
Expand All @@ -129,16 +129,16 @@ int main()
// Print result
std::cout << "\nSucess:\n The problem solved in "
<< ipoptApp->Statistics()->IterationCount() << " iterations!\n"
<< " Optimal value of Pm = " << model->param()[0] << "\n"
<< " Optimal value of Pm = " << model.param()[0] << "\n"
<< " The final value of the objective function G(Pm) = "
<< ipoptApp->Statistics()->FinalObjective() << "\n\n";
}

// Compare results of the two optimization methods
int retval = 0;
for (unsigned i = 0; i < model->sizeParams(); ++i)
for (unsigned i = 0; i < model.sizeParams(); ++i)
{
if (!isEqual(results[i], model->param()[i], 10 * tol))
if (!isEqual(results[i], model.param()[i], 10 * tol))
--retval;
}

Expand All @@ -148,7 +148,5 @@ int main()
}

delete[] results;
delete idas;
delete model;
return retval;
}
78 changes: 38 additions & 40 deletions examples/Experimental/GenInfiniteBus/GenInfiniteBus.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#include <iomanip>
#include <iostream>
#include <memory>

#include <IpIpoptApplication.hpp>
#include <IpSolveStatistics.hpp>
Expand All @@ -20,49 +20,49 @@ int main()
using namespace GridKit::Testing;

// Create an infinite bus
BaseBus<double, size_t>* bus = new BusSlack<double, size_t>(1.0, 0.0);
std::unique_ptr<BaseBus<double, size_t>> bus = std::make_unique<BusSlack<double, size_t>>(1.0, 0.0);

// Attach a generator to that bus
Generator4<double, size_t>* gen = new Generator4<double, size_t>(bus);
Generator4<double, size_t> gen(bus.get());

// Create a system model
SystemModel<double, size_t>* model = new SystemModel<double, size_t>();
model->addBus(bus);
model->addComponent(gen);
SystemModel<double, size_t> model;
model.addBus(bus.get());
model.addComponent(&gen);

// allocate model components
model->allocate();
model.allocate();

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

double t_init = 0.0;
double t_final = 15.0;

// setup simulation
idas->configureSimulation();
idas->configureAdjoint();
idas->getDefaultInitialCondition();
idas->initializeSimulation(t_init);
idas->configureQuadrature();
idas->initializeQuadrature();
idas.configureSimulation();
idas.configureAdjoint();
idas.getDefaultInitialCondition();
idas.initializeSimulation(t_init);
idas.configureQuadrature();
idas.initializeQuadrature();

double t_fault = 0.1;
double t_clear = 0.1;
idas->runSimulation(t_fault);
idas->saveInitialCondition();
idas.runSimulation(t_fault);
idas.saveInitialCondition();
// create initial condition after a fault
{
idas->getSavedInitialCondition();
idas->initializeSimulation(t_init);
gen->V() = 0.0;
idas->runSimulation(t_clear, 20);
gen->V() = 1.0;
idas->saveInitialCondition();
idas.getSavedInitialCondition();
idas.initializeSimulation(t_init);
gen.V() = 0.0;
idas.runSimulation(t_clear, 20);
gen.V() = 1.0;
idas.saveInitialCondition();
}

// Set integration time for dynamic constrained optimization
idas->setIntegrationTime(t_init, t_final, 100);
idas.setIntegrationTime(t_init, t_final, 100);

// Guess initial values of optimization parameters
double Pm = 1.0;
Expand Down Expand Up @@ -90,11 +90,11 @@ int main()

// Create dynamic objective interface to Ipopt solver
Ipopt::SmartPtr<Ipopt::TNLP> ipoptDynamicObjectiveInterface =
new IpoptInterface::DynamicObjective<double, size_t>(idas);
new IpoptInterface::DynamicObjective<double, size_t>(&idas);

// Initialize the problem
model->param()[0] = Pm;
model->param()[1] = Ef;
model.param()[0] = Pm;
model.param()[1] = Ef;

// Solve the problem
status = ipoptApp->OptimizeTNLP(ipoptDynamicObjectiveInterface);
Expand All @@ -105,26 +105,26 @@ int main()
// Print result
std::cout << "\nSucess:\n The problem solved in "
<< ipoptApp->Statistics()->IterationCount() << " iterations!\n"
<< " Optimal value of Pm = " << model->param()[0] << "\n"
<< " Optimal value of Ef = " << model->param()[1] << "\n"
<< " Optimal value of Pm = " << model.param()[0] << "\n"
<< " Optimal value of Ef = " << model.param()[1] << "\n"
<< " The final value of the objective function G(Pm,Ef) = "
<< ipoptApp->Statistics()->FinalObjective() << "\n\n";
}

// Create dynamic constraint interface to Ipopt solver
Ipopt::SmartPtr<Ipopt::TNLP> ipoptDynamicConstraintInterface =
new IpoptInterface::DynamicConstraint<double, size_t>(idas);
new IpoptInterface::DynamicConstraint<double, size_t>(&idas);

// Store dynamic objective optimization results
double* results = new double[model->sizeParams()];
for (unsigned i = 0; i < model->sizeParams(); ++i)
double* results = new double[model.sizeParams()];
for (unsigned i = 0; i < model.sizeParams(); ++i)
{
results[i] = model->param()[i];
results[i] = model.param()[i];
}

// Initialize the problem
model->param()[0] = Pm;
model->param()[1] = Ef;
model.param()[0] = Pm;
model.param()[1] = Ef;

// Solve the problem
status = ipoptApp->OptimizeTNLP(ipoptDynamicConstraintInterface);
Expand All @@ -135,17 +135,17 @@ int main()
// Print result
std::cout << "\nSucess:\n The problem solved in "
<< ipoptApp->Statistics()->IterationCount() << " iterations!\n"
<< " Optimal value of Pm = " << model->param()[0] << "\n"
<< " Optimal value of Ef = " << model->param()[1] << "\n"
<< " Optimal value of Pm = " << model.param()[0] << "\n"
<< " Optimal value of Ef = " << model.param()[1] << "\n"
<< " The final value of the objective function G(Pm,Ef) = "
<< ipoptApp->Statistics()->FinalObjective() << "\n\n";
}

// Compare results of the two optimization methods
int retval = 0;
for (unsigned i = 0; i < model->sizeParams(); ++i)
for (unsigned i = 0; i < model.sizeParams(); ++i)
{
if (!isEqual(results[i], model->param()[i], 100 * tol))
if (!isEqual(results[i], model.param()[i], 100 * tol))
--retval;
}

Expand All @@ -155,7 +155,5 @@ int main()
}

delete[] results;
delete idas;
delete model;
return 0;
}
Loading
Loading