Skip to content

Commit 729c826

Browse files
committed
move all the hpp files in include/examples to examples
1 parent 5e69791 commit 729c826

11 files changed

+1133
-1324
lines changed

docs/STANDARD_PARALLELISM.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This document shows how to use standard parallelism libraries with the diffeq li
77
For most users, the simplest approach is to use the unified convenience interface:
88

99
```cpp
10-
#include <examples/standard_parallelism.hpp>
10+
#include <diffeq.hpp>
1111

1212
// SIMPLEST USAGE: Just add parallel to existing code
1313
auto system = [](double t, const std::vector<double>& y, std::vector<double>& dydt) {
@@ -56,7 +56,7 @@ Instead of creating custom parallel classes, we recommend using established stan
5656
### 1. Basic Parallel Integration with std::execution
5757
5858
```cpp
59-
#include <examples/standard_parallelism.hpp>
59+
#include <diffeq.hpp>
6060
#include <execution>
6161
#include <algorithm>
6262

examples/README.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# diffeq Examples
2+
3+
This directory contains comprehensive examples demonstrating how to use the diffeq library for solving differential equations.
4+
5+
## Example Programs
6+
7+
### Core Integration Examples
8+
9+
- **`working_integrators_demo.cpp`** - Demonstrates all working ODE integrators (RK4, RK23, RK45, BDF, LSODA)
10+
- **`rk4_integrator_usage.cpp`** - Basic RK4 integrator usage with various ODE systems
11+
- **`advanced_integrators_usage.cpp`** - Advanced integrator features and configurations
12+
- **`state_concept_usage.cpp`** - Shows how to use different state types (vectors, arrays, custom types)
13+
14+
### Parallelism Examples
15+
16+
- **`parallelism_usage_demo.cpp`** - Comprehensive parallelism features including:
17+
- Quick start parallel interface
18+
- Robotics control systems with real-time parallelism
19+
- Stochastic process research with GPU-accelerated Monte Carlo
20+
- Multi-hardware target benchmarking
21+
- **`standard_parallelism_demo.cpp`** - Standard library parallelism integration:
22+
- C++17/20 std::execution policies
23+
- OpenMP parallel loops
24+
- Intel TBB integration
25+
- Task-based async dispatchers
26+
- **`simple_standard_parallelism.cpp`** - Simplified parallel usage patterns
27+
- **`standard_parallelism_demo.cpp`** - Advanced standard parallelism features
28+
- **`simplified_parallel_usage.cpp`** - Easy-to-use parallel interfaces
29+
- **`test_advanced_parallelism.cpp`** - Testing advanced parallelism features
30+
31+
### Advanced Features
32+
33+
- **`interface_usage_demo.cpp`** - Integration interface examples:
34+
- Financial portfolio modeling with signal processing
35+
- Robotics control with real-time feedback
36+
- Scientific simulations with parameter updates
37+
- **`sde_usage_demo.cpp`** - Stochastic Differential Equation examples:
38+
- Black-Scholes financial models
39+
- Heston stochastic volatility
40+
- Noisy oscillator control systems
41+
- Stochastic Lotka-Volterra ecosystem models
42+
- **`advanced_gpu_async_demo.cpp`** - GPU acceleration with async processing
43+
- **`realtime_signal_processing.cpp`** - Real-time signal processing integration
44+
45+
### Testing and Validation
46+
47+
- **`quick_test.cpp`** - Quick validation tests
48+
- **`test_dop853.cpp`** - DOP853 integrator testing
49+
- **`test_rk4_only.cpp`** - RK4 integrator testing
50+
- **`sde_demo.cpp`** - Basic SDE demonstration
51+
52+
## Building and Running Examples
53+
54+
### Prerequisites
55+
56+
- C++17 or later compiler
57+
- CMake or xmake build system
58+
- Optional: OpenMP, Intel TBB, CUDA for advanced parallelism examples
59+
60+
### Building
61+
62+
```bash
63+
# Using xmake (recommended)
64+
xmake
65+
66+
# Or using CMake
67+
mkdir build && cd build
68+
cmake ..
69+
make
70+
```
71+
72+
### Running Examples
73+
74+
```bash
75+
# Run a specific example
76+
./examples/working_integrators_demo
77+
78+
# Run parallelism examples
79+
./examples/parallelism_usage_demo
80+
./examples/standard_parallelism_demo
81+
82+
# Run SDE examples
83+
./examples/sde_usage_demo
84+
85+
# Run interface examples
86+
./examples/interface_usage_demo
87+
```
88+
89+
## Example Categories
90+
91+
### 1. Basic Usage
92+
Start with these examples to understand the fundamentals:
93+
- `working_integrators_demo.cpp`
94+
- `rk4_integrator_usage.cpp`
95+
- `state_concept_usage.cpp`
96+
97+
### 2. Parallelism
98+
For performance-critical applications:
99+
- `parallelism_usage_demo.cpp` - Full-featured parallelism
100+
- `standard_parallelism_demo.cpp` - Standard library integration
101+
- `simple_standard_parallelism.cpp` - Easy parallel usage
102+
103+
### 3. Advanced Features
104+
For complex applications:
105+
- `interface_usage_demo.cpp` - Signal processing and real-time integration
106+
- `sde_usage_demo.cpp` - Stochastic differential equations
107+
- `advanced_gpu_async_demo.cpp` - GPU acceleration
108+
109+
### 4. Domain-Specific Examples
110+
- **Finance**: Black-Scholes, Heston models in `sde_usage_demo.cpp`
111+
- **Robotics**: Control systems in `parallelism_usage_demo.cpp`
112+
- **Scientific**: Chemical reactions, ecosystem models in `sde_usage_demo.cpp`
113+
114+
## Key Features Demonstrated
115+
116+
### Integration Methods
117+
- **ODE Solvers**: RK4, RK23, RK45, BDF, LSODA, DOP853
118+
- **SDE Solvers**: Euler-Maruyama, Milstein, SRA1, SOSRA, SRIW1, SOSRI
119+
- **Adaptive Methods**: Automatic step size control
120+
- **Stiff Systems**: BDF and LSODA for stiff problems
121+
122+
### Parallelism
123+
- **CPU Parallelism**: std::execution, OpenMP, Intel TBB
124+
- **GPU Acceleration**: CUDA, Thrust integration
125+
- **Async Processing**: Task-based parallel execution
126+
- **Real-time Control**: Low-latency parallel integration
127+
128+
### Advanced Features
129+
- **Signal Processing**: Real-time event handling
130+
- **Parameter Sweeps**: Parallel parameter studies
131+
- **Multi-physics**: Coupled system integration
132+
- **Hardware Optimization**: Automatic backend selection
133+
134+
## Best Practices
135+
136+
1. **Start Simple**: Begin with `working_integrators_demo.cpp` to understand basic usage
137+
2. **Choose the Right Integrator**: Use RK45 for general problems, BDF for stiff systems
138+
3. **Leverage Parallelism**: Use parallel examples for performance-critical applications
139+
4. **Handle Real-time Requirements**: Use interface examples for systems with external signals
140+
5. **Validate Results**: Compare with analytical solutions when available
141+
142+
## Troubleshooting
143+
144+
### Common Issues
145+
- **Compilation Errors**: Ensure C++17 support and required libraries
146+
- **Performance Issues**: Check parallel backend availability
147+
- **Accuracy Problems**: Verify integrator choice and tolerances
148+
- **Memory Issues**: Use appropriate state types and batch sizes
149+
150+
### Getting Help
151+
- Check the main library documentation
152+
- Review the test suite for usage patterns
153+
- Examine the source code for implementation details
154+
155+
## Contributing
156+
157+
When adding new examples:
158+
1. Follow the existing naming convention
159+
2. Include comprehensive comments
160+
3. Demonstrate realistic use cases
161+
4. Add to this README if appropriate
162+
5. Ensure the example compiles and runs correctly

