Skip to content

Commit 261cd42

Browse files
committed
Use C++20
1 parent 090e022 commit 261cd42

15 files changed

Lines changed: 63 additions & 85 deletions

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}"
3838
endif()
3939

4040
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
41-
set(CMAKE_CXX_STANDARD 17)
41+
set(CMAKE_CXX_STANDARD 20)
4242
set(CMAKE_CXX_STANDARD_REQUIRED ON)
4343

4444
# Compiler flags

cmake/clang-tools.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if(CLANG_TIDY)
2929
clang-tidy
3030
COMMAND
3131
sh -c
32-
"${CLANG_TIDY} ${ALL_CXX_SOURCE_FILES} -- -std=c++17 -I${CMAKE_SOURCE_DIR}"
32+
"${CLANG_TIDY} ${ALL_CXX_SOURCE_FILES} -- -std=c++20 -I${CMAKE_SOURCE_DIR}"
3333
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
3434
else()
3535
message(STATUS "clang-tidy was not found")

doc/cpp_installation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The following section describes building the AMICI C++ library:
1515
Prerequisites:
1616

1717
* CBLAS compatible BLAS library
18-
* a C++17 compatible compiler
18+
* a C++20 compatible compiler
1919
* a C compiler
2020
* Optional:
2121

include/amici/forwardproblem.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,9 @@ class FinalStateStorer : public ContextManager {
456456
auto final_time = fwd_->getFinalTime();
457457
auto const timepoints = fwd_->model->getTimepoints();
458458
if (!fwd_->timepoint_states_.count(final_time)
459-
&& std::find(timepoints.cbegin(), timepoints.cend(), final_time)
460-
!= timepoints.cend()) {
459+
&& std::find(
460+
timepoints.cbegin(), timepoints.cend(), final_time
461+
) != timepoints.cend()) {
461462
fwd_->timepoint_states_[final_time] = fwd_->final_state_;
462463
}
463464
} catch (std::exception const&) {
@@ -469,7 +470,7 @@ class FinalStateStorer : public ContextManager {
469470
// `fwd_->{final_state_,timepoint_states_}` won't be set,
470471
// and we assume that they are either not accessed anymore, or
471472
// that there is appropriate error handling in place.
472-
if(!std::uncaught_exceptions()) {
473+
if (!std::uncaught_exceptions()) {
473474
throw;
474475
}
475476
}

matlab/@amimodel/compileAndLinkModel.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ function compileAndLinkModel(modelname, modelSourceFolder, coptim, debug, funs,
3434
end
3535

3636
% compile flags
37-
COPT = ['COPTIMFLAGS=''' coptim ' -DNDEBUG'' CXXFLAGS=''$CXXFLAGS -std=c++17'''];
37+
COPT = ['COPTIMFLAGS=''' coptim ' -DNDEBUG'' CXXFLAGS=''$CXXFLAGS -std=c++20'''];
3838
if(debug)
39-
DEBUG = ' -g CXXFLAGS=''$CXXFLAGS -Wall -std=c++17 -Wno-unused-function -Wno-unused-variable'' ';
39+
DEBUG = ' -g CXXFLAGS=''$CXXFLAGS -Wall -std=c++20 -Wno-unused-function -Wno-unused-variable'' ';
4040
COPT = ''; % no optimization with debug flags!
4141
else
4242
DEBUG = '';

src/edata.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ ExpData::ExpData(
129129
}
130130

131131
void ExpData::setTimepoints(std::vector<realtype> const& ts) {
132-
if (!std::is_sorted(ts.begin(), ts.end()))
132+
if (!std::ranges::is_sorted(ts))
133133
throw AmiException(
134134
"Encountered non-monotonic timepoints, please order timepoints "
135135
"such that they are monotonically increasing!"
@@ -196,9 +196,7 @@ void ExpData::setObservedDataStdDev(
196196

197197
void ExpData::setObservedDataStdDev(realtype const stdDev) {
198198
checkSigmaPositivity(stdDev, "stdDev");
199-
std::fill(
200-
observed_data_std_dev_.begin(), observed_data_std_dev_.end(), stdDev
201-
);
199+
std::ranges::fill(observed_data_std_dev_, stdDev);
202200
}
203201

204202
void ExpData::setObservedDataStdDev(
@@ -292,9 +290,7 @@ void ExpData::setObservedEventsStdDev(
292290

293291
void ExpData::setObservedEventsStdDev(realtype const stdDev) {
294292
checkSigmaPositivity(stdDev, "stdDev");
295-
std::fill(
296-
observed_events_std_dev_.begin(), observed_events_std_dev_.end(), stdDev
297-
);
293+
std::ranges::fill(observed_events_std_dev_, stdDev);
298294
}
299295

300296
void ExpData::setObservedEventsStdDev(
@@ -339,15 +335,10 @@ realtype const* ExpData::getObservedEventsStdDevPtr(int ie) const {
339335
}
340336

341337
void ExpData::clear_observations() {
342-
std::fill(observed_data_.begin(), observed_data_.end(), getNaN());
343-
std::fill(
344-
observed_data_std_dev_.begin(), observed_data_std_dev_.end(), getNaN()
345-
);
346-
std::fill(observed_events_.begin(), observed_events_.end(), getNaN());
347-
std::fill(
348-
observed_events_std_dev_.begin(), observed_events_std_dev_.end(),
349-
getNaN()
350-
);
338+
std::ranges::fill(observed_data_, getNaN());
339+
std::ranges::fill(observed_data_std_dev_, getNaN());
340+
std::ranges::fill(observed_events_, getNaN());
341+
std::ranges::fill(observed_events_std_dev_, getNaN());
351342
}
352343

353344
void ExpData::applyDimensions() {

src/forwardproblem.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ void ForwardProblem::workForwardProblem() {
8787
solver->setup(t0, model, x_, dx_, sx_, sdx_);
8888

8989
if (model->ne
90-
&& std::any_of(roots_found_.begin(), roots_found_.end(), [](int rf) {
91-
return rf == 1;
92-
}))
90+
&& std::ranges::any_of(roots_found_, [](int rf) { return rf == 1; }))
9391
handleEvent(&t0, false, true);
9492

9593
/* perform presimulation if necessary */
@@ -101,10 +99,9 @@ void ForwardProblem::workForwardProblem() {
10199
t_ = model->t0();
102100
if (model->ne) {
103101
model->initEvents(x_, dx_, roots_found_);
104-
if (std::any_of(
105-
roots_found_.begin(), roots_found_.end(),
106-
[](int rf) { return rf == 1; }
107-
))
102+
if (std::ranges::any_of(roots_found_, [](int rf) {
103+
return rf == 1;
104+
}))
108105
handleEvent(&t0, false, true);
109106
}
110107
}
@@ -136,10 +133,10 @@ void ForwardProblem::workForwardProblem() {
136133

137134
// get list of trigger timepoints for fixed-time triggered events
138135
auto trigger_timepoints = model->get_trigger_timepoints();
139-
auto it_trigger_timepoints = std::find_if(
140-
trigger_timepoints.begin(), trigger_timepoints.end(),
141-
[this](auto t) { return t > this->t_; }
142-
);
136+
auto it_trigger_timepoints
137+
= std::ranges::find_if(trigger_timepoints, [this](auto t) {
138+
return t > this->t_;
139+
});
143140

144141
/* loop over timepoints */
145142
for (it_ = 0; it_ < model->nt(); it_++) {
@@ -333,7 +330,7 @@ void ForwardProblem::store_pre_event_state(bool seflag, bool initial_event) {
333330
}
334331
}
335332
if (initial_event) // t0 has no parameter dependency
336-
std::fill(stau_.begin(), stau_.end(), 0.0);
333+
std::ranges::fill(stau_, 0.0);
337334
} else if (solver->computingASA()) {
338335
/* store x to compute jump in discontinuity */
339336
x_disc_.push_back(x_);

src/hdf5.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,8 @@ void writeReturnDataDiagnosis(
699699
// work-around for macos segfaults, use struct without std::string
700700
struct LogItemCStr {
701701
int severity;
702-
const char* identifier;
703-
const char* message;
702+
char const* identifier;
703+
char const* message;
704704
};
705705

706706
void writeLogItemsToHDF5(
@@ -738,8 +738,7 @@ void writeLogItemsToHDF5(
738738
// ... therefore, as a workaround, we use a struct without std::string
739739
H5::CompType logItemType(sizeof(LogItemCStr));
740740
logItemType.insertMember(
741-
"severity", HOFFSET(LogItemCStr, severity),
742-
H5::PredType::NATIVE_INT
741+
"severity", HOFFSET(LogItemCStr, severity), H5::PredType::NATIVE_INT
743742
);
744743
auto vlstr_type = H5::StrType(H5::PredType::C_S1, H5T_VARIABLE);
745744
logItemType.insertMember(

src/model.cpp

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static realtype getValueById(
9898
std::vector<std::string> const& ids, std::vector<realtype> const& values,
9999
std::string const& id, char const* variable_name, char const* id_name
100100
) {
101-
auto it = std::find(ids.begin(), ids.end(), id);
101+
auto it = std::ranges::find(ids, id);
102102
if (it != ids.end())
103103
return values.at(it - ids.begin());
104104

@@ -121,7 +121,7 @@ static void setValueById(
121121
realtype value, std::string const& id, char const* variable_name,
122122
char const* id_name
123123
) {
124-
auto it = std::find(ids.begin(), ids.end(), id);
124+
auto it = std::ranges::find(ids, id);
125125
if (it != ids.end())
126126
values.at(it - ids.begin()) = value;
127127
else
@@ -338,8 +338,8 @@ void Model::initializeSplineSensitivities() {
338338
std::vector<realtype> tmp_dvalues(allnodes, 0.0);
339339
std::vector<realtype> tmp_dslopes(allnodes, 0.0);
340340
for (int ip = 0; ip < nplist(); ip++) {
341-
std::fill(tmp_dvalues.begin(), tmp_dvalues.end(), 0.0);
342-
std::fill(tmp_dslopes.begin(), tmp_dslopes.end(), 0.0);
341+
std::ranges::fill(tmp_dvalues, 0.0);
342+
std::ranges::fill(tmp_dslopes, 0.0);
343343
fdspline_valuesdp(
344344
tmp_dvalues.data(), state_.unscaledParameters.data(),
345345
state_.fixedParameters.data(), plist(ip)
@@ -405,7 +405,7 @@ void Model::initEvents(
405405
) {
406406
std::vector<realtype> rootvals(ne, 0.0);
407407
froot(simulation_parameters_.tstart_, x, dx, rootvals);
408-
std::fill(roots_found.begin(), roots_found.end(), 0);
408+
std::ranges::fill(roots_found, 0);
409409
for (int ie = 0; ie < ne; ie++) {
410410
if (rootvals.at(ie) < 0) {
411411
state_.h.at(ie) = 0.0;
@@ -796,7 +796,7 @@ double Model::getTimepoint(int const it) const {
796796
}
797797

798798
void Model::setTimepoints(std::vector<realtype> const& ts) {
799-
if (!std::is_sorted(ts.begin(), ts.end()))
799+
if (!std::ranges::is_sorted(ts))
800800
throw AmiException("Encountered non-monotonic timepoints, please order"
801801
" timepoints such that they are monotonically"
802802
" increasing!");
@@ -813,9 +813,7 @@ std::vector<bool> const& Model::getStateIsNonNegative() const {
813813

814814
void Model::setStateIsNonNegative(std::vector<bool> const& nonNegative) {
815815
auto any_state_non_negative
816-
= std::any_of(nonNegative.begin(), nonNegative.end(), [](bool x) {
817-
return x;
818-
});
816+
= std::ranges::any_of(nonNegative, [](bool x) { return x; });
819817
if (nx_solver != nx_rdata) {
820818
if (any_state_non_negative)
821819
throw AmiException("Non-negative states are not supported with"
@@ -845,7 +843,7 @@ int Model::plist(int pos) const { return state_.plist.at(pos); }
845843

846844
void Model::setParameterList(std::vector<int> const& plist) {
847845
int np = this->np(); // cannot capture 'this' in lambda expression
848-
if (std::any_of(plist.begin(), plist.end(), [&np](int idx) {
846+
if (std::ranges::any_of(plist, [&np](int idx) {
849847
return idx < 0 || idx >= np;
850848
})) {
851849
throw AmiException("Indices in plist must be in [0..np]");
@@ -1112,7 +1110,7 @@ void Model::addObservableObjective(
11121110
std::vector<realtype> nllh(nJ, 0.0);
11131111
for (int iyt = 0; iyt < nytrue; iyt++) {
11141112
if (edata.isSetObservedData(it, iyt)) {
1115-
std::fill(nllh.begin(), nllh.end(), 0.0);
1113+
std::ranges::fill(nllh, 0.0);
11161114
fJy(nllh.data(), iyt, state_.unscaledParameters.data(),
11171115
state_.fixedParameters.data(), derived_state_.y_.data(),
11181116
derived_state_.sigmay_.data(), edata.getObservedDataPtr(it));
@@ -1303,7 +1301,7 @@ void Model::addEventObjective(
13031301
std::vector<realtype> nllh(nJ, 0.0);
13041302
for (int iztrue = 0; iztrue < nztrue; iztrue++) {
13051303
if (edata.isSetObservedEvents(nroots, iztrue)) {
1306-
std::fill(nllh.begin(), nllh.end(), 0.0);
1304+
std::ranges::fill(nllh, 0.0);
13071305
fJz(nllh.data(), iztrue, state_.unscaledParameters.data(),
13081306
state_.fixedParameters.data(), derived_state_.z_.data(),
13091307
derived_state_.sigmaz_.data(),
@@ -1323,7 +1321,7 @@ void Model::addEventObjectiveRegularization(
13231321
std::vector<realtype> nllh(nJ, 0.0);
13241322
for (int iztrue = 0; iztrue < nztrue; iztrue++) {
13251323
if (edata.isSetObservedEvents(nroots, iztrue)) {
1326-
std::fill(nllh.begin(), nllh.end(), 0.0);
1324+
std::ranges::fill(nllh, 0.0);
13271325
fJrz(
13281326
nllh.data(), iztrue, state_.unscaledParameters.data(),
13291327
state_.fixedParameters.data(), derived_state_.rz_.data(),
@@ -1395,7 +1393,7 @@ void Model::getEventTimeSensitivity(
13951393
AmiVector const& x, AmiVectorArray const& sx
13961394
) {
13971395

1398-
std::fill(stau.begin(), stau.end(), 0.0);
1396+
std::ranges::fill(stau, 0.0);
13991397

14001398
for (int ip = 0; ip < nplist(); ip++) {
14011399
fstau(
@@ -1525,7 +1523,7 @@ void Model::updateHeavisideB(int const* rootsfound) {
15251523
int Model::checkFinite(
15261524
gsl::span<realtype const> array, ModelQuantity model_quantity, realtype t
15271525
) const {
1528-
auto it = std::find_if(array.begin(), array.end(), [](realtype x) {
1526+
auto it = std::ranges::find_if(array, [](realtype x) {
15291527
return !std::isfinite(x);
15301528
});
15311529
if (it == array.end()) {
@@ -1624,7 +1622,7 @@ int Model::checkFinite(
16241622
gsl::span<realtype const> array, ModelQuantity model_quantity,
16251623
size_t num_cols, realtype t
16261624
) const {
1627-
auto it = std::find_if(array.begin(), array.end(), [](realtype x) {
1625+
auto it = std::ranges::find_if(array, [](realtype x) {
16281626
return !std::isfinite(x);
16291627
});
16301628
if (it == array.end()) {
@@ -1739,7 +1737,7 @@ int Model::checkFinite(SUNMatrix m, ModelQuantity model_quantity, realtype t)
17391737
// check flat array, to see if there are any issues
17401738
// (faster, in particular for sparse arrays)
17411739
auto m_flat = gsl::make_span(m);
1742-
auto it = std::find_if(m_flat.begin(), m_flat.end(), [](realtype x) {
1740+
auto it = std::ranges::find_if(m_flat, [](realtype x) {
17431741
return !std::isfinite(x);
17441742
});
17451743
if (it == m_flat.end()) {
@@ -1831,9 +1829,7 @@ void Model::setAlwaysCheckFinite(bool alwaysCheck) {
18311829
bool Model::getAlwaysCheckFinite() const { return always_check_finite_; }
18321830

18331831
void Model::fx0(AmiVector& x) {
1834-
std::fill(
1835-
derived_state_.x_rdata_.begin(), derived_state_.x_rdata_.end(), 0.0
1836-
);
1832+
std::ranges::fill(derived_state_.x_rdata_, 0.0);
18371833
/* this function also computes initial total abundances */
18381834
fx0(derived_state_.x_rdata_.data(), simulation_parameters_.tstart_,
18391835
state_.unscaledParameters.data(), state_.fixedParameters.data());
@@ -1877,10 +1873,7 @@ void Model::fsx0(AmiVectorArray& sx, AmiVector const& x) {
18771873
for (int ip = 0; ip < nplist(); ip++) {
18781874
if (ncl() > 0)
18791875
stcl = &state_.stotal_cl.at(plist(ip) * ncl());
1880-
std::fill(
1881-
derived_state_.sx_rdata_.begin(), derived_state_.sx_rdata_.end(),
1882-
0.0
1883-
);
1876+
std::ranges::fill(derived_state_.sx_rdata_, 0.0);
18841877
fsx0(
18851878
derived_state_.sx_rdata_.data(), simulation_parameters_.tstart_,
18861879
computeX_pos(x), state_.unscaledParameters.data(),
@@ -2244,10 +2237,7 @@ void Model::fdJydy(int const it, AmiVector const& x, ExpData const& edata) {
22442237
}
22452238
}
22462239
} else {
2247-
std::fill(
2248-
derived_state_.dJydy_matlab_.begin(),
2249-
derived_state_.dJydy_matlab_.end(), 0.0
2250-
);
2240+
std::ranges::fill(derived_state_.dJydy_matlab_, 0.0);
22512241
for (int iyt = 0; iyt < nytrue; iyt++) {
22522242
if (!edata.isSetObservedData(it, iyt))
22532243
continue;
@@ -2821,7 +2811,7 @@ void Model::fsspl(realtype const t) {
28212811

28222812
void Model::fw(realtype const t, realtype const* x, bool include_static) {
28232813
if (include_static) {
2824-
std::fill(derived_state_.w_.begin(), derived_state_.w_.end(), 0.0);
2814+
std::ranges::fill(derived_state_.w_, 0.0);
28252815
}
28262816
fspl(t);
28272817
fw(derived_state_.w_.data(), t, x, state_.unscaledParameters.data(),
@@ -3076,7 +3066,7 @@ std::vector<double> Model::get_trigger_timepoints() const {
30763066
for (auto const& kv : state_independent_events_) {
30773067
*(it++) = kv.first;
30783068
}
3079-
std::sort(trigger_timepoints.begin(), trigger_timepoints.end());
3069+
std::ranges::sort(trigger_timepoints);
30803070
return trigger_timepoints;
30813071
}
30823072

src/model_dae.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void Model_DAE::froot(
103103
void Model_DAE::froot(
104104
realtype t, const_N_Vector x, const_N_Vector dx, gsl::span<realtype> root
105105
) {
106-
std::fill(root.begin(), root.end(), 0.0);
106+
std::ranges::fill(root, 0.0);
107107
auto x_pos = computeX_pos(x);
108108
froot(
109109
root.data(), t, N_VGetArrayPointerConst(x_pos),

0 commit comments

Comments
 (0)