Skip to content

Commit bffc99d

Browse files
committed
add output facilities to Integrators
1 parent b9dd7bb commit bffc99d

13 files changed

+6416
-0
lines changed

docs/HIGH_PERFORMANCE_SDE_SUMMARY.md

Lines changed: 422 additions & 0 deletions
Large diffs are not rendered by default.

docs/OUTPUT_FACILITIES_GUIDE.md

Lines changed: 360 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,360 @@
1+
# Enhanced Output Facilities Guide
2+
3+
This guide covers the comprehensive output facilities added to the DiffEq library, providing dense output, interprocess communication, event-driven feedback, and SDE synchronization capabilities.
4+
5+
## Overview
6+
7+
The enhanced output facilities extend the existing composable architecture with powerful new capabilities:
8+
9+
- **Dense Output & Interpolation**: Query system state at arbitrary time points
10+
- **Interprocess Communication**: Real-time data exchange between processes
11+
- **Event-Driven Feedback**: Robotics control and sensor integration
12+
- **SDE Synchronization**: Coordinated noise processes for stochastic systems
13+
14+
All facilities follow the same composable decorator pattern and can be combined in any order.
15+
16+
## Dense Output & Interpolation
17+
18+
### Basic Usage
19+
20+
```cpp
21+
#include <core/composable_integration.hpp>
22+
23+
// Create integrator with dense output
24+
auto dense_integrator = make_builder(std::move(base_integrator))
25+
.with_interpolation(InterpolationConfig{
26+
.method = InterpolationMethod::CUBIC_SPLINE,
27+
.max_history_size = 1000,
28+
.enable_compression = true
29+
})
30+
.build();
31+
32+
// Access interpolation capabilities
33+
auto* interp = dynamic_cast<InterpolationDecorator<State, Time>*>(dense_integrator.get());
34+
35+
// Integrate normally
36+
std::vector<double> state = {1.0, 0.0};
37+
dense_integrator->integrate(state, 0.01, 2.0);
38+
39+
// Query state at arbitrary time
40+
auto interpolated_state = interp->interpolate_at(1.5);
41+
42+
// Get dense output over interval
43+
auto [times, states] = interp->get_dense_output(0.0, 2.0, 100);
44+
```
45+
46+
### Interpolation Methods
47+
48+
- **LINEAR**: Fast linear interpolation
49+
- **CUBIC_SPLINE**: Smooth cubic spline interpolation
50+
- **HERMITE**: Hermite polynomial interpolation
51+
- **AKIMA**: Akima spline (avoids oscillations)
52+
53+
### Configuration Options
54+
55+
```cpp
56+
InterpolationConfig config;
57+
config.method = InterpolationMethod::CUBIC_SPLINE;
58+
config.max_history_size = 10000; // Maximum stored points
59+
config.enable_compression = true; // Compress redundant points
60+
config.compression_tolerance = 1e-8; // Compression error tolerance
61+
config.allow_extrapolation = false; // Allow queries outside bounds
62+
config.extrapolation_warning_threshold = 0.1; // Warn for extrapolation
63+
```
64+
65+
## Interprocess Communication
66+
67+
### Shared Memory Communication
68+
69+
```cpp
70+
// Producer process
71+
InterprocessConfig producer_config;
72+
producer_config.method = IPCMethod::SHARED_MEMORY;
73+
producer_config.direction = IPCDirection::PRODUCER;
74+
producer_config.channel_name = "simulation_data";
75+
producer_config.buffer_size = 1024 * 1024; // 1MB
76+
77+
auto producer = make_builder(std::move(integrator))
78+
.with_interprocess(producer_config)
79+
.build();
80+
81+
// Consumer process
82+
InterprocessConfig consumer_config;
83+
consumer_config.method = IPCMethod::SHARED_MEMORY;
84+
consumer_config.direction = IPCDirection::CONSUMER;
85+
consumer_config.channel_name = "simulation_data";
86+
consumer_config.sync_mode = IPCSyncMode::BLOCKING;
87+
88+
auto consumer = make_builder(std::move(integrator))
89+
.with_interprocess(consumer_config)
90+
.build();
91+
```
92+
93+
### Named Pipes Communication
94+
95+
```cpp
96+
InterprocessConfig config;
97+
config.method = IPCMethod::NAMED_PIPES;
98+
config.direction = IPCDirection::BIDIRECTIONAL;
99+
config.channel_name = "control_channel";
100+
config.enable_acknowledgments = true;
101+
config.max_retries = 3;
102+
103+
auto ipc_integrator = make_builder(std::move(integrator))
104+
.with_interprocess(config)
105+
.build();
106+
```
107+
108+
### IPC Methods
109+
110+
- **SHARED_MEMORY**: Fastest, same-machine communication
111+
- **NAMED_PIPES**: Cross-platform, moderate speed
112+
- **MEMORY_MAPPED_FILE**: Persistent, file-based
113+
- **TCP_SOCKET**: Network-capable (planned)
114+
- **UDP_SOCKET**: Low-latency network (planned)
115+
116+
## Event-Driven Feedback
117+
118+
### Robotics Control Example
119+
120+
```cpp
121+
// Configure for real-time control
122+
EventConfig control_config;
123+
control_config.processing_mode = EventProcessingMode::IMMEDIATE;
124+
control_config.enable_control_loop = true;
125+
control_config.control_loop_period = std::chrono::microseconds{1000}; // 1kHz
126+
control_config.sensor_timeout = std::chrono::microseconds{2000}; // 2ms
127+
control_config.enable_sensor_validation = true;
128+
129+
auto control_system = make_builder(std::move(robot_integrator))
130+
.with_events(control_config)
131+
.build();
132+
133+
auto* events = dynamic_cast<EventDecorator<State, Time>*>(control_system.get());
134+
135+
// Set up safety limits
136+
events->set_threshold_event(0, 1.5, true, [](auto& state, auto time) {
137+
std::cout << "Joint limit exceeded! Emergency stop.\n";
138+
state[1] = 0.0; // Stop motion
139+
});
140+
141+
// Submit sensor data
142+
events->submit_sensor_data("position_sensor", {1.2, 0.8}, 0.98);
143+
144+
// Submit control feedback
145+
std::vector<double> target = {1.0, 0.5};
146+
events->submit_control_feedback("pid_controller", target, current_state);
147+
```
148+
149+
### Event Types
150+
151+
- **TIME_BASED**: Periodic time-triggered events
152+
- **STATE_BASED**: Condition-based state events
153+
- **SENSOR_DATA**: Sensor input events
154+
- **CONTROL_FEEDBACK**: Control loop feedback
155+
- **THRESHOLD_CROSSING**: Value crossing detection
156+
- **CUSTOM**: User-defined events
157+
158+
### Event Priorities
159+
160+
- **LOW**: Background tasks
161+
- **NORMAL**: Standard processing
162+
- **HIGH**: Important events
163+
- **CRITICAL**: Safety-critical events
164+
- **EMERGENCY**: Immediate response required
165+
166+
## SDE Synchronization
167+
168+
### Coordinated Noise Processes
169+
170+
```cpp
171+
#include <core/composable/sde_synchronization.hpp>
172+
173+
// Configure SDE synchronization
174+
SDESyncConfig sync_config;
175+
sync_config.sync_mode = SDESyncMode::BUFFERED;
176+
sync_config.noise_type = NoiseProcessType::WIENER;
177+
sync_config.noise_dimensions = 2;
178+
sync_config.noise_intensity = 0.5;
179+
sync_config.max_noise_delay = std::chrono::microseconds{1000};
180+
181+
// Create synchronized SDE pair
182+
auto [producer, consumer] = SDESynchronizer<State, Time>::create_synchronized_pair(
183+
std::move(noise_generator),
184+
std::move(sde_integrator),
185+
ipc_config,
186+
sync_config
187+
);
188+
```
189+
190+
### Noise Process Types
191+
192+
- **WIENER**: Standard Brownian motion
193+
- **COLORED_NOISE**: Correlated noise processes
194+
- **JUMP_PROCESS**: Jump diffusion processes
195+
- **LEVY_PROCESS**: Lévy processes
196+
- **CUSTOM**: User-defined noise
197+
198+
### Synchronization Modes
199+
200+
- **IMMEDIATE**: Blocking until noise available
201+
- **BUFFERED**: Buffer noise for smooth delivery
202+
- **INTERPOLATED**: Interpolate between samples
203+
- **GENERATED**: Local generation with sync seed
204+
205+
## Simultaneous Multiple Outputs
206+
207+
### Comprehensive Example
208+
209+
```cpp
210+
// Single integrator with ALL output facilities
211+
auto ultimate_integrator = make_builder(std::move(base_integrator))
212+
// Dense output for debugging
213+
.with_interpolation(InterpolationConfig{
214+
.method = InterpolationMethod::CUBIC_SPLINE,
215+
.max_history_size = 1000
216+
})
217+
// Real-time monitoring
218+
.with_output(OutputConfig{
219+
.mode = OutputMode::ONLINE,
220+
.output_interval = std::chrono::microseconds{100000}
221+
}, [](const auto& state, auto t, auto step) {
222+
std::cout << "Monitor: t=" << t << ", state=" << state[0] << "\n";
223+
})
224+
// Event-driven safety
225+
.with_events(EventConfig{
226+
.processing_mode = EventProcessingMode::IMMEDIATE,
227+
.max_event_processing_time = std::chrono::microseconds{500}
228+
})
229+
// IPC communication
230+
.with_interprocess(InterprocessConfig{
231+
.method = IPCMethod::SHARED_MEMORY,
232+
.direction = IPCDirection::PRODUCER,
233+
.channel_name = "realtime_data"
234+
})
235+
// Async processing
236+
.with_async(AsyncConfig{
237+
.thread_pool_size = 2
238+
})
239+
.build();
240+
```
241+
242+
## Performance Considerations
243+
244+
### Dense Output
245+
- Use LINEAR for fastest queries
246+
- Enable compression for memory efficiency
247+
- Limit history size for large simulations
248+
249+
### Interprocess Communication
250+
- SHARED_MEMORY fastest for same-machine
251+
- Named pipes for cross-platform
252+
- Adjust buffer sizes for throughput
253+
254+
### Event Processing
255+
- IMMEDIATE mode for real-time systems
256+
- Limit processing time for hard real-time
257+
- Use priority queues for mixed workloads
258+
259+
### SDE Synchronization
260+
- BUFFERED mode for most applications
261+
- IMMEDIATE for strict synchronization
262+
- Monitor timeout rates for performance
263+
264+
## Real-World Applications
265+
266+
### Robotics Control
267+
```cpp
268+
auto robot_controller = make_builder(std::move(robot_dynamics))
269+
.with_events(EventConfig{.control_loop_period = std::chrono::microseconds{1000}})
270+
.with_interpolation(InterpolationConfig{.method = InterpolationMethod::CUBIC_SPLINE})
271+
.with_output(OutputConfig{.mode = OutputMode::ONLINE})
272+
.build();
273+
```
274+
275+
### Financial Trading
276+
```cpp
277+
auto trading_system = make_builder(std::move(market_model))
278+
.with_events(EventConfig{.max_event_processing_time = std::chrono::microseconds{10}})
279+
.with_interprocess(InterprocessConfig{.method = IPCMethod::SHARED_MEMORY})
280+
.with_interpolation(InterpolationConfig{.method = InterpolationMethod::LINEAR})
281+
.build();
282+
```
283+
284+
### Distributed SDE Simulation
285+
```cpp
286+
// Noise generator process
287+
auto noise_producer = make_builder(std::move(wiener_generator))
288+
.with_interprocess(InterprocessConfig{.direction = IPCDirection::PRODUCER})
289+
.build();
290+
291+
// SDE integrator process
292+
auto sde_consumer = configure_for_external_noise(
293+
std::move(sde_integrator), "wiener_channel");
294+
```
295+
296+
### Scientific Computing
297+
```cpp
298+
auto research_integrator = make_builder(std::move(complex_system))
299+
.with_interpolation(InterpolationConfig{.max_history_size = 100000})
300+
.with_output(OutputConfig{.mode = OutputMode::OFFLINE})
301+
.with_timeout(TimeoutConfig{.timeout_duration = std::chrono::hours{24}})
302+
.build();
303+
```
304+
305+
## Error Handling
306+
307+
### Graceful Degradation
308+
- IPC failures fall back to local processing
309+
- Event timeouts use default handlers
310+
- Interpolation bounds checking with extrapolation warnings
311+
312+
### Debugging Support
313+
- Comprehensive statistics for all facilities
314+
- Event history tracking
315+
- Performance metrics collection
316+
317+
### Validation
318+
- Configuration validation at construction
319+
- Runtime bounds checking
320+
- Thread-safe error propagation
321+
322+
## Advanced Features
323+
324+
### Custom Interpolation
325+
```cpp
326+
class CustomInterpolator : public InterpolationDecorator<State, Time> {
327+
// Implement custom interpolation algorithm
328+
};
329+
```
330+
331+
### Custom IPC Channels
332+
```cpp
333+
class NetworkChannel : public IPCChannel<Time> {
334+
// Implement TCP/UDP networking
335+
};
336+
```
337+
338+
### Custom Event Triggers
339+
```cpp
340+
events->register_event_handler(EventTrigger::CUSTOM,
341+
[](auto& state, auto time) {
342+
// Custom event logic
343+
});
344+
```
345+
346+
## Best Practices
347+
348+
1. **Start Simple**: Begin with single decorators, combine as needed
349+
2. **Profile Performance**: Monitor statistics for bottlenecks
350+
3. **Validate Configurations**: Use config validation early
351+
4. **Handle Errors Gracefully**: Plan for IPC failures and timeouts
352+
5. **Use Appropriate Methods**: Match method to performance requirements
353+
6. **Test Thoroughly**: Use provided unit tests and examples
354+
355+
## See Also
356+
357+
- [Composable Architecture Guide](COMPOSABLE_ARCHITECTURE.md)
358+
- [Performance Optimization](PERFORMANCE_GUIDE.md)
359+
- [Example Programs](../examples/)
360+
- [API Reference](generated/)

0 commit comments

Comments
 (0)