Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dweindl committed Dec 4, 2023
1 parent 498301d commit 5d3ab73
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
22 changes: 14 additions & 8 deletions include/amici/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,35 @@ class AmiException : public std::exception {
};

/**
* @brief cvode exception handler class
* @brief CVODE exception handler class
*/
class CvodeException : public AmiException {
public:
/**
* @brief Constructor
* @param error_code error code returned by cvode function
* @param function cvode function name
* @param error_code error code returned by CVODE function
* @param function CVODE function name
* @param extra Extra text to append to error message
*/
CvodeException(int error_code, char const* function);
CvodeException(
int error_code, char const* function, char const* extra = nullptr
);
};

/**
* @brief ida exception handler class
* @brief IDA exception handler class
*/
class IDAException : public AmiException {
public:
/**
* @brief Constructor
* @param error_code error code returned by ida function
* @param function ida function name
* @param error_code error code returned by IDA function
* @param function IDA function name
* @param extra Extra text to append to error message
*/
IDAException(int error_code, char const* function);
IDAException(
int error_code, char const* function, char const* extra = nullptr
);
};

/**
Expand Down
10 changes: 6 additions & 4 deletions python/sdist/amici/de_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3895,22 +3895,24 @@ def _get_symbol_id_initializer_list(self, name: str) -> str:
)

def _get_state_independent_event_intializer(self) -> str:
tmp_map = {}
"""Get initializer list for state independent events in amici::Model."""
map_time_to_event_idx = {}
for event_idx, event in enumerate(self.model.events()):
if not event.triggers_at_fixed_timepoint():
continue
trigger_time = float(event.get_trigger_time())
try:
tmp_map[trigger_time].append(event_idx)
map_time_to_event_idx[trigger_time].append(event_idx)
except KeyError:
tmp_map[trigger_time] = [event_idx]
map_time_to_event_idx[trigger_time] = [event_idx]

def vector_initializer(v):
"""std::vector initializer list with elements from `v`"""
return f"{{{', '.join(map(str, v))}}}"

return ", ".join(
f"{{{trigger_time}, {vector_initializer(event_idxs)}}}"
for trigger_time, event_idxs in tmp_map.items()
for trigger_time, event_idxs in map_time_to_event_idx.items()
)

def _write_c_make_file(self):
Expand Down
14 changes: 10 additions & 4 deletions src/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ void AmiException::storeMessage(char const* fmt, va_list argptr) {
vsnprintf(msg_.data(), msg_.size(), fmt, argptr);
}

CvodeException::CvodeException(int const error_code, char const* function)
CvodeException::CvodeException(
int const error_code, char const* function, char const* extra
)
: AmiException(
"Cvode routine %s failed with error code %i", function, error_code
"CVODE routine %s failed with error code %i. %s", function, error_code,
extra ? extra : ""
) {}

IDAException::IDAException(int const error_code, char const* function)
IDAException::IDAException(
int const error_code, char const* function, char const* extra
)
: AmiException(
"IDA routine %s failed with error code %i", function, error_code
"IDA routine %s failed with error code %i. %s", function, error_code,
extra ? extra : ""
) {}

IntegrationFailure::IntegrationFailure(int code, realtype t)
Expand Down
15 changes: 9 additions & 6 deletions src/solver_cvodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,16 @@ void CVodeSolver::reInitPostProcess(
if (status == CV_ROOT_RETURN)
throw CvodeException(
status,
"CVode returned a root after "
"reinitialization. The initial step-size after the event or "
"heaviside function is too small. To fix this, increase absolute "
"CVode returned a root after reinitialization. "
"The initial step-size after the event or "
"Heaviside function is too small. To fix this, increase absolute "
"and relative tolerances!"
);
if (status != CV_SUCCESS)
throw CvodeException(status, "reInitPostProcess");
if (status != CV_SUCCESS) {
std::stringstream msg;
msg<<"tout: "<<tout<<", t: "<<*t<<".";
throw CvodeException(status, "reInitPostProcess", msg.str().c_str());
}

cv_mem->cv_nst = nst_tmp + 1;
if (cv_mem->cv_adjMallocDone == SUNTRUE) {
Expand All @@ -515,7 +518,7 @@ void CVodeSolver::reInitPostProcess(
dt_mem[cv_mem->cv_nst % ca_mem->ca_nsteps]->t = *t;
ca_mem->ca_IMstore(cv_mem, dt_mem[cv_mem->cv_nst % ca_mem->ca_nsteps]);

/* Set t1 field of the current ckeck point structure
/* Set t1 field of the current check point structure
for the case in which there will be no future
check points */
ca_mem->ck_mem->ck_t1 = *t;
Expand Down

0 comments on commit 5d3ab73

Please sign in to comment.