Skip to content

Commit

Permalink
Add function to convert ReturnData::status flags to string (#1864)
Browse files Browse the repository at this point in the history
  • Loading branch information
dweindl authored Sep 10, 2022
1 parent 81f8b43 commit 779624b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
9 changes: 9 additions & 0 deletions include/amici/amici.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ std::vector<std::unique_ptr<ReturnData>>
runAmiciSimulations(Solver const &solver, const std::vector<ExpData *> &edatas,
Model const &model, bool failfast, int num_threads);


/**
* @brief Get the string representation of the given simulation status code
* (see ReturnData::status).
* @param return_code
* @return Name of the variable representing this status code.
*/
std::string simulation_status_to_str(int status);

} // namespace amici

#endif /* amici_h */
5 changes: 4 additions & 1 deletion include/amici/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ constexpr double pi = M_PI;

constexpr int AMICI_ONEOUTPUT= 5;

/* Return codes */
// Return codes
//
// NOTE: When adding / removing / renaming return codes,
// please update simulation_status_to_str_map in amici.h
constexpr int AMICI_RECOVERABLE_ERROR= 1;
constexpr int AMICI_UNRECOVERABLE_ERROR= -10;
constexpr int AMICI_TOO_MUCH_WORK= -1;
Expand Down
31 changes: 31 additions & 0 deletions src/amici.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <type_traits>

Expand All @@ -43,6 +44,24 @@ static_assert(std::is_same<amici::realtype, realtype>::value,

namespace amici {

std::map<int, std::string> simulation_status_to_str_map = {
{AMICI_RECOVERABLE_ERROR, "AMICI_RECOVERABLE_ERROR"},
{AMICI_UNRECOVERABLE_ERROR, "AMICI_UNRECOVERABLE_ERROR"},
{AMICI_TOO_MUCH_WORK, "AMICI_TOO_MUCH_WORK"},
{AMICI_TOO_MUCH_ACC, "AMICI_TOO_MUCH_ACC"},
{AMICI_ERR_FAILURE, "AMICI_ERR_FAILURE"},
{AMICI_CONV_FAILURE, "AMICI_CONV_FAILURE"},
{AMICI_RHSFUNC_FAIL, "AMICI_RHSFUNC_FAIL"},
{AMICI_ILL_INPUT, "AMICI_ILL_INPUT"},
{AMICI_ERROR, "AMICI_ERROR"},
{AMICI_NO_STEADY_STATE, "AMICI_NO_STEADY_STATE"},
{AMICI_DAMPING_FACTOR_ERROR, "AMICI_DAMPING_FACTOR_ERROR"},
{AMICI_SINGULAR_JACOBIAN, "AMICI_SINGULAR_JACOBIAN"},
{AMICI_NOT_IMPLEMENTED, "AMICI_NOT_IMPLEMENTED"},
{AMICI_MAX_TIME_EXCEEDED, "AMICI_MAX_TIME_EXCEEDED"},
{AMICI_SUCCESS, "AMICI_SUCCESS"},
};

/** AMICI default application context, kept around for convenience for using
* amici::runAmiciSimulation or instantiating Solver and Model without special
* needs.
Expand Down Expand Up @@ -320,4 +339,16 @@ AmiciApplication::errorF(const char* identifier, const char* format, ...) const
error(identifier, str);
}

std::string simulation_status_to_str(int status)
{
try {
return simulation_status_to_str_map.at(status);
} catch (std::out_of_range const&) {
// Missing mapping - terminate if this is a debug build,
// but show the number if non-debug.
gsl_ExpectsDebug(false);
return std::to_string(status);
}
}

} // namespace amici
6 changes: 6 additions & 0 deletions tests/cpp/unittests/testMisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,5 +704,11 @@ TEST(UnravelIndex, UnravelIndexSunMatSparse)
SUNMatDestroy(S);
}

TEST(ReturnCodeToStr, ReturnCodeToStr)
{
EXPECT_EQ("AMICI_SUCCESS", simulation_status_to_str(AMICI_SUCCESS));
EXPECT_EQ("AMICI_UNRECOVERABLE_ERROR",
simulation_status_to_str(AMICI_UNRECOVERABLE_ERROR));
}

} // namespace

0 comments on commit 779624b

Please sign in to comment.