Skip to content

Commit 0b42635

Browse files
committed
Cleanup: ensure debug_bdf_order.cpp uses correct integrator and includes; update timeout_integrator.hpp for thread safety and robust timeout handling.
1 parent 2503bc6 commit 0b42635

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

debug_bdf_order.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <vector>
33
#include <iomanip>
44
#include <cmath>
5-
#include <integrators/ode/bdf_scipy.hpp>
5+
#include <diffeq.hpp>
66

77
// Simple exponential decay: dy/dt = -y
88
void exponential_decay(double t, const std::vector<double>& y, std::vector<double>& dydt) {
@@ -19,7 +19,7 @@ int main() {
1919
double t_end = 1.0;
2020
double dt = 0.1;
2121

22-
auto integrator = diffeq::ScipyBDFIntegrator<std::vector<double>>(exponential_decay, 1e-3, 1e-6);
22+
auto integrator = diffeq::BDFIntegrator<std::vector<double>>(exponential_decay, 1e-3, 1e-6);
2323
integrator.set_time(t_start);
2424

2525
std::cout << "Integration steps:" << std::endl;

include/core/timeout_integrator.hpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,26 @@ bool integrate_with_timeout(Integrator& integrator, State& state,
261261
typename Integrator::time_type dt,
262262
typename Integrator::time_type end_time,
263263
std::chrono::milliseconds timeout = std::chrono::milliseconds{5000}) {
264-
auto future = std::async(std::launch::async, [&integrator, &state, dt, end_time]() {
265-
integrator.integrate(state, dt, end_time);
264+
// Create a copy of the state to avoid race conditions
265+
State state_copy = state;
266+
267+
auto future = std::async(std::launch::async, [&integrator, &state_copy, dt, end_time]() {
268+
integrator.integrate(state_copy, dt, end_time);
266269
});
267270

268-
return future.wait_for(timeout) == std::future_status::ready;
271+
auto status = future.wait_for(timeout);
272+
273+
if (status == std::future_status::ready) {
274+
try {
275+
future.get(); // Check for exceptions
276+
state = state_copy; // Update original state only if successful
277+
return true;
278+
} catch (...) {
279+
return false;
280+
}
281+
} else {
282+
return false; // Timed out
283+
}
269284
}
270285

271286
} // namespace diffeq::core

0 commit comments

Comments
 (0)