Skip to content

Start clang tidy testing #27

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 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ __pycache__
.venv
.env
.nox
.DS_Store

setup.log
install_KLU_Sundials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "ExpressionTypes.hpp"
#include "../../common.hpp"
#include "../../Options.hpp"
#include <memory>
#include <vector>

class Expression {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#ifndef PYBAMM_IDAKLU_EXPRESSION_SET_HPP
#define PYBAMM_IDAKLU_EXPRESSION_SET_HPP

#include "ExpressionTypes.hpp"
#include "Expression.hpp"
#include "../../common.hpp"
#include "../../Options.hpp"
#include <memory>

template <class T>
class ExpressionSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "../Expressions.hpp"
#include <casadi/casadi.hpp>
#include <casadi/core/function.hpp>
#include <casadi/core/sparsity.hpp>
#include <memory>

/**
* @brief Class for handling individual casadi functions
Expand Down
6 changes: 4 additions & 2 deletions src/pybammsolvers/idaklu_source/IDAKLUSolverGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ std::vector<Solution> IDAKLUSolverGroup::solve(
throw std::invalid_argument(
"t_eval must have at least 2 entries"
);
} else if (save_interp_steps) {
}
if (save_interp_steps) {
if (t_interp.front() < t_eval.front()) {
throw std::invalid_argument(
"t_interp values must be greater than the smallest t_eval value: "
+ std::to_string(t_eval.front())
);
} else if (t_interp.back() > t_eval.back()) {
}
if (t_interp.back() > t_eval.back()) {
throw std::invalid_argument(
"t_interp values must be less than the greatest t_eval value: "
+ std::to_string(t_eval.back())
Expand Down
1 change: 0 additions & 1 deletion src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ using std::vector;

#include "Options.hpp"
#include "Solution.hpp"
#include "sundials_legacy_wrapper.hpp"

/**
* @brief Abstract solver class based on OpenMP vectors
Expand Down
2 changes: 2 additions & 0 deletions src/pybammsolvers/idaklu_source/IDAKLUSolverOpenMP.inl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include "Expressions/Expressions.hpp"
#include "sundials_functions.hpp"
#include <vector>
Expand Down
82 changes: 40 additions & 42 deletions src/pybammsolvers/idaklu_source/IdakluJax.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
#include "IdakluJax.hpp"

#include <pybind11/functional.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>

#include <vector>
#include <iostream>
#include <functional>

// Initialise static variable
Expand All @@ -18,7 +12,7 @@ std::map<std::int64_t, IdakluJax*> idaklu_jax_instances;

// Create a new IdakluJax object, assign identifier, add to the objects list and return as pointer
IdakluJax *create_idaklu_jax() {
IdakluJax *p = new IdakluJax();
auto *p = new IdakluJax();
idaklu_jax_instances[p->get_index()] = p;
return p;
}
Expand All @@ -32,21 +26,21 @@ IdakluJax::~IdakluJax() {
}

void IdakluJax::register_callback_eval(CallbackEval h) {
callback_eval = h;
callback_eval = std::move(h);
}

void IdakluJax::register_callback_jvp(CallbackJvp h) {
callback_jvp = h;
callback_jvp = std::move(h);
}

void IdakluJax::register_callback_vjp(CallbackVjp h) {
callback_vjp = h;
callback_vjp = std::move(h);
}

void IdakluJax::register_callbacks(CallbackEval h_eval, CallbackJvp h_jvp, CallbackVjp h_vjp) {
register_callback_eval(h_eval);
register_callback_jvp(h_jvp);
register_callback_vjp(h_vjp);
register_callback_eval(std::move(h_eval));
register_callback_jvp(std::move(h_jvp));
register_callback_vjp(std::move(h_vjp));
}

void IdakluJax::cpu_idaklu_eval(void *out_tuple, const void **in) {
Expand All @@ -55,10 +49,11 @@ void IdakluJax::cpu_idaklu_eval(void *out_tuple, const void **in) {
const std::int64_t n_t = *reinterpret_cast<const std::int64_t *>(in[k++]);
const std::int64_t n_vars = *reinterpret_cast<const std::int64_t *>(in[k++]);
const std::int64_t n_inputs = *reinterpret_cast<const std::int64_t *>(in[k++]);
const realtype *t = reinterpret_cast<const realtype *>(in[k++]);
realtype *inputs = new realtype(n_inputs);
for (int i = 0; i < n_inputs; i++)
const auto *t = reinterpret_cast<const realtype *>(in[k++]);
auto *inputs = new realtype(n_inputs);
for (int i = 0; i < n_inputs; i++) {
inputs[i] = reinterpret_cast<const realtype *>(in[k++])[0];
}
void *out = reinterpret_cast<realtype *>(out_tuple);

// Log
Expand All @@ -75,16 +70,16 @@ void IdakluJax::cpu_idaklu_eval(void *out_tuple, const void **in) {
PyGILState_STATE state = PyGILState_Ensure();

// Convert time vector to an np_array
py::capsule t_capsule(t, "t_capsule");
np_array t_np = np_array({n_t}, {sizeof(realtype)}, t, t_capsule);
const py::capsule t_capsule(t, "t_capsule");
const auto t_np = np_array({n_t}, {sizeof(realtype)}, t, t_capsule);

// Convert inputs to an np_array
py::capsule in_capsule(inputs, "in_capsule");
np_array in_np = np_array({n_inputs}, {sizeof(realtype)}, inputs, in_capsule);
const py::capsule in_capsule(inputs, "in_capsule");
const auto in_np = np_array({n_inputs}, {sizeof(realtype)}, inputs, in_capsule);

// Call solve function in python to obtain an np_array
np_array out_np = callback_eval(t_np, in_np);
auto out_buf = out_np.request();
const np_array out_np = callback_eval(t_np, in_np);
const auto out_buf = out_np.request();
const realtype *out_ptr = reinterpret_cast<realtype *>(out_buf.ptr);

// Arrange into 'out' array
Expand All @@ -100,14 +95,16 @@ void IdakluJax::cpu_idaklu_jvp(void *out_tuple, const void **in) {
const std::int64_t n_t = *reinterpret_cast<const std::int64_t *>(in[k++]);
const std::int64_t n_vars = *reinterpret_cast<const std::int64_t *>(in[k++]);
const std::int64_t n_inputs = *reinterpret_cast<const std::int64_t *>(in[k++]);
const realtype *primal_t = reinterpret_cast<const realtype *>(in[k++]);
realtype *primal_inputs = new realtype(n_inputs);
for (int i = 0; i < n_inputs; i++)
const auto *primal_t = reinterpret_cast<const realtype *>(in[k++]);
auto *primal_inputs = new realtype(n_inputs);
for (int i = 0; i < n_inputs; i++) {
primal_inputs[i] = reinterpret_cast<const realtype *>(in[k++])[0];
const realtype *tangent_t = reinterpret_cast<const realtype *>(in[k++]);
realtype *tangent_inputs = new realtype(n_inputs);
for (int i = 0; i < n_inputs; i++)
}
const auto *tangent_t = reinterpret_cast<const realtype *>(in[k++]);
auto *tangent_inputs = new realtype(n_inputs);
for (int i = 0; i < n_inputs; i++) {
tangent_inputs[i] = reinterpret_cast<const realtype *>(in[k++])[0];
}
void *out = reinterpret_cast<realtype *>(out_tuple);

// Log
Expand All @@ -125,8 +122,8 @@ void IdakluJax::cpu_idaklu_jvp(void *out_tuple, const void **in) {
PyGILState_STATE state = PyGILState_Ensure();

// Form primals time vector as np_array
py::capsule primal_t_capsule(primal_t, "primal_t_capsule");
np_array primal_t_np = np_array(
const py::capsule primal_t_capsule(primal_t, "primal_t_capsule");
const auto primal_t_np = np_array(
{n_t},
{sizeof(realtype)},
primal_t,
Expand All @@ -135,25 +132,25 @@ void IdakluJax::cpu_idaklu_jvp(void *out_tuple, const void **in) {

// Pack primals as np_array
py::capsule primal_inputs_capsule(primal_inputs, "primal_inputs_capsule");
np_array primal_inputs_np = np_array(
const auto primal_inputs_np = np_array(
{n_inputs},
{sizeof(realtype)},
primal_inputs,
primal_inputs_capsule
);

// Form tangents time vector as np_array
py::capsule tangent_t_capsule(tangent_t, "tangent_t_capsule");
np_array tangent_t_np = np_array(
const py::capsule tangent_t_capsule(tangent_t, "tangent_t_capsule");
const auto tangent_t_np = np_array(
{n_t},
{sizeof(realtype)},
tangent_t,
tangent_t_capsule
);

// Pack tangents as np_array
py::capsule tangent_inputs_capsule(tangent_inputs, "tangent_inputs_capsule");
np_array tangent_inputs_np = np_array(
const py::capsule tangent_inputs_capsule(tangent_inputs, "tangent_inputs_capsule");
const auto tangent_inputs_np = np_array(
{n_inputs},
{sizeof(realtype)},
tangent_inputs,
Expand All @@ -165,7 +162,7 @@ void IdakluJax::cpu_idaklu_jvp(void *out_tuple, const void **in) {
primal_t_np, primal_inputs_np,
tangent_t_np, tangent_inputs_np
);
auto buf = y_dot.request();
const auto buf = y_dot.request();
const realtype *ptr = reinterpret_cast<realtype *>(buf.ptr);

// Arrange into 'out' array
Expand All @@ -182,13 +179,14 @@ void IdakluJax::cpu_idaklu_vjp(void *out_tuple, const void **in) {
const std::int64_t n_y_bar0 = *reinterpret_cast<const std::int64_t *>(in[k++]);
const std::int64_t n_y_bar1 = *reinterpret_cast<const std::int64_t *>(in[k++]);
const std::int64_t n_y_bar = (n_y_bar1 > 0) ? (n_y_bar0*n_y_bar1) : n_y_bar0;
const realtype *y_bar = reinterpret_cast<const realtype *>(in[k++]);
const std::int64_t *invar = reinterpret_cast<const std::int64_t *>(in[k++]);
const realtype *t = reinterpret_cast<const realtype *>(in[k++]);
realtype *inputs = new realtype(n_inputs);
for (int i = 0; i < n_inputs; i++)
const auto *y_bar = reinterpret_cast<const realtype *>(in[k++]);
const auto *invar = reinterpret_cast<const std::int64_t *>(in[k++]);
const auto *t = reinterpret_cast<const realtype *>(in[k++]);
auto *inputs = new realtype(n_inputs);
for (int i = 0; i < n_inputs; i++) {
inputs[i] = reinterpret_cast<const realtype *>(in[k++])[0];
realtype *out = reinterpret_cast<realtype *>(out_tuple);
}
auto *out = reinterpret_cast<realtype *>(out_tuple);

// Log
DEBUG("cpu_idaklu_vjp");
Expand Down
2 changes: 1 addition & 1 deletion src/pybammsolvers/idaklu_source/Solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ class Solution
np_array y_term;
};

#endif // PYBAMM_IDAKLU_COMMON_HPP
#endif // PYBAMM_IDAKLU_SOLUTION_HPP
12 changes: 6 additions & 6 deletions src/pybammsolvers/idaklu_source/SolutionData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Solution SolutionData::generate_solution() {
py::capsule free_t_when_done(
t_return,
[](void *f) {
realtype *vect = reinterpret_cast<realtype *>(f);
auto *vect = reinterpret_cast<realtype *>(f);
delete[] vect;
}
);
Expand All @@ -18,7 +18,7 @@ Solution SolutionData::generate_solution() {
py::capsule free_y_when_done(
y_return,
[](void *f) {
realtype *vect = reinterpret_cast<realtype *>(f);
auto *vect = reinterpret_cast<realtype *>(f);
delete[] vect;
}
);
Expand All @@ -32,7 +32,7 @@ Solution SolutionData::generate_solution() {
py::capsule free_yp_when_done(
yp_return,
[](void *f) {
realtype *vect = reinterpret_cast<realtype *>(f);
auto *vect = reinterpret_cast<realtype *>(f);
delete[] vect;
}
);
Expand All @@ -46,7 +46,7 @@ Solution SolutionData::generate_solution() {
py::capsule free_yS_when_done(
yS_return,
[](void *f) {
realtype *vect = reinterpret_cast<realtype *>(f);
auto *vect = reinterpret_cast<realtype *>(f);
delete[] vect;
}
);
Expand All @@ -64,7 +64,7 @@ Solution SolutionData::generate_solution() {
py::capsule free_ypS_when_done(
ypS_return,
[](void *f) {
realtype *vect = reinterpret_cast<realtype *>(f);
auto *vect = reinterpret_cast<realtype *>(f);
delete[] vect;
}
);
Expand All @@ -83,7 +83,7 @@ Solution SolutionData::generate_solution() {
py::capsule free_yterm_when_done(
yterm_return,
[](void *f) {
realtype *vect = reinterpret_cast<realtype *>(f);
auto *vect = reinterpret_cast<realtype *>(f);
delete[] vect;
}
);
Expand Down
16 changes: 8 additions & 8 deletions src/pybammsolvers/idaklu_source/SolutionData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class SolutionData
* @brief constructor using fields
*/
SolutionData(
int flag,
int number_of_timesteps,
int length_of_return_vector,
int arg_sens0,
int arg_sens1,
int arg_sens2,
int length_of_final_sv_slice,
bool save_hermite,
const int flag,
const int number_of_timesteps,
const int length_of_return_vector,
const int arg_sens0,
const int arg_sens1,
const int arg_sens2,
const int length_of_final_sv_slice,
const bool save_hermite,
realtype *t_return,
realtype *y_return,
realtype *yp_return,
Expand Down
4 changes: 1 addition & 3 deletions src/pybammsolvers/idaklu_source/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
#include <sundials/sundials_config.h> /* defs. of SUNRabs, SUNRexp, etc. */
#include <sundials/sundials_types.h> /* defs. of realtype, sunindextype */

#if SUNDIALS_VERSION_MAJOR >= 6
#include <sundials/sundials_context.h>
#endif
#include <sundials/sundials_context.h>

#include <sunlinsol/sunlinsol_klu.h> /* access to KLU linear solver */
#include <sunlinsol/sunlinsol_dense.h> /* access to dense linear solver */
Expand Down
1 change: 0 additions & 1 deletion src/pybammsolvers/idaklu_source/idaklu_solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "IDAKLUSolverOpenMP_solvers.hpp"
#include "IDAKLUSolverGroup.hpp"
#include <idas/idas.h>
#include <memory>

/**
Expand Down
1 change: 0 additions & 1 deletion src/pybammsolvers/idaklu_source/observe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define PYBAMM_CREATE_OBSERVE_HPP

#include <memory>
#include <unordered_map>
#include <string>
#include "common.hpp"
#include <casadi/core/function.hpp>
Expand Down
10 changes: 7 additions & 3 deletions src/pybammsolvers/idaklu_source/sundials_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
#include "common.hpp"

template<typename T>
void axpy(int n, T alpha, const T* x, T* y) {
if (!x || !y) return;
for (int i=0; i<n; ++i) *y++ += alpha**x++;
void axpy(int const n, T alpha, const T* x, T* y) {
if (!x || !y) {
return;
}
for (int i=0; i<n; ++i) {
*y++ += alpha**x++;
}
}

int residual_eval(realtype tres, N_Vector yy, N_Vector yp, N_Vector rr,
Expand Down
Loading
Loading