Skip to content

Commit

Permalink
Merge pull request #39 from darioizzo/main
Browse files Browse the repository at this point in the history
Improvements on const usage
  • Loading branch information
darioizzo authored Dec 15, 2024
2 parents ad30054 + 449aa92 commit 7841842
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Build
run: bash tools/gha_linux_asan_debug.sh
osx_11_debug:
runs-on: macos-13
runs-on: macos-15
steps:
- uses: actions/checkout@v4
- name: Build
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ target_include_directories(kep3 PUBLIC
# NOTE: need 1.69 for safe numerics.
set(_kep3_MIN_BOOST_VERSION "1.69")

find_package(Boost ${_kep3_MIN_BOOST_VERSION} REQUIRED)
find_package(Boost ${_kep3_MIN_BOOST_VERSION} CONFIG REQUIRED)
target_link_libraries(kep3 PUBLIC Boost::boost)
# NOTE: quench warnings from Boost when building the library.
target_compile_definitions(kep3 PRIVATE BOOST_ALLOW_DEPRECATED_HEADERS)
Expand All @@ -202,7 +202,7 @@ if(${fmt_VERSION_MAJOR} LESS 10)
endif()

# heyoka.
find_package(heyoka 5.0.0 REQUIRED CONFIG)
find_package(heyoka 6 CONFIG REQUIRED)
target_link_libraries(kep3 PUBLIC heyoka::heyoka)

# spdlog.
Expand Down
2 changes: 1 addition & 1 deletion include/kep3/leg/sims_flanagan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class kep3_DLL_PUBLIC sims_flanagan
// Default Constructor.
sims_flanagan() = default;
// Constructors
sims_flanagan(const std::array<std::array<double, 3>, 2> &rvs, double ms, std::vector<double> throttles,
sims_flanagan(const std::array<std::array<double, 3>, 2> &rvs, double ms, const std::vector<double> &throttles,
const std::array<std::array<double, 3>, 2> &rvf, double mf, double tof, double max_thrust, double isp,
double mu, double cut = 0.5);

Expand Down
8 changes: 4 additions & 4 deletions include/kep3/leg/sims_flanagan_hf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ class kep3_DLL_PUBLIC sims_flanagan_hf
// Default Constructor.
sims_flanagan_hf(); // = default;
// Backwards-compatible constructor with rv and m states separately
sims_flanagan_hf(const std::array<std::array<double, 3>, 2> &rvs, double ms, std::vector<double> throttles,
sims_flanagan_hf(const std::array<std::array<double, 3>, 2> &rvs, double ms, const std::vector<double> &throttles,
const std::array<std::array<double, 3>, 2> &rvf, double mf, double tof, double max_thrust,
double isp, double mu, double cut = 0.5, double tol = 1e-16);
// Constructor with rvm states
sims_flanagan_hf(const std::array<double, 7> &rvms, std::vector<double> throttles,
sims_flanagan_hf(const std::array<double, 7> &rvms, const std::vector<double> &throttles,
const std::array<double, 7> &rvmf, double tof, double max_thrust, double isp, double mu,
double cut, double tol = 1e-16);

Expand Down Expand Up @@ -81,10 +81,10 @@ class kep3_DLL_PUBLIC sims_flanagan_hf

// Getters
[[nodiscard]] double get_tof() const;
[[nodiscard]] const std::array<std::array<double, 3>, 2> get_rvs() const;
[[nodiscard]] std::array<std::array<double, 3>, 2> get_rvs() const;
[[nodiscard]] std::array<std::array<double, 3>, 2> get_rvf() const;
[[nodiscard]] double get_ms() const;
[[nodiscard]] const std::vector<double> &get_throttles() const;
[[nodiscard]] const std::array<std::array<double, 3>, 2> get_rvf() const;
[[nodiscard]] double get_mf() const;
[[nodiscard]] double get_max_thrust() const;
[[nodiscard]] double get_isp() const;
Expand Down
6 changes: 3 additions & 3 deletions kep3_devel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ dependencies:
- clang-tools
- ninja
- cmake >=3.18
- boost-cpp >=1.73
- libboost >=1.73
- fmt
- heyoka >= 5*
- heyoka.py >= 5*
- heyoka >= 6*
- heyoka.py >= 6*
- spdlog
- xtensor
- xtensor-blas
Expand Down
3 changes: 2 additions & 1 deletion src/leg/sims_flanagan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ using kep3::linalg::mat63;
using kep3::linalg::mat66;

// Constructors
sims_flanagan::sims_flanagan(const std::array<std::array<double, 3>, 2> &rvs, double ms, std::vector<double> throttles,
sims_flanagan::sims_flanagan(const std::array<std::array<double, 3>, 2> &rvs, double ms,
const std::vector<double> &throttles,
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
const std::array<std::array<double, 3>, 2> &rvf, double mf, double tof, double max_thrust,
double isp, double mu, double cut)
Expand Down
67 changes: 38 additions & 29 deletions src/leg/sims_flanagan_hf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ namespace kep3::leg
sims_flanagan_hf::sims_flanagan_hf()
{
// We perform some sanity checks on the user provided inputs
kep3::leg::_sanity_checks(m_throttles, m_tof, m_max_thrust, m_isp, m_mu, m_cut, m_tol, m_nseg, m_nseg_fwd, m_nseg_bck);
kep3::leg::_sanity_checks(m_throttles, m_tof, m_max_thrust, m_isp, m_mu, m_cut, m_tol, m_nseg, m_nseg_fwd,
m_nseg_bck);

// Initialize m_tas and m_tas_var
const heyoka::taylor_adaptive<double> ta_cache = kep3::ta::get_ta_stark(m_tol);
Expand All @@ -71,7 +72,7 @@ sims_flanagan_hf::sims_flanagan_hf()
}

sims_flanagan_hf::sims_flanagan_hf(const std::array<std::array<double, 3>, 2> &rvs, double ms,
std::vector<double> throttles,
const std::vector<double> &throttles,
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
const std::array<std::array<double, 3>, 2> &rvf, double mf, double tof,
double max_thrust, double isp, double mu, double cut, double tol)
Expand All @@ -80,7 +81,8 @@ sims_flanagan_hf::sims_flanagan_hf(const std::array<std::array<double, 3>, 2> &r
m_nseg_fwd(static_cast<unsigned>(static_cast<double>(m_nseg) * m_cut)), m_nseg_bck(m_nseg - m_nseg_fwd)
{
// We perform some sanity checks on the user provided inputs
kep3::leg::_sanity_checks(m_throttles, m_tof, m_max_thrust, m_isp, m_mu, m_cut, m_tol, m_nseg, m_nseg_fwd, m_nseg_bck);
kep3::leg::_sanity_checks(m_throttles, m_tof, m_max_thrust, m_isp, m_mu, m_cut, m_tol, m_nseg, m_nseg_fwd,
m_nseg_bck);

// Initialize m_tas and m_tas_var
const heyoka::taylor_adaptive<double> ta_cache = kep3::ta::get_ta_stark(m_tol);
Expand Down Expand Up @@ -112,15 +114,16 @@ sims_flanagan_hf::sims_flanagan_hf(const std::array<std::array<double, 3>, 2> &r
set_mf(mf);
}

sims_flanagan_hf::sims_flanagan_hf(const std::array<double, 7> &rvms, std::vector<double> throttles,
sims_flanagan_hf::sims_flanagan_hf(const std::array<double, 7> &rvms, const std::vector<double> &throttles,
const std::array<double, 7> &rvmf, double tof, double max_thrust, double isp,
double mu, double cut, double tol)
: m_rvms(rvms), m_throttles(std::move(throttles)), m_rvmf(rvmf), m_tof(tof), m_max_thrust(max_thrust), m_isp(isp),
m_mu(mu), m_cut(cut), m_tol(tol), m_nseg(static_cast<unsigned>(m_throttles.size()) / 3u),
m_nseg_fwd(static_cast<unsigned>(static_cast<double>(m_nseg) * m_cut)), m_nseg_bck(m_nseg - m_nseg_fwd)
{
// We perform some sanity checks on the user provided inputs
kep3::leg::_sanity_checks(m_throttles, m_tof, m_max_thrust, m_isp, m_mu, m_cut, m_tol, m_nseg, m_nseg_fwd, m_nseg_bck);
kep3::leg::_sanity_checks(m_throttles, m_tof, m_max_thrust, m_isp, m_mu, m_cut, m_tol, m_nseg, m_nseg_fwd,
m_nseg_bck);

// Initialize m_tas and m_tas_var
const heyoka::taylor_adaptive<double> ta_cache = kep3::ta::get_ta_stark(m_tol);
Expand Down Expand Up @@ -320,7 +323,7 @@ double sims_flanagan_hf::get_tof() const
{
return m_tof;
}
const std::array<std::array<double, 3>, 2> sims_flanagan_hf::get_rvs() const
std::array<std::array<double, 3>, 2> sims_flanagan_hf::get_rvs() const
{
std::array<std::array<double, 3>, 2> rvs{};
std::copy(m_rvms.begin(), std::next(m_rvms.begin(), 3), rvs[0].begin());
Expand All @@ -335,7 +338,7 @@ const std::vector<double> &sims_flanagan_hf::get_throttles() const
{
return m_throttles;
}
const std::array<std::array<double, 3>, 2> sims_flanagan_hf::get_rvf() const
std::array<std::array<double, 3>, 2> sims_flanagan_hf::get_rvf() const
{
std::array<std::array<double, 3>, 2> rvf{{{0., 0., 0.}, {0., 0., 0.}}};
std::copy(m_rvmf.begin(), std::next(m_rvmf.begin(), 3), rvf[0].begin());
Expand Down Expand Up @@ -413,12 +416,12 @@ std::array<double, 7> sims_flanagan_hf::compute_mismatch_constraints() const
for (auto i = 0u; i < m_nseg_fwd; ++i) {
// Assign current thrusts to Taylor adaptive integrator
std::copy(std::next(m_thrusts.begin(), static_cast<long>(i * 3)),
std::next(m_thrusts.begin(), static_cast<long>(3 * (i + 1))),
std::next(m_tas.get_pars_data(), 2));
std::next(m_thrusts.begin(), static_cast<long>(3 * (i + 1))), std::next(m_tas.get_pars_data(), 2));
// ... and integrate
auto [status, min_h, max_h, nsteps, _1, _2] = m_tas.propagate_until((i + 1) * prop_seg_duration);
if (status != heyoka::taylor_outcome::time_limit) {
throw std::domain_error("stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
throw std::domain_error(
"stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
}
}

Expand All @@ -435,12 +438,13 @@ std::array<double, 7> sims_flanagan_hf::compute_mismatch_constraints() const
for (auto i = 0u; i < m_nseg_bck; ++i) {
// Assign current_thrusts to Taylor adaptive integrator
std::copy(std::next(m_thrusts.begin(), static_cast<long>((m_nseg - (i + 1)) * 3)),
std::next(m_thrusts.begin(), static_cast<long>((m_nseg - i) * 3)),
std::next(m_tas.get_pars_data(), 2));
std::next(m_thrusts.begin(), static_cast<long>((m_nseg - i) * 3)),
std::next(m_tas.get_pars_data(), 2));
// ... and integrate
auto [status, min_h, max_h, nsteps, _1, _2] = m_tas.propagate_until(m_tof - (i + 1) * prop_seg_duration);
if (status != heyoka::taylor_outcome::time_limit) {
throw std::domain_error("stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
throw std::domain_error(
"stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
}
}

Expand Down Expand Up @@ -553,12 +557,13 @@ sims_flanagan_hf::compute_all_gradients() const
std::copy(m_vars.begin(), m_vars.end(), m_tas_var.get_state_data() + 7);
// Assign current thrusts to Taylor adaptive integrator
std::copy(std::next(m_thrusts.begin(), static_cast<long>(i * 3)),
std::next(m_thrusts.begin(), static_cast<long>(3 * (i + 1))),
std::next(m_tas_var.get_pars_data(), 2));
std::next(m_thrusts.begin(), static_cast<long>(3 * (i + 1))),
std::next(m_tas_var.get_pars_data(), 2));
// ... and integrate
auto [status, min_h, max_h, nsteps, _1, _2] = m_tas_var.propagate_until((i + 1) * prop_seg_duration);
if (status != heyoka::taylor_outcome::time_limit) {
throw std::domain_error("stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
throw std::domain_error(
"stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
}
// Save the variational state variables to respective arrays
std::copy(m_tas_var.get_state().begin(), m_tas_var.get_state().begin() + 7, xf_per_seg[i].begin());
Expand All @@ -582,12 +587,13 @@ sims_flanagan_hf::compute_all_gradients() const
std::copy(m_vars.begin(), m_vars.end(), m_tas_var.get_state_data() + 7);
// Assign current thrusts to Taylor adaptive integrator
std::copy(std::next(m_thrusts.begin(), static_cast<long>((m_nseg - (i + 1)) * 3)),
std::next(m_thrusts.begin(), static_cast<long>((m_nseg - i) * 3)),
std::next(m_tas_var.get_pars_data(), 2));
std::next(m_thrusts.begin(), static_cast<long>((m_nseg - i) * 3)),
std::next(m_tas_var.get_pars_data(), 2));
// ... and integrate
auto [status, min_h, max_h, nsteps, _1, _2] = m_tas_var.propagate_until(m_tof - (i + 1) * prop_seg_duration);
if (status != heyoka::taylor_outcome::time_limit) {
throw std::domain_error("stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
throw std::domain_error(
"stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
}
// Save the variational state variables to respective arrays
std::copy(m_tas_var.get_state().begin(), m_tas_var.get_state().begin() + 7,
Expand Down Expand Up @@ -697,8 +703,9 @@ sims_flanagan_hf::get_relevant_gradients(const std::vector<std::array<double, 49
} else if (i > m_nseg_fwd) {
corresponding_M = Mn_o[i - 1] * -1; // Multiple by -1 because mass correlation is -1.
} else {
throw std::runtime_error("During calculation of the throttle derivatives, the index doesn't correspond to "
"any leg and therefore cannot find the corresponding gradients."); // LCOV_EXCL_LINE
throw std::runtime_error(
"During calculation of the throttle derivatives, the index doesn't correspond to "
"any leg and therefore cannot find the corresponding gradients."); // LCOV_EXCL_LINE
}
xt::view(xgrad_final_throttle, xt::all(), xt::range(3 * i, 3 * (i + 1)))
= xt::linalg::dot(corresponding_M, current_U);
Expand All @@ -718,8 +725,9 @@ sims_flanagan_hf::get_relevant_gradients(const std::vector<std::array<double, 49
} else if ((static_cast<int>(i) > static_cast<int>(m_nseg_fwd) - 1) && m_nseg_bck > 0) {
corresponding_M = Mn_o[i]; // Idem
} else {
throw std::runtime_error("During calculation of the tof derivatives, the index doesn't correspond to "
"any leg and therefore cannot find the corresponding gradients."); // LCOV_EXCL_LINE
throw std::runtime_error(
"During calculation of the tof derivatives, the index doesn't correspond to "
"any leg and therefore cannot find the corresponding gradients."); // LCOV_EXCL_LINE
}
xgrad_final_tof += xt::linalg::dot(corresponding_M, current_F);
}
Expand Down Expand Up @@ -789,8 +797,7 @@ std::vector<std::vector<double>> sims_flanagan_hf::get_state_history(unsigned gr
for (decltype(m_nseg_fwd) i = 0u; i < m_nseg_fwd; ++i) {
// Assign current thrusts to Taylor adaptive integrator
std::copy(std::next(m_thrusts.begin(), static_cast<long>(i * 3)),
std::next(m_thrusts.begin(), static_cast<long>(3 * (i + 1))),
std::next(m_tas.get_pars_data(), 2));
std::next(m_thrusts.begin(), static_cast<long>(3 * (i + 1))), std::next(m_tas.get_pars_data(), 2));

// Current leg time grid
std::copy(std::next(leg_time_grid.begin(), i * (grid_points_per_segment - 1)),
Expand All @@ -800,7 +807,8 @@ std::vector<std::vector<double>> sims_flanagan_hf::get_state_history(unsigned gr
// ... and integrate
auto [status, min_h, max_h, nsteps, _1, output_states] = m_tas.propagate_grid(current_leg_time_grid);
if (status != heyoka::taylor_outcome::time_limit) {
throw std::domain_error("stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
throw std::domain_error(
"stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
}
output_per_seg[i] = output_states;
}
Expand All @@ -816,8 +824,8 @@ std::vector<std::vector<double>> sims_flanagan_hf::get_state_history(unsigned gr
for (decltype(m_nseg) i = 0u; i < m_nseg_bck; ++i) {
// Assign current_thrusts to Taylor adaptive integrator
std::copy(std::next(m_thrusts.begin(), static_cast<long>((m_nseg - (i + 1)) * 3)),
std::next(m_thrusts.begin(), static_cast<long>((m_nseg - i) * 3)),
std::next(m_tas.get_pars_data(), 2));
std::next(m_thrusts.begin(), static_cast<long>((m_nseg - i) * 3)),
std::next(m_tas.get_pars_data(), 2));

// Current leg time grid
std::reverse_copy(leg_time_grid.begin() + (m_nseg - (i + 1)) * (grid_points_per_segment - 1),
Expand All @@ -828,7 +836,8 @@ std::vector<std::vector<double>> sims_flanagan_hf::get_state_history(unsigned gr
// ... and integrate
auto [status, min_h, max_h, nsteps, _1, output_states] = m_tas.propagate_grid(back_time_grid);
if (status != heyoka::taylor_outcome::time_limit) {
throw std::domain_error("stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
throw std::domain_error(
"stark_problem: failure to reach the final time requested during a propagation."); // LCOV_EXCL_LINE
}
output_per_seg[m_nseg - 1 - i] = output_states;
}
Expand Down
2 changes: 1 addition & 1 deletion tools/gha_linux_asan_debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge
export deps_dir=$HOME/local
export PATH="$HOME/miniforge3/bin:$PATH"
bash miniforge3.sh -b -p $HOME/miniforge3
mamba env create -f kep3_devel.yml -q -p $deps_dir
mamba env create --file=kep3_devel.yml -q -p $deps_dir
source activate $deps_dir

export CXXFLAGS="$CXXFLAGS -fsanitize=address"
Expand Down
2 changes: 1 addition & 1 deletion tools/gha_linux_coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge
export deps_dir=$HOME/local
export PATH="$HOME/miniforge3/bin:$PATH"
bash miniforge3.sh -b -p $HOME/miniforge3
mamba env create -f kep3_devel.yml -q -p $deps_dir
mamba env create --file=kep3_devel.yml -q -p $deps_dir
source activate $deps_dir
mamba install lcov -y

Expand Down
2 changes: 1 addition & 1 deletion tools/gha_linux_pykep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge
export deps_dir=$HOME/local
export PATH="$HOME/miniforge3/bin:$PATH"
bash miniforge3.sh -b -p $HOME/miniforge3
mamba env create -f kep3_devel.yml -q -p $deps_dir
mamba env create --file=kep3_devel.yml -q -p $deps_dir
source activate $deps_dir

# Install additional packages for Python compiling and docs building (19/11/2023 sphinx 7 not working)
Expand Down
2 changes: 1 addition & 1 deletion tools/gha_osx_debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge
export deps_dir=$HOME/local
export PATH="$HOME/miniforge3/bin:$PATH"
bash miniforge3.sh -b -p $HOME/miniforge3
mamba env create -f kep3_devel.yml -q -p $deps_dir
mamba env create --file=kep3_devel.yml -q -p $deps_dir
source activate $deps_dir

mkdir build
Expand Down

0 comments on commit 7841842

Please sign in to comment.