Skip to content

Commit e490a01

Browse files
committed
Fix template parameter issues in examples and main integrators
- Changed all integrators from two-parameter template to single-parameter - Fixed RK4Integrator, EulerMaruyamaIntegrator, and other integrators - Updated factory function calls from two-parameter to single-parameter - Fixed SDE demo and usage examples - Updated standard parallelism demos - Disabled problematic interface examples temporarily Most template parameter issues resolved, compilation progresses much further.
1 parent 3306365 commit e490a01

File tree

9 files changed

+138
-126
lines changed

9 files changed

+138
-126
lines changed

examples/rk4_integrator_usage.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main() {
5050
std::cout << "\n1. Exponential Decay (dy/dt = -0.5*y)" << std::endl;
5151
std::cout << "-------------------------------------" << std::endl;
5252

53-
diffeq::RK4Integrator<std::vector<double>, double> decay_integrator(exponential_decay);
53+
diffeq::RK4Integrator<std::vector<double>> decay_integrator(exponential_decay);
5454
std::vector<double> decay_state = {2.0}; // y(0) = 2
5555

5656
std::cout << "Time\tValue" << std::endl;
@@ -65,7 +65,7 @@ int main() {
6565
std::cout << "\n2. Lorenz Attractor (first 20 steps)" << std::endl;
6666
std::cout << "------------------------------------" << std::endl;
6767

68-
diffeq::RK4Integrator<std::vector<double>, double> lorenz_integrator(lorenz_system);
68+
diffeq::RK4Integrator<std::vector<double>> lorenz_integrator(lorenz_system);
6969
std::vector<double> lorenz_state = {1.0, 1.0, 1.0}; // Initial conditions
7070

7171
std::cout << "Time\tX\t\tY\t\tZ" << std::endl;
@@ -85,7 +85,7 @@ int main() {
8585
std::cout << "\n3. Damped Harmonic Oscillator (float precision)" << std::endl;
8686
std::cout << "----------------------------------------------" << std::endl;
8787

88-
diffeq::RK4Integrator<std::array<float, 2>, float> oscillator_integrator(damped_oscillator);
88+
diffeq::RK4Integrator<std::array<float, 2>> oscillator_integrator(damped_oscillator);
8989
std::array<float, 2> oscillator_state = {1.0f, 0.0f}; // x(0) = 1, v(0) = 0
9090

9191
std::cout << "Time\tPosition\tVelocity" << std::endl;
@@ -104,8 +104,8 @@ int main() {
104104
std::cout << "\n4. Polymorphic Usage" << std::endl;
105105
std::cout << "-------------------" << std::endl;
106106

107-
auto integrator = std::make_unique<diffeq::RK4Integrator<std::vector<double>, double>>(exponential_decay);
108-
AbstractIntegrator<std::vector<double>, double>* base_ptr = integrator.get();
107+
auto integrator = std::make_unique<diffeq::RK4Integrator<std::vector<double>>>(exponential_decay);
108+
AbstractIntegrator<std::vector<double>>* base_ptr = integrator.get();
109109

110110
std::vector<double> poly_state = {5.0};
111111
std::cout << "Initial: t=" << base_ptr->current_time() << ", y=" << poly_state[0] << std::endl;

examples/sde_demo.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ int main() {
1414
gx[0] = 0.1 * x[0];
1515
};
1616

17-
auto problem = diffeq::factory::make_sde_problem<std::vector<double>, double>(
17+
// Create SDE problem and Wiener process directly without factory functions
18+
auto problem = std::make_shared<diffeq::sde::SDEProblem<std::vector<double>>>(
1819
drift_func, diffusion_func, diffeq::sde::NoiseType::DIAGONAL_NOISE);
1920

20-
auto wiener = diffeq::sde::factory::make_wiener_process<std::vector<double>, double>(1, 12345);
21-
// auto integrator = diffeq::sde::factory::make_euler_maruyama_integrator<std::vector<double>, double>(problem, wiener);
22-
diffeq::EulerMaruyamaIntegrator<std::vector<double>, double> integrator(problem, wiener);
21+
auto wiener = std::make_shared<diffeq::sde::WienerProcess<std::vector<double>>>(1, 12345);
22+
23+
diffeq::EulerMaruyamaIntegrator<std::vector<double>> integrator(problem, wiener);
2324

2425
std::vector<double> state = {1.0}; // Initial condition
2526
double dt = 0.01;

examples/sde_usage_demo.cpp

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class BlackScholesModel {
3737
gS[0] = sigma * S[0];
3838
};
3939

40-
auto problem = diffeq::factory::make_sde_problem<std::vector<double>, double>(
41-
drift_func, diffusion_func, diffeq::NoiseType::DIAGONAL_NOISE);
40+
auto problem = diffeq::sde::factory::make_sde_problem<std::vector<double>>(
41+
drift_func, diffusion_func, diffeq::sde::NoiseType::DIAGONAL_NOISE);
4242

43-
auto wiener = diffeq::factory::make_wiener_process<std::vector<double>, double>(1, 12345);
43+
auto wiener = diffeq::sde::factory::make_wiener_process<std::vector<double>>(1, 12345);
4444

4545
double S0 = 100.0; // Initial stock price
4646
double T = 1.0; // Time to maturity
@@ -57,7 +57,7 @@ class BlackScholesModel {
5757

5858
// Euler-Maruyama
5959
{
60-
diffeq::EulerMaruyamaIntegrator<std::vector<double>, double> integrator(problem, wiener);
60+
diffeq::EulerMaruyamaIntegrator<std::vector<double>> integrator(problem, wiener);
6161
std::vector<double> S = {S0};
6262
integrator.set_time(0.0);
6363
wiener->set_seed(12345);
@@ -68,9 +68,13 @@ class BlackScholesModel {
6868
results.push_back(S[0]);
6969
}
7070

71-
// Milstein
71+
// Milstein (with diffusion derivative)
7272
{
73-
diffeq::MilsteinIntegrator<std::vector<double>, double> integrator(problem, wiener);
73+
auto diffusion_derivative = [this](double /*t*/, const std::vector<double>& S, std::vector<double>& dgdS) {
74+
// For GBM: g(S) = σS, so g'(S) = σ
75+
dgdS[0] = sigma;
76+
};
77+
diffeq::MilsteinIntegrator<std::vector<double>> integrator(problem, diffusion_derivative, wiener);
7478
std::vector<double> S = {S0};
7579
integrator.set_time(0.0);
7680
wiener->set_seed(12345);
@@ -83,7 +87,7 @@ class BlackScholesModel {
8387

8488
// SRA1
8589
{
86-
diffeq::SRA1Integrator<std::vector<double>, double> integrator(problem, wiener);
90+
diffeq::SRA1Integrator<std::vector<double>> integrator(problem, wiener);
8791
std::vector<double> S = {S0};
8892
integrator.set_time(0.0);
8993
wiener->set_seed(12345);
@@ -96,7 +100,7 @@ class BlackScholesModel {
96100

97101
// SOSRA
98102
{
99-
diffeq::SOSRAIntegrator<std::vector<double>, double> integrator(problem, wiener);
103+
diffeq::SOSRAIntegrator<std::vector<double>> integrator(problem, wiener);
100104
std::vector<double> S = {S0};
101105
integrator.set_time(0.0);
102106
wiener->set_seed(12345);
@@ -109,7 +113,7 @@ class BlackScholesModel {
109113

110114
// SRIW1
111115
{
112-
diffeq::SRIW1Integrator<std::vector<double>, double> integrator(problem, wiener);
116+
diffeq::SRIW1Integrator<std::vector<double>> integrator(problem, wiener);
113117
std::vector<double> S = {S0};
114118
integrator.set_time(0.0);
115119
wiener->set_seed(12345);
@@ -122,7 +126,7 @@ class BlackScholesModel {
122126

123127
// SOSRI
124128
{
125-
diffeq::SOSRIIntegrator<std::vector<double>, double> integrator(problem, wiener);
129+
diffeq::SOSRIIntegrator<std::vector<double>> integrator(problem, wiener);
126130
std::vector<double> S = {S0};
127131
integrator.set_time(0.0);
128132
wiener->set_seed(12345);
@@ -174,10 +178,10 @@ class HestonModel {
174178
gx[1] = sigma * std::sqrt(V);
175179
};
176180

177-
auto problem = diffeq::factory::make_sde_problem<std::vector<double>, double>(
178-
drift_func, diffusion_func, diffeq::NoiseType::GENERAL_NOISE);
181+
auto problem = diffeq::sde::factory::make_sde_problem<std::vector<double>>(
182+
drift_func, diffusion_func, diffeq::sde::NoiseType::GENERAL_NOISE);
179183

180-
auto wiener = diffeq::factory::make_wiener_process<std::vector<double>, double>(2, 54321);
184+
auto wiener = diffeq::sde::factory::make_wiener_process<std::vector<double>>(2, 54321);
181185

182186
// Set correlated noise
183187
auto correlated_noise_func = [this](double /*t*/, const std::vector<double>& /*x*/,
@@ -198,7 +202,7 @@ class HestonModel {
198202
int steps = static_cast<int>(T / dt);
199203

200204
// Use high-order SDE integrator for better accuracy
201-
diffeq::SOSRAIntegrator<std::vector<double>, double> integrator(problem, wiener);
205+
diffeq::SOSRAIntegrator<std::vector<double>> integrator(problem, wiener);
202206
integrator.set_time(0.0);
203207

204208
std::cout << "Initial state: S = " << x[0] << ", V = " << x[1] << std::endl;
@@ -266,11 +270,18 @@ class NoisyOscillator {
266270
gstate[1] = sigma; // Noise in velocity (acceleration)
267271
};
268272

269-
auto problem = diffeq::factory::make_sde_problem<std::vector<double>, double>(
270-
drift_func, diffusion_func, diffeq::NoiseType::DIAGONAL_NOISE);
273+
auto problem = diffeq::sde::factory::make_sde_problem<std::vector<double>>(
274+
drift_func, diffusion_func, diffeq::sde::NoiseType::DIAGONAL_NOISE);
271275

272-
auto wiener = diffeq::factory::make_wiener_process<std::vector<double>, double>(1, 67890);
273-
diffeq::MilsteinIntegrator<std::vector<double>, double> integrator(problem, wiener);
276+
auto wiener = diffeq::sde::factory::make_wiener_process<std::vector<double>>(1, 67890);
277+
278+
// For this problem, diffusion g = [0, σ], so derivative g' = [0, 0]
279+
auto diffusion_derivative = [this](double /*t*/, const std::vector<double>& /*state*/, std::vector<double>& dgd_state) {
280+
dgd_state[0] = 0.0; // d/dx(0) = 0
281+
dgd_state[1] = 0.0; // d/dx(σ) = 0 since σ is constant
282+
};
283+
284+
diffeq::MilsteinIntegrator<std::vector<double>> integrator(problem, diffusion_derivative, wiener);
274285

275286
std::vector<double> state = {0.0, 0.0}; // Initial [x, xdot]
276287
double dt = 0.01;
@@ -352,11 +363,11 @@ class StochasticLotkaVolterra {
352363
gpop[1] = sigma2 * y; // Multiplicative noise for predator
353364
};
354365

355-
auto problem = diffeq::factory::make_sde_problem<std::vector<double>, double>(
356-
drift_func, diffusion_func, diffeq::NoiseType::DIAGONAL_NOISE);
366+
auto problem = diffeq::sde::factory::make_sde_problem<std::vector<double>>(
367+
drift_func, diffusion_func, diffeq::sde::NoiseType::DIAGONAL_NOISE);
357368

358-
auto wiener = diffeq::factory::make_wiener_process<std::vector<double>, double>(2, 11111);
359-
diffeq::SRA1Integrator<std::vector<double>, double> integrator(problem, wiener);
369+
auto wiener = diffeq::sde::factory::make_wiener_process<std::vector<double>>(2, 11111);
370+
diffeq::SRA1Integrator<std::vector<double>> integrator(problem, wiener);
360371

361372
std::vector<double> population = {2.0, 1.0}; // Initial [prey, predator]
362373
double dt = 0.01;
@@ -419,11 +430,11 @@ void demonstrate_async_sde_integration() {
419430
gx[1] = 0.05 * x[1];
420431
};
421432

422-
auto problem = diffeq::factory::make_sde_problem<std::vector<double>, double>(
423-
drift_func, diffusion_func, diffeq::NoiseType::DIAGONAL_NOISE);
433+
auto problem = diffeq::sde::factory::make_sde_problem<std::vector<double>>(
434+
drift_func, diffusion_func, diffeq::sde::NoiseType::DIAGONAL_NOISE);
424435

425-
auto wiener = diffeq::factory::make_wiener_process<std::vector<double>, double>(2, 22222);
426-
diffeq::EulerMaruyamaIntegrator<std::vector<double>, double> integrator(problem, wiener);
436+
auto wiener = diffeq::sde::factory::make_wiener_process<std::vector<double>>(2, 22222);
437+
diffeq::EulerMaruyamaIntegrator<std::vector<double>> integrator(problem, wiener);
427438

428439
// Note: Async integrator functionality is not currently available
429440
// For now, we'll use the regular integrator

examples/simplified_parallel_usage.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void demo_std_execution_parallelism() {
3838
states.begin(),
3939
states.end(),
4040
[&](std::vector<double>& state) {
41-
auto integrator = diffeq::RK4Integrator<std::vector<double>, double>(system);
41+
auto integrator = diffeq::RK4Integrator<std::vector<double>>(system);
4242
for (int i = 0; i < 1000; ++i) {
4343
integrator.step(state, 0.01);
4444
}
@@ -82,7 +82,7 @@ void demo_std_execution_parallelism() {
8282
system_template(t, y, dydt, omega);
8383
};
8484

85-
auto integrator = diffeq::RK4Integrator<std::vector<double>, double>(system);
85+
auto integrator = diffeq::RK4Integrator<std::vector<double>>(system);
8686
for (int step = 0; step < 1000; ++step) {
8787
integrator.step(results[i], 0.01);
8888
}
@@ -113,19 +113,19 @@ void demo_std_execution_parallelism() {
113113
[&](size_t i) {
114114
if (i == 0) {
115115
// RK4
116-
auto integrator = diffeq::RK4Integrator<std::vector<double>, double>(system);
116+
auto integrator = diffeq::RK4Integrator<std::vector<double>>(system);
117117
for (int step = 0; step < 1000; ++step) {
118118
integrator.step(results[i], 0.01);
119119
}
120120
} else if (i == 1) {
121121
// Euler
122-
auto integrator = diffeq::EulerIntegrator<std::vector<double>, double>(system);
122+
auto integrator = diffeq::EulerIntegrator<std::vector<double>>(system);
123123
for (int step = 0; step < 1000; ++step) {
124124
integrator.step(results[i], 0.01);
125125
}
126126
} else if (i == 2) {
127127
// Improved Euler
128-
auto integrator = diffeq::ImprovedEulerIntegrator<std::vector<double>, double>(system);
128+
auto integrator = diffeq::ImprovedEulerIntegrator<std::vector<double>>(system);
129129
for (int step = 0; step < 1000; ++step) {
130130
integrator.step(results[i], 0.01);
131131
}
@@ -153,7 +153,7 @@ void demo_openmp_parallelism() {
153153
// OpenMP parallel loop
154154
#pragma omp parallel for
155155
for (int i = 0; i < static_cast<int>(states.size()); ++i) {
156-
auto integrator = diffeq::RK4Integrator<std::vector<double>, double>(system);
156+
auto integrator = diffeq::RK4Integrator<std::vector<double>>(system);
157157
for (int step = 0; step < 1000; ++step) {
158158
integrator.step(states[i], 0.01);
159159
}

examples/standard_parallelism_demo.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ODEStdExecution {
6868
initial_conditions.begin(),
6969
initial_conditions.end(),
7070
[&](State& state) {
71-
auto integrator = diffeq::RK4Integrator<State, Time>(system);
71+
auto integrator = diffeq::RK4Integrator<State>(system);
7272
for (int i = 0; i < steps; ++i) {
7373
integrator.step(state, dt);
7474
}
@@ -109,7 +109,7 @@ class ODEStdExecution {
109109
system_template(t, y, dydt, param); // Pass parameter
110110
};
111111

112-
auto integrator = diffeq::RK4Integrator<State, Time>(system);
112+
auto integrator = diffeq::RK4Integrator<State>(system);
113113
for (int step = 0; step < steps; ++step) {
114114
integrator.step(state, dt);
115115
}
@@ -138,7 +138,7 @@ class ODEOpenMP {
138138
// Simple OpenMP parallel loop - no custom classes needed!
139139
#pragma omp parallel for
140140
for (size_t i = 0; i < states.size(); ++i) {
141-
auto integrator = diffeq::RK4Integrator<State, Time>(system);
141+
auto integrator = diffeq::RK4Integrator<State>(system);
142142
for (int step = 0; step < steps; ++step) {
143143
integrator.step(states[i], dt);
144144
}
@@ -169,7 +169,7 @@ class ODEOpenMP {
169169
// RK4 integration
170170
#pragma omp parallel for
171171
for (int i = 0; i < num_runs; ++i) {
172-
auto integrator = diffeq::RK4Integrator<State, Time>(system);
172+
auto integrator = diffeq::RK4Integrator<State>(system);
173173
for (int step = 0; step < steps; ++step) {
174174
integrator.step(rk4_results[i], dt);
175175
}
@@ -181,7 +181,7 @@ class ODEOpenMP {
181181
// Euler integration (for comparison)
182182
#pragma omp parallel for
183183
for (int i = 0; i < num_runs; ++i) {
184-
auto integrator = diffeq::EulerIntegrator<State, Time>(system);
184+
auto integrator = diffeq::EulerIntegrator<State>(system);
185185
for (int step = 0; step < steps; ++step) {
186186
integrator.step(euler_results[i], dt);
187187
}
@@ -208,15 +208,15 @@ class ODETBB {
208208
int steps
209209
) {
210210
// TBB parallel for - handles load balancing automatically
211-
tbb::parallel_for(tbb::blocked_range<size_t>(0, states.size()),
212-
[&](const tbb::blocked_range<size_t>& range) {
213-
for (size_t i = range.begin(); i != range.end(); ++i) {
214-
auto integrator = diffeq::RK4Integrator<State, Time>(system);
215-
for (int step = 0; step < steps; ++step) {
216-
integrator.step(states[i], dt);
217-
}
218-
}
219-
});
211+
tbb::parallel_for(tbb::blocked_range<size_t>(0, states.size()),
212+
[&](const tbb::blocked_range<size_t>& range) {
213+
for (size_t i = range.begin(); i != range.end(); ++i) {
214+
auto integrator = diffeq::RK4Integrator<State>(system);
215+
for (int step = 0; step < steps; ++step) {
216+
integrator.step(states[i], dt);
217+
}
218+
}
219+
});
220220
}
221221

222222
template<typename System>
@@ -231,7 +231,7 @@ class ODETBB {
231231
tbb::parallel_for(tbb::blocked_range<size_t>(0, states.size(), block_size),
232232
[&](const tbb::blocked_range<size_t>& range) {
233233
for (size_t i = range.begin(); i != range.end(); ++i) {
234-
auto integrator = diffeq::RK4Integrator<State, Time>(system);
234+
auto integrator = diffeq::RK4Integrator<State>(system);
235235
for (int step = 0; step < steps; ++step) {
236236
integrator.step(states[i], dt);
237237
}
@@ -276,7 +276,7 @@ class ODETaskDispatcher {
276276
steps,
277277
promise]() mutable {
278278
State state = initial_state;
279-
auto integrator = diffeq::RK4Integrator<State, Time>(system);
279+
auto integrator = diffeq::RK4Integrator<State>(system);
280280

281281
for (int i = 0; i < steps; ++i) {
282282
integrator.step(state, dt);

examples/test_advanced_parallelism.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void test_std_execution_parallelism() {
3737
states.begin(),
3838
states.end(),
3939
[&](std::vector<double>& state) {
40-
auto integrator = diffeq::RK4Integrator<std::vector<double>, double>(system);
40+
auto integrator = diffeq::RK4Integrator<std::vector<double>>(system);
4141
for (int i = 0; i < 100; ++i) {
4242
integrator.step(state, 0.01);
4343
}
@@ -57,7 +57,7 @@ void test_basic_ode_integration() {
5757
auto system = simple_harmonic_oscillator(1.0);
5858
std::vector<double> state = {1.0, 0.0};
5959

60-
auto integrator = diffeq::RK4Integrator<std::vector<double>, double>(system);
60+
auto integrator = diffeq::RK4Integrator<std::vector<double>>(system);
6161

6262
std::cout << "Initial state: [" << state[0] << ", " << state[1] << "]\n";
6363

0 commit comments

Comments
 (0)