Skip to content

Commit 66ea8b1

Browse files
committed
Refactor names and ids
Refactor name and ID handling as preparation for #3107. * Store IDs/names in const string_view array and only pass around spans to avoid always constructing new vectors * Store solver state IDs/names arrays instead of creating them dynamically, to be compatible with the previous point * Update tests accordingly * Add the respective typemaps * Regenerate test models
1 parent 2b036e9 commit 66ea8b1

34 files changed

+765
-643
lines changed

include/amici/model.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "amici/sundials_matrix_wrapper.h"
1212

1313
#include <map>
14+
#include <span>
15+
#include <string_view>
1416
#include <vector>
1517

1618
namespace amici {
@@ -618,7 +620,7 @@ class Model : public AbstractModel, public ModelDimensions {
618620
*
619621
* @return The parameter names
620622
*/
621-
virtual std::vector<std::string> get_free_parameter_names() const;
623+
virtual std::span<std::string_view const> get_free_parameter_names() const;
622624

623625
/**
624626
* @brief Report whether the model has state names set.
@@ -633,14 +635,14 @@ class Model : public AbstractModel, public ModelDimensions {
633635
*
634636
* @return State names
635637
*/
636-
virtual std::vector<std::string> get_state_names() const;
638+
virtual std::span<std::string_view const> get_state_names() const;
637639

638640
/**
639641
* @brief Get names of the solver states.
640642
*
641643
* @return State names
642644
*/
643-
virtual std::vector<std::string> get_state_names_solver() const;
645+
virtual std::span<std::string_view const> get_state_names_solver() const;
644646

645647
/**
646648
* @brief Report whether the model has fixed parameter names set.
@@ -655,7 +657,7 @@ class Model : public AbstractModel, public ModelDimensions {
655657
*
656658
* @return Fixed parameter names
657659
*/
658-
virtual std::vector<std::string> get_fixed_parameter_names() const;
660+
virtual std::span<std::string_view const> get_fixed_parameter_names() const;
659661

660662
/**
661663
* @brief Report whether the model has observable names set.
@@ -670,7 +672,7 @@ class Model : public AbstractModel, public ModelDimensions {
670672
*
671673
* @return Observable names
672674
*/
673-
virtual std::vector<std::string> get_observable_names() const;
675+
virtual std::span<std::string_view const> get_observable_names() const;
674676

675677
/**
676678
* @brief Report whether the model has expression names set.
@@ -685,7 +687,7 @@ class Model : public AbstractModel, public ModelDimensions {
685687
*
686688
* @return Expression names
687689
*/
688-
virtual std::vector<std::string> get_expression_names() const;
690+
virtual std::span<std::string_view const> get_expression_names() const;
689691

690692
/**
691693
* @brief Report whether the model has parameter IDs set.
@@ -700,7 +702,7 @@ class Model : public AbstractModel, public ModelDimensions {
700702
*
701703
* @return Parameter IDs
702704
*/
703-
virtual std::vector<std::string> get_free_parameter_ids() const;
705+
virtual std::span<std::string_view const> get_free_parameter_ids() const;
704706

705707
/**
706708
* @brief Report whether the model has state IDs set.
@@ -715,14 +717,14 @@ class Model : public AbstractModel, public ModelDimensions {
715717
*
716718
* @return State IDs
717719
*/
718-
virtual std::vector<std::string> get_state_ids() const;
720+
virtual std::span<std::string_view const> get_state_ids() const;
719721

720722
/**
721723
* @brief Get IDs of the solver states.
722724
*
723725
* @return State IDs
724726
*/
725-
virtual std::vector<std::string> get_state_ids_solver() const;
727+
virtual std::span<std::string_view const> get_state_ids_solver() const;
726728

727729
/**
728730
* @brief Report whether the model has fixed parameter IDs set.
@@ -737,7 +739,7 @@ class Model : public AbstractModel, public ModelDimensions {
737739
*
738740
* @return Fixed parameter IDs
739741
*/
740-
virtual std::vector<std::string> get_fixed_parameter_ids() const;
742+
virtual std::span<std::string_view const> get_fixed_parameter_ids() const;
741743

742744
/**
743745
* @brief Report whether the model has observable IDs set.
@@ -752,7 +754,7 @@ class Model : public AbstractModel, public ModelDimensions {
752754
*
753755
* @return Observable IDs
754756
*/
755-
virtual std::vector<std::string> get_observable_ids() const;
757+
virtual std::span<std::string_view const> get_observable_ids() const;
756758

757759
/**
758760
* @brief Report whether the model has expression IDs set.
@@ -767,7 +769,7 @@ class Model : public AbstractModel, public ModelDimensions {
767769
*
768770
* @return Expression IDs
769771
*/
770-
virtual std::vector<std::string> get_expression_ids() const;
772+
virtual std::span<std::string_view const> get_expression_ids() const;
771773

772774
/**
773775
* @brief Checks whether the defined noise model is Gaussian, i.e., the nllh

models/model_calvetti_py/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
cmake_minimum_required(VERSION 3.22)
33
cmake_policy(VERSION 3.22...3.31)
44

5+
# We aren't using C++20 modules, so disable scanning for them to avoid
6+
# clang-scan-deps-notfound errors.
7+
# See also https://discourse.cmake.org/t/cmake-3-28-cmake-cxx-compiler-clang-scan-deps-notfound-not-found/9244/3
8+
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
9+
510
project(model_calvetti_py)
611

712
message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}")

models/model_calvetti_py/model_calvetti_py.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#include <amici/defines.h>
22
#include <array>
3+
#include <string_view>
34

45
namespace amici::model_model_calvetti_py {
56

67
// clang-format off
78

8-
std::array<const char*, 0> free_parameter_names = {
9+
extern const std::array<std::string_view const, 0> free_parameter_names = {
910

1011
};
1112

12-
std::array<const char*, 6> fixed_parameter_names = {
13+
extern const std::array<std::string_view const, 6> fixed_parameter_names = {
1314
"V1ss", // k[0]
1415
"R1ss", // k[1]
1516
"V2ss", // k[2]
@@ -18,7 +19,7 @@ std::array<const char*, 6> fixed_parameter_names = {
1819
"R3ss", // k[5]
1920
};
2021

21-
std::array<const char*, 6> state_names = {
22+
extern const std::array<std::string_view const, 6> state_names = {
2223
"V1", // x_rdata[0]
2324
"V2", // x_rdata[1]
2425
"V3", // x_rdata[2]
@@ -27,7 +28,16 @@ std::array<const char*, 6> state_names = {
2728
"f3", // x_rdata[5]
2829
};
2930

30-
std::array<const char*, 6> observable_names = {
31+
extern const std::array<std::string_view const, 6> state_names_solver = {
32+
"V1", // x_solver[0]
33+
"V2", // x_solver[1]
34+
"V3", // x_solver[2]
35+
"f1", // x_solver[3]
36+
"f2", // x_solver[4]
37+
"f3", // x_solver[5]
38+
};
39+
40+
extern const std::array<std::string_view const, 6> observable_names = {
3141
"y0", // y[0]
3242
"y1", // y[1]
3343
"y2", // y[2]
@@ -45,7 +55,7 @@ ObservableScaling::lin, // y[4]
4555
ObservableScaling::lin, // y[5]
4656
};
4757

48-
std::array<const char*, 16> expression_names = {
58+
extern const std::array<std::string_view const, 16> expression_names = {
4959
"C1ss", // w[0]
5060
"C2ss", // w[1]
5161
"C3ss", // w[2]
@@ -64,11 +74,11 @@ std::array<const char*, 16> expression_names = {
6474
"rate_of_V3", // w[15]
6575
};
6676

67-
std::array<const char*, 0> free_parameter_ids = {
77+
extern const std::array<std::string_view const, 0> free_parameter_ids = {
6878

6979
};
7080

71-
std::array<const char*, 6> fixed_parameter_ids = {
81+
extern const std::array<std::string_view const, 6> fixed_parameter_ids = {
7282
"V1ss", // k[0]
7383
"R1ss", // k[1]
7484
"V2ss", // k[2]
@@ -77,7 +87,7 @@ std::array<const char*, 6> fixed_parameter_ids = {
7787
"R3ss", // k[5]
7888
};
7989

80-
std::array<const char*, 6> state_ids = {
90+
extern const std::array<std::string_view const, 6> state_ids = {
8191
"V1", // x_rdata[0]
8292
"V2", // x_rdata[1]
8393
"V3", // x_rdata[2]
@@ -86,7 +96,16 @@ std::array<const char*, 6> state_ids = {
8696
"f3", // x_rdata[5]
8797
};
8898

89-
std::array<const char*, 6> observable_ids = {
99+
extern const std::array<std::string_view const, 6> state_ids_solver = {
100+
"V1", // x_solver[0]
101+
"V2", // x_solver[1]
102+
"V3", // x_solver[2]
103+
"f1", // x_solver[3]
104+
"f2", // x_solver[4]
105+
"f3", // x_solver[5]
106+
};
107+
108+
extern const std::array<std::string_view const, 6> observable_ids = {
90109
"obs_V1", // y[0]
91110
"obs_V2", // y[1]
92111
"obs_V3", // y[2]
@@ -95,7 +114,7 @@ std::array<const char*, 6> observable_ids = {
95114
"obs_f2", // y[5]
96115
};
97116

98-
std::array<const char*, 16> expression_ids = {
117+
extern const std::array<std::string_view const, 16> expression_ids = {
99118
"C1ss", // w[0]
100119
"C2ss", // w[1]
101120
"C3ss", // w[2]

0 commit comments

Comments
 (0)