mathhook_core/educational/message_registry/
pde.rs

1//! PDE-specific educational messages integrated with the main message registry
2//!
3//! This module provides educational messages for partial differential equation concepts.
4
5use super::core::{MessageCategory, MessageKey, MessageType};
6use once_cell::sync::Lazy;
7use std::collections::HashMap;
8
9/// Create a PDE message key
10pub fn pde_message_key(message_type: MessageType, variant: u8) -> MessageKey {
11    MessageKey {
12        category: MessageCategory::PartialDifferentialEquation,
13        message_type,
14        variant,
15    }
16}
17
18/// PDE message variants
19pub struct PdeMessageVariant;
20
21impl PdeMessageVariant {
22    // General PDE concepts
23    pub const WHAT_IS_PDE: u8 = 1;
24    pub const PDE_VS_ODE: u8 = 2;
25    pub const PDE_CLASSIFICATION: u8 = 3;
26
27    // PDE types
28    pub const ELLIPTIC_EQUATION: u8 = 4;
29    pub const PARABOLIC_EQUATION: u8 = 5;
30    pub const HYPERBOLIC_EQUATION: u8 = 6;
31
32    // Standard PDEs
33    pub const HEAT_EQUATION: u8 = 7;
34    pub const WAVE_EQUATION: u8 = 8;
35    pub const LAPLACE_EQUATION: u8 = 9;
36    pub const POISSON_EQUATION: u8 = 10;
37
38    // Solution methods
39    pub const SEPARATION_OF_VARIABLES: u8 = 11;
40    pub const METHOD_OF_CHARACTERISTICS: u8 = 12;
41    pub const FOURIER_SERIES: u8 = 13;
42    pub const GREENS_FUNCTIONS: u8 = 14;
43
44    // Boundary conditions
45    pub const DIRICHLET_CONDITION: u8 = 15;
46    pub const NEUMANN_CONDITION: u8 = 16;
47    pub const ROBIN_CONDITION: u8 = 17;
48    pub const PERIODIC_CONDITION: u8 = 18;
49}
50
51/// PDE educational messages
52pub static PDE_MESSAGES: Lazy<HashMap<MessageKey, &'static str>> = Lazy::new(|| {
53    let mut messages = HashMap::new();
54
55    // General PDE concepts
56    messages.insert(
57        pde_message_key(MessageType::Introduction, PdeMessageVariant::WHAT_IS_PDE),
58        "A **Partial Differential Equation (PDE)** is an equation that relates a function of several variables to its partial derivatives. \
59        PDEs describe phenomena involving multiple independent variables, such as heat distribution over time and space, \
60        wave propagation, or electromagnetic fields. The solution to a PDE is a function (or family of functions) \
61        that satisfies the equation and given boundary/initial conditions."
62    );
63
64    messages.insert(
65        pde_message_key(MessageType::Introduction, PdeMessageVariant::PDE_VS_ODE),
66        "**PDEs vs ODEs**: While Ordinary Differential Equations (ODEs) involve functions of a single variable and their derivatives, \
67        PDEs involve functions of multiple variables and their partial derivatives. For example, y'(t) = f(t,y) is an ODE \
68        (one variable t), while ∂u/∂t = α∂²u/∂x² is a PDE (two variables t and x). PDEs are generally more complex to solve \
69        and often require specialized techniques based on their type and boundary conditions."
70    );
71
72    messages.insert(
73        pde_message_key(MessageType::Strategy, PdeMessageVariant::PDE_CLASSIFICATION),
74        "**PDE Classification**: Second-order linear PDEs are classified based on the discriminant B² - 4AC: \
75        **Elliptic** (B² - 4AC < 0): Steady-state problems like Laplace's equation. \
76        **Parabolic** (B² - 4AC = 0): Diffusion processes like the heat equation. \
77        **Hyperbolic** (B² - 4AC > 0): Wave propagation like the wave equation. \
78        This classification determines solution behavior and appropriate numerical methods."
79    );
80
81    // PDE types
82    messages.insert(
83        pde_message_key(MessageType::Introduction, PdeMessageVariant::ELLIPTIC_EQUATION),
84        "**Elliptic PDEs** describe steady-state phenomena where time is not a factor. The Laplace equation ∇²u = 0 \
85        is the prototype, modeling equilibrium states in physics. Solutions are smooth and determined entirely by \
86        boundary conditions. Examples include electrostatic potential, steady heat distribution, and incompressible \
87        fluid flow. Numerical methods like finite elements work well for elliptic problems."
88    );
89
90    messages.insert(
91        pde_message_key(MessageType::Introduction, PdeMessageVariant::PARABOLIC_EQUATION),
92        "**Parabolic PDEs** model diffusion and dissipative processes that evolve toward equilibrium. The heat equation \
93        ∂u/∂t = α∇²u is the classic example, describing how temperature spreads through a material. Solutions smooth out \
94        discontinuities over time and exhibit infinite speed of propagation (disturbances affect the entire domain instantly, \
95        though with exponentially decreasing magnitude). Require initial conditions and boundary conditions."
96    );
97
98    messages.insert(
99        pde_message_key(MessageType::Introduction, PdeMessageVariant::HYPERBOLIC_EQUATION),
100        "**Hyperbolic PDEs** describe wave propagation and vibrations with finite speed. The wave equation ∂²u/∂t² = c²∇²u \
101        is the prototype, modeling sound waves, electromagnetic waves, and vibrating strings. Solutions preserve discontinuities \
102        along characteristics (paths of information propagation). D'Alembert's solution shows waves traveling at speed c. \
103        Require initial position and velocity, plus boundary conditions."
104    );
105
106    // Standard PDEs
107    messages.insert(
108        pde_message_key(MessageType::Introduction, PdeMessageVariant::HEAT_EQUATION),
109        "The **Heat Equation** ∂u/∂t = α∇²u models heat diffusion in materials. Here u(x,t) is temperature, \
110        α is thermal diffusivity. Heat flows from hot to cold regions, smoothing out temperature differences. \
111        Solutions can be found using separation of variables: u(x,t) = X(x)T(t), leading to Fourier series. \
112        The fundamental solution (Green's function) is a Gaussian that spreads over time, showing how point \
113        sources of heat diffuse."
114    );
115
116    messages.insert(
117        pde_message_key(MessageType::Introduction, PdeMessageVariant::WAVE_EQUATION),
118        "The **Wave Equation** ∂²u/∂t² = c²∇²u describes wave propagation at speed c. Solutions include \
119        traveling waves u = f(x - ct) + g(x + ct) (d'Alembert's formula in 1D). Energy is conserved and \
120        waves maintain their shape while traveling. Separation of variables gives standing wave solutions \
121        u(x,t) = sin(nπx/L)cos(nπct/L), representing harmonics of a vibrating string. The wave equation \
122        appears in acoustics, electromagnetics, and quantum mechanics."
123    );
124
125    messages.insert(
126        pde_message_key(MessageType::Introduction, PdeMessageVariant::LAPLACE_EQUATION),
127        "**Laplace's Equation** ∇²u = 0 describes equilibrium states in physics. Solutions (harmonic functions) \
128        have remarkable properties: they satisfy the maximum principle (extrema occur on boundaries), \
129        mean value property (value at a point equals average over any surrounding sphere), and are \
130        infinitely differentiable. Applications include electrostatics (potential), steady heat flow, \
131        incompressible fluid flow, and minimal surfaces. Solved using separation of variables, conformal \
132        mapping, or Green's functions."
133    );
134
135    messages.insert(
136        pde_message_key(MessageType::Introduction, PdeMessageVariant::POISSON_EQUATION),
137        "**Poisson's Equation** ∇²u = f is the inhomogeneous version of Laplace's equation, where f \
138        represents sources or sinks. In electrostatics, f is charge density and u is potential. \
139        Solutions combine the particular solution (accounting for sources) with homogeneous solutions \
140        (satisfying boundary conditions). Green's functions provide explicit integral representations. \
141        The equation appears in gravity (f is mass density), electromagnetism, and fluid mechanics."
142    );
143
144    // Solution methods
145    messages.insert(
146        pde_message_key(MessageType::Strategy, PdeMessageVariant::SEPARATION_OF_VARIABLES),
147        "**Separation of Variables** assumes the solution can be written as a product of single-variable functions: \
148        u(x,t) = X(x)T(t). Substituting into the PDE and dividing yields separate ODEs for each function. \
149        This works when the PDE and boundary conditions are separable. The method produces eigenvalue problems \
150        whose solutions form a complete basis (often trigonometric or special functions). The general solution \
151        is a superposition (Fourier series) of these eigenfunctions."
152    );
153
154    messages.insert(
155        pde_message_key(MessageType::Strategy, PdeMessageVariant::METHOD_OF_CHARACTERISTICS),
156        "The **Method of Characteristics** solves first-order PDEs by finding curves (characteristics) along which \
157        the PDE becomes an ODE. For the equation a∂u/∂x + b∂u/∂y = c, characteristics satisfy dx/a = dy/b = du/c. \
158        The solution is constant along characteristics for homogeneous equations. This method extends to systems \
159        and higher-order hyperbolic equations, revealing how information propagates through the domain. \
160        Wave fronts and shock waves follow characteristics."
161    );
162
163    messages.insert(
164        pde_message_key(MessageType::Calculation, PdeMessageVariant::FOURIER_SERIES),
165        "**Fourier Series** represent periodic functions as infinite sums of sines and cosines. In PDE solutions, \
166        they arise naturally from separation of variables with periodic boundary conditions. For a function on [0,L], \
167        f(x) = a₀/2 + Σ(aₙcos(nπx/L) + bₙsin(nπx/L)). Coefficients are found by orthogonality: \
168        aₙ = (2/L)∫f(x)cos(nπx/L)dx. Fourier series converge to the function (in L² sense) and provide \
169        spectral decomposition, showing which frequencies are present."
170    );
171
172    messages.insert(
173        pde_message_key(MessageType::Strategy, PdeMessageVariant::GREENS_FUNCTIONS),
174        "**Green's Functions** G(x,x';t,t') represent the response at (x,t) to a unit impulse at (x',t'). \
175        They convert PDEs into integral equations: u(x,t) = ∫G(x,x';t,0)f(x')dx' for initial value f. \
176        Green's functions satisfy the PDE with a delta function source and appropriate boundary conditions. \
177        They embody the superposition principle: the solution for any source is the integral of point source \
178        solutions. Finding Green's functions is often difficult but provides complete solution formulas."
179    );
180
181    // Boundary conditions
182    messages.insert(
183        pde_message_key(MessageType::Step, PdeMessageVariant::DIRICHLET_CONDITION),
184        "**Dirichlet Boundary Conditions** specify the value of the solution on the boundary: u|∂Ω = g. \
185        Physically, this fixes temperature (heat equation), displacement (wave equation), or potential \
186        (Laplace equation) at boundaries. For uniqueness, Dirichlet conditions completely determine \
187        elliptic and parabolic solutions. In separation of variables, they determine the eigenvalues \
188        and eigenfunctions. Example: u(0,t) = 0, u(L,t) = 0 for a string fixed at both ends."
189    );
190
191    messages.insert(
192        pde_message_key(MessageType::Step, PdeMessageVariant::NEUMANN_CONDITION),
193        "**Neumann Boundary Conditions** specify the normal derivative on the boundary: ∂u/∂n|∂Ω = h. \
194        This prescribes flux: heat flow (heat equation), velocity (wave equation), or electric field \
195        (Laplace equation). Pure Neumann problems for Laplace's equation are only solvable if ∫h = 0 \
196        (conservation). Solutions are unique up to a constant. Example: ∂u/∂x(0,t) = 0 represents \
197        an insulated boundary (no heat flow)."
198    );
199
200    messages.insert(
201        pde_message_key(MessageType::Step, PdeMessageVariant::ROBIN_CONDITION),
202        "**Robin Boundary Conditions** (mixed/third type) combine Dirichlet and Neumann: αu + β∂u/∂n = γ. \
203        They model realistic boundaries like convective heat transfer: -k∂u/∂n = h(u - u∞), where heat \
204        flux is proportional to temperature difference. Robin conditions often arise from coupling PDEs \
205        across interfaces. They ensure unique solutions for elliptic problems when α and β have appropriate \
206        signs. Eigenvalue problems with Robin conditions have discrete spectra."
207    );
208
209    messages.insert(
210        pde_message_key(MessageType::Step, PdeMessageVariant::PERIODIC_CONDITION),
211        "**Periodic Boundary Conditions** require u(x + L) = u(x) and ∂u/∂x(x + L) = ∂u/∂x(x), \
212        making the solution periodic with period L. They model systems on circles, tori, or with \
213        translational symmetry. Eigenfunctions are complex exponentials e^(2πinx/L) or sines/cosines. \
214        Periodic conditions lead naturally to Fourier series representations. Applications include \
215        crystal lattices, circular membranes, and periodic wave guides."
216    );
217
218    messages
219});
220
221#[cfg(test)]
222mod tests {
223    use super::*;
224
225    #[test]
226    fn test_pde_messages_loaded() {
227        assert!(!PDE_MESSAGES.is_empty());
228
229        // Test some specific messages exist
230        let heat_key = pde_message_key(MessageType::Introduction, PdeMessageVariant::HEAT_EQUATION);
231        assert!(PDE_MESSAGES.contains_key(&heat_key));
232
233        let wave_key = pde_message_key(MessageType::Introduction, PdeMessageVariant::WAVE_EQUATION);
234        assert!(PDE_MESSAGES.contains_key(&wave_key));
235    }
236
237    #[test]
238    fn test_pde_message_content() {
239        let key = pde_message_key(MessageType::Introduction, PdeMessageVariant::HEAT_EQUATION);
240        let message = PDE_MESSAGES.get(&key).unwrap();
241        assert!(message.contains("Heat Equation"));
242        assert!(message.contains("∂u/∂t = α∇²u"));
243    }
244
245    #[test]
246    fn test_all_pde_categories() {
247        let intro_key = pde_message_key(MessageType::Introduction, PdeMessageVariant::WHAT_IS_PDE);
248        let strategy_key = pde_message_key(
249            MessageType::Strategy,
250            PdeMessageVariant::SEPARATION_OF_VARIABLES,
251        );
252        let step_key = pde_message_key(MessageType::Step, PdeMessageVariant::DIRICHLET_CONDITION);
253        let calc_key = pde_message_key(MessageType::Calculation, PdeMessageVariant::FOURIER_SERIES);
254
255        assert!(PDE_MESSAGES.contains_key(&intro_key));
256        assert!(PDE_MESSAGES.contains_key(&strategy_key));
257        assert!(PDE_MESSAGES.contains_key(&step_key));
258        assert!(PDE_MESSAGES.contains_key(&calc_key));
259    }
260}