Skip to content

Commit

Permalink
linting and small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
darioizzo committed Sep 26, 2024
1 parent 61940f9 commit c6bba2f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
33 changes: 16 additions & 17 deletions benchmark/leg_sims_flanagan_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ void perform_test_speed(unsigned N, unsigned nseg, unsigned pop_size)
double count_a = 0;
double count_n = 0;
std::cout << std::endl;
for (auto i = 0; i < N; ++i) {
for (auto i = 0u; i < N; ++i) {
// And some epochs / tofs.
double tof_days = tof_d(rng_engine);
double tof = tof_days * kep3::DAY2SEC;
double ts = ts_d(rng_engine);
double mass = mass_d(rng_engine);
const double tof_days = tof_d(rng_engine);
const double tof = tof_days * kep3::DAY2SEC;
const double ts = ts_d(rng_engine);
const double mass = mass_d(rng_engine);
auto rvs = earth.eph(ts);
auto rvf = jupiter.eph(ts + tof_days);
// We create a ballistic arc matching the two.
kep3::lambert_problem lp{rvs[0], rvf[0], tof, kep3::MU_SUN};
const kep3::lambert_problem lp{rvs[0], rvf[0], tof, kep3::MU_SUN};
rvs[1][0] = lp.get_v0()[0][0];
rvs[1][1] = lp.get_v0()[0][1];
rvs[1][2] = lp.get_v0()[0][2];
Expand All @@ -88,22 +88,21 @@ void perform_test_speed(unsigned N, unsigned nseg, unsigned pop_size)
prob_n.set_c_tol(1e-8);

// We construct the random chromosmes
pagmo::population pop{prob_a, pop_size};

const pagmo::population pop{prob_a, pop_size};

// First we time the analytical gradients
auto start = high_resolution_clock::now();
for (auto i = 0u; i < pop_size; ++i) {
prob_a.gradient(pop.get_x()[i]);
for (decltype(pop_size) j = 0u; j < pop_size; ++j) {
prob_a.gradient(pop.get_x()[j]);
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
count_a+=static_cast<double>(duration.count()) / 1e6;

// then the numerical ones
start = high_resolution_clock::now();
for (auto i = 0u; i < pop_size; ++i) {
prob_n.gradient(pop.get_x()[i]);
for (decltype(pop_size) j = 0u; j < pop_size; ++j) {
prob_n.gradient(pop.get_x()[j]);
}
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
Expand Down Expand Up @@ -145,16 +144,16 @@ void perform_test_convergence(unsigned N, unsigned nseg)
unsigned count_a = 0;
unsigned count_n = 0;
std::cout << std::endl;
for (auto i = 0; i < N; ++i) {
for (auto i = 0u; i < N; ++i) {
// And some epochs / tofs.
double tof_days = tof_d(rng_engine);
double tof = tof_days * kep3::DAY2SEC;
const double tof_days = tof_d(rng_engine);
const double tof = tof_days * kep3::DAY2SEC;
double ts = ts_d(rng_engine);
double mass = mass_d(rng_engine);
const double mass = mass_d(rng_engine);
auto rvs = earth.eph(ts);
auto rvf = jupiter.eph(ts + tof_days);
// We create a ballistic arc matching the two.
kep3::lambert_problem lp{rvs[0], rvf[0], tof, kep3::MU_SUN};
const kep3::lambert_problem lp{rvs[0], rvf[0], tof, kep3::MU_SUN};
rvs[1][0] = lp.get_v0()[0][0];
rvs[1][1] = lp.get_v0()[0][1];
rvs[1][2] = lp.get_v0()[0][2];
Expand Down
2 changes: 1 addition & 1 deletion benchmark/propagate_lagrangian_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void perform_test_speed(

auto start = high_resolution_clock::now();
for (auto i = 0u; i < N; ++i) {
auto res = propagate(pos_vels[i], tofs[i], 1., false);
propagate(pos_vels[i], tofs[i], 1., false);
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
Expand Down
46 changes: 23 additions & 23 deletions pykep/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
namespace py = pybind11;
namespace pk = pykep;

PYBIND11_MODULE(core, m)
PYBIND11_MODULE(core, m) //NOLINT
{
py::options options;
options.disable_function_signatures();
Expand Down Expand Up @@ -140,19 +140,19 @@ PYBIND11_MODULE(core, m)
// Constructor from datetime py::object
.def(py::init([](const py::object &in) {
// We check that `in` is a datetimeobject
py::object Datetime = py::module_::import("datetime").attr("datetime");
const py::object Datetime = py::module_::import("datetime").attr("datetime");
if (!py::isinstance(in, Datetime)) {
pykep::py_throw(PyExc_TypeError, ("it seems you are trying to construct kep3::epoch object from a "
"python object that is not of type datetime"));
}
// We collect its info
int y = in.attr("year").cast<int>();
const int y = in.attr("year").cast<int>();
auto m = in.attr("month").cast<unsigned>();
auto d = in.attr("day").cast<unsigned>();
int h = in.attr("hour").cast<int>();
int min = in.attr("minute").cast<int>();
int s = in.attr("second").cast<int>();
int us = in.attr("microsecond").cast<int>();
const int h = in.attr("hour").cast<int>();
const int min = in.attr("minute").cast<int>();
const int s = in.attr("second").cast<int>();
const int us = in.attr("microsecond").cast<int>();
return kep3::epoch(y, m, d, h, min, s, 0, us);
}),
py::arg("when"), pk::epoch_from_datetime_doc().c_str())
Expand Down Expand Up @@ -215,7 +215,7 @@ PYBIND11_MODULE(core, m)
auto vec_ptr = std::make_unique<std::vector<double>>(std::move(res));

py::capsule vec_caps(vec_ptr.get(), [](void *ptr) {
std::unique_ptr<std::vector<double>> vptr(static_cast<std::vector<double> *>(ptr));
const std::unique_ptr<std::vector<double>> vptr(static_cast<std::vector<double> *>(ptr));
});

// NOTE: at this point, the capsule has been created successfully (including
Expand Down Expand Up @@ -338,13 +338,13 @@ PYBIND11_MODULE(core, m)
auto pl_retval = kep3::propagate_lagrangian(pos_vel, dt, mu, request_stm);
if (pl_retval.second) {
// The stm was requested lets transfer ownership to python
std::array<double, 36> &stm = pl_retval.second.value();
const std::array<double, 36> &stm = pl_retval.second.value();

// We create a capsule for the py::array_t to manage ownership change.
auto vec_ptr = std::make_unique<std::array<double, 36>>(stm);

py::capsule vec_caps(vec_ptr.get(), [](void *ptr) {
std::unique_ptr<std::array<double, 36>> vptr(static_cast<std::array<double, 36> *>(ptr));
const std::unique_ptr<std::array<double, 36>> vptr(static_cast<std::array<double, 36> *>(ptr));
});

// NOTE: at this point, the capsule has been created successfully (including
Expand Down Expand Up @@ -394,13 +394,13 @@ PYBIND11_MODULE(core, m)
// Lets transfer ownership of dxdx to python (not sure this is actually needed to
// get an efficient return value ... maybe its overkill here). It surely avoid one more copy /
// allocation of 49+21 values, but in the overall algorithm maybe irrelevant.
std::array<double, 49> &dxdx = std::get<1>(sp_retval);
const std::array<double, 49> &dxdx = std::get<1>(sp_retval);

// We create a capsule for the py::array_t to manage ownership change.
auto vec_ptr = std::make_unique<std::array<double, 49>>(dxdx);

py::capsule vec_caps(vec_ptr.get(), [](void *ptr) {
std::unique_ptr<std::array<double, 49>> vptr(static_cast<std::array<double, 49> *>(ptr));
const std::unique_ptr<std::array<double, 49>> vptr(static_cast<std::array<double, 49> *>(ptr));
});

// NOTE: at this point, the capsule has been created successfully (including
Expand All @@ -415,13 +415,13 @@ PYBIND11_MODULE(core, m)
ptr->data(), std::move(vec_caps));

// Lets transfer ownership of dxdu to python
std::array<double, 21> &dxdu = std::get<2>(sp_retval);
const std::array<double, 21> &dxdu = std::get<2>(sp_retval);

// We create a capsule for the py::array_t to manage ownership change.
auto vec_ptr2 = std::make_unique<std::array<double, 21>>(dxdu);

py::capsule vec_caps2(vec_ptr2.get(), [](void *ptr) {
std::unique_ptr<std::array<double, 21>> vec_ptr2(static_cast<std::array<double, 21> *>(ptr));
const std::unique_ptr<std::array<double, 21>> vec_ptr2(static_cast<std::array<double, 21> *>(ptr));
});

// NOTE: at this point, the capsule has been created successfully (including
Expand Down Expand Up @@ -493,22 +493,22 @@ PYBIND11_MODULE(core, m)
[](const kep3::leg::sims_flanagan &leg) {
auto tc_cpp = leg.compute_mc_grad();
// Lets transfer ownership to python of the three
std::array<double, 49> &rs_addr = std::get<0>(tc_cpp);
std::array<double, 49> &rf_addr = std::get<1>(tc_cpp);
std::vector<double> &th_addr = std::get<2>(tc_cpp);
const std::array<double, 49> &rs_addr = std::get<0>(tc_cpp);
const std::array<double, 49> &rf_addr = std::get<1>(tc_cpp);
const std::vector<double> &th_addr = std::get<2>(tc_cpp);

// We create three separate capsules for the py::array_t to manage ownership change.
auto vec_ptr_rs = std::make_unique<std::array<double, 49>>(rs_addr);
py::capsule vec_caps_rs(vec_ptr_rs.get(), [](void *ptr) {
std::unique_ptr<std::array<double, 49>> vptr(static_cast<std::array<double, 49> *>(ptr));
const std::unique_ptr<std::array<double, 49>> vptr(static_cast<std::array<double, 49> *>(ptr));
});
auto vec_ptr_rf = std::make_unique<std::array<double, 49>>(rf_addr);
py::capsule vec_caps_rf(vec_ptr_rf.get(), [](void *ptr) {
std::unique_ptr<std::array<double, 49>> vptr(static_cast<std::array<double, 49> *>(ptr));
const std::unique_ptr<std::array<double, 49>> vptr(static_cast<std::array<double, 49> *>(ptr));
});
auto vec_ptr_th = std::make_unique<std::vector<double>>(th_addr);
py::capsule vec_caps_th(vec_ptr_th.get(), [](void *ptr) {
std::unique_ptr<std::vector<double>> vptr(static_cast<std::vector<double> *>(ptr));
const std::unique_ptr<std::vector<double>> vptr(static_cast<std::vector<double> *>(ptr));
});
// NOTE: at this point, the capsules have been created successfully (including
// the registration of the destructor). We can thus release ownership from vec_ptr_xx,
Expand All @@ -532,13 +532,13 @@ PYBIND11_MODULE(core, m)
.def(
"compute_tc_grad",
[](const kep3::leg::sims_flanagan &leg) {
std::vector<double> tc_cpp = leg.compute_tc_grad();
const std::vector<double> tc_cpp = leg.compute_tc_grad();
// Lets transfer ownership to python
std::vector<double> &tc_cpp_addr = tc_cpp;
const std::vector<double> &tc_cpp_addr = tc_cpp;
// We create a capsule for the py::array_t to manage ownership change.
auto vec_ptr = std::make_unique<std::vector<double>>(tc_cpp_addr);
py::capsule vec_caps(vec_ptr.get(), [](void *ptr) {
std::unique_ptr<std::vector<double>> vptr(static_cast<std::vector<double> *>(ptr));
const std::unique_ptr<std::vector<double>> vptr(static_cast<std::vector<double> *>(ptr));
});
// NOTE: at this point, the capsule has been created successfully (including
// the registration of the destructor). We can thus release ownership from vec_ptr,
Expand Down
2 changes: 1 addition & 1 deletion src/core_astro/flyby.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ std::pair<double, double> fb_con(const std::array<double, 3> &v_rel_in, const st
std::pair<double, double> fb_con(const std::array<double, 3> &v_rel_in, const std::array<double, 3> &v_rel_out,
const kep3::planet &pl)
{
return std::move(fb_con(v_rel_in, v_rel_out, pl.get_mu_self(), pl.get_safe_radius()));
return fb_con(v_rel_in, v_rel_out, pl.get_mu_self(), pl.get_safe_radius());
}

double fb_dv(const std::array<double, 3> &v_rel_in, const std::array<double, 3> &v_rel_out, double mu,
Expand Down

0 comments on commit c6bba2f

Please sign in to comment.