Skip to content

Commit 5e0acb2

Browse files
authored
refactor: Rename AmiVector::getLength and AmiVectorArray::getLength to size (#2965)
To be more consistent with STL containers and #887.
1 parent 8b832ca commit 5e0acb2

File tree

11 files changed

+37
-35
lines changed

11 files changed

+37
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ BREAKING CHANGES
1414
* `ReturnDataView.posteq_status` and `ReturnDataView.preeq_status` now
1515
return `list[SteadyStateStatus]` instead of an `ndarray[int]` of shape
1616
`(1, 3)`.
17+
* `AmiVector::getLength` and `AmiVectorArray::getLength` have been renamed to
18+
`size` to be more consistent with STL containers.
1719
* The following deprecated functionality has been removed:
1820
* The complete MATLAB interface has been removed.
1921
* `NonlinearSolverIteration::functional` has been removed,

include/amici/vector.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class AmiVector {
199199
* @brief returns the length of the vector
200200
* @return length
201201
*/
202-
[[nodiscard]] int getLength() const;
202+
[[nodiscard]] int size() const;
203203

204204
/**
205205
* @brief fills vector with zero values
@@ -310,7 +310,7 @@ class AmiVector {
310310
*/
311311
inline std::ostream& operator<<(std::ostream& os, AmiVector const& v) {
312312
os << "[";
313-
for (int i = 0; i < v.getLength(); ++i) {
313+
for (int i = 0; i < v.size(); ++i) {
314314
if (i > 0)
315315
os << ", ";
316316
os << v.at(i);
@@ -429,7 +429,7 @@ class AmiVectorArray {
429429
* @brief length of AmiVectorArray
430430
* @return length
431431
*/
432-
int getLength() const;
432+
int size() const;
433433

434434
/**
435435
* @brief set every AmiVector in AmiVectorArray to zero
@@ -482,7 +482,7 @@ class AmiVectorArray {
482482
*/
483483
inline std::ostream& operator<<(std::ostream& os, AmiVectorArray const& arr) {
484484
os << "[";
485-
for (int i = 0; i < arr.getLength(); ++i) {
485+
for (int i = 0; i < arr.size(); ++i) {
486486
if (i > 0)
487487
os << ", ";
488488
os << arr[i];

src/model.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3038,7 +3038,7 @@ void Model::set_steadystate_mask(std::vector<realtype> const& mask) {
30383038

30393039
const_N_Vector Model::computeX_pos(const_N_Vector x) {
30403040
if (any_state_non_negative_) {
3041-
for (int ix = 0; ix < derived_state_.x_pos_tmp_.getLength(); ++ix) {
3041+
for (int ix = 0; ix < derived_state_.x_pos_tmp_.size(); ++ix) {
30423042
derived_state_.x_pos_tmp_.at(ix)
30433043
= (state_is_non_negative_.at(ix) && NV_Ith_S(x, ix) < 0)
30443044
? 0

src/rdata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ void ReturnData::processForwardProblem(
290290
initializeObjectiveFunction(model.hasQuadraticLLH());
291291

292292
auto const& initialState = fwd.getInitialSimulationState();
293-
if (initialState.sol.x.getLength() == 0 && model.nx_solver > 0)
293+
if (initialState.sol.x.size() == 0 && model.nx_solver > 0)
294294
return; // if x wasn't set forward problem failed during initialization
295295

296296
model.setModelState(initialState.mod);

src/solver.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,8 @@ void Solver::applySensitivityTolerances() const {
713713
}
714714

715715
void Solver::apply_constraints() const {
716-
if (constraints_.getLength() != 0
717-
&& gsl::narrow<int>(constraints_.getLength()) != nx()) {
716+
if (constraints_.size() != 0
717+
&& gsl::narrow<int>(constraints_.size()) != nx()) {
718718
throw std::invalid_argument(
719719
"Constraints must have the same size as the state vector."
720720
);
@@ -1230,11 +1230,11 @@ void Solver::setErrHandlerFn() const {
12301230
);
12311231
}
12321232

1233-
int Solver::nplist() const { return sx_.getLength(); }
1233+
int Solver::nplist() const { return sx_.size(); }
12341234

1235-
int Solver::nx() const { return x_.getLength(); }
1235+
int Solver::nx() const { return x_.size(); }
12361236

1237-
int Solver::nquad() const { return xQB_.getLength(); }
1237+
int Solver::nquad() const { return xQB_.size(); }
12381238

12391239
bool Solver::getInitDone() const { return initialized_; }
12401240

src/solver_cvodes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ void CVodeSolver::apply_constraints() const {
299299

300300
int status = CVodeSetConstraints(
301301
solver_memory_.get(),
302-
constraints_.getLength() > 0 ? constraints_.getNVector() : nullptr
302+
constraints_.size() > 0 ? constraints_.getNVector() : nullptr
303303
);
304304
if (status != CV_SUCCESS) {
305305
throw CvodeException(status, "CVodeSetConstraints");

src/solver_idas.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ void IDASolver::apply_constraints() const {
301301

302302
int status = IDASetConstraints(
303303
solver_memory_.get(),
304-
constraints_.getLength() > 0 ? constraints_.getNVector() : nullptr
304+
constraints_.size() > 0 ? constraints_.getNVector() : nullptr
305305
);
306306
if (status != IDA_SUCCESS) {
307307
throw IDAException(status, "IDASetConstraints");

src/steadystateproblem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ realtype getWrmsNorm(
4040
N_VInv(ewt.getNVector(), ewt.getNVector());
4141

4242
// wrms = sqrt(sum((xdot/ewt)**2)/n) where n = size of state vector
43-
if (mask.getLength()) {
43+
if (mask.size()) {
4444
return N_VWrmsNormMask(
4545
const_cast<N_Vector>(xdot.getNVector()), ewt.getNVector(),
4646
const_cast<N_Vector>(mask.getNVector())
@@ -189,7 +189,7 @@ bool NewtonsMethod::has_converged(
189189
auto nonnegative = model_->getStateIsNonNegative();
190190
Expects(nonnegative.size() == state.x.getVector().size());
191191
auto state_modified = false;
192-
for (int ix = 0; ix < state.x.getLength(); ix++) {
192+
for (int ix = 0; ix < state.x.size(); ix++) {
193193
if (state.x[ix] < 0.0 && nonnegative[ix]) {
194194
state.x[ix] = 0.0;
195195
state_modified = true;

src/sundials_linsol_wrapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ SUNLinSolBand::SUNLinSolBand(N_Vector x, SUNMatrixWrapper A)
181181

182182
SUNLinSolBand::SUNLinSolBand(AmiVector const& x, int ubw, int lbw)
183183
: SUNLinSolWrapper(
184-
nullptr, SUNMatrixWrapper(x.getLength(), ubw, lbw, x.get_ctx())
184+
nullptr, SUNMatrixWrapper(x.size(), ubw, lbw, x.get_ctx())
185185
) {
186186
solver_
187187
= SUNLinSol_Band(const_cast<N_Vector>(x.getNVector()), A_, x.get_ctx());
@@ -191,7 +191,7 @@ SUNLinSolBand::SUNLinSolBand(AmiVector const& x, int ubw, int lbw)
191191

192192
SUNLinSolDense::SUNLinSolDense(AmiVector const& x)
193193
: SUNLinSolWrapper(
194-
nullptr, SUNMatrixWrapper(x.getLength(), x.getLength(), x.get_ctx())
194+
nullptr, SUNMatrixWrapper(x.size(), x.size(), x.get_ctx())
195195
) {
196196
solver_ = SUNLinSol_Dense(
197197
const_cast<N_Vector>(x.getNVector()), A_, x.get_ctx()
@@ -212,7 +212,7 @@ SUNLinSolKLU::SUNLinSolKLU(
212212
: SUNLinSolWrapper(
213213
nullptr,
214214
SUNMatrixWrapper(
215-
x.getLength(), x.getLength(), nnz, sparsetype, x.get_ctx()
215+
x.size(), x.size(), nnz, sparsetype, x.get_ctx()
216216
)
217217
) {
218218
solver_

src/vector.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const_N_Vector AmiVector::getNVector() const { return nvec_; }
2121

2222
std::vector<realtype> const& AmiVector::getVector() const { return vec_; }
2323

24-
int AmiVector::getLength() const { return gsl::narrow<int>(vec_.size()); }
24+
int AmiVector::size() const { return gsl::narrow<int>(vec_.size()); }
2525

2626
void AmiVector::zero() { set(0.0); }
2727

@@ -48,11 +48,11 @@ realtype const& AmiVector::at(int const pos) const {
4848
}
4949

5050
void AmiVector::copy(AmiVector const& other) {
51-
if (getLength() != other.getLength())
51+
if (size() != other.size())
5252
throw AmiException(
5353
"Dimension of AmiVector (%i) does not "
5454
"match input dimension (%i)",
55-
getLength(), other.getLength()
55+
size(), other.size()
5656
);
5757
std::ranges::copy(other.vec_, vec_.begin());
5858
}
@@ -85,17 +85,17 @@ AmiVectorArray::AmiVectorArray(
8585

8686
AmiVectorArray& AmiVectorArray::operator=(AmiVectorArray const& other) {
8787
vec_array_ = other.vec_array_;
88-
nvec_array_.resize(other.getLength());
89-
for (int idx = 0; idx < other.getLength(); idx++) {
88+
nvec_array_.resize(other.size());
89+
for (int idx = 0; idx < other.size(); idx++) {
9090
nvec_array_.at(idx) = vec_array_.at(idx).getNVector();
9191
}
9292
return *this;
9393
}
9494

9595
AmiVectorArray::AmiVectorArray(AmiVectorArray const& vaold)
9696
: vec_array_(vaold.vec_array_) {
97-
nvec_array_.resize(vaold.getLength());
98-
for (int idx = 0; idx < vaold.getLength(); idx++) {
97+
nvec_array_.resize(vaold.size());
98+
for (int idx = 0; idx < vaold.size(); idx++) {
9999
nvec_array_.at(idx) = vec_array_.at(idx).getNVector();
100100
}
101101
}
@@ -132,7 +132,7 @@ AmiVector const& AmiVectorArray::operator[](int const pos) const {
132132
return vec_array_.at(pos);
133133
}
134134

135-
int AmiVectorArray::getLength() const {
135+
int AmiVectorArray::size() const {
136136
return gsl::narrow<int>(vec_array_.size());
137137
}
138138

@@ -145,7 +145,7 @@ void AmiVectorArray::flatten_to_vector(std::vector<realtype>& vec) const {
145145
int n_outer = gsl::narrow<int>(vec_array_.size());
146146
if (n_outer == 0)
147147
return; // nothing to do ...
148-
int n_inner = vec_array_.at(0).getLength();
148+
int n_inner = vec_array_.at(0).size();
149149

150150
if (gsl::narrow<int>(vec.size()) != n_inner * n_outer) {
151151
throw AmiException(
@@ -162,14 +162,14 @@ void AmiVectorArray::flatten_to_vector(std::vector<realtype>& vec) const {
162162
}
163163

164164
void AmiVectorArray::copy(AmiVectorArray const& other) {
165-
if (getLength() != other.getLength())
165+
if (size() != other.size())
166166
throw AmiException(
167167
"Dimension of AmiVectorArray (%i) does not "
168168
"match input dimension (%i)",
169-
getLength(), other.getLength()
169+
size(), other.size()
170170
);
171171

172-
for (int iv = 0; iv < getLength(); ++iv) {
172+
for (int iv = 0; iv < size(); ++iv) {
173173
vec_array_.at(iv).copy(other.vec_array_.at(iv));
174174
nvec_array_[iv] = vec_array_.at(iv).getNVector();
175175
}

0 commit comments

Comments
 (0)