include/examples/interface_usage.hpp renamed to examples/interface_usage_demo.cpp

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#pragma once
2-
31
#include <interfaces/integration_interface.hpp>
2+
#include <diffeq.hpp>
43
#include <vector>
54
#include <array>
65
#include <string>
@@ -176,6 +175,8 @@ auto create_scientific_interface() {
176175
*/
177176
template<system_state StateType>
178177
void demonstrate_usage(StateType initial_state) {
178+
std::cout << "\n=== Integration Interface Usage Demo ===" << std::endl;
179+
179180
// Create interface for your domain (finance example)
180181
auto interface = create_portfolio_interface<StateType>();
181182

@@ -198,22 +199,104 @@ void demonstrate_usage(StateType initial_state) {
198199
auto signal_proc = interface->get_signal_processor();
199200

200201
StateType state = initial_state;
202+
double t_start = 0.0, t_end = 1.0, dt = 0.1;
201203

202-
// Integration with signal processing
203-
for (int step = 0; step < 100; ++step) {
204-
integrator.integrate(state, 0.01, 0.01 * step);
205-
206-
// Simulate external signals
207-
if (step == 25) {
208-
signal_proc->emit_signal("price_update", 105.0);
204+
std::cout << "Initial portfolio state: [" << state[0] << ", " << state[1] << ", " << state[2] << "]" << std::endl;
205+
206+
// Integrate with signal processing
207+
for (double t = t_start; t < t_end; t += dt) {
208+
// Simulate market signals
209+
if (t > 0.3 && t < 0.4) {
210+
signal_proc->emit_signal("price_update", 110.0);
209211
}
210-
if (step == 50) {
212+
if (t > 0.7) {
211213
signal_proc->emit_signal("risk_alert", std::string("high_volatility"));
212214
}
213-
if (step == 75) {
214-
signal_proc->emit_signal("price_update", 95.0);
215-
}
215+
216+
// Integrate one step
217+
integrator->step(state, dt);
216218
}
219+
220+
std::cout << "Final portfolio state: [" << state[0] << ", " << state[1] << ", " << state[2] << "]" << std::endl;
221+
std::cout << "Integration completed successfully!" << std::endl;
217222
}
218223

219224
} // namespace diffeq::examples
225+
226+
int main() {
227+
std::cout << "=== diffeq Integration Interface Examples ===" << std::endl;
228+
229+
// Example with vector state
230+
std::vector<double> initial_portfolio = {1000.0, 2000.0, 1500.0};
231+
diffeq::examples::demonstrate_usage(initial_portfolio);
232+
233+
std::cout << "\n=== Robotics Interface Example ===" << std::endl;
234+
235+
// Create robotics interface
236+
auto robotics_interface = diffeq::examples::create_robotics_interface<std::vector<double>>();
237+
238+
// Define robot dynamics
239+
auto robot_ode = [](double t, const std::vector<double>& y, std::vector<double>& dydt) {
240+
// Simple double integrator: d²θ/dt² = u
241+
dydt[0] = y[1]; // dθ/dt = ω
242+
dydt[1] = -0.1 * y[0] - 0.5 * y[1]; // Simple PD control
243+
};
244+
245+
auto signal_aware_robot_ode = robotics_interface->make_signal_aware_ode(robot_ode);
246+
auto robot_integrator = diffeq::make_rk4<std::vector<double>>(signal_aware_robot_ode);
247+
248+
std::vector<double> robot_state = {0.1, 0.0}; // [angle, angular_velocity]
249+
auto robot_signal_proc = robotics_interface->get_signal_processor();
250+
251+
std::cout << "Initial robot state: angle=" << robot_state[0] << " rad, velocity=" << robot_state[1] << " rad/s" << std::endl;
252+
253+
// Simulate robot control
254+
for (double t = 0.0; t < 2.0; t += 0.01) {
255+
if (t > 0.5) {
256+
robot_signal_proc->emit_signal("control_command", std::vector<double>{0.5});
257+
}
258+
if (t > 1.5) {
259+
robot_signal_proc->emit_signal("emergency_stop", true);
260+
}
261+
262+
robot_integrator->step(robot_state, 0.01);
263+
}
264+
265+
std::cout << "Final robot state: angle=" << robot_state[0] << " rad, velocity=" << robot_state[1] << " rad/s" << std::endl;
266+
267+
std::cout << "\n=== Scientific Interface Example ===" << std::endl;
268+
269+
// Create scientific interface
270+
auto scientific_interface = diffeq::examples::create_scientific_interface<std::vector<double>>();
271+
272+
// Define scientific system (e.g., chemical reaction)
273+
auto chemical_ode = [](double t, const std::vector<double>& y, std::vector<double>& dydt) {
274+
// Simple chemical reaction: A -> B
275+
double k = 0.1; // reaction rate
276+
dydt[0] = -k * y[0]; // dA/dt = -k*A
277+
dydt[1] = k * y[0]; // dB/dt = k*A
278+
};
279+
280+
auto signal_aware_chemical_ode = scientific_interface->make_signal_aware_ode(chemical_ode);
281+
auto chemical_integrator = diffeq::make_rk45<std::vector<double>>(signal_aware_chemical_ode);
282+
283+
std::vector<double> chemical_state = {1.0, 0.0}; // [A, B]
284+
auto chemical_signal_proc = scientific_interface->get_signal_processor();
285+
286+
std::cout << "Initial chemical state: A=" << chemical_state[0] << ", B=" << chemical_state[1] << std::endl;
287+
288+
// Simulate chemical reaction with parameter updates
289+
for (double t = 0.0; t < 10.0; t += 0.1) {
290+
if (t > 5.0) {
291+
chemical_signal_proc->emit_signal("parameter_update", 0.2); // Increase reaction rate
292+
}
293+
294+
chemical_integrator->step(chemical_state, 0.1);
295+
}
296+
297+
std::cout << "Final chemical state: A=" << chemical_state[0] << ", B=" << chemical_state[1] << std::endl;
298+
299+
std::cout << "\n=== All examples completed successfully! ===" << std::endl;
300+
301+
return 0;
302+
}

0 commit comments

Comments
 (0)